The Watchdog as a System Test

A watchdog resets the chip unless software proves, in time, that it is still running. It is a product's last line of defence, so the chip level tests it as a system. That means the key, the window, the warning, the reset, the record and what survives.

You Will Be Able To

  • Explain the key sequence, the window and the early warning of ap_soc's watchdog, and why each exists.
  • Kick a watchdog inside its window from firmware, with a time base the core owns.
  • Test what a watchdog reset leaves behind, and what other resets do to the watchdog.
  • Order the steps of a watchdog test so that every failure can still be reported.
Lesson
V9.7
Time
26 min
Runs on
Your browser
Check
5 questions

A watchdog brings hung firmware back, so the day it fails the product stays dead. On ap_soc it runs on clk_aon; in this lab it bit 321 cycles after the last kick.

Kick, Warning, Reset, CauseOne run of the watchdog on one timeline; each column is a recorded moment.
0123456cycle279483651798104211191542armedkickwarningresetn_sysRESET_CAUSE0x010x05armedkick 1kick 2kick 3warningresetnext boot
Scroll horizontally ↔
  1. Cycle 0

    Cycle 279: the key, 0x51 then 0xA2, unlocks one write; CONTROL = 1 arms the watchdog.

  2. Cycle 3

    Three kicks, 168 and 147 cycles apart, each inside the window: no warning.

  3. Cycle 4

    No more kicks. At cycle 1042 the program sees the early warning, interrupt source 6, as a pending bit.

  4. Cycle 5

    Two ticks after the warning the watchdog resets the chip: resetn_sys falls at cycle 1119, 321 cycles after the last kick.

  5. Cycle 6

    The next boot reads RESET_CAUSE 0x05: power-on and the watchdog. By the RTL its own reset has disarmed it.

Recorded on ap_soc v1.5 (Verilator 5.052 2026-09-05): events written by the firmware, the reset from the bench's sample window. Columns are not evenly spaced in time.

Before You Start

Read A Test for Every Reset Source and Clock Control first. The watchdog's registers and its role in bring-up are introduced in The Watchdog Joins Bring-Up.

Five questions this page answers
Question
1Why does a watchdog need a key, and why a window?
2What is the early warning for?
3How does firmware kick inside the window when it cannot read the watchdog?
4What survives a watchdog reset, and what does a pin reset do to the watchdog?
5In what order should a watchdog test take its steps?

The Idea: A Timer Software Must Keep Feeding

The Watchdog's PartsFrom a kick to a reset, and the record it leaves.
  • Processor
  • Peripheral
  • Control / FSM
  • Clock / reset
  • Data
  • Interrupt
  • Reset
The Watchdog's PartsFrom a kick to a reset, and the record it leaves. processor Firmware key, kicks peripheral Watchdog on clk_aon control ap_plic source 6: warning clock reset ap_crg reset, cause bit 2 KEY, CONTROL,SERVICEearly warningreset request
  1. processor Firmwarekey, kicks
    • KEY, CONTROL, SERVICE → Watchdog
  2. peripheral Watchdogon clk_aon
    • early warning → ap_plic
    • reset request → ap_crg
  3. control ap_plicsource 6: warning
  4. clock reset ap_crgreset, cause bit 2
  1. Software feeds it

    The key arms it; each kick starts the count again.

  2. It warns

    With 1 tick left, or a kick too early, it raises interrupt source 6.

  3. It bites

    Two ticks later it asks ap_crg for a reset, recorded as cause bit 2.

Read the diagram as text
  • Firmware: key, kicks
  • Watchdog: on clk_aon
  • ap_plic: source 6: warning
  • ap_crg: reset, cause bit 2
  • Firmware → Watchdog: KEY, CONTROL, SERVICE
  • Watchdog → ap_plic: early warning
  • Watchdog → ap_crg: reset request

watchdog_supervisor.sv inside ap_watchdog.sv; ap_crg.sv (facts 22, 27.9, 27.11).

A watchdog counts down. Software must kick it, a write that starts the count again, before it reaches the end. If software hangs, the kicks stop and the watchdog resets the chip. So it guards against the one failure software cannot report: its own.

A runaway program can write anything, so a plain kick register is not enough. ap_soc's watchdog needs a two-step key before it can be armed. Once armed, only a reset disarms it. It also refuses a kick that comes too early. A program stuck in a loop that kicks very fast is as broken as one that never kicks.

Before it resets, the watchdog raises an early warning, an interrupt, so software can save what it knows. Then the reset comes, and the record of causes says it was the watchdog. Each of these is a feature, and each needs a test.

New Words

New words on this page
WordMeaning
KickA write of 1 to SERVICE: the count starts again.
Key sequenceKEY 0x51, then KEY 0xA2 on the next watchdog write: it unlocks one write to CONTROL.
TickOne step of the watchdog's count: one in 32 cycles of clk_aon, 448 ns.
WindowThe ticks in which a kick is accepted: with 3 or fewer left. A kick with more left is too early.
Early warningInterrupt source 6: raised on the tick that finds 1 left, or by a kick too early.
BiteThe watchdog's reset, two ticks after the warning.

The Smallest Example: Counting Ticks

Armed, the watchdog has 6 ticks left. Each tick takes one. A kick with 3 or fewer left starts again at 6; a kick with more left raises the warning. The tick that finds 1 left raises the warning, and the reset follows two ticks later. Kicks come at ticks 4, 8 and 12, then stop. When does the warning come, and the reset?

The count, tick by tick
TickEventLeft after
0armed6
1 to 3ticks5, 4, 3
4a kick with 3 left: inside the window; then the tick5
8, 12the same5
13 to 16ticks4, 3, 2, 1
17finds 1 left: early warning0
19two ticks later: reset request

In the example each kick comes just before that tick.

The example below runs the watchdog's own logic, with a tick every four clocks, and prints exactly these ticks.

The Rules

  1. Rule 1: Arm it with its key, and check that a wrong key does not.

    The key protects against a program that writes at random. A watchdog armed by any write protects against nothing.

  2. Rule 2: Kick in the middle of the window, by a time base you own.

    The watchdog cannot be read. Use mtime. A tick is about 45 cycles of clk_sys. The window runs from 3 to 5 ticks after an arm or a kick. This lab kicked every 160 cycles of mtime.

  3. Rule 3: Test both edges of the window.

    A kick too early must warn; no kick must warn and then reset. A test of the middle alone proves neither edge.

  4. Rule 4: See the warning without reading the watchdog.

    Its registers are write-only. The warning is a pending bit of ap_plic, source 6, which a test can poll.

  5. Rule 5: Check the record and what survives.

    After the bite RESET_CAUSE has bit 2; scratch registers and SRAM keep their contents; the watchdog itself is disarmed. Every warm reset disarms it, not only its own.

  6. Rule 6: Prove the disarm before you let it bite.

    A watchdog whose reset does not clear it keeps asking for a reset for ever, and the chip never leaves reset. Test that a pin reset disarms it first, while the test can still report.

Sources for These Rules

The key, the window, the warning and the reset: watchdog_supervisor.sv, used unchanged inside ap_watchdog.sv (facts 22 and 27.9).

The tick: one in 32 cycles of clk_aon (ap_watchdog.sv, TICK_DIV); write-only registers and full-word writes only (ap_watchdog.sv; facts 27.9).

Every warm reset resets the watchdog: ap_crg.sv (resetn_wdg is the bus reset request); facts 27.11.

dev_mode is tied to 0 on v1.5, so the watchdog counts through a debug halt: facts 27.11.

The Lab: One Bite, Measured

The firmware gives source 6 a priority, so its warning shows as a pending bit. It arms the watchdog and writes event 0x71. It kicks three times, 160 cycles of mtime apart, writing event 0x72 after each. Then it stops kicking and polls the pending bit; when the warning appears it writes event 0x73.

The events and the console line of the next boot, with the bench's cycle numbers.
EVENT 279: 00000071
EVENT 483: 00000072
EVENT 651: 00000072
EVENT 798: 00000072
EVENT 1042: 00000073
CONSOLE 1542: cause 00000005

Rule 2 at work: the kicks landed inside the window, so no warning came while they continued. The events show 168 and 147 cycles between kicks. Each kick waits for mtime to pass a target 160 cycles after the last one. The polling loop and the slow-bus write move each event by a few cycles.

Rule 4 at work: the program saw the warning in ap_plic's pending bits 244 cycles after the last kick. The bench's sample window then saw resetn_sys fall at cycle 1119, 77 cycles later. Two ticks are about 90 cycles. The program saw the warning a little after it rose, through two flops into clk_sys and its own polling loop.

Rule 5 at work: the next boot read RESET_CAUSE 0x05, power-on and the watchdog, and found its note in TB_ARG. It then ended at once, so this run does not show the disarm. Proving it, in the right order, is the job of problem V9.7.1.

Why It Is Built This Way

Why a window and not just a deadline? A program stuck in a short loop that happens to include the kick would kick for ever and never be reset. A kick that comes too early is a sign of that loop, so the watchdog treats it as a failure.

Why does every warm reset disarm the watchdog? Boot code may need to set things up before it can kick. A watchdog left armed through a pin reset could bite during that boot, and the chip would reset in a loop.

Traps

The first trap is a watchdog test that only waits for the bite. It proves the reset path, and nothing about the key, the window or the warning. A chip that bites at the wrong time, or warns too late to save state, passes it.

The second is the order of the steps. Suppose the test lets it bite first, then checks that a pin reset disarms it. A watchdog that ignores warm resets keeps asking for a reset after its first bite. The chip never leaves reset, and the run ends at the cycle limit.

The third is debugging with a halted core. ap_soc v1.5 ties dev_mode to 0, so the watchdog keeps counting while a debugger holds the core. A developer who stops at a breakpoint for a second sees the chip reset under them.

At Scale

ap_soc~12 blocks
Phone-class SoChundreds of blocks

This section looks ahead. ap_soc has one watchdog with one window.

  1. Many watchdogs

    Large chips have one per core or cluster, plus a system watchdog. Each is tested, and the order in which they bite.

  2. Windowed and question-answer watchdogs

    Safety chips use watchdogs that ask a question and expect a computed answer, so a stuck program cannot fake the kick.

  3. Safety standards

    For cars and machines, the watchdog's tests become evidence in the safety case: every failure mode, injected and caught.

Try It

Run it: the watchdog armed, three kicks, then the warning and the reset.

Count the ticks from the last kick to the warning. Why five?

Move the second kick to tick 6. What does the watchdog do with it?

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: armed: 6 left tick 4: kick, 6 left tick 8: kick, 6 left tick 12: kick, 6 left tick 17: early warning tick 19: reset request

Check Yourself

Predict the outputKicks now come at ticks 4 and 6. What is the last line printed?
// The watchdog's own logic: content/modules/v3/soc/ap_soc/watchdog_supervisor.sv, inside ap_watchdog unchanged.
// Here one tick comes every four clocks (on the chip: one in 32 cycles of clk_aon). Kicks at ticks 4, 6.
// ap_soc teaching policy. Input tick is an enable from the supervision clock.
// A privileged boot writer may arm once. Reset request persists until reset.
module watchdog_supervisor (
  input logic clk, reset_n, tick, debug_halt, dev_mode, privileged,
  input logic psel, penable, pwrite,
  input logic [11:0] offset,
  input logic [31:0] pwdata,
  output logic armed, irq, reset_req,
  output logic [3:0] remaining
);
  localparam KEY = 0, CONTROL = 4, SERVICE = 8;
  localparam RELOAD = 6, OPEN = 3;
  logic first_key, unlocked;
  logic [1:0] grace;
  wire write_word = psel && penable && pwrite;
  wire arm = write_word && privileged && unlocked && offset == CONTROL && pwdata == 1;
  wire kick = write_word && offset == SERVICE && pwdata == 1;

  always_ff @(posedge clk) begin
    if (!reset_n) begin
      first_key <= 0;
      unlocked <= 0;
      armed <= 0;
      remaining <= 0;
      irq <= 0;
      reset_req <= 0;
      grace <= 0;
    end else begin
      if (write_word) begin
        first_key <= privileged && offset == KEY && pwdata == 'h51;
        unlocked <= privileged && first_key && offset == KEY && pwdata == 'ha2;
      end
      if (!armed && arm) begin
        armed <= 1;
        remaining <= RELOAD;
      end else if (armed && !reset_req && !(dev_mode && debug_halt)) begin
        if (irq) begin
          if (tick && grace == 1) reset_req <= 1;
          else if (tick) grace <= grace - 1'b1;
        end else if ((kick && remaining > OPEN) || (tick && remaining == 1 && !kick)) begin
          irq <= 1;
          grace <= 2;
          remaining <= 0;
        end else if (kick) remaining <= RELOAD;
        else if (tick) remaining <= remaining - 1'b1;
      end
    end
  end
endmodule

module tb;
  logic clk = 0, reset_n = 0, tick = 0, psel = 0, penable = 0, pwrite = 0;
  logic [11:0] offset; logic [31:0] pwdata;
  wire armed, irq, reset_req; wire [3:0] remaining;
  int ticks = 0;
  bit was_irq = 0;
  watchdog_supervisor dut(.clk(clk), .reset_n(reset_n), .tick(tick), .debug_halt(1'b0), .dev_mode(1'b0), .privileged(1'b1),
    .psel(psel), .penable(penable), .pwrite(pwrite), .offset(offset), .pwdata(pwdata),
    .armed(armed), .irq(irq), .reset_req(reset_req), .remaining(remaining));
  always #5 clk = ~clk;
  task automatic write(input [11:0] a, input [31:0] v);
    @(negedge clk); psel = 1; penable = 1; pwrite = 1; offset = a; pwdata = v;
    @(negedge clk); psel = 0; penable = 0; pwrite = 0;
  endtask
  function automatic bit kick_now(input int t);
    return t == 4 || t == 6;
  endfunction
  initial begin
    @(negedge clk); reset_n = 1;
    write(0, 'h51); write(0, 'ha2); write(4, 1);
    $display("armed: %0d left", remaining);
    for (int t = 1; t <= 24 && !reset_req; t++) begin
      repeat (3) @(negedge clk);
      if (kick_now(t)) begin write(8, 1); $display("tick %0d: kick, %0d left", t, remaining); end
      tick = 1; @(negedge clk); tick = 0; ticks = t;
      if (irq && !was_irq) $display("tick %0d: early warning", t);
      was_irq = irq;
    end
    if (reset_req) $display("tick %0d: reset request", ticks);
    else $display("no reset by tick %0d", ticks);
    $finish;
  end
endmodule
ConceptWhy does ap_soc's watchdog need a two-step key to arm?
ConceptHow did the lab see the early warning?
ConceptWhat does a pin reset do to an armed watchdog on ap_soc?
ConceptWhy must a test prove the disarm before letting the watchdog bite?

Common Mistakes

Wrong Test the watchdog only by letting it bite.

The key, the window and the warning stay untested.

Fix Test a kick in the window, a kick too early, no kick, and the warning before the bite.

Wrong Kick by counting loop iterations.

Loop timing changes with the compiler; the window is a few ticks wide.

Fix Kick by mtime, aiming at the middle of the window.

Wrong Let the watchdog bite before proving a warm reset disarms it.

A watchdog that ignores warm resets then holds the chip in reset for ever, and nothing reports.

Fix Prove the disarm first, while the test can still report.

Remember

  • The watchdog arms with a two-step key, accepts a kick only inside its window, warns, then resets two ticks later.
  • Firmware kicks by its own time base, mtime, because the watchdog cannot be read.
  • After the bite RESET_CAUSE has bit 2, scratch and SRAM survive, and the watchdog is disarmed; every warm reset disarms it.
  • Order a watchdog test so every failure can still be reported: prove the disarm before the bite.

Practice

Report a problem with this page