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.

Five questions this page answers
Question
1Which commands does a DRAM understand?
2What is an open row, and why does it make the next access fast?
3Why must a row be closed before another is opened?
4Who decides when to refresh?
5What 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

New words on this page
WordMeaning
DRAM controllerThe memory controller of a DRAM. It turns bus accesses into commands, and sends refreshes in time.
ACTActivate. The command that opens one row: the row is copied into the sense amplifiers.
Open rowThe row that the sense amplifiers hold now. Only it can be read and written.
RD and WRThe commands that read and write columns of the open row.
PREPrecharge. The command that closes the open row and readies the bit lines for the next ACT.
REFThe refresh command. The DRAM refreshes some of its rows. Every row must be closed first.
Refresh timerA counter in the controller. It says when the next REF is due.
Worst waitThe longest time that one access can be held, when everything slow happens together.

One Picture: The Controller and Its Commands

Bus Accesses in, DRAM Commands OutStep through the picture. The controller turns each access into one to three commands. A timer adds refresh commands of its own.
  • Bus
  • Control / FSM
  • Memory
Bus Accesses In, DRAM Commands OutStep through the picture. The controller turns each access into one to three commands. A timer adds refresh commands of its own. component Bus read or write, row andcolumn component DRAM controller remembers the openrow component Refresh timer counts cycles component PHY the circuit at thepins component Open row held in the senseamplifiers component All other rows closed, and leaking accesswait statesACT, RD, WR, PRE,REFpinsrefresh is dueACT opens one
  1. component Busread or write, row and column
    • access → DRAM controller
  2. component DRAM controllerremembers the open row
    • wait states → Bus
    • ACT, RD, WR, PRE, REF → PHY
  3. component PHYthe circuit at the pins
    • pins → Open row
  4. component Open rowheld in the sense amplifiers
  5. component All other rowsclosed, and leaking
    • ACT opens one → Open row
  6. component Refresh timercounts cycles
    • refresh is due → DRAM controller
  1. Open the row: ACT

    No cell can be read directly. ACT copies one whole row into the sense amplifiers. That row is now open.

  2. 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.

  3. 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.

  4. The bus waits

    Each command takes time. The controller holds the bus with wait states until the RD or WR is done.

  5. 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.

Four accesses, and the commands each one needs
AccessOpen row beforeCommandsThe bus waitsOpen row after
read row 12noneACT, RD2 + 2 = 412
read row 1212RD212
write row 1212WR212
read row 4012PRE, ACT, RD2 + 2 + 2 = 640

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

  1. Rule 1: Only the open row can be read or written.

    RD and WR need an ACT of their row before them.

  2. Rule 2: One row is open at a time.

    PRE must close it before another ACT.

  3. Rule 3: The controller remembers the open row.

    An access to that row needs RD or WR only.

  4. 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.

  5. Rule 5: A refresh that is due goes before the next access.

    The cells cannot wait. The bus can.

  6. 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.

Printed by the lab on Icarus. A row shows the cycle at which the access arrived, the commands, and the wait.
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=6

Rules 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 choiceThe alternativeWhat the choice buys, and costs
Keep the row open after an accessClose it at onceThe next access to that row is fast. An access to another row pays a PRE first.
A controller between the bus and the DRAMSoftware sends the commandsThe processor sees a plain memory. The wait differs from access to access.
A timer in the controller for refreshRefresh by the programNo program can forget it. Now and then an access is held up.
Refresh in idle time when possibleRefresh exactly on the tickMost 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.

The same lab: eight reads, alternating between rows 12 and 40, with no idle time.
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=10

Seven 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 page1 open row, 5 commands, 4 prices
A phoneseveral banks, dozens of timing rules, two channels

This section looks ahead. A real DRAM controller follows the same rules with many more times, and with a fast link below it.

  1. 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.

  2. Banks

    A real DRAM has several banks, each with an open row of its own. A good controller keeps several rows open at once.

  3. A queue of requests

    A large controller holds several requests and picks an order that reuses open rows. The same lesson shows the queue.

  4. 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.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: 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=6

Check Yourself

Predict the outputA refresh is due every 6 cycles. Four reads of row 7 arrive one after another. What does the last row say?
// A DRAM controller, acted out: bus accesses become DRAM commands, and refreshes are fitted in between.
module tb;
  // Prices in clock cycles. They are values of this lab.
  localparam int T_ACT = 2;            // ACT: open a row. After this the row can be read and written
  localparam int T_RW  = 2;            // RD or WR: one access to the open row
  localparam int T_PRE = 2;            // PRE: close the open row
  localparam int T_REF = 4;            // REF: refresh some rows. Every row must be closed first
  localparam int REFRESH_EVERY = 6;   // a REF is due this often
  int now = 0, open_row = -1, due = REFRESH_EVERY;
  int accesses = 0, refreshes = 0, waited = 0, worst = 0;

  task automatic refresh(inout string commands);
    if (open_row >= 0) begin commands = {commands, "PRE "};  now += T_PRE; end
    commands = {commands, "REF "};  now += T_REF;
    open_row = -1;  due += REFRESH_EVERY;  refreshes++;
  endtask

  // One access from the bus. The bus waits from the start of the task to its end.
  task automatic access(input string what, input int row, input int column);
    int start = now;
    string commands = "";
    accesses++;
    if (now >= due) refresh(commands);                     // a refresh that is due goes first
    if (open_row != row) begin
      if (open_row >= 0) begin commands = {commands, "PRE "};  now += T_PRE; end
      commands = {commands, "ACT "};  now += T_ACT;  open_row = row;
    end
    if (what == "write") commands = {commands, "WR"}; else commands = {commands, "RD"};
    now += T_RW;
    waited += now - start;
    if (now - start > worst) worst = now - start;
    $display("cycle %3d | %s row %0d column %0d | %s | the bus waited %0d cycles", start, what, row, column, commands, now - start);
  endtask

  // The bus is quiet for some cycles. A refresh that falls due is done in the quiet time.
  task automatic idle(input int cycles);
    int stop = now + cycles;
    string commands;
    while (due <= stop) begin
      if (now < due) now = due;
      commands = "";
      $write("cycle %3d | the bus is idle | ", now);
      refresh(commands);
      $display("%s| nobody waited", commands);
    end
    if (now < stop) now = stop;
  endtask
  task automatic totals;
    $display("accesses=%0d refreshes=%0d waited=%0d worst=%0d", accesses, refreshes, waited, worst);
  endtask

  initial begin
    for (int i = 0; i < 4; i++) access("read ", 7, i);
    totals;
  end
endmodule
ConceptWhat does ACT do?
ConceptRow 3 is open. A read of row 3 arrives, and no refresh is due. Which commands are sent?
ConceptWhy must PRE come before a REF when a row is open?
ConceptA refresh is due as an access arrives. Which goes first, on this page?
ConceptWith the prices of this page, what is the worst wait of a read?

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.

Practice

Report a problem with this page