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.
| Question | |
|---|---|
| 1 | What does a memory controller do? |
| 2 | Which bits go to a serial flash chip for one read, and in which order? |
| 3 | How long does the bus wait for one word? |
| 4 | What does "execute in place" mean? |
| 5 | Why 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
| Word | Meaning |
|---|---|
| Memory controller | The block between the bus and a memory device. It turns each bus transfer into what the device needs. |
| Serial flash | A flash chip with few pins. It takes and sends one bit on each clock. |
| SPI | Serial peripheral interface. Four wires: a chip select, a clock, one data wire each way. |
| Chip select | The wire that tells one chip that it is meant. It is low for as long as that chip is spoken to. |
| Serial clock | The clock wire of SPI. The controller makes it. One bit moves on each of its periods. |
| Exchange | Everything between the fall and the rise of chip select: a command, an address and data. |
| Read command | The 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. |
| Name | Driven by | Meaning |
|---|---|---|
| cs_n | the controller | Chip select. The _n says that 0 means selected. |
| sck | the controller | The serial clock. It rests at 0. |
| mosi | the controller | Data to the flash. The flash takes a bit as sck rises. |
| miso | the flash | Data from the flash. The flash changes it as sck falls. |
One Picture: From One Address to 64 Clocks
- Processor
- Control / FSM
- Bus
- Memory
- The processor asks once
It puts an address on the bus, as for any memory. It does not know that this one is slow.
- Chip select falls
The controller pulls the chip select of the flash low. That starts one exchange. The bus is told to wait.
- 8 clocks: the command
The controller sends the byte 03, highest bit first. To the flash that means: read data.
- 24 clocks: the address
Next come the 24 address bits. Now the flash knows which byte is wanted first.
- 32 clocks: the data
The flash sends four bytes, one bit on each clock. The controller shifts them in.
- 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.
| Serial clocks | To the flash | From the flash | System cycles |
|---|---|---|---|
| none | chip select falls | 1 | |
| 1 to 8 | 0000 0011: the command 03 | nothing | 16 |
| 9 to 32 | the address 000100, highest bit first | nothing | 48 |
| 33 to 40 | nothing | 44: the byte at 000100 | 16 |
| 41 to 48 | nothing | 33: the byte at 000101 | 16 |
| 49 to 56 | nothing | 22 | 16 |
| 57 to 64 | nothing | 11 | 16 |
| none | chip select rises | 1 |
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
Rule 1: One bus read is one exchange, or a part of one.
The controller starts it. The bus does nothing but wait.
Rule 2: An exchange starts with the command, then the address, then the data.
Each goes with its highest bit first.
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.
Rule 4: The bytes arrive lowest address first.
The controller puts them into the word in the byte order of the processor.
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.
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.
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=390Rules 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.
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=323Rule 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 choice | The alternative | What the choice buys, and costs |
|---|---|---|
| Flash outside, on a serial link | Flash on the chip | A cheap chip and a cheap flash, with four pins between them. Every read is slow. |
| A controller that looks like a ROM | Software that toggles the pins | The processor can fetch from flash with no code at all. One more block, and wait states. |
| Execute in place | Copy into SRAM, then run | No SRAM is needed for the program. Every fetch pays the serial read. |
| Keep chip select low | One exchange for each word | Code 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.
bus read 000100 | command 03, address 000100 | bytes 44 33 22 11 | word 44332211 | 64 serial clocks | the bus waited 130 cyclesThe 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 section looks ahead. Real flash controllers are faster, and they still make the bus wait.
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.
A cache in front
A cache between the processor and the controller hides most of the wait. Loops then run from the cache.
Copy at start-up
ap_soc does not execute in place. Its Boot ROM copies the program into SRAM: the boot flow.
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.
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=390Check Yourself
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.