This guide provides step-by-step instructions on how to build a complete, 5-stage pipelined MIPS32 processor from scratch. By following these five phases, you will implement all components of a classical Von Neumann computer: CPU, Memory, Datapath, Input, and Output.
Goal: Implement the Arithmetic Logic Unit and basic combinational building blocks.
adder.sv: A simple 32-bit behavioral adder.mux2.sv, mux3.sv, mux4.sv: Multiplexers for path selection.sl2.sv: Shift-left-by-2 for branch address calculation.signext.sv: Sign-extending 16-bit immediates to 32 bits.eqcmp.sv: An equality comparator for branch resolution in the Decode stage.alu.sv): Build a 32-bit ALU that supports:
AND, OR, ADD, NOR, SUB, SLT.MULT) and division (DIV) results stored in a 64-bit HiLo register on the negedge clk.alucontrol signal to avoid collisions between instructions (e.g., mult vs nor).aludec.sv): Map MIPS funct codes and aluop signals to your 4-bit alucontrol lines.Goal: Implement registers and the structural stages of the pipeline.
dff.sv: A standard 32-bit D Flip-Flop.flopenr.sv / flopenrc.sv: Flip-flops with Enable and Synchronous Clear (crucial for stalling and flushing).regfile.sv: A three-ported 32-word register file.datapath.sv):
pcnext (handling Jump, Branch, and Exception vectors).eqcmp to minimize branch delay.EPC (Exception Program Counter) register.pcplus4D - 4 into the EPC register when Exception_Flag is high to ensure precise interrupt recovery.Goal: Implement the logic that steers data and resolves timing conflicts.
maindec.sv): Translate opcodes into 9-bit control vectors (RegWrite, RegDst, AluSrc, Branch, MemWrite, MemToReg, Jump, AluOp).hazard.sv): This is the most complex part of a pipelined CPU.
lw instruction is followed by a dependent instruction, assert stallF and stallD while asserting flushE to insert a bubble.intr) occurs, assert flushD and flushE to scrub instructions currently in the pipeline.Goal: Connect your CPU to the outside world.
imem.sv: Instruction memory (Harvard Architecture). Use parameter r = 8 for a 256-word depth.dmem.sv: Data memory.computer.sv): Connect the CPU’s PC to I-Mem and its ALU result/WriteData to D-Mem.intr pin is your primary asynchronous input. When HIGH, it triggers the hardware exception vector.$finish when the CPU writes to address 252 (0xFC).32'h8000_0180 in the datapath.Goal: Translate code and verify the hardware.
assembler.py): Use the Python script to translate .asm MIPS files into .exe hex files..org 0x180 section.sw $zero, 252($zero)).Makefile to compile the SV source with iverilog.vvp +PROG=your_program.exe.tb_exceptions.vcd. Track pcF, intr, Exception_Flag, and EPC to verify the pipeline flush at 105ns.All verified source code can be found in the reference_src/ directory. Use these files to compare your implementation against a working 5-stage pipelined model.