Capstone Step: The Watchdog Joins Bring-Up

Four steps pass. Now arm the guard that resets the chip when firmware stops. It must see a real failure, and leave a note about it.

You Will Be Able To

  • Explain why the watchdog is armed last, and arm it with its key sequence.
  • Place and pace the kick so that it proves that the main program is alive.
  • Use the early warning to record where a program failed, for the next boot.
Lesson
S6.18
Time
25 min
Runs on
Your browser
Check
6 questions

A field unit resets itself about once a day. The log after each boot says only "started". Nobody knows what failed, because the reset wiped the evidence.

Before You Start

You need The Watchdog: the counter, the kick, the window, the early warning and the reset. This page does not teach them again.

You need Firmware Bring-Up: steps 1 to 4, and the claim of ap_plic. mtime comes from Timers.

Five questions this page answers
Question
1Why is the watchdog armed last in bring-up?
2How is the watchdog of ap_soc armed, and can firmware disarm it?
3Where should the kick be, and how often?
4What happens, tick by tick, when the main program fails?
5What can firmware learn from a watchdog reset?

The Idea: The Last Step Guards the Others

A watchdog resets the chip when software stops kicking it. During bring-up that is a nuisance. A slow or failing step would be cut short by a reset, and the evidence would vanish.

So the watchdog is armed last, as step 5. It joins only when steps 1 to 4 pass. From then on, it must catch a real failure of the main program.

Two things make that work. The kick must come from the main loop, paced by time, so that only a living main can kick. And the early warning must be used to write down what went wrong, before the reset.

New Words

The three registers of the watchdog
RegisterAddressWhat a write does
KEY0x1000_30000x51, then 0xA2 on the next watchdog write: unlocks the next register once
CONTROL0x1000_30041, while unlocked: arms the watchdog
SERVICE0x1000_30081: the kick

The watchdog of ap_soc, from watchdog_supervisor.sv. It has no register to read.

New words on this page
WordMeaning
Armed watchdogA watchdog that has been started. In ap_soc it is armed once, and then only a reset stops it.
Key sequenceTwo fixed values written to the KEY register, one right after the other. Only then does a write to CONTROL arm the watchdog.
Supervision tickOne step of the watchdog counter. In the lab there is one in every 32 ticks of mtime.
PacingWaiting for a set time before an action, so that it happens at a steady rate.
Failure pathThe code that runs when a check fails. Here it is a loop that never ends, like "while (1);" in C.
Last wordsA small record that the warning handler leaves in SRAM: the source, and the address where the program was.

One Picture: Step 5

Step 5: Arm, Kick, Warn, ResetStep through the picture. It follows the watchdog through a healthy run, then through a failure of main.
  • Control / FSM
  • Clock / reset
  • Bus
  • Processor
  • Memory
  • Debug
Step 5: Arm, Kick, Warn, ResetStep through the picture. It follows the watchdog through a healthy run, then through a failure of main. component Main loop one job, then a kickwhen its time comes component Watchdog loads 6 at a kick;loses 1 at eachsupervision tick component ap_plic the early warning issource 6 component Warning handler claims 6 and readsmepc component SRAM keeps its wordsthrough a reset component Reset request two supervision ticksafter the warning keys, then CONTROLa kick every 128ticksearly warningexternal requestlast wordsreset request
  1. component Main loopone job, then a kick when its time comes
    • keys, then CONTROL → Watchdog
    • a kick every 128 ticks → Watchdog
  2. component Watchdogloads 6 at a kick; loses 1 at each supervision tick
    • early warning → ap_plic
    • reset request → Reset request
  3. component ap_plicthe early warning is source 6
    • external request → Warning handler
  4. component Warning handlerclaims 6 and reads mepc
    • last words → SRAM
  5. component SRAMkeeps its words through a reset
  6. component Reset requesttwo supervision ticks after the warning
  1. Arm, last

    Steps 1 to 4 have passed. Main writes two keys in a row, then 1 to CONTROL. From now on nothing can disarm the watchdog.

  2. Kick, on time

    After each job, main waits for the time of the next kick and kicks. 128 ticks are four supervision ticks, inside the window.

  3. Main fails

    Job 3 fails, and main stays in a loop for ever. No kick comes. The watchdog loses 1 at every supervision tick.

  4. The warning

    The tick that finds 1 left raises the early warning. It is source 6, so ap_plic interrupts the processor.

  5. Last words

    The handler stores the source and mepc, the address where main was stuck, in SRAM. Then it waits.

  6. The reset

    Two supervision ticks after the warning, the watchdog asks for a reset. SRAM keeps the last words for the next boot.

Read the diagram as text
  • Main loop: one job, then a kick when its time comes
  • Watchdog: loads 6 at a kick; loses 1 at each supervision tick
  • ap_plic: the early warning is source 6
  • Warning handler: claims 6 and reads mepc
  • SRAM: keeps its words through a reset
  • Reset request: two supervision ticks after the warning
  • Main loop → Watchdog: keys, then CONTROL
  • Main loop → Watchdog: a kick every 128 ticks
  • Watchdog → ap_plic: early warning
  • ap_plic → Warning handler: external request
  • Warning handler → SRAM: last words
  • Watchdog → Reset request: reset request

Bring-up step 5 on ap_soc. The lab runs both stories: the healthy run, and job 3 failing.

The first two steps of the picture are the healthy run. The last four are what the watchdog is for.

The Smallest Example, Worked by Hand

The watchdog loads 6 at a kick. Each supervision tick takes 1 away. A kick is early while more than 3 are left. The tick that finds 1 left raises the warning.

Main kicks every 128 ticks of mtime. That is 128 / 32 = 4 supervision ticks. So each kick finds 6 - 4 = 2 left: inside the window.

Now job 3 fails right after a kick. Count the supervision ticks after that last kick.

After the last kick, tick by tick
Supervision tickLeft after itWhat happens
15nothing
24nothing
33a kick would now be on time; none comes
42nothing
51nothing
6-it finds 1 left: the early warning
7-one tick of grace
8-the reset request

So the warning comes six supervision ticks after the last kick: at most 6 × 32 = 192 ticks of mtime, and more than 5 × 32 = 160. The reset request follows 2 × 32 = 64 ticks later.

The Rules

  1. Rule 1: Arm the watchdog last.

    Only after steps 1 to 4 pass. A reset during a slow step hides the fault you are looking for.

  2. Rule 2: Arm it with the key sequence.

    In ap_soc: 0x51 and then 0xA2 to KEY, with no other watchdog write between, then 1 to CONTROL.

  3. Rule 3: Kick from the main loop, after its work.

    Never from a timer handler. A timer keeps running when main has failed.

  4. Rule 4: Pace the kicks by time, inside the window.

    In the lab: every 3 to 5 supervision ticks. The lab uses 4.

  5. Rule 5: Use the warning for last words.

    Record the source and mepc in SRAM, then wait for the reset.

  6. Rule 6: Read the last words at the next boot.

    Keep them where start-up code does not clear, and read the reset cause too.

Sources for These Rules

The key values, the window of 3, the grace of 2 and the arm-once rule come from watchdog_supervisor.sv. The address 0x1000_3000 is in map.json. These are our own choices.

The SRAM model of ap_soc, ap_sram.sv, has no reset input, so its words survive a watchdog reset. The reset cause register of ap_sysctl marks a watchdog reset in bit 2.

Rules 1, 3, 5 and 6 are common practice. No source in our library lists them.

Now Run It

The lab at the end of this page is step 5. The watchdog of ap_soc, ap_plic and the ap_clint timer are the chip files, unchanged. The processor is the model of S6.16.

Recorded from the lab on Icarus: the healthy run, stopped after 1200 ticks.
t=26 | watchdog armed, 6 ticks left
t=158 | watchdog kick, 2 ticks were left
t=286 | watchdog kick, 2 ticks were left
t=414 | watchdog kick, 2 ticks were left
t=542 | watchdog kick, 2 ticks were left
t=670 | watchdog kick, 2 ticks were left
t=798 | watchdog kick, 2 ticks were left
t=926 | watchdog kick, 2 ticks were left
t=1054 | watchdog kick, 2 ticks were left
t=1182 | watchdog kick, 2 ticks were left
still running after 1200 instructions, 0 exceptions and 0 interrupts
jobs=10 kicks=9 | armed=1 warning=0 reset=0

Rules 2 and 4 at work. The watchdog arms with 6 left. Every kick finds 2 left, as worked out by hand. No warning, no reset.

Now the same program, with job 3 failing. Main enters its failure path, a loop that never ends.

Recorded from the lab with job 3 failing. It ends at the reset request.
t=26 | watchdog armed, 6 ticks left
t=158 | watchdog kick, 2 ticks were left
t=286 | watchdog kick, 2 ticks were left
t=448 | watchdog early warning: source 6 asks ap_plic
t=449 | interrupt, external: mcause=8000000b mepc=000000a8, next is 000000ac
t=449 | PLIC claim returns 6
t=512 | watchdog reset request: the chip resets here
jobs=3 kicks=2 | last words: source 6 at 000000a8 | armed=1 warning=1 reset=1

The last kick is at 286. The warning comes at 448, which is 162 ticks later: between 160 and 192, as the hand table said. The reset request follows 64 ticks after the warning.

Rule 5 at work. The handler claims source 6, and stores mepc. mepc is 000000a8, the address of the failure path. The next boot can read exactly where main was stuck.

A probe ran these programs on the real five-stage ap_rv32, with the same chip files behind its bus. The final results were equal to the model.

From the probe: the healthy run, the failing run and the run of the trap below. "end" says how each run ended.
uart= gpio=0000 ram= end=time exceptions=0 armed=1 warning=0 reset=0
uart= gpio=0000 ram=00000003,00000002,000000a8,00000006 end=reset exceptions=0 armed=1 warning=1 reset=1
uart= gpio=0000 ram=00000003 end=time exceptions=0 armed=1 warning=0 reset=0

This lab leaves things out. The reset itself is not modelled: the run stops at the request. The next boot, which reads the last words, is not run.

Why It Is Built This Way

Design choiceThe alternativeWhat the choice buys, and costs
A key sequence before armingOne write armsA stray write cannot arm it. Firmware writes three words, in order.
Armed until resetSoftware may disarm itA failed program cannot switch off its own guard. So bring-up must arm it last.
An early warning before the resetReset at onceA handler gets two ticks to leave last words. The watchdog needs an interrupt line.
A window for the kickAny kick countsA loop stuck on the kick itself is caught. Firmware must pace its kicks.

The Trap: A Watchdog That Never Barks

Back to the field unit. Say the team moves the kick into the timer handler, to be safe. The resets stop. Here is that program, with the same failure of job 3.

The first rows of the run with the kick in a timer handler.
t=33 | watchdog armed, 6 ticks left
t=147 | interrupt, machine timer: mcause=80000007 mepc=000000a4, next is 000000b4
t=155 | watchdog kick, 3 ticks were left
t=159 | mret: back to 000000a4
t=275 | interrupt, machine timer: mcause=80000007 mepc=000000a4, next is 000000b4
Its last line, after 1200 ticks.
jobs=3 kicks=9 | armed=1 warning=0 reset=0

Three jobs, then main is stuck for good. Yet the kicks go on, nine of them, and there is no warning. The timer handler proves only that the timer works.

The unit no longer resets. It hangs instead, and nobody is told. Put the kick back in the main loop, after the work, as rule 3 says.

At Scale

This page1 loop, 1 watchdog
A productmany tasks, 1 watchdog

This section looks ahead. A product has more than one loop to watch, and a debugger on the bench.

  1. Many tasks, one kick

    Each task sets a flag when it completes its work. One place kicks only when every flag is set, then clears them all.

  2. The debugger

    A core stopped by a debugger cannot kick. In ap_soc the watchdog then pauses only in development mode. Otherwise a long stop ends in a reset.

  3. A section that start-up skips

    Last words need a section that start-up code does not clear. The linker script of S6.17 can place one.

  4. Seconds, not ticks

    A real watchdog counts in milliseconds or seconds, on a clock that keeps running when the main clock fails.

Try It

Run the lab. Count the supervision ticks between two kicks, from the times after "t=".

Change both rows that add 128 to x21 so that they add 64. Predict the line of the first kick, then run.

Put 128 back. Delete the kick: the store of x5 to offset 8 of x9. Predict when the warning comes.

Put the kick back. Write 0xa3 as the second key. Predict whether the watchdog arms.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: t=26 | watchdog armed, 6 ticks left t=158 | watchdog kick, 2 ticks were left t=286 | watchdog kick, 2 ticks were left t=414 | watchdog kick, 2 ticks were left t=542 | watchdog kick, 2 ticks were left t=670 | watchdog kick, 2 ticks were left t=798 | watchdog kick, 2 ticks were left t=926 | watchdog kick, 2 ticks were left t=1054 | watchdog kick, 2 ticks were left t=1182 | watchdog kick, 2 ticks were left still running after 1200 instructions, 0 exceptions and 0 interrupts jobs=10 kicks=9 | armed=1 warning=0 reset=0
Read included file: ap_plic.sv
// One machine context, synchronous level sources. Register-side interface:
// commit pulses once at a successful word transfer's data edge.
module ap_plic #(parameter integer SOURCES = 16) (
  input logic clk, reset_n, commit, write_en,
  input logic [21:0] offset,
  input logic [31:0] wdata,
  input logic [SOURCES:1] level_irq,
  output logic [31:0] rdata,
  output logic notify_irq
);
  localparam ENABLE = 'h2000, THRESHOLD = 'h200000, CLAIM = 'h200004;
  logic [2:0] weight [1:SOURCES];
  logic [2:0] threshold, best;
  logic [SOURCES:1] pending, busy, enabled;
  integer winner;

  always_comb begin
    best = 0;
    winner = 0;
    for (int id = 1; id <= SOURCES; id++) begin
      if (pending[id] && enabled[id] && weight[id] > best) begin
        best = weight[id];
        winner = id;
      end
    end
    notify_irq = best > threshold;
    rdata = 0;
    for (int id = 1; id <= SOURCES; id++) begin
      if (offset == 4*id) rdata = weight[id];
      if (offset == 'h1000 + 4*(id/32)) rdata[id%32] = pending[id];
      if (offset == ENABLE + 4*(id/32)) rdata[id%32] = enabled[id];
    end
    case (offset)
      THRESHOLD: rdata = threshold;
      CLAIM: rdata = winner;
    endcase
  end

  always_ff @(posedge clk) begin
    if (!reset_n) begin
      pending <= '0;
      busy <= '0;
      enabled <= '0;
      threshold <= 0;
      for (int id = 1; id <= SOURCES; id++) weight[id] <= 0;
    end else begin
      for (int id = 1; id <= SOURCES; id++) begin
        if (level_irq[id] && !busy[id]) begin
          pending[id] <= 1;
          busy[id] <= 1;
        end
        if (commit && write_en && offset == 4*id) weight[id] <= wdata[2:0];
        if (commit && write_en && offset == ENABLE + 4*(id/32))
          enabled[id] <= wdata[id%32];
        if (commit && offset == CLAIM) begin
          if (!write_en && winner == id) pending[id] <= 0;
          if (write_en && enabled[id] && wdata == id && busy[id] && !pending[id]) busy[id] <= 0;
        end
      end
      if (commit && write_en && offset == THRESHOLD) threshold <= wdata[2:0];
    end
  end
endmodule
Read included file: ap_clint_timer.sv
// Timer slice of ap_soc's local interrupt block. One hart, one timebase tick
// per clk and a registered interrupt output. Word strobes arrive at commitment.
module ap_clint_timer #(parameter logic [63:0] RESET_COMPARE = '1) (
  input logic clk, reset_n, write_cmp_low, write_cmp_high,
  input logic [31:0] wdata,
  output logic [63:0] mtime, mtimecmp,
  output logic mtip
);
  always_ff @(posedge clk) begin
    if (!reset_n) begin
      mtime <= 0;
      mtimecmp <= RESET_COMPARE;
      mtip <= 0;
    end else begin
      mtime <= mtime + 1'b1;
      if (write_cmp_low) mtimecmp[31:0] <= wdata;
      if (write_cmp_high) mtimecmp[63:32] <= wdata;
      mtip <= mtime >= mtimecmp;
    end
  end
endmodule
Read included file: watchdog_supervisor.sv
// 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

Check Yourself

Predict the outputMain kicks at once after arming, with no wait. What is the last line the lab prints?
// Step 5, with the kick at once after arming.
// The program is the task at the top. Each row is one word of the Boot ROM: i(kind, a, b, c).
// The comment beside a row is its address and the same instruction in assembly.
// The numbers after "t=" are ticks of this model: one instruction for each tick, and one tick of mtime.
// The watchdog takes one supervision tick in every 32 ticks of mtime.
module tb;
  task automatic the_program();
    i(LUI,   2,  0,  'h80010);   // 000: lui x2, 0x80010      the stack pointer
    i(ADDI,  5,  0,  156);       // 004: addi x5, x0, 156
    i(CSRW,  5,  0,  MTVEC);     // 008: csrw mtvec, x5      mtvec: the handler
    i(LUI,   8,  0,  'h80000);   // 00c: lui x8, 0x80000      x8: SRAM
    i(SW,    0,  8,  0);         // 010: sw x0, 0(x8)      jobs = 0
    i(SW,    0,  8,  4);         // 014: sw x0, 4(x8)      kicks = 0
    i(LUI,   20, 0,  'h200c);    // 018: lui x20, 0x200c      x20: mtime is at -8 from here
    // the early warning of the watchdog is source 6 of ap_plic
    i(LUI,   18, 0,  'hc000);    // 01c: lui x18, 0xc000      x18: ap_plic
    i(ADDI,  5,  0,  1);         // 020: addi x5, x0, 1
    i(SW,    5,  18, 24);        // 024: sw x5, 24(x18)      priority of source 6 (watchdog) = 1
    i(LUI,   19, 0,  'hc002);    // 028: lui x19, 0xc002
    i(ADDI,  5,  0,  64);        // 02c: addi x5, x0, 64
    i(SW,    5,  19, 0);         // 030: sw x5, 0(x19)      enable bit 6
    i(LUI,   22, 0,  'hc200);    // 034: lui x22, 0xc200      x22: threshold, and claim at offset 4
    i(LUI,   5,  0,  'h1);       // 038: lui x5, 0x1
    i(ADDI,  5,  5,  -2048);     // 03c: addi x5, x5, -2048      x5 = 0x800
    i(CSRS,  5,  0,  MIE);       // 040: csrs mie, x5      mie: the external enable, bit 11
    i(ADDI,  5,  0,  8);         // 044: addi x5, x0, 8
    i(CSRS,  5,  0,  MSTATUS);   // 048: csrs mstatus, x5      mstatus: MIE
    // arm the watchdog: two keys in a row, then CONTROL
    i(LUI,   9,  0,  'h10003);   // 04c: lui x9, 0x10003      x9: the watchdog
    i(ADDI,  5,  0,  81);        // 050: addi x5, x0, 81
    i(SW,    5,  9,  0);         // 054: sw x5, 0(x9)      KEY: the first key
    i(ADDI,  5,  0,  162);       // 058: addi x5, x0, 162
    i(SW,    5,  9,  0);         // 05c: sw x5, 0(x9)      KEY: the second key
    i(ADDI,  5,  0,  1);         // 060: addi x5, x0, 1
    i(SW,    5,  9,  4);         // 064: sw x5, 4(x9)      CONTROL = 1: armed, and nothing can disarm it
    i(LW,    21, 20, -8);        // 068: lw x21, -8(x20)      mtime
    i(ADDI,  21, 21, 128);       // 06c: addi x21, x21, 128      x21: the time of the next kick
    // main: one job, then wait for the time of the kick, then kick
    i(LW,    6,  8,  0);         // 070: lw x6, 0(x8)
    i(ADDI,  6,  6,  1);         // 074: addi x6, x6, 1
    i(SW,    6,  8,  0);         // 078: sw x6, 0(x8)      jobs = jobs + 1
    i(ADDI,  5,  0,  1);         // 07c: addi x5, x0, 1
    i(SW,    5,  9,  8);         // 080: sw x5, 8(x9)      SERVICE = 1: the kick
    i(LW,    6,  8,  4);         // 084: lw x6, 4(x8)
    i(ADDI,  6,  6,  1);         // 088: addi x6, x6, 1
    i(SW,    6,  8,  4);         // 08c: sw x6, 4(x8)      kicks = kicks + 1
    i(ADDI,  21, 21, 128);       // 090: addi x21, x21, 128
    i(JAL,   0,  0,  'h70);      // 094: jal x0, 0x70
    i(JAL,   0,  0,  'h98);      // 098: jal x0, 0x98      the failure path: stay here, as "while (1);" does
    // the handler
    i(LW,    28, 22, 4);         // 09c: lw x28, 4(x22)      claim: which source?
    i(SW,    28, 8,  12);        // 0a0: sw x28, 12(x8)      last words: the source
    i(CSRR,  29, 0,  MEPC);      // 0a4: csrr x29, mepc      mepc: where main was
    i(SW,    29, 8,  8);         // 0a8: sw x29, 8(x8)      last words: the address
    i(JAL,   0,  0,  'hac);      // 0ac: jal x0, 0xac      wait here for the reset
  endtask

  // ---- From here down: a small model of the processor and of three devices, as test code. ----
  // It is a model beside the real core ap_rv32, not a part of it. It runs one instruction for each clock tick.
  localparam int LUI = 0, ADDI = 1, ANDI = 2, ORI = 3, SLLI = 4, SRLI = 5, ADD = 6, SUB = 7, AND = 8, OR = 9, LW = 10, SW = 11,
                 BEQ = 12, BNE = 13, BLT = 14, BGE = 15, JAL = 16, JALR = 17, CSRR = 18, CSRW = 19, CSRS = 20, CSRC = 21, MRET = 22, WORD = 23;
  localparam int MSTATUS = 'h300, MIE = 'h304, MTVEC = 'h305, MSCRATCH = 'h340, MEPC = 'h341, MCAUSE = 'h342, MIP = 'h344;
  int kind [0:1023], fa [0:1023], fb [0:1023];   // the Boot ROM: one row for each word
  logic [31:0] fc [0:1023];
  int here = 0;                                   // the address of the next row
  task automatic at(input int address);           // the rows that follow start at this address
    here = address;
  endtask
  task automatic i(input int k, input int a, input int b, input logic [31:0] c);   // add one row
    if (here < 4096) begin kind[here >> 2] = k; fa[here >> 2] = a; fb[here >> 2] = b; fc[here >> 2] = c; end
    here += 4;
  endtask
  logic        clk = 0;
  logic [31:0] x [0:31];             // the 32 registers
  logic [31:0] pc;                   // the address of the next instruction
  logic [31:0] ram [0:16383];        // SRAM: 64 KiB from 8000_0000. Nothing is in it at power-on
  logic [31:0] mstatus, mie, mtvec, mscratch, mepc, mcause;   // the machine CSRs of this model
  int          steps, traps, taken;  // instructions, exceptions, interrupts
  bit          stopped;
  localparam int SHOW = 1;            // 2: print every instruction. 1: print events only. 0: print nothing
  task automatic say(input string text);
    if (SHOW != 0) $display("t=%0d | %s", ticks, text);
  endtask
  // ---- UART0 (test code). TXDATA 00, STATUS 04 (bit 0 ROOM, bit 1 RX_READY), RXDATA 0c ----
  string       uart_text = "";                 // every byte that was sent, as text
  int          uart_wait = 0;                  // after a byte: 12 ticks with no room (a lab number)
  logic [7:0]  rx_queue [0:3];                 // received bytes that software has not read yet
  int          rx_count = 0;
  logic [7:0]  rx_plan [0:7];                  // the bytes the board will send, and at which tick
  int          rx_tick [0:7], rx_planned = 0;
  wire         rx_ready = (rx_count != 0);     // UART0 asks for an interrupt while a received byte waits
  // ---- GPIO (test code). OUT 08, TGL 14 ----
  logic [15:0] gpio_out = 0;
  int          gpio_changes = 0;
  // ---- DMA (test code). SRC 00, DST 04, LEN 08, START 0c. It moves one byte of SRAM in each tick. ----
  logic [31:0] dma_src = 0, dma_dst = 0, dma_len = 0;
  int          dma_left = 0;
  bit          dma_done = 0;                   // its interrupt request: 1 from the last byte until the next start
  int          ticks = 0;

  function automatic logic [31:0] tc_peek(input logic [31:0] ad);
    case (ad)
      32'h1000_0004: return {30'b0, rx_ready, (uart_wait == 0)};
      32'h1000_000c: return {24'b0, rx_queue[0]};
      32'h1000_1008: return {16'b0, gpio_out};
      default:       return 32'h0;
    endcase
  endfunction
  function automatic bit tc_exists(input bit wr, input logic [31:0] ad);   // 1: a register is at this address
    case (ad)
      32'h1000_0000, 32'h1000_1014, 32'h1000_6000, 32'h1000_6004, 32'h1000_6008, 32'h1000_600c: return wr;
      32'h1000_0004, 32'h1000_000c: return !wr;
      32'h1000_1008: return 1;
      default: return 0;
    endcase
  endfunction
  task automatic tc_read(input logic [31:0] ad);
    if (ad == 32'h1000_000c && rx_count != 0) begin        // reading RXDATA removes the oldest byte
      for (int n = 0; n < 3; n++) rx_queue[n] = rx_queue[n + 1];
      rx_count--;
    end
  endtask
  task automatic tc_write(input logic [31:0] ad, input logic [31:0] wd);
    case (ad)
      32'h1000_0000: if (uart_wait == 0) begin             // a byte written with no room is lost
        uart_text = {uart_text, $sformatf("%c", wd[7:0])};  uart_wait = 12;
        say($sformatf("UART0 sends %02h \"%c\"", wd[7:0], wd[7:0]));
      end else say($sformatf("UART0 has no room: %02h is lost", wd[7:0]));
      32'h1000_1008: begin gpio_out = wd[15:0];  gpio_changes++;  say($sformatf("GPIO OUT = %04h", gpio_out)); end
      32'h1000_1014: begin gpio_out = gpio_out ^ wd[15:0];  gpio_changes++;  say($sformatf("GPIO OUT = %04h", gpio_out)); end
      32'h1000_6000: dma_src = wd;
      32'h1000_6004: dma_dst = wd;
      32'h1000_6008: dma_len = wd;
      32'h1000_600c: if (wd[0]) begin
        dma_left = dma_len;  dma_done = 0;
        say($sformatf("DMA starts: %0d bytes from %08h to %08h", dma_len, dma_src, dma_dst));
      end
      default: ;
    endcase
  endtask
  task automatic tc_tick();
    ticks++;
    if (uart_wait != 0) uart_wait--;
    for (int n = 0; n < rx_planned; n++) if (rx_tick[n] == ticks && rx_count < 4) begin
      rx_queue[rx_count] = rx_plan[n];  rx_count++;
      say($sformatf("UART0 receives %02h \"%c\"", rx_plan[n], rx_plan[n]));
    end
    if (dma_left != 0) begin                               // one byte: read it from SRAM, write it to SRAM
      ram[dma_dst[15:2]][8 * dma_dst[1:0] +: 8] = ram[dma_src[15:2]][8 * dma_src[1:0] +: 8];
      dma_src++;  dma_dst++;  dma_left--;
      if (dma_left == 0) begin dma_done = 1;  say("DMA done"); end
    end
  endtask
  task automatic board_sends(input int tick, input logic [7:0] value);   // plan one received byte
    rx_plan[rx_planned] = value;  rx_tick[rx_planned] = tick;  rx_planned++;
  endtask
  // Interrupt sources of ap_soc: 1 UART0, 2 GPIO, 3 timer and PWM, 4 SPI, 5 DMA, 6 watchdog, 7 to 16 spare.
  wire         wdg_warn;                       // the early warning of the watchdog; tied to 0 where a lab has none
  wire [16:1] sources = {10'b0, wdg_warn, dma_done, 3'b0, rx_ready};
  // ---- The two chip blocks of ap_soc, unchanged: the interrupt controller and the machine timer ----
  logic        reset_n = 0, plic_commit = 0, plic_write = 0, cmp_low = 0, cmp_high = 0;
  logic [21:0] plic_offset = 0;
  logic [31:0] dev_wdata = 0;
  wire  [31:0] plic_rdata;
  wire  [63:0] mtime, mtimecmp;
  wire         irq_external, irq_timer;
  ap_plic plic (.clk, .reset_n, .commit(plic_commit), .write_en(plic_write), .offset(plic_offset), .wdata(dev_wdata),
                .level_irq(sources), .rdata(plic_rdata), .notify_irq(irq_external));
  ap_clint_timer clint (.clk, .reset_n, .write_cmp_low(cmp_low), .write_cmp_high(cmp_high), .wdata(dev_wdata),
                        .mtime, .mtimecmp, .mtip(irq_timer));
  logic        wdg_write;                      // one write to the watchdog commits at the next rising edge
  logic [11:0] wdg_offset;
  wire         wdg_armed, wdg_reset;
  wire  [3:0]  wdg_left;                       // ticks left before the warning
  wire         wdg_tick = (mtime[4:0] == 5'd31);   // the supervision tick: one in every 32 clock ticks (a lab number)
  watchdog_supervisor wdg (.clk(clk), .reset_n(reset_n), .tick(wdg_tick), .debug_halt(1'b0), .dev_mode(1'b0), .privileged(1'b1),
                           .psel(wdg_write), .penable(wdg_write), .pwrite(wdg_write), .offset(wdg_offset), .pwdata(dev_wdata),
                           .armed(wdg_armed), .irq(wdg_warn), .reset_req(wdg_reset), .remaining(wdg_left));
  bit          wdg_was_armed, wdg_was_warned;
  wire [31:0] mip = {20'b0, irq_external, 3'b0, irq_timer, 7'b0};   // bit 11: external. bit 7: machine timer

  function automatic string cn(input logic [11:0] a);   // the name of a CSR
    case (a)
      MSTATUS: return "mstatus";  MIE: return "mie";  MTVEC: return "mtvec";  MSCRATCH: return "mscratch";
      MEPC: return "mepc";  MCAUSE: return "mcause";  MIP: return "mip";
      default: return "?";
    endcase
  endfunction
  function automatic logic [31:0] csr_value(input logic [11:0] a);
    case (a)
      MSTATUS: return mstatus;  MIE: return mie;  MTVEC: return mtvec;  MSCRATCH: return mscratch;
      MEPC: return mepc;  MCAUSE: return mcause;
      default: return mip;
    endcase
  endfunction
  task automatic csr_store(input logic [11:0] a, input logic [31:0] v);   // each CSR keeps only its writable bits
    case (a)
      MSTATUS:  begin mstatus[3] = v[3];  mstatus[7] = v[7]; end           // MIE and MPIE
      MIE:      mie = v & 32'h0000_0888;                                   // external, timer and software enables
      MTVEC:    mtvec = v & 32'hffff_fffd;
      MSCRATCH: mscratch = v;
      MEPC:     mepc = v & 32'hffff_fffc;
      MCAUSE:   mcause = v;
      default: ;                                                           // mip is read-only
    endcase
  endtask
  // The entry of a trap, for an exception or an interrupt: what the processor does by itself.
  task automatic enter(input logic [31:0] cause, input string text);
    mepc = pc & 32'hffff_fffc;  mcause = cause;
    mstatus[7] = mstatus[3];  mstatus[3] = 1'b0;                           // MPIE takes MIE, MIE closes
    say($sformatf("%s: mcause=%08h mepc=%08h, next is %08h", text, cause, mepc, mtvec & 32'hffff_fffc));
    pc = mtvec & 32'hffff_fffc;
  endtask
  // Start one read or write of a word. The value of a read is in bus_rdata after half a tick.
  logic [31:0] bus_rdata;
  bit          bus_fault;
  task automatic bus(input bit wr, input logic [31:0] ad, input logic [31:0] wd);
    bus_fault = 0;  bus_rdata = 0;
    if (ad[31:12] == 20'h0) begin                                          // Boot ROM: data words can be read
      if (wr) bus_fault = 1;
      else bus_rdata = (kind[ad[11:2]] == WORD) ? fc[ad[11:2]] : 32'h0;
    end
    else if (ad[31:16] == 16'h8000) begin                                  // SRAM
      if (wr) ram[ad[15:2]] = wd;
      else bus_rdata = ram[ad[15:2]];
    end
    else if (ad[31:22] == 10'h030) begin                                   // ap_plic, from 0c00_0000
      plic_offset = ad[21:0];  plic_write = wr;  dev_wdata = wd;  plic_commit = 1;
    end
    else if (ad == 32'h0200_4000 || ad == 32'h0200_4004) begin              // ap_clint: mtimecmp
      if (wr) begin dev_wdata = wd;  cmp_low = !ad[2];  cmp_high = ad[2]; end
      else bus_rdata = ad[2] ? mtimecmp[63:32] : mtimecmp[31:0];
    end
    else if (ad == 32'h0200_bff8 || ad == 32'h0200_bffc) begin              // ap_clint: mtime, read only here
      if (wr) bus_fault = 1;
      else bus_rdata = ad[2] ? mtime[63:32] : mtime[31:0];
    end
    else if (tc_exists(wr, ad)) begin                                      // UART0, GPIO, DMA
      if (wr) tc_write(ad, wd);
      else begin bus_rdata = tc_peek(ad);  tc_read(ad); end
    end
    else if (wr && (ad == 32'h1000_3000 || ad == 32'h1000_3004 || ad == 32'h1000_3008)) begin   // the watchdog: writes only
      wdg_offset = ad[11:0];  dev_wdata = wd;  wdg_write = 1;
      if (ad[3:0] == 4'h8) say($sformatf("watchdog kick, %0d ticks were left", wdg_left));
    end
    else bus_fault = 1;                                                    // nothing answers at this address
  endtask
  // One tick: one instruction, then one rising clock edge.
  task automatic step();
    int k, a, b;
    logic [31:0] c, r1, r2, res, next, ad;
    string text, effect;
    bit writes, take, done, access;
    // An interrupt is taken between two instructions: MIE at 1, and a request that is pending and enabled.
    if (mstatus[3] && (mie & mip) != 0) begin
      if (mie[11] && mip[11]) enter(32'h8000_000b, "interrupt, external");
      else                    enter(32'h8000_0007, "interrupt, machine timer");
      taken++;
    end
    done = 0;  writes = 1;  access = 0;  res = 0;  effect = "";  next = pc + 4;  text = "";
    if (pc >= 32'h1000) begin enter(1, "exception, no instruction at this address");  traps++;  done = 1;  k = WORD;  a = 0;  b = 0;  c = 0; end
    else begin k = kind[pc >> 2];  a = fa[pc >> 2];  b = fb[pc >> 2];  c = fc[pc >> 2]; end
    r1 = x[b[4:0]];  r2 = x[c[4:0]];
    // An immediate has 12 bits with a sign (20 bits in lui). A row outside that range is no instruction.
    if (!done && ((k == LUI && c[31:20] != 0) || ((k == ADDI || k == ANDI || k == ORI || k == LW || k == SW || k == JALR) && !($signed(c) >= -2048 && $signed(c) <= 2047)))) begin
      enter(2, $sformatf("exception, immediate out of range at %08h", pc));  traps++;  done = 1;
    end
    if (!done) case (k)
      LUI:  begin res = c << 12;  text = $sformatf("lui x%0d, 0x%0h", a, c); end
      ADDI: begin res = r1 + c;          text = $sformatf("addi x%0d, x%0d, %0d", a, b, $signed(c)); end
      ANDI: begin res = r1 & c;          text = $sformatf("andi x%0d, x%0d, %0d", a, b, $signed(c)); end
      ORI:  begin res = r1 | c;          text = $sformatf("ori x%0d, x%0d, %0d", a, b, $signed(c)); end
      SLLI: begin res = r1 << c[4:0];    text = $sformatf("slli x%0d, x%0d, %0d", a, b, c); end
      SRLI: begin res = r1 >> c[4:0];    text = $sformatf("srli x%0d, x%0d, %0d", a, b, c); end
      ADD:  begin res = r1 + r2;         text = $sformatf("add x%0d, x%0d, x%0d", a, b, c[4:0]); end
      SUB:  begin res = r1 - r2;         text = $sformatf("sub x%0d, x%0d, x%0d", a, b, c[4:0]); end
      AND:  begin res = r1 & r2;         text = $sformatf("and x%0d, x%0d, x%0d", a, b, c[4:0]); end
      OR:   begin res = r1 | r2;         text = $sformatf("or x%0d, x%0d, x%0d", a, b, c[4:0]); end
      LW, SW: begin
        ad = r1 + c;  access = 1;
        text = $sformatf("%s x%0d, %0d(x%0d)", (k == LW) ? "lw" : "sw", a, $signed(c), b);
        bus(k == SW, ad, x[a[4:0]]);
        if (k == SW) begin writes = 0;  effect = $sformatf("mem[%08h] = %08h", ad, x[a[4:0]]); end
      end
      BEQ, BNE, BLT, BGE: begin
        r1 = x[a[4:0]];  r2 = x[b[4:0]];  writes = 0;
        case (k)
          BEQ: take = (r1 == r2);
          BNE: take = (r1 != r2);
          BLT: take = ($signed(r1) < $signed(r2));
          default: take = ($signed(r1) >= $signed(r2));
        endcase
        text = $sformatf("%s x%0d, x%0d, 0x%0h", (k == BEQ) ? "beq" : (k == BNE) ? "bne" : (k == BLT) ? "blt" : "bge", a, b, c);
        if (take) begin next = c;  effect = $sformatf("taken: next is %08h", next); end
        else effect = "not taken";
      end
      JAL, JALR: begin
        res = pc + 4;
        if (k == JAL) begin next = c;  text = $sformatf("jal x%0d, 0x%0h", a, c); end
        else begin next = (r1 + c) & ~32'd1;  text = $sformatf("jalr x%0d, %0d(x%0d)", a, $signed(c), b); end
        if (k == JAL && next == pc && !wdg_armed) begin
          if (SHOW == 2) $display("%08h | %s | the program stops here", pc, text);
          else say($sformatf("the program stops at %08h", pc));
          stopped = 1;  done = 1;
        end
      end
      CSRR: begin res = csr_value(c[11:0]);  text = $sformatf("csrr x%0d, %s", a, cn(c[11:0])); end
      CSRW, CSRS, CSRC: begin
        writes = 0;
        case (k)
          CSRW:    csr_store(c[11:0], x[a[4:0]]);
          CSRS:    csr_store(c[11:0], csr_value(c[11:0]) | x[a[4:0]]);
          default: csr_store(c[11:0], csr_value(c[11:0]) & ~x[a[4:0]]);
        endcase
        text = $sformatf("%s %s, x%0d", (k == CSRW) ? "csrw" : (k == CSRS) ? "csrs" : "csrc", cn(c[11:0]), a);
        effect = $sformatf("%s = %08h", cn(c[11:0]), csr_value(c[11:0]));
      end
      MRET: begin
        writes = 0;  next = mepc;  text = "mret";
        mstatus[3] = mstatus[7];  mstatus[7] = 1'b1;                       // MIE takes MPIE back
        effect = $sformatf("next is %08h, MIE=%0d", next, mstatus[3]);
        if (SHOW == 1) say($sformatf("mret: back to %08h", next));
      end
      default: begin enter(2, $sformatf("exception, illegal instruction %08h at %08h", c, pc));  traps++;  done = 1; end
    endcase
    #1;                                                                    // half a tick: the devices answer
    if (access && !done) begin
      if (plic_commit && !plic_write) begin
        bus_rdata = plic_rdata;
        if (plic_offset == 22'h20_0004) say($sformatf("PLIC claim returns %0d", plic_rdata));
      end
      if (plic_commit && plic_write && plic_offset == 22'h20_0004) say($sformatf("PLIC complete %0d", dev_wdata));
      if (bus_fault) begin
        enter((k == LW) ? 5 : 7, $sformatf("exception, no memory at %08h for %s at %08h", ad, (k == LW) ? "a load" : "a store", pc));
        traps++;  done = 1;
      end
      else if (k == LW) begin res = bus_rdata;  effect = $sformatf(" from mem[%08h]", ad); end
    end
    if (!done) begin
      if (writes) begin
        if (a[4:0] != 0) x[a[4:0]] = res;                                  // register 0 is never written
        effect = {$sformatf("x%0d = %08h", a, x[a[4:0]]), effect};
        if (k == JAL || k == JALR) effect = {effect, $sformatf(", next is %08h", next)};
      end
      if (SHOW == 2) $display("%08h | %s | %s", pc, text, effect);
      pc = next;  steps++;
    end
    tc_tick();
    clk = 1;  #1;  clk = 0;                                                // the rising edge: registers of the chip blocks load
    plic_commit = 0;  plic_write = 0;  cmp_low = 0;  cmp_high = 0;
    wdg_write = 0;
    if (wdg_armed && !wdg_was_armed) say($sformatf("watchdog armed, %0d ticks left", wdg_left));
    if (wdg_warn && !wdg_was_warned) say("watchdog early warning: source 6 asks ap_plic");
    if (wdg_reset) say("watchdog reset request: the chip resets here");
    wdg_was_armed = wdg_armed;  wdg_was_warned = wdg_warn;
  endtask
  task automatic power_on();
    for (int n = 0; n < 32; n++) x[n] = 0;
    for (int n = 0; n < 16384; n++) ram[n] = 'x;                           // SRAM holds nothing known
    mstatus = 32'h0000_1800;  mie = 0;  mtvec = 0;  mscratch = 0;  mepc = 0;  mcause = 0;   // the values after reset
    pc = 0;  steps = 0;  traps = 0;  taken = 0;  stopped = 0;
    uart_text = "";  uart_wait = 0;  rx_count = 0;  rx_planned = 0;  gpio_out = 0;  gpio_changes = 0;
    dma_left = 0;  dma_done = 0;  ticks = 0;
    reset_n = 0;  #1 clk = 1;  #1 clk = 0;  reset_n = 1;                   // one clock edge in reset for the chip blocks
    wdg_write = 0;  wdg_offset = 0;  wdg_was_armed = 0;  wdg_was_warned = 0;
  endtask
  task automatic run();
    while (!stopped && !wdg_reset && steps + traps + taken < 4000) step();
  endtask
  initial begin
    for (int n = 0; n < 1024; n++) begin kind[n] = WORD; fa[n] = 0; fb[n] = 0; fc[n] = 0; end   // empty ROM reads as 0
    the_program();
    power_on();
    run();
    if (!stopped && !wdg_reset) $display("still running after %0d instructions, %0d exceptions and %0d interrupts", steps, traps, taken);
    $display("warning=%0d reset=%0d", wdg_warn, wdg_reset);
    $finish;
  end
endmodule
Read included file: ap_plic.sv
// One machine context, synchronous level sources. Register-side interface:
// commit pulses once at a successful word transfer's data edge.
module ap_plic #(parameter integer SOURCES = 16) (
  input logic clk, reset_n, commit, write_en,
  input logic [21:0] offset,
  input logic [31:0] wdata,
  input logic [SOURCES:1] level_irq,
  output logic [31:0] rdata,
  output logic notify_irq
);
  localparam ENABLE = 'h2000, THRESHOLD = 'h200000, CLAIM = 'h200004;
  logic [2:0] weight [1:SOURCES];
  logic [2:0] threshold, best;
  logic [SOURCES:1] pending, busy, enabled;
  integer winner;

  always_comb begin
    best = 0;
    winner = 0;
    for (int id = 1; id <= SOURCES; id++) begin
      if (pending[id] && enabled[id] && weight[id] > best) begin
        best = weight[id];
        winner = id;
      end
    end
    notify_irq = best > threshold;
    rdata = 0;
    for (int id = 1; id <= SOURCES; id++) begin
      if (offset == 4*id) rdata = weight[id];
      if (offset == 'h1000 + 4*(id/32)) rdata[id%32] = pending[id];
      if (offset == ENABLE + 4*(id/32)) rdata[id%32] = enabled[id];
    end
    case (offset)
      THRESHOLD: rdata = threshold;
      CLAIM: rdata = winner;
    endcase
  end

  always_ff @(posedge clk) begin
    if (!reset_n) begin
      pending <= '0;
      busy <= '0;
      enabled <= '0;
      threshold <= 0;
      for (int id = 1; id <= SOURCES; id++) weight[id] <= 0;
    end else begin
      for (int id = 1; id <= SOURCES; id++) begin
        if (level_irq[id] && !busy[id]) begin
          pending[id] <= 1;
          busy[id] <= 1;
        end
        if (commit && write_en && offset == 4*id) weight[id] <= wdata[2:0];
        if (commit && write_en && offset == ENABLE + 4*(id/32))
          enabled[id] <= wdata[id%32];
        if (commit && offset == CLAIM) begin
          if (!write_en && winner == id) pending[id] <= 0;
          if (write_en && enabled[id] && wdata == id && busy[id] && !pending[id]) busy[id] <= 0;
        end
      end
      if (commit && write_en && offset == THRESHOLD) threshold <= wdata[2:0];
    end
  end
endmodule
Read included file: ap_clint_timer.sv
// Timer slice of ap_soc's local interrupt block. One hart, one timebase tick
// per clk and a registered interrupt output. Word strobes arrive at commitment.
module ap_clint_timer #(parameter logic [63:0] RESET_COMPARE = '1) (
  input logic clk, reset_n, write_cmp_low, write_cmp_high,
  input logic [31:0] wdata,
  output logic [63:0] mtime, mtimecmp,
  output logic mtip
);
  always_ff @(posedge clk) begin
    if (!reset_n) begin
      mtime <= 0;
      mtimecmp <= RESET_COMPARE;
      mtip <= 0;
    end else begin
      mtime <= mtime + 1'b1;
      if (write_cmp_low) mtimecmp[31:0] <= wdata;
      if (write_cmp_high) mtimecmp[63:32] <= wdata;
      mtip <= mtime >= mtimecmp;
    end
  end
endmodule
Read included file: watchdog_supervisor.sv
// 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
ConceptWhy is the watchdog armed last in bring-up?
ConceptCan firmware disarm the watchdog of ap_soc once it is armed?
ConceptWhere should the kick be?
ConceptMain fails right after a kick. When does the early warning come?
ConceptWhy can the next boot read the last words?

Common Mistakes

Wrong Arm the watchdog in the first bring-up program.

Every slow or failing step is cut short by a reset, and its evidence is lost.

Fix Arm it last, once steps 1 to 4 pass.

Wrong Kick from a timer handler.

The timer keeps running when main has failed, so the watchdog never sees the failure.

Fix Kick from the main loop, after the work of a pass.

Wrong Kick as often as possible.

A kick with more than 3 left is early, and raises the warning.

Fix Pace the kicks by time, inside the window.

Remember

  • Arm the watchdog last in bring-up, with its key sequence. In ap_soc only a reset stops it.
  • Kick from the main loop, after its work, paced by time inside the window.
  • A kick from a timer handler hides a failure of main.
  • The warning comes on the sixth supervision tick without a kick; the reset request two ticks later.
  • Use the warning to leave last words in SRAM for the next boot.

Practice

Report a problem with this page