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.
| Question | |
|---|---|
| 1 | What is a device register, and what is a field? |
| 2 | Why can a write and the read that follows it give different values? |
| 3 | What do the access types RW, RO, WO, W1C and RC each promise? |
| 4 | How can a read change the device, and who is hurt when it does? |
| 5 | What 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
| Word | Meaning |
|---|---|
| Device register | A word inside a device that software reads or writes at one address. |
| Register field | A bit, or a group of neighbouring bits, of a register that does one job. Field for short. |
| Access type | The rule of a field: what a read returns, and what a write does. |
| RW | Read-write. A write is stored. A read returns the stored value. |
| RO | Read-only. A read returns a value that the hardware supplies. A write is ignored. |
| WO | Write-only. A write causes an action. A read returns 0. |
| W1C | Write 1 to clear. Hardware sets the bit. Software clears it by writing 1. Writing 0 changes nothing. |
| RC | Read to clear. Hardware sets the bit. A read returns it and then clears it. |
| Write-once | The first write is stored and locked. Later writes are ignored until reset. |
| Reserved bit | A bit with no job yet. Here it reads 0 and ignores writes. |
| Field reset value | What a stored field holds just after reset. |
| Access side effect | Anything a read or a write does besides returning or storing a value. |
| Register description | The table that lists every field of a register: its bits, its access type and its reset value. |
| Device driver | The software that operates one device through its registers. Driver for short. |
| Debugger | A 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.
- Bus
- Peripheral
- Bit 0: enable
The 1 is stored. Every later read returns it.
Follow the calls, values and waits
- data 1: Write e5 → Bit 0: enable
- 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
- data 0: Write e5 → Bit 1: online
- 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
- data 1: Write e5 → Bit 2: kick
- Bit 3: pending
The 0 written here does nothing. Only a written 1 would clear the flag.
Follow the calls, values and waits
- data 0: Write e5 → Bit 3: pending
- Bit 4: seen
A write never changes this flag. A read returns it and then clears it.
Follow the calls, values and waits
- data 0: Write e5 → Bit 4: seen
- Bit 5: route
The 1 is stored and locked. No later write can change it until reset.
Follow the calls, values and waits
- 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.
| Bit | Field | Access type | Reset value | Value of the bit in a read |
|---|---|---|---|---|
| 0 | enable | RW | 0 | 01 |
| 1 | online | RO | none: it shows a live wire | 02 |
| 2 | kick | WO | none: nothing is stored | always reads 0 |
| 3 | pending | W1C | 0 | 08 |
| 4 | seen | RC | 0 | 10 |
| 5 | route | write-once | 0, and unlocked | 20 |
| 6 to 31 | reserved | reads 0, ignores writes | none | 00 |
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.
| Step | What happens | What each field does | A read would now return |
|---|---|---|---|
| 1 | Reset ends | Every stored field is 0. The online wire is 1. | 02 |
| 2 | Software writes e5 | enable stores 1. kick pulses. route stores 1 and locks. The other bits are ignored. | 20 + 02 + 01 = 23 |
| 3 | The hardware reports one event | pending and seen are both set to 1. | 23 + 08 + 10 = 3b |
| 4 | A read | It returns 3b. Then seen is cleared, because seen is RC. | 2b |
| 5 | A second read | It returns 2b. Nothing is left for a read to clear. | 2b |
| 6 | Software writes 09 | The 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
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.
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.
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.
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.
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.
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.
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.
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=23Rule 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.
- Sample 0
The setup cycle. PSEL is 1 and PENABLE is 0. The register still reads 02.
- Sample 1
The access cycle, just before its edge. PSEL, PENABLE and PREADY are all 1. The register still reads 02.
- 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 choice | The alternative | What the choice buys, and costs |
|---|---|---|
| A flag is W1C | A flag is RW, and software stores 0 to clear it | Software 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 W1C | A flag is RC | Looking at a W1C flag is harmless. An RC flag saves one write, but every reader spends it. |
| A command is WO | A command is an RW bit that software sets and then clears | One write makes exactly one pulse. Software cannot leave the command switched on by mistake. |
| A setting is write-once | The setting stays RW | A bug later in the program cannot change it. The cost: a wrong first write stays until reset. |
| Reserved bits read 0 | Reserved bits return anything | A 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
typedef unsigned word;
word observe(volatile const word *event_word) {
return *event_word;
}observe:
lw a0, 0(a0)
retAt Scale
This section looks ahead. A real chip has thousands of registers, and the seven rules do not change.
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.
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.
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.
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.
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=23Read 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
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.