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.
| Question | |
|---|---|
| 1 | When does an SRAM look at its pins, and when does a read show its word? |
| 2 | What do the byte strobes of a write do? |
| 3 | What does the data output show after a write? |
| 4 | How does a bus transfer become a word address and strobes? |
| 5 | Why 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
| Word | Meaning |
|---|---|
| Chip enable | A pin of a memory. At an edge where it is 0, the memory does nothing. |
| Write enable | A pin of a memory. At an enabled edge, 1 means write and 0 means read. |
| Byte strobe | One bit for each byte of a word. On a write, only bytes whose strobe is 1 are changed. |
| Word address | The number of a whole word in a memory. For words of four bytes it is the byte address divided by four. |
| Offset in a memory | A byte address, counted from the first byte of that memory. |
| Wrapper of a memory | The 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
- Bus
- Control / FSM
- Memory
- Processor
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
| Edge | Chip enable | Write enable | Strobes | Write data | Word 3 after the edge | Data output after the edge |
|---|---|---|---|---|---|---|
| e1 | 1 | 1 | 1111 | 11223344 | 11223344 | nothing yet: no read so far |
| e2 | 1 | 0 | 11223344 | 11223344 | ||
| e3 | 1 | 1 | 0100 | aabbccdd | 11bb3344 | 11223344: a write does not change it |
| e4 | 0 | 11bb3344 | 11223344 | |||
| e5 | 1 | 0 | 11bb3344 | 11bb3344 |
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.
| Bus write | Offset in binary | Word address | Byte inside the word | Strobes |
|---|---|---|---|---|
| one byte at offset 0x0d | 0000 1101 | 3 | 1 | 0010 |
| two bytes at offset 0x0e | 0000 1110 | 3 | 2 and 3 | 1100 |
| four bytes at offset 0x10 | 0001 0000 | 4 | 0 to 3 | 1111 |
The two low bits of the offset pick the byte. The other bits are the word address.
The Rules
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.
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.
Rule 3: A write changes only the bytes whose strobe is 1.
The other bytes of the word keep their value.
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.
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.
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.
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=11bb3344Rule 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 choice | The alternative | What the choice buys, and costs |
|---|---|---|
| Everything sampled on one edge | A memory with no clock | Timing is checked like that of a flip-flop. A read takes a cycle. |
| Byte strobes | Whole words only | A store of one byte needs one access. Without strobes it needs a read and then a write. |
| A word address at the port | A byte address at the port | Two address pins fewer, and no adder. The wrapper must work out the strobes. |
| A wait for a read after a write | A buffer that holds the write and answers such reads itself | Very 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.
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 55667788The 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
This section looks ahead. A large chip has hundreds of memory macros, and each one has a wrapper of some kind.
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.
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.
Wider strobes
A bus of 64 bits has eight byte lanes, and its memories have eight strobes. The rule is the same.
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?
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=11bb3344Read 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
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.