Multi-Port Memories and Banking

Serve two requests per cycle with extra ports or with banks. Then handle the cycles in which both want the same bank.

You Will Be Able To

  • Explain what an extra read or write port adds to a memory or register file.
  • Split a memory into interleaved banks and find the bank of an address.
  • Detect a bank conflict, pick a winner and stall the other request.
  • Predict which access patterns conflict.
Lesson
G8.8
Time
30 min
Runs on
Your browser
Check
5 questions

A filter reads two neighbouring samples in every cycle from a memory of two banks. It ran at half speed. Every pair of samples sat in the same bank.

Neighbours in the Same BankEach column is one clock cycle. A lit wire is a read served in that cycle. Port B is ringed while it waits.
  • I/O
  • Storage
AABBPort Areads x[k]Port Breads x[k+1]Bank 0one read per cycleBank 1one read per cycle
A→ Bank 0A→ Bank 1B→ Bank 0B→ Bank 1Port Areads x[k]Port Breads x[k+1]Bank 0one read per cycleBank 1one read per cycle

A lit wire is a read served in that cycle.

012345678910111213clkaddr_a0123456addr_b1234567a_bank0a_bank1b_bank0b_bank1b_waitsA is done
Scroll horizontally ↔
  1. Cycle 0

    A wants word 0 and B wants word 1. The top address bit picks the bank, so both are in bank 0. A wins, B waits.

  2. Cycle 3

    Still the same bank. B has waited four cycles.

  3. Cycle 7

    A has finished. Only now does B get bank 0, one word per cycle.

Measured on Icarus Verilog: the example of this lesson as it ships. 14 cycles for 14 words.

More Than One Port

A port is one address and one data path into a memory. With two ports, two accesses can happen in the same cycle.

A Memory Cell with Two PortsArrows show the wires of each port. Every port needs its own word line and its own bit lines at every cell.
  • I/O
  • Storage
  • Data
  • Structure
A memory cell with two portsArrows show the wires of each port. Every port needs its own word line and its own bit lines at every cell. io Port 1 address and data io Port 2 address and data storage One cell stores one bit storage Bigger cell more wires through it word line 1, bit lines1word line 2, bit lines2area grows
  1. io Port 1address and data
    • word line 1, bit lines 1 → One cell
  2. storage One cellstores one bit
    • area grows → Bigger cell
  3. io Port 2address and data
    • word line 2, bit lines 2 → One cell
  4. storage Bigger cellmore wires through it
  1. One port

    A single-port cell has one word line and one pair of bit lines.

  2. Two ports

    A second port adds a second word line and a second pair of bit lines to every cell.

  3. The cost

    The cell grows with every port, because each port brings its own wires through it.

Read the diagram as text
  • Port 1: address and data
  • Port 2: address and data
  • One cell: stores one bit
  • Bigger cell: more wires through it
  • Port 1 → One cell: word line 1, bit lines 1
  • Port 2 → One cell: word line 2, bit lines 2
  • One cell → Bigger cell: area grows

A processor register file often has two read ports and one write port: two operands in, one result out.

Built from flip-flops, a register file keeps one copy of the bits. Each read port is a multiplexer that picks one word. Each write port adds enable logic to every word.

Read portsWrite portsYosys cells
11170
21258
41434
22348

Measured with Yosys on a register file of 8 words of 8 bits, combinational reads. The 64 flip-flops are the same in every row.

Banks Instead of Ports

Ports are costly in a large memory. A cheaper way is to split the memory into banks. Each bank has one port. Two requests can be served together when they hit different banks.

Interleaving uses the low address bits to pick the bank. Word 0 is in bank 0, word 1 in bank 1, word 2 in bank 0 again. Neighbouring words are then always in different banks.

Neighbours in Different BanksEach column is one clock cycle. A lit wire is a read served in that cycle.
  • I/O
  • Storage
AABBPort Areads x[k]Port Breads x[k+1]Bank 0one read per cycleBank 1one read per cycle
A→ Bank 0A→ Bank 1B→ Bank 0B→ Bank 1Port Areads x[k]Port Breads x[k+1]Bank 0one read per cycleBank 1one read per cycle

A lit wire is a read served in that cycle.

0123456clkaddr_a0123456addr_b1234567a_bank0a_bank1b_bank0b_bank1b_waits
Scroll horizontally ↔
  1. Cycle 0

    Word 0 is in bank 0 and word 1 in bank 1. Both reads are served.

  2. Cycle 1

    Word 1 is in bank 1 and word 2 in bank 0. The banks swap, and both are served again.

  3. Cycle 6

    Fourteen words in seven cycles, with no conflict.

Measured on Icarus Verilog: the example with the low bit picking the bank. 7 cycles, 0 conflicts.

Top bit picks the bank

return {a[3], a[2:0]};

Icarus: 14 cycles, 7 conflicts.

Low bit picks the bank

return {a[0], a[3:1]};

Icarus: 7 cycles, 0 conflicts.

Conflicts and Stalls

A bank conflict is two requests for the same bank in one cycle. The memory needs three small pieces of logic for it.

Detect, Arbitrate, StallArrows show the order of the decisions in one cycle.
  • Control / FSM
  • I/O
  • Control
Detect, arbitrate, stallArrows show the order of the decisions in one cycle. control Compare banks bank of A == bank ofB? control Arbiter picks the winner io Winner served at the edge io Loser holds its request conflictgrantno grant
  1. control Compare banksbank of A == bank of B?
    • conflict → Arbiter
  2. control Arbiterpicks the winner
    • grant → Winner
    • no grant → Loser
  3. io Winnerserved at the edge
  4. io Loserholds its request
  1. Detect

    Both ports ask, and their bank bits are equal: a conflict.

  2. Arbitrate

    A fixed rule picks one. In the lab, port A always wins. A fairer rule alternates the winner.

  3. Serve and stall

    The winner is granted. The loser keeps its request up and tries again in the next cycle.

Read the diagram as text
  • Compare banks: bank of A == bank of B?
  • Arbiter: picks the winner
  • Winner: served at the edge
  • Loser: holds its request
  • Compare banks → Arbiter: conflict
  • Arbiter → Winner: grant
  • Arbiter → Loser: no grant

With a fixed winner, a busy port A can starve port B. Lesson R5.4 builds a round-robin arbiter.

More banks make conflicts rarer: four banks interleaved on two bits serve any two requests unless their low two bits match. A cache with many banks works the same way.

Asked in interviews
  1. Multi-port memory or banks?

    Extra ports serve any two addresses in one cycle, but every cell grows. Banks keep single-port cells and serve two requests only when they hit different banks. Banks are cheaper, but they need conflict logic.

  2. What is a bank conflict and how do you handle it?

    Two requests want the same single-port bank in one cycle. Compare the bank bits, let an arbiter grant one request, and hold the other for the next cycle.

  3. Why interleave on the low address bits?

    Sequential accesses then rotate through the banks, so neighbouring words can be read together. A stride equal to a multiple of the bank count still collides every time.

Try It

Port A reads words 0 to 6 and port B words 1 to 7. A filter that needs x[k] and x[k+1] reads this way. Each line is one cycle: the two addresses, the two grants and the conflict flag.

Every line up to e7 shows gnt=10 conflict=1. Words 0 to 7 all have a 0 as their top bit, so they are all in bank 0. Port A wins each time.

The run ends with 14 cycles, 7 conflicts. Fix the line in place: the bank must come from a[0], and the row from a[3:1].

Run again. Every line shows gnt=11 conflict=0, and the run ends with 7 cycles, 0 conflicts. The words read are the same.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: e1 a=0 b=1 gnt=10 conflict=1 e2 a=1 b=1 gnt=10 conflict=1 e3 a=2 b=1 gnt=10 conflict=1 e4 a=3 b=1 gnt=10 conflict=1 e5 a=4 b=1 gnt=10 conflict=1 e6 a=5 b=1 gnt=10 conflict=1 e7 a=6 b=1 gnt=10 conflict=1 e8 a=6 b=1 gnt=01 conflict=0 e9 a=6 b=2 gnt=01 conflict=0 e10 a=6 b=3 gnt=01 conflict=0 e11 a=6 b=4 gnt=01 conflict=0 e12 a=6 b=5 gnt=01 conflict=0 e13 a=6 b=6 gnt=01 conflict=0 e14 a=6 b=7 gnt=01 conflict=0 14 cycles, 7 conflicts, last words c6 c7
Read included file: tb.sv
// tb.sv: the bench (read-only). Port A reads words 0 to 6, port B words 1 to 7. Each port asks
// for its next word only after the last one was served. One line per clock edge.
`timescale 1ns/1ps
module tb;
  logic       clk = 0, req_a = 0, req_b = 0;
  logic [3:0] addr_a = 0, addr_b = 1;
  wire        gnt_a, gnt_b, conflict;
  wire  [7:0] data_a, data_b;
  int         cycles = 0, conflicts = 0;
  two_bank dut (.*);
  always #5 clk = ~clk;
  initial begin
    @(posedge clk); #1;
    req_a = 1; req_b = 1;
    while (req_a || req_b) begin
      #3;
      $display("e%0d a=%0d b=%0d gnt=%b%b conflict=%b", cycles + 1, addr_a, addr_b, gnt_a, gnt_b, conflict);
      conflicts += conflict;
      @(posedge clk); #1;
      cycles++;
      if (gnt_a) begin if (addr_a == 6) req_a = 0; else addr_a++; end
      if (gnt_b) begin if (addr_b == 7) req_b = 0; else addr_b++; end
    end
    $display("%0d cycles, %0d conflicts, last words %h %h", cycles, conflicts, data_a, data_b);
    $finish(0);
  end
endmodule

Check Yourself

Predict the outputFour banks are interleaved on the two low address bits. Port A reads words 0, 4, 8 and so on, and port B words 0, 2, 4. What exact line does the program print?
module tb;
  int a, b, conflicts = 0;
  initial begin
    for (int k = 0; k < 8; k++) begin
      a = 4 * k;                         // port A: words 0, 4, 8, ...
      b = 2 * k;                         // port B: words 0, 2, 4, ...
      if (a % 4 == b % 4) conflicts++;   // four banks, chosen by the two low bits
    end
    $display("conflicts=%0d", conflicts);
  end
endmodule
Spot the bugEach option is the place function of the two-bank memory: {bank, row}. The bench runs the filter and expects 7 cycles. Which option fails?
ConceptTwo requests hit the same single-port bank in one cycle. What should the memory do?
ConceptFour banks are interleaved on the two low address bits. Which pair of addresses conflicts?
ConceptWhy is a two-port SRAM cell larger than a single-port cell?

Common Mistakes

Wrong Pick the bank from the top address bit when the readers want neighbouring words.

Neighbours then live in the same bank. In the lab, port B waits seven times: 14 cycles instead of 7.

Fix Interleave: let the low address bits pick the bank, so neighbouring words sit in different banks.

Before
return {a[3], a[2:0]};
After
return {a[0], a[3:1]};

Wrong Grant both requests when they hit the same bank.

A single-port bank does one access per cycle. One of the two requests is silently lost.

Fix Compare the two banks. On a conflict, grant one request and hold the other for the next cycle.

Wrong Assume interleaving removes every conflict.

Two streams with a stride of 2 always hit the same bank. The predict item counts 8 conflicts in 8 cycles.

Fix Match the bank bits to the access pattern, or add banks. Measure the conflicts on real traffic.

Wrong Add read ports to a register file for free.

Each read port is another full multiplexer: Yosys builds 170 cells for one read port and 434 for four.

Fix Count the ports the datapath really needs. Banks or a second copy of the file can be cheaper.

Remember

  • Each extra port adds wires to every cell, or a full multiplexer to a register file.
  • Banks give parallel access with single-port cells, as long as the requests hit different banks.
  • Interleaving on the low bits puts neighbouring words in different banks.
  • A conflict needs a compare, an arbiter and a stall of the loser.
  • Some strides always conflict: check the access pattern.

Practice

Report a problem with this page