/*----------------*\
|Nanite OS         |
|Copyright (C) 2026|
|Xircon            |
\*----------------*/
#include "page.h"

#include <stdio.h>
#include <stdint.h>
#include <dri/serial.h>
#include <arch/i686/util.h>

extern uint16_t DEBUG_COM_PORT;

uint32_t Page_Directory[1024] __attribute__((aligned(4096)));
uint32_t Page_Table[1024] __attribute__((aligned(4096)));

void Memory_Page_Init()
{
    unsigned long address = 0;
    unsigned int PDi;

    Serial_Printf(DEBUG_COM_PORT, "MEMORY:> Inititalizing Memory Paging\n");
    // Set entries to not Present
    for(PDi=0; PDi<1024; PDi++) {
        Page_Directory[PDi] = 0x00000002;
    }
    // Set R/W + Supervisor Flags For Table
    unsigned int PTi;
    for(PTi=0; PTi<1024; PTi++) {
        Page_Table[PTi] = (PTi * 0x1000) | 3;
    }
    // Add Table to Directory & Set R/W Supervisor
    Page_Directory[0] = ((unsigned int)Page_Table) | 3;

    // Enable Paging
    Write_CR3(Page_Directory);
    Write_CR0(Read_CR0() | 0x80000000);
}