A Test for Every Reset Source

A chip can be reset from five places. Each is its own path from a request to a reset to a record. Test each one: did the reset happen, how far did it reach, and did the chip write down why?

You Will Be Able To

  • List the reset sources of a chip and what each one reaches.
  • Write a firmware test that survives its own resets and checks the cause after each one.
  • Say what a warm reset leaves behind, in the chip and in a C program.
  • Raise a reset at a chosen moment from the bench, including in the middle of a line of output.
Lesson
V9.1
Time
26 min
Runs on
Your browser
Check
5 questions

A product restarts in the field, and RESET_CAUSE says "watchdog". That is true only if someone tested that the watchdog, and nothing else, sets that bit.

Five Reset Sources and What Each ReachesFive sources enter ap_crg, which records the cause and sets the reach of each reset.
  • Clock / reset
  • Pads
  • Peripheral
  • Debug
  • Processor
  • Bus
  • Reset
Five Reset Sources and What Each ReachesFive sources enter ap_crg, which records the cause and sets the reach of each reset. clock reset Power-on cell supply comes up pads RST_N pin two samples peripheral Watchdog no kick in time peripheral SOFT_RESET firmware asks debug Debug request tied low on v1.5 clock reset ap_crg RESET_CAUSE,reach processor Core every source bus Bus, peripherals,watchdog not the debugrequest bit 0bit 1bit 2bit 3bit 4resetn_coreresetn_sys
  1. clock reset Power-on cellsupply comes up
    • bit 0 → ap_crg
  2. pads RST_N pintwo samples
    • bit 1 → ap_crg
  3. peripheral Watchdogno kick in time
    • bit 2 → ap_crg
  4. peripheral SOFT_RESETfirmware asks
    • bit 3 → ap_crg
  5. debug Debug requesttied low on v1.5
    • bit 4 → ap_crg
  6. clock reset ap_crgRESET_CAUSE, reach
    • resetn_core → Core
    • resetn_sys → Bus, peripherals, watchdog
  7. processor Coreevery source
  8. bus Bus, peripherals, watchdognot the debug request
  1. Five ways in

    Power, the pin, the watchdog, software and the debugger can each reset the chip.

  2. One record

    ap_crg sets a bit of RESET_CAUSE for each source. Power-on starts a new record.

  3. Different reach

    Pin, watchdog and software reset the core and the bus. The debug request resets the core alone.

  4. What only power reaches

    ap_pwrctl, the slow bus, the scratch registers, the debug module, its TAP and the sampled BOOT strap are reset by power-on only.

Read the diagram as text
  • Power-on cell: supply comes up
  • RST_N pin: two samples
  • Watchdog: no kick in time
  • SOFT_RESET: firmware asks
  • Debug request: tied low on v1.5
  • ap_crg: RESET_CAUSE, reach
  • Core: every source
  • Bus, peripherals, watchdog: not the debug request
  • Power-on cell → ap_crg: bit 0
  • RST_N pin → ap_crg: bit 1
  • Watchdog → ap_crg: bit 2
  • SOFT_RESET → ap_crg: bit 3
  • Debug request → ap_crg: bit 4
  • ap_crg → Core: resetn_core
  • ap_crg → Bus, peripherals, watchdog: resetn_sys

ap_crg.sv and the lab block reset_sources.sv it uses unchanged (facts 10, 27.3, 27.11). The debug module of v1.5 asks for no reset: its request is tied low, and only the bench can raise it.

Before You Start

Read Clocks, Resets and Straps From the Outside and The Mailbox first. How a reset controller is built is taught in Reset Sources and the Reset Cause Register; this page tests it.

Five questions this page answers
Question
1Why does each reset source need its own test?
2How does a firmware test survive the reset it asks for?
3What does RESET_CAUSE read after several resets in a row?
4Which parts of the chip does each source reset?
5What does a warm reset leave in SRAM and in a C program's variables?

The Idea: One Path per Source

A reset is not one thing. The power-on cell, the pin, the watchdog, a software request and the debugger each start a reset through their own logic. Each sets its own bit of the record, and each reaches its own set of blocks. A bug can sit in any one of the five paths and leave the other four working.

So a chip-level reset test asks three questions of every source. Did the reset happen at all? Did it reach exactly what the specification says, no less and no more? Did the chip record the right cause? A fourth question is about software: what is left after the reset, for the program that runs next?

The test has a strange shape. It is reset in the middle by its own request, and it must carry on afterwards. The chip gives it a place to keep its step: four scratch registers that only power-on clears.

New Words

New words on this page
WordMeaning
Reset sourceSomething that can start a reset: power-on, the RST_N pin, the watchdog, SOFT_RESET, the debug module.
Cold resetThe power-on reset: everything starts again, the record of causes too.
Warm resetAny other reset: power stays on, so some state survives it.
RESET_CAUSEThe register of ap_sysctl that records which sources have reset the chip since power-on.
StickyA bit that stays set until something clears it on purpose. Each cause bit is sticky until the next power-on.
ReachThe set of blocks a reset source puts into reset.
Scratch registerA register with no meaning to the chip. TB_STATUS to TB_ARG are cleared by power-on only, so a test can keep its step there.

The Smallest Example: A Record That Grows

RESET_CAUSE starts at 0x01 after power-on: bit 0 is set. Then a pin reset sets bit 1, a software reset bit 3, the watchdog bit 2. Work out the word after each step before you read on.

The record, step by step
AfterBits setRESET_CAUSE
power-on00x01
a pin reset0, 10x03
a software reset0, 1, 30x0B
a watchdog reset0, 1, 2, 30x0F
power-on again00x01

The word only grows, until power-on starts it again. A test that compares the whole word with a single bit fails on the second reset. A test that ignores the word misses a source that records nothing.

The Rules

  1. Rule 1: Raise every source at least once.

    Each source is its own logic from request to reset. A test of the pin says nothing about the watchdog.

  2. Rule 2: Keep the step where only power-on clears it.

    A scratch register survives a warm reset. A C variable in .bss does not: start-up code clears it on every boot.

  3. Rule 3: Check the record after every reset.

    Compare the bits you are sure of exactly. Accept a bit another agent may add, such as a debug reset, and print the word.

  4. Rule 4: Check the reach, not only the restart.

    A pin reset must reset UART0's registers; a debug reset must not. Read a block that should and one that should not have been reset.

  5. Rule 5: Give every wait for a reset a limit.

    A source that never resets the chip leaves the test waiting for ever. A limit turns that into a fail code.

  6. Rule 6: Raise sources at awkward moments too.

    The bench can reset the chip at any cycle: in the middle of a line, a bus transfer or a DMA copy. The next lesson does that on purpose.

Sources for These Rules

The sources, the record and the reach: reset_sources.sv (content/modules/v3/soc/ap_soc). ap_crg.sv uses it unchanged (facts 10 and 27.11).

The scratch registers and their reset: facts 27.3 ("cleared by the power-on reset only").

The bench's knobs: tools/soc/ap_soc/bench/soc_tb/README.md, "Interconnect and boot experiments (E19)": +reset_<source>=<cycle>, +count_resets. The debug request is tied low at the chip boundary, so the bench drives ap_crg's synchronizer input.

Start-up code clears .bss and copies .data only when the two addresses differ: tools/soc/fwkit/crt0.S and the kit README.

Debug resets: RV-DBG 3.2 (reset control) and 3.14.2 (dmcontrol fields hartreset and ndmreset). mcause after reset: RV-PRIV 3.4; ap_rv32_csr.sv resets it to 0.

The Lab: Five Sources on the Chip

One image, built with the firmware kit, runs on the site's chip bench. At each boot it prints its step (kept in TB_ARG), RESET_CAUSE, a variable with a first value (.data) and one without (.bss). Then it asks for the next reset. The pin comes by TB_EVENT 2, software by SOFT_RESET bit 1. The watchdog is armed and never kicked.

The console and the events of the run, with the bench's cycle numbers.
CONSOLE 982: step 00000000 cause 00000001 data 00000100 bss 00000000
EVENT 1009: 00000002
CONSOLE 1999: step 00000001 cause 00000003 data 00000101 bss 00000000
CONSOLE 3013: step 00000002 cause 0000000b data 00000102 bss 00000000
CONSOLE 4400: step 00000003 cause 0000000f data 00000103 bss 00000000

Rules 1 and 3 at work: four boots, and the record grows 0x01, 0x03, 0x0B, 0x0F. The bench counted one reset of each kind: pin 1, software 1, watchdog 1, and the power-on of the start, 1.

Rule 2 at work: the step in TB_ARG went 0, 1, 2, 3 through three warm resets. The .data variable went 0x100, 0x101, 0x102, 0x103: SRAM kept its words. In this layout .data already sits where the program runs, so start-up code copies nothing over it. The .bss variable read 0 at every boot, although the program wrote 7 each time.

Now the bench raises the two sources firmware cannot. Be clear about the first one. On ap_soc v1.5 the debug module never asks for a reset. Its request is tied low, so no program and no debugger can raise it. The bench forces the input of ap_crg's synchronizer instead. That tests the path a later chip would use, not anything v1.5 can do in the field. Here it comes 200 cycles after the core first left reset:

The same image, with +reset_debug=200.
CONSOLE 1215: step 00000000 cause 00000011 data 00000100 bss 00000000
CONSOLE 2233: step 00000001 cause 00000013 data 00000101 bss 00000000
CONSOLE 3247: step 00000002 cause 0000001b data 00000102 bss 00000000
CONSOLE 4635: step 00000003 cause 0000001f data 00000103 bss 00000000

It struck before the first line was printed, so the core simply started main again. RESET_CAUSE read 0x11 at that boot and kept bit 4 for good: 0x1F at the end. The bench counted reset_debug = 1. By the RTL the bus was not reset: a debug reset reaches the core alone. Problem V9.2.2 shows a DMA job running straight through one.

Then a power failure 300 cycles in, in the middle of the first line of output:

The same image, with +reset_por=300: the first two console lines.
CONSOLE 1284: step step 00000000 cause 00000001 data 00000100 bss 00000000
CONSOLE 2302: step 00000001 cause 00000003 data 00000101 bss 00000000

The first line was cut after "step " and the program started again from power-on. RESET_CAUSE read 0x01 again: power-on starts a new record. A half line in a log is the mark of a reset that struck while firmware was talking.

Why It Is Built This Way

Why is the record sticky? Because the program that reads it runs after the reset, sometimes after several. A record that held only the last cause would lose the first one when a second reset followed. Power-on clears it, because a new supply is a new story.

Why do the scratch registers survive warm resets? So a test, or a product's boot code, can carry a note across them: a step number, a crash address. Real chips have such registers for exactly that reason.

Why does the debug request reach only the core? A debugger restarts the program but keeps its own session and the chip's state for inspection. So the debug module and the TAP are reset by power-on only, and the bus keeps running. The RISC-V debug specification names two such resets: one of the selected cores alone, and one of everything except the debug logic. ap_soc's request is the first kind. V9.6 shows what it costs.

Why a register outside the core at all? RISC-V lets a core report a reset's cause in mcause, but only as a value the chip defines. The core of ap_soc always resets mcause to 0, the value for a core that does not tell causes apart. So on this chip only RESET_CAUSE says why.

Traps

The first trap is the test that checks only the restart. It asks for a software reset, sees main again, and passes. A chip whose software reset records the debug bit instead passes it too. The field engineer who reads that register later is told the wrong story.

The second is a step kept in a C variable. A variable with no first value, or a first value of 0, lives in .bss. Start-up code clears it at every boot. The test starts again from step 0 after every reset and never ends.

The third is a simulation artifact. ap_sram.sv has no reset input, so in simulation SRAM keeps its words even through a power-on reset. A real SRAM loses them when the supply goes. A test must not lean on SRAM after power-on, even though the bench lets it.

At Scale

ap_soc~12 blocks
Phone-class SoChundreds of blocks

This section looks ahead. ap_soc has five sources and one record.

  1. More sources

    Large chips add brown-out detectors, clock monitors, thermal limits, lockstep compare errors and security alarms. Each is one more path to test.

  2. A reach matrix

    The plan lists every source against every reset domain, with the expected effect in each cell. Each cell is a check.

  3. Reset in every state

    Each source is raised in each power mode and during boot, not only while main runs.

Try It

Run it: the record after each source, and which resets each source raised.

Find the line where "bus and I/O" is 0. Which source is that, and why?

Change the list to put "por" in the middle. What happens to the bits before it?

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: power-on cause=00001 reached: core 1, bus and I/O 1, debug 1 pin cause=00011 reached: core 1, bus and I/O 1, debug 0 software cause=01011 reached: core 1, bus and I/O 1, debug 0 watchdog cause=01111 reached: core 1, bus and I/O 1, debug 0 debug cause=11111 reached: core 1, bus and I/O 0, debug 0

Check Yourself

Predict the outputThe slice now raises the watchdog, then power-on, then software. What is the last line printed?
// The record of reset causes: content/modules/v3/soc/ap_soc/reset_sources.sv, the block ap_crg uses unchanged.
// Each source is raised in turn; the record and what the source reached are printed after each.
// ap_soc lesson slice. The four warm requests are in the always-on clock domain.
// pin_sample is the pad level AFTER synchronization. POR starts a new log.
module reset_sources (
  input logic clk, por, pin_sample, watchdog, software_req, debug_req,
  output logic core_reset, io_reset, debug_reset,
  output logic [4:0] cause
);
  logic previous_pin;
  logic pin_qualified;
  assign io_reset = por || pin_qualified || watchdog || software_req;
  assign core_reset = io_reset || debug_req;
  assign debug_reset = por;

  always_ff @(posedge clk) begin
    if (por) begin
      previous_pin <= 0;
      pin_qualified <= 0;
      cause <= 5'b00001;
    end else begin
      previous_pin <= pin_sample;
      pin_qualified <= previous_pin && pin_sample;
      if (previous_pin && pin_sample) cause[1] <= 1;
      if (watchdog)      cause[2] <= 1;
      if (software_req)  cause[3] <= 1;
      if (debug_req)     cause[4] <= 1;
    end
  end
endmodule

module tb;
  logic clk = 0, por = 1, pin = 0, wd = 0, sw = 0, dbg = 0;
  wire core_reset, io_reset, debug_reset;
  wire [4:0] cause;
  bit saw_core, saw_io, saw_debug;
  reset_sources dut(.clk(clk), .por(por), .pin_sample(pin), .watchdog(wd), .software_req(sw), .debug_req(dbg),
                    .core_reset(core_reset), .io_reset(io_reset), .debug_reset(debug_reset), .cause(cause));
  always #7 clk = ~clk;
  always @(posedge clk) begin saw_core |= core_reset; saw_io |= io_reset; saw_debug |= debug_reset; end
  task automatic show(input string name);
    @(posedge clk); #1;
    $display("%-9s cause=%b reached: core %0d, bus and I/O %0d, debug %0d", name, cause, saw_core, saw_io, saw_debug);
    saw_core = 0; saw_io = 0; saw_debug = 0;
  endtask
  initial begin
    @(posedge clk); @(posedge clk); #1;
    show("power-on");
    por = 0;
    @(posedge clk); #1;
    wd = 1; @(posedge clk); #1 wd = 0;
    show("watchdog");
    por = 1; @(posedge clk); #1 por = 0;
    show("por");
    sw = 1; @(posedge clk); #1 sw = 0;
    show("software");
    $finish;
  end
endmodule
ConceptWhy does the lab keep its step in TB_ARG?
ConceptIn the lab, what did the debug reset reach?
ConceptWhat does RESET_CAUSE read after power-on, a pin reset and a second power-on?
ConceptA software reset restarts the chip but sets no bit. Which test catches it?

Common Mistakes

Wrong Pass a reset test once main runs again.

The reset happened, but its record and its reach are untested.

Fix Check RESET_CAUSE and one block inside and one outside the reach after every reset.

Wrong Keep the test's step in a C variable.

A .bss variable is cleared by start-up code at every boot, so the test loops for ever.

Fix Keep it in a scratch register that only power-on clears.

Wrong Compare RESET_CAUSE with the bit of the newest source.

The record is sticky: earlier bits are still set.

Fix Compare with the bits of every source so far, and accept bits another agent may add.

Remember

  • Each reset source is its own path: request, reset, reach and record. Test all five.
  • RESET_CAUSE is sticky: bits gather until power-on starts a new record.
  • A debug reset reaches the core only; pin, watchdog and software reach the bus too; power-on reaches everything.
  • A test survives its own resets by keeping its step in a register only power-on clears.

Practice

Report a problem with this page