riscv

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit 77c753a76dc481afb44b3b8596ac00f4cf4e5a63
parent 3960a8a7fe04cce7b2ce8657f7fe410a913ec6ec
Author: Brian Swetland <swetland@frotz.net>
Date:   Wed, 23 Oct 2019 08:46:22 -0700

rvsim: custom ops

- wire up _exit, _exiti, _putc custom ops
- disable debug tracing

Diffstat:
MMakefile | 2+-
Mrvmain.c | 5+++++
Mrvsim.c | 27+++++++++++++++++++++------
Mrvsim.h | 2+-
4 files changed, 28 insertions(+), 8 deletions(-)

diff --git a/Makefile b/Makefile @@ -22,7 +22,7 @@ out/%.lst: out/%.elf HELLO_SRCS := start.S hello.c out/hello.elf: $(HELLO_SRCS) Makefile @mkdir -p out - $(CC) $(CFLAGS) -o $@ $(HELLO_SRCS) + $(CC) $(CFLAGS) -o $@ $(HELLO_SRCS) -lgcc RVSIM_SRCS := rvmain.c rvsim.c rvdis.c bin/rvsim: $(RVSIM_SRCS) Makefile gen/instab.h diff --git a/rvmain.c b/rvmain.c @@ -18,6 +18,11 @@ uint32_t ior32(uint32_t addr) { void iow32(uint32_t addr, uint32_t val) { } +void ioputc(uint32_t c) { + uint8_t x = c; + if (write(1, &x, 1)) {} // appease warning +} + int load_image(const char* fn, uint8_t* ptr, size_t sz) { struct stat s; int fd = open(fn, O_RDONLY); diff --git a/rvsim.c b/rvsim.c @@ -9,10 +9,10 @@ #include "riscv.h" #include "rvsim.h" -#define DO_TRACE_INS 1 -#define DO_TRACE_TRAPS 1 -#define DO_TRACE_MEM_WR 1 -#define DO_TRACE_REG_WR 1 +#define DO_TRACE_INS 0 +#define DO_TRACE_TRAPS 0 +#define DO_TRACE_MEM_WR 0 +#define DO_TRACE_REG_WR 0 #define RVMEMBASE 0x80000000 #define RVMEMSIZE 32768 @@ -149,14 +149,16 @@ static uint32_t get_csr(rvstate_t* s, uint32_t csr) { fprintf(stderr, " ([%08x] = %08x)\n", a, v);\ } while (0) #else -#define trace_mem_wr(v) do {} while (0) +#define trace_mem_wr(a, v) do {} while (0) #endif int rvsim_exec(rvstate_t* s, uint32_t _pc) { uint32_t pc = _pc; uint32_t next = _pc; uint32_t ins; + uint64_t ccount = 0; for (;;) { + ccount++; pc = next; ins = rd32(s->memory, pc); #if DO_TRACE_INS @@ -197,7 +199,20 @@ int rvsim_exec(rvstate_t* s, uint32_t _pc) { goto trap_common; } case OC_CUSTOM_0: - return 0; + switch (get_fn3(ins)) { + case 0b000: // _exiti + fprintf(stderr, "CCOUNT %lu\n", ccount); + return get_ii(ins); + case 0b100: // _exit + fprintf(stderr, "CCOUNT %lu\n", ccount); + return RdR1(); + case 0b101: // _putc + ioputc(RdR1()); + break; + default: + goto inval; + } + break; case OC_MISC_MEM: switch (get_fn3(ins)) { case F3_FENCE: diff --git a/rvsim.h b/rvsim.h @@ -13,5 +13,5 @@ uint32_t rvsim_rd32(rvstate_t* s, uint32_t addr); uint32_t ior32(uint32_t addr); void iow32(uint32_t addr, uint32_t val); - +void ioputc(uint32_t c);