FVO — Formal Verification Online
Lesson 14.6 · Handshakes and Credit-Based Flow Control

Lab: hunting a lost credit

This lab connects a sender, a one-cycle link in each direction and a receive buffer into one module. With BUG = 1 the sender updates its credit counter with an if / else if that gives sending priority over a returned credit. It looks harmless. Read it, predict what happens, then let formal tell you.

credit_link.sv
// credit_link.sv — sender + 1-cycle link + DEPTH-entry receive buffer
// DEPTH must be a power of two (2, 4, 8, ...).
module credit_link #(
  parameter int DEPTH = 4,
  parameter int W     = 8,
  parameter bit BUG   = 1'b0
) (
  input  logic                       clk,
  input  logic                       rst,
  // sender side
  input  logic                       tx_valid,
  input  logic [W-1:0]               tx_data,
  output logic                       tx_take,
  // receiver side
  input  logic                       rx_pop,
  output logic                       rx_valid,
  output logic [W-1:0]               rx_data,
  // status, for checks and debug
  output logic [$clog2(DEPTH+1)-1:0] credits,
  output logic [$clog2(DEPTH+1)-1:0] rx_count,
  output logic                       link_vld,
  output logic                       cred_ret
);
  localparam int CW = $clog2(DEPTH + 1);
  localparam int AW = $clog2(DEPTH);

  // ---------------- sender: may only send while it holds a credit
  assign tx_take = tx_valid && (credits != '0);

  always_ff @(posedge clk) begin
    if (rst)
      credits <= CW'(DEPTH);
    else if (BUG) begin
      if (tx_take)       credits <= credits - 1'b1;   // a returned credit in the same cycle is lost
      else if (cred_ret) credits <= credits + 1'b1;
    end else
      credits <= credits - CW'(tx_take) + CW'(cred_ret);
  end

  // ---------------- link: one register in each direction
  logic [W-1:0] link_data;
  logic         do_pop;
  always_ff @(posedge clk) begin
    if (rst) begin
      link_vld <= 1'b0;
      cred_ret <= 1'b0;
    end else begin
      link_vld <= tx_take;
      cred_ret <= do_pop;
    end
    link_data <= tx_data;
  end

  // ---------------- receiver: buffer with one credit returned per pop
  logic [W-1:0]  mem [DEPTH];
  logic [AW-1:0] wptr, rptr;
  assign rx_valid = (rx_count != '0);
  assign do_pop   = rx_pop && rx_valid;
  assign rx_data  = mem[rptr];

  always_ff @(posedge clk) begin
    if (rst) begin
      wptr     <= '0;
      rptr     <= '0;
      rx_count <= '0;
    end else begin
      if (link_vld) begin                 // no full check: credits guarantee a free slot
        mem[wptr] <= link_data;
        wptr      <= wptr + 1'b1;
      end
      if (do_pop) rptr <= rptr + 1'b1;
      rx_count <= rx_count + CW'(link_vld) - CW'(do_pop);
    end
  end
endmodule
credit_link_fv.sv
// credit_link_fv.sv — formal wrapper: conservation, overflow, overdraft, deadlock
module credit_link_fv #(
  parameter int DEPTH = 4,
  parameter int W     = 2,
  parameter bit BUG   = 1'b0
) (
  input logic         clk,
  input logic         rst,
  input logic         tx_valid,
  input logic [W-1:0] tx_data,
  input logic         rx_pop
);
  localparam int CW = $clog2(DEPTH + 1);
  logic          tx_take, rx_valid, link_vld, cred_ret;
  logic [W-1:0]  rx_data;
  logic [CW-1:0] credits, rx_count;

  credit_link #(.DEPTH(DEPTH), .W(W), .BUG(BUG)) dut (.*);

  initial assume (rst);
  default clocking cb @(posedge clk); endclocking
  default disable iff (rst);

  // auxiliary: where are all the credits?
  wire [7:0] total = 8'(credits) + 8'(link_vld) + 8'(rx_count) + 8'(cred_ret);

  a_conserve:      assert property (total == 8'(DEPTH));
  a_no_overdraft:  assert property (tx_take |-> credits != '0);
  a_credit_max:    assert property (credits <= CW'(DEPTH));
  a_no_overflow:   assert property (link_vld |-> rx_count < CW'(DEPTH));
  a_no_deadlock:   assert property (!(credits == '0 && rx_count == '0 && !link_vld && !cred_ret));
  a_work_conserve: assert property (tx_valid && credits != '0 |-> tx_take);

  c_no_credits:    cover property (credits == '0);
  c_buffer_full:   cover property (rx_count == CW'(DEPTH));
  c_send_and_ret:  cover property (tx_take && cred_ret);
  c_full_drain:    cover property (rx_count == CW'(DEPTH) ##[1:$] credits == CW'(DEPTH));
endmodule
credit_link.sby
# credit_link.sby
[tasks]
prove
cover
bug

[options]
prove: mode prove
bug:   mode prove
cover: mode cover
depth 16

[engines]
prove: abc pdr
bug:   smtbmc
cover: smtbmc

[script]
read -formal credit_link.sv credit_link_fv.sv
bug: chparam -set BUG 1 credit_link_fv
prep -top credit_link_fv

[files]
credit_link.sv
credit_link_fv.sv

What happens

  • prove passes: all six assertions hold for the correct counter.
  • bug fails a_conserve in a few cycles: a pop returns a credit in the same cycle as a send, and the counter only subtracts. One credit is gone forever.
  • Interesting detail: a_no_deadlock still holds with the bug. Losing a credit needs a send and a return in the same cycle, which needs at least two credits in circulation — so the link always keeps the last one. The buggy link never hangs; it just slows down to one transfer per round trip.

Chapter takeaways

Discussion

No comments yet.

Was this page helpful?

Tip: select any text on the page before clicking Report and it will be quoted in your report.