Memory Expansion and Address Decoding
Build wide and deep memories from small parts, and decode the address so that each part answers in its own range only.
You Will Be Able To
- Put parts side by side to make a wider word, one byte lane per part.
- Use the high address bits and a decoder to select one bank of a deeper memory.
- Draw an address map and decode it fully.
- Explain aliasing and overlap, and find them in a decoder.
- Lesson
- G8.7
- Time
- 30 min
- Runs on
- Your browser
- Check
- 5 questions
A peripheral register at word 52 worked. But each time the program wrote it, RAM word 20 changed too. The RAM decoder did not look at the top address bit.
ram_cs_n and dev_cs_n are the chip selects, active low. rbus is the word on the bus.- Cycle 1
Word 20 was written with 5678. It reads abcd: the later write to 52 also selected RAM bank 1, word 4.
- Cycle 2
Nothing should live at 36. RAM bank 0 answers with word 4, which holds 0444.
- Cycle 3
At 52 both the peripheral and RAM bank 1 are selected. They agree only because the same write reached both.
Measured on Icarus Verilog: the example of this lesson as it ships.
Wider Words: Parts Side by Side
A part has a fixed word width, such as 8 bits. For a 16-bit word, put two parts side by side. They share the address and the control pins. Each one holds one byte lane of the word.
- I/O
- Storage
- Data
- One address
Both parts see the same address, chip select and write enable.
- Two lanes
The low part stores bits 7:0 and the high part bits 15:8. Together they form one word.
Read the diagram as text
- Address and controls: shared by both parts
- Part, lane 0: data bits 7:0
- Part, lane 1: data bits 15:8
- 16-bit data: two lanes together
- Address and controls → Part, lane 0: same address
- Address and controls → Part, lane 1: same address
- Part, lane 0 → 16-bit data: bits 7:0
- Part, lane 1 → 16-bit data: bits 15:8
Width costs parts, not address bits: the number of words stays the same.
Deeper Memories: Chip Selects
For more words, stack banks of parts. All banks share the low address bits. The high address bits go to a decoder, which selects one bank through its chip select.
- Storage
- Control / FSM
- Processor
- Structure
- Control
- Data
- The part
An 8K x 8 part has 213 = 8,192 words, so it takes 13 address bits, A12 to A0.
- Wider
Two parts side by side make one bank of 8K x 16 words.
- Deeper
Four banks hold 32K words. 32K needs 15 address bits. A12 to A0 still go to every part.
- Decode
The two extra bits, A14 and A13, go to a 2-to-4 decoder. Its outputs are the chip selects of the four banks. Exactly one is active.
Read the diagram as text
- One 8K x 8 part: 13 address bits, 8 data bits
- One bank: 8K x 16: two parts side by side
- 2-to-4 decoder: A14 and A13 in, 4 chip selects out
- 32K x 16: 4 banks, 8 parts
- Processor: 15 address bits, 16 data bits
- One 8K x 8 part → One bank: 8K x 16: x 2: wider
- One bank: 8K x 16 → 32K x 16: x 4: deeper
- 2-to-4 decoder → 32K x 16: one bank selected
- Processor → 32K x 16: A12 to A0 to every part
- Processor → 2-to-4 decoder: A14, A13
Eight parts in all: four banks of two. Every read and write uses exactly two parts.
| Target | Parts | Address bits | Part bits | Decoder bits |
|---|---|---|---|---|
| 8K x 16 | 2 | 13 | 13 | 0 |
| 16K x 8 | 2 | 14 | 13 | 1 |
| 32K x 16 | 8 | 15 | 13 | 2 |
| 64K x 32 | 32 | 16 | 13 | 3 |
Built from 8K x 8 parts. Parts = (words / 8K) x (width / 8).
The Address Map: Full and Partial Decoding
An address map lists what answers at each address. The decoder turns the map into chip selects.
| Words | Address bits 5:4 | What answers |
|---|---|---|
| 0 to 15 | 00 | RAM bank 0 |
| 16 to 31 | 01 | RAM bank 1 |
| 32 to 47 | 10 | nothing |
| 48 to 63 | 11 | the peripheral |
The map of the lab. Each part has 16 words, so address bits 3:0 go to every part.
Full decoding checks every address bit above the part's own bits. Each part then answers in one range only. Partial decoding ignores some of those bits. It needs fewer gates, but a part then answers in several places.
- Cycle 1
Word 20 keeps 5678. The write to 52 reached the peripheral only.
- Cycle 2
At 36 no chip select is active. Nothing drives the bus, so it floats: z.
- Cycle 3
At 52 only the peripheral answers.
Measured on Icarus Verilog: the example with the RAM select fixed.
RAM select ignores A5
assign ram_area = 1'b1;
Icarus: read 20: abcd and read 36: 0444. The RAM shows up in two places.
RAM select checks A5
assign ram_area = (addr[5] == 1'b0);
Icarus: read 20: 5678 and read 36: zzzz.
Aliasing and Overlap
Aliasing: one part answers at several addresses, because the decoder ignores a bit. With the bug, RAM word 4 also appears at 36. Aliasing is harmless only where nothing else lives.
Overlap: two parts answer at the same address. With the bug, RAM bank 1 and the peripheral both answer at 52. When their words differ, the read is a fight.
| Read of 52, after a new write to RAM word 20 | Result |
|---|---|
| Partial decoding (the bug) | xXXX |
| Full decoding | abcd |
Measured on Icarus Verilog. X marks a hex digit with some unknown bits: two drivers disagree.
In hardware a fight means two outputs short each other. So a decoder review asks two questions. Does every address select at most one part? Does every part own exactly the range in the map?
Asked in interviews
How do you build a 32K x 16 memory from 8K x 8 chips?
Two chips side by side make one 8K x 16 bank. Four banks make 32K words. A12 to A0 go to every chip, and a 2-to-4 decoder on A14 and A13 drives the chip selects. That is eight chips.
What is the difference between full and partial decoding?
Full decoding checks every address bit above the chip's own bits, so each chip has one range. Partial decoding skips some bits. It is cheaper, but the chip appears at several addresses.
What is aliasing in an address map?
One device answers at more than one address because the decoder ignores some address bits. It is harmless in empty space, but a bug when another device lives at the alias.
Try It
The bench writes 0444 to word 4, 1234 to word 5, 5678 to word 20 and abcd to the peripheral at 52. Then it reads 5, 20, 36 and 52.
Word 5 is right. Word 20 reads abcd: the peripheral write also selected RAM bank 1. Address 36 is empty in the map, yet it reads 0444.
Fix the line that sets ram_area: the RAM owns only the words with addr[5] == 0. Run again.
Now word 20 reads 5678, address 36 reads zzzz with no chip select active, and 52 selects the peripheral alone.
read 5: 1234 ram_cs_n=10 dev_cs_n=1 read 20: abcd ram_cs_n=01 dev_cs_n=1 read 36: 0444 ram_cs_n=10 dev_cs_n=1 read 52: abcd ram_cs_n=01 dev_cs_n=0Read included file: tb.sv
// tb.sv: the bench (read-only). Four RAM parts and a peripheral on one bus. It writes three RAM words
// and one peripheral register, then reads four addresses. "zzzz" means that nothing answered.
`timescale 1ns/1ps
// ram16x8: one RAM part, 16 words of 8 bits (a model, read-only). The output is off (z) unless selected.
module ram16x8 (input logic clk, cs_n, we_n, input logic [3:0] a, input logic [7:0] d, output wire [7:0] q);
logic [7:0] mem [16];
assign q = (!cs_n && we_n) ? mem[a] : 8'hzz;
always @(posedge clk) if (!cs_n && !we_n) mem[a] <= d;
endmodule
// dev16: a peripheral with 16 registers of 16 bits, on the same kind of pins.
module dev16 (input logic clk, cs_n, we_n, input logic [3:0] a, input logic [15:0] d, output wire [15:0] q);
logic [15:0] r [16];
assign q = (!cs_n && we_n) ? r[a] : 16'hzzzz;
always @(posedge clk) if (!cs_n && !we_n) r[a] <= d;
endmodule
module tb;
logic clk = 0, we_n = 1;
logic [5:0] addr = 6'd0;
logic [15:0] wdata = 16'h0000;
wire [15:0] rbus; // every part drives its lane of this bus, or z
wire [1:0] ram_cs_n;
wire dev_cs_n;
always #5 clk = ~clk;
addr_decode dec (.addr(addr), .ram_cs_n(ram_cs_n), .dev_cs_n(dev_cs_n));
ram16x8 lo0 (.clk(clk), .cs_n(ram_cs_n[0]), .we_n(we_n), .a(addr[3:0]), .d(wdata[7:0]), .q(rbus[7:0]));
ram16x8 hi0 (.clk(clk), .cs_n(ram_cs_n[0]), .we_n(we_n), .a(addr[3:0]), .d(wdata[15:8]), .q(rbus[15:8]));
ram16x8 lo1 (.clk(clk), .cs_n(ram_cs_n[1]), .we_n(we_n), .a(addr[3:0]), .d(wdata[7:0]), .q(rbus[7:0]));
ram16x8 hi1 (.clk(clk), .cs_n(ram_cs_n[1]), .we_n(we_n), .a(addr[3:0]), .d(wdata[15:8]), .q(rbus[15:8]));
dev16 dev (.clk(clk), .cs_n(dev_cs_n), .we_n(we_n), .a(addr[3:0]), .d(wdata), .q(rbus));
task automatic write(input logic [5:0] at, input logic [15:0] value);
addr = at; wdata = value; we_n = 0;
@(posedge clk); #1 we_n = 1;
endtask
task automatic read(input logic [5:0] at);
addr = at; #1;
$display("read %0d: %h ram_cs_n=%b dev_cs_n=%b", at, rbus, ram_cs_n, dev_cs_n);
endtask
initial begin
write(6'd4, 16'h0444); // RAM bank 0, word 4
write(6'd5, 16'h1234); // RAM bank 0, word 5
write(6'd20, 16'h5678); // RAM bank 1, word 4
write(6'd52, 16'habcd); // the peripheral, register 4
read(6'd5); read(6'd20); read(6'd36); read(6'd52);
$finish(0);
end
endmodule
Check Yourself
Common Mistakes
Wrong Leave the top address bit out of the RAM select.
The RAM then answers in the peripheral's range too. In the lab, the write to 52 also lands in RAM word 20, which then reads abcd.
Fix Decode every address bit that is not an address bit of the part. Full decoding gives each part one range.
assign ram_area = 1'b1;assign ram_area = (addr[5] == 1'b0);Wrong Let two parts answer the same read.
Both drive the same lane, and the bits that differ fight. With the bug of the lab and a new write to word 20, a read of 52 shows xXXX.
Fix Give each address exactly one chip select. Check that no two selects can be active together.
Wrong Take the bank select from the low address bits.
Consecutive words then sit in different banks, and each bank holds a scattered set of addresses. The memory still works, but it no longer matches a map in which each bank is one block.
Fix Take the bank number from the bits just above the part's own address bits.
Wrong Wire the high byte of the data to the low-lane part.
Reads still return the right word, because the swap happens twice. But anything that reads one part alone, such as a byte write or a debugger, sees the wrong byte.
Fix Fix one rule, such as bits 7:0 in the lane-0 part, and keep it for writes, reads and byte enables.
Remember
- Parts side by side make a wider word: each one is a byte lane.
- Banks make a deeper memory: low address bits go to every part, high bits to a decoder of chip selects.
- Parts = (words / part words) x (width / part width).
- Full decoding checks every bit above the part's own bits. Partial decoding causes aliases.
- Never let two parts answer the same address: that is overlap, and the bus fights.