A compact, pedagogical single-cycle RV32I (RISC-V RV32I) CPU written in Verilog. This repository implements a simple single-cycle datapath (Fetch → Decode → Execute → Memory → Writeback) intended for learning, simulation, and incremental improvement toward a synthesizable CPU.
This CPU implements the RV32I base ISA in a single-cycle style. It is optimized for clarity and education rather than top frequency. The design highlights practical tradeoffs encountered when moving from simulation-friendly Verilog to synthesizable FPGA designs.
Use it to:
- Learn RISC-V datapath/control interactions;
- Run instruction- and data-level tests with simple
imem.hex/dmem.hex; - Prototype extensions (pipeline, CSR, exceptions, caches).
- Full RV32I support for integer arithmetic, loads/stores (LB/LH/LW/LBU/LHU/SB/SH/SW), branches, JAL/JALR, LUI, AUIPC.
- Modular structure:
imem,if_stage,decoder,alu_control,alu_core,alu_top,pc_reg,regfile,mem_branch_unit,wb_controller_single,cpu_top. - Register file with write-through semantics (combinational read sees same-cycle write).
- Combined DMEM + branch unit for compactness.
- Easy-to-read code and test harnesses for simulation.
.
├── rtl/
│ ├── imem.v
│ ├── if_stage.v
│ ├── decoder.v
│ ├── alu_control.v
│ ├── alu_core.v
│ ├── alu_top.v
│ ├── pc_reg.v
│ ├── regfile.v
│ ├── mem_branch_unit.v
│ ├── wb_controller_single.v
│ └── cpu_top.v
├── tb/
│ ├── tb_cpu.v # simple testbench that instantiates cpu_top
│ ├── imem.hex # example instruction memory image
│ └── dmem.hex # example data memory image
├── doc/
│ └── note.md # design note (module guide, dataflow, timing, optimizations)
├── sim/ # example simulation scripts
├── README.md
└── LICENSE
Refer to doc/note.md for a full module-by-module explanation. Short recap:
- imem — instruction ROM, combinational read, default NOPs, supports
$readmemh. - if_stage — fetch stage wrapper (inst + pc+4).
- decoder — opcode/funct fields -> control signals, imm extraction.
- alu_control & alu_core — maps decoded ops to ALU behavior; outputs flags (
zero,slt,sltu). - alu_top — connects control to core, selects operand2 (imm vs rs2).
- pc_reg — PC register with async reset.
- regfile — 32x32 registers, combinational reads, synchronous write, write-through forwarding.
- mem_branch_unit — combined DMEM & branch decision; synchronous writes, combinational reads, supports byte/half/word accesses and sign-extension.
- wb_controller_single — selects writeback source into regfile.
-
tb/imem.hex— put a minimal program (one instruction per line, hex, little-endian style according to your assembler or manual encoding). -
tb/dmem.hex— initial data for memory inspections. -
Create a testbench that:
- Asserts reset for a few cycles,
- Steps the clock,
- Monitors
dbg_pc, register file content, memory locations, - Optionally diffs expected memory/reg states.
Suggested tests:
- Arithmetic sequence to validate ALU ops.
- Loads/stores at different alignments (aligned & unaligned).
- Branches, JAL/JALR, LUI/AUIPC correctness.
-
Simulation-friendly constructs:
mem_branch_unituses blocking assignments and combinational reads to show "write-through" immediately. This is convenient in simulation but not BRAM-accurate. When targeting FPGA:- Map instruction/data memory to vendor BRAM primitives or infer block RAM;
- Adjust to synchronous read semantics (1-cycle latency) or pipeline memory accesses.
-
Single-cycle timing: entire instruction executes in one clock — long critical path and low maximum frequency. Use pipelining to improve performance.
-
CSR/SYSTEM: system instructions and traps are not implemented (opcode
1110011markedillegal). -
Non-aligned accesses: design handles common aligned cases; unaligned accesses may not be fully supported or require special handling.
- Make DMEM/IMEM BRAM-friendly (synchronous reads).
- Convert to a pipelined CPU (IF/ID/EX/MEM/WB) with hazard detection and forwarding.
- Implement CSR, exceptions, and interrupt support.
- Add a basic assembler/test generation script and a compliance test suite.
- Add a minimal runtime (bootloader) and run small C programs (via toolchain) to exercise ISA.
This project is released under the MIT License.