Interrupt Registers in a Peripheral: Level, Edge and Write-1-to-Clear
A device has several reasons to ask for the processor, and one wire to ask with. Three small registers stand between the reasons and the wire.
You Will Be Able To
- Tell a level cause from an edge cause, and say why an edge must be saved.
- Read raw status, the enable bits and masked status, and find the cause behind one request wire.
- Explain why a saved flag is cleared one bit at a time, and why a new event must win a tie.
- Lesson
- S6.2
- Time
- 26 min
- Runs on
- Your browser
- Check
- 6 questions
A parts feeder has two wires to the chip. One stays high while its tray needs attention. The other pulses each time a part arrives. At the end of a shift the tray holds two parts, and the driver has counted one.
Before You Start
You need Registers: fields, access types and W1C. You need S0.8: an interrupt request, pending and the handler.
| Question | |
|---|---|
| 1 | What is the difference between a level cause and an edge cause? |
| 2 | Why must an edge be saved, and what saves it? |
| 3 | What do raw status, the enable bits and masked status each tell the driver? |
| 4 | Why does the handler clear a saved flag by writing 1 to that one bit? |
| 5 | A new event and a clear arrive on the same clock edge. Which must win, and why? |
The Idea: Some Causes Last and Some Are Moments
A device asks for the processor for a reason. Each reason is an interrupt cause, or cause for short. A full tray is one cause. A part arriving is another. Causes come in two kinds, and the difference decides the hardware.
A level cause is a condition that lasts. The tray needs attention until someone empties it. The device can simply show the condition. Whenever software looks, the answer is true.
An edge cause is a moment. The part arrives, the wire pulses, and the wire is quiet again. Software may look a thousand clocks later, and the wire will tell it nothing. So the device must save the moment in a flip-flop. That flip-flop is a sticky flag: an event sets it, and it stays set until software clears it.
Two more needs follow. Software must be able to silence a cause it is not ready to serve, without losing it. And when the one request wire goes high, software must be able to ask which cause did it. Three small registers meet both needs. The rest of this page is about them, and about one clock edge on which clearing a flag can go wrong.
New Words
| Word | Meaning |
|---|---|
| Interrupt cause | One reason a device has for asking the processor to serve it. Cause for short. |
| AND, OR | The two gate functions, written in capitals. AND is 1 when both inputs are 1. OR is 1 when any input is 1. |
| Level cause | A cause that is a lasting condition. It is true for as long as the condition holds. |
| Edge cause | A cause that is a moment: a wire changing from 0 to 1. |
| Sticky flag | A flip-flop that an event sets and that stays 1 until software clears it. |
| Raw status | A register that software reads. It has one bit for each cause, whether software wants it or not. |
| Interrupt enable bit | A bit that software writes to let one cause raise the interrupt request. Enable bit for short. |
| Mask | To switch a cause off by clearing its enable bit. To unmask is to set the bit. |
| Masked status | A read-only register: raw status AND the enable bits. It shows the causes that are present and allowed. |
| Acknowledge | To tell the device that an event has been dealt with, by clearing its flag. |
One Picture: From Two Pins to One Wire
- Peripheral
- Memory
- Bus
- Processor
- A level
Pin 0 is high for as long as the tray needs attention. The device passes it straight into bit 0 of raw status. When the pin falls, the bit falls.
- An edge
Pin 1 is high for a moment and then low again. The device compares each sample of the pin with the one before. A rise sets the sticky flag, and the flag stays.
- Raw status against the enable bits
Raw status shows both causes, wanted or not. The driver sets an enable bit for each cause it is ready to serve. Masked status is the AND of the two.
- One wire
The interrupt request is 1 if any bit of masked status is 1. The wire says "something here needs you". It does not say what.
- The clear
The handler reads masked status to learn the cause. After dealing with the part, it writes 1 to bit 1. That clears the sticky flag. The level needs no clear, and cannot be cleared.
Read the diagram as text
- Pin 0: a level: high while the tray needs attention
- Pin 1: a pulse: high for a moment when a part arrives
- Sticky flag: set by a rise of pin 1; stays 1 until cleared
- Raw status: bit 0 = the level, bit 1 = the flag
- Enable bits: one for each cause, written by the driver
- Masked status: raw status AND enable bits
- Interrupt request: 1 if any masked bit is 1
- Clear write: the driver writes 1 to bit 1
- Pin 0 → Raw status: the level itself
- Pin 1 → Sticky flag: a rise sets it
- Sticky flag → Raw status: the saved flag
- Raw status → Masked status: every cause
- Enable bits → Masked status: allowed causes
- Masked status → Interrupt request: OR of all bits
- Clear write → Sticky flag: clears it
The two interrupt causes of the GPIO block in this lab. A real GPIO block has such a path for every pin.
Here are the registers of that picture as a register description. The block is GPIO, short for general-purpose input and output: plain pins that software can read. Its page starts at 0x1000_1000.
Two rows share offset 0x24. A read of that word returns raw status. A write to it is the clear. You write 1 to the very bit you just read. GPIO has more registers than these four rows. The lesson on GPIO, later in the track, lists them all.
| Offset | Register | Access type | Bit 1 (the part pulse) | Bit 0 (the tray level) |
|---|---|---|---|---|
| 0x18 | Enable | RW, reset value 0 | lets the flag ask | lets the level ask |
| 0x24, when read | Raw status | RO | the sticky flag | the level of pin 0, live |
| 0x24, when written | Clear | W1C | writing 1 clears the sticky flag | nothing to clear |
| 0x28 | Masked status | RO | flag AND enable | level AND enable |
The Smallest Example, Worked by Hand
Follow both pins through five moments. Two-bit values are written with bit 1 on the left. Work the last three columns yourself from the first three.
| Moment | Pins | What happens | Raw status | Enable | Masked status | Request |
|---|---|---|---|---|---|---|
| 1 | 01 | Pin 1 pulsed and fell. Pin 0 rose and stays high. Nothing is enabled. | 11 | 00 | 00 | 0 |
| 2 | 01 | The driver writes 11 to Enable. | 11 | 11 | 11 | 1 |
| 3 | 00 | The tray is seen to. Pin 0 falls. | 10 | 11 | 10 | 1 |
| 4 | 10 | The driver writes 10 to Clear. On that same edge pin 1 rises again. | 10 | 11 | 10 | 1 |
| 5 | 00 | The driver writes 10 to Clear once more. No part arrives. | 00 | 11 | 00 | 0 |
Moment 1 shows why raw status exists. Both causes are recorded while the request wire is 0. Moment 3 shows a level clearing itself: nobody wrote to the Clear register.
Moment 4 is the hard one. One flag received two orders on one edge: "clear", for the first part, and "set", for the second. The table says the flag stays 1. Here is why it must.
| Flag before | A rise at this edge | A clear at this edge | Flag after, if the set wins | Flag after, if the clear wins |
|---|---|---|---|---|
| 1 | no | yes | 0 | 0 |
| 0 | yes | no | 1 | 1 |
| 1 | yes | yes | 1 | 0: the second part is lost |
The two designs differ in one row only. In that row the clear was meant for the old event, which the handler has dealt with. The new event has been seen by nobody. If the clear wins, the only record of it is gone.
The Rules
Rule 1: A level cause is true while its condition lasts.
No register write ends it. It goes away when the condition does.
Rule 2: An edge cause is saved in a sticky flag.
The flag is set by the event and stays 1 until software clears it.
Rule 3: Raw status records every cause, enabled or not.
Masking a cause does not stop the device from recording it.
Rule 4: The request is the OR of raw status AND enable.
One wire means only "some allowed cause is present". Masked status says which.
Rule 5: The handler clears only what it has dealt with.
It writes 1 to those bits and 0 to the rest. A 0 leaves a flag alone.
Rule 6: On a tie, the set wins.
If an event and a clear reach one flag on the same edge, the flag is 1 afterwards.
Sources for These Rules
A device asks for service with an interrupt: Harris and Harris, Digital Design and Computer Architecture, ARM edition, section 9.3.7.
The edge on which an APB write counts: Arm AMBA APB Protocol Specification, IHI 0024 issue E, section 3.1.
Rules 1 to 6 are common engineering practice for interrupt registers. No bus specification states them. Rule 6 is the choice of our GPIO block, and this page gives the reason for it.
The offsets, the two pins and the names of the registers are our own choices for ap_soc.
Now Run It
The lab at the end of this page runs the five moments on the GPIO block. Test code, a bus-functional model, plays the driver. The lab holds two copies of the block on the same pins. In one the set wins a tie. In the other, the faulty one, the clear wins. A switch named COLLIDE, set to 1, makes the second part arrive on the edge of the first clear.
first-rise pins=01 raw=11 en=00 masked=00 irq=0 faulty_raw=11 faulty_irq=0
read raw address=10001024 value=00000003
unmask pins=01 raw=11 en=11 masked=11 irq=1 faulty_raw=11 faulty_irq=1
read masked address=10001028 value=00000002
bus sel=1 access=0 rise=0 clear=0 raw_before=10
setup pins=00 raw=10 en=11 masked=10 irq=1 faulty_raw=10 faulty_irq=1
bus sel=1 access=1 rise=1 clear=1 raw_before=10
access pins=10 raw=10 en=11 masked=10 irq=1 faulty_raw=00 faulty_irq=0
bus sel=1 access=0 rise=0 clear=0 raw_before=10
bus sel=1 access=1 rise=0 clear=1 raw_before=10
service pins=00 raw=00 en=11 masked=00 irq=0 faulty_raw=00 faulty_irq=0Rule 3 at work: the first row has raw=11 with en=00 and irq=0. Rule 4 at work: after the unmask row, masked=11 and irq=1. The read of masked status then returns 2. Pin 0 has fallen, so the flag of pin 1 is the one cause left.
The waveform below shows that write. In it, raw_after is the flag of the correct copy just after each edge. The name faulty_after is the same flag in the faulty copy.
- Processor
- Peripheral
A lit wire carries a 1. A pill is a transfer in that cycle.
- Cycle 0
Setup selects the register. Its old flag survives this edge.
- Cycle 1
GPIO accepts the clear just as the pin rises. Compare the two flags after this edge.
Icarus capture. Bus lanes and raw_before are sampled at the edge. The after lanes show the settled register state.
Rule 6 at work: the second "bus" row has rise=1 and clear=1 at one edge. The row marked "access" follows it. The correct copy shows raw=10. The faulty copy shows faulty_raw=00 and faulty_irq=0: it has forgotten the second part.
The last row, "service", hides the fault again. After one more clear both copies read 00. A test that looks only at the end of the run passes both designs.
This lab leaves things out. Both pins are already lined up with the clock. A real pin is not, and must pass through two flip-flops first. The processor and the interrupt controller are not here: the request wire ends in the test code.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| Save an edge in a sticky flag | Pass the pulse straight to the request wire | The event waits for software. The cost is one flip-flop for each cause, and a clear that software must remember. |
| Record while masked | Ignore a cause while its enable bit is 0 | Software can be busy without losing events. It must check raw status when it unmasks. |
| A masked status register | The driver reads raw status and the enable bits and ANDs them | One read gives the answer, taken at one moment. The cost is a few gates. |
| Clear by writing 1 to a bit | Clear the whole register with any write | The handler names what it dealt with. Flags it never looked at survive. |
| The set wins a tie | The clear wins a tie | No event is ever lost. The handler may run once more for a flag it meant to clear. |
The Trap: The Part Nobody Counted
Back to the feeder. A part arrives and its flag is set. The handler counts the part and writes 1 to clear the flag. On the very edge where that write counts, a second part arrives.
In the faulty copy the clear wins. The flag is 0, the request wire is 0, and the pin is already quiet. No later clock can bring that edge back. The second part sits on the tray until something else wakes the driver.
Every simple test passes. Send parts one at a time and the count is right. Check the registers at the end of a run and they read 00. The fault needs two things together: a tie on one edge, and a look at the flag before the next clear. The practice problem The Event Lost on Clear hands you such a design to repair.
There is a quieter trap in rule 5. A handler reads masked status, gets 11, and writes that whole value to Clear. If it then serves only one of the two causes, it has thrown the other away itself.
At Scale
This section looks ahead. A real device has tens of causes, and the six rules stay the same.
A flag cannot count
A sticky flag says "at least one". If two parts arrive before the handler runs, it still says 1. A count needs a counter, or a queue, in the device.
After the device, a controller
Many devices each raise one wire. The next page, The Interrupt Controller, shows the block that chooses among them.
Proving rule 6
A tie on one edge is rare in simulation. A formal tool can prove that no event is lost: see register properties.
The registers as a design
A full register block for a peripheral is built in the peripheral register map.
Try It
Run the lab. Find the row where the two copies of the device first differ. Name the edge of the write that made them differ.
Set COLLIDE to 0 and run again. Explain why the two copies now agree on every row.
Change the enable write from 3 to 1. Predict which raw bit survives, and whether it can raise irq.
Names Printed by the Lab
| Name | Meaning |
|---|---|
| pins | The two pins, pin 1 on the left. |
| raw | Raw status of the correct copy. |
| en | The two enable bits. |
| masked | Masked status of the correct copy. |
| irq | The interrupt request of the correct copy. |
| faulty_raw, faulty_irq | The same two values in the faulty copy, where the clear wins a tie. |
| rise, clear | In a row that starts with "bus": pin 1 rises at this edge, and the clear write counts at this edge. |
| raw_before | In a row that starts with "bus": raw status just before this edge. |
first-rise pins=01 raw=11 en=00 masked=00 irq=0 faulty_raw=11 faulty_irq=0 read raw address=10001024 value=00000003 unmask pins=01 raw=11 en=11 masked=11 irq=1 faulty_raw=11 faulty_irq=1 read masked address=10001028 value=00000002 bus sel=1 access=0 rise=0 clear=0 raw_before=10 setup pins=00 raw=10 en=11 masked=10 irq=1 faulty_raw=10 faulty_irq=1 bus sel=1 access=1 rise=1 clear=1 raw_before=10 access pins=10 raw=10 en=11 masked=10 irq=1 faulty_raw=00 faulty_irq=0 bus sel=1 access=0 rise=0 clear=0 raw_before=10 bus sel=1 access=1 rise=0 clear=1 raw_before=10 service pins=00 raw=00 en=11 masked=00 irq=0 faulty_raw=00 faulty_irq=0Read included file: irq_lab_bus.svh
// APB register slice. One clock, aligned 32-bit transfers, all lanes enabled.
// Both peripheral models are always ready. Drive on falling edges.
logic clk = 0, rst_n = 0;
always #5 clk = ~clk;
logic select = 0, access_phase = 0, writing = 0;
logic [31:0] address = 0, data = 0;
wire accepted = select && access_phase && writing; // Completed write.
task automatic setup_write(input logic [31:0] addr, value);
@(negedge clk);
select = 1;
writing = 1;
address = addr;
data = value;
endtask
task automatic access_write;
@(negedge clk);
access_phase = 1;
endtask
task automatic end_write;
@(negedge clk);
select = 0;
access_phase = 0;
endtask
task automatic write_word(input logic [31:0] addr, value);
setup_write(addr, value);
access_write();
end_write();
endtask
Read included file: gpio_irq_slice.sv
// Two already-synchronous GPIO inputs. Pin 0 reports its live level.
// Pin 1 remembers a rise. The parameter selects a faulty comparison copy.
// A two-pin cut of the GPIO block: pin 0 is fixed as a level cause and pin 1 as a rising-edge cause.
module gpio_irq_slice #(parameter bit LOSE_COLLISION = 0) (
input logic clk, rst_n, accepted,
input logic [31:0] address, data,
input logic [1:0] pins,
output logic [1:0] raw, enabled, masked,
output logic [31:0] read_data,
output logic irq
);
// The offsets are those of the GPIO register table of ap_soc. Raw status and
// the clear share one word: a read returns raw status, a write of 1 clears that saved flag.
localparam logic [31:0] ENABLE = 32'h1000_1018, CLEAR = 32'h1000_1024;
localparam logic [31:0] RAW = 32'h1000_1024, MASKED = 32'h1000_1028;
always_comb begin
case (address)
ENABLE: read_data = {30'b0, enabled};
RAW: read_data = {30'b0, raw};
MASKED: read_data = {30'b0, masked};
default: read_data = 0;
endcase
end
logic previous_pin, edge_seen;
wire rise = pins[1] && !previous_pin;
wire clear_edge = accepted && address == CLEAR && data[1];
assign raw = {edge_seen, pins[0]};
assign masked = raw & enabled;
assign irq = |masked;
always_ff @(posedge clk) begin
if (!rst_n) begin
previous_pin <= 0;
edge_seen <= 0;
enabled <= 0;
end else begin
previous_pin <= pins[1];
if (accepted && address == ENABLE) enabled <= data[1:0];
if (rise && !(LOSE_COLLISION && clear_edge)) edge_seen <= 1;
else if (clear_edge) edge_seen <= 0;
end
end
endmodule
Check Yourself
Common Mistakes
Wrong Mask a cause to forget its saved event.
The enable bit stops the request only. The sticky flag is still set.
Fix Read raw status before unmasking, and clear what is stale.
Wrong Write a whole status value back to acknowledge one event.
Every 1 in the write clears a flag, served or not.
Fix Write 1 only to the bits of the causes you served.
Wrong Try to clear a level cause in the Clear register.
A level is the condition itself. Nothing is saved, so nothing can be cleared.
Fix Remove the condition at the device. The bit then falls by itself.
Remember
- A level cause shows a lasting condition. An edge cause is a moment and must be saved in a sticky flag.
- Raw status records every cause. The enable bits choose which may ask. Masked status is their AND.
- The request wire is the OR of masked status: it says that something needs service, not what.
- The handler clears a flag by writing 1 to that bit alone.
- When an event and a clear meet on one edge, the set must win, or the event is lost.