This is a project that aims to construct a single RISC-V ISA-based CPU and associated RAM using the R32I instruction set (and the R32F extension if time and interest permit).
Key features will include conditional branching, ALUops, and 4kb of user-dictable memory.
compandrun.py does the whole loop — assembles the source, rebuilds the design, loads the image, runs it, and reports:
python compandrun.py programs/control.s
prints the execution trace, the final non-zero registers and memory, and the path to the waveform. flags: --no-trace, --no-wave, --full, --gtkwave, --max N. takes a prebuilt .hex too. everything lands in build/.
the sections below are the manual equivalents, if you want to drive the pieces yourself.
install icarusverilog sim and gtkwave from respective vendors (don't use icarusverilog bundled gtkwave, I noticed performance issues); add both to PATH. cd into /Testbench. Modify list.f as needed to define compilation list. Then run:
iverilog -g2012 -f list.f -o testsim
vvp testsim
To view waveform:
gtkwave.exe wave.vcd (or your dumpfile name, can be changed in tb_top)
add +TRACE to print one line per instruction — the PC, the instruction word, the disassembly, and which register changed:
vvp testsim +TRACE
TRACE 00000008 002081b3 add x3, x1, x2 x3: 00000000 -> 0000000c
TRACE 00000010 04302023 sw x3, 64(x0) -
the disassembler decodes from the instruction word, so it works on any image, not just ones this assembler produced.
write RV32I assembly (see /programs for examples), then run tools/asm.py to produce a hex image. needs python 3, no RISC-V toolchain:
python tools/asm.py programs/arith.s -o programs/arith.hex
registers by number (x5) or ABI name (t0); labels end in ':'; comments with # or //; loads and stores use offset(base). branch and jump targets are labels. pseudo-instructions: nop, li, mv, j, jr, ret, beqz, bnez.
the image is loaded into SRAM with the +PROG plusarg. swap list.f to tb_run rather than tb_cpu, which loads its own programs and ignores +PROG:
vvp testsim +PROG=../programs/arith.hex
programs load at address 0 and memory is unified, so keep data clear of the program — the examples use addresses from 64 upward.
Ecall/Ebreak are treated as program terminations. FENCE is treated as FENCE.I for simplicity!
Invalid instructions, misaligned loads/stores, and jumps past the end of memory halt the core with the PC naming the faulting instruction. Reserved encodings are not detected (nonzero funct7 bits, RV32M, CSRs) — they execute as the nearest base RV32I instruction.
assembler that will translate assembly code into .bin?done, tools/asm.py