The DRAM Controller: Commands, the Open Row and Refresh
A DRAM does not take an address and give a word. It takes commands: open a row, read it, close it, refresh. A controller issues them, and the bus waits a different time for each access.
You Will Be Able To
- Name the five commands that a DRAM controller sends, and say what each does to a row.
- Work out how long the bus waits for an access, from the row that is open.
- Explain where refresh commands come from, and what they do to the worst wait.
- Lesson
- S3.26
- Time
- 25 min
- Runs on
- Your browser
- Check
- 6 questions
A loop reads a table from DRAM. Almost every read takes 2 cycles, and now and then one takes 10. The loop did nothing different. Something else took the DRAM for a moment.
Before You Start
You need S3.14: the cell, the row, the sense amplifier and the refresh.
You need the last lesson, S3.11: a controller that turns a bus transfer into an exchange, and makes the bus wait.
| Question | |
|---|---|
| 1 | Which commands does a DRAM understand? |
| 2 | What is an open row, and why does it make the next access fast? |
| 3 | Why must a row be closed before another is opened? |
| 4 | Who decides when to refresh? |
| 5 | What is the longest that one access can wait? |
The Idea: A Memory Driven by Commands
S3.14 showed what happens inside a DRAM. A read empties the cells of a whole row onto the bit lines. The sense amplifiers decide each bit, hold it, and write it back.
That mechanism shows at the pins. The DRAM is driven by commands, and the first one opens a row: ACT, short for activate. After it, the sense amplifiers hold the whole row. This is the open row.
Now RD and WR read and write columns of the open row. They are quick, because the row is already in the sense amplifiers. Programs use nearby addresses again and again, so many accesses find their row open.
An access to another row needs two steps first. PRE, short for precharge, closes the open row and readies the bit lines. Then ACT opens the new row.
The fifth command is REF, for refresh. The DRAM refreshes some rows by itself when it gets one. But it must be asked in time, and every row must be closed first. A timer in the controller does the asking.
The block that issues all these commands is the DRAM controller. It remembers which row is open, picks the commands for each access, fits the refreshes in, and holds the bus meanwhile.
New Words
| Word | Meaning |
|---|---|
| DRAM controller | The memory controller of a DRAM. It turns bus accesses into commands, and sends refreshes in time. |
| ACT | Activate. The command that opens one row: the row is copied into the sense amplifiers. |
| Open row | The row that the sense amplifiers hold now. Only it can be read and written. |
| RD and WR | The commands that read and write columns of the open row. |
| PRE | Precharge. The command that closes the open row and readies the bit lines for the next ACT. |
| REF | The refresh command. The DRAM refreshes some of its rows. Every row must be closed first. |
| Refresh timer | A counter in the controller. It says when the next REF is due. |
| Worst wait | The longest time that one access can be held, when everything slow happens together. |
One Picture: The Controller and Its Commands
- Bus
- Control / FSM
- Memory
- Open the row: ACT
No cell can be read directly. ACT copies one whole row into the sense amplifiers. That row is now open.
- Read or write the open row: RD, WR
RD and WR pick columns of the open row. Any number of them may follow one ACT.
- Close the row: PRE
Before another row can be opened, the open one must be closed. PRE does that, and makes the DRAM ready for the next ACT.
- The bus waits
Each command takes time. The controller holds the bus with wait states until the RD or WR is done.
- The timer asks for a refresh: REF
Every so often the timer says that a refresh is due. The controller closes the open row and sends REF. An access that arrives then waits longer.
Read the diagram as text
- Bus: read or write, row and column
- DRAM controller: remembers the open row
- Refresh timer: counts cycles
- PHY: the circuit at the pins
- Open row: held in the sense amplifiers
- All other rows: closed, and leaking
- Bus → DRAM controller: access
- DRAM controller → Bus: wait states
- DRAM controller → PHY: ACT, RD, WR, PRE, REF
- PHY → Open row: pins
- Refresh timer → DRAM controller: refresh is due
- All other rows → Open row: ACT opens one
An SRAM needs an address. A DRAM needs the right commands in the right order, with the right gaps.
The Smallest Example, Worked by Hand
Take these prices: ACT 2 cycles, RD or WR 2 cycles, PRE 2 cycles, REF 4 cycles. They are values of this page. Follow four accesses, starting with no row open.
| Access | Open row before | Commands | The bus waits | Open row after |
|---|---|---|---|---|
| read row 12 | none | ACT, RD | 2 + 2 = 4 | 12 |
| read row 12 | 12 | RD | 2 | 12 |
| write row 12 | 12 | WR | 2 | 12 |
| read row 40 | 12 | PRE, ACT, RD | 2 + 2 + 2 = 6 | 40 |
The same DRAM answers in 2, 4 or 6 cycles. It depends on the row that is open.
Now a refresh falls due just as a read of the open row arrives. The controller sends the refresh first: PRE and REF, 6 cycles. That closed the row. So the read needs ACT and RD after it: 4 more. The bus waits 10 cycles for a read that usually takes 2.
The Rules
Rule 1: Only the open row can be read or written.
RD and WR need an ACT of their row before them.
Rule 2: One row is open at a time.
PRE must close it before another ACT.
Rule 3: The controller remembers the open row.
An access to that row needs RD or WR only.
Rule 4: A REF needs every row closed.
So a refresh is PRE and REF when a row is open, and it leaves no row open.
Rule 5: A refresh that is due goes before the next access.
The cells cannot wait. The bus can.
Rule 6: The worst wait is the sum of everything slow.
Here: PRE, REF, ACT and the access.
Sources for These Rules
The textbook "Computer Organization and Design" by Patterson and Hennessy, section 5.2. See "DRAM Technology": the charge must be refreshed, and a row is kept in a buffer for repeated access.
The textbook "Digital Design and Computer Architecture" by Harris and Harris, section 5.5.2: the DRAM cell, the destructive read and the refresh.
The command names ACT, RD, WR, PRE and REF are common usage. No DRAM standard is in the reference library, so the names and the rules of order are not checked against one.
The prices, one open row for the whole DRAM, and "refresh first" are choices of this page. Real DRAMs have several banks with an open row each, and allow a refresh to be put off a little.
Now Run It
The lab at the end of this page is test code that counts. It holds no data. It keeps the open row and a refresh timer, and prints the commands of each access. A REF is due every 40 cycles.
cycle 0 | read row 12 column 3 | ACT RD | the bus waited 4 cycles
cycle 4 | read row 12 column 4 | RD | the bus waited 2 cycles
cycle 6 | write row 12 column 5 | WR | the bus waited 2 cycles
cycle 8 | read row 40 column 0 | PRE ACT RD | the bus waited 6 cycles
cycle 40 | the bus is idle | PRE REF | nobody waited
cycle 46 | read row 40 column 1 | ACT RD | the bus waited 4 cycles
cycle 50 | write row 40 column 2 | WR | the bus waited 2 cycles
accesses=6 refreshes=1 waited=20 worst=6Rules 1 to 3 at work: the first four rows are the hand table.
Rules 4 and 5 at work: row 5. The bus is idle when the refresh falls due at cycle 40. Row 40 is open, so the refresh is PRE and REF. Nobody waits for it.
Rule 4 again: row 6. The read is of row 40, which was open before the refresh. It is closed now, and the read needs an ACT.
The count: six accesses waited 20 cycles in all, and no access waited longer than 6.
This lab leaves out the data, the pins and all real times. The pins and their tuning are the job of the PHY: see At Scale.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| Keep the row open after an access | Close it at once | The next access to that row is fast. An access to another row pays a PRE first. |
| A controller between the bus and the DRAM | Software sends the commands | The processor sees a plain memory. The wait differs from access to access. |
| A timer in the controller for refresh | Refresh by the program | No program can forget it. Now and then an access is held up. |
| Refresh in idle time when possible | Refresh exactly on the tick | Most refreshes cost the bus nothing. The controller needs to know that the bus is idle. |
The Trap: The Read That Was Slow Once in a While
A control loop reads two tables in turn, in rows 12 and 40. The designer measures a few reads: 6 cycles each. The budget of the loop is set at 8 cycles for a read.
cycle 0 | read row 12 column 0 | ACT RD | the bus waited 4 cycles
cycle 4 | read row 40 column 1 | PRE ACT RD | the bus waited 6 cycles
cycle 10 | read row 12 column 2 | PRE ACT RD | the bus waited 6 cycles
cycle 16 | read row 40 column 3 | PRE ACT RD | the bus waited 6 cycles
cycle 22 | read row 12 column 4 | PRE ACT RD | the bus waited 6 cycles
cycle 28 | read row 40 column 5 | PRE ACT RD | the bus waited 6 cycles
cycle 34 | read row 12 column 6 | PRE ACT RD | the bus waited 6 cycles
cycle 40 | read row 40 column 7 | PRE REF ACT RD | the bus waited 10 cycles
accesses=8 refreshes=1 waited=50 worst=10Seven reads fit the budget. The eighth arrives at cycle 40, just as a refresh is due. It waits 10 cycles.
The loop has no idle time, so the refresh cannot hide. It will hit one read in every 40 cycles, for as long as the loop runs.
A measurement of a few reads shows the usual wait. Rule 6 gives the worst one, and a deadline must be set against that.
At Scale
This section looks ahead. A real DRAM controller follows the same rules with many more times, and with a fast link below it.
The PHY
The circuit at the pins launches and samples bits at very high speed, and it must be tuned at start-up. See the memory of a large chip.
Banks
A real DRAM has several banks, each with an open row of its own. A good controller keeps several rows open at once.
A queue of requests
A large controller holds several requests and picks an order that reuses open rows. The same lesson shows the queue.
Small chips
ap_soc has no DRAM and no such controller. Its 64 KiB of SRAM answer every read in one cycle.
Try It
Run the lab. Check the first four rows against the hand table.
Remove the idle call. Which access now meets the refresh, and how long does it wait?
Change T_REF to 8. Predict the worst wait of the eight reads of the trap before you try it.
cycle 0 | read row 12 column 3 | ACT RD | the bus waited 4 cycles cycle 4 | read row 12 column 4 | RD | the bus waited 2 cycles cycle 6 | write row 12 column 5 | WR | the bus waited 2 cycles cycle 8 | read row 40 column 0 | PRE ACT RD | the bus waited 6 cycles cycle 40 | the bus is idle | PRE REF | nobody waited cycle 46 | read row 40 column 1 | ACT RD | the bus waited 4 cycles cycle 50 | write row 40 column 2 | WR | the bus waited 2 cycles accesses=6 refreshes=1 waited=20 worst=6Check Yourself
Common Mistakes
Wrong Plan with the wait of a read to the open row.
A change of row adds PRE and ACT, and a refresh adds more.
Fix Work out the usual wait and the worst wait, and use the worst for deadlines.
Wrong Send ACT for a new row while another row is open.
The sense amplifiers still hold the old row.
Fix Send PRE first, and wait its time.
Wrong Assume the row is still open after a refresh.
A REF needs every row closed.
Fix After a refresh, the next access starts with ACT.
Remember
- A DRAM takes commands: ACT opens a row, RD and WR use it, PRE closes it, REF refreshes.
- The controller remembers the open row. An access to it is fast, and a change of row is slower.
- A timer in the controller sends the refreshes. A REF needs every row closed.
- Set deadlines against the worst wait: PRE, REF, ACT and the access.