















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
















Instructional lecturer: TRAN HOANG LINH Class: L Group: 23 Academic year: 2025–
Ho Chi Minh City, November 2025
Name Student ID 1 Nguyen Thanh Phong 2312626 2 Vu Tien Tuan 2313774 3 Nguyen Hoang Tuan 2313746
Ho Chi Minh City, November 2025
Faculty of Electrical and Electronic Engineering
9 Conclusion 20
References 21
Faculty of Electrical and Electronic Engineering
This project implements a single-cycle RV32I processor that can execute a compliance-style demo and drive on-board peripherals on the DE2 (Cyclone-II) board. The goals are:
Supported groups: R-type (ADD/SUB/AND/OR/XOR/SLL/SRL/SRA/SLT/SLTU), I-type ALU (ADDI/ANDI/ORI/XORI/SLLI/SRLI/SRAI/SLTI/SLTIU), LOAD/STORE (LB/LH/LW/LBU/LHU, SB/SH/SW), BRANCH (BEQ/BNE/BLT/BGE/BLTU/BGEU), JAL/JALR, LUI, AUIPC.
The single_cycle RV32I core is implemented as a textbook single-cycle datapath with separate instruc- tion memory (IMEM), register file, ALU/branch unit, control logic, and a combined memory/I/O block on the data side. For readability, the data memory (DMEM), load/store datapath, MMIO decoder, and output-peripheral registers (HEX/LEDR/LEDG/LCD, SW/KEY) are grouped into a single LSU/MMIO block in the diagram. The core still executes one instruction per clock cycle and exposes a debug PC plus MMIO registers for LEDs, seven-segment displays, switches, keys, and LCD. To make the high-level structure clear, the top-level datapath is summarized in Figure 2.1. The single_cycle core reads instructions from a synchronous ROM, fetches operands from the register file, drives the ALU and BRU, issues load/store requests into the LSU/MMIO block, and receives either DMEM data or MMIO data depending on the address region.
Figure 2.1: Milestone-2 single-cycle core with IMEM and a combined LSU/DMEM/MMIO block.
At the board level, the singlecycle_wrapper module bridges the DE2 pins (50 MHz clock, keys, switches, LEDs, seven-seg displays, LCD) to the core’s abstract I/O. It divides the clock down to 25 MHz, synchronizes reset from KEY0, and wires the MMIO LED/HEX/LCD buses to the physical pins, as summarized later in Table 3.8.
The program counter (PC) is stored in a 32-bit register and updated every cycle based on jump and branch decisions. The instruction ROM is indexed by pc[31:2] to enforce word alignment.
Faculty of Electrical and Electronic Engineering
Figure 3.1: ALU datapath with add/sub/less, shifter, and bitwise logic sub-blocks.
3.1.2 I/O Interface
Signal Width Dir Description i_op_a 32 In Operand A (rs1 or PC) i_op_b 32 In Operand B (rs2 or immediate) i_alu_op 4 In {funct7[5], funct3} opcode select o_alu_data 32 Out ALU result Table 3.1: ALU I/O specification.
3.2.1 Functionality
The branch unit (brc) provides domain-correct comparisons for RV32I branch instructions:
This structure avoids using high-level relational operators and is fully structural. At the lowest level, a 1-bit cell (Figure 3.2) receives a pair of bits a, b together with “pre_less” and “pre_equal” flags coming from more-significant bits. It updates the flags according to:
lessi = pre_less |
pre_equal & ∼ ai & bi
, equali = pre_equal & ∼
ai ⊕ bi
Faculty of Electrical and Electronic Engineering
Figure 3.2: 1-bit comparator cell propagating pre_less and pre_equal flags.
Thirty-two of these cells are chained to build the unsigned comparator, as shown in Figure 3.3. Bits are processed from MSB to LSB, so the final less and equal flags correspond to the full 32-bit unsigned comparison.
Figure 3.3: 32-bit unsigned comparator built as a ripple of 1-bit cells.
Signed comparison (Figure 3.4) wraps the unsigned block. The sign bits of i_rs1_data and i_rs2_data are examined first:
Two multiplexers implement this rule and select the proper less flag while reusing the same equal output for both domains.
Faculty of Electrical and Electronic Engineering
Figure 3.5: Structural 32x32 register file with 1 write port and 2 read ports.
3.3.2 I/O Interface
Signal Width Dir Description i_clk 1 In Clock i_rst_n 1 In Active-low reset i_rd_wren 1 In Write enable i_rd_addr 5 In Write address (rd) i_rd_data 32 In Write data i_rs1_addr 5 In Read address 1 i_rs2_addr 5 In Read address 2 o_rs1_data 32 Out Read data 1 o_rs2_data 32 Out Read data 2 Table 3.3: Register file I/O specification.
3.4.1 Functionality
The immediate generator (immgen) extracts and sign-extends immediates for all five RV32I formats: I, S, B, U, and J. B- and J-type immediates are generated with the correct bit shuffles and enforced word alignment (least significant bit set to 0). The implementation follows the official RV32I bit layout exactly.
Faculty of Electrical and Electronic Engineering
3.4.2 I/O Interface
Signal Width Dir Description i_instr 32 In Raw instruction word o_imm_I 32 Out I-type immediate o_imm_S 32 Out S-type immediate o_imm_B 32 Out B-type immediate o_imm_U 32 Out U-type immediate o_imm_J 32 Out J-type immediate Table 3.4: Immediate generator outputs for RV32I formats.
3.5.1 Functionality
The LSU/MMIO block implements the entire data-side subsystem grouped in Figure 2.1. Internally, it contains:
For store instructions, a helper block STORE_MERGE constructs a word-sized byte-lane mask and merges the new byte/half/word into the old word returned from DMEM. For load instructions, WORD_MISALIGNED and LOAD_EXTRACT build the correct 32-bit value and perform sign/zero extension based on the instruction (LB/LH/LW/LBU/LHU). Misaligned LW is supported by assembling a 32-bit result from two adjacent words using only slicing and concatenation, still avoiding shift operators. On MMIO addresses, the LSU/MMIO block forwards writes directly into the peripheral registers and multiplexes read data from the corresponding MMIO page instead of DMEM.
3.5.1.1 Top-level LSU/MMIO organization. Figure 3.6 and Figure 3.7 show the split between the top-level LSU/MMIO wrapper and the internal LSU core.
Faculty of Electrical and Electronic Engineering
and the store-merge logic, while the offset controls the load-extract and misaligned-LW blocks. A final multiplexer selects between the “DMEM load result” and the “MMIO load result” to form o_rdata.
3.5.1.2 Load datapath. The detailed load datapath is shown in Figure 3.8. For DMEM accesses, two helper blocks are used:
For MMIO loads, the same LOAD_EXTRACT logic is reused, but the input word comes from the MMIO read-data path instead of DMEM.
Figure 3.8: Load datapath inside the LSU: DMEM path with misaligned-word handling and MMIO path sharing the same extract logic.
3.5.1.3 Store path (STORE_MERGE). As shown in Figure 3.9, the store datapath never uses shift operators. The STORE_MERGE block takes the old 32-bit word from DMEM (oldw), the new store data (wd), the access size (size[2:0]: byte / half / word), and the byte offset ofs[1:0] and builds:
Faculty of Electrical and Electronic Engineering
The final DMEM write data is computed structurally as
dmem_wdata = (oldw & !mask) | (data & mask),
so bytes outside the selected lanes are preserved, while SB/SH/SW update exactly one, two, or four byte lanes in the target word.
Figure 3.9: STORE_MERGE: construct byte/half/word masks and merged write data for SB/SH/SW.
3.5.1.4 Load helpers (WORD_MISALIGNED and LOAD_EXTRACT). For loads, the LSU splits the work into two helper blocks illustrated in Figure 3.10:
Faculty of Electrical and Electronic Engineering
MMIO words located in specific pages; all decoding of fields and display control is handled inside output_peripherals. The block supports three display modes for the HEX outputs:
The auto-decimal path reuses a sequential binary-to-BCD converter and an 8-digit 7-segment encoder, implemented in the bin2bcd and hex_auto_glue modules. The LSU simply forwards MMIO writes into these registers and returns their current contents on MMIO reads; all user-visible formatting of HEX/LED/LCD is encapsulated inside the output_peripherals block.
3.5.2 I/O Interface
Signal Width Dir Description i_clk 1 In Clock i_mem_we 1 In Memory write enable i_mem_re 1 In Memory read enable i_mem_size 3 In 0: byte, 1: half, 2: word i_mem_unsigned 1 In Unsigned load (LBU/LHU) i_addr 32 In Byte address i_wdata 32 In Store data o_rdata 32 Out Load data (from DMEM or MMIO) o_mmio_we 1 Out MMIO write enable o_mmio_addr 32 Out MMIO address o_mmio_wdata 32 Out MMIO write data i_mmio_rdata 32 In MMIO read data Table 3.5: LSU/MMIO I/O specification.
3.6.1 Functionality
The control logic decodes the RV32I instruction fields (opcode, funct3, funct7) and generates:
Write-back priority is implemented as:
JALR/JAL > MEM > AUIPC > LUI > ALU,
ensuring that control-flow instructions always return pc+4 to rd, while loads override ALU results when active.
Faculty of Electrical and Electronic Engineering
3.6.2 I/O Interface
Signal Width Dir Description i_instr 32 In Raw instruction o_reg_we 1 Out Register write enable o_alu_op 4 Out ALU opcode o_br_un 1 Out Unsigned branch domain o_br_funct3 3 Out Branch function (BEQ/BNE/...) o_use_imm_b 1 Out Use immediate as ALU B operand o_mem_we 1 Out Memory write enable o_mem_re 1 Out Memory read enable o_mem_size 3 Out Access size (B/H/W) o_mem_unsigned 1 Out Unsigned load o_is_jal 1 Out JAL instruction o_is_jalr 1 Out JALR instruction o_is_branch 1 Out Branch instruction o_wb_sel_mem 1 Out WB from MEM instead of ALU o_is_lui 1 Out LUI instruction o_is_auipc 1 Out AUIPC instruction Table 3.6: Control logic I/O specification.
3.7.1 Functionality
The single_cycle module is the top-level RV32I core used in Milestone 2. It instantiates all previously described building blocks: instruction memory, control logic, register file, immediate generator, ALU, branch comparator, LSU/MMIO, output peripherals, and an optional timer wrapper. At a high level, it implements the classic single-cycle datapath shown in Figure 2.1, where one instruction is fetched, decoded, executed, and written back in a single clock cycle. The core is organized as follows:
Faculty of Electrical and Electronic Engineering
3.8.2 I/O Interface
Signal Width Dir Description CLOCK_50 1 In 50 MHz system clock KEY 4 In KEY0 = active-low reset, others for demo control SW 18 In Switch inputs LEDR 18 Out Red LEDs LEDG 9 Out Green LEDs HEX0..HEX7 7 Out Seven-segment segments (active-low) LCD_DATA 8 Out LCD data bus LCD_RS,RW,EN,ON 1 each Out LCD control lines Table 3.8: Top-level DE2 wrapper I/O summary.
All arithmetic is explicitly built from structural components:
Immediate fields strictly follow the RV32I specification for I/S/B/U/J and are always sign-extended and aligned as required.
Instruction memory (inst_mem) is implemented as an 8 KiB ROM initialized from isa_4b.hex. The PC uses byte addresses, but the ROM index discards [1:0] to enforce word alignment. Data memory is a 2 KiB word-addressed RAM with a 32-bit data path. The LSU/MMIO block computes a word-aligned address {addr[31:2], 2’b00} and an internal byte offset addr[1:0]. Stores use lane masks for SB/SH/SW; loads use extract/extend helpers and a second word read for misaligned LW. A page-based MMIO decode (using PAGE_MASK) forwards accesses in MMIO regions to the output- peripheral and SW/KEY registers instead of DMEM.
Directed and randomized tests cover all ALU ops (ADD/SUB, logical ops, shifts, SLT/SLTU), including edge values such as 0, INT_MIN, and INT_MAX. Overflow behavior is observed via the subtractor’s overflow flag and compare outputs.
The BRU is tested with:
Faculty of Electrical and Electronic Engineering
The LSU/MMIO block is stressed with:
At the core level, an instruction ROM image (e.g., isa_4b.hex) is loaded into IMEM. The testbench monitors:
Branches, jumps, and memory operations are checked against a compact golden model. Writes to x are explicitly verified to be ignored.
Self-checking SystemVerilog testbenches are used, with assertions on:
Waveform checkpoints are captured for the first 10–20 instructions to debug bring-up, after which longer randomized streams are run.
For the Milestone 2 demo on the DE2 board, we implemented a simple accumulator-based adder/sub- tractor application:
S ← S + SW_value.