Minimax: a Compressed-First, Microcoded RISC-V CPU
github.com/gsmecher/minimaxPosted Oct 26, 2022, 11:06 PM UTC
Posted Oct 26, 2022, 11:06 PM UTC
Have you looked at benchmark performance? It's great that you can execute compressed instructions so quickly, but how does that translate to expected workloads?
If you take your uArch and compare it to a RV32I uArch, both running at the same clock speed, and you require 4x as many cycles to execute while only saving 5% the resources, then it may not be a viable tradeoff. You should be comparing performance in more ways than simply area.
Also, another approach you might want to consider (for a followup version), is executing multiple compressed instructions in parallel. There's some indication that this was the intended use case for the C instruction set, allowing you to build more complex instructions seen in ARM out of two simpler C instructions (similar to the x86 LEA). Since the operations all take one register argument, that should be implementable with a 3 port register file (2R+1W).
Parallelizing RVC instructions is a neat idea - I have not been chasing performance, and intended this core to fit in places where resource usage was important and performance just needed to be "good enough".
The ability to execute 1 IPC in straight-line code is more of a consequence of the design's simplicity than any explicit performance goals. (I expect FMAX to suffer due to logic depth, which pretty well wipes away any performance credentials I try to establish here.)
You might check out Chris Batten's Tiny RISC-V ISA.
Interesting, I didn't know about this one.
TinyRV1 with 8 instructions only has a couple of instructions fewer than my cut down RV32I (10 if I'm allowed to introduce NAND, 11 with AND and XOR). I have full JALR instead of JR, BLT instead of BNE (much more useful), and SLL and SRA instead of MUL.
You could compile full C/C++ to my subset (or post-process an RV32I assembly language output) with some code expansion but very little performance penalty. This would be very hard or very slow with TinyRV1 due to the complete lack of shifts and boolean operations. Without an effective NOT or NEG operation you can't even do a subtraction (as far as I can see!) without something like...
int res = 0; while (a != b + res) ++res;
... which potentially loops 2^32 times.
And without subtract you can't easily do less than or greater than tests.
TinyRV1 is simply not a practical ISA. It's almost on a level with a one-instruction ISA.
TinyRV2 on the other hand is barely cut down at all from RV32I.
or post-process an RV32I assembly language output
Or make a full RV32I microcoded in BH11? BH11 is the most useful of the RV32 subsets?
TinyRV1 is only designed to run in student's minds from a slide, so it has a pretty niche target.
I find subsetting larger systems to be an interesting pedagogical exercise, condensing something down to the smallest possible useful (for some domain) from provides an immense amount of clarity.
It would be wonderful if specifications were written (and rewritten) using this technique, then the reader would know why each feature was added.
Children learn so much via play, but adults avoid play during serious work and lose that flow and velocity. Play is critical in learning and technological development.
What is the RISC-V game?
I'm just scratching the surface of it but it seems a really awesame RISC-V implementation. I'm in for any CPU that does as much as possible with the minimum of resources, basically, it seems better to retire one instruction per clock using 10% of the chip area than 2 instructions using 50%...
How can I simulate this design? For example with Verilator or something that I can hook to a C++ program (I plan on doing some graphics and render them in realtime in a linux box)
I use Vivado for simulation (see test/Makefile). It looks like recent GHDL releases can simulate the core, but not the testbench. That's probably fine - you will want to use a different wrapper anyways.
You can embed Vivado's simulator within C++ code using XSI, and GHDL has cosimulation interfaces too. I would happily shift to GHDL (especially if a pull request comes my way!)
Yes I'm trying GHDL but so far I obtained this error:
minimax.vhd:412:78:error: synth_dyadic_operation: unhandled IIR_PREDEFINED_IEEE_NUMERIC_STD_AND_UNS_LOG
shamt <= (unsigned(inst(6 downto 2)) and (op16_SLLI or op16_SRLI or op16_SRAI))
If I can process it with ghdl, I can translate it to verilog using yosys, then with verilator or CXXRTL, then make an interesting simulator with graphical output ;-)
Here's what I just tried:
minimax$ docker pull ghdl/ghdl:buster-gcc-9.4.0
minimax$ docker run -it -v `pwd`:/minimax -u `id -u`:`id -g` ghdl/ghdl:buster-gcc-9.4.0 /bin/bash
Then, inside Docker:
$ cd /minimax/rtl
$ ghdl -a --std=08 minimax.vhd
$ ghdl -e --std=08 minimax
$ ./minimax
../../src/ieee2008/numeric_std-body.vhdl:3036:7:@0ms:(assertion warning): NUMERIC_STD.TO_INTEGER: metavalue detected, returning 0
../../src/ieee2008/numeric_std-body.vhdl:3036:7:@0ms:(assertion warning): NUMERIC_STD.TO_INTEGER: metavalue detected, returning 0
I'm not sure if you're using a different GHDL flow, or if it's a GHDL version thing - any idea?
Ah, this is a synthesizer limitation.
$ ghdl --synth --std=08 minimax
minimax.vhd:412:78:error: synth_dyadic_operation: unhandled IIR_PREDEFINED_IEEE_NUMERIC_STD_AND_UNS_LOG
shamt <= (unsigned(inst(6 downto 2)) and (op16_SLLI or op16_SRLI or op16_SRAI))
^
minimax.vhd:503:69:error: synth_dyadic_operation: unhandled IIR_PREDEFINED_IEEE_NUMERIC_STD_AND_UNS_LOG
or (std_logic_vector(resize(pc_fetch_dly & "0", 32) and (op16_JAL or op16_JALR or op32_trap))) -- instruction following the jump (hence _dly)
^
minimax.vhd:508:27:error: synth_dyadic_operation: unhandled IIR_PREDEFINED_IEEE_NUMERIC_STD_AND_UNS_LOG
aguA <= (pc_fetch and not (op16_JR or op16_JALR or op32_trap or branch_taken))
^
minimax.vhd:511:45:error: synth_dyadic_operation: unhandled IIR_PREDEFINED_IEEE_NUMERIC_STD_AND_UNS_LOG
aguB <= (unsigned(regS(aguB'range)) and (op16_JR or op16_JALR or op16_slli_thunk))
^
minimax.vhd:143:19:warning: unhandled attribute "ram_style"
attribute RAM_STYLE of register_file : signal is "distributed";
^
minimax.vhd:147:52:warning: signal "dnext" is never assigned and has no default value [-Wnowrite]
signal regS, regD, aluA, aluB, aluS, aluX, Dnext : std_logic_vector(31 downto 0); -- datapath
^
minimax.vhd:154:22:warning: signal "agua" is never assigned [-Wnowrite]
signal aguX, aguA, aguB : unsigned(PC_BITS-1 downto 1) := (others => '0');
^
minimax.vhd:154:28:warning: signal "agub" is never assigned [-Wnowrite]
signal aguX, aguA, aguB : unsigned(PC_BITS-1 downto 1) := (others => '0');
^
minimax.vhd:141:16:note: found RAM "register_file", width: 32 bits, depth: 64
signal register_file : reg_array := (others => (others => '0'));
I will bet the implementations in GHDL are trivial but missing.
btw I plan to process it with a simulator I'm building that's the fastest (so far in my tests) and I'm selecting a RISC-V design to try, if that interest you please see this: https://twitter.com/suarezvictor/status/1585321811360858126
For benchmarking a simulator, I will bet you're better off picking a middle-of-the-road RISC-V implementation. FemtoRV32 and PicoRV32 are currently better candidates than Minimax, and I doubt that will change.
I like that CPUs but I find your code well structured. So why not?
So, I nerd-sniped myself some time ago - this is the result. It's an attempt to understand what happens if a RISC-V CPU targets the compressed extension (RVC) as if it were an instruction set, rather than an afterthought to be expanded into regular RV32I instructions.
In order to make this core useful, complete RV32IC support is necessary. I use two strategies to supplement the RVC implementation (which is not adequate by itself) with the rest of the ISA:
In short: it works, though the implementation lacks the crystal clarity of FemtoRV32 and PicoRV32. The core is larger than SERV but has higher IPC and (very arguably) a more conventional implementation. The compressed instruction set is easier to expand into regular RV32I instructions than it is to execute directly.
Interesting. RVC is (deliberately) even less of a complete ISA than Thumb1 / T16 is. In the case of Thumb, it was designed to be able to compile most normal C integer code into it, and call out to A32 functions for things such as floating point, divide or high bits from multiply, CSR access etc. RVC on the other hand is designed in the expectation you can fall back on full 32 bit opcodes on an instruction-by-instruction basis.
There were back in maybe 2017 or 2018 or so pre RISC-V base ISA ratification some proposed alternative C extensions that were more useful as a stand-alone ISA (needed to fall back to 32 bit opcodes less often, and even got better code compression on average code -- RVC is a bit too influenced by SPECfp, in my opinion)
e.g. https://groups.google.com/a/groups.riscv.org/g/isa-dev/c/iK3enKGb5bw?pli=1
RVC is profoundly impoverished. You can't even do variable-length shifts or computed (table) jumps. Support for RV32I is an absolute requirement for an usable ISA from purely technical considerations, leaving aside all the ecosystem benefits from a full RV32I implementation.
(How impoverished? Minimax started without any "native" RV32I instructions - microcoded only - and it was an exercise in futility just to get it working, never mind performing acceptably.)
The microcode approach used here is a nice way to demote RV32I instructions that don't earn their gates, or are effectively supplanted by RVC instructions, without losing ecosystem compatibility.
This would be a fun project for homebrew cpu folk who put together CPUs from more discrete logic. Those 800 CLBs could be perhaps 300 GALs, and even some FFs would be taken care of :) Or perhaps even good old bipolar PALs if you got a few kWs of 5V to burn :)
An RV32I implementation on discrete logic would be much more fun, and much more instructive, than something like Minimax. Unfortunately, RVC is a dogs'-breakfast to decode and the Minimax RTL relies heavily on the synthesizer to make sense of it. While working on this, I kept hoping that structure would crystallize out of chaos but it hasn't happened to the degree necessary to make a good teaching tool.
In other words: start with Bruno Levy's excellent notes on FemtoRV32 instead.
... because dynamic shift amounts are very rare in most code. Many many ISAs historically don't have dynamic shift amounts, and even have only static shift amounts of ±1 only, in which case you need to execute up to around 96 or 192 instructions (32 bit / 64 bit) to do a dynamically specified shift. At least with static shifts of any amount, RVC can implement an arbitrary dynamic shift with at most 15 (32 bit) or 18 (64 bit) instructions.
That's just wrong.
C.JRis perfectly useable for table jumps, following a shift and an add -- which you have to do in RV32I also.My perspective here is slanted by minimax itself - in order to emulate shift opcodes, variable shifts are necessary.
It's not impossible to do table jumps in straight RVC, but it is clunky. The problem is not the C.JR, it's getting the table base into a register in the first place.
In RV32I, I'd use the LA pseudoinstruction, which ends up as an AUIPC followed by ADDI. There's no equivalent to AUIPC in RVC - to get the program counter into a register, I'd need a "dummy" C.JAL and some arithmetic. When prototyping the microcode without AUIPC, I was not able to find a satisfying way to do this with the GNU assembler. (It's possible to hand-craft something extremely brittle but I had hoped for something less arcane. I am not a binutils expert and there are certainly possibilities I didn't explore.)
Please understand: when I say "impoverished" and "clunky", I am not criticizing RISC-V or RVC in any way. RISC-V is elegant and RVC is a reasonable set of very constrained design trades. I use these words when describing RVC as a stand-alone ISA - which is no surprise, since (as you pointed out) it was never intended as one.
What is the maximum frequency you got?
To be honest, I've only synthesized the design to take resource measurements. It passes timing with a 100 MHz with some slack. However, since I've used the core as the top level, the I/O bus is assigned directly to I/O pins and the limiting factor is I/O placement constraints - which are totally unnatural here.