This project simulates a basic RISC-V CPU pipeline in C++ with 5 stages:
- IF: Instruction Fetch
- ID: Instruction Decode
- EX: Execute
- MEM: Memory Access
- WB: Write Back
It includes hazard detection, basic branching, jump handling, and register write-back support.
.
βββ main.cpp # Entry point and pipeline execution
βββ CPU.hpp / CPU.cpp # CPU pipeline logic and stage management
βββ ControlUnit.hpp # Control signal generation
βββ ALU.hpp # ALU logic and control
βββ InstructionMemory.hpp # Reads instructions into memory
βββ utils.hpp # Utility functions like ImmGen, SignedExtend, etc.
βββ machine.txt # Machine code (auto-generated from assembly)
βββ assembly.txt # Input RISC-V assembly file
βββ README.md # Project documentation
- Supports basic RISC-V instructions:
addi,beq,add,mul,sw
- Implements all five pipeline stages
- Hazard detection and basic forwarding
- Instruction memory and simple data memory
- Output of register file and memory state after simulation
Use g++ or any C++17-compatible compiler:
g++ -std=c++17 main.cpp -o riscv_sim./riscv_simPlace this RISC-V assembly code inside assembly.txt:
addi x0, x0, 1
addi x1, x1, 7
addi x1, x1, 1
addi x2, x2, 1
beq x0, x1, 16
mul x2, x2, x0
addi x0, x0, 1
beq x0, x0, -12The simulator will:
- Convert it to binary (machine.txt)
- Run it through the 5-stage pipeline
- Output register values and memory state
- You can modify the instruction set and memory size as needed.
- Only a subset of the RISC-V spec is implemented.
- Extend
ALUControlandControlUnitfor more instructions (slti,xor,sll, etc).
Developed by [Your Name]
Educational pipeline simulator for learning purposes.