Registers: The Contract Between Hardware and Software

A device register looks like a word of memory at an address. It is a set of promises, one for each field. This page teaches how to read those promises.

You Will Be Able To

  • Say what a device register and a field are, and why fields have different rules.
  • Predict what the access types RW, RO, WO, W1C and RC return on a read and do on a write.
  • Explain how a read can change a device, and what a register description must state.
Lesson
S6.8
Time
27 min
Runs on
Your browser
Check
6 questions

A debugger, a tool that reads registers while a program is stopped, shows that the timer saw an event. You let the program run one more line, and it reports no event. Both looked at the same address, a moment apart, and both were told the truth.

Before You Start

You need S0.2: a device is reached at an address. You need S0.4: an APB transfer has a setup cycle and an access cycle.

This is the first page of the module. The pages on interrupts that follow all use its words.

Five questions this page answers
Question
1What is a device register, and what is a field?
2Why can a write and the read that follows it give different values?
3What do the access types RW, RO, WO, W1C and RC each promise?
4How can a read change the device, and who is hurt when it does?
5What must a register description say about reset and about reserved bits?

The Idea: One Address, Many Small Promises

Software has only two ways to touch a device. It can read a word at an address, or write a word there. A device register is one such word. Inside the device, its bits are flip-flops and wires.

Those bits do different jobs. One stores a setting. One shows a wire that the hardware drives. One starts an action. One remembers that something happened. So the word is cut into pieces. Each piece is a register field: a bit or a group of bits with one job.

A memory box has one rule: a read returns what was last written. A field may break that rule, and it must say how. The rule of a field is its access type. The list of all fields with their rules is the register description.

That description is an agreement between two people who may never meet. One designs the hardware. The other writes the device driver, the software that operates the device. A driver that treats a register as memory fails in ways no test of the hardware will show.

New Words

New words on this page
WordMeaning
Device registerA word inside a device that software reads or writes at one address.
Register fieldA bit, or a group of neighbouring bits, of a register that does one job. Field for short.
Access typeThe rule of a field: what a read returns, and what a write does.
RWRead-write. A write is stored. A read returns the stored value.
RORead-only. A read returns a value that the hardware supplies. A write is ignored.
WOWrite-only. A write causes an action. A read returns 0.
W1CWrite 1 to clear. Hardware sets the bit. Software clears it by writing 1. Writing 0 changes nothing.
RCRead to clear. Hardware sets the bit. A read returns it and then clears it.
Write-onceThe first write is stored and locked. Later writes are ignored until reset.
Reserved bitA bit with no job yet. Here it reads 0 and ignores writes.
Field reset valueWhat a stored field holds just after reset.
Access side effectAnything a read or a write does besides returning or storing a value.
Register descriptionThe table that lists every field of a register: its bits, its access type and its reset value.
Device driverThe software that operates one device through its registers. Driver for short.
DebuggerA tool that stops a program and lets an engineer read memory and registers.

One Picture: One Write, Six Fields

Here is one register with six fields, one of each access type. It is the EVENT word of the Timer and PWM block of ap_soc. Software writes one byte to it. Step through what each field does with its bit.

One Write Reaches Six FieldsStep through the picture. Software writes the byte e5, which is 1110 0101 in binary. Each field treats its own bit by its own rule.
  • Bus
  • Peripheral
CallValueReturnWaiting
One Write Reaches Six FieldsStep through the picture. Software writes the byte e5, which is 1110 0101 in binary. Each field treats its own bit by its own rule. component Write e5 the low byte ofPWDATA RW Bit 0: enable a stored setting RO Bit 1: online a live input WO Bit 2: kick a command W1C Bit 3: pending an event flag RC Bit 4: seen an event flag write once Bit 5: route a locked setting bit 0 = 1bit 1 = 0bit 2 = 1bit 3 = 0bit 4 = 0bit 5 = 1
  1. component Write e5the low byte of PWDATA
    • bit 0 = 1 → Bit 0: enable
    • bit 1 = 0 → Bit 1: online
    • bit 2 = 1 → Bit 2: kick
    • bit 3 = 0 → Bit 3: pending
    • bit 4 = 0 → Bit 4: seen
    • bit 5 = 1 → Bit 5: route
  1. Bit 0: enable

    The 1 is stored. Every later read returns it.

    Follow the calls, values and waits
    1. data 1: Write e5 → Bit 0: enable
  2. Bit 1: online

    The 0 written here is ignored. The bit shows a wire, and software cannot move that wire.

    Follow the calls, values and waits
    1. data 0: Write e5 → Bit 1: online
  3. Bit 2: kick

    The 1 makes one pulse on the kick output. Nothing is stored, so the bit reads 0.

    Follow the calls, values and waits
    1. data 1: Write e5 → Bit 2: kick
  4. Bit 3: pending

    The 0 written here does nothing. Only a written 1 would clear the flag.

    Follow the calls, values and waits
    1. data 0: Write e5 → Bit 3: pending
  5. Bit 4: seen

    A write never changes this flag. A read returns it and then clears it.

    Follow the calls, values and waits
    1. data 0: Write e5 → Bit 4: seen
  6. Bit 5: route

    The 1 is stored and locked. No later write can change it until reset.

    Follow the calls, values and waits
    1. data 1: Write e5 → Bit 5: route
Read the diagram as text
  • Write e5: the low byte of PWDATA
  • Bit 0: enable: a stored setting
  • Bit 1: online: a live input
  • Bit 2: kick: a command
  • Bit 3: pending: an event flag
  • Bit 4: seen: an event flag
  • Bit 5: route: a locked setting
  • Write e5 → Bit 0: enable: bit 0 = 1
  • Write e5 → Bit 1: online: bit 1 = 0
  • Write e5 → Bit 2: kick: bit 2 = 1
  • Write e5 → Bit 3: pending: bit 3 = 0
  • Write e5 → Bit 4: seen: bit 4 = 0
  • Write e5 → Bit 5: route: bit 5 = 1

The EVENT word of the Timer and PWM block. Bits 6 to 31 are reserved. All six fields see the same clock edge.

Six bits arrived on the same edge, and only two of them were stored. The byte written was e5. A read just afterwards returns 23. Nothing went wrong: each field kept its promise.

The Smallest Example, Worked by Hand

First, the register description. You need nothing else to predict every read below.

The register description of the EVENT word
BitFieldAccess typeReset valueValue of the bit in a read
0enableRW001
1onlineROnone: it shows a live wire02
2kickWOnone: nothing is storedalways reads 0
3pendingW1C008
4seenRC010
5routewrite-once0, and unlocked20
6 to 31reservedreads 0, ignores writesnone00

The last column is in hexadecimal. A read returns the sum of the fields that are 1.

Now six steps. The online wire is held at 1 throughout. Work each row from the table above before you read its last cell.

Six steps, by hand
StepWhat happensWhat each field doesA read would now return
1Reset endsEvery stored field is 0. The online wire is 1.02
2Software writes e5enable stores 1. kick pulses. route stores 1 and locks. The other bits are ignored.20 + 02 + 01 = 23
3The hardware reports one eventpending and seen are both set to 1.23 + 08 + 10 = 3b
4A readIt returns 3b. Then seen is cleared, because seen is RC.2b
5A second readIt returns 2b. Nothing is left for a read to clear.2b
6Software writes 09The 1 in bit 3 clears pending. enable stores 1 again. The 0 in bit 5 is ignored: route is locked.23

Step 6 shows why W1C exists. The write named one flag, pending, and cleared only that flag. Step 4 shows the price of RC: the read itself spent the flag.

The Rules

  1. Rule 1: An access counts on one edge only.

    For APB that is the edge at the end of the access cycle, with PSEL, PENABLE and PREADY all at 1. Before it, nothing in the register changes.

  2. Rule 2: Every field has one access type.

    The type says what a read returns and what a write does. Predict each field by itself, then add the bits.

  3. Rule 3: A read can have a side effect.

    A read of an RC field clears it. So it matters who reads the register, and how often.

  4. Rule 4: A write of 0 is not always "no change".

    It is no change for a W1C or a WO field. For an RW field it stores 0, and for a write-once field it uses up the one write.

  5. Rule 5: A stored field has a reset value. A live field has none.

    After reset, a read returns the reset values plus whatever the live wires show.

  6. Rule 6: Software writes 0 to reserved bits.

    A later version of the chip may give such a bit a job. An old driver that wrote 1 there would then do something new.

  7. Rule 7: The description says who wins a tie.

    Hardware may set a flag on the same edge on which software clears it. One of them must win, and the description must say which.

Rule 7 is only stated here. The next page, Level, Edge and Write-1-to-Clear, shows such a tie on a real edge. It also shows what is lost when the wrong side wins.

Sources for These Rules

Rule 1: Arm AMBA APB Protocol Specification, IHI 0024 issue E, section 3.1 (write transfers) and section 3.3 (read transfers).

Devices reached by ordinary loads and stores: Harris and Harris, Digital Design and Computer Architecture, ARM edition, section 9.2.

The names RO, RW, RC, WO and W1C are field access policies of IEEE Std 1800.2-2020, clause 18.5.4.6. The same list has a write-once policy. That standard describes register models for verification. Chip makers use the same names in their own register descriptions.

Rules 3 to 7 are common engineering practice. No bus specification states them, because a bus does not know what a register means.

The EVENT word, its six fields and its address are our own choices for ap_soc.

Now Run It

The lab at the end of this page runs the six steps on the real register. Test code stands in for the processor and drives the APB wires. That kind of test code is called a bus-functional model.

Printed by the lab. Values are in hexadecimal. The two rows that start with "write" are one write, sampled at two edges.
reset read=02
write sel=1 enable=0 ready=1 data=e5 fields=02 kick=0
write sel=1 enable=1 ready=1 data=e5 fields=02 kick=0
config fields=23 kick=1
debugger read=3b
driver read=2b
acknowledged read=23

Rule 5 at work: the reset read is 02, the live online wire and nothing else. Rule 1 at work: both "write" rows still show 02, and the "config" row after the edge shows 23.

The Edge on Which the Write CountsThree samples of the same write, recorded from the lab.
012fields0223kick
Scroll horizontally ↔
  1. Sample 0

    The setup cycle. PSEL is 1 and PENABLE is 0. The register still reads 02.

  2. Sample 1

    The access cycle, just before its edge. PSEL, PENABLE and PREADY are all 1. The register still reads 02.

  3. Sample 2

    Just after that edge. The register reads 23, and the kick output is 1 for this one cycle.

Recorded from the lab. Samples two and three sit on either side of one clock edge.

Rule 3 at work: the read marked "debugger" returns 3b and the read marked "driver" returns 2b. They differ by 10, which is the seen field. Rule 2 at work: the last row is 23 again, because the write of 09 cleared pending alone.

The lab does not show a tie on one edge, and it does not show a value wider than one word. Both have pages of their own, named at the end.

Why It Is Built This Way

Design choiceThe alternativeWhat the choice buys, and costs
A flag is W1CA flag is RW, and software stores 0 to clear itSoftware names exactly the flags it has dealt with. With RW, storing a whole word could erase a flag that was set a moment ago.
A flag is W1CA flag is RCLooking at a W1C flag is harmless. An RC flag saves one write, but every reader spends it.
A command is WOA command is an RW bit that software sets and then clearsOne write makes exactly one pulse. Software cannot leave the command switched on by mistake.
A setting is write-onceThe setting stays RWA bug later in the program cannot change it. The cost: a wrong first write stays until reset.
Reserved bits read 0Reserved bits return anythingA driver can compare a whole word against a known value. The hardware spends a few gates.

The Trap: The Read That Spent the Evidence

Return to the debugger. An engineer stops the program and opens a window on the timer registers. The window reads the EVENT word and shows 3b: an event, pending and seen. The engineer lets the program run.

The driver now reads the same word and gets 2b. Its code tests the seen bit, finds 0, and reports that nothing happened. The debugger read first, and that read cleared the flag. Close the window and the bug is gone, which makes it worse.

There is a second trap on the write side. A driver reads 2b, changes one setting in its copy, and writes the whole word back. The copy holds a 1 in bit 3. That 1 clears pending, though nobody dealt with the event. Build each write from what you mean to do, never from what you read.

One Read in C, and the Bus Transfer It Becomes
The caller passes the address of the EVENT word. The word "volatile" tells the compiler that every read must really happen.
typedef unsigned word;
word observe(volatile const word *event_word) {
    return *event_word;
}
The same function as RISC-V instructions. The one load is one bus read, and that read clears the seen field.
observe:
    lw  a0, 0(a0)
    ret

At Scale

This page1 register, 6 fields
A phone-class chipthousands of registers

This section looks ahead. A real chip has thousands of registers, and the seven rules do not change.

  1. One description, many readers

    The description is kept as one data file. Tools make the hardware decode, the driver's constants and the manual from it, so the three cannot drift apart.

  2. A value wider than one word

    A 64-bit counter needs two reads on a 32-bit bus, and it can move between them. The page Timers shows the fault and the cure.

  3. A tie on one edge

    A fresh event and a clear can meet on one edge. The page Level, Edge and Write-1-to-Clear runs both outcomes.

  4. Flags that ask for service

    A pending flag, an enable bit and a wire to the processor make an interrupt. That is the subject of the next three pages.

Try It

Run the lab. Find the two reads that differ in exactly one bit. Name the field, and say which read changed it.

Delete the line that prints the debugger read. Predict what the driver read returns now.

Change the last write from 09 to 01. Predict the last row before you run it.

The second file, event_word.sv, is the register itself. Find the line that makes kick last one cycle.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: reset read=02 write sel=1 enable=0 ready=1 data=e5 fields=02 kick=0 write sel=1 enable=1 ready=1 data=e5 fields=02 kick=0 config fields=23 kick=1 debugger read=3b driver read=2b acknowledged read=23
Read included file: event_word.sv
// S6 teaching register at timer base + 0x20. Low-byte writes only.
// HW event wins over simultaneous acknowledgement or consumption.
module event_word(
  input logic pclk, presetn, psel, penable, pwrite,
  input logic [31:0] paddr, pwdata, input logic [3:0] pstrb,
  input logic event_in, online,
  output logic pready, pslverr, kick,
  output logic [31:0] prdata
);
  logic enable, pending, seen, route, locked;
  logic [63:0] event_count;
  wire low_word = paddr == 32'h1000_2024;
  wire high_word = paddr == 32'h1000_2028;
  wire target = paddr == 32'h1000_2020;
  wire completed = target && psel && penable && pready;
  wire write_byte = completed && pwrite && pstrb[0];
  assign pready = 1;
  assign pslverr = psel && penable && !(target || (!pwrite && (low_word || high_word)));
  assign prdata = target ? {26'b0, route, seen, pending, 1'b0, online, enable} :
                  low_word ? event_count[31:0] : high_word ? event_count[63:32] : 0;

  always_ff @(posedge pclk) begin
    if (!presetn) begin
      // Seed close to carry so one hardware pulse crosses the word boundary.
      event_count <= 64'h0000_0007_ffff_fffe;
      enable <= 0; pending <= 0; seen <= 0;
      route <= 0; locked <= 0; kick <= 0;
    end else begin
      if (event_in) event_count <= event_count + 1'b1;
      kick <= write_byte && pwdata[2];
      if (write_byte) begin
        enable <= pwdata[0];
        if (!locked) begin route <= pwdata[5]; locked <= 1; end
      end
      if (event_in) begin pending <= 1; seen <= 1; end
      else begin
        if (write_byte && pwdata[3]) pending <= 0;
        if (completed && !pwrite) seen <= 0;
      end
    end
  end
endmodule

Check Yourself

Predict the outputIn this run the online wire is held at 0. Nothing else changes. What is the last line the lab prints?
`timescale 1ns/1ns
module tb;
  logic pclk = 0, presetn = 0;
  always #5 pclk = ~pclk;                  // the clock: one rising edge every 10 ns
  logic psel = 0, penable = 0, pwrite = 0; // the APB wires of S0.4
  logic [31:0] paddr = 0, pwdata = 0;
  logic [3:0] pstrb = 0;                   // one bit for each byte of pwdata that counts
  logic event_in = 0;                      // from the hardware side: 1 for one clock when something happens
  logic online = 0;                        // from the hardware side: a live level
  wire pready, pslverr, kick;
  wire [31:0] prdata;
  localparam logic [31:0] EVENT = 32'h1000_2020;   // Timer and PWM, offset 0x20
  event_word dut (.*);                     // the register itself, in the second file

  logic [31:0] got;
  bit trace_write = 0;
  // One APB transfer to the EVENT word: a setup cycle, then an access cycle.
  task automatic transfer(input bit wr, input logic [31:0] data);
    @(negedge pclk); psel = 1; penable = 0; pwrite = wr; paddr = EVENT; pwdata = data; pstrb = wr ? 4'b0001 : 0;
    @(negedge pclk); penable = 1;
    @(posedge pclk); got = prdata;         // the edge that completes the transfer
    @(negedge pclk); psel = 0; penable = 0;
  endtask
  always @(posedge pclk) if (trace_write && psel)
    $display("write sel=%b enable=%b ready=%b data=%02h fields=%02h kick=%b",
             psel, penable, pready, pwdata[7:0], prdata[7:0], kick);

  initial begin
    repeat (2) @(negedge pclk); presetn = 1;        // leave reset
    transfer(0, 0);            $display("reset read=%02h", got[7:0]);
    trace_write = 1;
    transfer(1, 32'hffff_ffe5);                     // one write: ones in bits 0, 2, 5, 6, 7 and above
    trace_write = 0;
    $display("config fields=%02h kick=%b", prdata[7:0], kick);
    @(negedge pclk); event_in = 1;                  // the hardware reports one event
    @(negedge pclk); event_in = 0;
    transfer(0, 0);            $display("debugger read=%02h", got[7:0]);
    transfer(0, 0);            $display("driver read=%02h", got[7:0]);
    transfer(1, 32'h0000_0009);                     // enable stays 1; the 1 in bit 3 acknowledges pending
    transfer(0, 0);            $display("acknowledged read=%02h", got[7:0]);
    $finish;
  end
endmodule
Read included file: event_word.sv
// S6 teaching register at timer base + 0x20. Low-byte writes only.
// HW event wins over simultaneous acknowledgement or consumption.
module event_word(
  input logic pclk, presetn, psel, penable, pwrite,
  input logic [31:0] paddr, pwdata, input logic [3:0] pstrb,
  input logic event_in, online,
  output logic pready, pslverr, kick,
  output logic [31:0] prdata
);
  logic enable, pending, seen, route, locked;
  logic [63:0] event_count;
  wire low_word = paddr == 32'h1000_2024;
  wire high_word = paddr == 32'h1000_2028;
  wire target = paddr == 32'h1000_2020;
  wire completed = target && psel && penable && pready;
  wire write_byte = completed && pwrite && pstrb[0];
  assign pready = 1;
  assign pslverr = psel && penable && !(target || (!pwrite && (low_word || high_word)));
  assign prdata = target ? {26'b0, route, seen, pending, 1'b0, online, enable} :
                  low_word ? event_count[31:0] : high_word ? event_count[63:32] : 0;

  always_ff @(posedge pclk) begin
    if (!presetn) begin
      // Seed close to carry so one hardware pulse crosses the word boundary.
      event_count <= 64'h0000_0007_ffff_fffe;
      enable <= 0; pending <= 0; seen <= 0;
      route <= 0; locked <= 0; kick <= 0;
    end else begin
      if (event_in) event_count <= event_count + 1'b1;
      kick <= write_byte && pwdata[2];
      if (write_byte) begin
        enable <= pwdata[0];
        if (!locked) begin route <= pwdata[5]; locked <= 1; end
      end
      if (event_in) begin pending <= 1; seen <= 1; end
      else begin
        if (write_byte && pwdata[3]) pending <= 0;
        if (completed && !pwrite) seen <= 0;
      end
    end
  end
endmodule
ConceptWhat is a field of a device register?
ConceptSoftware writes 1 to a WO command bit and reads the register at once. Why is that bit 0?
ConceptA W1C flag is 1. Which access clears it?
ConceptTwo programs read a register that has an RC field. What can go wrong?
ConceptWhat should a driver write to a reserved bit, and why?

Common Mistakes

Wrong Expect every written bit to read back.

A command only pulses, and a status bit shows a live wire.

Fix Predict the readback one field at a time, from its access type.

Wrong Watch a read-to-clear register in a debugger while the driver runs.

The first read spends the flag.

Fix Let one reader own the register, or look at a copy that the driver saved.

Wrong Read a register, change one bit and write the word back.

Every 1 read from a W1C field is written back, and clears a flag.

Fix Write 1 only to the flags you have dealt with.

Remember

  • A device register is a word at an address, cut into fields that each do one job.
  • The access type of a field says what a read returns and what a write does.
  • A write counts on one edge only, and a read just after it can differ from the value written.
  • A read can have a side effect. With an RC field, the first reader spends the flag.
  • The register description is the agreement: bits, access types, reset values, reserved bits and who wins a tie.

Practice

Report a problem with this page