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.

Five questions this page answers
Question
1What is the difference between a level cause and an edge cause?
2Why must an edge be saved, and what saves it?
3What do raw status, the enable bits and masked status each tell the driver?
4Why does the handler clear a saved flag by writing 1 to that one bit?
5A 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

New words on this page
WordMeaning
Interrupt causeOne reason a device has for asking the processor to serve it. Cause for short.
AND, ORThe two gate functions, written in capitals. AND is 1 when both inputs are 1. OR is 1 when any input is 1.
Level causeA cause that is a lasting condition. It is true for as long as the condition holds.
Edge causeA cause that is a moment: a wire changing from 0 to 1.
Sticky flagA flip-flop that an event sets and that stays 1 until software clears it.
Raw statusA register that software reads. It has one bit for each cause, whether software wants it or not.
Interrupt enable bitA bit that software writes to let one cause raise the interrupt request. Enable bit for short.
MaskTo switch a cause off by clearing its enable bit. To unmask is to set the bit.
Masked statusA read-only register: raw status AND the enable bits. It shows the causes that are present and allowed.
AcknowledgeTo tell the device that an event has been dealt with, by clearing its flag.

One Picture: From Two Pins to One Wire

From Two Pins to One Request WireStep through the picture. It follows a level and an edge from the pins to the one wire that leaves the device.
  • Peripheral
  • Memory
  • Bus
  • Processor
From Two Pins to One Request WireStep through the picture. It follows a level and an edge from the pins to the one wire that leaves the device. component Pin 0 a level: high while thetray needs attention component Pin 1 a pulse: high for amoment when a partarrives component Sticky flag set by a rise of pin 1;stays 1 until cleared component Raw status bit 0 = the level, bit1 = the flag component Enable bits one for each cause,written by the driver component Masked status raw status ANDenable bits component Interrupt request 1 if any masked bit is1 component Clear write the driver writes 1to bit 1 the level itselfa rise sets itthe saved flagevery causeallowed causesOR of all bitsclears it
  1. component Pin 0a level: high while the tray needs attention
    • the level itself → Raw status
  2. component Raw statusbit 0 = the level, bit 1 = the flag
    • every cause → Masked status
  3. component Pin 1a pulse: high for a moment when a part arrives
    • a rise sets it → Sticky flag
  4. component Sticky flagset by a rise of pin 1; stays 1 until cleared
    • the saved flag → Raw status
  5. component Enable bitsone for each cause, written by the driver
    • allowed causes → Masked status
  6. component Masked statusraw status AND enable bits
    • OR of all bits → Interrupt request
  7. component Interrupt request1 if any masked bit is 1
  8. component Clear writethe driver writes 1 to bit 1
    • clears it → Sticky flag
  1. 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.

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

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

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

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

The interrupt registers of GPIO in this lab
OffsetRegisterAccess typeBit 1 (the part pulse)Bit 0 (the tray level)
0x18EnableRW, reset value 0lets the flag asklets the level ask
0x24, when readRaw statusROthe sticky flagthe level of pin 0, live
0x24, when writtenClearW1Cwriting 1 clears the sticky flagnothing to clear
0x28Masked statusROflag AND enablelevel 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.

Five moments, by hand
MomentPinsWhat happensRaw statusEnableMasked statusRequest
101Pin 1 pulsed and fell. Pin 0 rose and stays high. Nothing is enabled.1100000
201The driver writes 11 to Enable.1111111
300The tray is seen to. Pin 0 falls.1011101
410The driver writes 10 to Clear. On that same edge pin 1 rises again.1011101
500The driver writes 10 to Clear once more. No part arrives.0011000

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.

One flag, one clock edge, two possible designs
Flag beforeA rise at this edgeA clear at this edgeFlag after, if the set winsFlag after, if the clear wins
1noyes00
0yesno11
1yesyes10: 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

  1. Rule 1: A level cause is true while its condition lasts.

    No register write ends it. It goes away when the condition does.

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

  3. Rule 3: Raw status records every cause, enabled or not.

    Masking a cause does not stop the device from recording it.

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

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

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

Printed by the lab. Rows that start with "bus" are sampled at a clock edge of a write to Clear. The other rows show the state after a step. The names are listed with the lab.
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=0

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

Two Copies on the Same ClockStep across the two rising edges of the first clear write.
  • Processor
  • Peripheral
accessHandler modelGPIO flags
access→ GPIO flagsHandler modelGPIO flags

A lit wire carries a 1. A pill is a transfer in that cycle.

01clkselaccessriseclearraw_before10raw_after10faulty_after1000
Scroll horizontally ↔
  1. Cycle 0

    Setup selects the register. Its old flag survives this edge.

  2. 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 choiceThe alternativeWhat the choice buys, and costs
Save an edge in a sticky flagPass the pulse straight to the request wireThe event waits for software. The cost is one flip-flop for each cause, and a clear that software must remember.
Record while maskedIgnore a cause while its enable bit is 0Software can be busy without losing events. It must check raw status when it unmasks.
A masked status registerThe driver reads raw status and the enable bits and ANDs themOne read gives the answer, taken at one moment. The cost is a few gates.
Clear by writing 1 to a bitClear the whole register with any writeThe handler names what it dealt with. Flags it never looked at survive.
The set wins a tieThe clear wins a tieNo 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 page2 causes, 1 wire
A real peripheraltens of causes, 1 wire

This section looks ahead. A real device has tens of causes, and the six rules stay the same.

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

  2. After the device, a controller

    Many devices each raise one wire. The next page, The Interrupt Controller, shows the block that chooses among them.

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

  4. 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
NameMeaning
pinsThe two pins, pin 1 on the left.
rawRaw status of the correct copy.
enThe two enable bits.
maskedMasked status of the correct copy.
irqThe interrupt request of the correct copy.
faulty_raw, faulty_irqThe same two values in the faulty copy, where the clear wins a tie.
rise, clearIn a row that starts with "bus": pin 1 rises at this edge, and the clear write counts at this edge.
raw_beforeIn a row that starts with "bus": raw status just before this edge.
RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: 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=0
Read 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

Predict the outputIn this run no second part arrives, and the lab stops just after the first clear. What is the last row it prints?
`timescale 1ns/1ns
module tb;
  `include "irq_lab_bus.svh"
  localparam bit COLLIDE = 0;
  logic [1:0] pins = 0;
  wire [1:0] raw, enabled, masked, faulty_raw;
  wire irq, faulty_irq;
  wire [31:0] read_data;
  task automatic read_word(input logic [31:0] addr, input string label_text);
    @(negedge clk);
    select = 1;
    writing = 0;
    address = addr;
    @(negedge clk);
    access_phase = 1;
    @(posedge clk);
    $display("read %s address=%h value=%h", label_text, address, read_data);
    @(negedge clk);
    select = 0;
    access_phase = 0;
  endtask
  always @(posedge clk) begin
    if (select && writing && address == 32'h1000_1024) begin
      $display("bus sel=%b access=%b rise=%b clear=%b raw_before=%b",
               select, access_phase, fixed_copy.rise, fixed_copy.clear_edge, raw);
    end
  end
  gpio_irq_slice fixed_copy (.*);
  gpio_irq_slice #(.LOSE_COLLISION(1)) faulty_copy (
    .raw(faulty_raw), .irq(faulty_irq), .read_data(), .enabled(), .masked(), .*
  );
  task automatic report(input string step);
    $display("%s pins=%b raw=%b en=%b masked=%b irq=%b faulty_raw=%b faulty_irq=%b",
             step, pins, raw, enabled, masked, irq, faulty_raw, faulty_irq);
  endtask
  initial begin
    repeat (2) @(negedge clk);
    rst_n = 1;
    pins = 3;
    @(negedge clk);
    pins = 1;
    #1 report("first-rise");
    read_word(32'h1000_1024, "raw");
    write_word(32'h1000_1018, 3);
    report("unmask");
    pins = 0;
    read_word(32'h1000_1028, "masked");
    setup_write(32'h1000_1024, 2);
    @(posedge clk);
    #1 report("setup");
    access_write();
    pins[1] = COLLIDE;
    @(posedge clk);
    #1 report("access");
    $finish;
    end_write();
    pins = 0;
    write_word(32'h1000_1024, 2);
    report("service");
    $finish;
  end
endmodule
Read 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
ConceptWhich cause can go away with no write from software?
ConceptWhy does a device save an edge in a sticky flag?
ConceptThe request wire is 1. Which register tells the handler which allowed cause is present?
ConceptWhy does the handler write 1 to one bit of Clear, and 0 to the rest?
ConceptAn event and a clear reach one flag on the same edge. What must the flag be afterwards, and why?

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.

Practice

Report a problem with this page