Reset Domain Crossing

Software restarts one timer and leaves the rest of the chip running. The timer forgets everything, as a reset should. Its neighbour does not forget, and that is where the trouble starts.

You Will Be Able To

  • Find a reset domain crossing, also between two blocks on one clock.
  • Name the two hazards of a reset domain crossing, and say which one a simulator can show.
  • Put the steps of a safe local reset in order: drain, isolate, reset, release.
Lesson
S5.6
Time
26 min
Runs on
Your browser
Check
6 questions

A driver restarts a timer that has misbehaved, and the timer comes back healthy. The bus does not: one read never finishes, and no later read can start. Both blocks have a running clock. What is the bus waiting for?

Before You Start

You need Reset Architecture: a reset is asserted with no clock. You need Reset Sources and the Reset Cause Register: a reset domain, and a reset that reaches only part of the chip.

You need APB, a Few Signals at a Time: a read has a setup cycle and an access cycle. The device says when it is ready. The bridge that drives those cycles is in Two Buses and the Bridge.

The timer on this page is read over the bus like any peripheral. The Anatomy of a Peripheral shows what such a block is made of. Read it first if a peripheral behind a bus is new to you.

Metastability is explained in Planning Clock Domains.

Five questions this page answers
Question
1What is a reset domain crossing?
2Can two blocks on one clock have a reset domain crossing between them?
3What are the two hazards of a reset domain crossing?
4In the lab, why does waiting longer not finish the stuck read?
5In which order do drain, isolate and reset come?

The Idea: One Side Forgets and the Other Runs On

A chip can reset one block and leave its neighbours running. That is useful: a driver can restart a device without a reboot. It also creates a border. On one side are flip-flops that the reset clears. On the other are flip-flops that keep running.

A signal that goes from the first side to the second is a reset domain crossing. The two sides may share a clock. The crossing is still there, because the reset is asserted with no clock.

Such a crossing has two hazards, and they are different in kind.

The first is electrical. The reset can change the signal at any moment. The running side may sample it at just that moment, and a flip-flop there can go metastable. This is the hazard of a clock domain crossing, on one clock.

The second is a progress hazard. The running side may be in the middle of a job that needs something the reset side knew. The reset wipes that knowledge. The signal that arrives is a clean 0, sampled cleanly, and the job still never finishes.

The cure for both is an order of events. Finish the work in progress and start no more. Hold the crossing at a safe value. Then, and only then, reset.

New Words

New words on this page
WordMeaning
Reset domain crossing (RDC)A signal from flip-flops that a reset clears to flip-flops that the same reset does not clear.
Local reset of a blockA reset that restarts one block while the rest of the chip runs on.
Live sideThe side of a crossing that is not reset and keeps running.
Progress hazardA fault in which every value is clean and some job can still never finish.
DrainStart no new work, and let the work already accepted finish.
Isolate a crossingHold a crossing signal at a safe value while its sender is in reset.
Stand-inA small block that plays the part of a real one in a lab, with rules of its own.

One Picture: The Crossing and Its Two Hazards

One Clock, Two Reset DomainsStep through the picture. It finds the crossing, names its two hazards and ends with the cure.
  • Bus
  • Control / FSM
  • Peripheral
  • Clock / reset
One Clock, Two Reset DomainsStep through the picture. It finds the crossing, names its two hazards and ends with the cure. component Bridge keeps running: noreset reaches it here component Isolation gate can hold the answerat a safe value component Timer has a reset of its own component Reset controller resets the timer whensoftware asks component One clock the same clock forbridge and timer read requestready and dataanswertimer resetisolateclockclock
  1. component Bridgekeeps running: no reset reaches it here
    • read request → Timer
  2. component Timerhas a reset of its own
    • ready and data → Isolation gate
  3. component One clockthe same clock for bridge and timer
    • clock → Bridge
    • clock → Timer
  4. component Reset controllerresets the timer when software asks
    • timer reset → Timer
    • isolate → Isolation gate
  5. component Isolation gatecan hold the answer at a safe value
    • answer → Bridge
  1. Two blocks, one clock

    The bridge reads the timer over the bus. Both use the same clock. A check of clock domains finds nothing to worry about here.

  2. The timer has its own reset

    Software can restart the timer alone. That reset clears the flip-flops of the timer and none of the bridge. They are two reset domains.

  3. The crossing

    The answer of the timer leaves one reset domain and enters the other. That wire is a reset domain crossing.

  4. Hazard one: a change at any moment

    The reset is asserted with no clock. The answer can change at any moment, also right beside a clock edge of the bridge.

  5. Hazard two: a forgotten read

    The bridge is in the middle of a read. The reset wipes what the timer knew about that read. The bridge goes on waiting for an answer.

  6. The cure, in order

    First let the read in progress finish, and start no new one. Then hold the answer at a safe value. Only then reset the timer.

Read the diagram as text
  • Bridge: keeps running: no reset reaches it here
  • Isolation gate: can hold the answer at a safe value
  • Timer: has a reset of its own
  • Reset controller: resets the timer when software asks
  • One clock: the same clock for bridge and timer
  • Bridge → Timer: read request
  • Timer → Isolation gate: ready and data
  • Isolation gate → Bridge: answer
  • Reset controller → Timer: timer reset
  • Reset controller → Isolation gate: isolate
  • One clock → Bridge: clock
  • One clock → Timer: clock

The timer and the bridge of the lab. The isolation gate is drawn as one box. In the lab it is a few lines of test code.

The Smallest Example, Worked by Hand

The lab has a bridge and a timer on one clock. The timer is a stand-in with one rule of its own. In the setup cycle of a read it saves its count and sets a bit called pending. It reports ready only while pending is 1. In the access cycle it gives up the saved count and clears pending.

Now software asks for a restart of the timer. The reset lands between the setup cycle and the end of the access cycle.

One read, cut by a reset of the timer, by hand
MomentBridge (not reset)Lab timer (reset)Ready
Setup cycleSelects the timer. Enable is 0.Saves its count. Sets pending.0
Just afterMoves to the access cycle. Enable is 1.Pending is 1.1
Timer reset, before the next edgeStill in the access cycle. Waits for ready.Pending is cleared.0
Every later edgeStill in the access cycle.Sets pending only in a setup cycle. None comes.0

Look at the last row from both sides. The timer would answer if it saw a setup cycle. The bridge would send a setup cycle if this read finished. Each waits for the other. A longer wait changes nothing.

This failure rests on the rule of our lab timer. A timer that answered every access cycle would let this read finish, perhaps with a count of 0. So the lab shows one progress hazard, in one design. It is not a law that a read cut by a reset hangs.

The safe order uses the same parts. First the read finishes: that is the drain. Then the timer is cut off from the bus: it sees no select, and its data is held at 0. That is the isolation. Then the timer is reset. No read is in progress, so nobody waits.

One Read Across a Timer ResetStep through the samples. Each column is one look at the bus, in the order of the run.
0123momentsetupjust aftertimer resetafter releaseselectenablependingready
Scroll horizontally ↔
  1. Sample 0

    The setup cycle of the read. The bridge has selected the timer. Enable is still 0.

  2. Sample 1

    Just after that clock edge. The lab timer has saved a count and set its pending bit. The bridge is now in the access cycle, and ready is 1.

  3. Sample 2

    The timer is reset before the next edge. Its pending bit is gone, so ready is 0. The bridge still waits in the access cycle.

  4. Sample 3

    The reset is released and two more clock edges pass. Nothing has moved. Nothing will.

Drawn from the first four rows of the lab. The bridge is not reset. The timer is.

The Rules

  1. Rule 1: A reset domain crossing is found by resets, not by clocks.

    Ask of every signal: can its sender be reset while its receiver runs?

  2. Rule 2: The live side must not sample a crossing while its sender is being reset.

    An asynchronous assertion can change it at any moment. This is the electrical hazard.

  3. Rule 3: A reset must not wipe state that the live side still waits for.

    This is the progress hazard. No synchronizer cures it.

  4. Rule 4: Drain first.

    Stop every block that can start new work at the target. Wait for the work already accepted to finish.

  5. Rule 5: Then isolate, then reset.

    The crossing is held at a safe value before the reset is asserted.

  6. Rule 6: Release the reset first, then the isolation.

    The sender must be in a known state again before the live side listens to it.

Sources for These Rules

Sampling a signal while it changes, and metastability: Harris and Harris, Digital Design and Computer Architecture, ARM edition, section 3.5.4.

The setup and access cycles of an APB read, and the ready signal: Arm AMBA APB Protocol Specification, IHI 0024 issue E, sections 3.3 and 4.1.

Reset domain crossing as a topic, and rules 3 to 6, are common design practice. No book or standard in our reference library covers them, so none is quoted.

The timer that saves a count in the setup cycle, the restart command and the order of the second trial are choices of our lab.

Now Run It

The lab at the end of this page runs the read twice. Test code plays the processor: it writes a restart command to a control register, then reads the timer. Test code also plays the reset controller, so it can choose the moment of the reset.

Printed by the lab on Icarus. The first four rows are the first trial. The last five are the second.
setup sel=1 enable=0 armed=1 pending=0 hready=0
reserved sel=1 enable=1 pending=1 hready=1
reset isolated=0 pending=0 enable=1 hready=0 sel=1 local_reset=1
after release sel=1 enable=1 pending=0 hready=0
setup sel=1 enable=0 armed=1 pending=0 hready=0
reserved sel=1 enable=1 pending=1 hready=1
drain pending=1 hready=1 read=00000005
reset isolated=1 pending=0 enable=0 hready=1 sel=0 local_reset=1
next read=00000004 hready=1

Rule 3 broken, in rows three and four. At the reset, enable=1 and pending=0: the bridge waits and the timer has forgotten. After the release the row is the same, and hready is still 0.

Rule 4 at work, in the row that starts with "drain". The read finishes first: hready=1 and the count is 5.

Rule 5 at work, in the next row. With isolated=1 the timer is reset while sel=0 and enable=0. No read is in progress. Rule 6 at work, in the last row: the next read finishes and returns 4. The count is small because the reset started the timer again.

This lab leaves things out, and the first is the most important. It does not show the electrical hazard at all. A simulator of 0 and 1 gives a clean 0 when the timer is reset. It cannot show a flip-flop of the bridge caught by a changing signal. The second: the stuck read rests on the rule of the stand-in timer, as said above.

Why It Is Built This Way

Design choiceThe alternativeWhat the choice buys, and costs
Drain before a local resetReset at onceNo job is cut in half. The reset must wait, and a block that is truly stuck may never drain.
Isolate the crossingAdd a synchronizer on itThe live side sees a steady, safe value. A synchronizer would only deliver the wrong value cleanly.
Reset both sides togetherA local resetNo crossing at all. More of the chip restarts, and more state is lost.
A timeout on the live sideWait for everA way out when the target cannot drain. The read ends with an error, and software must handle it.

The Trap: The Clock Check Said Nothing

Back to the driver that restarted its timer. The review had checked every clock domain crossing on the chip. The timer and the bridge share a clock, so the list had no entry for them. Nobody asked the other question: which of the two can be reset alone?

The second trap is the fix that seems obvious. Put two flip-flops on the ready signal. They would deliver the 0 a little later, and just as clean. The read would hang in the same place. A synchronizer answers the electrical hazard. It has no answer for a forgotten read.

The third trap is at the end of a recovery. Software sees a timeout and writes the same value again. The first write may have taken effect before its answer was lost. Now it has happened twice.

At Scale

This page1 crossing
A phone chipthousands of crossings

This section looks ahead. One timer and one bridge become every block that software or a watchdog can restart alone.

  1. Find every listener

    For each reset, a tool lists the signals that leave its domain and reach flip-flops that keep running. People then decide what protects each one.

  2. Close every door

    The processor may be idle while a debugger can still start a read at the same target. Every block that can start a read must stop.

  3. A way out for a dead block

    A block that is truly stuck never drains. A part that stays alive ends its transfers with an error, so that the reset can go ahead.

  4. The same order at a power boundary

    A block that is switched off needs the same steps: drain, isolate, then off. Power at Chip Level is the next page.

Try It

Run the lab. It makes the same software request twice. The first time the reset comes in the middle of a read. The second time the read is allowed to finish first.

In the first trial, wait for ten clock edges after the release in place of two. Does the read finish?

In the second trial, remove the line that sets "isolated" to 1. Which row changes?

Names Printed by the Lab
NameMeaning
sel, enableThe select and the enable of the APB read of the timer. Enable 0 is the setup cycle. Enable 1 is the access cycle.
armedSoftware has asked for a restart of the timer. The reset controller has not applied it yet.
pendingA bit inside the lab timer. 1 means: I have saved a count for the read in progress.
hreadyThe transfer is complete, as the bus model that plays the processor sees it.
local_reset1 while the reset of the timer is asserted.
isolated1 while the timer is cut off from the bus: it sees no select, and its data is held at 0.
read=The count that a read returned, in hexadecimal.
RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: setup sel=1 enable=0 armed=1 pending=0 hready=0 reserved sel=1 enable=1 pending=1 hready=1 reset isolated=0 pending=0 enable=1 hready=0 sel=1 local_reset=1 after release sel=1 enable=1 pending=0 hready=0 setup sel=1 enable=0 armed=1 pending=0 hready=0 reserved sel=1 enable=1 pending=1 hready=1 drain pending=1 hready=1 read=00000005 reset isolated=1 pending=0 enable=0 hready=1 sel=0 local_reset=1 next read=00000004 hready=1
Read included file: bridge.sv
`timescale 1ns/1ns
// ahb_apb_bridge.sv: an AHB subordinate that turns each accepted transfer into one APB transfer
// (read-only). The address phase is registered when HSEL, HREADY and a NONSEQ or SEQ transfer are
// all present. During SETUP and unfinished ACCESS the bridge keeps HREADYOUT low. A successful APB
// completion ends the AHB data phase with OKAY and HRDATA = PRDATA. A failed APB completion starts
// the two-cycle ERROR (HREADYOUT low then high, HRESP high in both). HWDATA and HWSTRB are data-phase signals the manager holds
// through the wait states, so PWDATA and PSTRB are driven from them directly; PADDR is the aligned
// word address (IHI 0024E 2.1.1 leaves an unaligned PADDR unpredictable). HPROT maps onto PPROT:
// privileged from HPROT[1], instruction from HPROT[0] = 0, and the access is Secure.
// A write therefore costs one setup and one access cycle: two cycles of data phase with no APB wait.
module ahb_apb_bridge (
  input  logic        hclk,
  input  logic        hresetn,
  input  logic        hsel,
  input  logic [31:0] haddr,
  input  logic [1:0]  htrans,
  input  logic        hwrite,
  input  logic [3:0]  hprot,
  input  logic [31:0] hwdata,
  input  logic [3:0]  hwstrb,
  input  logic        hready,
  output logic        hreadyout,
  output logic        hresp,
  output logic [31:0] hrdata,
  output logic        psel,
  output logic        penable,
  output logic        pwrite,
  output logic [31:0] paddr,
  output logic [31:0] pwdata,
  output logic [3:0]  pstrb,
  output logic [2:0]  pprot,
  input  logic        pready,
  input  logic [31:0] prdata,
  input  logic        pslverr);
  localparam int IDLE = 0, SETUP = 1, ACCESS = 2, ERR1 = 3, ERR2 = 4;
  bit trace = 0;
  int st = IDLE;
  logic        wr = 0;
  logic [31:0] a = 0;
  logic [2:0]  prot = 0;
  wire take = hsel && hready && htrans[1];
  assign psel      = (st == SETUP || st == ACCESS);
  assign penable   = (st == ACCESS);
  assign pwrite    = wr;
  assign paddr     = {a[31:2], 2'b00};     // APB leaves an unaligned PADDR unpredictable: the word, and PSTRB picks the bytes
  assign pwdata    = wr ? hwdata : 32'h0;
  assign pstrb     = wr ? hwstrb : 4'h0;
  assign pprot     = prot;
  assign hreadyout = (st == IDLE || st == ERR2) ? 1'b1 : (st == ACCESS) ? (pready === 1'b1 && pslverr !== 1'b1) : 1'b0;
  assign hresp     = (st == ERR1 || st == ERR2);
  assign hrdata    = (st == ACCESS) ? prdata : 32'h0;
  always @(posedge hclk or negedge hresetn) begin
    if (!hresetn) st <= IDLE;
    else begin
      case (st)
        SETUP:  st <= ACCESS;
        ACCESS: if (pready === 1'b1) begin
                  if (trace) $display("apb e%0d: %0s 0x%03h%0s", ($time - 5) / 10, wr ? "write" : "read", a[11:0], pslverr === 1'b1 ? " PSLVERR" : "");
                  st <= (pslverr === 1'b1) ? ERR1 : IDLE;
                end
        ERR1:   st <= ERR2;
        default: st <= IDLE;
      endcase
      // A new address phase can be taken whenever HREADY is 1: in IDLE, in the last cycle of ERROR,
      // or at the edge an access completes with OKAY.
      if (take && (st == IDLE || st == ERR2 || (st == ACCESS && pready === 1'b1 && pslverr !== 1'b1))) begin
        wr <= hwrite; a <= haddr; prot <= {~hprot[0], 1'b0, hprot[1]}; st <= SETUP;
      end
    end
  end
endmodule
Read included file: reset_bus.svh
// One aligned word at a time. The manager stands in for the future ap_rv32.
logic clk = 0, bus_reset_n;
always #5 clk = ~clk;
logic [31:0] addr = 0, wdata = 0;
logic write_req = 0, request = 0;
wire sel, enable, writing, ready, error, pready, slave_error;
wire [31:0] paddr, pwdata, prdata, rdata;
ahb_apb_bridge bridge (
  .hclk(clk), .hresetn(bus_reset_n), .hsel(addr[31:16] == 16'h1000),
  .haddr(addr), .htrans({request,1'b0}), .hwrite(write_req), .hprot(4'b0011),
  .hwdata(wdata), .hwstrb(4'hf), .hready(ready), .hreadyout(ready),
  .hresp(error), .hrdata(rdata), .psel(sel), .penable(enable), .pwrite(writing),
  .paddr(paddr), .pwdata(pwdata), .pstrb(), .pprot(), .pready(pready),
  .prdata(prdata), .pslverr(slave_error)
);
task automatic issue(input bit wr, input logic [31:0] address, data);
  @(negedge clk);
  write_req = wr;
  addr = address;
  request = 1;
  @(negedge clk);
  request = 0;
  addr = 0;
  wdata = data;
endtask
Read included file: ap_sysctl_reset_port.sv
// Reset-service words within the ap_soc laboratory system-control page.
// All manager accesses to this port are aligned, full-word APB transfers.
// The cause storage has a separate reset owner and enters through cause.
module ap_sysctl_reset_port (
  input  logic        clk, bus_reset_n,
  input  logic        sel, enable, writing,
  input  logic [31:0] paddr, pwdata,
  input  logic [4:0]  cause,
  input  logic        service_slot,
  output logic        page_selected, access_error, armed,
  output logic [31:0] read_data
);
  localparam logic [31:0] CAUSE = 32'h1000_4004, COMMAND = 32'h1000_4008;
  wire cause_read = !writing && paddr == CAUSE;
  wire command_write = writing && paddr == COMMAND;

  assign page_selected = sel && paddr[31:12] == CAUSE[31:12];
  assign read_data = cause_read ? {27'b0,cause} : 32'b0;
  assign access_error = page_selected && enable && !(cause_read || command_write);

  always_ff @(posedge clk or negedge bus_reset_n) begin
    if (!bus_reset_n)
      armed <= 0;
    else if (page_selected && enable && command_write)
      armed <= pwdata[0];
    else if (service_slot)
      armed <= 0;
  end
endmodule
Read included file: rdc_timer_port.sv
// The timer runs in the same clock domain as the bridge but has its own reset.
// A read reserves the count in setup and consumes the reservation in access.
module rdc_timer_port (
  input logic clk, timer_reset_n, selected, enable,
  output logic pending, output logic [31:0] snapshot
);
  logic [31:0] ticks;

  always_ff @(posedge clk or negedge timer_reset_n) begin
    if (!timer_reset_n) begin
      ticks <= 0;
      pending <= 0;
      snapshot <= 0;
    end else begin
      ticks <= ticks + 1'b1;
      if (selected && !enable) begin
        snapshot <= ticks;
        pending <= 1;
      end else if (selected && enable && pending) begin
        pending <= 0;
      end
    end
  end
endmodule

Check Yourself

Predict the outputThis run has the first trial only: the timer is reset in the middle of a read, with no drain. Two clock edges after the release, the lab prints sel, enable, pending and hready. What is that last row?
`timescale 1ns/1ns
module tb;
  `include "reset_bus.svh"
  localparam COMMAND = 32'h1000_4008, TIMER = 32'h1000_2000;
  logic service_slot = 0, isolated = 0;
  wire armed;
  // A software write arms one restart. The test chooses when the always-on
  // controller services it, so both orderings are repeatable.
  wire timer_reset_n = bus_reset_n && !(armed && service_slot);
  wire selected = sel && paddr == TIMER;
  wire sysctl_selected, sysctl_error;
  wire [31:0] sysctl_data;
  wire pending;
  wire [31:0] snapshot;
  rdc_timer_port timer(.clk(clk),.timer_reset_n(timer_reset_n),
    .selected(selected && !isolated),.enable(enable),.pending(pending),.snapshot(snapshot));
  ap_sysctl_reset_port sysctl(.clk(clk),.bus_reset_n(bus_reset_n),
    .sel(sel),.enable(enable),.writing(writing),.paddr(paddr),.pwdata(pwdata),
    .cause(5'b0),.service_slot(service_slot),.page_selected(sysctl_selected),
    .access_error(sysctl_error),.armed(armed),.read_data(sysctl_data));
  assign pready = !selected || (!isolated && pending);
  assign slave_error = sysctl_error || (sel && !(selected || sysctl_selected));
  assign prdata = sysctl_selected ? sysctl_data : (isolated ? 0 : snapshot);

  task automatic trial(input bit drain_first);
    @(negedge clk);
    bus_reset_n = 0;
    service_slot = 0;
    isolated = 0;
    @(posedge clk);
    bus_reset_n <= 1;
    @(negedge clk);
    issue(1, COMMAND, 1);
    do @(posedge clk); while (!ready);
    issue(0, TIMER, 0);
    @(posedge clk);
    $display("setup sel=%b enable=%b armed=%b pending=%b hready=%b",selected,enable,armed,pending,ready);
    #1;
    $display("reserved sel=%b enable=%b pending=%b hready=%b",selected,enable,pending,ready);
    @(negedge clk);
    if (!armed) $fatal(1,"Missing service command");
    if (drain_first) begin
      @(posedge clk);
      $display("drain pending=%b hready=%b read=%08h",pending,ready,rdata);
      @(negedge clk);
      isolated = 1;
      #1;
    end
    service_slot = 1;
    @(posedge clk);
    $display("reset isolated=%b pending=%b enable=%b hready=%b sel=%b local_reset=%b",isolated,pending,enable,ready,selected,!timer_reset_n);
    @(negedge clk);
    service_slot = 0;
    repeat (2) @(posedge clk);
    @(negedge clk);
    isolated = 0;
    if (drain_first) begin
      issue(0, TIMER, 0);
      do @(posedge clk); while (!ready);
      $display("next read=%08h hready=%b",rdata,ready);
    end else
      $display("after release sel=%b enable=%b pending=%b hready=%b",selected,enable,pending,ready);
  endtask

  initial begin
    trial(0);
    // Stop after the interrupted read.
    $finish;
  end
endmodule
Read included file: bridge.sv
`timescale 1ns/1ns
// ahb_apb_bridge.sv: an AHB subordinate that turns each accepted transfer into one APB transfer
// (read-only). The address phase is registered when HSEL, HREADY and a NONSEQ or SEQ transfer are
// all present. During SETUP and unfinished ACCESS the bridge keeps HREADYOUT low. A successful APB
// completion ends the AHB data phase with OKAY and HRDATA = PRDATA. A failed APB completion starts
// the two-cycle ERROR (HREADYOUT low then high, HRESP high in both). HWDATA and HWSTRB are data-phase signals the manager holds
// through the wait states, so PWDATA and PSTRB are driven from them directly; PADDR is the aligned
// word address (IHI 0024E 2.1.1 leaves an unaligned PADDR unpredictable). HPROT maps onto PPROT:
// privileged from HPROT[1], instruction from HPROT[0] = 0, and the access is Secure.
// A write therefore costs one setup and one access cycle: two cycles of data phase with no APB wait.
module ahb_apb_bridge (
  input  logic        hclk,
  input  logic        hresetn,
  input  logic        hsel,
  input  logic [31:0] haddr,
  input  logic [1:0]  htrans,
  input  logic        hwrite,
  input  logic [3:0]  hprot,
  input  logic [31:0] hwdata,
  input  logic [3:0]  hwstrb,
  input  logic        hready,
  output logic        hreadyout,
  output logic        hresp,
  output logic [31:0] hrdata,
  output logic        psel,
  output logic        penable,
  output logic        pwrite,
  output logic [31:0] paddr,
  output logic [31:0] pwdata,
  output logic [3:0]  pstrb,
  output logic [2:0]  pprot,
  input  logic        pready,
  input  logic [31:0] prdata,
  input  logic        pslverr);
  localparam int IDLE = 0, SETUP = 1, ACCESS = 2, ERR1 = 3, ERR2 = 4;
  bit trace = 0;
  int st = IDLE;
  logic        wr = 0;
  logic [31:0] a = 0;
  logic [2:0]  prot = 0;
  wire take = hsel && hready && htrans[1];
  assign psel      = (st == SETUP || st == ACCESS);
  assign penable   = (st == ACCESS);
  assign pwrite    = wr;
  assign paddr     = {a[31:2], 2'b00};     // APB leaves an unaligned PADDR unpredictable: the word, and PSTRB picks the bytes
  assign pwdata    = wr ? hwdata : 32'h0;
  assign pstrb     = wr ? hwstrb : 4'h0;
  assign pprot     = prot;
  assign hreadyout = (st == IDLE || st == ERR2) ? 1'b1 : (st == ACCESS) ? (pready === 1'b1 && pslverr !== 1'b1) : 1'b0;
  assign hresp     = (st == ERR1 || st == ERR2);
  assign hrdata    = (st == ACCESS) ? prdata : 32'h0;
  always @(posedge hclk or negedge hresetn) begin
    if (!hresetn) st <= IDLE;
    else begin
      case (st)
        SETUP:  st <= ACCESS;
        ACCESS: if (pready === 1'b1) begin
                  if (trace) $display("apb e%0d: %0s 0x%03h%0s", ($time - 5) / 10, wr ? "write" : "read", a[11:0], pslverr === 1'b1 ? " PSLVERR" : "");
                  st <= (pslverr === 1'b1) ? ERR1 : IDLE;
                end
        ERR1:   st <= ERR2;
        default: st <= IDLE;
      endcase
      // A new address phase can be taken whenever HREADY is 1: in IDLE, in the last cycle of ERROR,
      // or at the edge an access completes with OKAY.
      if (take && (st == IDLE || st == ERR2 || (st == ACCESS && pready === 1'b1 && pslverr !== 1'b1))) begin
        wr <= hwrite; a <= haddr; prot <= {~hprot[0], 1'b0, hprot[1]}; st <= SETUP;
      end
    end
  end
endmodule
Read included file: reset_bus.svh
// One aligned word at a time. The manager stands in for the future ap_rv32.
logic clk = 0, bus_reset_n;
always #5 clk = ~clk;
logic [31:0] addr = 0, wdata = 0;
logic write_req = 0, request = 0;
wire sel, enable, writing, ready, error, pready, slave_error;
wire [31:0] paddr, pwdata, prdata, rdata;
ahb_apb_bridge bridge (
  .hclk(clk), .hresetn(bus_reset_n), .hsel(addr[31:16] == 16'h1000),
  .haddr(addr), .htrans({request,1'b0}), .hwrite(write_req), .hprot(4'b0011),
  .hwdata(wdata), .hwstrb(4'hf), .hready(ready), .hreadyout(ready),
  .hresp(error), .hrdata(rdata), .psel(sel), .penable(enable), .pwrite(writing),
  .paddr(paddr), .pwdata(pwdata), .pstrb(), .pprot(), .pready(pready),
  .prdata(prdata), .pslverr(slave_error)
);
task automatic issue(input bit wr, input logic [31:0] address, data);
  @(negedge clk);
  write_req = wr;
  addr = address;
  request = 1;
  @(negedge clk);
  request = 0;
  addr = 0;
  wdata = data;
endtask
Read included file: ap_sysctl_reset_port.sv
// Reset-service words within the ap_soc laboratory system-control page.
// All manager accesses to this port are aligned, full-word APB transfers.
// The cause storage has a separate reset owner and enters through cause.
module ap_sysctl_reset_port (
  input  logic        clk, bus_reset_n,
  input  logic        sel, enable, writing,
  input  logic [31:0] paddr, pwdata,
  input  logic [4:0]  cause,
  input  logic        service_slot,
  output logic        page_selected, access_error, armed,
  output logic [31:0] read_data
);
  localparam logic [31:0] CAUSE = 32'h1000_4004, COMMAND = 32'h1000_4008;
  wire cause_read = !writing && paddr == CAUSE;
  wire command_write = writing && paddr == COMMAND;

  assign page_selected = sel && paddr[31:12] == CAUSE[31:12];
  assign read_data = cause_read ? {27'b0,cause} : 32'b0;
  assign access_error = page_selected && enable && !(cause_read || command_write);

  always_ff @(posedge clk or negedge bus_reset_n) begin
    if (!bus_reset_n)
      armed <= 0;
    else if (page_selected && enable && command_write)
      armed <= pwdata[0];
    else if (service_slot)
      armed <= 0;
  end
endmodule
Read included file: rdc_timer_port.sv
// The timer runs in the same clock domain as the bridge but has its own reset.
// A read reserves the count in setup and consumes the reservation in access.
module rdc_timer_port (
  input logic clk, timer_reset_n, selected, enable,
  output logic pending, output logic [31:0] snapshot
);
  logic [31:0] ticks;

  always_ff @(posedge clk or negedge timer_reset_n) begin
    if (!timer_reset_n) begin
      ticks <= 0;
      pending <= 0;
      snapshot <= 0;
    end else begin
      ticks <= ticks + 1'b1;
      if (selected && !enable) begin
        snapshot <= ticks;
        pending <= 1;
      end else if (selected && enable && pending) begin
        pending <= 0;
      end
    end
  end
endmodule
ConceptWhat is a reset domain crossing?
ConceptCan two blocks on one clock have a reset domain crossing between them?
ConceptWhat are the two hazards of a reset domain crossing?
ConceptIn the lab, why does waiting longer not finish the stuck read?
ConceptWhich order makes a local reset safe?

Common Mistakes

Wrong Check the clocks and call the crossing safe.

A reset is asserted with no clock, also between two blocks on one clock.

Fix Ask which side can be reset while the other runs.

Wrong Put a synchronizer on the crossing and stop there.

It delivers a clean value. It cannot bring back what the reset wiped.

Fix Drain the work in progress, then isolate, then reset.

Wrong Isolate as soon as software asks for the reset.

A read already accepted still needs its answer.

Fix Let that read finish first.

Remember

  • A reset domain crossing goes from flip-flops that a reset clears to flip-flops that keep running. One clock is no protection.
  • It has two hazards: a signal sampled while it changes, and a job that can never finish.
  • A simulator of 0 and 1 shows only the second, and here only through a rule of the lab timer.
  • The safe order is drain, isolate, reset. Then release the reset, then the isolation.

Practice

Report a problem with this page