Back to projects

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.

Single-cycle RV32I datapathA program counter fetches from instruction memory. The decoder drives a 32 by 32-bit register file and an immediate generator. The ALU takes register, PC, or immediate operands, and a comparator decides taken branches. The ALU result addresses data memory and returns to the PC as the branch or jump target. A write-back mux sends the ALU result, formatted load data, or PC plus 4 back to the register file.FETCHDECODEEXECUTEMEMORYWRITE-BACKPC+4IMEMDECODEIMMGENREGSALUCMPDMEMEXTtake_branchbranch or jump targetPC+4pcinstructionrs1rs2rdimm32 x 322R · 1Wwrite_enablepcdata_rs2 to storedata_rd: ALU, load, or PC+4alu_resultalu_result to write-backmemory_writememory_datasign/zeroreset:0x01000000opcode sets every muxone cycle today · pipeline registers go herein the waveform belowwritten on the rising edge
Single-cycle coreComplete
Instruction setRV32I · 37
Golden traces48 / 48
Programs run10
Waveform · Store, then load

Overwrite it, read it back.

addi x2, x2, 0xAAsw x2, 0(x1)lw x30, 0(x1)lui x29, 0xAA0clockpc0100000C010000100100001401000018instruction0AA101130020A0230000AF0300AA0EB7memory_writealu_result00AA00AA010004C400AA0000data_rs20000000000AA00AA00000000memory_data00000000DEADBEEF00AA00AA00000000write_enablerd02001E1Ddata_rd00AA00AA010004C400AA00AA00AA0000store commits
sw x2, 0(x1)lw x30, 0(x1)clockmemory_writealu_result010004C4memory_dataDEADBEEF00AA00AAwrite_enablerd001Edata_rd010004C400AA00AAstore commits

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.

The blocks

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.

Immediate generation

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.

RV32I immediate layoutsHow the decoder builds each 32-bit immediate from instruction bits. I and S copy instruction bit 31 into the upper 21 bits as the sign. B does the same for the upper 20 bits and forces bit 0 to zero. U copies instruction bits 31 to 12 and zero-fills the low 12 bits. J sign-extends the upper 12 bits and forces bit 0 to zero.FORMATimm[31]imm[0]Iaddi · lw · jalr31 · sign30:2524:2120Ssw · sh · sb31 · sign30:2511:87Bbeq · bne · blt31 · sign730:2511:80Ului · auipc31:120Jjal31 · sign19:122030:2524:210sign: copies of inst[31]zero fillinstruction bits copied

Each cell names the instruction bits it copies · all five share the sign at inst[31]

Verification

48 of 48 golden traces.

01

48 of 48 tests match course-supplied golden traces in Verilator: 38 instruction benchmarks and 10 programs.

02

The programs are BubbleSort, CheckVowel, fact-short, Fibonacci, gcd, SimpleAdd, SimpleIf, SumArray, Swap, and SwapShift.

03

Each comparison covers every line of the reference across fetch, decode, register, execute, memory, and write-back.

04

30 probes expose internal signals to the testbench, and I inspect them as waveforms in GTKWave.

05

This is exact agreement with the supplied references, not proof of every input or full ISA compliance.

Debugging

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.

Roadmap

From fetch to FPGA.

01

Memory & fetch

Byte-addressable instruction memory and a 32-bit PC stepping through the program.

Done
02

Decode & immediates

Field extraction for six formats and five immediate layouts, integrated with fetch.

Done
03

Register file & ALU

Two-read, one-write register file and a combinational ALU that also resolves branches.

Done
04

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.

Done
05

Five-stage pipeline

Pipeline registers, data forwarding, and hazard stalls to keep instructions moving correctly.

Next
06

FPGA bring-up

Synthesize the pipelined core and bring it up on a PYNQ-Z1 board.

Planned
Next

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.