This project implements a 32-bit RISC-V Processor (RV32IM) using SystemVerilog. The core design is a single-cycle datapath integrated with a multi-cycle Hardware Multiplier Co-processor.
The primary engineering challenge (CEP) addressed in this project is the synchronization of a slow, iterative hardware unit (32-cycle multiplier) with a fast, single-cycle processor pipeline. This required the design of a Hazard Detection Unit to stall the CPU, freeze the Program Counter (PC), and manage data consistency during multiplication operations.
- ISA Support: RISC-V RV32I (Base Integer) + RV32M (Multiplication Extension).
- Architecture: Single-Cycle Datapath for standard instructions; Multi-Cycle stall-based execution for multiplication.
- Multiplier Unit:
- Radix-2 Shift-and-Add Algorithm.
- 32-cycle latency.
- Supports
MUL,MULH,MULHSU, andMULHU. - Handles signed/unsigned operands and edge cases (e.g.,
0x80000000).
- Hazard Handling: Dedicated control logic to stall the pipeline immediately upon detecting a multiply instruction.
- Memory: Separate Instruction Memory (ROM) and Data Memory (RAM).
| Module File | Description |
|---|---|
top.sv |
Top-Level Module. Connects the Datapath, Control Unit, and Multiplier. Handles global stall logic. |
multiplier_coprocessor.sv |
The Co-Processor. Implements the 32-cycle shift-and-add logic and sign handling. |
multiplier_control.sv |
Hazard Unit. Detects MUL opcodes, asserts stall_cpu, and manages the FSM handshake. |
control.sv |
Wrapper for the main control logic. |
control_unit.sv |
Main decoder for Opcode to generate ALU, Memory, and Branch signals. |
alu_control.sv |
Decodes funct3/funct7 to drive the ALU. |
alu.sv |
Arithmetic Logic Unit. Acts as a pass-through for the multiplier result when mult_done is high. |
register_file.sv |
32x32-bit Register File with write-enable gating during stalls. |
program_counter.sv |
PC register with stall capability. |
instruction_memory.sv |
ROM initialized from instructions.mem. |
data_memory.sv |
RAM for Load/Store operations. |
imm_gen.sv |
Immediate value generator and sign extender. |
mux_2to1.sv |
Generic multiplexer for datapath routing. |
write_back_mux.sv |
Selects the final data to be written to the Register File. |
tb_top.sv |
Testbench for simulation and verification. |
The multiplier uses a sequential state machine (IDLE -> BIT0...BIT31 -> FINISH).
- Input Capture: To prevent "Ghost Data" (reading registers while they are changing), the multiplier captures inputs
aandbusing combinational logic immediately in theIDLEstate. - Sign Handling: Inputs are converted to absolute values, multiplied as unsigned integers, and the sign is restored at the end based on the instruction type (
MULHvsMULHU).
Since the base processor is single-cycle, it expects an instruction to finish in 1 clock tick. The multiplier takes 33 ticks.
- Detection: The
multiplier_controlunit detects opcode0110011withfunct7[0]=1. - Immediate Stall: It asserts
stall_cpu = 1combinatorially. - Effect:
program_counter: Freezes at the current address.register_file: Write Enable is forced low to prevent corruption.
- Completion: When the multiplier signals
done, the stall is released, andmult_write_pendingallows the result to be written to the destination register.
| Type | Instructions | Description |
|---|---|---|
| R-Type | ADD, SUB, AND, OR, XOR, SLL, SRL, SRA, SLT, SLTU | Register-Register Arithmetic |
| I-Type | ADDI, ANDI, ORI, XORI, SLLI, SRLI, SRAI, SLTI, SLTIU | Immediate Arithmetic |
| Loads | LB, LH, LW, LBU, LHU | Load from Memory |
| Stores | SB, SH, SW | Store to Memory |
| Branch | BEQ, BNE, BLT, BGE, BLTU, BGEU | Conditional Branching |
| Jump | JAL, JALR | Unconditional Jumps |
| M-Ext | MUL, MULH, MULHSU, MULHU | Hardware Multiplication |
| System | LUI, AUIPC | Upper Immediate handling |
The processor has been verified using a comprehensive testbench (tb_top.sv) and hex code (instructions.mem).
- Positive × Positive:
10 × 20 = 200(Verified) - Negative × Positive:
-10 × 25 = -250(Verified) - Negative × Negative:
-8 × -6 = 48(Verified) - Edge Case:
0 × 0 = 0(Verified) - Hazard Check: Verified that the PC does not advance and the next instruction (
ADDI) is not executed until the multiplication is complete.
- Icarus Verilog (
iverilog) - GTKWave (for waveform viewing)
- Save the source code files in a single directory.
- Create the
instructions.memfile with the hex machine code. - Compile the design:
iverilog -g2012 -o cpu_sim *.sv - Run the simulation:
vvp cpu_sim
- View the waveforms (optional):
gtkwave simulation.vcd
- Check
processor_debug.txtfor the cycle-by-cycle execution log.