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.
- I/O
- Storage
A lit wire is a read served in that cycle.
- 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.
- Cycle 3
Still the same bank. B has waited four cycles.
- 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.
- I/O
- Storage
- Data
- Structure
- One port
A single-port cell has one word line and one pair of bit lines.
- Two ports
A second port adds a second word line and a second pair of bit lines to every cell.
- 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 ports | Write ports | Yosys cells |
|---|---|---|
| 1 | 1 | 170 |
| 2 | 1 | 258 |
| 4 | 1 | 434 |
| 2 | 2 | 348 |
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.
- I/O
- Storage
A lit wire is a read served in that cycle.
- Cycle 0
Word 0 is in bank 0 and word 1 in bank 1. Both reads are served.
- Cycle 1
Word 1 is in bank 1 and word 2 in bank 0. The banks swap, and both are served again.
- 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.
- Control / FSM
- I/O
- Control
- Detect
Both ports ask, and their bank bits are equal: a conflict.
- Arbitrate
A fixed rule picks one. In the lab, port A always wins. A fairer rule alternates the winner.
- 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
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.
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.
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.
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 c7Read 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
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.
return {a[3], a[2:0]};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.