A 5-stage pipelined RISC-V processor core designed for FPGA deployment, featuring an integrated UART controller for system programming and debugging.
- Base ISA: RV32I (RISC-V 32-bit Integer Base Instruction Set)
- Extensions:
- Zicsr: Control and Status Register (CSR) Instructions
- Zifencei: Instruction-Fetch Fence (FENCE.I)
- Pipeline: 5-stage (Fetch, Decode, Execute, Memory, Writeback)
- Branch Prediction:
- Predictor: BHT & BTB in IF stage for zero-penalty branching
- Branch Unit: Resolution & validation in EX stage. Registers inputs for timing optimization (1-cycle latency).
- Recovery: 3-cycle penalty on misprediction (Flush ID/EX/MEM, redirect PC)
- Hazard Handling:
- Data forwarding for RAW hazards from MEM and WB stages to EX
- Store-data forwarding to resolve memory data hazards (MEM/WB)
- Load-use hazard detection with 1 cycle pipeline stall
- Branch misprediction recovery with pipeline flush
- Trap/Exception Support:
- ECALL, EBREAK, MRET
- Illegal Instruction
- Instruction/Data Address Misalign
- Instruction/Data Access Fault
- Board: ALINX AX7Z020B (Zynq-7020)
- Input Clock: 50 MHz (onboard oscillator)
- Internal Clock: 100 MHz (via MMCM)
| Signal | Type | Description |
|---|---|---|
clk |
Input | System clock |
rstn_push |
Input | Active-low asynchronous reset (Button, synchronized with 2-FF + debounce) |
uart_rx |
Input | UART Receive Data |
uart_tx |
Output | UART Transmit Data |
rstn_led |
Output | Reset status LED (active when reset asserted) |
start_led |
Output | CPU run status LED (active when CPU running) |
- Hard Reset: Physical button or MMCM lock.
- Soft Reset: Controlled via UART commands during programming.
- Initialization: PC resets to
0x00000000, pipeline flushes.
- Size: 16 KB (4096 words)
- Width: 32-bit
- Access: Runtime programmable via UART
- Description: Stores program code. Can be updated without FPGA reconfiguration.
- Size: 64 KB (16384 words)
- Width: 32-bit
- Access: Read/Write
- IO Mapping: Writes to
0xFFFF_0000(PRINT_ADDR) are redirected to UART TX for debug output.
- Ideal CPI: 1.0
- Actual CPI: Depends on program characteristics (typically 1.1-1.5 due to hazards)
| Hazard Type | Penalty (Cycles) | Detection Stage | Notes |
|---|---|---|---|
| Data Hazard (RAW) | 0 | EX | Resolved by forwarding from MEM/WB stages |
| Store-Data Hazard | 0 | MEM | Resolved by forwarding from WB stage |
| Load-Use Hazard | 1 | ID | Detect on ID → use in EX (next cycle) |
| Branch Prediction Hit | 0 | IF | Zero penalty (Seamless execution) |
| Branch Prediction Miss | 3 | EX | Flush ID/EX/MEM stages, redirect PC |
| Trap/Flush Type | Penalty (Cycles) | Processing Stage | Notes |
|---|---|---|---|
| All Traps | 3 | MEM | Flush IF, ID, EX stages, redirect to mtvec |
| MRET | 3 | MEM | Flush IF, ID, EX stages, restore PC from mepc |
| FENCE.I | 3 | MEM | Flush IF, ID, EX stages, instruction memory sync |
- Arithmetic: ADD, SUB, AND, OR, XOR, SLL, SRL, SRA, SLT, SLTU
- Immediate Arithmetic: ADDI, ANDI, ORI, XORI, SLLI, SRLI, SRAI, SLTI, SLTIU
- Load: LB, LH, LW, LBU, LHU
- Store: SB, SH, SW
- Branch: BEQ, BNE, BLT, BGE, BLTU, BGEU
- Jump: JAL, JALR
- Upper Immediate: LUI, AUIPC
- System: ECALL, EBREAK, MRET, WFI (Hint, NOP)
- Zicsr: CSRRW, CSRRS, CSRRC, CSRRWI, CSRRSI, CSRRCI
- Zifencei: FENCE.I (Instruction cache/pipeline flush)
The core implements the following Machine-mode CSRs:
| CSR Address | Name | Description |
|---|---|---|
| 0x300 | mstatus | Machine status register (MIE, MPIE bits) |
| 0x304 | mie | Machine interrupt-enable register |
| 0x305 | mtvec | Machine trap-handler base address (default: 0x40) |
| 0x340 | mscratch | Machine scratch register for trap handlers |
| 0x341 | mepc | Machine exception program counter |
| 0x342 | mcause | Machine trap cause |
| 0x343 | mtval | Machine trap value (bad address or instruction) |
| 0x344 | mip | Machine interrupt-pending register |
| 0xF14 | mhartid | Hardware thread ID (read-only, hart ID = 0) |
Integrated UART controller for communication and system control.
- System Programmer: Loads compiled programs (
.hex) into Instruction Memory. - Standard Output: MMIO-based character output for debugging.
| Parameter | Value |
|---|---|
| Baud Rate | 115200 bps |
| Data Bits | 8 bits |
| Stop Bits | 1 bit |
| Parity | None |
| Oversampling | 16x |
RX Packet (Host → FPGA)
[START: 0xA5] [CMD: 1B] [LEN: 1B] [PAYLOAD: 0~252B] [CHECKSUM: 1B]
- Payload: Address (4B) + Data (nB). All little-endian.
TX Packet (FPGA → Host)
[START: 0xA5] [RES: 1B] [LEN: 1B] [DATA: 0~4B] [CHECKSUM: 1B]
| Command | Code | Description | Payload |
|---|---|---|---|
| CMD_RESET | 0x01 | Halt and reset CPU | None |
| CMD_WRITE | 0x02 | Write to memory | Addr(4B) + Data |
| CMD_RUN | 0x03 | Start execution | None |
| Response | Code | Description | Data |
|---|---|---|---|
| RES_ACK | 0x06 | Command Success | None |
| RES_NAK | 0x15 | Command Failed | None |
| RES_PRINT | 0x80 | Async CPU Output | 4 Bytes |
Note: The programmer tool is Windows-specific due to COM port handling.
The programmer tool requires MinGW or similar GCC environment:
cd Software
gcc cpu_programmer.c serial_port.c -o cpu_programmer.exe- Program File: Place
program.hexin the root directory (../program.hexrelative to Software) - Connect: Connect FPGA board via USB.
- Run:
cpu_programmer.exe - Sequence:
RESET->WRITE->RUN
This project relies on Vivado Simulator (XSim) for functional verification.
-
Setup:
- Add
RTL/Testbench/cpu_testbench.svto the project as a Simulation Source. - Set
cpu_testbenchas the Top Module in simulation settings.
- Add
-
Waveform:
- Add
Simulation/waveform.wcfgto the simulation sources for pre-configured signal views. - This configuration includes grouped signals for each pipeline stage (IF, ID, EX, MEM, WB) and debug interfaces.
- Add
-
Execution:
- Run Behavioral Simulation.
- The testbench initializes the CPU and executes a test sequence.
RTL/: Top-level FPGA module (riscv_cpu_fpga.sv)Core/: SystemVerilog source code for the RISC-V CPU CoreUART/: Source code for the UART controller and PHYTestbench/: Simulation testbench files
Simulation/: Waveform configuration files for Vivado SimulatorSoftware/: C-based host programmer tool and libraries (Windows)Constraints/: FPGA constraint files (.xdc)
See LICENSE file for details.
Developed as part of a RISC-V CPU design project.
