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.

The Write Enable Falls with the AddressEach column shows the SRAM pins after one clock edge. we_n is active low: 0 means write.
012345clka39da53cwe_na goes 3 to 9 while we_n falls
Scroll horizontally ↔
  1. Cycle 0

    The first write is over. Word 3 holds a5, and we_n is 1.

  2. Cycle 2

    The address changes from 3 to 9. we_n falls at the same edge. For a moment the SRAM still points at word 3.

  3. Cycle 4

    we_n rises 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.

Three Reads of an Array of 8 Rows and 8 ColumnsThe array holds 64 bits. The top three address bits pick the row. The low three bits pick the column.
  • Control / FSM
  • Storage
  • Datapath
addrbit linesRow decoderaddr[5:3] picks one rowWord line onlane012345670x00x80x100x180x200x280x300x38Column mux pickslane012345670x00x80x100x180x200x280x300x38
addr→ Word line onbit lines→ Column mux picksRow decoderaddr[5:3] picks one rowWord line onlane012345670x00x80x100x180x200x280x300x38Column mux pickslane012345670x00x80x100x180x200x280x300x38

Left grid: every cell on the raised word line. Right grid: the one cell the column mux lets through.

0123clkreadaddr0d2a3frow_cells11111111column001000000000010010000000
Scroll horizontally ↔
  1. 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.

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

  3. Cycle 1

    Address 2a is row 5, column 2. A different word line rises, and the mux picks a different bit line.

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

WordsOne decoder for all wordsRow decoder + column mux
6488 cells12 + 7 = 19 cells
256304 cells24 + 25 = 49 cells
1,0241,120 cells48 + 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.

The Read Path of One ColumnArrows show what drives what during one read. Each column has a pair of bit lines.
  • Power
  • Control / FSM
  • Bus
  • Storage
  • Datapath
  • Device
  • Power
  • Control
  • Data
The read path of one columnArrows show what drives what during one read. Each column has a pair of bit lines. power Precharge pulls both bit lineshigh control Row decoder raises one word line bus Bit line pair two long wires storage Memory cell holds a 0 or a 1 datapath Column mux passes one pair device Sense amplifier decides early chargeword linepulls one line downsmall differencechosen pair
  1. power Prechargepulls both bit lines high
    • charge → Bit line pair
  2. bus Bit line pairtwo long wires
    • small difference → Column mux
  3. control Row decoderraises one word line
    • word line → Memory cell
  4. storage Memory cellholds a 0 or a 1
    • pulls one line down → Bit line pair
  5. datapath Column muxpasses one pair
    • chosen pair → Sense amplifier
  6. device Sense amplifierdecides early
  1. Precharge

    Before the read, both bit lines of every column are pulled high. Then they are left alone.

  2. Word line

    The row decoder raises one word line. Each cell of that row connects to its two bit lines.

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

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

One Correct Write: Address, Then Pulse, Then HoldEach column shows the pins after one clock edge of the controller. The SRAM itself sees only the pins.
012345clkbusya39da53cwe_nthe pulse
Scroll horizontally ↔
  1. Cycle 1

    Idle. The pins still hold the last write, and we_n is 1.

  2. Cycle 2

    Setup. The address becomes 9 and the data 3c. we_n stays 1 while the decoder settles.

  3. Cycle 3

    Pulse. we_n is 0 for one cycle. The address and the data do not move.

  4. Cycle 4

    Hold. we_n rises, 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.

RuleWhy
Address stable before we_n fallsA decoder that is still moving can open the old word.
Data stable before we_n risesThe cell keeps what the bit lines hold at that moment.
Address stable after we_n risesThe 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
  1. 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.

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

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

RTLIcarus Verilog · in your browser · Lint: Verilator service · Synthesize: Yosys service
Edit the design, then Run, Lint or Synthesize
Expected: 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 violations

Original 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

Predict the outputThis small model stores the word when we_n rises. The data changes during and after the pulse. What exact line does it print?
module tb;
  logic [7:0] d, word;
  logic       we_n = 1'b1;
  always @(posedge we_n) word = d;         // the word is stored when we_n rises
  initial begin
    d = 8'h11;
    #5 we_n = 1'b0;
    #5 d = 8'h22;
    #5 we_n = 1'b1;
    #5 d = 8'h33;
    #5 $display("word=%h", word);
  end
endmodule
Spot the bugEach option is the we_n line of the four-state controller. The bench writes two words to the SRAM model, which checks the pin order. Which option fails?
ConceptA 4,096-bit SRAM is built as a square array. How many word lines and how many columns does it have?
ConceptDuring an asynchronous write, which moment stores the data in the cell?
ConceptWhat does a synchronous SRAM change for the designer who uses it?

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.

Before
assign we_n = !(state == SETUP || state == PULSE);
After
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_n pulse, then hold the address and the data.
  • The rise of we_n stores the word. oe_n and we_n are never low together.
  • A synchronous SRAM samples every input at a clock edge.

Practice

Report a problem with this page