#ifndef GDT_H
#define GDT_H

#include <stdint.h>

// the GDT descriptor format makes literally no sense, who came up with this
typedef struct {
    uint16_t limit_low16; // Low 16 bits of limit
    uint16_t base_low16;  // Low 16 bits of base
    uint8_t  base_mid8;   // Middle 8 bits of base
    uint8_t  access;      // 7: present, 6-5: priv level, 4: type, 3: executable, 2: dc, 1: rw, 0: accessed (zero)
    uint8_t  limit_flags; // High 4 bits of limit AND flags (?? why). flags: 3: granularity, 2: size, 1: long, 0: reserved
    uint8_t  base_high8;  // High 8 bits of base
} __attribute__((packed)) gdt_entry_t;

typedef struct {
    uint16_t size;   // GDT size (bytes) - 1
    uint32_t offset; // Address of element 0 of the GDT
} __attribute__((packed)) gdt_ptr_t;

static gdt_entry_t gdt[8192];
static gdt_ptr_t gdtr;

void init_gdt();
void gdt_set_segment(int limit, int base, int access);
void load_gdt();

#endif