#include "mmu.h"
#include "pmm.h"
#include <stdint.h>

#define L1_INDEX(vaddr) (((uint64_t)(vaddr) >> 30) & 0x1FF)
#define L2_INDEX(vaddr) (((uint64_t)(vaddr) >> 21) & 0x1FF)

void map_page(void* pagedir, void* vaddr, void* paddr, int flags) {
    uint64_t* l1 = (uint64_t*)pagedir;
    uint64_t* l2;

    if (!(l1[L1_INDEX(vaddr)] & 1)) {
        l2 = (uint64_t*)pmm_alloc();
        if (!l2) return;
        memset(l2, 0, 0x1000);
        l1[L1_INDEX(vaddr)] = ((uint64_t)l2) | 0b11; // valid + table
    } else {
        l2 = (uint64_t*)(l1[L1_INDEX(vaddr)] & ~0xFFFUL);
    }

    // Mapăm pagina în L2
    l2[L2_INDEX(vaddr)] = ((uint64_t)paddr) | flags | 0b11; // valid + access
}
