// SPDX-License-Identifier: MPL-2.0
#ifndef SH_LIB_IDT_H
#define SH_LIB_IDT_H
#include "std/status.h"
#include "std/type.h"
// IDT entry structure
#pragma pack(1)
typedef struct {
  sh_uint16 offset_low;
  sh_uint16 selector;
  sh_uint8 ist;
  sh_uint8 type_attr;
  sh_uint16 offset_mid;
  sh_uint32 offset_high;
  sh_uint32 reserved;
} sh_idt_IDT_ENTRY;
#pragma pack()
// IDTR structure
#pragma pack(1)
typedef struct {
  sh_uint16 limit;
  sh_uint64 base;
} sh_idt_IDTR;
#pragma pack()
// Full IDT structure
#pragma pack(1)
typedef struct {
  sh_idt_IDT_ENTRY entries[256];
} sh_idt_IDT;
#pragma pack()
// Generate type_attr byte
inline sh_uint8 sh_idt_fill_type_attr_byte(sh_bool present,sh_uint8 dpl,sh_uint8 type) {
  return ((present?1:0)<<7)|((dpl&0x3)<<5)|(type&0x1F);
}
// Fill an IDT entry, expect type_attr as sh_idt_fill_type_attr_byte would return it
sh_idt_IDT_ENTRY sh_idt_fill_idt_entry(sh_uint64 offset,sh_uint16 selector,sh_uint8 ist,sh_uint8 type_attr);
// Fill the IDT
SH_STATUS sh_idt_fill_idt(sh_idt_IDT *idt);
// Fill and load IDTR
SH_STATUS sh_idt_load_idtr(sh_idt_IDT *idt);
#endif
