Skip to content

Holmes-99/pipelined-risc-processor-verilog

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

35 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Typing SVG

Verilog Pipeline Bits Status University


Logisim DataPath of Piplined Proccessor

Piplined_proccessor_Datapath

๐Ÿ“‹ Abstract

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.


๐Ÿ“ Instruction Set Architecture

๐Ÿ”ท R-Type โ€” Opcode(5) | Rp(5) | Rd(5) | Rs(5) | Rt(5) | Unused(7)
Instruction Opcode Operation Path
ADD Rd,Rs,Rt,Rp 0 Reg[Rd] = Reg[Rs] + Reg[Rt] IFโ†’IDโ†’EXโ†’WB
SUB Rd,Rs,Rt,Rp 1 Reg[Rd] = Reg[Rs] - Reg[Rt] IFโ†’IDโ†’EXโ†’WB
OR Rd,Rs,Rt,Rp 2 Reg[Rd] = Reg[Rs] | Reg[Rt] IFโ†’IDโ†’EXโ†’WB
NOR Rd,Rs,Rt,Rp 3 Reg[Rd] = ~(Reg[Rs] | Reg[Rt]) IFโ†’IDโ†’EXโ†’WB
AND Rd,Rs,Rt,Rp 4 Reg[Rd] = Reg[Rs] & Reg[Rt] IFโ†’IDโ†’EXโ†’WB
JR Rs,Rp 13 PC = Reg[Rs] IFโ†’ID
๐Ÿ”ถ I-Type โ€” Opcode(5) | Rp(5) | Rd(5) | Rs(5) | Imm(12)
Instruction Opcode Operation Extension
ADDI Rd,Rs,Imm,Rp 5 Reg[Rd] = Reg[Rs] + Imm Sign-extend
ORI Rd,Rs,Imm,Rp 6 Reg[Rd] = Reg[Rs] | Imm Zero-extend
NORI Rd,Rs,Imm,Rp 7 Reg[Rd] = ~(Reg[Rs] | Imm) Zero-extend
ANDI Rd,Rs,Imm,Rp 8 Reg[Rd] = Reg[Rs] & Imm Zero-extend
LW Rd,Imm(Rs),Rp 9 Reg[Rd] = Mem[Reg[Rs]+Imm] Sign-extend
SW Rd,Imm(Rs),Rp 10 Mem[Reg[Rs]+Imm] = Reg[Rd] Sign-extend
๐Ÿ”ด J-Type โ€” Opcode(5) | Rp(5) | Offset(22)
Instruction Opcode Operation Penalty
J Label,Rp 11 PC = PC + SignExt(Offset) 1 cycle flush
CALL Label,Rp 12 R31 = PC+1 ; PC = PC + SignExt(Offset) 1 cycle flush

๐Ÿ—๏ธ Datapath โ€” Stage by Stage

IF โ€” Instruction Fetch
  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.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors