Skip to content

DevanshuDangi/CPU-

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 

Repository files navigation

RISC-V 5-Stage Pipeline CPU Simulator

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.


πŸ“ Project Structure

.
β”œβ”€β”€ 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

πŸš€ Features

  • 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

πŸ”§ How to Compile and Run

πŸ”¨ Compile

Use g++ or any C++17-compatible compiler:

g++ -std=c++17 main.cpp -o riscv_sim

▢️ Run

./riscv_sim

πŸ§ͺ Example: Factorial of 7

Place 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, -12

The simulator will:

  • Convert it to binary (machine.txt)
  • Run it through the 5-stage pipeline
  • Output register values and memory state

πŸ“Œ Notes

  • You can modify the instruction set and memory size as needed.
  • Only a subset of the RISC-V spec is implemented.
  • Extend ALUControl and ControlUnit for more instructions (slti, xor, sll, etc).

🧠 Credits

Developed by [Your Name]
Educational pipeline simulator for learning purposes.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages