What you practice
Load/store control and byte enables (3.1). Implement per-byte write enable generation and data alignment logic for the RISC-V Load/Store Unit based on access size and address offset. It is in LSU control & byte_en — 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
Today you will implement the logic that generates per-byte write enables based on the access size and address offset. This logic is critical for correctly handling byte, half-word, and word memory operations in a RISC-V processor.
Learning objectives
- Generate the correct 4-bit
byte_enmask for byte, half, and word accesses. - Align
store_wdatato the correct byte lane based on the address offset. - Gate the byte enables with the
memwritesignal to prevent unintended writes. - Detect alignment violations for half-word and word accesses.
Theory
The Load/Store Unit (LSU) must translate a generic memory request into specific byte-level control signals. The mem_size input indicates whether the operation is a byte (0), half-word (1), or word (2). The addr input provides the lower bits of the address, which determine the byte offset within a 32-bit word.
For a byte access (mem_size == 2'd0), only one byte is enabled. The enabled byte corresponds to the offset in addr[1:0]. For example, addr == 2'b00 enables the least significant byte (4'b0001).
For a half-word access (mem_size == 2'd1), two adjacent bytes are enabled. The alignment check misalign = addr[0] ensures the address is even for half-word accesses. If addr[1] == 0, the lower two bytes are enabled (4'b0011). If addr[1] == 1, the upper two bytes are enabled (4'b1100).
For a word access (mem_size == 2'd2), all four bytes are enabled (4'b1111). The alignment check misalign = |addr ensures the address is word-aligned.
Practice
- Open RtlStudio and select this chapter from the Examples list.
- Navigate to the Explore panel and open
student/rtl/ip/riscv/rv32i_lsu.svandstudent/tb/tb_top.sv. - Locate the
always_combblock inside the module. - Implement the byte enable mask for byte accesses (
mem_size == 0) usingaddr[1:0]. - Implement the byte enable mask for half-word accesses (
mem_size == 1) usingaddr[1]. - Implement the byte enable mask for word accesses (
mem_size == 2) as all ones. - Implement the
store_wdataalignment logic for each access size. - Run the simulation by clicking Simulation in the sidebar and then Run.
Summary
You implemented the logic to generate per-byte write enables based on access size and address offset within the Load/Store Unit. This practice reinforced how to map address alignment to specific byte enable masks for byte, half-word, and word operations. The key takeaway is ensuring that memory writes are precisely gated by the memwrite signal and aligned correctly to the target bytes.
© 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 Load/store control and byte enables. Change the source, run the Verilog simulator again, and read the waveform without leaving the IDE. Simulation guide · Yosys guide.