#include "main.h"
#include "riscv.h"
#include "stdint.h"
#include "timer.h"
#include "vsrv.h"

// extern void timervec();
extern void trap_entry();

void start() {
    uint32_t *tmr  = (uint32_t *)(void *)VSRV_TMR_BASE;

    // set M Exception Program Counter to main, for mret.
    // requires gcc -mcmodel=medany
    w_mepc((uint32_t)main);

    // keep each CPU's hartid in its tp register, for cpuid().
    int id = r_mhartid();
    w_tp(id);

    tmr[TMR_CTRL0>>2] = (1<<2);
    timer_init();

    // jump to main().
    asm volatile("mret");
}
