aboutsummaryrefslogtreecommitdiff
path: root/sim/gentestvec/src/register_file.c
diff options
context:
space:
mode:
authorFlavian Kaufmann <flavian@flaviankaufmann.ch>2024-05-21 13:50:28 +0200
committerFlavian Kaufmann <flavian@flaviankaufmann.ch>2024-05-21 13:50:28 +0200
commitcb0be9e2039569ee7d18657e8f675d1f8369b407 (patch)
tree91fa71b3960d1ad5217759371143efbdd833d475 /sim/gentestvec/src/register_file.c
parent98d0dd96611dc2c0e444eaf9410f8adf2924c6b5 (diff)
downloadriscv_cpu-cb0be9e2039569ee7d18657e8f675d1f8369b407.tar.gz
riscv_cpu-cb0be9e2039569ee7d18657e8f675d1f8369b407.zip
restructured project
Diffstat (limited to 'sim/gentestvec/src/register_file.c')
-rw-r--r--sim/gentestvec/src/register_file.c49
1 files changed, 49 insertions, 0 deletions
diff --git a/sim/gentestvec/src/register_file.c b/sim/gentestvec/src/register_file.c
new file mode 100644
index 0000000..5a50d9b
--- /dev/null
+++ b/sim/gentestvec/src/register_file.c
@@ -0,0 +1,49 @@
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+
+uint32_t registers[32];
+
+uint32_t read_reg(uint32_t addr) { return addr == 0 ? 0 : registers[addr]; }
+
+void write_reg(uint32_t addr, uint32_t data, bool we) {
+ if (addr != 0 && we)
+ registers[addr] = data;
+}
+
+void test(uint32_t addr_rs0, uint32_t addr_rs1, uint32_t addr_rd2,
+ uint32_t data_rd2, bool we) {
+
+ write_reg(addr_rd2, data_rd2, we);
+ uint32_t data_rs0 = read_reg(addr_rs0);
+ uint32_t data_rs1 = read_reg(addr_rs1);
+ printf("%08X_%08X__%08X_%08X__%08X_%08X_%01X\n", addr_rs0, data_rs0, addr_rs1,
+ data_rs1, addr_rd2, data_rd2, we);
+}
+
+void tests(int num) {
+ for (int i = 0; i < num; ++i) {
+ uint32_t addr_rs0 = ((uint32_t)((rand() << 16) | rand())) % 32;
+ uint32_t addr_rs1 = ((uint32_t)((rand() << 16) | rand())) % 32;
+ uint32_t addr_rd2 = ((uint32_t)((rand() << 16) | rand())) % 32;
+ uint32_t data_rd2 = ((uint32_t)((rand() << 16) | rand()));
+ bool we = ((uint32_t)rand()) % 2;
+ test(addr_rs0, addr_rs1, addr_rd2, data_rd2, we);
+ }
+}
+
+int main(int argc, const char *argv[]) {
+ srand(time(NULL));
+ for (int i = 0; i < 32; ++i)
+ registers[0] = 0;
+
+ for (int i = 0; i < 32; ++i)
+ test(i, i, 0, 0, false);
+
+ for (int i = 0; i < 32; ++i)
+ test(i, i, i, 0xffffffff, true);
+
+ tests(10000);
+ return 0;
+}