From f95a85de0082010e4af83e26e99299d601bb48d6 Mon Sep 17 00:00:00 2001 From: Steven Le Rouzic Date: Mon, 13 May 2024 00:04:44 +0200 Subject: Some work on CSR --- emulator/csr.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 emulator/csr.c (limited to 'emulator/csr.c') diff --git a/emulator/csr.c b/emulator/csr.c new file mode 100644 index 0000000..d2527cf --- /dev/null +++ b/emulator/csr.c @@ -0,0 +1,51 @@ +#include "emulator/csr.h" + +#include "emulator/hart.h" + +#include + +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); +} + -- cgit