// DESCRIPTION: Verilator: Verilog Test module
//
// This file ONLY is placed under the Creative Commons Public Domain
// SPDX-FileCopyrightText: 2026 Wilson Snyder
// SPDX-License-Identifier: CC0-1.0

// Test transition bins: simple 2-value, 3-value sequences, array bins,
// and multi-value items in transition steps.

// verilog_format: off
`define stop $stop
`define checkr(gotv,expv) do if ((gotv) != (expv)) begin $write("%%Error: %s:%0d:  got=%f exp=%f\n", `__FILE__,`__LINE__, (gotv), (expv)); `stop; end while(0);
// verilog_format: on

module t;
  logic [2:0] state;

  covergroup cg;
    // Simple 2-value transitions
    cp_trans2: coverpoint state {
      bins trans1 = (0 => 1); bins trans2 = (1 => 2); bins trans3 = (2 => 3);
    }
    // 3-value sequence transitions
    cp_trans3: coverpoint state {
      bins seq_a = (0 => 1 => 2); bins seq_b = (2 => 3 => 4);
    }
    // Array bins: creates a separate bin per listed transition
    cp_array: coverpoint state {
      bins arr[] = (0 => 1), (1 => 2), (2 => 3);
    }
    // Multi-value item (comma list) in transition: matches 1 or 2 in second step
    cp_multi_item: coverpoint state {
      bins multi = (0 => 1, 2);  // second element is a two-value list
    }
    // Repetition-type bins: goto ([->N]) and non-consecutive ([=N]) repetition
    cp_reptype: coverpoint state {
      bins goto_bin = (0 => 1 [-> 2]);  // goto repetition - currently matched as simple (0=>1)
      bins noncons_bin = (2 [=1] => 3);  // non-consecutive repetition - currently matched as simple (2=>3)
    }
  endgroup

  // Non-convertible coverpoint (it has a transition bin) that also carries a value-array bin,
  // so the legacy array codegen path (which converted coverpoints bypass) is still exercised.
  covergroup cg_legacy;
    cp_mix: coverpoint state {
      bins tr = (0 => 1);
      bins vals[] = {2, [4:5]};  // discrete + range elements (both legacy array paths)
    }
  endgroup

  cg cg_inst = new;
  cg_legacy cg_legacy_inst = new;

  initial begin
    // Drive sequence 0->1->2->3->4 which hits all bins
    state = 0;
    cg_inst.sample();
    `checkr(cg_inst.get_inst_coverage(), 0.0);
    state = 1;
    cg_inst.sample();  // 0=>1: trans1, seq_a pos1, arr[0=>1], multi
    state = 2;
    cg_inst.sample();  // 1=>2: trans2, seq_a done, arr[1=>2]
    state = 3;
    cg_inst.sample();  // 2=>3: trans3, seq_b pos1, arr[2=>3]
    state = 4;
    cg_inst.sample();  // 3=>4: seq_b done
    `checkr(cg_inst.get_inst_coverage(), 100.0);

    // cg_legacy: exercise legacy array codegen (non-convertible coverpoint)
    state = 0; cg_legacy_inst.sample();
    state = 1; cg_legacy_inst.sample();  // 0=>1: tr
    state = 2; cg_legacy_inst.sample();  // vals
    state = 3; cg_legacy_inst.sample();  // vals

    $write("*-* All Finished *-*\n");
    $finish;
  end

endmodule
