summaryrefslogtreecommitdiff
path: root/emulator/csr.c
diff options
context:
space:
mode:
Diffstat (limited to 'emulator/csr.c')
-rw-r--r--emulator/csr.c51
1 files changed, 51 insertions, 0 deletions
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 <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);
+}
+