How abdoahmed704/riscv-microkernel-architecture works
1. Repository purpose
- This repo is a hardware/software co-design around a bare-metal RV32I 5-stage pipelined RISC-V core.
- The main deliverable is the CPU subsystem in rtl/RV32I, with software in sw/ that boots on it and runs a small nano-OS/Snake application.
- Standard/ contains the ISA and privileged spec PDFs used as design references.
2. Primary hardware boundary: the RV32I pipeline
- rtl/RV32I/riscv_top_pipeline.sv is the top-level CPU datapath wrapper.
- The datapath is split into the usual IF, ID, EX, MEM, WB stages, each implemented in its own module:
- Fetch: riscv_fetch_stage.sv
- Decode: riscv_decode_stage.sv
- Execute: riscv_execute_stage.sv
- Memory: riscv_memory_stage.sv
- The pipeline connects instruction memory, register file, ALU, data memory, PC/branch logic, control, hazard handling, and CSR/trap handling.
3. Instruction flow through the core
- Instructions come from riscv_instruction_mem.sv, which is the fetch-side ROM/firmware source.
- Decode uses riscv_control_unit.sv and riscv_extend.sv to turn opcode fields into control signals and immediates.
- Execute uses riscv_alu.sv, riscv_pc_target.sv, and muxes to choose operand sources and branch/jump targets.
- Memory uses riscv_data_mem.sv for loads/stores, and writeback uses riscv_mux_3_1.sv to select ALU result, memory data, or PC+4.
4. Hazard and pipeline control
- riscv_hazard_unit.sv is the main control boundary for pipeline correctness.
- It handles:
- Forwarding from MEM/WB back to EX
- Load-use stalls when a value is not yet available
- Flushes on control transfers such as branches and jumps
- riscv_pc_src_controller.sv and the fetch-stage control signals determine whether the PC advances normally or is redirected.
5. Register file and architectural state
- riscv_register_file.sv implements the 32 general-purpose registers, with x0 hardwired to zero.
- The README indicates the register file writes on the falling edge to support same-cycle bypassing behavior.
- This architectural state is the main shared state between decode, execute, and writeback.
6. Privilege, CSRs, and traps
- riscv_csr_unit.sv adds privileged-machine functionality on top of the base integer core.
- The documentation files explain_Privilege.md and explain_CSR_signals.md show this is a deliberate subsystem, not just a few extra registers.
- Key CSRs include mstatus, mtvec, mepc, mcause, stvec, sepc, and scause.
- The trap model supports ecall, ebreak, mret, and sret, so software can use syscall-style transitions and exception return.
7. Memory and I/O boundary
- riscv_data_mem.sv is the main memory interface for the CPU.
- The README describes a memory-mapped virtual UART/console used by software at address 0x00003FF0, which is how the Snake demo prints and receives input.
- That makes the memory block both program data storage and the boundary to interactive I/O during simulation or emulation.
8. Software stack on top of the core
- sw/src/start.s is the bare-metal bootstrap: it initializes execution state, sets up trap vectors/stacks, and transfers control into the kernel.
- sw/src/main.c is the C kernel/application layer; in this repo it drives the Snake game and syscall-based console interaction.
- sw/src/riscv_test.s appears to be an early validation program.
- sw/linker.ld defines the firmware memory layout that must match the RTL memory map.
9. Build and image generation flow
- The software build flow is centralized in sw/Makefile, with Windows entry points in build.bat and build.ps1.
- elf2hex.py converts the compiled ELF into firmware.hex, which is the image consumed by the instruction memory in simulation and by the web emulator flow described in the README.
- sw/input.txt and play_game.py are part of the interactive simulation input path.
10. Verification and simulation
- rtl/RV32I/riscv_top_tb.sv is the main CPU testbench.
- sim/ contains focused Questa/ModelSim .do scripts for the top pipeline, control unit, hazard unit, and instruction memory.
- rtl/RV32I/run.do, run_xsim.bat, and the sim/run_*.do files show that simulation is a first-class part of the repo, not an afterthought.
11. Adjacent RTL subsystems
- Several folders are separate design lines rather than part of the main RV32I top:
- rtl/compressed_decoder for RVC decoding
- rtl/d_cache for a data cache/controller/memory subsystem
- rtl/dynamic_branch_prediction for BHT/BTB-based branch prediction
- rtl/m_extension for multiply/divide support
- rtl/tiny_inference_chip for a distinct accelerator-style neural/inference block
- These should be treated as parallel subsystems or experiments, not automatically assumed to be integrated into the main pipeline.
12. What an architecture diagram should emphasize
- The central diagram should show a single RV32I pipelined core with clear stage boundaries, plus side blocks for hazard control, CSR/trap handling, and instruction/data memories.
- A second layer should show the software firmware stack: boot code → kernel → Snake application → memory-mapped console/input.
- Separate callouts should indicate the optional/adjacent RTL projects (branch prediction, cache, M extension, inference chip) and the simulation/verification toolchain around them.
Main entry points: riscv_top_pipeline.sv RV32I Top · riscv_execute_stage.sv Execute · riscv_hazard_unit.sv Hazard Unit · riscv_decode_stage.sv Decode
Components 28
RV32I Core
Main bare-metal pipelined CPU subsystem.
- RV32I Top · Top-level wrapper for the five-stage RV32I core.
rtl/RV32I/riscv_top_pipeline.sv - Fetch · Program counter, instruction fetch, and redirect handling.
rtl/RV32I/riscv_fetch_stage.sv - Decode · Instruction decode, control generation, and immediate extension.
rtl/RV32I/riscv_decode_stage.sv - Execute · ALU operations, operand selection, and branch target computation.
rtl/RV32I/riscv_execute_stage.sv - Memory · Data memory access and memory-stage result handling.
rtl/RV32I/riscv_memory_stage.sv - Register File · 32 general-purpose registers with x0 hardwired to zero.
rtl/RV32I/riscv_register_file.sv - Control Unit · Opcode and funct decoding into control signals.
rtl/RV32I/riscv_control_unit.sv - Immediate Ext · Builds sign-extended immediates from instruction fields.
rtl/RV32I/riscv_extend.sv - ALU · Core arithmetic and logical operations.
rtl/RV32I/riscv_alu.sv - PC Target · Computes jump and branch destination addresses.
rtl/RV32I/riscv_pc_target.sv - PC Src Ctrl · Chooses normal advance versus redirected fetch path.
rtl/RV32I/riscv_pc_src_controller.sv - Hazard Unit · Forwarding, load-use stalls, and flush control.
rtl/RV32I/riscv_hazard_unit.sv - Instr Mem · Fetch-side instruction source for simulation and boot image.
rtl/RV32I/riscv_instruction_mem.sv - Data Mem · Main memory-mapped data and I/O boundary.
rtl/RV32I/riscv_data_mem.sv - CSR Unit · Machine/supervisor CSRs, traps, and return-from-trap support.
rtl/RV32I/riscv_csr_unit.sv - Writeback Mux · Selects ALU, memory, or PC+4 result for writeback.
rtl/RV32I/riscv_mux_3_1.sv
Software Stack
Boot, kernel, and demo application built for the core.
- Bootstrap · Bare-metal initialization and trap-vector setup.
sw/src/start.s - Kernel/App · Main C layer driving Snake and console/syscall interaction.
sw/src/main.c - Linker Map · Firmware layout matched to the RTL memory map.
sw/linker.ld - ELF to Hex · Converts built firmware into instruction-memory hex.
sw/elf2hex.py - Console I/O · Memory-mapped virtual UART/console used by the demo.
Adjacent RTL
Parallel design lines and experiments outside the main CPU pipeline.
- Compressed Decoder · Standalone compressed-instruction decoder subsystem.
rtl/compressed_decoder/riscv_core_compressed_decoder.sv - D Cache · Standalone data cache/controller/memory design line.
rtl/d_cache/riscv_core_dcache_top.sv - Branch Prediction · Standalone BHT/BTB-based branch prediction design.
rtl/dynamic_branch_prediction/risc_top_branch.sv - M Extension · Standalone multiply/divide implementation and verification.
rtl/m_extension/design/m_extension_top.sv - Inference Chip · Separate tiny inference accelerator subsystem.
rtl/tiny_inference_chip/top.sv
Verification
Testbenches and simulation entry points.
- Top TB · Main CPU testbench for the RV32I core.
rtl/RV32I/riscv_top_tb.sv - Sim Scripts · Questa/ModelSim scripts for core subsystems and snake run.
sim/run.do
Connections 33
- RV32I Top → Fetch: inst fetch · Top-level core instantiates the fetch stage.
- RV32I Top → Decode: decode · Top-level core instantiates the decode stage.
- RV32I Top → Execute: execute · Top-level core instantiates the execute stage.
- RV32I Top → Memory: memory · Top-level core instantiates the memory stage.
- RV32I Top → Register File: regs · Architectural register file shared across pipeline stages.
- Decode → Control Unit: control · Decode stage derives control signals from instruction fields.
- Decode → Immediate Ext: immediates · Decode stage expands instruction immediates.
- Execute → ALU: alu op · Execute stage performs arithmetic and logical operations.
- Execute → PC Target: branch target · Execute stage computes redirect targets.
- Execute → PC Src Ctrl: pc select · Execute results participate in PC redirect selection.
- Memory → Data Mem: loads/stores · Memory stage accesses data memory and MMIO.
- Fetch → Instr Mem: instrs · Fetch stage reads the instruction source.
- Hazard Unit → Fetch: stall/flush · Hazard logic controls fetch behavior.
- Hazard Unit → Decode: stall/flush · Hazard logic controls decode behavior.
- Hazard Unit → Execute: forward · Hazard logic forwards operands into execute.
- Hazard Unit → Memory: forward · Hazard logic coordinates later-stage result availability.
- CSR Unit → RV32I Top: traps · CSR/trap subsystem is integrated into the core wrapper.
- Writeback Mux → RV32I Top: wb · Writeback selection feeds the core datapath.
- PC Src Ctrl → Fetch: redirect · PC source controller steers the fetch stage.
- PC Target → PC Src Ctrl: target · Branch/jump target feeds PC source selection.
- Bootstrap → Kernel/App: boot to kernel · Bootstrap transfers control into the main firmware.
- Kernel/App → Console I/O: syscalls/mmio · Application interacts with the console via memory-mapped I/O.
- Bootstrap → Linker Map: layout · Startup code and kernel rely on the linker memory map.
- Linker Map → ELF to Hex: image build · Linked ELF is converted into hex firmware for the core.
- ELF to Hex → Instr Mem: firmware load · Hex image becomes the instruction memory contents.
- Top TB → RV32I Top: verify · CPU testbench exercises the top-level pipeline.
- Sim Scripts → Top TB: run · Simulation scripts drive the main testbench.
- Sim Scripts → RV32I Top: simulate · Scripts also target the top pipeline directly.
- Compressed Decoder → Branch Prediction: parallel rtl · Compressed decoder is a separate RTL line adjacent to the core.
- D Cache → RV32I Top: adjacent · Cache subsystem is separate from the main RV32I top.
- Branch Prediction → RV32I Top: adjacent · Branch prediction block is a separate design line.
- M Extension → RV32I Top: adjacent · Multiply/divide extension is maintained as a parallel subsystem.
- Inference Chip → RV32I Top: adjacent · Tiny inference chip is an independent accelerator project.