Inside an SRAM: Array, Decoders and Timing
Explain how an SRAM finds one cell in its array, and drive its write cycle in the right order.
You Will Be Able To
- Trace an address through the row decoder, a word line, the bit lines and the column mux.
- Explain why the bit lines are precharged and sensed.
- Order the address, the data and the write enable of an asynchronous write cycle.
- Compare an asynchronous SRAM with a synchronous one.
- Lesson
- G8.3
- Time
- 30 min
- Runs on
- Your browser
- Check
- 5 questions
A logger wrote three words to an SRAM chip and read them back. Two of the three came back as garbage. Every value was right, but the write enable fell in the same cycle as the new address.
we_n is active low: 0 means write.- Cycle 0
The first write is over. Word 3 holds a5, and
we_nis 1. - Cycle 2
The address changes from 3 to 9.
we_nfalls at the same edge. For a moment the SRAM still points at word 3. - Cycle 4
we_nrises and 3c goes into word 9. Word 3 was hit on the way: it now reads xx.
Measured on Icarus Verilog: the example of this lesson as it ships, edges 4 to 9.
A Square Array of Cells
An SRAM keeps each bit in a small cell. The cells sit in a grid of rows and columns. One part of the address picks a row, and the other part picks a column.
- Control / FSM
- Storage
- Datapath
Left grid: every cell on the raised word line. Right grid: the one cell the column mux lets through.
- Cycle 0
Address 0d is row 1, column 5. The row decoder raises word line 1. All eight cells of row 1 drive their bit lines.
- Cycle 0
The column mux passes only column 5. Seven cells were read for nothing.
More context
This waste is the price of a short decoder. The next table shows what it saves.
- Cycle 1
Address 2a is row 5, column 2. A different word line rises, and the mux picks a different bit line.
- Cycle 2
Address 3f is the last cell: row 7, column 7.
A word line turns on a whole row. The column mux then chooses one bit line.
A wire along a row is a word line. A wire down a column is a bit line. The row decoder raises exactly one word line. The column mux connects one bit line to the output.
Why two steps? One decoder for every word grows too fast. A square array needs two small parts.
| Words | One decoder for all words | Row decoder + column mux |
|---|---|---|
| 64 | 88 cells | 12 + 7 = 19 cells |
| 256 | 304 cells | 24 + 25 = 49 cells |
| 1,024 | 1,120 cells | 48 + 59 = 107 cells |
Measured with Yosys on plain gates, for one bit per word, with decoders written as wl[addr] = 1. Other codings give other counts, but the same trend. A real SRAM uses custom circuits.
Size also costs time. Longer word lines and bit lines are slower to move. So a large memory is split into several smaller arrays.
Precharge, Then Sense
A bit line is a long wire with many cells on it. One small cell moves it slowly. So an SRAM reads in four steps, and it does not wait for a full swing.
- Power
- Control / FSM
- Bus
- Storage
- Datapath
- Device
- Power
- Control
- Data
- Precharge
Before the read, both bit lines of every column are pulled high. Then they are left alone.
- Word line
The row decoder raises one word line. Each cell of that row connects to its two bit lines.
- One line starts to fall
The cell pulls one of its two bit lines down. Which one depends on the stored bit. The line falls slowly.
- Sense
The sense amplifier sees a small difference between the two lines and turns it into a full 0 or 1.
Read the diagram as text
- Precharge: pulls both bit lines high
- Row decoder: raises one word line
- Bit line pair: two long wires
- Memory cell: holds a 0 or a 1
- Column mux: passes one pair
- Sense amplifier: decides early
- Precharge → Bit line pair: charge
- Row decoder → Memory cell: word line
- Memory cell → Bit line pair: pulls one line down
- Bit line pair → Column mux: small difference
- Column mux → Sense amplifier: chosen pair
The read is finished long before the bit line reaches 0. Lesson G9.16 builds the cell and the sense amplifier from transistors.
Precharge gives every read the same start. Sensing saves the time that a full swing would take.
The Write Cycle Has an Order
An asynchronous SRAM has no clock pin. A write is a pulse on we_n, the write enable. The pins must change in a fixed order around that pulse.
- Cycle 1
Idle. The pins still hold the last write, and
we_nis 1. - Cycle 2
Setup. The address becomes 9 and the data 3c.
we_nstays 1 while the decoder settles. - Cycle 3
Pulse.
we_nis 0 for one cycle. The address and the data do not move. - Cycle 4
Hold.
we_nrises, and that moment stores 3c in word 9. The address and the data stay for one more cycle.
Measured on Icarus Verilog: the example with its line fixed, edges 4 to 9. The model reports 0 timing violations.
| Rule | Why |
|---|---|
Address stable before we_n falls | A decoder that is still moving can open the old word. |
Data stable before we_n rises | The cell keeps what the bit lines hold at that moment. |
Address stable after we_n rises | The word line must close before another one opens. |
A datasheet gives each rule a time in nanoseconds. The model of this lesson uses 5 ns for each.
Write enable low in SETUP and PULSE
assign we_n = !(state == SETUP || state == PULSE);
Icarus: 3 timing violations. Words 3 and 9 hold xx.
Write enable low in PULSE only
assign we_n = !(state == PULSE);
Icarus: 0 timing violations. The words hold a5, 3c and 7e.
Reading, and the Synchronous SRAM
A read needs no pulse. With we_n at 1, the output follows the address after the access time. A pin named oe_n turns the output driver on.
The data pins are shared. During a write the writer drives them. During a read the SRAM drives them. Both at once is a fight, so oe_n and we_n are never low together.
A synchronous SRAM adds a register on its inputs. It samples the address, the data and the write enable at a clock edge. The order of the pins then stops being your problem.
Asynchronous SRAM
// no clock: the pins are the interface
assign q = mem[a];
always @(posedge we_n) mem[a] <= d;
You must meet each setup and hold time with your own state machine.
Synchronous SRAM
always_ff @(posedge clk) begin
if (we) mem[a] <= d;
q <= mem[a];
end
One rule: every input is stable around the clock edge. The read word comes one edge later.
Memories inside a chip are almost always synchronous. Lesson G8.1 used one.
Asked in interviews
How does an SRAM pick one cell?
The address is split in two. The row decoder raises one word line, which turns on a whole row. The column mux then picks one bit line. A square array keeps both parts small.
Why are bit lines precharged?
A bit line is long and one cell is weak. Both lines start high, and the cell pulls one down a little. A sense amplifier decides on that small difference, so the read is fast.
What is the order of an asynchronous write?
The address goes first and must be stable before the write enable falls. The data must be stable before the write enable rises. Both are held for a short time after it rises.
Try It
The bench writes a5 to word 3, 3c to word 9 and 7e to word 4. Each line shows the SRAM pins after one clock edge.
At edge 6 the address goes from 3 to 9, and we_n is already 0 on the same line. The model prints the address changed as WE fell: word 3 is damaged.
The run ends with word 3 holds xx, word 9 holds xx and 3 timing violations. Only the last word survived, because no write came after it.
Fix the line that assigns we_n: it must be 0 only in the PULSE state. Run again. Edge 6 now shows we_n=1, and the last lines are a5, 3c, 7e and 0 timing violations.
Synthesize the design to measure its cells and inspect the mapped gates.
e0 start=0 busy=0 a=0 d=00 we_n=1 SRAM: the address changed as WE fell: word 0 is damaged e1 start=1 busy=1 a=3 d=a5 we_n=0 e2 start=0 busy=1 a=3 d=a5 we_n=0 e3 start=0 busy=1 a=3 d=a5 we_n=1 e4 start=0 busy=0 a=3 d=a5 we_n=1 e5 start=0 busy=0 a=3 d=a5 we_n=1 SRAM: the address changed as WE fell: word 3 is damaged e6 start=1 busy=1 a=9 d=3c we_n=0 e7 start=0 busy=1 a=9 d=3c we_n=0 e8 start=0 busy=1 a=9 d=3c we_n=1 e9 start=0 busy=0 a=9 d=3c we_n=1 e10 start=0 busy=0 a=9 d=3c we_n=1 SRAM: the address changed as WE fell: word 9 is damaged e11 start=1 busy=1 a=4 d=7e we_n=0 e12 start=0 busy=1 a=4 d=7e we_n=0 e13 start=0 busy=1 a=4 d=7e we_n=1 e14 start=0 busy=0 a=4 d=7e we_n=1 e15 start=0 busy=0 a=4 d=7e we_n=1 word 3 holds xx word 9 holds xx word 4 holds 7e 3 timing violationsOriginal design · 20 cells · 14 flip-flops · 0 latches · 2 logic levels. Synthesis uses Yosys; counts depend on the toolchain.
Read included file: tb.sv
// tb.sv: the bench (read-only). It writes a5 to word 3, 3c to word 9 and 7e to word 4, then prints the three words.
`timescale 1ns/1ps
// sram16x8: an asynchronous SRAM of 16 words (a model, read-only). It has no clock.
// A write happens when we_n rises. The model checks three rules, each with a 5 ns limit:
// the address is stable for 5 ns before we_n falls, and for 5 ns after we_n rises;
// the data is stable for 5 ns before we_n rises.
// A word that breaks a rule is damaged: it reads as xx.
module sram16x8 (
input logic [3:0] a,
input logic [7:0] d,
input logic we_n,
output logic [7:0] q
);
logic [7:0] mem [16];
int violations = 0;
logic we_q = 1'b1;
wire [3:0] #5 a_was = a; // the address as it was 5 ns ago
wire [7:0] #5 d_was = d; // the data as it was 5 ns ago
logic [3:0] wa;
assign q = mem[a]; // a read: the word follows the address
always @(we_n) begin
if (we_q === 1'b1 && we_n === 1'b0) begin // we_n falls
we_q = 1'b0;
#1;
if (a !== a_was) begin
violations++;
mem[a_was] = 'x;
$display(" SRAM: the address changed as WE fell: word %h is damaged", a_was);
end
end else if (we_q === 1'b0 && we_n === 1'b1) begin // we_n rises: the write
we_q = 1'b1;
wa = a_was;
mem[wa] = d_was;
#1;
if (d !== d_was) begin
violations++;
mem[wa] = 'x;
$display(" SRAM: the data changed as WE rose: word %h is damaged", wa);
end
if (a !== wa) begin
violations++;
mem[wa] = 'x;
mem[a] = 'x;
$display(" SRAM: the address changed as WE rose: words %h and %h are damaged", wa, a);
end
end
end
endmodule
module tb;
logic clk = 0, rst = 0, start = 0;
logic [3:0] waddr = 4'h0;
logic [7:0] wdata = 8'h00;
wire [3:0] a;
wire [7:0] d, q;
wire we_n, busy;
int edge_n = -1;
always #5 clk = ~clk; // a 10 ns clock
always @(posedge clk) edge_n = edge_n + 1;
sram_writer dut (.clk(clk), .rst(rst), .start(start), .waddr(waddr), .wdata(wdata),
.a(a), .d(d), .we_n(we_n), .busy(busy));
sram16x8 ram (.a(a), .d(d), .we_n(we_n), .q(q));
task automatic tick(input logic do_rst, input logic do_start, input logic [3:0] addr, input logic [7:0] word);
rst = do_rst; start = do_start; waddr = addr; wdata = word;
@(posedge clk); #2;
$display("e%0d start=%b busy=%b a=%h d=%h we_n=%b", edge_n, start, busy, a, d, we_n);
endtask
task automatic write(input logic [3:0] addr, input logic [7:0] word);
tick(0, 1, addr, word);
repeat (4) tick(0, 0, addr, word);
endtask
task automatic show(input logic [3:0] addr);
$display("word %h holds %h", addr, ram.mem[addr]);
endtask
initial begin
tick(1, 0, 4'h0, 8'h00); // edge 0: reset
write(4'h3, 8'ha5);
write(4'h9, 8'h3c);
write(4'h4, 8'h7e);
show(4'h3); show(4'h9); show(4'h4);
$display("%0d timing violations", ram.violations);
$finish(0);
end
endmodule
Check Yourself
Common Mistakes
Wrong Pull the write enable low in the same cycle that the address changes.
The decoder is still moving, so the old word is hit: on Icarus the lab reports 3 violations and two damaged words.
Fix Change the address first. Pull the write enable low one cycle later, when the address is stable.
assign we_n = !(state == SETUP || state == PULSE);assign we_n = !(state == PULSE);Wrong Change the data pins at the edge where the write enable rises.
The word is stored as the write enable rises: the model reports the data changed as WE rose and the word reads xx.
Fix Hold the data for one more cycle after the write enable is high again.
Wrong Enable the SRAM output while the write enable is low.
The SRAM and the writer then both drive the same data pins, and the two values fight.
Fix Keep the output enable off for the whole write cycle. Turn it on only for a read.
Wrong Give a large array one decoder with one word line per word.
The decoder grows with the number of words: Yosys builds 1,120 cells for 1,024 word lines.
Fix Make the array square. A row decoder and a column mux for 1,024 words take 107 cells.
Remember
- An SRAM is a grid: the row decoder raises one word line, and the column mux picks one bit line.
- Bit lines are precharged, and a sense amplifier decides on a small difference.
- Asynchronous write: address stable, then the
we_npulse, then hold the address and the data. - The rise of
we_nstores the word.oe_nandwe_nare never low together. - A synchronous SRAM samples every input at a clock edge.