Ready-to-run snippets

Code Examples

Ready-to-run Verilog, SystemVerilog, and UVM examples. Click any to open it live in the IDE.

← All examples

FV Proof in Two Halves

SystemVerilog Formal Verification

Base case and induction step for the lockstep counters, selected with a `define switch. Both must pass.

Open in IDE
design.sv
// Run as is for the base case (depth 10); uncomment `define INDUCTION_STEP for the step (depth 2).
module lockstep (input clk, input rst, input en, output done_a, output done_b);
  // two copies of the same counter, as in a lockstep safety design
  reg [3:0] a, b;
  always @(posedge clk)
    if (rst) begin
      a <= 4'd0; b <= 4'd0;
    end else if (en) begin
      a <= (a == 4'd9) ? 4'd0 : a + 4'd1;
      b <= (b == 4'd9) ? 4'd0 : b + 4'd1;
    end
  assign done_a = (a == 4'd9);
  assign done_b = (b == 4'd9);

`ifdef FORMAL
// `define INDUCTION_STEP      // uncomment to run the induction step instead of the base case
`ifndef INDUCTION_STEP
  // base case: an ordinary bounded check from reset (depth >= K)
  reg init = 1'b1;
  always @(posedge clk) init <= 1'b0;
  always @(*) if (init) assume (rst);
  always @(*) if (!init) assert (done_a == done_b);
  always @(*) if (!init) assert (a == b);                 // helper invariant
`else
  // induction step with K = 1: start anywhere, properties hold in step 0, check step 1 (depth 2)
  always @(*) assume (!rst);
  reg [7:0] step = 8'd0;
  always @(posedge clk) step <= step + 8'd1;
  always @(*) if (step < 8'd1) assume (done_a == done_b && a == b);
  always @(*) if (step == 8'd1) assert (done_a == done_b && a == b);
`endif
`endif
endmodule
Admin mode — edit and delete controls are visible on each row. Use the IDE to add new snippets via File → Save as Snippet.