You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A 32-bit predicated RISC processor implemented in Verilog HDL with a 5-stage pipeline architecture. Supports R-Type, I-Type, and J-Type instructions with predicated execution controlled by a predicate register (Rp). Features a 32-register file, Harvard memory architecture, full forwarding network, stall logic, and kill logic. Verified through simulation covering all instruction types, data hazards, control hazards, and predicated execution.
Measured CPI โ 1.15 across a full test program including stalls and control-hazard flushes.
โ๏ธ The 5-Stage Pipeline
โโโโโโโโโโโโฌโโโโโโโโโโโฌโโโโโโโโโโโฌโโโโโโโโโโโฌโโโโโโโโโโโ
โ IF โ ID โ EX โ MEM โ WB โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Fetch โ Decode โ Execute โ Memory โ Write โ
โ Instr โ Regs โ ALU Ops โ Access โ Back โ
โโโโโโโโโโโโดโโโโโโโโโโโดโโโโโโโโโโโดโโโโโโโโโโโดโโโโโโโโโโโ
โ โ โ โ โ
IF/ID โโโบ ID/EX โโโบ EX/MEM โโโบ MEM/WB โโโบ Register File
Pipeline Registers carry data forward
Pipeline Timing โ 3 instructions in flight simultaneously
Instruction
C1
C2
C3
C4
C5
C6
C7
ADD R1,R2,R3
IF
ID
EX
MEM
WB
โ
โ
LW R4,0(R5)
โ
IF
ID
EX
MEM
WB
โ
SUB R6,R4,R1
โ
โ
IF
ID
EX
MEM
WB
๐ฎ Predicated Execution
Every instruction has a predicate register field Rp โ this is what makes this processor unique.
ExecuteEn = (Rp_addr == 0) OR (Reg[Rp] != 0)
Case 1: Rp = R0 โ always executes (unconditional)
Case 2: Rp = R5, R5 = 99 โ ExecuteEn = 1 โ instruction runs
Case 3: Rp = R5, R5 = 0 โ ExecuteEn = 0 โ instruction becomes NOP
What happens on skip? The instruction travels through all 5 stages normally. But in the ID stage, RegWrite, MemRead, and MemWrite are all gated to 0. No register or memory is ever modified. No pipeline flush needed โ this is more efficient than branching.
R30 (PC) โโโบ Instruction Memory (1KB ROM)
โ โ
โ 32-bit instruction
โ
โโโโบ PC + 1 Adder
โ
โโโโบ PC Control Unit
โโโ PCSrc=00 โ PC+1 (normal sequential)
โโโ PCSrc=01 โ PC+Offset (J / CALL)
โโโ PCSrc=10 โ Reg[Rs] (JR)
StallSignal=1 โ freeze IF/ID (do not fetch new instruction)
KillSignal=1 โ replace fetched instruction with NOP 0x00000000
ID โ Instruction Decode (most complex stage)
32-bit instruction
โ
โโโโบ Splitter โ Opcode | Rp | Rd | Rs | Rt | Imm | Offset
โโโโบ Control Unit โ all control signals from opcode
โโโโบ Register File โ read Rs, Rt, Rp (3 read ports)
โโโโบ Predicate Unit โ ExecuteEn = (Rp_addr==0) OR (Reg[Rp]!=0)
โโโโบ Extender โ 32-bit immediate (zero or sign extend)
โโโโบ Forwarding Muxes โ select most recent Rs, Rt, Rp values
โโโโบ Stall Unit โ Stall = MemRead_EX AND (Rd_EX==Rs/Rt/Rp_ID)
โโโโบ Kill Unit โ Kill = (PCSrc!=00) AND ExecuteEn
Control gating (key design choice):
RegWrite_actual = RegWrite_CU AND ExecuteEn AND NOT(Stall)
MemWrite_actual = MemWrite_CU AND ExecuteEn AND NOT(Stall)
MemRead_actual = MemRead_CU AND ExecuteEn AND NOT(Stall)
EX โ Execute (ALU)
ALU Input 1 โ forwarded Rs value
ALU Input 2 โ Mux(forwarded Rt, ExtImm) โ ALUSrc signal
ALU_OP encoding:
000 โ ADD (also: LW/SW address = Reg[Rs] + Imm)
001 โ SUB
010 โ OR
011 โ NOR
100 โ AND
ALU Result โโโบ EX/MEM pipeline register
โโโบ forwarding network (immediate availability)
MEM โ Memory Access
ALU Result โ memory address (lower 10 bits used for 4KB RAM)
LW: MemRead=1 โ data_out = Mem[address]
SW: MemWrite=1 โ Mem[address] = Reg[Rd]
Both signals already gated by ExecuteEn from ID stage
Predicated-false instructions: MemRead=0, MemWrite=0 โ memory unchanged
WB โ Write Back
3-to-1 Mux controlled by WB_Sel:
WB_Sel=00 โ ALU result (arithmetic / logical)
WB_Sel=01 โ Memory data (LW instruction)
WB_Sel=10 โ PC+1 (CALL instruction โ written to R31)
Final_Data_WB โ Reg[Rd] if RegWrite=1
โก Hazard Management
๐ Forwarding Network โ RAW Data Hazards
3 independent forwarding units: Rs, Rt, Rp
Priority (highest โ lowest):
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ if RegWrite_EX AND Rd_EX ==SrcAddr โ fwd = 01 โ EX stage
โ elif RegWrite_MEM AND Rd_MEM==SrcAddr โ fwd = 10 โ MEM stage
โ elif RegWrite_WB AND Rd_WB ==SrcAddr โ fwd = 11 โ WB stage
โ else โ fwd = 00 โ Register File
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ ๏ธ Rp is also forwarded โ correct predicate evaluation
even when Rp was just written by an instruction in the pipeline
โธ๏ธ Stall Unit โ Load-Use Hazard
Problem: LW result not available until end of MEM stage.
Cannot forward from EX to next instruction.
Detection:
Stall = MemRead_EX AND
(Rd_EX == Rs_ID OR Rd_EX == Rt_ID OR Rd_EX == Rp_ID)
When Stall=1:
โโโ Freeze PC (do not increment)
โโโ Freeze IF/ID register (same instruction held)
โโโ Clear ID/EX control signals (insert bubble)
Penalty: +1 cycle
๐ซ Kill Unit โ Control Hazard
Problem: When J/CALL/JR is taken, the instruction already
fetched in IF comes from the WRONG address.
Detection:
Kill = (PCSrc != 00) AND ExecuteEn
When Kill=1:
โโโ IF/ID instruction replaced with NOP (0x00000000)
Penalty: +1 cycle
Key advantage: resolving in ID (not EX) keeps penalty to 1 cycle only
๐งช Test Program Results
๐ Expected vs Actual Results โ All 30 Cycles
Register
Value
Commit Cycle
Notes
R1
10
5
ADDI R1, R0, 10
R2
15
6
ADDI R2, R1, 5
R3
15
7
ANDI R3, R2, 15
R4
255
8
ORI R4, R0, 255
R5
0xFFFFFF00
9
NORI R5, R4, 0
R6
240
10
SUB R6, R4, R2
R7
0
11
AND R7, R6, R1
R8
15
12
OR R8, R1, R2
R9
0xFFFFFFF5
13
NOR R9, R1, R0
R10
0
15
LW R10, 20(R0)
R11
0
17
ADD R11, R10, R0 โ +1 stall (load-use hazard)
R12
0
18
ADDI R12, R0, 0
R13
1
19
ADDI R13, R0, 1
R14
unchanged
โ
Predicated by R12=0 โ SKIPPED โ
R15
25
21
Predicated by R13=1 โ EXECUTES โ
R31
22
24
CALL saves return address โ
R24
0
29
Inside CALL function body โ
โ All actual results matched expected results
๐ Total: 30 cycles = N instructions + 4 pipeline stages + stall cycles + flush cycles
๐ฌ Component Testbench Results
Component
Tests
Result
ALU
ADD, SUB, OR, NOR, AND, overflow wrap
โ All passed
Data Memory
Read, Write, Write-protect
โ All passed
Extender
Zero-extend 12b, Sign-extend 12b, Sign-extend 22b
โ All passed
Predicate Unit
Rp=R0, Rpโ 0 non-zero, Rpโ 0 zero
โ All passed
Forwarding Unit
No hazard, EX forward, MEM forward
โ All passed
๐ Project Structure
pipelined-risc-processor-verilog/
โโโ src/
โ โโโ processor.v โ top-level processor
โ โโโ datapath.v โ full pipelined datapath
โ โโโ control.v โ control unit
โ โโโ alu.v โ ALU module
โ โโโ register_file.v โ 32ร32-bit register file
โ โโโ memory.v โ instruction ROM + data RAM
โ โโโ forwarding_unit.v โ RAW hazard forwarding
โ โโโ stall_unit.v โ load-use hazard detection
โ โโโ kill_unit.v โ control hazard flush
โ โโโ predicate_unit.v โ predicated execution
โ โโโ extender.v โ immediate extension
โโโ testbench/
โ โโโ testbench.v โ full simulation testbench
โโโ datapath/
โ โโโ datapath.circ โ Logisim datapath diagram
โโโ report/
โโโ report.pdf โ full report with waveforms
โถ๏ธ How to Run
vlog src/*.v testbench/testbench.v
vsim testbench
๐ Course Info
Course
ENCS4370 โ Computer Architecture
University
Birzeit University ๐ต๐ธ
Semester
Fall 2025/2026
About
5-stage pipelined 32-bit predicated RISC processor designed and implemented in Verilog - includes datapath, control path, hazard handling, and testbench verification.