RISC-V processor in RTL
A 32-bit single-cycle RV32I processor in Verilog that runs programs from fetch through write-back. It matches course-supplied golden traces on all 48 tests in Verilator. Next it becomes a five-stage pipeline.
Overwrite it, read it back.
Redrawn from my GTKWave capture of rv32ui-p-sw · Values unchanged, simulator time omitted · Raw capture
Before the edge
sw raises memory_write with 00AA00AA on data_rs2. memory_data still reads the old DEADBEEF: reads are combinational, and the write hasn't happened yet.
On the rising edge
The store commits. memory_data changes to 00AA00AA as the PC moves to the next instruction.
The next cycle
lw reads the same address, 010004C4. The value reaches data_rd with write_enable high, and x30 takes it on the following edge.
Fetch to write-back, in RTL.
Instruction memory
Byte-addressable, little-endian storage built from 8-bit elements. A 32-bit read assembles four bytes, and writes land on the rising clock edge. $readmemh loads the program as hex words and splits each word into four bytes. Capacity is configurable, 1 MiB by default in simulation.
Fetch and PC control
A 32-bit program counter resets synchronously to 0x01000000 and advances 4 bytes each clock edge, or takes the ALU's target on JAL, JALR, and taken branches.
Decode
A combinational decoder extracts opcode, register, and function fields for all six RV32I formats (R, I, S, B, U, and J), covering the 37-instruction subset the core targets.
Immediate generation
Five immediate layouts, selected by opcode, reassembled from their scattered instruction bits, sign-extended, and padded to 32 bits.
Register file
32 registers of 32 bits with two combinational read ports and one write port on the rising clock edge. x0 ignores writes and always reads zero, and x2, the stack pointer, starts at the top of memory.
ALU and branches
A fully combinational execute stage: arithmetic, logic, shifts, and signed and unsigned compares for register and immediate operands, load and store addresses, LUI and AUIPC, jump targets with JALR's bit 0 cleared, and all six branch conditions with the branch target.
Data memory
Byte-addressable and little-endian, with combinational reads and writes on the rising clock edge. SB, SH, and SW write only the bytes they target, and LB, LH, LW, LBU, and LHU format the read with sign or zero extension.
Write-back
A mux sends the ALU result, formatted load data, or PC+4 for JAL and JALR back to rd. Stores and branches leave the registers untouched.
Five layouts, one 32-bit value.
RISC-V scatters immediate bits across the instruction so the register fields stay in fixed positions. The decoder reverses that: the opcode picks the layout, the bit slices are copied into place, bit 31 is sign-extended, and the low bits are zero-filled where the format requires it.
Each cell names the instruction bits it copies · all five share the sign at inst[31]
48 of 48 golden traces.
48 of 48 tests match course-supplied golden traces in Verilator: 38 instruction benchmarks and 10 programs.
The programs are BubbleSort, CheckVowel, fact-short, Fibonacci, gcd, SimpleAdd, SimpleIf, SumArray, Swap, and SwapShift.
Each comparison covers every line of the reference across fetch, decode, register, execute, memory, and write-back.
30 probes expose internal signals to the testbench, and I inspect them as waveforms in GTKWave.
This is exact agreement with the supplied references, not proof of every input or full ISA compliance.
Bugs fixed on the way.
Writes during reset
Registers and data memory could be written while reset was high. Both write enables are now gated on reset, so nothing changes state until the core is running.
Shift-immediate decode
SRAI's function bits were ending up in the decoded immediate. The ALU's results were already right, but the decoded value wasn't. Shifts now decode to a zero-extended five-bit amount.
From fetch to FPGA.
Memory & fetch
Byte-addressable instruction memory and a 32-bit PC stepping through the program.
Decode & immediates
Field extraction for six formats and five immediate layouts, integrated with fetch.
Register file & ALU
Two-read, one-write register file and a combinational ALU that also resolves branches.
Write-back & control flow
Data memory, write-back to rd, and PC updates on taken branches and jumps, so the core runs all 37 instructions.
Five-stage pipeline
Pipeline registers, data forwarding, and hazard stalls to keep instructions moving correctly.
FPGA bring-up
Synthesize the pipelined core and bring it up on a PYNQ-Z1 board.
The pipeline comes next.
Pipeline registers go in at the dashed lines, with forwarding and hazard stalls to keep instructions moving, then the core goes onto a PYNQ-Z1 FPGA board.