Loads and stores are how a program moves data between registers and memory, and they bring a new difficulty: memory is organized in 32-bit words, but programs read and write single bytes and halfwords too. This article specifies the load/store unit, cvr32_lsu, that bridges the two for exercise E08, and then adds it to your core in E09 so that it can run every load and store of RV32I. It follows real accesses from the test program byte by byte, shows why a store of one byte uses all four byte lanes, and explains why a load is the slowest instruction of a single-cycle core.

11 min read | Beginner to Intermediate

What You'll Learn

  • Map bytes and halfwords at any aligned address to the byte lanes of a 32-bit memory word
  • Generate byte enables and place store data for sb, sh and sw
  • Extract and sign- or zero-extend loaded bytes and halfwords
  • Connect the load/store unit and data memory into the single-cycle core and explain its longest path

Before You Start

You need Verilog's shift, replication and concatenation operators; the Verilog Tutorial covers them. Two articles from the first half of this tutorial explain the ideas: Memory, Addresses and Endianness (byte addresses, little-endian order, alignment and byte enables) and Loads, Stores and Addressing (the RV32I load and store instructions). E08 is a block exercise you can do any time; E09 needs your E07 core and your E08 unit.

Words, Bytes and Byte Lanes

The data memory is 32 bits wide: each access reads or writes one aligned word, at an address whose low two bits are 00. The four bytes of a word sit in four byte lanes: lane 0 is bits 7 to 0, lane 1 bits 15 to 8, lane 2 bits 23 to 16, and lane 3 bits 31 to 24. RISC-V is little-endian, so the byte at the word's lowest address is in lane 0. The low two bits of a byte address, the byte offset, say which lane a byte is in.

A store of one byte must change exactly one lane and leave the other three alone. That is what the memory's byte enables are for: one bit per lane, and the memory writes only the lanes whose bit is 1. A halfword uses two lanes, at offset 0 or 2; a word uses all four. Accesses in this core are always naturally aligned (the compiler guarantees it), so a halfword never straddles two words.

The Load/Store Unit

The load/store unit is combinational. It has a store side and a load side, which both use the access size and the byte offset:

module cvr32_lsu (
    input  logic [2:0]  funct3,       // size and sign: 000 b, 001 h, 010 w, 100 bu, 101 hu
    input  logic [1:0]  byte_off,     // address bits [1:0]
    input  logic        mem_write,    // this instruction is a store
    input  logic [31:0] store_data,   // rs2
    output logic [3:0]  wstrb,        // byte enables, all zero when not storing
    output logic [31:0] wdata,        // store data placed in its byte lanes
    input  logic [31:0] mem_rdata,    // the whole memory word read
    output logic [31:0] load_data     // the value for rd
);

funct3 comes straight from the instruction: for loads and stores, it encodes the size, and for loads also whether to sign-extend.

The Store Side

funct3InstructionwstrbData in wdata
000sb0001 shifted left by byte_offthe low byte of store_data, in lane byte_off
001sh0011 shifted left by byte_off (0 or 2)the low halfword, in lanes byte_off and byte_off + 1
010sw1111store_data

When mem_write is 0, wstrb must be 0000: the load/store unit sees every instruction, and only stores may enable a write. Only the enabled lanes of wdata matter, which allows a neat trick: instead of shifting the byte into its lane, copy it into all four lanes. The byte enables then pick the right one. The reference core does that, and you can see it in the test trace below.

The Load Side

funct3Instructionload_data
000lbthe byte in lane byte_off, sign-extended
001lhthe halfword at byte_off, sign-extended
010lwmem_rdata
100lbuthe byte, zero-extended
101lhuthe halfword, zero-extended

The memory always returns the whole word. The simplest way to pick a byte or halfword is to shift the word right by 8 times the byte offset, so that the wanted data lands in bits 7 to 0 or 15 to 0, and then extend it: copies of its top bit for the signed loads, zeros for the unsigned ones. Sign extension is the same idea as for immediates (Numbers in Hardware): a byte 0xCD is -51 as a signed number, and -51 in 32 bits is 0xFFFF_FFCD.

Real Accesses From the Test

The test program mem.S stores the word 0x89AB_CDEF at address 0x0000_1000, then reads it back in every size and with both signs. These are the reference core's results:

InstructionByte offsetBytes readResult
lw x3, 0(x1)0EF CD AB 890x89AB_CDEF
lb x4, 0(x1)0EF0xFFFF_FFEF
lb x5, 1(x1)1CD0xFFFF_FFCD
lbu x8, 0(x1)0EF0x0000_00EF
lbu x9, 3(x1)3890x0000_0089
lh x10, 0(x1)0EF CD0xFFFF_CDEF
lh x11, 2(x1)2AB 890xFFFF_89AB
lhu x12, 0(x1)0EF CD0x0000_CDEF

Then it stores the byte 0x55 with sb x14, 1(x1). The reference core puts 0x5555_5555 on dmem_wdata (the byte copied into every lane) with byte enables 0010, so only lane 1 changes, and a following lw reads 0x89AB_55EF. A halfword store of 0xFFFF at offset 2, byte enables 1100, then turns the word into 0xFFFF_55EF.

Quick Check

Q: The word at 0x1004 is 0x0000_0000. The program executes sh x2, 6(x1) with x1 = 0x1000 and x2 = 0x89AB_CDEF. What are the byte enables, which bytes change, and what does lw at 0x1004 read afterwards?

Show Answer

The address is 0x1006: word 0x1004, byte offset 2. sh stores the low halfword, 0xCDEF, in lanes 2 and 3, so the byte enables are 1100. The word becomes 0xCDEF_0000, which is what lw reads.

Adding Memory to the Core

In E09 the load/store unit joins your core. The new parts are green:

The single-cycle datapath with loads and stores: the ALU result is the data memory address; the load/store unit takes rs2 as store data and drives the byte enables and write data; the memory's read data returns through the load/store unit, and the write-back multiplexer chooses the loaded value or the ALU result.
  • Address. For loads and stores the decoder already selects rs1 and the immediate with ALU ADD, so the ALU result is the address: drive dmem_addr with it, and the load/store unit's byte_off with its low two bits.
  • Stores. The store data is rs2. dmem_we is the decoder's mem_write; dmem_wstrb and dmem_wdata come from the load/store unit.
  • Loads. dmem_re is mem_read. The memory answers dmem_rdata in the same cycle, the load/store unit extends it, and the write-back multiplexer now chooses between the ALU result (wb_sel = 0) and the loaded value (wb_sel = 1).

A store writes memory at the same clock edge at which the core moves to the next instruction, so a load right after a store already sees the new data.

Why a Load Is the Slowest Instruction

A load does the most work of any instruction in one cycle: fetch the instruction from memory, decode it and read rs1, add the immediate in the ALU, send the address to the data memory, wait for the word, pick and extend the byte, and set up the register write. That chain crosses memory twice, and it sets the clock of a single-cycle core. In a synthesized and placed version of the reference single-cycle core, with each memory read assumed to take 2 ns, the path of an ALU instruction needs 10.038 ns and the path of a load 14.373 ns, so every instruction, even an add, gets 14.373 ns. Splitting this path is the main reason to pipeline, which the last chapter of this section does.

Quick Check

Q: Using the numbers above, how much longer is a load's path than an ALU instruction's, and how much of the difference is the second memory read?

Show Answer

14.373 - 10.038 = 4.335 ns longer. The assumed data memory read accounts for 2 ns of it. Most of the rest is the way back: from the memory's data through the byte selection and extension in the load/store unit and the write-back multiplexer to the register file. Computing the address takes about as long as computing an ALU result, because it is one.

How It Is Tested

E08 tests the load/store unit on its own: every load and store size at every aligned byte offset, 60 random cases each, including words with the sign bit set in every byte. For loads it checks load_data and that wstrb is 0000; for stores the byte enables and the enabled lanes of wdata. That is 2,400 checks. Five typical mistakes were run against it, with messages like these:

Output
FAIL: sb at byte offset 1: byte enables expected 0010, got 0001
FAIL: lb at byte offset 0 of word 0x80808080: expected 0xffffff80, got 0x00000080
FAIL: lb (not a store): byte enables must be 0000, got 0001

E09 tests the core with alu.S again and then mem.S, whose 36 instructions make 25 register writes and 11 stores. The test compares every register write and every store (address, byte enables and the enabled data) with the reference core. A core that never selects the loaded value gets this, at the first load:

Output
FAIL: register write 4: expected x3 = 0x89abcdef, got x3 = 0x00001000

It wrote the address, the ALU result, instead of the data at the address.

Where Is This Used?

Byte enables are everywhere a narrow write meets a wide memory: on-chip SRAMs have a write-enable bit per byte, and buses such as AXI carry them as a "write strobe" signal with every write, which is where the name wstrb comes from. The Buses and Transactions article of the SoC Design tutorial describes them on a real bus.

Common Mistakes

Mistake #1: Forgetting to shift the byte enables

Byte enables of 0001 for every sb write lane 0 whatever the address. Only the bytes at offset 0 are right, so word-sized tests pass and the first byte array fails.

Mistake #2: Sign-extending from the wrong bit

The sign of a loaded byte is its own bit 7, after it has been moved to the bottom of the word, not bit 31 of the memory word. Extend after selecting the byte.

Mistake #3: Enabling bytes on loads

If wstrb depends only on the size and offset, it is non-zero for loads too. The memory writes only when dmem_we is 1, but the specification and the test require 0000, and a real bus would treat a load with byte enables set as a malformed access.

Your Turn in the Lab

Each exercise opens with a visible test, testbench.v, next to your design: for the load/store unit, a short list of directed checks; for the core, the program mem.S, compared write by write with the reference. Simulate it as you work and add your own checks to it. When you submit, it runs together with the hidden tests, and your edits to it do not change the grading.

  1. E08: write cvr32_lsu. Check your results against the table of real accesses above.
  2. E09: paste your E07 core and blocks plus your load/store unit, and connect the data memory.
Open exercise E08 in ChipVerify Lab Open exercise E09 in ChipVerify Lab

Summary

  • The 32-bit data memory has four byte lanes; the byte offset, address bits 1 to 0, selects the lane, and the lowest address is lane 0 (little-endian).
  • Stores set one, two or four byte enables, shifted by the offset, and place the data in those lanes; non-stores give byte enables 0000.
  • Loads shift the word right by 8 times the offset and sign- or zero-extend the byte or halfword.
  • In the core, the ALU result is the address, rs2 the store data, and the write-back multiplexer chooses the ALU result or the loaded value.
  • A load crosses memory twice in one cycle and sets the single-cycle clock period: 14.373 ns against 10.038 ns for an ALU instruction in the placed reference core.
Did you find what you were looking for?

Quiz

No quiz questions available for this article.