set_retention: Elements, Controls and Retention Supplies

Your core can lose every live bit and still recover its accumulator. Follow the byte into its shadow and back.

You Will Be Able To

  • Choose the registers and supply that must survive a power cut.
  • Predict which write survives a level save or an edge save.
  • Refine a retention strategy and distinguish control failures from loss of the keeper rail.
Lesson
W4.9
Time
34 min
Runs on
Your browser
Check
6 questions
Where Does Acc Go While UNIT Sleeps?Follow the saved byte.
  • Clock / reset
  • Storage
  • Data
  • Power
Where Does acc Go While UNIT Sleeps?Follow the saved byte. clock reset d and clk ordinary writes storage u_core/acc working byte on UNIT storage Saved acc storage on KEEP component store save control component recall_n restore control component VKEEP retention power clocked datasaverestoresave windowrestore windowindependent power
  1. clock reset d and clkordinary writes
    • clocked data → u_core/acc
  2. storage u_core/accworking byte on UNIT
    • save → Saved acc
  3. storage Saved accstorage on KEEP
    • restore → u_core/acc
  4. component storesave control
    • save window → Saved acc
  5. component VKEEPretention power
    • independent power → Saved acc
  6. component recall_nrestore control
    • restore window → u_core/acc
  1. Before UPF

    acc follows writes. Cutting UNIT destroys the only working copy in this model.

  2. Select acc

    SNAP adds a saved byte for acc. The other registers have no saved copy.

  3. Close the window

    When store falls, the current acc enters storage on KEEP.

  4. Return the byte

    With power back, low recall_n holds acc at the saved byte.

Read the diagram as text
  • d and clk: ordinary writes
  • u_core/acc: working byte on UNIT
  • Saved acc: storage on KEEP
  • store: save control
  • recall_n: restore control
  • VKEEP: retention power
  • d and clk → u_core/acc: clocked data
  • u_core/acc → Saved acc: save
  • Saved acc → u_core/acc: restore
  • store → Saved acc: save window
  • recall_n → u_core/acc: restore window
  • VKEEP → Saved acc: independent power

Drawn retention paths. Follow the values in the trace to test the drawing.

When the packet engine sleeps, you can discard its scratch data but need its running total.

In sleep_soc, the bench writes a distinctive total to acc, cuts UNIT, then writes zero at restart.

Recorded without SNAP

recall acc=00 tag=ff scratch=01

The restart write survives.

Recorded with SNAP

recall acc=2c tag=ff scratch=01

The saved byte replaces the restart write.

With identical writes, what makes their recall rows different?

Select the State That Must Survive

One clock writes acc, tag and scratch in sleep_soc. Only acc must survive sleep.

Because each packet supplies its own tag, the old tag can go.

On recall, acc returns to 2c while tag stays ff and scratch stays 01.

Select PD_UNIT with -domain, then narrow its state with -elements (IEEE 1801-2024 6.53).

The lab strategy. Find the register path.
set_retention SNAP -domain PD_UNIT -elements {u_core/acc} -retention_supply KEEP -save_signal {store high} -restore_signal {recall_n low}
SelectionWhat the tool searchesWhat changes at wake
u_core/accOne named byte in PD_UNITOnly the accumulator has a saved copy.
u_coreState inside the selected instance, within the domainHere all three bytes become candidates.
Omit -elementsEligible state across the selected domainHere the domain contains just u_core.

Three scopes for the same circuit. Here the last two coincide.

Recorded after selecting the instance.
  set_retention SNAP (answer.upf:22): u_core.acc, u_core.scratch, u_core.tag; save store high, restore recall_n low; supply KEEP
Recorded after omitting the element list.
  set_retention SNAP (answer.upf:22): u_core.acc, u_core.scratch, u_core.tag; save store high, restore recall_n low; supply KEEP

What powers acc’s saved copy during sleep?

Power the Saved Copy

When UNIT goes off, the asleep row shows acc=xx. How did recall recover 2c?

SNAP uses KEEP, with power from VKEEP and ground from VSS.

One supply set

create_supply_set KEEP -function {power KEEP_N} -function {ground GND_N}

SNAP uses this pair through -retention_supply KEEP.

Legacy net form

-retention_power_net KEEP_N
-retention_ground_net GND_N

The same power and return can be named separately.

Recorded with the legacy net pair on this circuit.
  set_retention SNAP (answer.upf:22): u_core.acc; save store high, restore recall_n low; supply {KEEP_N GND_N}

IEEE 1801-2024 uses -retention_supply for the set (6.53). OpenPA also accepts the older spelling -retention_supply_set used in W4.9.1.

If you omit the retention supply, verification assumes an always-on supply. Implementation needs an explicit supply in the power intent (6.53).

Recorded run. Only VKEEP falls in this experiment. UNIT stays live.
rail live acc=61 tag=9e scratch=62
OPENPA-CHECK D6 @ 42000: set_retention SNAP: retention supply KEEP lost while holding saved state
rail lost acc=xx tag=9e scratch=62

The rail lost row changes acc from 61 to xx while tag stays 9e.

tag stays known because UNIT remains on.

D6 reports keeper loss under the default RET_SUP_COR setting (6.53, 9.7.4).

With KEEP healthy, which write gets saved?

Close a Level Save and Hold a Level Restore

The last write reaches acc while store is high. Will it survive?

Follow One Byte Across the Power CutRegister values across the power cut.
0123456samplesave openssave closesasleeprestartrecallheldreleasedstorerecall_nacc182cxx002c19tage7d3xxff01e6scratch192dxx01ff1a
Scroll horizontally ↔
  1. Cycle 0

    store opens. acc can still change.

  2. Cycle 1

    Closing store saves the second write.

  3. Cycle 2

    UNIT goes off. Compare with recall.

  4. Cycle 3

    A restart write replaces the working byte.

  5. Cycle 4

    Only acc returns to its saved value. Look across at tag and scratch.

  6. Cycle 5

    Writing fe changes tag and scratch. acc holds.

  7. Cycle 6

    Once recall ends, a fresh write reaches all three registers.

Recorded with OpenPA 0.3.0 and Icarus.

The save closes row reads acc=2c because the write occurred before store fell.

For a level save, the trailing edge captures the byte (6.53, 9.7.3). A high window closes on a fall. A low window closes on a rise.

In held, tag=01 confirms the clock wrote fe.

acc stays at 2c because low recall_n holds it.

A level restore also applies at power-up if already active (9.7.3).

Control in the labWhen it mattersTry this in the bench
store highThe window closes when store goes low.Move the final write before the window opens. Compare recall.
recall_n lowThe saved byte drives the working state during recall.Move the fe write past the release of recall_n.
clkA write distinguishes a held restore from a pulse.Read tag to establish that the clock really wrote.

Move one event per run.

Recorded in OpenPA 1.0 mode

recall acc=18 tag=ff scratch=01

The opening transition captures the first write.

Recorded in OpenPA 4.0 mode

recall acc=2c tag=ff scratch=01

The trailing edge captures the last write.

Compare those writes with a rising-edge save.

Capture on an Edge

In sleep_soc’s edge trial, store rises between writes.

EVENT names a rising save edge and a falling restore edge.
set_retention EVENT -domain PD_UNIT -elements {HEADER} -retention_supply KEEP -save_signal {store posedge} -restore_signal {recall_n negedge}

Leaving store high cannot capture later writes.

Recorded with the edge bench in the quiz.
edge capture acc=18 tag=e7 scratch=19
later write acc=2c tag=d3 scratch=2d
OPENPA-CHECK D7 @ 12000: set_retention EVENT: u_core.clk had an active edge at 9000, after the save and before PD_UNIT went off; the retained state may differ from the saved one (lesson.upf:23)
edge recall acc=18 tag=e7 scratch=01

Edge recall returns 18 even though the later write put 2c in acc.

Here posedge captures on a rise and negedge restores on a fall (6.53).

D7 flags a clock after capture. That later write explains the stale checkpoint.

Recorded with store high

recall acc=2c tag=ff scratch=01

The trailing edge followed the final write.

Recorded with store posedge

edge recall acc=18 tag=e7 scratch=01

The rising edge preceded that write.

Which Write Came Back?Compare matching observation points from the two experiments.
0123phasefirst writesecond writepower offafter recalllevel acc182cxx2cedge acc182cxx18
Scroll horizontally ↔
  1. Cycle 1

    Both registers accept the last write.

  2. Cycle 3

    Only the level save captures that last write.

Matched phases from two recorded runs.

Why did tag come back alongside acc?

In sleep_soc, the block author lists tag so integration can preserve its checkpoint.

HEADER and its user.
set_retention_elements HEADER -elements {u_core/tag}
set_retention EVENT -domain PD_UNIT -elements {HEADER} -retention_supply KEEP -save_signal {store posedge} -restore_signal {recall_n negedge}

set_retention_elements names an inventory (6.54). EVENT supplies the controls and power when it consumes HEADER.

First try HEADER alone.

Recorded before adding acc to EVENT.
edge capture acc=18 tag=e7 scratch=19
edge recall acc=00 tag=e7 scratch=01

With HEADER alone, edge recall returns tag=e7 and leaves acc=00.

If EVENT retains any state in PD_UNIT, every member of a required list there needs retention. An optional list allows partial selection.

CommandObject createdRead it back
set_retention_elements HEADERAn element list rooted in this scopeFind the actual register names in the consuming strategy.
set_retention EVENTA policy using HEADERRead its resolved registers, controls and supply.

The block owner supplies HEADER before integration.

How can the team add acc while keeping HEADER?

Extend the Strategy with -update

The team needs acc beside tag, with both on the same controls.

Because EVENT already saves tag, extend its elements with -update (6.53).

This line follows the definition that consumes HEADER.
set_retention EVENT -domain PD_UNIT -update -elements {u_core/acc}

Recorded before the update

edge recall acc=00 tag=e7 scratch=01

The register list contains tag.

Recorded after the update

edge recall acc=18 tag=e7 scratch=01

acc joins tag on the same controls and supply.

After the update, edge recall returns acc=18 beside tag=e7.

scratch still reads 01 from restart, ruling out whole-instance retention.

Recorded run. One resolved strategy after both commands.
  set_retention EVENT (answer.upf:23): u_core.tag, u_core.acc; save store posedge, restore recall_n negedge; supply KEEP

What happens when the PMU drives X?

Diagnose Unknown and Overlapping Controls

In sleep_soc, store goes unknown while acc still holds known data.

Recall the byte to expose a damaged shadow.

Two separate control failures, recorded on four-state Icarus.
OPENPA-CHECK D4 @ 27000: set_retention SNAP: save signal store is X or Z; the shadow is corrupt (lesson.upf:22)
uncertain save acc=xx tag=e6 scratch=1a
OPENPA-CHECK D5 @ 37000: set_retention SNAP: restore signal recall_n is X or Z; the registers are corrupt (lesson.upf:22)
uncertain recall acc=xx tag=ca scratch=36

Uncertain save reads acc=xx after recall because store went unknown before the transfer.

D5 identifies the later restore fault. tag still has data.

Saving or restoring while UNIT is off can also corrupt the shadow (9.7.4).

For the overlap trial, first save 24. Then write 57 and pull recall_n low while store is high.

Recorded run. Extra run with a joint level-control exclusion.
saved first acc=24 tag=db scratch=25
OPENPA-CHECK D14 @ 13000: set_retention SNAP -assert_rs_mutex: save and restore active together (answer.upf:23)
both active acc=xx tag=a8 scratch=58

Both active reads acc=xx despite the earlier save. With SAV_RES_COR, overlapping levels can destroy a valid checkpoint (6.53, 9.7.4).

In OpenPA 0.3.0, set_retention_control adds a check to SNAP. The 2024 command defines the controls on set_retention (6.53).

The edge trial checks EVENT against busy.
set_retention_control EVENT -domain PD_UNIT -assert_s_mutex {busy high} -assert_r_mutex {busy high} -assert_rs_mutex
Legacy checkWhat the PMU must keep separateObservation
-assert_s_mutex {busy high}A save and active busyD14 identifies the save-side overlap.
-assert_r_mutex {busy high}A restore and active busyD14 identifies the restore-side overlap.
-assert_rs_mutex without a listSave and restore themselvesThe level-control trial exercises both together.
-assert_rs_mutex with a listEither control and the listed activityW4.9.2 exercises each edge against a level.

Assertions report illegal control combinations. They do not arbitrate which operation should win.

Recorded run. One save and one restore occur while busy is high.
OPENPA-CHECK D14 @ 22000: set_retention EVENT -assert_s_mutex: save active together with busy high (lesson.upf:25)
OPENPA-CHECK D14 @ 23000: set_retention EVENT -assert_r_mutex: restore active together with busy high (lesson.upf:25)
Recorded limit for an edge against another edge.
  -: set_retention EVENT -assert_rs_mutex: both sides are edges; two edges in one time step are not checked

D14 distinguishes the two overlaps. OpenPA skips edge-against-edge coincidences.

What if a legal event should skip a transfer?

Gate Save, Restore and the Sleep Interval

Suppose sleep_soc must keep an older checkpoint despite a new save event.

The hand model first saves 32. It then writes 74 while permission to save is low.

Legacy option (6.53.1)Decision in the drawn circuitIf the condition fails
-save_conditionMay this save replace the shadow?Skip the copy into saved storage.
-restore_conditionMay this recall load the register?Leave the working register at its current value.
-retention_conditionMay the shadow survive the sleep interval?A failed condition with the primary outside NORMAL corrupts the retained value.

Separate gates on capture, recall and preservation.

Recorded OpenPA refusal for the legacy save condition.
Refused: answer.upf:22: set_retention -save_condition: not supported by OpenPA (IEEE 1801-2024 6.53.1, legacy: retention conditions)

OpenPA 0.3.0 refuses all three legacy conditions. Run the SystemVerilog hand model in the quiz to see each decision.

Recorded Icarus output from the hand model.
save gated q=74 shadow=32 primary_pin=74 backed_pin=74
recall gated q=00 shadow=32 primary_pin=00 backed_pin=00
recall allowed q=32 shadow=32 primary_pin=32 backed_pin=32
hold broken q=xx shadow=xx primary_pin=xx backed_pin=xx

Save gated keeps shadow=32 while q advances to 74.

Recall gated leaves q at 00. Recall allowed recovers 32 from the untouched shadow.

Hold broken spoils the shadow during sleep.

For current files, distinguish -save_event_condition and -restore_event_condition from -restore_period_condition and -powerdown_period_condition (6.53).

A false restore-event condition skips that transfer. Breaking a restore-period condition corrupts the element. Which rail drives its output?

Choose the Supply for the Output Driver

The host wants status while sleep_soc sleeps. Which rail drives the pin?

Drawn with ordinary output supply

UNIT -> working register -> output driver on UNIT
KEEP -> saved copy

The output can lose power while the shadow survives.

Drawn with retention-powered output

UNIT -> working register
KEEP -> saved copy and output driver

Drawn supply change for -use_retention_as_primary.

With -use_retention_as_primary, the retention supply powers the output driver. The working data input still receives on the domain primary (6.53).

Recorded refusal. OpenPA cannot exercise this output-supply option.
Refused: answer.upf:22: set_retention -use_retention_as_primary: not supported by OpenPA (IEEE 1801-2024 6.53: the register output powered by the retention supply)
Recorded hand model. During sleep its backed output selects the saved byte.
sleep pins q=xx shadow=32 primary_pin=xx backed_pin=32
hold broken q=xx shadow=xx primary_pin=xx backed_pin=xx

Sleep pins reads primary_pin=xx and backed_pin=32. The hand model explicitly connects the shadow to backed_pin during sleep.

Try the pin prediction, then protect the queue in W4.9.2.

Try It

Run the bench. Find the last write before store closes. Look for that byte again after the restart write.

Change the second data byte and rerun. Predict acc, tag and scratch separately in the held row.

Read the three intentional fault messages in order. D4 names the save control, D5 names restore, and D6 names KEEP.

Open the power-aware copy. Trace SNAP from its UPF source to the selected register in the object table.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: OPENPA-INFO corruption=x seed=1 save opens acc=18 tag=e7 scratch=19 save closes acc=2c tag=d3 scratch=2d asleep acc=xx tag=xx scratch=xx restart acc=00 tag=ff scratch=01 recall acc=2c tag=ff scratch=01 held acc=2c tag=01 scratch=ff released acc=19 tag=e6 scratch=1a OPENPA-CHECK D4 @ 27000: set_retention SNAP: save signal store is X or Z; the shadow is corrupt (lesson.upf:22) uncertain save acc=xx tag=e6 scratch=1a OPENPA-CHECK D5 @ 37000: set_retention SNAP: restore signal recall_n is X or Z; the registers are corrupt (lesson.upf:22) uncertain recall acc=xx tag=ca scratch=36 rail live acc=61 tag=9e scratch=62 OPENPA-CHECK D6 @ 42000: set_retention SNAP: retention supply KEEP lost while holding saved state rail lost acc=xx tag=9e scratch=62 OPENPA-SUMMARY D1=0 D2=0 D3=0 D4=1 D5=1 D6=1 D7=0 D8=0 D9=0 D10=0 D11=0 D12=0 D13=0 D14=0 OPENPA-COVER sequence D4 SNAP: save while PD_UNIT is on 2 OPENPA-COVER sequence D5 SNAP: restore of a saved state after power-up 2 OPENPA-COVER-SUMMARY states=0/0 declared_transitions=0/0 sequences=2/2
Read the power intent: lesson.upf
upf_version 4.0
set_design_top sleep_soc
create_power_domain PD_TOP -include_scope
create_power_domain PD_UNIT -elements {u_core}
create_supply_port VHOST -direction in
create_supply_net HOST_N
connect_supply_net HOST_N -ports {VHOST}
create_supply_port VUNIT -direction in
create_supply_net UNIT_N
connect_supply_net UNIT_N -ports {VUNIT}
create_supply_port VKEEP -direction in
create_supply_net KEEP_N
connect_supply_net KEEP_N -ports {VKEEP}
create_supply_port VSS -direction in
create_supply_net GND_N
connect_supply_net GND_N -ports {VSS}
create_supply_set HOST -function {power HOST_N} -function {ground GND_N}
create_supply_set UNIT -function {power UNIT_N} -function {ground GND_N}
create_supply_set KEEP -function {power KEEP_N} -function {ground GND_N}
associate_supply_set HOST -handle PD_TOP.primary
associate_supply_set UNIT -handle PD_UNIT.primary
set_retention SNAP -domain PD_UNIT -elements {u_core/acc} -retention_supply KEEP -save_signal {store high} -restore_signal {recall_n low}
Read the power-unaware RTL: bank.sv
module bank(input logic clk, input logic [7:0] d);
  logic [7:0] acc, tag, scratch;
  always_ff @(posedge clk) begin
    acc <= d;
    tag <= ~d;
    scratch <= d + 1'b1;
  end
endmodule
module sleep_soc(input logic clk, input logic [7:0] d, input logic store, recall_n, busy);
  bank u_core(.clk(clk), .d(d));
endmodule
What UPF added: 20 objects

4 supply port, 4 supply net, 2 output corruption, 3 supply set, 3 register corruption, 1 retention, 3 dynamic check.

ObjectWhereDetailRuleUPF line
Supply portVHOSTlesson.upf:5
Supply portVUNITlesson.upf:8
Supply portVKEEPlesson.upf:11
Supply portVSSlesson.upf:14
Supply netHOST_Nlesson.upf:5
Supply netUNIT_Nlesson.upf:8
Supply netKEEP_Nlesson.upf:11
Supply netGND_Nlesson.upf:14
Output corruptionu_core/clkC2lesson.upf:3
Output corruptionu_core/dC2lesson.upf:3
Supply setHOSTlesson.upf:17
Supply setUNITlesson.upf:18
Supply setKEEPlesson.upf:19
Register corruptionu_core.accC1lesson.upf:4
Register corruptionu_core.scratchC1lesson.upf:4
Register corruptionu_core.tagC1lesson.upf:4
RetentionSNAPu_core.accR1-R4lesson.upf:22
Dynamic checkD9 crossing 1D9lesson.upf:3
Dynamic checkD9 crossing 2D9lesson.upf:3
Dynamic checkD11 PD_TOP->PD_UNITD11lesson.upf:3
Read the power-aware copy that runs (generated by OpenPA 0.3.0): pa_sleep_soc.sv
// pa_sleep_soc.sv: power-aware copy of sleep_soc, generated by OpenPA from lesson.upf.
// Do not edit: regenerate. Simulators: Icarus 12 (4-state, X corruption) or Verilator 5 with --timing (2-state).
// Build with -Wno-fatal on Verilator: forced registers raise MULTIDRIVEN warnings.
`timescale 1ns/1ps
// ---------------------------------------------------------------------------
// package UPF: generated by OpenPA. Supply types, supply_on/supply_off and the power
// state of the design. Names starting with openpa_ are internal.
// ---------------------------------------------------------------------------
package UPF;
  typedef string upfStringT; typedef int upfIntegerT; typedef bit upfBooleanT; typedef logic upfLogicT; typedef real upfRealT; typedef int upfHandleT;
  typedef enum { OFF, FULL_ON, PARTIAL_ON, UNDETERMINED } upfSupplyStateE;
  typedef struct packed { upfSupplyStateE state; int voltage; } upfSupplyTypeT;  // voltage in microvolts
  typedef upfSupplyTypeT supply_net_type;
  bit openpa_armed;        // set by the first call that drives a supply
  bit openpa_kick;         // pulsed once at time 0 to evaluate everything
  bit openpa_xmode;        // 1: corrupt to X; 0: seeded 2-state values
  int unsigned openpa_seed = 1;
  int openpa_cmode;        // 2-state corruption: 0 random, 1 all zero, 2 all one
  supply_net_type openpa_port_VHOST;   // supply port VHOST (lesson.upf:5)
  supply_net_type openpa_port_VUNIT;   // supply port VUNIT (lesson.upf:8)
  supply_net_type openpa_port_VKEEP;   // supply port VKEEP (lesson.upf:11)
  supply_net_type openpa_port_VSS;   // supply port VSS (lesson.upf:14)
  supply_net_type openpa_net_HOST_N;   // supply net HOST_N
  supply_net_type openpa_net_UNIT_N;   // supply net UNIT_N
  supply_net_type openpa_net_KEEP_N;   // supply net KEEP_N
  supply_net_type openpa_net_GND_N;   // supply net GND_N
  bit openpa_ss_HOST_ok; int unsigned openpa_ss_HOST_ep; int openpa_ss_HOST_st = -1; bit openpa_ss_HOST_multi; int openpa_ss_HOST_sim = 5;   // supply set HOST
  bit openpa_ss_UNIT_ok; int unsigned openpa_ss_UNIT_ep; int openpa_ss_UNIT_st = -1; bit openpa_ss_UNIT_multi; int openpa_ss_UNIT_sim = 5;   // supply set UNIT
  bit openpa_ss_KEEP_ok; int unsigned openpa_ss_KEEP_ep; int openpa_ss_KEEP_st = -1; bit openpa_ss_KEEP_multi; int openpa_ss_KEEP_sim = 5;   // supply set KEEP
  bit openpa_dom_PD_TOP_ok; int unsigned openpa_dom_PD_TOP_ep; int openpa_dom_PD_TOP_sim = 5;   // power domain PD_TOP: ok = powered (simstate not CORRUPT); sim = simstate code (lesson.upf:3)
  bit openpa_dom_PD_UNIT_ok; int unsigned openpa_dom_PD_UNIT_ep; int openpa_dom_PD_UNIT_sim = 5;   // power domain PD_UNIT: ok = powered (simstate not CORRUPT); sim = simstate code (lesson.upf:4)
  // simstate codes, by IEEE 1801-2024 4.8 precedence: 0 NORMAL, 1 CORRUPT_STATE_ON_CHANGE, 2 CORRUPT_STATE_ON_ACTIVITY,
  // 3 CORRUPT_ON_CHANGE, 4 CORRUPT_ON_ACTIVITY, 5 CORRUPT (and NOT_NORMAL, 9.5.8); README "Simstates"
  logic openpa_ctl_0_store;   // copy of store (retention SNAP save)
  logic openpa_ctl_1_recall_n;   // copy of recall_n (retention SNAP restore)
  function automatic logic [63:0] openpa_mix(int unsigned id, int unsigned ep, int unsigned lane);
    logic [63:0] z;
    if (openpa_cmode == 1) return 64'd0;
    if (openpa_cmode == 2) return ~64'd0;
    z = {32'd0, openpa_seed} * 64'h9E3779B97F4A7C15 + {32'd0, id} * 64'hC2B2AE3D27D4EB4F + {32'd0, ep} * 64'h165667B19E3779F9 + {32'd0, lane} * 64'hD6E8FEB86659FD93;
    z = (z ^ (z >> 30)) * 64'hBF58476D1CE4E5B9;
    z = (z ^ (z >> 27)) * 64'h94D049BB133111EB;
    return z ^ (z >> 31);
  endfunction
  function automatic bit openpa_bit(int unsigned id, int unsigned ep); logic [63:0] z; z = openpa_mix(id, ep, 0); return z[0]; endfunction
  function automatic int openpa_uv(real v); return $rtoi(v * 1.0e6 + (v < 0.0 ? -0.5 : 0.5)); endfunction
  // supply_on: FULL_ON at the given voltage (11.2.4; default 1.0 V)
  function automatic upfBooleanT supply_on(string pad_name, real value = 1.0);
    openpa_armed = 1;
    if (pad_name == "VHOST") begin openpa_port_VHOST.state = FULL_ON; openpa_port_VHOST.voltage = openpa_uv(value); return 1; end
    if (pad_name == "VUNIT") begin openpa_port_VUNIT.state = FULL_ON; openpa_port_VUNIT.voltage = openpa_uv(value); return 1; end
    if (pad_name == "VKEEP") begin openpa_port_VKEEP.state = FULL_ON; openpa_port_VKEEP.voltage = openpa_uv(value); return 1; end
    if (pad_name == "VSS") begin openpa_port_VSS.state = FULL_ON; openpa_port_VSS.voltage = openpa_uv(value); return 1; end
    if (pad_name == "HOST_N") begin $display("OPENPA-ERROR supply_on(\"%s\"): HOST_N already has a driver (port VHOST); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "HOST.power") begin $display("OPENPA-ERROR supply_on(\"%s\"): HOST.power already has a driver (port VHOST); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "PD_TOP.primary.power") begin $display("OPENPA-ERROR supply_on(\"%s\"): PD_TOP.primary.power already has a driver (port VHOST); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "UNIT_N") begin $display("OPENPA-ERROR supply_on(\"%s\"): UNIT_N already has a driver (port VUNIT); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "UNIT.power") begin $display("OPENPA-ERROR supply_on(\"%s\"): UNIT.power already has a driver (port VUNIT); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "PD_UNIT.primary.power") begin $display("OPENPA-ERROR supply_on(\"%s\"): PD_UNIT.primary.power already has a driver (port VUNIT); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "KEEP_N") begin $display("OPENPA-ERROR supply_on(\"%s\"): KEEP_N already has a driver (port VKEEP); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "KEEP.power") begin $display("OPENPA-ERROR supply_on(\"%s\"): KEEP.power already has a driver (port VKEEP); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "GND_N") begin $display("OPENPA-ERROR supply_on(\"%s\"): GND_N already has a driver (port VSS); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "HOST.ground") begin $display("OPENPA-ERROR supply_on(\"%s\"): HOST.ground already has a driver (port VSS); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "UNIT.ground") begin $display("OPENPA-ERROR supply_on(\"%s\"): UNIT.ground already has a driver (port VSS); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "KEEP.ground") begin $display("OPENPA-ERROR supply_on(\"%s\"): KEEP.ground already has a driver (port VSS); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "PD_TOP.primary.ground") begin $display("OPENPA-ERROR supply_on(\"%s\"): PD_TOP.primary.ground already has a driver (port VSS); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "PD_UNIT.primary.ground") begin $display("OPENPA-ERROR supply_on(\"%s\"): PD_UNIT.primary.ground already has a driver (port VSS); drive its root supply instead", pad_name); return 0; end
    $display("OPENPA-ERROR supply_on(\"%s\"): no supply port or root supply net of that name (writable: VHOST VUNIT VKEEP VSS)", pad_name);
    return 0;
  endfunction
  // supply_off: OFF, voltage unchanged (9.3.1); a second argument is accepted and ignored
  function automatic upfBooleanT supply_off(string pad_name, real value = 0.0);
    openpa_armed = 1;
    if (pad_name == "VHOST") begin openpa_port_VHOST.state = OFF; return 1; end
    if (pad_name == "VUNIT") begin openpa_port_VUNIT.state = OFF; return 1; end
    if (pad_name == "VKEEP") begin openpa_port_VKEEP.state = OFF; return 1; end
    if (pad_name == "VSS") begin openpa_port_VSS.state = OFF; return 1; end
    if (pad_name == "HOST_N") begin $display("OPENPA-ERROR supply_off(\"%s\"): HOST_N already has a driver (port VHOST); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "HOST.power") begin $display("OPENPA-ERROR supply_off(\"%s\"): HOST.power already has a driver (port VHOST); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "PD_TOP.primary.power") begin $display("OPENPA-ERROR supply_off(\"%s\"): PD_TOP.primary.power already has a driver (port VHOST); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "UNIT_N") begin $display("OPENPA-ERROR supply_off(\"%s\"): UNIT_N already has a driver (port VUNIT); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "UNIT.power") begin $display("OPENPA-ERROR supply_off(\"%s\"): UNIT.power already has a driver (port VUNIT); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "PD_UNIT.primary.power") begin $display("OPENPA-ERROR supply_off(\"%s\"): PD_UNIT.primary.power already has a driver (port VUNIT); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "KEEP_N") begin $display("OPENPA-ERROR supply_off(\"%s\"): KEEP_N already has a driver (port VKEEP); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "KEEP.power") begin $display("OPENPA-ERROR supply_off(\"%s\"): KEEP.power already has a driver (port VKEEP); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "GND_N") begin $display("OPENPA-ERROR supply_off(\"%s\"): GND_N already has a driver (port VSS); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "HOST.ground") begin $display("OPENPA-ERROR supply_off(\"%s\"): HOST.ground already has a driver (port VSS); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "UNIT.ground") begin $display("OPENPA-ERROR supply_off(\"%s\"): UNIT.ground already has a driver (port VSS); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "KEEP.ground") begin $display("OPENPA-ERROR supply_off(\"%s\"): KEEP.ground already has a driver (port VSS); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "PD_TOP.primary.ground") begin $display("OPENPA-ERROR supply_off(\"%s\"): PD_TOP.primary.ground already has a driver (port VSS); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "PD_UNIT.primary.ground") begin $display("OPENPA-ERROR supply_off(\"%s\"): PD_UNIT.primary.ground already has a driver (port VSS); drive its root supply instead", pad_name); return 0; end
    $display("OPENPA-ERROR supply_off(\"%s\"): no supply port or root supply net of that name (writable: VHOST VUNIT VKEEP VSS)", pad_name);
    return 0;
  endfunction
  // supply_partial_on: PARTIAL_ON at the given voltage (11.2.4)
  function automatic upfBooleanT supply_partial_on(string pad_name, real value = 1.0);
    openpa_armed = 1;
    if (pad_name == "VHOST") begin openpa_port_VHOST.state = PARTIAL_ON; openpa_port_VHOST.voltage = openpa_uv(value); return 1; end
    if (pad_name == "VUNIT") begin openpa_port_VUNIT.state = PARTIAL_ON; openpa_port_VUNIT.voltage = openpa_uv(value); return 1; end
    if (pad_name == "VKEEP") begin openpa_port_VKEEP.state = PARTIAL_ON; openpa_port_VKEEP.voltage = openpa_uv(value); return 1; end
    if (pad_name == "VSS") begin openpa_port_VSS.state = PARTIAL_ON; openpa_port_VSS.voltage = openpa_uv(value); return 1; end
    if (pad_name == "HOST_N") begin $display("OPENPA-ERROR supply_partial_on(\"%s\"): HOST_N already has a driver (port VHOST); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "HOST.power") begin $display("OPENPA-ERROR supply_partial_on(\"%s\"): HOST.power already has a driver (port VHOST); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "PD_TOP.primary.power") begin $display("OPENPA-ERROR supply_partial_on(\"%s\"): PD_TOP.primary.power already has a driver (port VHOST); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "UNIT_N") begin $display("OPENPA-ERROR supply_partial_on(\"%s\"): UNIT_N already has a driver (port VUNIT); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "UNIT.power") begin $display("OPENPA-ERROR supply_partial_on(\"%s\"): UNIT.power already has a driver (port VUNIT); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "PD_UNIT.primary.power") begin $display("OPENPA-ERROR supply_partial_on(\"%s\"): PD_UNIT.primary.power already has a driver (port VUNIT); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "KEEP_N") begin $display("OPENPA-ERROR supply_partial_on(\"%s\"): KEEP_N already has a driver (port VKEEP); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "KEEP.power") begin $display("OPENPA-ERROR supply_partial_on(\"%s\"): KEEP.power already has a driver (port VKEEP); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "GND_N") begin $display("OPENPA-ERROR supply_partial_on(\"%s\"): GND_N already has a driver (port VSS); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "HOST.ground") begin $display("OPENPA-ERROR supply_partial_on(\"%s\"): HOST.ground already has a driver (port VSS); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "UNIT.ground") begin $display("OPENPA-ERROR supply_partial_on(\"%s\"): UNIT.ground already has a driver (port VSS); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "KEEP.ground") begin $display("OPENPA-ERROR supply_partial_on(\"%s\"): KEEP.ground already has a driver (port VSS); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "PD_TOP.primary.ground") begin $display("OPENPA-ERROR supply_partial_on(\"%s\"): PD_TOP.primary.ground already has a driver (port VSS); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "PD_UNIT.primary.ground") begin $display("OPENPA-ERROR supply_partial_on(\"%s\"): PD_UNIT.primary.ground already has a driver (port VSS); drive its root supply instead", pad_name); return 0; end
    $display("OPENPA-ERROR supply_partial_on(\"%s\"): no supply port or root supply net of that name (writable: VHOST VUNIT VKEEP VSS)", pad_name);
    return 0;
  endfunction
  // set_supply_state: the state only, voltage unchanged (9.3.1)
  function automatic upfBooleanT set_supply_state(string pad_name, upfSupplyStateE supply_state);
    openpa_armed = 1;
    if (pad_name == "VHOST") begin openpa_port_VHOST.state = supply_state; return 1; end
    if (pad_name == "VUNIT") begin openpa_port_VUNIT.state = supply_state; return 1; end
    if (pad_name == "VKEEP") begin openpa_port_VKEEP.state = supply_state; return 1; end
    if (pad_name == "VSS") begin openpa_port_VSS.state = supply_state; return 1; end
    if (pad_name == "HOST_N") begin $display("OPENPA-ERROR set_supply_state(\"%s\"): HOST_N already has a driver (port VHOST); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "HOST.power") begin $display("OPENPA-ERROR set_supply_state(\"%s\"): HOST.power already has a driver (port VHOST); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "PD_TOP.primary.power") begin $display("OPENPA-ERROR set_supply_state(\"%s\"): PD_TOP.primary.power already has a driver (port VHOST); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "UNIT_N") begin $display("OPENPA-ERROR set_supply_state(\"%s\"): UNIT_N already has a driver (port VUNIT); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "UNIT.power") begin $display("OPENPA-ERROR set_supply_state(\"%s\"): UNIT.power already has a driver (port VUNIT); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "PD_UNIT.primary.power") begin $display("OPENPA-ERROR set_supply_state(\"%s\"): PD_UNIT.primary.power already has a driver (port VUNIT); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "KEEP_N") begin $display("OPENPA-ERROR set_supply_state(\"%s\"): KEEP_N already has a driver (port VKEEP); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "KEEP.power") begin $display("OPENPA-ERROR set_supply_state(\"%s\"): KEEP.power already has a driver (port VKEEP); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "GND_N") begin $display("OPENPA-ERROR set_supply_state(\"%s\"): GND_N already has a driver (port VSS); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "HOST.ground") begin $display("OPENPA-ERROR set_supply_state(\"%s\"): HOST.ground already has a driver (port VSS); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "UNIT.ground") begin $display("OPENPA-ERROR set_supply_state(\"%s\"): UNIT.ground already has a driver (port VSS); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "KEEP.ground") begin $display("OPENPA-ERROR set_supply_state(\"%s\"): KEEP.ground already has a driver (port VSS); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "PD_TOP.primary.ground") begin $display("OPENPA-ERROR set_supply_state(\"%s\"): PD_TOP.primary.ground already has a driver (port VSS); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "PD_UNIT.primary.ground") begin $display("OPENPA-ERROR set_supply_state(\"%s\"): PD_UNIT.primary.ground already has a driver (port VSS); drive its root supply instead", pad_name); return 0; end
    $display("OPENPA-ERROR set_supply_state(\"%s\"): no supply port or root supply net of that name (writable: VHOST VUNIT VKEEP VSS)", pad_name);
    return 0;
  endfunction
  // set_power_state (9.3.1) is not supported: OpenPA has no deferred power states. A call is reported, never ignored.
  function automatic upfBooleanT set_power_state(string object_name, string power_state);
    $display("OPENPA-ERROR set_power_state(\"%s\", \"%s\"): not supported by OpenPA; drive the supplies with supply_on, supply_off or set_supply_state", object_name, power_state);
    return 0;
  endfunction
  // Supply queries (11.2.4). Names: supply ports, supply nets and supply set functions as the UPF wrote them.
  function automatic upfSupplyTypeT get_supply_value(string name);
    if (name == "VHOST" || name == "HOST_N" || name == "HOST.power" || name == "PD_TOP.primary.power") return openpa_net_HOST_N;
    if (name == "VUNIT" || name == "UNIT_N" || name == "UNIT.power" || name == "PD_UNIT.primary.power") return openpa_net_UNIT_N;
    if (name == "VKEEP" || name == "KEEP_N" || name == "KEEP.power") return openpa_net_KEEP_N;
    if (name == "VSS" || name == "GND_N" || name == "HOST.ground" || name == "UNIT.ground" || name == "KEEP.ground" || name == "PD_TOP.primary.ground" || name == "PD_UNIT.primary.ground") return openpa_net_GND_N;
    $display("OPENPA-ERROR get_supply_value(\"%s\"): no supply net, port or supply set function of that name", name);
    return 0;
  endfunction
  function automatic upfRealT get_supply_voltage(upfSupplyTypeT arg); return arg.voltage / 1.0e6; endfunction
  // 11.2.4: 1 for FULL_ON; 0 otherwise.
  function automatic upfBooleanT get_supply_on_state(upfSupplyTypeT arg); return arg.state == FULL_ON; endfunction
  function automatic upfSupplyStateE get_supply_state(upfSupplyTypeT arg); return arg.state; endfunction
endpackage
// ---------------------------------------------------------------------------
// OpenPA cells (generated; do not edit)
// ---------------------------------------------------------------------------
// openpa_corrupt: rule C2. Passes d while the driving domain is NORMAL; otherwise drives X (4-state)
// or a seeded value (2-state). The value is computed ahead for the episode the next power-down starts
// (ep + 1 while ok), so at power-down the output goes straight from d to the new value: no delta glitch
// through the previous episode's value (Icarus would turn one into a clock edge).
module openpa_corrupt #(parameter int W = 1, parameter int unsigned ID = 0) (
  input  logic [W-1:0] d,
  output logic [W-1:0] q,
  input  bit           ok,
  input  int unsigned  ep
);
  localparam int L = (W + 63) / 64;
  logic [L*64-1:0] cv;
  always @(ep or ok or UPF::openpa_kick)
    for (int i = 0; i < L; i++) cv[i*64 +: 64] = UPF::openpa_mix(ID, ok ? ep + 1 : ep, i);
  assign q = ok ? d : (UPF::openpa_xmode ? {W{1'bx}} : cv[W-1:0]);
endmodule

// openpa_corrupt_sim: rule C2 for a domain that has intermediate simstates (IEEE 1801-2024 9.5). As
// openpa_corrupt, and also corrupt while lhit is set (CORRUPT_ON_ACTIVITY after input activity), or after d
// changed while coc is set (CORRUPT_ON_CHANGE); leaving CORRUPT_ON_CHANGE clears that.
module openpa_corrupt_sim #(parameter int W = 1, parameter int unsigned ID = 0) (
  input  logic [W-1:0] d,
  output logic [W-1:0] q,
  input  bit           ok,
  input  int unsigned  ep,
  input  bit           lhit,
  input  bit           coc
);
  localparam int L = (W + 63) / 64;
  logic [L*64-1:0] cv;
  bit chg;
  always @(ep or ok or UPF::openpa_kick)
    for (int i = 0; i < L; i++) cv[i*64 +: 64] = UPF::openpa_mix(ID, ok ? ep + 1 : ep, i);
  // verilator lint_off MULTIDRIVEN
  for (genvar i = 0; i < W; i++) begin : g_chg
    always @(posedge d[i] or negedge d[i]) if (coc && ok) chg = 1'b1;
  end
  always @(negedge coc) chg = 1'b0;
  // verilator lint_on MULTIDRIVEN
  assign q = (ok && !lhit && !chg) ? d : (UPF::openpa_xmode ? {W{1'bx}} : cv[W-1:0]);
endmodule

// openpa_iso: rules I1-I4 (IEEE 1801-2024 9.8). CLAMP 0, 1, 2 (latch: the value the input had while the enable
// was inactive) or 3 (Z: high impedance on 4-state; 0 on 2-state backends, which cannot represent Z).
// sup_ok is 1 while the isolation supply is NORMAL (always, for a strategy with no supply); otherwise the output,
// and a latched value, are corrupt. An enable at X or Z corrupts the output and the latched value.
module openpa_iso #(parameter int W = 1, parameter int CLAMP = 0, parameter bit SENSE = 1'b1, parameter int unsigned ID = 0) (
  input  logic [W-1:0] d,
  output logic [W-1:0] q,
  input  logic         en,
  input  bit           sup_ok,
  input  int unsigned  sup_ep
);
  localparam int L = (W + 63) / 64;
  logic [L*64-1:0] cv;
  logic [W-1:0] held;
  always @(sup_ep or sup_ok or UPF::openpa_kick)
    for (int i = 0; i < L; i++) cv[i*64 +: 64] = UPF::openpa_mix(ID + 32'h4000_0000, sup_ok ? sup_ep + 1 : sup_ep, i);
  wire active   = (en === SENSE);
  wire inactive = (en === !SENSE);
  wire [W-1:0] bad    = UPF::openpa_xmode ? {W{1'bx}} : cv[W-1:0];
  if (CLAMP == 2) begin : g_latch
    always @(d or en or sup_ok or bad) if (!sup_ok || !(active || inactive)) held = bad; else if (inactive) held = d;
  end
  wire [W-1:0] clampv = (CLAMP == 3) ? (UPF::openpa_xmode ? {W{1'bz}} : {W{1'b0}}) : (CLAMP == 2) ? held : (CLAMP == 1) ? {W{1'b1}} : {W{1'b0}};
  assign q = !sup_ok ? bad : active ? clampv : inactive ? d : bad;
endmodule

// openpa_ls: rule L1 (IEEE 1801-2024 9.9). Passes d while every supply set of the cell is NORMAL; otherwise corrupt.
module openpa_ls #(parameter int W = 1, parameter int unsigned ID = 0) (
  input  logic [W-1:0] d,
  output logic [W-1:0] q,
  input  bit           src_ok,
  input  bit           dst_ok,
  input  int unsigned  ep
);
  localparam int L = (W + 63) / 64;
  logic [L*64-1:0] cv;
  always @(ep or src_ok or dst_ok or UPF::openpa_kick)
    for (int i = 0; i < L; i++) cv[i*64 +: 64] = UPF::openpa_mix(ID + 32'h8000_0000, (src_ok && dst_ok) ? ep + 1 : ep, i);
  assign q = (src_ok && dst_ok) ? d : (UPF::openpa_xmode ? {W{1'bx}} : cv[W-1:0]);
endmodule

`line 1 "bank.sv" 0
module bank(input logic clk, input logic [7:0] d);
  logic [7:0] acc, tag, scratch;
  always_ff @(posedge clk) begin
    acc <= d;
    tag <= ~d;
    scratch <= d + 1'b1;
  end
endmodule
module sleep_soc(input logic clk, input logic [7:0] d, input logic store, recall_n, busy);
  wire [0:0] openpa_u_core_clk_s1; wire [7:0] openpa_u_core_d_s1; bank u_core(.clk(openpa_u_core_clk_s1), .d(openpa_u_core_d_s1)); openpa_corrupt #(.W(1), .ID(268435456)) openpa_u_core_clk_cor (.d(clk), .q(openpa_u_core_clk_s1), .ok(UPF::openpa_dom_PD_TOP_ok), .ep(UPF::openpa_dom_PD_TOP_ep)); openpa_corrupt #(.W(8), .ID(268435464)) openpa_u_core_d_cor (.d(d), .q(openpa_u_core_d_s1), .ok(UPF::openpa_dom_PD_TOP_ok), .ep(UPF::openpa_dom_PD_TOP_ep));

`line 1 "openpa_overlay.sv" 0
  // ===================================================================================
  // OpenPA overlay for sleep_soc: generated from lesson.upf. Do not edit.
  // ===================================================================================
  // verilator lint_off MULTIDRIVEN
  // verilator lint_off ENUMVALUE
  // verilator lint_off WIDTHTRUNC
  // verilator lint_off WIDTHEXPAND
  // verilator lint_off BLKSEQ
  // verilator lint_off IGNOREDRETURN
  import UPF::openpa_cmode;
  import UPF::openpa_ctl_0_store;
  import UPF::openpa_ctl_1_recall_n;
  import UPF::openpa_dom_PD_TOP_ep;
  import UPF::openpa_dom_PD_TOP_ok;
  import UPF::openpa_dom_PD_TOP_sim;
  import UPF::openpa_dom_PD_UNIT_ep;
  import UPF::openpa_dom_PD_UNIT_ok;
  import UPF::openpa_dom_PD_UNIT_sim;
  import UPF::openpa_kick;
  import UPF::openpa_net_GND_N;
  import UPF::openpa_net_HOST_N;
  import UPF::openpa_net_KEEP_N;
  import UPF::openpa_net_UNIT_N;
  import UPF::openpa_seed;
  import UPF::openpa_ss_HOST_ep;
  import UPF::openpa_ss_HOST_multi;
  import UPF::openpa_ss_HOST_ok;
  import UPF::openpa_ss_HOST_sim;
  import UPF::openpa_ss_HOST_st;
  import UPF::openpa_ss_KEEP_ep;
  import UPF::openpa_ss_KEEP_multi;
  import UPF::openpa_ss_KEEP_ok;
  import UPF::openpa_ss_KEEP_sim;
  import UPF::openpa_ss_KEEP_st;
  import UPF::openpa_ss_UNIT_ep;
  import UPF::openpa_ss_UNIT_multi;
  import UPF::openpa_ss_UNIT_ok;
  import UPF::openpa_ss_UNIT_sim;
  import UPF::openpa_ss_UNIT_st;
  import UPF::openpa_xmode;
  // ---- mode, seed, trace
  bit openpa_tr, openpa_obs;
  int openpa_n [1:14];
  int openpa_seq [1:2];   // good-form sequence bins (coverage)
  bit openpa_sreq; bit openpa_sp [1:8];   // deferred checks: request and pending flags
  string openpa_mode_arg;
  initial begin
  `ifdef VERILATOR
    openpa_xmode = 1'b0;
  `else
    openpa_xmode = 1'b1;
  `endif
    openpa_cmode = 0;
    if ($value$plusargs("openpa_corrupt=%s", openpa_mode_arg)) begin
      if (openpa_mode_arg == "x") openpa_xmode = 1'b1;
      else if (openpa_mode_arg == "random") openpa_xmode = 1'b0;
      else if (openpa_mode_arg == "zero") begin openpa_xmode = 1'b0; openpa_cmode = 1; end
      else if (openpa_mode_arg == "one") begin openpa_xmode = 1'b0; openpa_cmode = 2; end
      else $display("OPENPA-ERROR +openpa_corrupt=%s: use x, random, zero or one", openpa_mode_arg);
    end
    if (!$value$plusargs("openpa_seed=%d", openpa_seed)) openpa_seed = 1;
    openpa_tr = $test$plusargs("openpa_trace");
    openpa_obs = $test$plusargs("openpa_obs");
    $display("OPENPA-INFO corruption=%0s seed=%0d", openpa_xmode ? "x" : (openpa_cmode == 1 ? "zero" : openpa_cmode == 2 ? "one" : "random"), openpa_seed);
    #0 openpa_kick = 1'b1;
  end
  task automatic openpa_check(input int id, input string msg);
    openpa_n[id] = openpa_n[id] + 1;
    $display("OPENPA-CHECK D%0d @ %0t: %s", id, $time, msg);
  endtask
  task automatic openpa_t(input string msg);
    if (openpa_tr) $display("OPENPA-TRACE @ %0t: %s", $time, msg);
  endtask

  // ---- copies of the design signals the UPF names (4-state, so X is visible)
  always @(store or openpa_kick) openpa_ctl_0_store = store;
  always @(recall_n or openpa_kick) openpa_ctl_1_recall_n = recall_n;

  // ---- supply network, switches, supply sets, simstates, power states (one block, evaluated in order)
  always @(openpa_kick or UPF::openpa_port_VHOST or UPF::openpa_port_VUNIT or UPF::openpa_port_VKEEP or UPF::openpa_port_VSS) begin : openpa_network
    openpa_net_HOST_N = UPF::openpa_port_VHOST;   // HOST_N <- port VHOST
    openpa_net_UNIT_N = UPF::openpa_port_VUNIT;   // UNIT_N <- port VUNIT
    openpa_net_KEEP_N = UPF::openpa_port_VKEEP;   // KEEP_N <- port VKEEP
    openpa_net_GND_N = UPF::openpa_port_VSS;   // GND_N <- port VSS
    // supply set HOST (also PD_TOP.primary): IEEE 1801-2024 4.7.3, 4.7.4, 6.5.6, 9.3.2, 5.7.8
    begin : openpa_ss_HOST
      bit nx; int sm;
      sm = (UPF::openpa_net_HOST_N.state == UPF::FULL_ON && UPF::openpa_net_GND_N.state == UPF::FULL_ON) ? 0 : 5;   // predefined ON (NORMAL) or OFF (CORRUPT), IEEE 1801-2024 4.7.4
      openpa_ss_HOST_sim = sm;
      nx = sm < 5;
      if (openpa_ss_HOST_ok && !nx) openpa_ss_HOST_ep = openpa_ss_HOST_ep + 1;
      openpa_ss_HOST_ok = nx;
    end
    // supply set UNIT (also PD_UNIT.primary): IEEE 1801-2024 4.7.3, 4.7.4, 6.5.6, 9.3.2, 5.7.8
    begin : openpa_ss_UNIT
      bit nx; int sm;
      sm = (UPF::openpa_net_UNIT_N.state == UPF::FULL_ON && UPF::openpa_net_GND_N.state == UPF::FULL_ON) ? 0 : 5;   // predefined ON (NORMAL) or OFF (CORRUPT), IEEE 1801-2024 4.7.4
      openpa_ss_UNIT_sim = sm;
      nx = sm < 5;
      if (openpa_ss_UNIT_ok && !nx) openpa_ss_UNIT_ep = openpa_ss_UNIT_ep + 1;
      openpa_ss_UNIT_ok = nx;
    end
    // supply set KEEP: IEEE 1801-2024 4.7.3, 4.7.4, 6.5.6, 9.3.2, 5.7.8
    begin : openpa_ss_KEEP
      bit nx; int sm;
      sm = (UPF::openpa_net_KEEP_N.state == UPF::FULL_ON && UPF::openpa_net_GND_N.state == UPF::FULL_ON) ? 0 : 5;   // predefined ON (NORMAL) or OFF (CORRUPT), IEEE 1801-2024 4.7.4
      openpa_ss_KEEP_sim = sm;
      nx = sm < 5;
      if (openpa_ss_KEEP_ok && !nx) openpa_ss_KEEP_ep = openpa_ss_KEEP_ep + 1;
      openpa_ss_KEEP_ok = nx;
    end
    // domain PD_TOP: simstate of its primary supply set HOST
    if (openpa_dom_PD_TOP_ok && !UPF::openpa_ss_HOST_ok) openpa_dom_PD_TOP_ep = openpa_dom_PD_TOP_ep + 1;
    openpa_dom_PD_TOP_ok = UPF::openpa_ss_HOST_ok;
    openpa_dom_PD_TOP_sim = openpa_ss_HOST_sim;
    // domain PD_UNIT: simstate of its primary supply set UNIT
    if (openpa_dom_PD_UNIT_ok && !UPF::openpa_ss_UNIT_ok) openpa_dom_PD_UNIT_ep = openpa_dom_PD_UNIT_ep + 1;
    openpa_dom_PD_UNIT_ok = UPF::openpa_ss_UNIT_ok;
    openpa_dom_PD_UNIT_sim = openpa_ss_UNIT_sim;
  end

  // ---- corruption: registers forced, memories overwritten (rules C1, C3)
  bit openpa_PD_UNIT_await;   // PD_UNIT powered up with saved state not yet restored (D3)
  // domain PD_TOP (lesson.upf:3): 0 register(s), 0 memory(ies)
  bit openpa_PD_TOP_applied, openpa_PD_TOP_valid;
  // The first evaluation counts as a power-up from OFF: registers take the corruption value even when the
  // supplies are already on at time 0 (rule C3), so 4-state and 2-state runs start alike.
  always @(UPF::openpa_dom_PD_TOP_ok or openpa_kick) if (!openpa_PD_TOP_valid || openpa_PD_TOP_applied != UPF::openpa_dom_PD_TOP_ok) begin
    openpa_t({"PD_TOP ", UPF::openpa_dom_PD_TOP_ok ? "NORMAL" : "CORRUPT"});
    if (!UPF::openpa_dom_PD_TOP_ok || !openpa_PD_TOP_valid) begin
    end
    if (UPF::openpa_dom_PD_TOP_ok) begin
    end
    openpa_PD_TOP_valid = 1'b1; openpa_PD_TOP_applied = UPF::openpa_dom_PD_TOP_ok;
  end
  // domain PD_UNIT (lesson.upf:4): 3 register(s), 0 memory(ies)
  bit openpa_PD_UNIT_applied, openpa_PD_UNIT_valid;
  logic [7:0] openpa_cv1;   // corruption value for u_core.acc
  logic [7:0] openpa_cv2;   // corruption value for u_core.scratch
  logic [7:0] openpa_cv3;   // corruption value for u_core.tag
  // The first evaluation counts as a power-up from OFF: registers take the corruption value even when the
  // supplies are already on at time 0 (rule C3), so 4-state and 2-state runs start alike.
  always @(UPF::openpa_dom_PD_UNIT_ok or openpa_kick) if (!openpa_PD_UNIT_valid || openpa_PD_UNIT_applied != UPF::openpa_dom_PD_UNIT_ok) begin
    openpa_t({"PD_UNIT ", UPF::openpa_dom_PD_UNIT_ok ? "NORMAL" : "CORRUPT"});
    if (!UPF::openpa_dom_PD_UNIT_ok || !openpa_PD_UNIT_valid) begin
      if (openpa_xmode) openpa_cv1 = 'x; else openpa_cv1 = UPF::openpa_mix(1, UPF::openpa_dom_PD_UNIT_ep, 0);
      force u_core.acc = openpa_cv1;
      if (openpa_xmode) openpa_cv2 = 'x; else openpa_cv2 = UPF::openpa_mix(2, UPF::openpa_dom_PD_UNIT_ep, 0);
      force u_core.scratch = openpa_cv2;
      if (openpa_xmode) openpa_cv3 = 'x; else openpa_cv3 = UPF::openpa_mix(3, UPF::openpa_dom_PD_UNIT_ep, 0);
      force u_core.tag = openpa_cv3;
    end
    if (UPF::openpa_dom_PD_UNIT_ok) begin
      release u_core.acc;
      release u_core.scratch;
      release u_core.tag;
    end
    openpa_PD_UNIT_valid = 1'b1; openpa_PD_UNIT_applied = UPF::openpa_dom_PD_UNIT_ok;
  end

  // ---- set_retention SNAP (lesson.upf:22): u_core.acc (rules R1-R4)
  bit openpa_PD_UNIT_SNAP_valid; logic openpa_PD_UNIT_SNAP_sv, openpa_PD_UNIT_SNAP_rv;
  bit openpa_PD_UNIT_SNAP_arm7; realtime openpa_PD_UNIT_SNAP_st7, openpa_PD_UNIT_SNAP_ck7_0 = -1.0;
  logic [7:0] openpa_PD_UNIT_SNAP_sh0;   // retained value of u_core.acc, on KEEP
  always @(posedge UPF::openpa_ctl_0_store or negedge UPF::openpa_ctl_0_store) begin
    if ($isunknown(openpa_ctl_0_store)) begin   // a save signal at X or Z: the retained value is corrupt (UPF 1.0 5.3)
      openpa_PD_UNIT_SNAP_sh0 = openpa_xmode ? 'x : UPF::openpa_mix(60001, UPF::openpa_ss_KEEP_ep, 0);
      if (UPF::openpa_ss_KEEP_ok) openpa_PD_UNIT_SNAP_valid = 1'b1;   // the shadow holds a (corrupt) saved value
      openpa_check(4, "set_retention SNAP: save signal store is X or Z; the shadow is corrupt (lesson.upf:22)");
    end
    else if (openpa_PD_UNIT_SNAP_sv === 1'b1 && UPF::openpa_ctl_0_store !== 1'b1)
      begin openpa_sp[1] = 1'b1; openpa_sreq = 1'b1; end
    openpa_PD_UNIT_SNAP_sv = UPF::openpa_ctl_0_store;
  end
  always @(posedge UPF::openpa_ctl_1_recall_n or negedge UPF::openpa_ctl_1_recall_n) begin
    if ($isunknown(openpa_ctl_1_recall_n)) begin   // a restore signal at X or Z: the register is corrupt (UPF 1.0 5.3)
      if (openpa_xmode) openpa_cv1 = 'x; else openpa_cv1 = UPF::openpa_mix(1, UPF::openpa_dom_PD_UNIT_ep, 0);
      force u_core.acc = openpa_cv1; release u_core.acc;
      openpa_check(5, "set_retention SNAP: restore signal recall_n is X or Z; the registers are corrupt (lesson.upf:22)");
    end
    else if (openpa_PD_UNIT_SNAP_rv !== 1'b0 && UPF::openpa_ctl_1_recall_n === 1'b0)
      begin openpa_sp[2] = 1'b1; openpa_sreq = 1'b1; end
    else if (UPF::openpa_ctl_1_recall_n !== 1'b0 && openpa_PD_UNIT_SNAP_rv === 1'b0 && UPF::openpa_dom_PD_UNIT_ok) begin   // the restore period ends: registers keep the restored value
      release u_core.acc;
    end
    openpa_PD_UNIT_SNAP_rv = UPF::openpa_ctl_1_recall_n;
  end
  always @(posedge UPF::openpa_dom_PD_UNIT_ok) if (UPF::openpa_ctl_1_recall_n === 1'b0)
    begin openpa_sp[3] = 1'b1; openpa_sreq = 1'b1; end
  bit openpa_PD_UNIT_SNAP_both;
  always @(posedge UPF::openpa_ctl_0_store or negedge UPF::openpa_ctl_0_store or posedge UPF::openpa_ctl_1_recall_n or negedge UPF::openpa_ctl_1_recall_n) begin
    if (UPF::openpa_ctl_0_store === 1'b1 && UPF::openpa_ctl_1_recall_n === 1'b0) begin
      if (!openpa_PD_UNIT_SNAP_both) begin
        openpa_PD_UNIT_SNAP_sh0 = openpa_xmode ? 'x : UPF::openpa_mix(95001, UPF::openpa_ss_KEEP_ep, 0);
        if (openpa_xmode) openpa_cv1 = 'x; else openpa_cv1 = UPF::openpa_mix(96001, UPF::openpa_dom_PD_UNIT_ep, 0);
        force u_core.acc = openpa_cv1; release u_core.acc;
        openpa_t("SNAP: save and restore active together (SAV_RES_COR): register and retained value corrupted");
      end
      openpa_PD_UNIT_SNAP_both = 1'b1;
    end else openpa_PD_UNIT_SNAP_both = 1'b0;
  end
  always @(negedge UPF::openpa_ss_KEEP_ok) if (!UPF::openpa_ss_KEEP_ok) begin
    if (openpa_PD_UNIT_SNAP_valid) openpa_check(6, "set_retention SNAP: retention supply KEEP lost while holding saved state");
    openpa_PD_UNIT_SNAP_valid = 1'b0;
    openpa_PD_UNIT_SNAP_sh0 = openpa_xmode ? 'x : UPF::openpa_mix(50001, UPF::openpa_ss_KEEP_ep, 0);
    if (openpa_xmode) openpa_cv1 = 'x; else openpa_cv1 = UPF::openpa_mix(70001, UPF::openpa_ss_KEEP_ep, 0);
    force u_core.acc = openpa_cv1; release u_core.acc;
  end
  always @(posedge u_core.clk) if (openpa_PD_UNIT_SNAP_arm7 && UPF::openpa_dom_PD_UNIT_ok) openpa_PD_UNIT_SNAP_ck7_0 = $realtime;
  always @(negedge UPF::openpa_dom_PD_UNIT_ok) if (!UPF::openpa_dom_PD_UNIT_ok && openpa_PD_UNIT_SNAP_arm7) begin
    if (openpa_PD_UNIT_SNAP_ck7_0 > openpa_PD_UNIT_SNAP_st7 && openpa_PD_UNIT_SNAP_ck7_0 < $realtime) openpa_check(7, $sformatf("set_retention SNAP: u_core.clk had an active edge at %0t, after the save and before PD_UNIT went off; the retained state may differ from the saved one (lesson.upf:22)", openpa_PD_UNIT_SNAP_ck7_0));
    openpa_PD_UNIT_SNAP_arm7 = 1'b0;
  end
  always @(posedge UPF::openpa_dom_PD_UNIT_ok) openpa_PD_UNIT_await = openpa_PD_UNIT_SNAP_valid;
  // crossing 1: sleep_soc input clk (PD_TOP) -> u_core/clk (PD_UNIT), not isolated
  bit openpa_x1_t;
  always @(UPF::openpa_dom_PD_TOP_ok or UPF::openpa_dom_PD_UNIT_ok or UPF::openpa_net_HOST_N or UPF::openpa_net_GND_N) openpa_x1_t = !UPF::openpa_dom_PD_TOP_ok && UPF::openpa_dom_PD_UNIT_ok && !(1'b0) && !(UPF::openpa_net_HOST_N.state == UPF::UNDETERMINED || UPF::openpa_net_GND_N.state == UPF::UNDETERMINED);
  always @(posedge openpa_x1_t)
    begin openpa_sp[4] = 1'b1; openpa_sreq = 1'b1; end
  // crossing 2: sleep_soc input d (PD_TOP) -> u_core/d (PD_UNIT), not isolated
  bit openpa_x2_t;
  always @(UPF::openpa_dom_PD_TOP_ok or UPF::openpa_dom_PD_UNIT_ok or UPF::openpa_net_HOST_N or UPF::openpa_net_GND_N) openpa_x2_t = !UPF::openpa_dom_PD_TOP_ok && UPF::openpa_dom_PD_UNIT_ok && !(1'b0) && !(UPF::openpa_net_HOST_N.state == UPF::UNDETERMINED || UPF::openpa_net_GND_N.state == UPF::UNDETERMINED);
  always @(posedge openpa_x2_t)
    begin openpa_sp[5] = 1'b1; openpa_sreq = 1'b1; end
  // level shifter check 1: PD_TOP -> PD_UNIT (no level shifter strategy, threshold 0)
  bit openpa_l1_t;
  always @(UPF::openpa_dom_PD_TOP_ok or UPF::openpa_dom_PD_UNIT_ok or UPF::openpa_net_HOST_N or UPF::openpa_net_UNIT_N) openpa_l1_t = UPF::openpa_dom_PD_TOP_ok && UPF::openpa_dom_PD_UNIT_ok && (UPF::openpa_net_HOST_N.voltage - UPF::openpa_net_UNIT_N.voltage > 0 || UPF::openpa_net_UNIT_N.voltage - UPF::openpa_net_HOST_N.voltage > 0) && !1'b0;
  always @(posedge openpa_l1_t)
    begin openpa_sp[6] = 1'b1; openpa_sreq = 1'b1; end
  always @(negedge UPF::openpa_dom_PD_TOP_ok) if (!UPF::openpa_dom_PD_TOP_ok)
    begin openpa_sp[7] = 1'b1; openpa_sreq = 1'b1; end
  always @(negedge UPF::openpa_dom_PD_TOP_ok) if (!UPF::openpa_dom_PD_TOP_ok)
    begin openpa_sp[8] = 1'b1; openpa_sreq = 1'b1; end

  // Icarus 12 runs no named block inside a final block, so the report is a function.
  function automatic void openpa_cover_report();
      int hs, ht, hq;
      hs = 0; ht = 0; hq = 0;
      $display("OPENPA-COVER sequence D4 SNAP: save while PD_UNIT is on %0d", openpa_seq[1]);
      $display("OPENPA-COVER sequence D5 SNAP: restore of a saved state after power-up %0d", openpa_seq[2]);
      if (openpa_seq[1]) hq = hq + 1;
      if (openpa_seq[2]) hq = hq + 1;
      $display("OPENPA-COVER-SUMMARY states=%0d/0 declared_transitions=%0d/0 sequences=%0d/2", hs, ht, hq);
  endfunction

  // ---- settle process: 8 deferred check(s), run after one #0 in a fixed order
  initial forever begin
    wait (openpa_sreq && openpa_kick);
    #0;
    openpa_sreq = 1'b0;
    if (openpa_sp[1]) begin
      openpa_sp[1] = 1'b0;
          if (!UPF::openpa_dom_PD_UNIT_ok) openpa_check(4, "set_retention SNAP: save while PD_UNIT is off; the saved value is corrupt (lesson.upf:22)");
          else openpa_seq[1] = openpa_seq[1] + 1;
          if (UPF::openpa_ss_KEEP_ok) begin
            openpa_PD_UNIT_SNAP_sh0 = u_core.acc;
            openpa_PD_UNIT_SNAP_valid = 1'b1; openpa_t("SNAP save");
            openpa_PD_UNIT_SNAP_arm7 = UPF::openpa_dom_PD_UNIT_ok;
            openpa_PD_UNIT_SNAP_st7 = $realtime;
          end
    end
    if (openpa_sp[2]) begin
      openpa_sp[2] = 1'b0;
          if (!UPF::openpa_dom_PD_UNIT_ok) begin
            openpa_check(5, "set_retention SNAP: restore while PD_UNIT is off; the retained value is corrupted (lesson.upf:22)");
            openpa_PD_UNIT_SNAP_sh0 = openpa_xmode ? 'x : UPF::openpa_mix(90001, UPF::openpa_ss_KEEP_ep, 0);
          end
          else if (!openpa_PD_UNIT_SNAP_valid) openpa_check(5, "set_retention SNAP: restore with no saved state (lesson.upf:22)");
          else begin
            if (UPF::openpa_dom_PD_UNIT_sim == 4) begin
              if (openpa_xmode) openpa_cv1 = 'x; else openpa_cv1 = UPF::openpa_mix(80001, UPF::openpa_dom_PD_UNIT_ep, 0);
              force u_core.acc = openpa_cv1; release u_core.acc;
              openpa_t("SNAP restore in CORRUPT_ON_ACTIVITY: registers corrupted");
            end else begin
              force u_core.acc = openpa_PD_UNIT_SNAP_sh0;
            end
            openpa_PD_UNIT_await = 1'b0; openpa_t("SNAP restore"); openpa_seq[2] = openpa_seq[2] + 1;
            openpa_PD_UNIT_SNAP_arm7 = 1'b0;
          end
    end
    if (openpa_sp[3]) begin
      openpa_sp[3] = 1'b0;
        if (UPF::openpa_dom_PD_UNIT_ok && UPF::openpa_ctl_1_recall_n === 1'b0 && openpa_PD_UNIT_SNAP_valid) begin
          if (UPF::openpa_dom_PD_UNIT_sim == 4) begin
            if (openpa_xmode) openpa_cv1 = 'x; else openpa_cv1 = UPF::openpa_mix(80001, UPF::openpa_dom_PD_UNIT_ep, 0);
            force u_core.acc = openpa_cv1; release u_core.acc;
            openpa_t("SNAP restore in CORRUPT_ON_ACTIVITY: registers corrupted");
          end else begin
            force u_core.acc = openpa_PD_UNIT_SNAP_sh0;
          end
          openpa_PD_UNIT_await = 1'b0; openpa_t("SNAP restore at power-up (level restore active)"); openpa_seq[2] = openpa_seq[2] + 1;
        end
    end
    if (openpa_sp[4]) begin
      openpa_sp[4] = 1'b0;
      if (openpa_x1_t) openpa_check(9, "sleep_soc input clk of PD_TOP (off) is read by u_core/clk in PD_UNIT (on) with no active isolation");
    end
    if (openpa_sp[5]) begin
      openpa_sp[5] = 1'b0;
      if (openpa_x2_t) openpa_check(9, "sleep_soc input d of PD_TOP (off) is read by u_core/d in PD_UNIT (on) with no active isolation");
    end
    if (openpa_sp[6]) begin
      openpa_sp[6] = 1'b0;
      if (openpa_l1_t) openpa_check(11, $sformatf("PD_TOP (%0d uV) drives PD_UNIT (%0d uV) on 2 port(s) with no level shifter for this direction", UPF::openpa_net_HOST_N.voltage, UPF::openpa_net_UNIT_N.voltage));
    end
    if (openpa_sp[7]) begin
      openpa_sp[7] = 1'b0;
      if (!UPF::openpa_dom_PD_TOP_ok && (UPF::openpa_dom_PD_UNIT_ok)) openpa_check(12, "store (retention SNAP save) comes from PD_TOP, which is off while other domains are on");
    end
    if (openpa_sp[8]) begin
      openpa_sp[8] = 1'b0;
      if (!UPF::openpa_dom_PD_TOP_ok && (UPF::openpa_dom_PD_UNIT_ok)) openpa_check(12, "recall_n (retention SNAP restore) comes from PD_TOP, which is off while other domains are on");
    end
  end
  final begin
    $display("OPENPA-SUMMARY D1=%0d D2=%0d D3=%0d D4=%0d D5=%0d D6=%0d D7=%0d D8=%0d D9=%0d D10=%0d D11=%0d D12=%0d D13=%0d D14=%0d", openpa_n[1], openpa_n[2], openpa_n[3], openpa_n[4], openpa_n[5], openpa_n[6], openpa_n[7], openpa_n[8], openpa_n[9], openpa_n[10], openpa_n[11], openpa_n[12], openpa_n[13], openpa_n[14]);
    openpa_cover_report();
    if (!UPF::openpa_armed) $display("OPENPA-WARNING no supply was driven (supply_on/supply_off never called): every domain stayed CORRUPT");
  end
  // verilator lint_on MULTIDRIVEN
`line 11 "bank.sv" 0
endmodule

Check Yourself

Predict the outputWhat does edge recall print after the two writes and the restart?
`timescale 1ns/1ps
module tb;
  import UPF::*;
  logic clk=0, store=0, recall_n=1, busy=0;
  logic [7:0] d=0;
  sleep_soc dut(.*);
  task write_word(input logic [7:0] word);
    d=word; #1 clk=1; #1 clk=0; #1;
  endtask
  task show(input string label);
    $display("%s acc=%02h tag=%02h scratch=%02h", label, dut.u_core.acc, dut.u_core.tag, dut.u_core.scratch);
  endtask
  initial begin
    void'(supply_on("VHOST",1.0));
    void'(supply_on("VUNIT",1.0));
    void'(supply_on("VKEEP",1.0));
    void'(supply_on("VSS",0.0));
    #4;
    write_word(8'h18); store=1; #1; show("edge capture");
    write_word(8'h2c); store=0; #1; show("later write");
    void'(supply_off("VUNIT")); #2; show("asleep");
    void'(supply_on("VUNIT",1.0)); #2; write_word(0); recall_n=0; #1; show("edge recall");
    recall_n=1; #1;
busy=1; #1; store=1; #1; recall_n=0; #1;
    busy=0; store=0; recall_n=1; #1; show("overlap trial");
    #2; $finish;
  end
endmodule
Read included file: pa_sleep_soc.sv
// pa_sleep_soc.sv: power-aware copy of sleep_soc, generated by OpenPA from lesson.upf.
// Do not edit: regenerate. Simulators: Icarus 12 (4-state, X corruption) or Verilator 5 with --timing (2-state).
// Build with -Wno-fatal on Verilator: forced registers raise MULTIDRIVEN warnings.
`timescale 1ns/1ps
// ---------------------------------------------------------------------------
// package UPF: generated by OpenPA. Supply types, supply_on/supply_off and the power
// state of the design. Names starting with openpa_ are internal.
// ---------------------------------------------------------------------------
package UPF;
  typedef string upfStringT; typedef int upfIntegerT; typedef bit upfBooleanT; typedef logic upfLogicT; typedef real upfRealT; typedef int upfHandleT;
  typedef enum { OFF, FULL_ON, PARTIAL_ON, UNDETERMINED } upfSupplyStateE;
  typedef struct packed { upfSupplyStateE state; int voltage; } upfSupplyTypeT;  // voltage in microvolts
  typedef upfSupplyTypeT supply_net_type;
  bit openpa_armed;        // set by the first call that drives a supply
  bit openpa_kick;         // pulsed once at time 0 to evaluate everything
  bit openpa_xmode;        // 1: corrupt to X; 0: seeded 2-state values
  int unsigned openpa_seed = 1;
  int openpa_cmode;        // 2-state corruption: 0 random, 1 all zero, 2 all one
  supply_net_type openpa_port_VHOST;   // supply port VHOST (lesson.upf:5)
  supply_net_type openpa_port_VUNIT;   // supply port VUNIT (lesson.upf:8)
  supply_net_type openpa_port_VKEEP;   // supply port VKEEP (lesson.upf:11)
  supply_net_type openpa_port_VSS;   // supply port VSS (lesson.upf:14)
  supply_net_type openpa_net_HOST_N;   // supply net HOST_N
  supply_net_type openpa_net_UNIT_N;   // supply net UNIT_N
  supply_net_type openpa_net_KEEP_N;   // supply net KEEP_N
  supply_net_type openpa_net_GND_N;   // supply net GND_N
  bit openpa_ss_HOST_ok; int unsigned openpa_ss_HOST_ep; int openpa_ss_HOST_st = -1; bit openpa_ss_HOST_multi; int openpa_ss_HOST_sim = 5;   // supply set HOST
  bit openpa_ss_UNIT_ok; int unsigned openpa_ss_UNIT_ep; int openpa_ss_UNIT_st = -1; bit openpa_ss_UNIT_multi; int openpa_ss_UNIT_sim = 5;   // supply set UNIT
  bit openpa_ss_KEEP_ok; int unsigned openpa_ss_KEEP_ep; int openpa_ss_KEEP_st = -1; bit openpa_ss_KEEP_multi; int openpa_ss_KEEP_sim = 5;   // supply set KEEP
  bit openpa_dom_PD_TOP_ok; int unsigned openpa_dom_PD_TOP_ep; int openpa_dom_PD_TOP_sim = 5;   // power domain PD_TOP: ok = powered (simstate not CORRUPT); sim = simstate code (lesson.upf:3)
  bit openpa_dom_PD_UNIT_ok; int unsigned openpa_dom_PD_UNIT_ep; int openpa_dom_PD_UNIT_sim = 5;   // power domain PD_UNIT: ok = powered (simstate not CORRUPT); sim = simstate code (lesson.upf:4)
  // simstate codes, by IEEE 1801-2024 4.8 precedence: 0 NORMAL, 1 CORRUPT_STATE_ON_CHANGE, 2 CORRUPT_STATE_ON_ACTIVITY,
  // 3 CORRUPT_ON_CHANGE, 4 CORRUPT_ON_ACTIVITY, 5 CORRUPT (and NOT_NORMAL, 9.5.8); README "Simstates"
  logic openpa_ctl_0_store;   // copy of store (retention EVENT save)
  logic openpa_ctl_1_recall_n;   // copy of recall_n (retention EVENT restore)
  logic openpa_ctl_2_busy;   // copy of busy (retention EVENT -assert_r_mutex; retention EVENT -assert_s_mutex)
  function automatic logic [63:0] openpa_mix(int unsigned id, int unsigned ep, int unsigned lane);
    logic [63:0] z;
    if (openpa_cmode == 1) return 64'd0;
    if (openpa_cmode == 2) return ~64'd0;
    z = {32'd0, openpa_seed} * 64'h9E3779B97F4A7C15 + {32'd0, id} * 64'hC2B2AE3D27D4EB4F + {32'd0, ep} * 64'h165667B19E3779F9 + {32'd0, lane} * 64'hD6E8FEB86659FD93;
    z = (z ^ (z >> 30)) * 64'hBF58476D1CE4E5B9;
    z = (z ^ (z >> 27)) * 64'h94D049BB133111EB;
    return z ^ (z >> 31);
  endfunction
  function automatic bit openpa_bit(int unsigned id, int unsigned ep); logic [63:0] z; z = openpa_mix(id, ep, 0); return z[0]; endfunction
  function automatic int openpa_uv(real v); return $rtoi(v * 1.0e6 + (v < 0.0 ? -0.5 : 0.5)); endfunction
  // supply_on: FULL_ON at the given voltage (11.2.4; default 1.0 V)
  function automatic upfBooleanT supply_on(string pad_name, real value = 1.0);
    openpa_armed = 1;
    if (pad_name == "VHOST") begin openpa_port_VHOST.state = FULL_ON; openpa_port_VHOST.voltage = openpa_uv(value); return 1; end
    if (pad_name == "VUNIT") begin openpa_port_VUNIT.state = FULL_ON; openpa_port_VUNIT.voltage = openpa_uv(value); return 1; end
    if (pad_name == "VKEEP") begin openpa_port_VKEEP.state = FULL_ON; openpa_port_VKEEP.voltage = openpa_uv(value); return 1; end
    if (pad_name == "VSS") begin openpa_port_VSS.state = FULL_ON; openpa_port_VSS.voltage = openpa_uv(value); return 1; end
    if (pad_name == "HOST_N") begin $display("OPENPA-ERROR supply_on(\"%s\"): HOST_N already has a driver (port VHOST); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "HOST.power") begin $display("OPENPA-ERROR supply_on(\"%s\"): HOST.power already has a driver (port VHOST); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "PD_TOP.primary.power") begin $display("OPENPA-ERROR supply_on(\"%s\"): PD_TOP.primary.power already has a driver (port VHOST); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "UNIT_N") begin $display("OPENPA-ERROR supply_on(\"%s\"): UNIT_N already has a driver (port VUNIT); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "UNIT.power") begin $display("OPENPA-ERROR supply_on(\"%s\"): UNIT.power already has a driver (port VUNIT); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "PD_UNIT.primary.power") begin $display("OPENPA-ERROR supply_on(\"%s\"): PD_UNIT.primary.power already has a driver (port VUNIT); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "KEEP_N") begin $display("OPENPA-ERROR supply_on(\"%s\"): KEEP_N already has a driver (port VKEEP); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "KEEP.power") begin $display("OPENPA-ERROR supply_on(\"%s\"): KEEP.power already has a driver (port VKEEP); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "GND_N") begin $display("OPENPA-ERROR supply_on(\"%s\"): GND_N already has a driver (port VSS); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "HOST.ground") begin $display("OPENPA-ERROR supply_on(\"%s\"): HOST.ground already has a driver (port VSS); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "UNIT.ground") begin $display("OPENPA-ERROR supply_on(\"%s\"): UNIT.ground already has a driver (port VSS); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "KEEP.ground") begin $display("OPENPA-ERROR supply_on(\"%s\"): KEEP.ground already has a driver (port VSS); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "PD_TOP.primary.ground") begin $display("OPENPA-ERROR supply_on(\"%s\"): PD_TOP.primary.ground already has a driver (port VSS); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "PD_UNIT.primary.ground") begin $display("OPENPA-ERROR supply_on(\"%s\"): PD_UNIT.primary.ground already has a driver (port VSS); drive its root supply instead", pad_name); return 0; end
    $display("OPENPA-ERROR supply_on(\"%s\"): no supply port or root supply net of that name (writable: VHOST VUNIT VKEEP VSS)", pad_name);
    return 0;
  endfunction
  // supply_off: OFF, voltage unchanged (9.3.1); a second argument is accepted and ignored
  function automatic upfBooleanT supply_off(string pad_name, real value = 0.0);
    openpa_armed = 1;
    if (pad_name == "VHOST") begin openpa_port_VHOST.state = OFF; return 1; end
    if (pad_name == "VUNIT") begin openpa_port_VUNIT.state = OFF; return 1; end
    if (pad_name == "VKEEP") begin openpa_port_VKEEP.state = OFF; return 1; end
    if (pad_name == "VSS") begin openpa_port_VSS.state = OFF; return 1; end
    if (pad_name == "HOST_N") begin $display("OPENPA-ERROR supply_off(\"%s\"): HOST_N already has a driver (port VHOST); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "HOST.power") begin $display("OPENPA-ERROR supply_off(\"%s\"): HOST.power already has a driver (port VHOST); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "PD_TOP.primary.power") begin $display("OPENPA-ERROR supply_off(\"%s\"): PD_TOP.primary.power already has a driver (port VHOST); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "UNIT_N") begin $display("OPENPA-ERROR supply_off(\"%s\"): UNIT_N already has a driver (port VUNIT); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "UNIT.power") begin $display("OPENPA-ERROR supply_off(\"%s\"): UNIT.power already has a driver (port VUNIT); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "PD_UNIT.primary.power") begin $display("OPENPA-ERROR supply_off(\"%s\"): PD_UNIT.primary.power already has a driver (port VUNIT); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "KEEP_N") begin $display("OPENPA-ERROR supply_off(\"%s\"): KEEP_N already has a driver (port VKEEP); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "KEEP.power") begin $display("OPENPA-ERROR supply_off(\"%s\"): KEEP.power already has a driver (port VKEEP); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "GND_N") begin $display("OPENPA-ERROR supply_off(\"%s\"): GND_N already has a driver (port VSS); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "HOST.ground") begin $display("OPENPA-ERROR supply_off(\"%s\"): HOST.ground already has a driver (port VSS); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "UNIT.ground") begin $display("OPENPA-ERROR supply_off(\"%s\"): UNIT.ground already has a driver (port VSS); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "KEEP.ground") begin $display("OPENPA-ERROR supply_off(\"%s\"): KEEP.ground already has a driver (port VSS); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "PD_TOP.primary.ground") begin $display("OPENPA-ERROR supply_off(\"%s\"): PD_TOP.primary.ground already has a driver (port VSS); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "PD_UNIT.primary.ground") begin $display("OPENPA-ERROR supply_off(\"%s\"): PD_UNIT.primary.ground already has a driver (port VSS); drive its root supply instead", pad_name); return 0; end
    $display("OPENPA-ERROR supply_off(\"%s\"): no supply port or root supply net of that name (writable: VHOST VUNIT VKEEP VSS)", pad_name);
    return 0;
  endfunction
  // supply_partial_on: PARTIAL_ON at the given voltage (11.2.4)
  function automatic upfBooleanT supply_partial_on(string pad_name, real value = 1.0);
    openpa_armed = 1;
    if (pad_name == "VHOST") begin openpa_port_VHOST.state = PARTIAL_ON; openpa_port_VHOST.voltage = openpa_uv(value); return 1; end
    if (pad_name == "VUNIT") begin openpa_port_VUNIT.state = PARTIAL_ON; openpa_port_VUNIT.voltage = openpa_uv(value); return 1; end
    if (pad_name == "VKEEP") begin openpa_port_VKEEP.state = PARTIAL_ON; openpa_port_VKEEP.voltage = openpa_uv(value); return 1; end
    if (pad_name == "VSS") begin openpa_port_VSS.state = PARTIAL_ON; openpa_port_VSS.voltage = openpa_uv(value); return 1; end
    if (pad_name == "HOST_N") begin $display("OPENPA-ERROR supply_partial_on(\"%s\"): HOST_N already has a driver (port VHOST); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "HOST.power") begin $display("OPENPA-ERROR supply_partial_on(\"%s\"): HOST.power already has a driver (port VHOST); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "PD_TOP.primary.power") begin $display("OPENPA-ERROR supply_partial_on(\"%s\"): PD_TOP.primary.power already has a driver (port VHOST); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "UNIT_N") begin $display("OPENPA-ERROR supply_partial_on(\"%s\"): UNIT_N already has a driver (port VUNIT); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "UNIT.power") begin $display("OPENPA-ERROR supply_partial_on(\"%s\"): UNIT.power already has a driver (port VUNIT); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "PD_UNIT.primary.power") begin $display("OPENPA-ERROR supply_partial_on(\"%s\"): PD_UNIT.primary.power already has a driver (port VUNIT); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "KEEP_N") begin $display("OPENPA-ERROR supply_partial_on(\"%s\"): KEEP_N already has a driver (port VKEEP); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "KEEP.power") begin $display("OPENPA-ERROR supply_partial_on(\"%s\"): KEEP.power already has a driver (port VKEEP); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "GND_N") begin $display("OPENPA-ERROR supply_partial_on(\"%s\"): GND_N already has a driver (port VSS); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "HOST.ground") begin $display("OPENPA-ERROR supply_partial_on(\"%s\"): HOST.ground already has a driver (port VSS); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "UNIT.ground") begin $display("OPENPA-ERROR supply_partial_on(\"%s\"): UNIT.ground already has a driver (port VSS); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "KEEP.ground") begin $display("OPENPA-ERROR supply_partial_on(\"%s\"): KEEP.ground already has a driver (port VSS); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "PD_TOP.primary.ground") begin $display("OPENPA-ERROR supply_partial_on(\"%s\"): PD_TOP.primary.ground already has a driver (port VSS); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "PD_UNIT.primary.ground") begin $display("OPENPA-ERROR supply_partial_on(\"%s\"): PD_UNIT.primary.ground already has a driver (port VSS); drive its root supply instead", pad_name); return 0; end
    $display("OPENPA-ERROR supply_partial_on(\"%s\"): no supply port or root supply net of that name (writable: VHOST VUNIT VKEEP VSS)", pad_name);
    return 0;
  endfunction
  // set_supply_state: the state only, voltage unchanged (9.3.1)
  function automatic upfBooleanT set_supply_state(string pad_name, upfSupplyStateE supply_state);
    openpa_armed = 1;
    if (pad_name == "VHOST") begin openpa_port_VHOST.state = supply_state; return 1; end
    if (pad_name == "VUNIT") begin openpa_port_VUNIT.state = supply_state; return 1; end
    if (pad_name == "VKEEP") begin openpa_port_VKEEP.state = supply_state; return 1; end
    if (pad_name == "VSS") begin openpa_port_VSS.state = supply_state; return 1; end
    if (pad_name == "HOST_N") begin $display("OPENPA-ERROR set_supply_state(\"%s\"): HOST_N already has a driver (port VHOST); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "HOST.power") begin $display("OPENPA-ERROR set_supply_state(\"%s\"): HOST.power already has a driver (port VHOST); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "PD_TOP.primary.power") begin $display("OPENPA-ERROR set_supply_state(\"%s\"): PD_TOP.primary.power already has a driver (port VHOST); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "UNIT_N") begin $display("OPENPA-ERROR set_supply_state(\"%s\"): UNIT_N already has a driver (port VUNIT); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "UNIT.power") begin $display("OPENPA-ERROR set_supply_state(\"%s\"): UNIT.power already has a driver (port VUNIT); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "PD_UNIT.primary.power") begin $display("OPENPA-ERROR set_supply_state(\"%s\"): PD_UNIT.primary.power already has a driver (port VUNIT); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "KEEP_N") begin $display("OPENPA-ERROR set_supply_state(\"%s\"): KEEP_N already has a driver (port VKEEP); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "KEEP.power") begin $display("OPENPA-ERROR set_supply_state(\"%s\"): KEEP.power already has a driver (port VKEEP); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "GND_N") begin $display("OPENPA-ERROR set_supply_state(\"%s\"): GND_N already has a driver (port VSS); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "HOST.ground") begin $display("OPENPA-ERROR set_supply_state(\"%s\"): HOST.ground already has a driver (port VSS); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "UNIT.ground") begin $display("OPENPA-ERROR set_supply_state(\"%s\"): UNIT.ground already has a driver (port VSS); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "KEEP.ground") begin $display("OPENPA-ERROR set_supply_state(\"%s\"): KEEP.ground already has a driver (port VSS); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "PD_TOP.primary.ground") begin $display("OPENPA-ERROR set_supply_state(\"%s\"): PD_TOP.primary.ground already has a driver (port VSS); drive its root supply instead", pad_name); return 0; end
    if (pad_name == "PD_UNIT.primary.ground") begin $display("OPENPA-ERROR set_supply_state(\"%s\"): PD_UNIT.primary.ground already has a driver (port VSS); drive its root supply instead", pad_name); return 0; end
    $display("OPENPA-ERROR set_supply_state(\"%s\"): no supply port or root supply net of that name (writable: VHOST VUNIT VKEEP VSS)", pad_name);
    return 0;
  endfunction
  // set_power_state (9.3.1) is not supported: OpenPA has no deferred power states. A call is reported, never ignored.
  function automatic upfBooleanT set_power_state(string object_name, string power_state);
    $display("OPENPA-ERROR set_power_state(\"%s\", \"%s\"): not supported by OpenPA; drive the supplies with supply_on, supply_off or set_supply_state", object_name, power_state);
    return 0;
  endfunction
  // Supply queries (11.2.4). Names: supply ports, supply nets and supply set functions as the UPF wrote them.
  function automatic upfSupplyTypeT get_supply_value(string name);
    if (name == "VHOST" || name == "HOST_N" || name == "HOST.power" || name == "PD_TOP.primary.power") return openpa_net_HOST_N;
    if (name == "VUNIT" || name == "UNIT_N" || name == "UNIT.power" || name == "PD_UNIT.primary.power") return openpa_net_UNIT_N;
    if (name == "VKEEP" || name == "KEEP_N" || name == "KEEP.power") return openpa_net_KEEP_N;
    if (name == "VSS" || name == "GND_N" || name == "HOST.ground" || name == "UNIT.ground" || name == "KEEP.ground" || name == "PD_TOP.primary.ground" || name == "PD_UNIT.primary.ground") return openpa_net_GND_N;
    $display("OPENPA-ERROR get_supply_value(\"%s\"): no supply net, port or supply set function of that name", name);
    return 0;
  endfunction
  function automatic upfRealT get_supply_voltage(upfSupplyTypeT arg); return arg.voltage / 1.0e6; endfunction
  // 11.2.4: 1 for FULL_ON; 0 otherwise.
  function automatic upfBooleanT get_supply_on_state(upfSupplyTypeT arg); return arg.state == FULL_ON; endfunction
  function automatic upfSupplyStateE get_supply_state(upfSupplyTypeT arg); return arg.state; endfunction
endpackage
// ---------------------------------------------------------------------------
// OpenPA cells (generated; do not edit)
// ---------------------------------------------------------------------------
// openpa_corrupt: rule C2. Passes d while the driving domain is NORMAL; otherwise drives X (4-state)
// or a seeded value (2-state). The value is computed ahead for the episode the next power-down starts
// (ep + 1 while ok), so at power-down the output goes straight from d to the new value: no delta glitch
// through the previous episode's value (Icarus would turn one into a clock edge).
module openpa_corrupt #(parameter int W = 1, parameter int unsigned ID = 0) (
  input  logic [W-1:0] d,
  output logic [W-1:0] q,
  input  bit           ok,
  input  int unsigned  ep
);
  localparam int L = (W + 63) / 64;
  logic [L*64-1:0] cv;
  always @(ep or ok or UPF::openpa_kick)
    for (int i = 0; i < L; i++) cv[i*64 +: 64] = UPF::openpa_mix(ID, ok ? ep + 1 : ep, i);
  assign q = ok ? d : (UPF::openpa_xmode ? {W{1'bx}} : cv[W-1:0]);
endmodule

// openpa_corrupt_sim: rule C2 for a domain that has intermediate simstates (IEEE 1801-2024 9.5). As
// openpa_corrupt, and also corrupt while lhit is set (CORRUPT_ON_ACTIVITY after input activity), or after d
// changed while coc is set (CORRUPT_ON_CHANGE); leaving CORRUPT_ON_CHANGE clears that.
module openpa_corrupt_sim #(parameter int W = 1, parameter int unsigned ID = 0) (
  input  logic [W-1:0] d,
  output logic [W-1:0] q,
  input  bit           ok,
  input  int unsigned  ep,
  input  bit           lhit,
  input  bit           coc
);
  localparam int L = (W + 63) / 64;
  logic [L*64-1:0] cv;
  bit chg;
  always @(ep or ok or UPF::openpa_kick)
    for (int i = 0; i < L; i++) cv[i*64 +: 64] = UPF::openpa_mix(ID, ok ? ep + 1 : ep, i);
  // verilator lint_off MULTIDRIVEN
  for (genvar i = 0; i < W; i++) begin : g_chg
    always @(posedge d[i] or negedge d[i]) if (coc && ok) chg = 1'b1;
  end
  always @(negedge coc) chg = 1'b0;
  // verilator lint_on MULTIDRIVEN
  assign q = (ok && !lhit && !chg) ? d : (UPF::openpa_xmode ? {W{1'bx}} : cv[W-1:0]);
endmodule

// openpa_iso: rules I1-I4 (IEEE 1801-2024 9.8). CLAMP 0, 1, 2 (latch: the value the input had while the enable
// was inactive) or 3 (Z: high impedance on 4-state; 0 on 2-state backends, which cannot represent Z).
// sup_ok is 1 while the isolation supply is NORMAL (always, for a strategy with no supply); otherwise the output,
// and a latched value, are corrupt. An enable at X or Z corrupts the output and the latched value.
module openpa_iso #(parameter int W = 1, parameter int CLAMP = 0, parameter bit SENSE = 1'b1, parameter int unsigned ID = 0) (
  input  logic [W-1:0] d,
  output logic [W-1:0] q,
  input  logic         en,
  input  bit           sup_ok,
  input  int unsigned  sup_ep
);
  localparam int L = (W + 63) / 64;
  logic [L*64-1:0] cv;
  logic [W-1:0] held;
  always @(sup_ep or sup_ok or UPF::openpa_kick)
    for (int i = 0; i < L; i++) cv[i*64 +: 64] = UPF::openpa_mix(ID + 32'h4000_0000, sup_ok ? sup_ep + 1 : sup_ep, i);
  wire active   = (en === SENSE);
  wire inactive = (en === !SENSE);
  wire [W-1:0] bad    = UPF::openpa_xmode ? {W{1'bx}} : cv[W-1:0];
  if (CLAMP == 2) begin : g_latch
    always @(d or en or sup_ok or bad) if (!sup_ok || !(active || inactive)) held = bad; else if (inactive) held = d;
  end
  wire [W-1:0] clampv = (CLAMP == 3) ? (UPF::openpa_xmode ? {W{1'bz}} : {W{1'b0}}) : (CLAMP == 2) ? held : (CLAMP == 1) ? {W{1'b1}} : {W{1'b0}};
  assign q = !sup_ok ? bad : active ? clampv : inactive ? d : bad;
endmodule

// openpa_ls: rule L1 (IEEE 1801-2024 9.9). Passes d while every supply set of the cell is NORMAL; otherwise corrupt.
module openpa_ls #(parameter int W = 1, parameter int unsigned ID = 0) (
  input  logic [W-1:0] d,
  output logic [W-1:0] q,
  input  bit           src_ok,
  input  bit           dst_ok,
  input  int unsigned  ep
);
  localparam int L = (W + 63) / 64;
  logic [L*64-1:0] cv;
  always @(ep or src_ok or dst_ok or UPF::openpa_kick)
    for (int i = 0; i < L; i++) cv[i*64 +: 64] = UPF::openpa_mix(ID + 32'h8000_0000, (src_ok && dst_ok) ? ep + 1 : ep, i);
  assign q = (src_ok && dst_ok) ? d : (UPF::openpa_xmode ? {W{1'bx}} : cv[W-1:0]);
endmodule

`line 1 "bank.sv" 0
module bank(input logic clk, input logic [7:0] d);
  logic [7:0] acc, tag, scratch;
  always_ff @(posedge clk) begin
    acc <= d;
    tag <= ~d;
    scratch <= d + 1'b1;
  end
endmodule
module sleep_soc(input logic clk, input logic [7:0] d, input logic store, recall_n, busy);
  wire [0:0] openpa_u_core_clk_s1; wire [7:0] openpa_u_core_d_s1; bank u_core(.clk(openpa_u_core_clk_s1), .d(openpa_u_core_d_s1)); openpa_corrupt #(.W(1), .ID(268435456)) openpa_u_core_clk_cor (.d(clk), .q(openpa_u_core_clk_s1), .ok(UPF::openpa_dom_PD_TOP_ok), .ep(UPF::openpa_dom_PD_TOP_ep)); openpa_corrupt #(.W(8), .ID(268435464)) openpa_u_core_d_cor (.d(d), .q(openpa_u_core_d_s1), .ok(UPF::openpa_dom_PD_TOP_ok), .ep(UPF::openpa_dom_PD_TOP_ep));

`line 1 "openpa_overlay.sv" 0
  // ===================================================================================
  // OpenPA overlay for sleep_soc: generated from lesson.upf. Do not edit.
  // ===================================================================================
  // verilator lint_off MULTIDRIVEN
  // verilator lint_off ENUMVALUE
  // verilator lint_off WIDTHTRUNC
  // verilator lint_off WIDTHEXPAND
  // verilator lint_off BLKSEQ
  // verilator lint_off IGNOREDRETURN
  import UPF::openpa_cmode;
  import UPF::openpa_ctl_0_store;
  import UPF::openpa_ctl_1_recall_n;
  import UPF::openpa_ctl_2_busy;
  import UPF::openpa_dom_PD_TOP_ep;
  import UPF::openpa_dom_PD_TOP_ok;
  import UPF::openpa_dom_PD_TOP_sim;
  import UPF::openpa_dom_PD_UNIT_ep;
  import UPF::openpa_dom_PD_UNIT_ok;
  import UPF::openpa_dom_PD_UNIT_sim;
  import UPF::openpa_kick;
  import UPF::openpa_net_GND_N;
  import UPF::openpa_net_HOST_N;
  import UPF::openpa_net_KEEP_N;
  import UPF::openpa_net_UNIT_N;
  import UPF::openpa_seed;
  import UPF::openpa_ss_HOST_ep;
  import UPF::openpa_ss_HOST_multi;
  import UPF::openpa_ss_HOST_ok;
  import UPF::openpa_ss_HOST_sim;
  import UPF::openpa_ss_HOST_st;
  import UPF::openpa_ss_KEEP_ep;
  import UPF::openpa_ss_KEEP_multi;
  import UPF::openpa_ss_KEEP_ok;
  import UPF::openpa_ss_KEEP_sim;
  import UPF::openpa_ss_KEEP_st;
  import UPF::openpa_ss_UNIT_ep;
  import UPF::openpa_ss_UNIT_multi;
  import UPF::openpa_ss_UNIT_ok;
  import UPF::openpa_ss_UNIT_sim;
  import UPF::openpa_ss_UNIT_st;
  import UPF::openpa_xmode;
  // ---- mode, seed, trace
  bit openpa_tr, openpa_obs;
  int openpa_n [1:14];
  int openpa_seq [1:2];   // good-form sequence bins (coverage)
  bit openpa_sreq; bit openpa_sp [1:8];   // deferred checks: request and pending flags
  string openpa_mode_arg;
  initial begin
  `ifdef VERILATOR
    openpa_xmode = 1'b0;
  `else
    openpa_xmode = 1'b1;
  `endif
    openpa_cmode = 0;
    if ($value$plusargs("openpa_corrupt=%s", openpa_mode_arg)) begin
      if (openpa_mode_arg == "x") openpa_xmode = 1'b1;
      else if (openpa_mode_arg == "random") openpa_xmode = 1'b0;
      else if (openpa_mode_arg == "zero") begin openpa_xmode = 1'b0; openpa_cmode = 1; end
      else if (openpa_mode_arg == "one") begin openpa_xmode = 1'b0; openpa_cmode = 2; end
      else $display("OPENPA-ERROR +openpa_corrupt=%s: use x, random, zero or one", openpa_mode_arg);
    end
    if (!$value$plusargs("openpa_seed=%d", openpa_seed)) openpa_seed = 1;
    openpa_tr = $test$plusargs("openpa_trace");
    openpa_obs = $test$plusargs("openpa_obs");
    $display("OPENPA-INFO corruption=%0s seed=%0d", openpa_xmode ? "x" : (openpa_cmode == 1 ? "zero" : openpa_cmode == 2 ? "one" : "random"), openpa_seed);
    #0 openpa_kick = 1'b1;
  end
  task automatic openpa_check(input int id, input string msg);
    openpa_n[id] = openpa_n[id] + 1;
    $display("OPENPA-CHECK D%0d @ %0t: %s", id, $time, msg);
  endtask
  task automatic openpa_t(input string msg);
    if (openpa_tr) $display("OPENPA-TRACE @ %0t: %s", $time, msg);
  endtask

  // ---- copies of the design signals the UPF names (4-state, so X is visible)
  always @(store or openpa_kick) openpa_ctl_0_store = store;
  always @(recall_n or openpa_kick) openpa_ctl_1_recall_n = recall_n;
  always @(busy or openpa_kick) openpa_ctl_2_busy = busy;

  // ---- supply network, switches, supply sets, simstates, power states (one block, evaluated in order)
  always @(openpa_kick or UPF::openpa_port_VHOST or UPF::openpa_port_VUNIT or UPF::openpa_port_VKEEP or UPF::openpa_port_VSS) begin : openpa_network
    openpa_net_HOST_N = UPF::openpa_port_VHOST;   // HOST_N <- port VHOST
    openpa_net_UNIT_N = UPF::openpa_port_VUNIT;   // UNIT_N <- port VUNIT
    openpa_net_KEEP_N = UPF::openpa_port_VKEEP;   // KEEP_N <- port VKEEP
    openpa_net_GND_N = UPF::openpa_port_VSS;   // GND_N <- port VSS
    // supply set HOST (also PD_TOP.primary): IEEE 1801-2024 4.7.3, 4.7.4, 6.5.6, 9.3.2, 5.7.8
    begin : openpa_ss_HOST
      bit nx; int sm;
      sm = (UPF::openpa_net_HOST_N.state == UPF::FULL_ON && UPF::openpa_net_GND_N.state == UPF::FULL_ON) ? 0 : 5;   // predefined ON (NORMAL) or OFF (CORRUPT), IEEE 1801-2024 4.7.4
      openpa_ss_HOST_sim = sm;
      nx = sm < 5;
      if (openpa_ss_HOST_ok && !nx) openpa_ss_HOST_ep = openpa_ss_HOST_ep + 1;
      openpa_ss_HOST_ok = nx;
    end
    // supply set UNIT (also PD_UNIT.primary): IEEE 1801-2024 4.7.3, 4.7.4, 6.5.6, 9.3.2, 5.7.8
    begin : openpa_ss_UNIT
      bit nx; int sm;
      sm = (UPF::openpa_net_UNIT_N.state == UPF::FULL_ON && UPF::openpa_net_GND_N.state == UPF::FULL_ON) ? 0 : 5;   // predefined ON (NORMAL) or OFF (CORRUPT), IEEE 1801-2024 4.7.4
      openpa_ss_UNIT_sim = sm;
      nx = sm < 5;
      if (openpa_ss_UNIT_ok && !nx) openpa_ss_UNIT_ep = openpa_ss_UNIT_ep + 1;
      openpa_ss_UNIT_ok = nx;
    end
    // supply set KEEP: IEEE 1801-2024 4.7.3, 4.7.4, 6.5.6, 9.3.2, 5.7.8
    begin : openpa_ss_KEEP
      bit nx; int sm;
      sm = (UPF::openpa_net_KEEP_N.state == UPF::FULL_ON && UPF::openpa_net_GND_N.state == UPF::FULL_ON) ? 0 : 5;   // predefined ON (NORMAL) or OFF (CORRUPT), IEEE 1801-2024 4.7.4
      openpa_ss_KEEP_sim = sm;
      nx = sm < 5;
      if (openpa_ss_KEEP_ok && !nx) openpa_ss_KEEP_ep = openpa_ss_KEEP_ep + 1;
      openpa_ss_KEEP_ok = nx;
    end
    // domain PD_TOP: simstate of its primary supply set HOST
    if (openpa_dom_PD_TOP_ok && !UPF::openpa_ss_HOST_ok) openpa_dom_PD_TOP_ep = openpa_dom_PD_TOP_ep + 1;
    openpa_dom_PD_TOP_ok = UPF::openpa_ss_HOST_ok;
    openpa_dom_PD_TOP_sim = openpa_ss_HOST_sim;
    // domain PD_UNIT: simstate of its primary supply set UNIT
    if (openpa_dom_PD_UNIT_ok && !UPF::openpa_ss_UNIT_ok) openpa_dom_PD_UNIT_ep = openpa_dom_PD_UNIT_ep + 1;
    openpa_dom_PD_UNIT_ok = UPF::openpa_ss_UNIT_ok;
    openpa_dom_PD_UNIT_sim = openpa_ss_UNIT_sim;
  end

  // ---- corruption: registers forced, memories overwritten (rules C1, C3)
  bit openpa_PD_UNIT_await;   // PD_UNIT powered up with saved state not yet restored (D3)
  // domain PD_TOP (lesson.upf:3): 0 register(s), 0 memory(ies)
  bit openpa_PD_TOP_applied, openpa_PD_TOP_valid;
  // The first evaluation counts as a power-up from OFF: registers take the corruption value even when the
  // supplies are already on at time 0 (rule C3), so 4-state and 2-state runs start alike.
  always @(UPF::openpa_dom_PD_TOP_ok or openpa_kick) if (!openpa_PD_TOP_valid || openpa_PD_TOP_applied != UPF::openpa_dom_PD_TOP_ok) begin
    openpa_t({"PD_TOP ", UPF::openpa_dom_PD_TOP_ok ? "NORMAL" : "CORRUPT"});
    if (!UPF::openpa_dom_PD_TOP_ok || !openpa_PD_TOP_valid) begin
    end
    if (UPF::openpa_dom_PD_TOP_ok) begin
    end
    openpa_PD_TOP_valid = 1'b1; openpa_PD_TOP_applied = UPF::openpa_dom_PD_TOP_ok;
  end
  // domain PD_UNIT (lesson.upf:4): 3 register(s), 0 memory(ies)
  bit openpa_PD_UNIT_applied, openpa_PD_UNIT_valid;
  logic [7:0] openpa_cv1;   // corruption value for u_core.acc
  logic [7:0] openpa_cv2;   // corruption value for u_core.scratch
  logic [7:0] openpa_cv3;   // corruption value for u_core.tag
  // The first evaluation counts as a power-up from OFF: registers take the corruption value even when the
  // supplies are already on at time 0 (rule C3), so 4-state and 2-state runs start alike.
  always @(UPF::openpa_dom_PD_UNIT_ok or openpa_kick) if (!openpa_PD_UNIT_valid || openpa_PD_UNIT_applied != UPF::openpa_dom_PD_UNIT_ok) begin
    openpa_t({"PD_UNIT ", UPF::openpa_dom_PD_UNIT_ok ? "NORMAL" : "CORRUPT"});
    if (!UPF::openpa_dom_PD_UNIT_ok || !openpa_PD_UNIT_valid) begin
      if (openpa_xmode) openpa_cv1 = 'x; else openpa_cv1 = UPF::openpa_mix(1, UPF::openpa_dom_PD_UNIT_ep, 0);
      force u_core.acc = openpa_cv1;
      if (openpa_xmode) openpa_cv2 = 'x; else openpa_cv2 = UPF::openpa_mix(2, UPF::openpa_dom_PD_UNIT_ep, 0);
      force u_core.scratch = openpa_cv2;
      if (openpa_xmode) openpa_cv3 = 'x; else openpa_cv3 = UPF::openpa_mix(3, UPF::openpa_dom_PD_UNIT_ep, 0);
      force u_core.tag = openpa_cv3;
    end
    if (UPF::openpa_dom_PD_UNIT_ok) begin
      release u_core.acc;
      release u_core.scratch;
      release u_core.tag;
    end
    openpa_PD_UNIT_valid = 1'b1; openpa_PD_UNIT_applied = UPF::openpa_dom_PD_UNIT_ok;
  end

  // ---- set_retention EVENT (lesson.upf:23): u_core.tag, u_core.acc (rules R1-R4)
  bit openpa_PD_UNIT_EVENT_valid; logic openpa_PD_UNIT_EVENT_sv, openpa_PD_UNIT_EVENT_rv;
  bit openpa_PD_UNIT_EVENT_arm7; realtime openpa_PD_UNIT_EVENT_st7, openpa_PD_UNIT_EVENT_ck7_0 = -1.0;
  logic [7:0] openpa_PD_UNIT_EVENT_sh0;   // retained value of u_core.tag, on KEEP
  logic [7:0] openpa_PD_UNIT_EVENT_sh1;   // retained value of u_core.acc, on KEEP
  always @(posedge UPF::openpa_ctl_0_store or negedge UPF::openpa_ctl_0_store) begin
    if ($isunknown(openpa_ctl_0_store)) begin   // a save signal at X or Z: the retained value is corrupt (UPF 1.0 5.3)
      openpa_PD_UNIT_EVENT_sh0 = openpa_xmode ? 'x : UPF::openpa_mix(60003, UPF::openpa_ss_KEEP_ep, 0);
      openpa_PD_UNIT_EVENT_sh1 = openpa_xmode ? 'x : UPF::openpa_mix(60001, UPF::openpa_ss_KEEP_ep, 0);
      if (UPF::openpa_ss_KEEP_ok) openpa_PD_UNIT_EVENT_valid = 1'b1;   // the shadow holds a (corrupt) saved value
      openpa_check(4, "set_retention EVENT: save signal store is X or Z; the shadow is corrupt (lesson.upf:23)");
    end
    else if (openpa_PD_UNIT_EVENT_sv === 1'b0 && UPF::openpa_ctl_0_store === 1'b1)
      begin openpa_sp[1] = 1'b1; openpa_sreq = 1'b1; end
    openpa_PD_UNIT_EVENT_sv = UPF::openpa_ctl_0_store;
  end
  always @(posedge UPF::openpa_ctl_1_recall_n or negedge UPF::openpa_ctl_1_recall_n) begin
    if ($isunknown(openpa_ctl_1_recall_n)) begin   // a restore signal at X or Z: the register is corrupt (UPF 1.0 5.3)
      if (openpa_xmode) openpa_cv3 = 'x; else openpa_cv3 = UPF::openpa_mix(3, UPF::openpa_dom_PD_UNIT_ep, 0);
      force u_core.tag = openpa_cv3; release u_core.tag;
      if (openpa_xmode) openpa_cv1 = 'x; else openpa_cv1 = UPF::openpa_mix(1, UPF::openpa_dom_PD_UNIT_ep, 0);
      force u_core.acc = openpa_cv1; release u_core.acc;
      openpa_check(5, "set_retention EVENT: restore signal recall_n is X or Z; the registers are corrupt (lesson.upf:23)");
    end
    else if (openpa_PD_UNIT_EVENT_rv === 1'b1 && UPF::openpa_ctl_1_recall_n === 1'b0)
      begin openpa_sp[2] = 1'b1; openpa_sreq = 1'b1; end
    openpa_PD_UNIT_EVENT_rv = UPF::openpa_ctl_1_recall_n;
  end
  always @(negedge UPF::openpa_ctl_1_recall_n) if ((UPF::openpa_ctl_1_recall_n === 1'b0) && (UPF::openpa_ctl_2_busy === 1'b1)) openpa_check(14, "set_retention EVENT -assert_r_mutex: restore active together with busy high (lesson.upf:25)");
  always @(posedge UPF::openpa_ctl_0_store) if ((UPF::openpa_ctl_0_store === 1'b1) && (UPF::openpa_ctl_2_busy === 1'b1)) openpa_check(14, "set_retention EVENT -assert_s_mutex: save active together with busy high (lesson.upf:25)");
  always @(negedge UPF::openpa_ss_KEEP_ok) if (!UPF::openpa_ss_KEEP_ok) begin
    if (openpa_PD_UNIT_EVENT_valid) openpa_check(6, "set_retention EVENT: retention supply KEEP lost while holding saved state");
    openpa_PD_UNIT_EVENT_valid = 1'b0;
    openpa_PD_UNIT_EVENT_sh0 = openpa_xmode ? 'x : UPF::openpa_mix(50003, UPF::openpa_ss_KEEP_ep, 0);
    openpa_PD_UNIT_EVENT_sh1 = openpa_xmode ? 'x : UPF::openpa_mix(50001, UPF::openpa_ss_KEEP_ep, 0);
    if (openpa_xmode) openpa_cv3 = 'x; else openpa_cv3 = UPF::openpa_mix(70003, UPF::openpa_ss_KEEP_ep, 0);
    force u_core.tag = openpa_cv3; release u_core.tag;
    if (openpa_xmode) openpa_cv1 = 'x; else openpa_cv1 = UPF::openpa_mix(70001, UPF::openpa_ss_KEEP_ep, 0);
    force u_core.acc = openpa_cv1; release u_core.acc;
  end
  always @(posedge u_core.clk) if (openpa_PD_UNIT_EVENT_arm7 && UPF::openpa_dom_PD_UNIT_ok) openpa_PD_UNIT_EVENT_ck7_0 = $realtime;
  always @(negedge UPF::openpa_dom_PD_UNIT_ok) if (!UPF::openpa_dom_PD_UNIT_ok && openpa_PD_UNIT_EVENT_arm7) begin
    if (openpa_PD_UNIT_EVENT_ck7_0 > openpa_PD_UNIT_EVENT_st7 && openpa_PD_UNIT_EVENT_ck7_0 < $realtime) openpa_check(7, $sformatf("set_retention EVENT: u_core.clk had an active edge at %0t, after the save and before PD_UNIT went off; the retained state may differ from the saved one (lesson.upf:23)", openpa_PD_UNIT_EVENT_ck7_0));
    openpa_PD_UNIT_EVENT_arm7 = 1'b0;
  end
  always @(posedge UPF::openpa_dom_PD_UNIT_ok) openpa_PD_UNIT_await = openpa_PD_UNIT_EVENT_valid;
  // crossing 1: sleep_soc input clk (PD_TOP) -> u_core/clk (PD_UNIT), not isolated
  bit openpa_x1_t;
  always @(UPF::openpa_dom_PD_TOP_ok or UPF::openpa_dom_PD_UNIT_ok or UPF::openpa_net_HOST_N or UPF::openpa_net_GND_N) openpa_x1_t = !UPF::openpa_dom_PD_TOP_ok && UPF::openpa_dom_PD_UNIT_ok && !(1'b0) && !(UPF::openpa_net_HOST_N.state == UPF::UNDETERMINED || UPF::openpa_net_GND_N.state == UPF::UNDETERMINED);
  always @(posedge openpa_x1_t)
    begin openpa_sp[3] = 1'b1; openpa_sreq = 1'b1; end
  // crossing 2: sleep_soc input d (PD_TOP) -> u_core/d (PD_UNIT), not isolated
  bit openpa_x2_t;
  always @(UPF::openpa_dom_PD_TOP_ok or UPF::openpa_dom_PD_UNIT_ok or UPF::openpa_net_HOST_N or UPF::openpa_net_GND_N) openpa_x2_t = !UPF::openpa_dom_PD_TOP_ok && UPF::openpa_dom_PD_UNIT_ok && !(1'b0) && !(UPF::openpa_net_HOST_N.state == UPF::UNDETERMINED || UPF::openpa_net_GND_N.state == UPF::UNDETERMINED);
  always @(posedge openpa_x2_t)
    begin openpa_sp[4] = 1'b1; openpa_sreq = 1'b1; end
  // level shifter check 1: PD_TOP -> PD_UNIT (no level shifter strategy, threshold 0)
  bit openpa_l1_t;
  always @(UPF::openpa_dom_PD_TOP_ok or UPF::openpa_dom_PD_UNIT_ok or UPF::openpa_net_HOST_N or UPF::openpa_net_UNIT_N) openpa_l1_t = UPF::openpa_dom_PD_TOP_ok && UPF::openpa_dom_PD_UNIT_ok && (UPF::openpa_net_HOST_N.voltage - UPF::openpa_net_UNIT_N.voltage > 0 || UPF::openpa_net_UNIT_N.voltage - UPF::openpa_net_HOST_N.voltage > 0) && !1'b0;
  always @(posedge openpa_l1_t)
    begin openpa_sp[5] = 1'b1; openpa_sreq = 1'b1; end
  always @(negedge UPF::openpa_dom_PD_TOP_ok) if (!UPF::openpa_dom_PD_TOP_ok)
    begin openpa_sp[6] = 1'b1; openpa_sreq = 1'b1; end
  always @(negedge UPF::openpa_dom_PD_TOP_ok) if (!UPF::openpa_dom_PD_TOP_ok)
    begin openpa_sp[7] = 1'b1; openpa_sreq = 1'b1; end
  always @(negedge UPF::openpa_dom_PD_TOP_ok) if (!UPF::openpa_dom_PD_TOP_ok)
    begin openpa_sp[8] = 1'b1; openpa_sreq = 1'b1; end

  // Icarus 12 runs no named block inside a final block, so the report is a function.
  function automatic void openpa_cover_report();
      int hs, ht, hq;
      hs = 0; ht = 0; hq = 0;
      $display("OPENPA-COVER sequence D4 EVENT: save while PD_UNIT is on %0d", openpa_seq[1]);
      $display("OPENPA-COVER sequence D5 EVENT: restore of a saved state after power-up %0d", openpa_seq[2]);
      if (openpa_seq[1]) hq = hq + 1;
      if (openpa_seq[2]) hq = hq + 1;
      $display("OPENPA-COVER-SUMMARY states=%0d/0 declared_transitions=%0d/0 sequences=%0d/2", hs, ht, hq);
  endfunction

  // ---- settle process: 8 deferred check(s), run after one #0 in a fixed order
  initial forever begin
    wait (openpa_sreq && openpa_kick);
    #0;
    openpa_sreq = 1'b0;
    if (openpa_sp[1]) begin
      openpa_sp[1] = 1'b0;
          if (!UPF::openpa_dom_PD_UNIT_ok) openpa_check(4, "set_retention EVENT: save while PD_UNIT is off; the saved value is corrupt (lesson.upf:23)");
          else openpa_seq[1] = openpa_seq[1] + 1;
          if (UPF::openpa_ss_KEEP_ok) begin
            openpa_PD_UNIT_EVENT_sh0 = u_core.tag;
            openpa_PD_UNIT_EVENT_sh1 = u_core.acc;
            openpa_PD_UNIT_EVENT_valid = 1'b1; openpa_t("EVENT save");
            openpa_PD_UNIT_EVENT_arm7 = UPF::openpa_dom_PD_UNIT_ok;
            openpa_PD_UNIT_EVENT_st7 = $realtime;
          end
    end
    if (openpa_sp[2]) begin
      openpa_sp[2] = 1'b0;
          if (!UPF::openpa_dom_PD_UNIT_ok) begin
            openpa_check(5, "set_retention EVENT: restore while PD_UNIT is off; the retained value is corrupted (lesson.upf:23)");
            openpa_PD_UNIT_EVENT_sh0 = openpa_xmode ? 'x : UPF::openpa_mix(90003, UPF::openpa_ss_KEEP_ep, 0);
            openpa_PD_UNIT_EVENT_sh1 = openpa_xmode ? 'x : UPF::openpa_mix(90001, UPF::openpa_ss_KEEP_ep, 0);
          end
          else if (!openpa_PD_UNIT_EVENT_valid) openpa_check(5, "set_retention EVENT: restore with no saved state (lesson.upf:23)");
          else begin
            if (UPF::openpa_dom_PD_UNIT_sim == 4) begin
              if (openpa_xmode) openpa_cv3 = 'x; else openpa_cv3 = UPF::openpa_mix(80003, UPF::openpa_dom_PD_UNIT_ep, 0);
              force u_core.tag = openpa_cv3; release u_core.tag;
              if (openpa_xmode) openpa_cv1 = 'x; else openpa_cv1 = UPF::openpa_mix(80001, UPF::openpa_dom_PD_UNIT_ep, 0);
              force u_core.acc = openpa_cv1; release u_core.acc;
              openpa_t("EVENT restore in CORRUPT_ON_ACTIVITY: registers corrupted");
            end else begin
              force u_core.tag = openpa_PD_UNIT_EVENT_sh0; release u_core.tag;
              force u_core.acc = openpa_PD_UNIT_EVENT_sh1; release u_core.acc;
            end
            openpa_PD_UNIT_await = 1'b0; openpa_t("EVENT restore"); openpa_seq[2] = openpa_seq[2] + 1;
            openpa_PD_UNIT_EVENT_arm7 = 1'b0;
          end
    end
    if (openpa_sp[3]) begin
      openpa_sp[3] = 1'b0;
      if (openpa_x1_t) openpa_check(9, "sleep_soc input clk of PD_TOP (off) is read by u_core/clk in PD_UNIT (on) with no active isolation");
    end
    if (openpa_sp[4]) begin
      openpa_sp[4] = 1'b0;
      if (openpa_x2_t) openpa_check(9, "sleep_soc input d of PD_TOP (off) is read by u_core/d in PD_UNIT (on) with no active isolation");
    end
    if (openpa_sp[5]) begin
      openpa_sp[5] = 1'b0;
      if (openpa_l1_t) openpa_check(11, $sformatf("PD_TOP (%0d uV) drives PD_UNIT (%0d uV) on 2 port(s) with no level shifter for this direction", UPF::openpa_net_HOST_N.voltage, UPF::openpa_net_UNIT_N.voltage));
    end
    if (openpa_sp[6]) begin
      openpa_sp[6] = 1'b0;
      if (!UPF::openpa_dom_PD_TOP_ok && (UPF::openpa_dom_PD_UNIT_ok)) openpa_check(12, "store (retention EVENT save) comes from PD_TOP, which is off while other domains are on");
    end
    if (openpa_sp[7]) begin
      openpa_sp[7] = 1'b0;
      if (!UPF::openpa_dom_PD_TOP_ok && (UPF::openpa_dom_PD_UNIT_ok)) openpa_check(12, "recall_n (retention EVENT restore) comes from PD_TOP, which is off while other domains are on");
    end
    if (openpa_sp[8]) begin
      openpa_sp[8] = 1'b0;
      if (!UPF::openpa_dom_PD_TOP_ok && (UPF::openpa_dom_PD_UNIT_ok)) openpa_check(12, "busy (retention EVENT -assert_r_mutex; retention EVENT -assert_s_mutex) comes from PD_TOP, which is off while other domains are on");
    end
  end
  final begin
    $display("OPENPA-SUMMARY D1=%0d D2=%0d D3=%0d D4=%0d D5=%0d D6=%0d D7=%0d D8=%0d D9=%0d D10=%0d D11=%0d D12=%0d D13=%0d D14=%0d", openpa_n[1], openpa_n[2], openpa_n[3], openpa_n[4], openpa_n[5], openpa_n[6], openpa_n[7], openpa_n[8], openpa_n[9], openpa_n[10], openpa_n[11], openpa_n[12], openpa_n[13], openpa_n[14]);
    openpa_cover_report();
    if (!UPF::openpa_armed) $display("OPENPA-WARNING no supply was driven (supply_on/supply_off never called): every domain stayed CORRUPT");
  end
  // verilator lint_on MULTIDRIVEN
`line 11 "bank.sv" 0
endmodule

Predict the outputIn the hand-written condition model, what does sleep pins print?
module tb;
  reg [7:0] q, shadow;
  reg live, keep_ok, save_ok, restore_ok;
  wire [7:0] primary_pin = live ? q : 8'hxx;
  wire [7:0] backed_pin = live ? q : shadow;
  task save; if (live && save_ok) shadow=q; endtask
  task restore; if (live && restore_ok) q=shadow; endtask
  task show(input string row);
    #1; $display("%s q=%h shadow=%h primary_pin=%h backed_pin=%h",row,q,shadow,primary_pin,backed_pin);
  endtask
  initial begin
    live=1; keep_ok=1; save_ok=1; restore_ok=1;
    q=8'h32; save; q=8'h74; save_ok=0; save; show("save gated");
    live=0; q=8'hxx; show("sleep pins");
    live=1; q=0; restore_ok=0; restore; show("recall gated");
    restore_ok=1; restore; show("recall allowed");
    live=0; q=8'hxx; keep_ok=0; if (!keep_ok) shadow=8'hxx; show("hold broken");
    $finish;
  end
endmodule
ConceptThe live acc reads xx during sleep. Which observation demonstrates successful retention?
ConceptD14 reports a restore while busy is high. Which PMU event would avoid that overlap?
ConceptYou define HEADER for tag, then remove EVENT. What remains?
ConceptWhat can corrupt a shadow even while the primary supply stays healthy?

Common Mistakes

Wrong Read the live register during sleep to see whether the save worked.

Its working supply is off. The saved byte lives on another rail.

Fix Wake the core, write a different value, then recall and compare.

Wrong Take the byte present when an active-high level save starts.

The clock can still write before the window closes.

Fix Trace writes up to the trailing edge of store.

Wrong Treat simultaneous save and restore as a priority choice.

An overlap can spoil the saved and working values.

Fix Separate the controls. Exercise each prohibited overlap on the bench.

Remember

  • Retain the state the next transaction needs. Check at least one byte that should remain volatile.
  • Trace capture, power loss and recall as separate events. A sleeping working register can read X after a good save.
  • Compare the control window with the clock before deciding which write should return.
  • Separate the power for working storage, saved storage and the output driver.

Practice

Report a problem with this page