#include "sysinfo.h"

#include "../kklibc/mem.h"
#include "../kklibc/paging.h"
#include "../kklibc/stdio.h"
#include "../kklibc/stdlib.h"

static system_info_t sys_info;

void detect_cpu(void) {
    u32 eax, ebx, ecx, edx;
    char vendor[13];

    __asm__ volatile("cpuid" : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx) : "a"(0));
    *((u32*)vendor) = ebx;
    *((u32*)(vendor + 4)) = edx;
    *((u32*)(vendor + 8)) = ecx;
    vendor[12] = '\0';
    strcpy(sys_info.cpu_vendor, vendor);

    __asm__ volatile("cpuid" : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx) : "a"(1));
    sys_info.cpu_cores = 1;
}

void detect_memory() {
    meminfo_t info = get_meminfo();
    sys_info.total_memory = nframes * PAGE_SIZE;

    u32 used_frames = 0;
    for (u32 i = 0; i < nframes; i++) {
        if (test_frame(i * PAGE_SIZE)) {
            used_frames++;
        }
    }
    sys_info.used_memory = used_frames * PAGE_SIZE;
    sys_info.free_memory = sys_info.total_memory - sys_info.used_memory;

    // Получение информации о heap из менеджера памяти
    sys_info.kernel_memory = free_mem_addr - HEAP_START;
    sys_info.heap_size = heap_current_end - HEAP_START;
    sys_info.heap_used = info.total_used;
    sys_info.heap_free = info.total_free;
}

system_info_t* get_system_info() {
    detect_memory();
    return &sys_info;
}
