aboutsummaryrefslogtreecommitdiff
path: root/customext/dummy_rocc_test.c
blob: 94de8c041e77e615c1d1ff7f433db5af14421753 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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");
}