aboutsummaryrefslogtreecommitdiff
path: root/customext
diff options
context:
space:
mode:
authorChih-Min Chao <chihmin.chao@sifive.com>2020-06-08 00:36:37 -0700
committerChih-Min Chao <chihmin.chao@sifive.com>2020-06-10 11:40:57 -0700
commitfefd356697bbbe732c22b0e6dc6121b8aeca2946 (patch)
tree71eb390d092e7615e3ddcbd204a5760372fbe35a /customext
parent33a6eb57564c257037780ddd2691ca621c44a55b (diff)
downloadspike-fefd356697bbbe732c22b0e6dc6121b8aeca2946.zip
spike-fefd356697bbbe732c22b0e6dc6121b8aeca2946.tar.gz
spike-fefd356697bbbe732c22b0e6dc6121b8aeca2946.tar.bz2
ext: rename libdummy_rocc by libcustomext
make library name general for multiple custom extension built in one shared library. Signed-off-by: Chih-Min Chao <chihmin.chao@sifive.com>
Diffstat (limited to 'customext')
-rw-r--r--customext/customext.ac0
-rw-r--r--customext/customext.mk.in11
-rw-r--r--customext/dummy_rocc.cc47
-rw-r--r--customext/dummy_rocc_test.c29
4 files changed, 87 insertions, 0 deletions
diff --git a/customext/customext.ac b/customext/customext.ac
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/customext/customext.ac
diff --git a/customext/customext.mk.in b/customext/customext.mk.in
new file mode 100644
index 0000000..1bf336c
--- /dev/null
+++ b/customext/customext.mk.in
@@ -0,0 +1,11 @@
+customext_subproject_deps = \
+ spike_main \
+ riscv \
+ softfloat \
+
+customext_srcs = \
+ dummy_rocc.cc \
+
+customext_CFLAGS = -fPIC
+
+customext_install_shared_lib = yes
diff --git a/customext/dummy_rocc.cc b/customext/dummy_rocc.cc
new file mode 100644
index 0000000..85ab7aa
--- /dev/null
+++ b/customext/dummy_rocc.cc
@@ -0,0 +1,47 @@
+#include "rocc.h"
+#include "mmu.h"
+#include <cstring>
+
+class dummy_rocc_t : public rocc_t
+{
+ public:
+ const char* name() { return "dummy_rocc"; }
+
+ reg_t custom0(rocc_insn_t insn, reg_t xs1, reg_t xs2)
+ {
+ reg_t prev_acc = acc[insn.rs2];
+
+ if (insn.rs2 >= num_acc)
+ illegal_instruction();
+
+ switch (insn.funct)
+ {
+ case 0: // acc <- xs1
+ acc[insn.rs2] = xs1;
+ break;
+ case 1: // xd <- acc (the only real work is the return statement below)
+ break;
+ case 2: // acc[rs2] <- Mem[xs1]
+ acc[insn.rs2] = p->get_mmu()->load_uint64(xs1);
+ break;
+ case 3: // acc[rs2] <- accX + xs1
+ acc[insn.rs2] += xs1;
+ break;
+ default:
+ illegal_instruction();
+ }
+
+ return prev_acc; // in all cases, xd <- previous value of acc[rs2]
+ }
+
+ dummy_rocc_t()
+ {
+ memset(acc, 0, sizeof(acc));
+ }
+
+ private:
+ static const int num_acc = 4;
+ reg_t acc[num_acc];
+};
+
+REGISTER_EXTENSION(dummy_rocc, []() { return new dummy_rocc_t; })
diff --git a/customext/dummy_rocc_test.c b/customext/dummy_rocc_test.c
new file mode 100644
index 0000000..94de8c0
--- /dev/null
+++ b/customext/dummy_rocc_test.c
@@ -0,0 +1,29 @@
+// The following is a RISC-V program to test the functionality of the
+// dummy RoCC accelerator.
+// Compile with riscv64-unknown-elf-gcc dummy_rocc_test.c
+// Run with spike --extension=dummy_rocc pk a.out
+
+#include <assert.h>
+#include <stdio.h>
+#include <stdint.h>
+
+int main() {
+ uint64_t x = 123, y = 456, z = 0;
+ // load x into accumulator 2 (funct=0)
+ asm volatile ("custom0 x0, %0, 2, 0" : : "r"(x));
+ // read it back into z (funct=1) to verify it
+ asm volatile ("custom0 %0, x0, 2, 1" : "=r"(z));
+ assert(z == x);
+ // accumulate 456 into it (funct=3)
+ asm volatile ("custom0 x0, %0, 2, 3" : : "r"(y));
+ // verify it
+ asm volatile ("custom0 %0, x0, 2, 1" : "=r"(z));
+ assert(z == x+y);
+ // do it all again, but initialize acc2 via memory this time (funct=2)
+ asm volatile ("custom0 x0, %0, 2, 2" : : "r"(&x));
+ asm volatile ("custom0 x0, %0, 2, 3" : : "r"(y));
+ asm volatile ("custom0 %0, x0, 2, 1" : "=r"(z));
+ assert(z == x+y);
+
+ printf("success!\n");
+}