← All posts

Building a Pipelined RISC Processor in Logisim

On this page

Single cycle processor in Logisim

Every software engineer uses a processor all day; very few of us ever build one. In ICS 233 (Computer Architecture & Assembly Language) at KFUPM, my team of three did exactly that. We designed a 32-bit RISC processor gate by gate in Logisim, first as a single-cycle CPU and then as a five-stage pipeline. My part of the pipelined version was the next-PC logic, the main control unit and most of the integration work.

Everything below comes straight from our project report and circuits, so the screenshots are the real thing, bugs-fixed edition. The full project lives on GitHub.

The instruction set

Before drawing a single wire we had to agree on the instruction set. Here are the main properties of our processor:

Property Value
Data width 32-bit
Registers 8 general-purpose registers R0–R7 (so each register field is 3 bits)
Instruction width 16-bit, with a 5-bit opcode
Memory Word-addressed, so the next instruction is PC + 1
Special registers R7 holds the return address for JAL, R0 is the target of SET/SSET

All instructions fit in one of four formats:

bit 15 bit 0 R-type op (5) rs (3) rt (3) rd (3) f (2) I-type op (5) rs (3) rt (3) imm5 B-type op (5) rs (3) imm8 (PC-relative) J-type op (5) imm11
Format Opcodes Instructions
R 0 and 1 (function 0–3) AND CAND OR XOR · ADD NADD SLT SLTU
I 4–17 ANDI CANDI ORI XORI ADDI NADDI SLTI SLTUI · SLL SRL SRA ROR · LW SW
B 20–27 BEQZ BNEZ BLTZ BGEZ BGTZ BLEZ · JR JALR
J 28–31 SET SSET · J JAL

Two instructions are not the usual MIPS ones:

  • CAND rd, rs, rt means rd = ~rs & rt (complement then AND)
  • NADD rd, rs, rt means rd = rt − rs, so we get subtraction without a separate SUB opcode

Worth knowing: With only 11 bits of immediate, a 32-bit constant is built in steps: set imm11 loads R0 = imm11, then every sset imm11 does R0 = (R0 << 11) | imm11. It is the same idea as lui + ori in MIPS.

Single cycle design

Start with the single-cycle processor. My advice here: build it and test it completely before you even think about the pipeline. Any bug you leave in this stage becomes much harder to find when five instructions are running at the same time.

Register file

The register file reads two registers and writes one register in the same cycle. Writing is done with a demultiplexer that enables only the selected register, and reading is done with two 8-to-1 multiplexers, one for each output (Read Data 1 and Read Data 2):

Register file circuit

Arithmetic and Logic Unit (ALU)

The ALU is built from logic gates, shifters, comparators and multiplexers. All operations (AND, CAND, OR, XOR, ADD, NADD, SLT, SLTU and the shifts) are calculated in parallel, and a multiplexer controlled by the 4-bit ALUOp signal selects the result we need:

ALU circuit

Main control unit

Instead of writing a logic equation for every control signal, we decoded the opcode and used a ROM, where every word is the set of control signals for one instruction. Adding or fixing an instruction means changing one row in the ROM instead of redesigning gates:

Main control unit with decoder and ROM

These are the control signals it generates:

Signal 0 1 2 3
RegDst Rt Rd R7 (for JAL) R0 (for SET)
ExtOp zero-extend sign-extend – –
ALUSrc BusB (Rt) extended imm5 extended imm11 extended imm11
MemRd / MemWr off on – –
WBdata ALU result memory output PC + 1 (return address) extended imm11

Once all components are connected, we get the complete single-cycle processor shown at the top of this article: instruction fetch, instruction splitter, register file, ALU, data memory, next PC logic and the control unit.

Pipelined design

In the pipelined version we split the datapath into five stages with pipeline registers between them: IF (instruction fetch), ID (instruction decode), EX (execute), MEM (memory access) and WB (write back). Now a new instruction can start every cycle:

cycle → 12345678 add $1,$2,$3 add $4,$1,$5 or $6,$1,$7 xor $2,$1,$4 IF ID EX MEM WB IF ID EX MEM WB IF ID EX MEM WB IF ID EX MEM WB Every later instruction reads $1 before the first one has written it back → forwarding needed.

In the program above, every instruction after the first one needs $1 before it has been written back. This is called a data hazard, and solving hazards is the real work in a pipelined design. Lets go through the stages one by one.

IF — Instruction Fetch

The PC selects the next address from four options (PC+1, JUMP, BRANCH, JUMPR) using the PCSrc signal. Notice two important signals here: Stall disables writing the PC and the instruction register, and Kill1 replaces the fetched instruction with 0000 (a no-op):

Instruction fetch stage

ID — Instruction Decode

Here the instruction is split into its fields, registers are read, and the control unit generates the signals. This is also where the forwarding multiplexers sit: ForwardA and ForwardB decide whether each ALU operand comes from the register file or from one of the three instructions ahead (FW1, FW2, FW3):

Instruction decode stage with hazard unit

EX, MEM and WB

The ALU runs in EX, data memory is accessed in MEM, and the result is written back in WB. At the bottom you can see how control signals travel with the instruction through the pipeline registers. If Kill2 or Stall is active, the multiplexer sends zeros instead, which turns the instruction into a bubble:

Execute, memory and write back stages

Hazard detection unit

The hazard unit compares the source registers of the current instruction (s, t) with the destination registers of the three previous instructions (d2, d3, d4) and checks if those instructions really write a register (RegWr):

Hazard detection and forwarding unit

The forwarding signals mean:

ForwardA / ForwardB ALU operand comes from
0 Register file (no hazard)
1 Previous instruction (EX stage)
2 Second previous instruction (MEM stage)
3 Third previous instruction (WB stage)

Look at the chain of three multiplexers on the right side of the circuit. The comparison for the most recent instruction (EC1) is connected last, so it always wins. This order is very important: if two older instructions write the same register, we must take the newest value. A wrong order passes simple tests and fails on loops.

The load-use stall

Forwarding cannot solve this case:

lw    $7, 0($2)    ; the value exists only after MEM
naddi $2, $7, 15   ; but it is needed in EX one cycle earlier

That is why the bottom of the hazard unit generates:

Stall = (EC1A OR EC1B) AND EX.MemRd

In words: if the previous instruction is a load (MemRd) and the current instruction needs its destination register, freeze the PC and the instruction register for one cycle and insert a bubble. In the next cycle the value is forwarded normally.

PC control unit and branches

The PC control unit decides the next PC and which wrong instructions must be cancelled. Branch conditions come from the ALU flags (>0, =0, <0), for example BGEZ is taken when EQZ OR GTZ:

PC control unit with branch and kill logic

There are two kill signals:

  • Kill1 cancels the instruction in IF. It is used for jumps (and branches), because one wrong instruction is already fetched.
  • Kill2 cancels the instruction in ID as well. It is used for taken branches, because a branch is resolved later and two wrong instructions are already in the pipeline.

Testing

Each test program was written to test one specific part of the design:

Program What it tests
Sequence of all instructions Every opcode in the single-cycle design
Bubble sort Loops, branches, LW/SW and real dependency chains
Count the 1-bits in a register Shifts, branches and a tight loop
Independent instructions Pipeline fill and drain without hazards
Dependent instructions (more than 5 in a chain) All forwarding paths
Dependent instructions after LW The load-use stall
Branching logic Kill1 / Kill2 flushing

This is the bubble sort program in our instruction set:

          set 4                ; numOfComparisons = arraySize - 1
whileLoop: beqz $0, EndWhile
          xor  $1, $1, $1      ; i = 0
ForLoop:  nadd $5, $0, $1      ; $5 = $1 - $0, zero when i == numOfComparisons
          beqz $5, EndForLoop
          addi $2, $1, 0
          lw   $3, 0($2)       ; array[i]
          lw   $4, 1($2)       ; array[i+1]
          slt  $6, $4, $3
          beqz $6, Endif
          sw   $4, 0($2)       ; swap
          sw   $3, 1($2)
Endif:    addi $1, $1, 1
          j ForLoop
EndForLoop: addi $0, $0, -1
          j whileLoop
EndWhile: addi $7, $7, 1

Before running the program, the array in data memory is in reverse order:

Data memory before bubble sort

After running it, the array is sorted:

Data memory after bubble sort

Why this program: Notice the two lw instructions followed directly by slt, which uses both loaded values. In four lines this program tests the load-use stall and two levels of forwarding. Real programs find bugs that simple tests miss.

If you want to build your own

Building a processor teaches things a textbook can’t: why the control unit is easier as a ROM, why forwarding priority matters, and why some hazards can only be solved by stalling. If you try it yourself, this is the order I would follow, testing after every step:

  1. Register file alone
  2. ALU alone, with edge values (0, -1, overflow)
  3. Control ROM (write the truth table in a spreadsheet first)
  4. Complete single-cycle CPU
  5. Add the pipeline registers and test only independent instructions
  6. Add forwarding and test dependent instructions
  7. Add the load-use stall
  8. Add branch flushing, then run bubble sort as the final test

Writing all those test programs by hand in hex was painful — which is exactly why I ended up writing an assembler and simulator for this instruction set. That’s the next post. The full Logisim circuit, test programs and report are on GitHub.