Memory Controllers: Flash and DRAM at the Chip Boundary

The bus speaks one language, and a memory chip on the board speaks another. A controller stands between them, and the bus waits while it translates.

You Will Be Able To

  • Explain what a memory controller does between the bus and a memory device.
  • Follow one bus read through a serial flash read, and count its serial clocks and wait cycles.
  • Explain execute in place, what it costs, and two ways to make it cheaper.
Lesson
S3.11
Time
25 min
Runs on
Your browser
Check
6 questions

A cheap chip has no room for its program. The program sits in a flash chip with eight pins, beside it on the board. The processor still wants each instruction as a word of 32 bits, at once. The flash chip gives one bit at a time.

Before You Start

You need flash memory from S3.13, and wait states from S0.4.

It helps to know the SRAM port of S3.15: that wrapper does a small version of the job on this page.

Five questions this page answers
Question
1What does a memory controller do?
2Which bits go to a serial flash chip for one read, and in which order?
3How long does the bus wait for one word?
4What does "execute in place" mean?
5Why is the next word cheaper than the first?

The Idea: A Translator That Makes the Bus Wait

The bus wants an answer in a cycle or two: an address goes out, and a word comes back. A memory outside the chip cannot answer like that. It has rules of its own: commands, an order of bits, a clock, times to respect.

So a block sits at the edge of the chip, between the bus and the memory device. It is called a memory controller. It takes one bus transfer and does everything that the device needs for it. Meanwhile it holds the bus with wait states.

The cheapest link to a chip on the board is serial: it moves one bit at a time. This page takes the simplest case, reading a serial flash chip. That is a flash chip with few pins, which sends and takes one bit on each clock. Its wires follow SPI, a common way to connect small chips with a clock and two data wires.

Some chips run their program straight from such a flash chip. Every fetch goes through the controller. That is called execute in place, or XIP. The other way is to copy the program into SRAM first, and run it there.

The next lesson does the same for DRAM. Its controller has more to do: it must also refresh.

New Words

New words on this page
WordMeaning
Memory controllerThe block between the bus and a memory device. It turns each bus transfer into what the device needs.
Serial flashA flash chip with few pins. It takes and sends one bit on each clock.
SPISerial peripheral interface. Four wires: a chip select, a clock, one data wire each way.
Chip selectThe wire that tells one chip that it is meant. It is low for as long as that chip is spoken to.
Serial clockThe clock wire of SPI. The controller makes it. One bit moves on each of its periods.
ExchangeEverything between the fall and the rise of chip select: a command, an address and data.
Read commandThe byte 03 in hex, sent first in an exchange. It tells the flash to send data.
Execute in place (XIP)Running a program straight from flash. Every fetch reads the flash.
The four wires, as the lab names them
NameDriven byMeaning
cs_nthe controllerChip select. The _n says that 0 means selected.
sckthe controllerThe serial clock. It rests at 0.
mosithe controllerData to the flash. The flash takes a bit as sck rises.
misothe flashData from the flash. The flash changes it as sck falls.

One Picture: From One Address to 64 Clocks

One Bus Read Becomes 64 Serial ClocksStep through the picture. The processor asks once. The controller talks to the flash chip for 64 serial clocks, and the bus waits all that time.
  • Processor
  • Control / FSM
  • Bus
  • Memory
One Bus Read Becomes 64 Serial ClocksStep through the picture. The processor asks once. The controller talks to the flash chip for 64 serial clocks, and the bus waits all that time. component Processor reads one word fromthe bus component Flash controller on the chip component Shift out command, thenaddress component Shift in 32 data bits component Serial flash chip on the board address03 and addressone bit a clockone bit a clockfour byteswait, then ready
  1. component Processorreads one word from the bus
    • address → Flash controller
  2. component Flash controlleron the chip
    • 03 and address → Shift out
    • wait, then ready → Processor
  3. component Serial flash chipon the board
    • one bit a clock → Shift in
  4. component Shift outcommand, then address
    • one bit a clock → Serial flash chip
  5. component Shift in32 data bits
    • four bytes → Flash controller
  1. The processor asks once

    It puts an address on the bus, as for any memory. It does not know that this one is slow.

  2. Chip select falls

    The controller pulls the chip select of the flash low. That starts one exchange. The bus is told to wait.

  3. 8 clocks: the command

    The controller sends the byte 03, highest bit first. To the flash that means: read data.

  4. 24 clocks: the address

    Next come the 24 address bits. Now the flash knows which byte is wanted first.

  5. 32 clocks: the data

    The flash sends four bytes, one bit on each clock. The controller shifts them in.

  6. Chip select rises, and the bus goes on

    The controller ends the exchange, puts the four bytes together and stops the wait. The processor takes the word.

Read the diagram as text
  • Processor: reads one word from the bus
  • Flash controller: on the chip
  • Shift out: command, then address
  • Shift in: 32 data bits
  • Serial flash chip: on the board
  • Processor → Flash controller: address
  • Flash controller → Shift out: 03 and address
  • Shift out → Serial flash chip: one bit a clock
  • Serial flash chip → Shift in: one bit a clock
  • Shift in → Flash controller: four bytes
  • Flash controller → Processor: wait, then ready

The processor sees a slow ROM. Everything else is hidden in the controller.

The Smallest Example, Worked by Hand

The processor reads the word at address 000100 of the flash. The four bytes there are 44, 33, 22 and 11. In this controller one serial clock takes two cycles of the system clock.

One read, clock by clock
Serial clocksTo the flashFrom the flashSystem cycles
nonechip select falls1
1 to 80000 0011: the command 03nothing16
9 to 32the address 000100, highest bit firstnothing48
33 to 40nothing44: the byte at 00010016
41 to 48nothing33: the byte at 00010116
49 to 56nothing2216
57 to 64nothing1116
nonechip select rises1

64 serial clocks and 130 cycles. An SRAM would have answered in one cycle.

Now the four bytes become a word. The byte at the lowest address is the lowest byte: 11223344. That is the little-endian order of the processor.

More than half of the clocks carry no data: 32 of the 64 are command and address. For the next word in a row, they can be saved. The flash goes on to the next byte for as long as chip select stays low. So the word at 000104 needs only 32 more clocks: 64 cycles.

The Rules

  1. Rule 1: One bus read is one exchange, or a part of one.

    The controller starts it. The bus does nothing but wait.

  2. Rule 2: An exchange starts with the command, then the address, then the data.

    Each goes with its highest bit first.

  3. Rule 3: The bus waits until the last data bit is in.

    The wait is the serial clocks times the cycles of one serial clock.

  4. Rule 4: The bytes arrive lowest address first.

    The controller puts them into the word in the byte order of the processor.

  5. Rule 5: While chip select stays low, the flash goes on with the next byte.

    A read of the next word needs no new command and no address.

  6. Rule 6: A read of any other address needs a new exchange.

    Chip select must rise first, and fall again.

Sources for These Rules

The read command of a serial flash chip follows the data sheet of the Winbond W25Q128JV (revision F, sections 8 and 8.2.6), as cited by the lesson P7.6. The data sheet was not read again for this page.

A serial clock takes two system cycles in the lab. The fall and the rise of chip select take one cycle each. Both are choices of the lab.

ap_soc itself does not execute in place. Its Boot ROM copies the program into SRAM: see the note in At Scale.

Now Run It

The lab at the end of this page has a small model of a flash chip, and test code that toggles its wires as a controller would. It reads three words. The first two lie in a row, and the third is a jump.

Printed by the lab on Icarus. Each row is one bus read: what was sent, the four bytes in the order of arrival, the word, and the wait.
bus read 000100 | command 03, address 000100 | bytes 44 33 22 11 | word 11223344 | 64 serial clocks | the bus waited 130 cycles
bus read 000104 | command 03, address 000104 | bytes 88 77 66 55 | word 55667788 | 64 serial clocks | the bus waited 130 cycles
bus read 000200 | command 03, address 000200 | bytes 01 00 fe ca | word cafe0001 | 64 serial clocks | the bus waited 130 cycles
reads=3 serial_clocks=192 wait_cycles=390

Rules 2 and 3 at work: every row has 64 serial clocks and a wait of 130 cycles. That is the hand table.

Rule 4 at work: the bytes arrive as 44 33 22 11, and the word is 11223344.

This controller ends the exchange after every word. Now let it keep chip select low. It goes on when the next read asks for the next word.

The same lab and the same three reads, with chip select kept low between reads.
bus read 000100 | command 03, address 000100 | bytes 44 33 22 11 | word 11223344 | 64 serial clocks | the bus waited 129 cycles
bus read 000104 | chip select is still low: go on | bytes 88 77 66 55 | word 55667788 | 32 serial clocks | the bus waited 64 cycles
bus read 000200 | command 03, address 000200 | bytes 01 00 fe ca | word cafe0001 | 64 serial clocks | the bus waited 130 cycles
reads=3 serial_clocks=160 wait_cycles=323

Rule 5 at work: row 2. No command and no address, 32 serial clocks, and half the wait.

Rule 6 at work: row 3. The jump to 000200 needs a new exchange. It costs one cycle more than before, because the old exchange must be ended first.

This lab leaves out the bus signals, the state machine and the reset. Building those is the practice problem.

Why It Is Built This Way

Design choiceThe alternativeWhat the choice buys, and costs
Flash outside, on a serial linkFlash on the chipA cheap chip and a cheap flash, with four pins between them. Every read is slow.
A controller that looks like a ROMSoftware that toggles the pinsThe processor can fetch from flash with no code at all. One more block, and wait states.
Execute in placeCopy into SRAM, then runNo SRAM is needed for the program. Every fetch pays the serial read.
Keep chip select lowOne exchange for each wordCode in a row costs half. Jumps cost the same as before.

The Trap: The Word That Came Out Backwards

A new controller shifts the 32 data bits into one register, first bit on top, and gives that register to the bus. The serial side is right: every byte arrives as the flash sent it.

The same lab, with the four bytes used as one number in the order of arrival.
bus read 000100 | command 03, address 000100 | bytes 44 33 22 11 | word 44332211 | 64 serial clocks | the bus waited 130 cycles

The bytes are 44 33 22 11, as before. The word is 44332211. The byte from the lowest address has landed on top.

A test that reads single bytes passes. A processor that fetches this word decodes an instruction that nobody wrote.

Rule 4 is the check. Bits within a byte arrive highest first. Bytes within a word arrive lowest first. Those are two different orders, and a controller must handle both.

At Scale

This page1 data wire: 64 clocks for a word
Four data wires4 bits on each clock, for address and data

This section looks ahead. Real flash controllers are faster, and they still make the bus wait.

  1. Four data wires

    Most serial flash chips can move four bits on each clock. The commands and their extra clocks are in P7.7 and P7.9.

  2. A cache in front

    A cache between the processor and the controller hides most of the wait. Loops then run from the cache.

  3. Copy at start-up

    ap_soc does not execute in place. Its Boot ROM copies the program into SRAM: the boot flow.

  4. Flash on the chip

    Arm names a bus for flash inside the chip: the Generic Flash Bus. It is only named here, and not checked.

Try It

Run the lab. Check the first row against the hand table.

Add a fourth read, of 000108. Predict its word from the lines that fill the flash.

Change one_clock so that a serial clock takes four cycles. Predict the wait of one read before you run it.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: bus read 000100 | command 03, address 000100 | bytes 44 33 22 11 | word 11223344 | 64 serial clocks | the bus waited 130 cycles bus read 000104 | command 03, address 000104 | bytes 88 77 66 55 | word 55667788 | 64 serial clocks | the bus waited 130 cycles bus read 000200 | command 03, address 000200 | bytes 01 00 fe ca | word cafe0001 | 64 serial clocks | the bus waited 130 cycles reads=3 serial_clocks=192 wait_cycles=390

Check Yourself

Predict the outputThe controller keeps chip select low. It reads the words at 000100, 000104 and 000108. What does the last row say?
// A serial flash chip that knows one command: 03, read data.
module spi_flash(input logic cs_n, sck, mosi, output logic miso);
  logic [7:0]  mem [0:4095];
  logic [7:0]  command, shift;
  logic [23:0] address;
  int clocks;
  always @(negedge cs_n) clocks = 0;                       // chip select falls: a new exchange begins
  always @(posedge sck) if (!cs_n) begin                   // the chip takes one bit on every rising edge
    if (clocks < 8) command = {command[6:0], mosi};
    else if (clocks < 32) address = {address[22:0], mosi};
    clocks++;
  end
  always @(negedge sck) if (!cs_n && clocks >= 32 && command == 8'h03) begin   // and gives one on every falling edge
    if ((clocks - 32) % 8 == 0) begin shift = mem[address[11:0]];  address++; end
    miso = shift[7];  shift = {shift[6:0], 1'b0};
  end
endmodule

// What a flash controller does between the bus and the chip, acted out by tasks.
module tb;
  logic cs_n = 1, sck = 0, mosi = 0;
  wire  miso;
  spi_flash flash(.*);
  int reads = 0, serial_clocks = 0, wait_cycles = 0;
  logic [23:0] next_address;                               // where the flash would go on, if chip select is still low

  // One serial clock takes two cycles of the system clock: one with sck at 1, one with sck at 0.
  task automatic one_clock(input logic to_flash, output logic from_flash);
    mosi = to_flash;
    #1 sck = 1;  from_flash = miso;
    #1 sck = 0;
    serial_clocks++;  wait_cycles += 2;
  endtask
  task automatic send(input logic [23:0] value, input int bits);
    logic unused;
    for (int i = bits - 1; i >= 0; i--) one_clock(value[i], unused);
  endtask

  // The bus asks for the word at address a. The bus waits until the task returns.
  task automatic bus_read(input logic [23:0] a);
    logic [7:0]  b [0:3];
    logic [31:0] word;
    logic bit_in;
    int waited_so_far = wait_cycles, clocks_so_far = serial_clocks;
    reads++;
    $write("bus read %06h | ", a);
    if (!cs_n && a == next_address) $write("chip select is still low: go on | ");
    else begin
      if (!cs_n) begin #1 cs_n = 1;  wait_cycles++; end      // another address: end the old exchange first
      #1 cs_n = 0;  wait_cycles++;
      send(24'h03, 8);                                     // the command: read data
      send(a, 24);                                         // the address, highest bit first
      $write("command 03, address %06h | ", a);
    end
    for (int k = 0; k < 4; k++) for (int i = 7; i >= 0; i--) begin one_clock(1'b0, bit_in);  b[k][i] = bit_in; end
    word = {b[3], b[2], b[1], b[0]};                           // the first byte is the lowest byte of the word
    next_address = a + 24'd4;
    $display("bytes %02h %02h %02h %02h | word %08h | %0d serial clocks | the bus waited %0d cycles", b[0], b[1], b[2], b[3], word, serial_clocks - clocks_so_far, wait_cycles - waited_so_far);
  endtask
  task automatic store_word(input logic [11:0] a, input logic [31:0] word);   // the lowest byte at the lowest address
    for (int k = 0; k < 4; k++) flash.mem[a + k] = word[8*k +: 8];
  endtask
  task automatic totals;
    $display("reads=%0d serial_clocks=%0d wait_cycles=%0d", reads, serial_clocks, wait_cycles);
  endtask

  initial begin
    for (int i = 0; i < 4096; i++) flash.mem[i] = 8'hff;   // an erased flash reads ff
    store_word(12'h100, 32'h1122_3344);
    store_word(12'h104, 32'h5566_7788);
    store_word(12'h108, 32'h99aa_bbcc);
    store_word(12'h200, 32'hcafe_0001);
    bus_read(24'h000100);
    bus_read(24'h000104);
    bus_read(24'h000108);
    totals;
  end
endmodule
ConceptWhat does a memory controller do?
ConceptIn what order does a read exchange send its parts?
ConceptThe flash sends the bytes a1, b2, c3, d4, in this order. Which word does a little-endian processor get?
ConceptWhy is the next word in a row cheaper than the first?
ConceptWhat does execute in place save, and what does it cost?

Common Mistakes

Wrong Put the 32 data bits on the bus in the order of arrival.

The first byte lands on top, and every word is turned round.

Fix Put the first byte into bits 7 to 0, in the byte order of the processor.

Wrong Go on with the next byte after a jump.

The flash sends what follows the old address, not what the bus asked for.

Fix Compare the address with the one the flash would send next. If they differ, start a new exchange.

Wrong Plan the speed of a program from the clock of the processor alone.

With execute in place, each fetch waits about 130 cycles.

Fix Count the wait of the flash read, or copy the hot code into SRAM.

Remember

  • A memory controller turns one bus transfer into the exchange that the device needs, and holds the bus with wait states.
  • A serial flash read: chip select falls, command 03, 24 address bits, then data, highest bit first.
  • Bytes arrive lowest address first. The controller builds the word in the byte order of the processor.
  • Keeping chip select low makes the next word in a row cost half.

Practice

Report a problem with this page