Write one covergroup with value and ref arguments, so that four instances cover four different ports.
Hardware context: A 4-port switch whose ports allow different packet lengths. Cover every port with one covergroup type, built once per port with its own range and weight.
A switch has four ports. Each port has its own largest packet length, and the plan weights length coverage more on some ports. Today the testbench has four hand-edited covergroups.
Write one covergroup port_cov. The testbench builds an array of four instances, one per port, each tracking that port's length and direction variables.
Signals in the Timeline
| Signal | Width | Role |
|---|---|---|
samples | 8 | input of each check |
Full specification
bit [5:0] plen [4]; bit pwr [4];
port_cov pc [4];
int MAXL [4] = '{8, 16, 32, 63}; // largest length per port
int LW [4] = '{1, 2, 3, 1}; // weight of the length coverpoint per port
foreach (pc[k]) pc[k] = new(plen[k], pwr[k], MAXL[k], LW[k]);
// a sample on port p: plen[p] = n; pwr[p] = w; pc[p].sample();
| Coverpoint | Bins |
|---|---|
| sizes (len) | lengths 1 to max_len in 4 groups, dealt as s[4] deals them; option.weight = len_weight |
| dir (wr) | automatic: read and write |
Each case builds a fresh array, samples it, and reports the sum over the four ports of get_inst_coverage() times 100, each rounded. A port with no samples adds 0. The full check samples more lists than the examples show.
Examples
Example 1
One Packet on Port 0- Input
packets (port, len, write): (0, 3, 1)
- Output
coverage_percent_x100 = 3750
Port 0: sizes 1 of 4 (weight 1), dir 1 of 2: (25 + 50) / 2 = 37.50%, so 3750. The other ports add 0.
Example 2
The Same Length, Two Ports- Input
packets (port, len, write): (0, 8, 0) (1, 8, 1)
- Output
coverage_percent_x100 = 7083
Length 8 is the last group of port 0 (<7,8>) but the second of port 1 (<5..8>). Port 1 weights sizes 2: (25 x 2 + 50) / 3 = 33.33%.
Example 3
Port 3- Input
packets (port, len, write): (3, 2, 0) (3, 60, 1)
- Output
coverage_percent_x100 = 7500
2 is in the first group and 60 in the last (<46..63>): sizes 2 of 4, dir 2 of 2: 75.00%.
Constraints and Contract
- Keep the name and the argument list:
(ref bit [5:0] len, ref bit wr, input int max_len, input int len_weight). - The coverage build rejects an argument inside a bin range (lesson C19): filter a constant range with the argument instead.
Hints
Hint 1
A ref argument is read at every sample; a value argument only at new.
Hint 2
Which values does {[1:63]} with (item <= max_len) keep, and how are they dealt into 4 bins?
Hint 3
A coverpoint option can be set from an argument.
What the grader checks: Four instances of your covergroup are built per case and sampled with 18 lists of packets. The sum of their instance numbers is compared with the values IEEE 1800-2023 clause 19 gives. Three lists are shown.
18 checks on Verilator coverage build · server. Run the examples first, then check the full regression. Full checks use the server judge. Downloads contain public samples only. A passing result establishes the tested cases, not a proof for all possible inputs.
See all 3 steps and the final capstone