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.

Four Reads with the RAM Answering EverywhereEach column is one read. ram_cs_n and dev_cs_n are the chip selects, active low. rbus is the word on the bus.
0123addr5203652ram_cs_n[1]ram_cs_n[0]dev_cs_nrbus1234abcd0444abcdshould be 5678should be empty
Scroll horizontally ↔
  1. Cycle 1

    Word 20 was written with 5678. It reads abcd: the later write to 52 also selected RAM bank 1, word 4.

  2. Cycle 2

    Nothing should live at 36. RAM bank 0 answers with word 4, which holds 0444.

  3. 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.

Two 8-bit Parts Make One 16-bit MemoryArrows show the pins. The address and the controls go to both parts. Each part has its own 8 data pins.
  • I/O
  • Storage
  • Data
Two 8-bit parts make one 16-bit memoryArrows show the pins. The address and the controls go to both parts. Each part has its own 8 data pins. io Address andcontrols shared by both parts storage Part, lane 0 data bits 7:0 storage Part, lane 1 data bits 15:8 io 16-bit data two lanes together same addresssame addressbits 7:0bits 15:8
  1. io Address and controlsshared by both parts
    • same address → Part, lane 0
    • same address → Part, lane 1
  2. storage Part, lane 0data bits 7:0
    • bits 7:0 → 16-bit data
  3. storage Part, lane 1data bits 15:8
    • bits 15:8 → 16-bit data
  4. io 16-bit datatwo lanes together
  1. One address

    Both parts see the same address, chip select and write enable.

  2. 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.

Building 32K x 16 from 8K x 8 PartsEach step adds parts or a decoder. The arrows show which address bits go where.
  • Storage
  • Control / FSM
  • Processor
  • Structure
  • Control
  • Data
Building 32K x 16 from 8K x 8 partsEach step adds parts or a decoder. The arrows show which address bits go where. storage One 8K x 8 part 13 address bits, 8data bits storage One bank: 8K x 16 two parts side by side control 2-to-4 decoder A14 and A13 in, 4chip selects out storage 32K x 16 4 banks, 8 parts processor Processor 15 address bits, 16data bits x 2: widerx 4: deeperone bank selectedA12 to A0 to everypartA14, A13
  1. storage One 8K x 8 part13 address bits, 8 data bits
    • x 2: wider → One bank: 8K x 16
  2. storage One bank: 8K x 16two parts side by side
    • x 4: deeper → 32K x 16
  3. storage 32K x 164 banks, 8 parts
  4. processor Processor15 address bits, 16 data bits
    • A12 to A0 to every part → 32K x 16
    • A14, A13 → 2-to-4 decoder
  5. control 2-to-4 decoderA14 and A13 in, 4 chip selects out
    • one bank selected → 32K x 16
  1. The part

    An 8K x 8 part has 213 = 8,192 words, so it takes 13 address bits, A12 to A0.

  2. Wider

    Two parts side by side make one bank of 8K x 16 words.

  3. Deeper

    Four banks hold 32K words. 32K needs 15 address bits. A12 to A0 still go to every part.

  4. 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.

TargetPartsAddress bitsPart bitsDecoder bits
8K x 16213130
16K x 8214131
32K x 16815132
64K x 323216133

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.

WordsAddress bits 5:4What answers
0 to 1500RAM bank 0
16 to 3101RAM bank 1
32 to 4710nothing
48 to 6311the 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.

The Same Four Reads with Full DecodingEach column is one read. A chip select is active when it is 0.
0123addr5203652ram_cs_n[1]ram_cs_n[0]dev_cs_nrbus12345678zabcd
Scroll horizontally ↔
  1. Cycle 1

    Word 20 keeps 5678. The write to 52 reached the peripheral only.

  2. Cycle 2

    At 36 no chip select is active. Nothing drives the bus, so it floats: z.

  3. 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 20Result
Partial decoding (the bug)xXXX
Full decodingabcd

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
  1. 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.

  2. 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.

  3. 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.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: 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=0
Read 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

Predict the outputBank 1 holds 16 words, but its select looks at A4 only. In a space of 64 words, how many addresses select it? What exact line does the program print?
module tb;
  int answers = 0;
  initial begin
    for (int a = 0; a < 64; a++)            // every word address of a 6-bit space
      if (a[4] == 1'b1) answers++;          // bank 1 decoded from A4 alone
    $display("bank 1 answers at %0d addresses", answers);
  end
endmodule
Spot the bugEach option is the RAM-area line of the decoder. The bench writes words at 3, 19 and 51, then checks them and two empty addresses. Which option fails?
ConceptHow many 8K x 8 parts make a 32K x 16 memory?
ConceptA 16K-word RAM sits in a 64K-word address space. Its decoder uses no address bit above the RAM's own 14 bits. At how many addresses does each RAM word appear?
ConceptTwo parts drive the same data lane in one read, with different words. What does the simulation show?

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.

Before
assign ram_area = 1'b1;
After
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.

Practice

Report a problem with this page