What you practice
Address alignment check (3.4). Implement address alignment checks for half-word and word accesses in the RV32I load/store unit to detect misaligned memory operations. It is in Alignment & misalign flag — Part 3 · Load/Store & Local Memory of the RISC-V RV32I Core Masterclass.
The RV32I core is Verilog you simulate in the browser IDE. Verilator runs the program; the waveform shows PC, the instruction, and the register write.
What you will learn
You will implement address alignment checks for half-word and word accesses in the RV32I load/store unit. This ensures that memory operations respect the architectural alignment requirements of the RISC-V ISA. You will also implement the byte-enable logic to handle aligned accesses correctly.
Learning objectives
- Detect misalignment for half-word accesses on odd addresses.
- Detect misalignment for word accesses on addresses not divisible by four.
- Implement correct byte-enable logic for aligned and misaligned accesses.
- Verify alignment checks using a testbench with specific stimulus patterns.
Theory
The RISC-V ISA requires specific alignment for multi-byte memory accesses. A half-word (16-bit) access must be aligned to a 2-byte boundary, meaning the least significant bit of the address must be 0. A word (32-bit) access must be aligned to a 4-byte boundary, meaning the two least significant bits of the address must be 0. The load/store unit (LSU) must detect these violations and assert a misalign signal to trigger a trap in the processor pipeline. The byte_en signal controls which bytes of the data bus are active during a transaction. For byte accesses, alignment is always satisfied. For half-word and word accesses, the LSU must check the address bits to determine if the access is legal. If the access is misaligned, the misalign signal should be asserted, and the byte_en signal should typically be disabled or set to a default value to prevent invalid memory operations.
Practice
- Open RtlStudio and select this chapter from the Examples or Lecture list.
- Confirm that
student/rtl/design.svandstudent/tb/tb_top.svappear in the Explore tree. - Open
student/rtl/ip/riscv/rv32i_lsu.svin the editor. - Locate the
misalignsignal assignment logic.
Add logic to detect misalignment: - For half-word accesses (mem_size == 2'd1), assert misalign if addr[0] == 1'b1. - For word accesses (mem_size == 2'd2 or 2'd3), assert misalign if addr[1:0] != 2'b00.
- Locate the
byte_enassignments inside theunique case (addr)blocks for the byte access case (2'd0). - Replace the
__TODO__placeholders with the correct 4-bit byte-enable patterns for each address offset (00, 01, 10, 11). - Run Simulation from the left sidebar.
- Check the Log panel for
INFO: PASSand verify thatmisalignsignals are correctly asserted for misaligned accesses. - If the test fails, verify that your alignment check logic and byte-enable logic match the expected behavior.
Summary
You implemented the address alignment check logic within the RV32I Load/Store Unit to detect invalid memory accesses. By evaluating address bits against the access size, you ensured that half-word and word operations respect their required boundaries. This practice reinforces the critical role of the LSU in maintaining data integrity and preventing hardware exceptions.
© 2026 RtlStudio. Licensed under CC BY-NC-ND 4.0. Commercial use and modifications by third parties are not permitted.
Run it in the Verilog IDE
- Open the RV32I lesson in the IDE. Fetch, decode, and execute are source you can simulate.
- Run Verilator and watch PC, the instruction, and the register write on the waveform.
- Treat a program printout as done only after the ISA step this lesson names matches the wave.
- Change the RTL in the editor, rerun Verilator, and read the waveform again in the same tab.
The lesson is done when the simulation matches Address alignment check. Change the source, run the Verilog simulator again, and read the waveform without leaving the IDE. Simulation guide · Yosys guide.