The idea: RISC-V on a single-cycle datapath

RV32I is the 32-bit base integer instruction set of RISC-V. This page runs real RV32I machine words on the single-cycle processor of Harris and Harris (RISC-V Edition, Section 7.3), extended for lui: every instruction takes one clock cycle, values settle through the memories, the register file, the immediate generator and the ALU, and the clock edge writes the PC, one register and one memory word.

How a single-cycle processor works in general (state elements, the clock edge, why the slowest instruction sets the clock period) is on The Single-Cycle MIPS Processor. This page is about what RISC-V does differently: its six instruction formats, the scattered immediate bits, x0, and branches that compare registers and jump relative to the PC.

The six RV32I formats as bit fields from bit 31 to bit 0: R has funct7, rs2, rs1, funct3, rd, opcode; I has imm[11:0], rs1, funct3, rd, opcode; S has imm[11:5], rs2, rs1, funct3, imm[4:0], opcode; B has imm[12|10:5], rs2, rs1, funct3, imm[4:1|11], opcode; U has imm[31:12], rd, opcode; J has imm[20|10:1|11|19:12], rd, opcode. rs1, rs2 and rd sit in the same columns in every format that has them
opcode, rd, funct3, rs1 and rs2 never move, so the register file can be read before the instruction is decoded; only the immediate bits move between formats.

Pick an instruction and press Execute Instruction: it is written into instruction memory at the current PC and run. The cycle is shown as steps: fetch, decode, control, the immediate, the ALU, the memory and the clock edge. Only the clock edge writes state (green). Red wires carry values the instruction uses; faded wires are not used or don't care. Blue lines are control signals.

Six formats, fixed register fields

FormatUsed by (on this page)Fields, bit 31 to bit 0
Radd sub and or sltfunct7 | rs2 | rs1 | funct3 | rd | opcode
Iaddi lwimm[11:0] | rs1 | funct3 | rd | opcode
Sswimm[11:5] | rs2 | rs1 | funct3 | imm[4:0] | opcode
Bbeqimm[12|10:5] | rs2 | rs1 | funct3 | imm[4:1|11] | opcode
Uluiimm[31:12] | rd | opcode
Jjalimm[20|10:1|11|19:12] | rd | opcode

The opcode (bits 6:0) picks the format. rd is always bits 11:7, rs1 bits 19:15 and rs2 bits 24:20, and funct3 (bits 14:12) and funct7 (bits 31:25) pick the operation. For example add x7, x5, x6 is 0x006283B3: funct7 0000000, rs2 6, rs1 5, funct3 000, rd 7, opcode 0110011. sub is the same word with funct7 = 0100000 (0x406283B3). (Demo: add, an R-type instruction)

The immediate generator

Keeping the register fields fixed means the immediate has to fit around them. S puts imm[4:0] where rd would be. B and J also shuffle bits, so that most bits land in the same place as in S and I. The immediate generator (called Extend in the book) undoes this with wires only; the Control Unit tells it the format with ImmSrc.

Where the immediate bits go for S, B and J. S: instruction bits 31:25 become imm[11:5] and bits 11:7 become imm[4:0]. B: bit 31 becomes imm[12], bit 7 becomes imm[11], bits 30:25 become imm[10:5], bits 11:8 become imm[4:1], imm[0] is 0. J: bit 31 becomes imm[20], bits 19:12 stay imm[19:12], bit 20 becomes imm[11], bits 30:21 become imm[10:1], imm[0] is 0. In all three the upper bits copy instruction bit 31
The immediate generator only rewires bits; bit 31 of the instruction is always the sign, so sign extension starts before the format is even known.
ImmSrcFormatImmExt
000I{20 × inst[31], inst[31:20]}
001S{20 × inst[31], inst[31:25], inst[11:7]}
010B{19 × inst[31], inst[31], inst[7], inst[30:25], inst[11:8], 0}
011J{11 × inst[31], inst[31], inst[19:12], inst[20], inst[30:21], 0}
100U{inst[31:12], 12 × 0}

B and J offsets are always even, so imm[0] is not stored: the encoded range doubles (±4 KiB for branches, ±1 MiB for jal). Examples from the demos: sw x6, -12(x8) is 0xFE642A23; -12 is 1111111 10100, so bits 31:25 are 1111111 and bits 11:7 are 10100 (Demo: sw then lw). beq x5, x5, 8 is 0x00528463: imm[4:1] = 0100 sits in bits 11:8.

The datapath and its control

The main decoder turns the opcode into the control lines; the active row is marked on the canvas. x means don't care.

InstropcodeRegWriteImmSrcALUSrcMemWriteResultSrcBranchALUOpJump
lw0000011100010010000
sw0100011000111xx0000
R-type01100111xxx00000100
beq1100011001000xx1010
I-ALU (addi)0010011100010000100
jal11011111011x0100xx1
lui01101111100x0110xx0
  • ResultSrc picks what is written back: 00 ALUResult, 01 ReadData (loads), 10 PCPlus4 (the return address of jal), 11 ImmExt (lui; this page adds this fourth input and ImmSrc 100 to the book's datapath, one simple way to support lui).
  • PCSrc = Branch · Zero + Jump: the next PC is PCPlus4 or PCTarget = PC + ImmExt, made by a second adder.
  • The ALU decoder makes ALUControl: ALUOp 00 → add (000), 01 → subtract (001), 10 → look at funct3: 000 add, or subtract when op[5] and funct7[5] are both 1 (so addi never subtracts), 010 slt (101), 110 or (011), 111 and (010).

x0, no flags, branches relative to the PC

  • x0 is hard-wired to 0. Reads give 0 and writes are dropped. addi x0, x5, 7 (0x00728013) has RegWrite = 1 and still changes nothing; nop is addi x0, x0, 0 = 0x00000013, and jal x0, offset is a plain jump. (Demo: x0 is always zero)
  • No condition flags. beq sends both registers to the ALU, which subtracts; Zero decides. No earlier instruction leaves a flag behind. (Demo: beq not taken, then taken: 5 − 12 ≠ 0 is not taken, 5 − 5 = 0 is taken and skips one instruction.)
  • PC-relative. The target is the branch's own PC plus a byte offset: beq x5, x5, 8 at 0x04 goes to 0x0C. jal x1, 12 at 0x00 goes to 0x0C and writes x1 = 0x04, the return address (Demo: jal). Code can be loaded at any address without changing these words.
  • lui + addi build a 32-bit constant: lui sets the upper 20 bits, addi adds a sign-extended 12-bit value. For 0xDEADBEEF the low part 0xEEF has bit 11 set, so it counts as −273 and the upper part becomes 0xDEADB + 1 = 0xDEADC: lui x7, 0xDEADC (0xDEADC3B7) then addi x7, x7, -273 (0xEEF38393). (Demo: lui + addi)

The loop demo puts it together: 7 instructions add 3 + 2 + 1 in 16 cycles. beq x10, x0, 16 (0x00050863) tests the counter against x0, and jal x0, -12 (0xFF5FF06F) jumps back; its J immediate is negative, so bits 31:21 of ImmExt are copies of the sign bit. The end state is x11 = 6 and mem[0x110] = 6.

MIPS and RISC-V: what changes

The datapath has the same parts as the single-cycle MIPS. The differences come from the instruction set:

MIPS (the MIPS pages)RISC-V RV32I (this page)
Destination registerrd (R-type) or rt (I-type): a RegDst mux picksalways rd, bits 11:7: no RegDst mux
Immediate16 bits in one place (bits 15:0)12 bits (I, S, B) or 20 bits (U, J), scattered; an immediate generator with ImmSrc
Branch targetPC + 4 + (imm << 2), word offsetPC + imm, byte offset with imm[0] = 0, from the branch itself
Jumpj/jal: 26-bit pseudo-direct address; jal always writes $rajal rd, offset: PC-relative ±1 MiB, any rd (x0 = plain jump)
Delay slotreal MIPS runs the instruction after a branch (the book's model leaves this out)none
Flagsnone either: beq/bne compare registers; less-than needs slt firstnone; RV32I also has blt, bge, bltu, bgeu
Zero register$0x0
Building a 32-bit constantlui (16 bits) + ori (zero-extended, no correction)lui (20 bits) + addi (sign-extended, +1 correction)
Branch target in MIPS and RISC-V side by side. MIPS: the immediate is shifted left by 2 and added to PC + 4, so a branch at 0x04 with immediate 1 goes to 0x0C. RISC-V: the immediate is a byte offset added to the PC of the branch, so beq at 0x04 with offset 8 goes to 0x0C. Both skip the instruction at 0x08
MIPS counts words from the next instruction; RISC-V counts bytes from the branch itself, so there is no shifter in front of the target adder.

Machine code for MIPS, field by field: From C to MIPS Machine Code. The same instruction steps spread over several shorter cycles: The Multicycle MIPS Processor. Overlapped in five stages, with hazards and forwarding: The MIPS Pipeline. Why fixed-length, load/store instruction sets like these two are called RISC: RISC vs CISC.

What the page leaves out

  • The rest of RV32I: bne blt bge bltu bgeu, jalr (so there is no return here), auipc, shifts, xor, sltu, the other immediate ALU instructions, byte and halfword loads and stores, ecall/ebreak and fence.
  • Extensions (M, A, F, D, C) and the 16-bit compressed instructions; that is why branch offsets count in 2-byte steps.
  • Exceptions: misaligned addresses, illegal instructions, and CSRs.
  • Only x0 to x15 and 8 data words at 0x100 to 0x11C are shown; code starts at 0x00.
  • Timing: the steps are one cycle split up for explanation; wires settle instantly here.

On the mind map

This page on the Computer Architecture mind map · 64 pages in 7 branches · open it to see the whole map and test yourself in recall mode.