Starting point

Computer engineering: from instructions to logic gates

A processor can be studied at several levels: which instructions it understands, how it reads them, how they change its state, how it overlaps them in time and which circuits perform each calculation. This page follows a single instruction through all of those levels and links the tool that lets you check each step.

1. The ISA: a contract between software and hardware

The instruction set architecture (ISA) defines which instructions exist, what each one does and how it is encoded in bits. RISC-V is an open ISA; its 32-bit integer base, RV32I, has 40 instructions, each 32 bits wide. Take add a2, a0, a1: the specification says it adds registers a0 and a1 and puts the result in a2, and that it is encoded as the word 0x00B50633.

2. Decode: read the fields of the word

Hardware does not “read” text: it splits the word into fixed-position fields. The low 7 bits (opcode) say it is a register-register operation (R format); funct3 and funct7 select addition; and three 5-bit fields give the register numbers. This is the breakdown produced by the site’s decoder:

0x00B50633 · R-Type
FieldBitsBinaryMeaning
funct7[31:25]0000000distinguishes variants (0100000 = sub/sra)
rs2[24:20]01011register x11 (a1)
rs1[19:15]01010register x10 (a0)
funct3[14:12]000selects the operation within the group
rd[11:7]01100register x12 (a2)
opcode[6:0]0110011group OP
Instructionadd a2, a0, a1

You can repeat it with any word in the RISC-V decoder.

3. Execute: change the processor state

Executing the instruction means applying its effect to the visible state: the 32 registers and the program counter (PC). If we first load a0 = 5 and a1 = 7, after add register a2 holds 12 and the PC moves on by 4 bytes.

addi a0, zero, 5
addi a1, zero, 7
add  a2, a0, a1

The RISC-V debugger runs this program one instruction at a time and highlights the register that changes at each step.

4. Pipeline: overlap instructions in time

A pipelined processor splits each instruction into stages (IF, ID, EX, MEM, WB) and starts a new one every cycle. The problem appears when an instruction needs a result the previous one has not written yet. In the program above, add needs a1 right after it is computed: without forwarding the simulator makes it wait 3 cycles (10 cycles in total); with forwarding the value is passed on from the next stage and there is no wait (7 cycles).

Check it in the pipeline simulator by switching forwarding on and off.

5. Digital logic: the circuit that adds

The addition is done by the ALU, which is built from logic gates. A 32-bit adder chains 1-bit full adders; each takes A, B and the carry-in C and produces a carry-out that is 1 when at least two inputs are 1. On a Karnaugh map that function occupies cells 3, 5, 6 and 7:

A \ BC00011110
000011302
104151716
Cout(A, B, C) = Σm(3, 5, 6, 7)

The three groups of two cells give Cout = BC + AC + AB: three 2-input AND gates and one 3-input OR, instead of the four 3-variable products of the truth table. Open it in the Karnaugh minimizer.

Where to go next

What this site does not cover

The tools focus on the RV32I integer base and on combinational logic. They do not simulate caches, the memory hierarchy, the privileged ISA, exceptions or extensions such as M, F or C, and the minimizer does not handle sequential circuits (flip-flops or state machines). Each tool page details its specific limits.

References

  • The RISC-V Instruction Set Manual, Volume I: Unprivileged Architecture (RISC-V International) — definition of RV32I, instruction formats, immediate encoding and register x0
  • D. A. Patterson, J. L. Hennessy — Computer Organization and Design RISC-V Edition: The Hardware/Software Interface (Morgan Kaufmann) — processor chapter: 5-stage pipeline, data and control hazards, forwarding and load-use detection
  • S. L. Harris, D. Harris — Digital Design and Computer Architecture: RISC-V Edition (Morgan Kaufmann) — combinational logic, Karnaugh maps, 7-segment decoder and RISC-V microarchitecture