The SRAM Port and Its Wrapper

An SRAM looks at its pins on one clock edge and does one thing. The bus hands over a transfer in two cycles. A small block between them makes the two fit.

You Will Be Able To

  • Read the port of an SRAM: what it samples on a clock edge, and when read data appears.
  • Turn a bus transfer into a word address and byte strobes.
  • Explain why a bus needs a wrapper in front of an SRAM, and when a read must wait.
Lesson
S3.15
Time
26 min
Runs on
Your browser
Check
6 questions

A test writes a word into the SRAM and looks at the data output in the next cycle. It sees the old word. The memory is not broken, and the write did happen. The test asked the wrong pin at the wrong time.

Before You Start

You need S3.1: a memory macro, and a port that does one access at each edge. You need S0.5: the address phase, the data phase and HREADY.

You also need bytes and hexadecimal numbers, to split a byte address into a word and a byte inside it.

Five questions this page answers
Question
1When does an SRAM look at its pins, and when does a read show its word?
2What do the byte strobes of a write do?
3What does the data output show after a write?
4How does a bus transfer become a word address and strobes?
5Why does a bus need a wrapper in front of an SRAM, and when must a read wait?

The Idea: One Edge, One Access, and a Bus That Takes Two Cycles

Most of the memory on a small chip is SRAM, and it comes as a memory macro. You do not see its inside. You get its pins and its timing. The timing is simple and strict: the block looks at its pins on one rising clock edge, and does one thing.

That one thing is a read or a write of one word. For a read, the word shows up on the data output after the edge. For a write, a few extra pins say which bytes of the word to change. So a program can store one byte without touching its three neighbours.

The bus does not work that way. It hands over the address in one cycle and the write data in the next. It also counts in bytes, where the SRAM counts in words. So a small block of logic stands between the bus and the SRAM.

That block has one hard moment. A write is done late, when its data has come. The next read wants the port at the same edge. The port can do one thing, so one of the two must wait. Getting this wrong gives memory bugs that look like broken silicon.

New Words

New words on this page
WordMeaning
Chip enableA pin of a memory. At an edge where it is 0, the memory does nothing.
Write enableA pin of a memory. At an enabled edge, 1 means write and 0 means read.
Byte strobeOne bit for each byte of a word. On a write, only bytes whose strobe is 1 are changed.
Word addressThe number of a whole word in a memory. For words of four bytes it is the byte address divided by four.
Offset in a memoryA byte address, counted from the first byte of that memory.
Wrapper of a memoryThe logic between a bus and the port of a memory. It turns each bus transfer into one access.

One Picture: The Port, the Bus and the Wrapper

From Two Bus Cycles to One Clock EdgeStep through the picture. It starts with the SRAM alone, and then puts the bus in front of it.
  • Bus
  • Control / FSM
  • Memory
  • Processor
From Two Bus Cycles to One Clock EdgeStep through the picture. It starts with the SRAM alone, and then puts the bus in front of it. component Fast bus address in one cycle,write data in the next component Wrapper makes the bus fit theport component Port of theSRAM enable, write enable,strobes, address,data component Words 16,384 words of 32bits component Data output the word of the lastread a transfereverything for oneedgeone wordafter a read edgeread data
  1. component Port of the SRAMenable, write enable, strobes, address, data
    • one word → Words
  2. component Words16,384 words of 32 bits
    • after a read edge → Data output
  3. component Data outputthe word of the last read
    • read data → Wrapper
  4. component Fast busaddress in one cycle, write data in the next
    • a transfer → Wrapper
  5. component Wrappermakes the bus fit the port
    • everything for one edge → Port of the SRAM
  1. The SRAM alone

    The SRAM has one port. At each rising clock edge it looks at its pins and does one thing: a read, a write, or nothing.

  2. A read

    Chip enable is 1 and write enable is 0. The SRAM takes the address at the edge. The word appears on the data output after the edge, and stays there.

  3. A write

    Chip enable and write enable are 1. Address, data and byte strobes must all be on the pins at the same edge. Only the strobed bytes change.

  4. The bus in front

    The bus of S0.5 gives the address in its address phase and the write data one cycle later. The SRAM wants both at one edge.

  5. The wrapper

    A read can pass straight through: the SRAM needs only the address. A write is kept for one cycle, until its data has come.

  6. Two transfers, one edge

    A kept write and the next read can want the same edge. The port can do one thing. So the write goes first, and the read waits one cycle.

Read the diagram as text
  • Fast bus: address in one cycle, write data in the next
  • Wrapper: makes the bus fit the port
  • Port of the SRAM: enable, write enable, strobes, address, data
  • Words: 16,384 words of 32 bits
  • Data output: the word of the last read
  • Fast bus → Wrapper: a transfer
  • Wrapper → Port of the SRAM: everything for one edge
  • Port of the SRAM → Words: one word
  • Words → Data output: after a read edge
  • Data output → Wrapper: read data

The SRAM of ap_soc. The lab below is the port and the words. The wrapper is the practice problem of this page.

The Smallest Example, Worked by Hand

First the port alone, for five clock edges. Word 3 is written, read, written in one byte only, left alone, and read again. Fill in the last two columns yourself before you read them.

Five edges at the port of the SRAM
EdgeChip enableWrite enableStrobesWrite dataWord 3 after the edgeData output after the edge
e11111111122334411223344nothing yet: no read so far
e2101122334411223344
e3110100aabbccdd11bb334411223344: a write does not change it
e4011bb334411223344
e51011bb334411bb3344

The strobes are written with byte 3 on the left and byte 0 on the right. Strobes 0100 pick byte 2: of aabbccdd, only bb is stored.

Now the bus side. A bus transfer names a byte address and a size. The wrapper must turn that into a word address and four strobes. The SRAM of ap_soc starts at 0x80000000, so only the offset inside it counts.

Three bus writes, turned into word address and strobes
Bus writeOffset in binaryWord addressByte inside the wordStrobes
one byte at offset 0x0d0000 1101310010
two bytes at offset 0x0e0000 111032 and 31100
four bytes at offset 0x100001 000040 to 31111

The two low bits of the offset pick the byte. The other bits are the word address.

The Rules

  1. Rule 1: An SRAM looks at all its pins on one rising clock edge.

    Chip enable, write enable, address, strobes and write data must all be right at that edge.

  2. Rule 2: A read shows its word after the edge.

    The word is on the data output during the next cycle. It is one cycle behind its address.

  3. Rule 3: A write changes only the bytes whose strobe is 1.

    The other bytes of the word keep their value.

  4. Rule 4: The data output changes at a read edge and at no other time.

    After a write, or an edge with chip enable at 0, it still shows the word of the last read.

  5. Rule 5: The word address is the offset without its two low bits.

    Those two bits, with the size of the transfer, give the strobes.

  6. Rule 6: One port does one access at each edge.

    So the wrapper keeps a write for one cycle, and a read that meets that write waits one cycle.

Sources for These Rules

The textbook "Computer Organization and Design" by Patterson and Hennessy, section 5.2: SRAM, DRAM and flash, and what each costs.

The textbook "CMOS VLSI Design" by Weste and Harris: SRAM in section 12.2, DRAM in 12.3, ROM and flash in 12.4, fuses and antifuses in 3.4.3.8.

The textbook "Digital Design and Computer Architecture" by Harris and Harris, section 5.5: memory arrays, register files and ROM.

Arm AMBA AHB Protocol Specification, IHI 0033 issue C: the two phases of a transfer in section 3.1, the transfer size in 3.4, the byte lanes in 6.2.

Rules 1 to 4 describe our model of the SRAM, ap_sram. Real memory macros follow rules 1 to 3 in this form. Rule 4 is a choice: some macros show the new data after a write, and some show nothing defined. Our reference library holds no data sheet of a memory generator, so rule 4 is given as common practice, not checked.

The wrapper of rule 6, with one wait cycle, is our choice for ap_soc. Other wrappers keep the write in a small buffer and never wait.

Now Run It

The lab at the end of this page is the SRAM of ap_soc alone. Test code sets the pins before each clock edge and prints the data output just after it. There is no bus and no wrapper in this lab.

Printed by the lab on Icarus, one row for each clock edge. An x means: no value yet.
e1 write word=3 be=1111 wdata=11223344 | rdata=xxxxxxxx
e2 read  word=3 be=0000 wdata=00000000 | rdata=11223344
e3 write word=3 be=0100 wdata=aabbccdd | rdata=11223344
e4 idle  word=0 be=0000 wdata=00000000 | rdata=11223344
e5 read  word=3 be=0000 wdata=00000000 | rdata=11bb3344

Rule 2 at work: the read at e2 shows 11223344, the word written at e1. Rule 3 at work: the write at e3 has strobes 0100, and the read at e5 shows 11bb3344. Only one byte changed.

Rule 4 at work: at e3 and e4 the data output still shows 11223344. The word in the memory changed at e3. The output did not, because no read was made.

The first row shows rdata as x. No read has been made yet, so the output holds nothing. A real SRAM is the same after power-up.

This lab leaves out the wrapper. It is the practice problem of this page, and you build it there.

Why It Is Built This Way

Design choiceThe alternativeWhat the choice buys, and costs
Everything sampled on one edgeA memory with no clockTiming is checked like that of a flip-flop. A read takes a cycle.
Byte strobesWhole words onlyA store of one byte needs one access. Without strobes it needs a read and then a write.
A word address at the portA byte address at the portTwo address pins fewer, and no adder. The wrapper must work out the strobes.
A wait for a read after a writeA buffer that holds the write and answers such reads itselfVery little logic. One lost cycle, each time a read follows a write.

The Trap: The Read-Back That Showed the Old Word

Back to the test from the top of the page. It wrote 55667788 into word 3 and then compared the data output with 55667788. The compare failed, and the memory macro was blamed.

The same lab with other steps, and one more row that looks inside the model.
e1 write word=3 be=1111 wdata=11223344 | rdata=xxxxxxxx
e2 read  word=3 be=0000 wdata=00000000 | rdata=11223344
e3 write word=3 be=1111 wdata=55667788 | rdata=11223344
after the write: rdata=11223344, and word 3 of the memory holds 55667788

The last row settles it. Word 3 holds 55667788, so the write worked. The data output shows 11223344, the word of the read at e2. Rule 4 held: a write is not a read.

The dangerous part is what happens on another model. Some memory models do show the new data after a write. On those, the wrong test passes. Then the real macro arrives, and the test fails on the chip. A test must make a read to see a word.

At Scale

ap_soc1 SRAM, 1 wrapper
A phone chiphundreds of macros

This section looks ahead. A large chip has hundreds of memory macros, and each one has a wrapper of some kind.

  1. Testing every bit

    A macro cannot be reached by scan. So the chip gets logic that tests each macro, and it shares the port with the wrapper: Memory Self-Test.

  2. Wrappers that never wait

    A fast wrapper keeps the late write in a buffer and answers a read of the same word from that buffer.

  3. Wider strobes

    A bus of 64 bits has eight byte lanes, and its memories have eight strobes. The rule is the same.

  4. SRAM as a cache

    The fastest use of SRAM is next to the processor. The next lesson asks what that buys.

Try It

Run the lab. Check the last column of each row against your hand table.

Change the strobes of the second write from 4'b0100 to 4'b0011. Predict the last row.

Set the chip enable of the first read to 0. Which rows change, and which rule says so?

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: e1 write word=3 be=1111 wdata=11223344 | rdata=xxxxxxxx e2 read word=3 be=0000 wdata=00000000 | rdata=11223344 e3 write word=3 be=0100 wdata=aabbccdd | rdata=11223344 e4 idle word=0 be=0000 wdata=00000000 | rdata=11223344 e5 read word=3 be=0000 wdata=00000000 | rdata=11bb3344
Read included file: ap_sram.sv
// A model of the on-chip SRAM of ap_soc: one port, words of 32 bits, one strobe for each byte.
// Everything is sampled on the rising clock edge. A read shows its word after that edge.
// This is a model for simulation. A real chip uses a block made by a memory generator, with these pins.
module ap_sram #(parameter int WORDS = 16384) (
  input  logic                     clk,
  input  logic                     ce,     // chip enable: 1 = this edge is an access
  input  logic                     we,     // 1 = write, 0 = read
  input  logic [3:0]               be,     // byte strobes of a write. Bit 0 is the lowest byte
  input  logic [$clog2(WORDS)-1:0] addr,   // a word address, not a byte address
  input  logic [31:0]              wdata,
  output logic [31:0]              rdata
);
  logic [31:0] mem [0:WORDS-1];
  always_ff @(posedge clk) begin
    if (ce && we) begin
      for (int i = 0; i < 4; i++)
        if (be[i]) mem[addr][8*i +: 8] <= wdata[8*i +: 8];   // only the strobed bytes change
    end
    if (ce && !we) rdata <= mem[addr];                       // rdata changes on a read edge only
  end
endmodule

Check Yourself

Predict the outputWord 9 is written whole with 11223344. Then it is written with strobes 0011 and data aabbccdd, and then read. What does the last row say?
// The on-chip SRAM of ap_soc, alone. Test code drives its port and prints one row for each clock edge.
module tb;
  logic clk = 0;
  logic        ce = 0, we = 0;     // chip enable, write enable
  logic [3:0]  be = 0;             // one strobe for each byte of a write
  logic [13:0] addr = 0;           // a word address: 16,384 words need 14 bits
  logic [31:0] wdata = 0;
  wire  [31:0] rdata;
  ap_sram sram (.*);               // the memory itself, in the second file
  int edge_no = 0;

  // One clock edge. The inputs are set before the edge. rdata is printed just after it.
  task automatic step(input string what, input logic c, input logic w, input logic [3:0] b,
                      input logic [13:0] a, input logic [31:0] d);
    ce = c;  we = w;  be = b;  addr = a;  wdata = d;
    #5 clk = 1;  edge_no++;
    #1 $display("e%0d %-5s word=%0d be=%b wdata=%08h | rdata=%08h", edge_no, what, a, b, d, rdata);
    #4 clk = 0;
  endtask

  initial begin
    step("write", 1, 1, 4'b1111, 9, 32'h1122_3344);
    step("write", 1, 1, 4'b0011, 9, 32'haabb_ccdd);   // the two low bytes of word 9
    step("read",  1, 0, 4'b0000, 9, 0);
  end
endmodule
Read included file: ap_sram.sv
// A model of the on-chip SRAM of ap_soc: one port, words of 32 bits, one strobe for each byte.
// Everything is sampled on the rising clock edge. A read shows its word after that edge.
// This is a model for simulation. A real chip uses a block made by a memory generator, with these pins.
module ap_sram #(parameter int WORDS = 16384) (
  input  logic                     clk,
  input  logic                     ce,     // chip enable: 1 = this edge is an access
  input  logic                     we,     // 1 = write, 0 = read
  input  logic [3:0]               be,     // byte strobes of a write. Bit 0 is the lowest byte
  input  logic [$clog2(WORDS)-1:0] addr,   // a word address, not a byte address
  input  logic [31:0]              wdata,
  output logic [31:0]              rdata
);
  logic [31:0] mem [0:WORDS-1];
  always_ff @(posedge clk) begin
    if (ce && we) begin
      for (int i = 0; i < 4; i++)
        if (be[i]) mem[addr][8*i +: 8] <= wdata[8*i +: 8];   // only the strobed bytes change
    end
    if (ce && !we) rdata <= mem[addr];                       // rdata changes on a read edge only
  end
endmodule
ConceptAt which moment does a read of an SRAM show its word?
ConceptA word holds 11223344. It is written with strobes 1000 and data aabbccdd. What does it hold now?
ConceptA write has just been made, and no read since. What does the data output show?
ConceptA bus write of one byte goes to offset 0x2a of the SRAM. Which word address and strobes follow?
ConceptWhy must a read sometimes wait at the wrapper?

Common Mistakes

Wrong Look at the data output after a write to check the write.

The output changes at a read edge only.

Fix Make a read of that word, and look one cycle later.

Wrong Give the SRAM a byte address.

Its address counts words. The two low bits of a byte address pick the strobes.

Fix Drop the two low bits for the address, and use them for the strobes.

Wrong Send a write to the SRAM in its address phase.

The write data is not on the bus yet.

Fix Keep the address and the strobes for one cycle, and write in the data phase.

Remember

  • An SRAM looks at all its pins on one clock edge. A read shows its word in the next cycle.
  • A write changes only the strobed bytes, and does not change the data output.
  • The word address is the offset without its two low bits. Those bits and the size give the strobes.
  • The bus gives address and write data in two cycles, so a wrapper stands in front of the SRAM.

Practice

Report a problem with this page