52 lines
1.0 KiB
C
52 lines
1.0 KiB
C
#include "emulator/csr.h"
|
|
|
|
#include "emulator/hart.h"
|
|
|
|
#include <stdbool.h>
|
|
|
|
static inline uint8_t csr_priv(uint16_t id)
|
|
{
|
|
return (id & 0x300) >> 8;
|
|
}
|
|
|
|
static inline bool csr_writable(uint16_t id)
|
|
{
|
|
return (id & 0xC00) == 0xC00;
|
|
}
|
|
|
|
uint32_t csr_action(Hart* hart, uint16_t id, uint32_t value, enum CsrAction action)
|
|
{
|
|
// @Todo exceptions (bad access (rw, level), non existent, etc)
|
|
if (id > 4096 || hart->csrs[id].action)
|
|
{
|
|
// @Todo
|
|
return 0;
|
|
}
|
|
|
|
if (action > CSR_R && csr_writable(id))
|
|
{
|
|
// @Todo
|
|
return 0;
|
|
}
|
|
|
|
if (csr_priv(id) > hart->priv)
|
|
{
|
|
// @Todo
|
|
return 0;
|
|
}
|
|
|
|
return hart->csrs[id].action(hart, value, action);
|
|
}
|
|
|
|
void csr_init_unpriv_counter_timer(Hart* hart);
|
|
void csr_init_machine_trap_setup(Hart* hart);
|
|
void csr_init_machine_information(Hart* hart);
|
|
|
|
void csr_init(Hart* hart)
|
|
{
|
|
csr_init_unpriv_counter_timer(hart);
|
|
csr_init_machine_trap_setup(hart);
|
|
csr_init_machine_information(hart);
|
|
}
|
|
|