systemverilog learning----coverage completion&& array operation method 1

cross coverage

Cross coverage refers to the coverage point or the value of a variable. Previously we only discussed all the values ​​of one variable as coverage points, but we can also use the permutation and combination of the values ​​of two variables as coverage points. Expressions cannot be used in cross, a coverage point must be clearly defined beforehand.

bit [3:0] a, b;
covergroup cg @(posedge clk);
	c1:coverpoint a;
	c2:coverpoint b;
	c1Xc2: cross c1, c2;
endgroup:cg

In the above example, two coverage points c1 and c2 are defined, and an intersection coverage c1Xc2 is defined based on c1,c2.

bit [3:0] a,b;
covergroup cov @(posedge clk);
	aXb: cross a,b;
endgroup

In the above example, each coverage point has 16 bins, namely auto[0], auto[1],...,auto[15]. The intersection of variables a and b is marked by aXb. Crossed together, there are 256 possible values.

bit [3:0] a, b, c;
covergroup cov @(posedge clk);
	BC:	coverpoint b+c;
	aXb: cross a, BC;
endgroup

In the above example, this covering combination still has 256 possible value combinations. It is more complicated. You can use variables and expressions at the same time. The BC value covers all possible value combinations of b+c, and aXb is The value set of variable a and coverage point BC.

cover option

We can control the behavior of covergroup, coverpoint and cross through some switch options.
at_least
specifies the number of hits to bins. As mentioned earlier, if the value falls into the bins range, bins will increase automatically. When the number of bins hits is less than the specified value, it is considered that the bins are not covered at this time. The default is 1.
auto_bin_max
will be created automatically when we create bins without displaying them. Changing the option can automatically specify the number of automatically created bins. The default value is 64.
cross_auto_bin_max
is similar to auto_bin_max, but this applies to cross coverage.
The following example shows how to use option

covergroup cg @(poseedge clk);
	c1:	coverpoint addr	{
    
    option.auto_bin_max = 128;}
	c2:	coverpoint wr_rd {
    
    option.atleast = 2;}
	c2Xc2: cross c1, c2 {
    
    option.cross_auto_bin_max = 128;}
endgroup

Array methods

Arrays include arrays, dynamic arrays, and queues. The following mainly discusses methods of manipulating array data.

Parametric definition

There are two ways to define constants, as follows:

  • parameter
  • `define

The parameter
parameter must be defined inside the module and must be modified with the keyword parameter. A parameter is a constant inside a module that can be optionally redefined outside the module. It is generally used to specify the bit width or time delay of the variable.

module mem_model #(
 parameter ADDR_WIDTH=8;
 parameter DATA_WIDTH=32;)
 (clk, addr, data);
 
 input  clk;
 input  [ADDR_WIDTH-1:0] addr;
 output [DATA_WIDTH-1:0] data;
 .....
 .....
endmodule

`define
modifies a global variable that can be accessed by all files. There are two situations when it changes. One is that another macro definition changes the value, or the undef statement is used.

`define WIDTH 8
//to avoid redefincation `ifdef can be used

`ifdef WIDTH
// do nothing (better to use `ifndef)
`else
`define WIDTH 8

`ifndef WIDTH
    `define WIDTH 8
`endif

`ifdef can be used as if   else

`ifdef TYPE_1
    `define WIDTH 8
`else
    `define WIDTH 32
`endif

//`ifdef can also be used to avoid redefining/recompiling the module/class
//In the below example
//definition of MODULE_1 is checked, if it is not defined then MODULE_1 will be
//defined and compiles the module/class inside the `ifndef ---- `endif

`ifndef MODULE_1
`define MODULE_1
module mem;


endmodule
`endif

Array sorting method

This type of method handles one-dimensional arrays or queues and can reorder data elements. The following construction methods are available:

method describe
reverse() Reverse all elements in the array (encapsulated or not)
sort() Sort all elements in the array in ascending order
rsort() Sort all elements in an array in descending order
shuffle() Randomize the order of elements in an array, that is, scramble it
module fixedsize_array;

    //declaration of array's
    int array_1[4];
    int array_2[4];
    int array_3[4];
    int array_4[4];

    initial begin
        //array initialization
        array_1 = '{
    
    0,1,2,3};
        array_2 = '{
    
    2,3,1,0};
        array_3 = '{
    
    2,3,1,0};
        array_4 = '{
    
    0,1,2,3};

        $display("==============reverse==============");
        $display("Before:\t %p", array_1);
        array_1.reverse();
        $display("After :\t %p", array_1);
        $display("==================================");

        $display("==============sort==============");
        $display("Before:\t %p", array_2);
        array_2.sort();
        $display("After :\t %p", array_2);
        $display("==================================");

        $display("==============rsort==============");
        $display("Before:\t %p", array_3);
        array_3.rsort();
        $display("After :\t %p", array_3);
        $display("==================================");

        $display("==============shuffle==============");
        $display("Before:\t %p", array_4);
        array_4.shuffle();
        $display("After :\t %p", array_4);
        $display("==================================");



    end

endmodule

The execution results are as follows:
Insert image description here

class packet;
    int a;
    int b;

    function void display();
        $display("\t Value od a = %0d", a);
        $display("\t Value of b = %0d", b);
    endfunction
endclass

module assoc_array;

    //declaratio of array's
    packet array_1[*];

    packet pkt;

    initial begin
        pkt = new();
        pkt.a = 8;
        pkt.b = 3;
        array_1[3] = pkt;

        pkt = new();
        pkt.a = 0;
        pkt.b = 6;
        array_1[7] = pkt;

        pkt = new();
        pkt.a = 2;
        pkt.b = 1;
        array_1[9] = pkt;

        $display("======== sort ==========");
        $display("Before");
        foreach (array_1[i]) begin
            $display("array_1[%0d]:", i);
            array_1[i].display();
        end

        array_1.sort with (item.a);

        $display("\n After");
        foreach (array_1[i]) begin
            $display("array_1[%0d]:", i);
            array_1[i].display();
        end

        $display("===========================");
    end

endmodule

For sorting, if the array element is an instance of a class and the class has more than one variable, the sorting needs to specify which value should be used as the sorting criterion. In this case, you can provide with to specify the criterion. Associative arrays cannot be executed in qutasim, so the following experiment is run on vcs, and the execution results are as follows:
Insert image description here
It can be seen that we can use with(item.a) to sort the classes according to the size of the variable a.

Guess you like

Origin blog.csdn.net/weixin_45614076/article/details/126478210