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.
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
| Format | Used by (on this page) | Fields, bit 31 to bit 0 |
|---|---|---|
| R | add sub and or slt | funct7 | rs2 | rs1 | funct3 | rd | opcode |
| I | addi lw | imm[11:0] | rs1 | funct3 | rd | opcode |
| S | sw | imm[11:5] | rs2 | rs1 | funct3 | imm[4:0] | opcode |
| B | beq | imm[12|10:5] | rs2 | rs1 | funct3 | imm[4:1|11] | opcode |
| U | lui | imm[31:12] | rd | opcode |
| J | jal | imm[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.
| ImmSrc | Format | ImmExt |
|---|---|---|
| 000 | I | {20 × inst[31], inst[31:20]} |
| 001 | S | {20 × inst[31], inst[31:25], inst[11:7]} |
| 010 | B | {19 × inst[31], inst[31], inst[7], inst[30:25], inst[11:8], 0} |
| 011 | J | {11 × inst[31], inst[31], inst[19:12], inst[20], inst[30:21], 0} |
| 100 | U | {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.
| Instr | opcode | RegWrite | ImmSrc | ALUSrc | MemWrite | ResultSrc | Branch | ALUOp | Jump |
|---|---|---|---|---|---|---|---|---|---|
lw | 0000011 | 1 | 000 | 1 | 0 | 01 | 0 | 00 | 0 |
sw | 0100011 | 0 | 001 | 1 | 1 | xx | 0 | 00 | 0 |
| R-type | 0110011 | 1 | xxx | 0 | 0 | 00 | 0 | 10 | 0 |
beq | 1100011 | 0 | 010 | 0 | 0 | xx | 1 | 01 | 0 |
I-ALU (addi) | 0010011 | 1 | 000 | 1 | 0 | 00 | 0 | 10 | 0 |
jal | 1101111 | 1 | 011 | x | 0 | 10 | 0 | xx | 1 |
lui | 0110111 | 1 | 100 | x | 0 | 11 | 0 | xx | 0 |
- 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 supportlui). - 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
addinever 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;nopisaddi x0, x0, 0=0x00000013, andjal x0, offsetis a plain jump. (Demo: x0 is always zero) - No condition flags.
beqsends 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, 8at 0x04 goes to 0x0C.jal x1, 12at 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:
luisets the upper 20 bits,addiadds a sign-extended 12-bit value. For0xDEADBEEFthe low part0xEEFhas bit 11 set, so it counts as −273 and the upper part becomes0xDEADB + 1 = 0xDEADC:lui x7, 0xDEADC(0xDEADC3B7) thenaddi 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 register | rd (R-type) or rt (I-type): a RegDst mux picks | always rd, bits 11:7: no RegDst mux |
| Immediate | 16 bits in one place (bits 15:0) | 12 bits (I, S, B) or 20 bits (U, J), scattered; an immediate generator with ImmSrc |
| Branch target | PC + 4 + (imm << 2), word offset | PC + imm, byte offset with imm[0] = 0, from the branch itself |
| Jump | j/jal: 26-bit pseudo-direct address; jal always writes $ra | jal rd, offset: PC-relative ±1 MiB, any rd (x0 = plain jump) |
| Delay slot | real MIPS runs the instruction after a branch (the book's model leaves this out) | none |
| Flags | none either: beq/bne compare registers; less-than needs slt first | none; RV32I also has blt, bge, bltu, bgeu |
| Zero register | $0 | x0 |
| Building a 32-bit constant | lui (16 bits) + ori (zero-extended, no correction) | lui (20 bits) + addi (sign-extended, +1 correction) |
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/ebreakandfence. - 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
References
Harris and Harris, Digital Design and Computer Architecture, RISC-V Edition, Chapter 6 (architecture) and Chapter 7 (Section 7.3, the single-cycle processor: datapath, main decoder and ALU decoder)
Patterson and Hennessy, Computer Organization and Design RISC-V Edition, Chapter 2 (instruction formats) and Chapter 4 (Section 4.4, a simple implementation scheme)