Daily record (23) code familiarity

'assignment process

module datt ();
    reg [7:0] mem;
    initial begin
        $monitor("ans is %b", mem);
    end

    initial begin
        mem=0;
        #10 mem=1;
        #10 mem='1;
        #10 $finish;
    end

    bit[7:0] mem2[3];
    initial begin
        mem2[0]='x;
        mem2[1]='1;
        mem2[2]=2;
        foreach(mem2[i])
            $display("mem2 %b", mem2[i]);
    end
endmodule

output

mem2 00000000
mem2 11111111
mem2 00000010
ans is 00000000
ans is 00000001
ans is 11111111
$finish called from file "datt.v", line 11.
$finish at simulation time                   30

  

foreach, initialization assignment {}, dynamic array, queue

When using {} for initialization assignment, 'can be used for abbreviation, and constants can be described without single quotation marks.

module tcc ();
    int a[10]='{10{16'hfafa}};
    int b[]={1,3,4};
    initial begin
        a[0]=3;
        a[1]=5;
        b=a;
        b[0]=1;
        foreach(a[i])
            $display("a %b",a[i]);

        foreach(b[i])
            $display("b %b",b[i]);
    end

    int c[$]='{5{11}};
    int d[$]='{1,2,3,4,56};
    int e[5]={1,2,3,4,5};
    initial begin
        c.insert(2,0);
        c.insert(6,1);
        // out of bound will be wrong in run time. insert in the tail is use the finall index+1
        for(int i=0;i<c.size();i++)
            $display("the queue value is %b",c[i]);
    end
    initial begin
        for(int i=0;i<d.size();i++)
            $display("the queue value d is %b",d[i]);
    end
endmodule

output

a 00000000000000000000000000000011
a 00000000000000000000000000000101
a 00000000000000001111101011111010
a 00000000000000001111101011111010
a 00000000000000001111101011111010
a 00000000000000001111101011111010
a 00000000000000001111101011111010
a 00000000000000001111101011111010
a 00000000000000001111101011111010
a 00000000000000001111101011111010
b 00000000000000000000000000000001
b 00000000000000000000000000000101
b 00000000000000001111101011111010
b 00000000000000001111101011111010
b 00000000000000001111101011111010
b 00000000000000001111101011111010
b 00000000000000001111101011111010
b 00000000000000001111101011111010
b 00000000000000001111101011111010
b 00000000000000001111101011111010
the queue value is 00000000000000000000000000001011
the queue value is 00000000000000000000000000001011
the queue value is 00000000000000000000000000000000
the queue value is 00000000000000000000000000001011
the queue value is 00000000000000000000000000001011
the queue value is 00000000000000000000000000001011
the queue value is 00000000000000000000000000000001
the queue value d is 00000000000000000000000000000001
the queue value d is 00000000000000000000000000000010
the queue value d is 00000000000000000000000000000011
the queue value d is 00000000000000000000000000000100
the queue value d is 00000000000000000000000000111000

  

Associative array, itoa function

sv's syntax allows variables to be defined in initial, but it seems that they can only be defined at the beginning. Other definitions will report errors. In Verilog syntax, it is irrelevant. It must be outside initial and always.

module tdd ();
initial begin

    bit [63:0] assoc1[bit[63:0]], idx=1;
    int assoc2[string];
    string idx2_str;
    int idx2=123;

    repeat (64) begin
        assoc1[idx]=idx;
        idx=idx<<1;
    end

    foreach(assoc1[i])
    $display("the assoc1 is %b", assoc1[i]);


    repeat (64) begin
        idx2_str.itoa(idx2);
        assoc2[idx2_str]=idx2;
        ++idx2;
    end
    foreach(assoc2[i])
    $display("%s, the assoc2 is %b",i, assoc2[i]);

end

reg xx;
endmodule

output

  1 the assoc1 is 0000000000000000000000000000000000000000000000000000000000000001
  2 the assoc1 is 0000000000000000000000000000000000000000000000000000000000000010
  3 the assoc1 is 0000000000000000000000000000000000000000000000000000000000000100
  4 the assoc1 is 0000000000000000000000000000000000000000000000000000000000001000
  5 the assoc1 is 0000000000000000000000000000000000000000000000000000000000010000
  6 the assoc1 is 0000000000000000000000000000000000000000000000000000000000100000
  7 the assoc1 is 0000000000000000000000000000000000000000000000000000000001000000
  8 the assoc1 is 0000000000000000000000000000000000000000000000000000000010000000
  9 the assoc1 is 0000000000000000000000000000000000000000000000000000000100000000
 10 the assoc1 is 0000000000000000000000000000000000000000000000000000001000000000
 11 the assoc1 is 0000000000000000000000000000000000000000000000000000010000000000
 12 the assoc1 is 0000000000000000000000000000000000000000000000000000100000000000
 13 the assoc1 is 0000000000000000000000000000000000000000000000000001000000000000
 14 the assoc1 is 0000000000000000000000000000000000000000000000000010000000000000
 15 the assoc1 is 0000000000000000000000000000000000000000000000000100000000000000
 16 the assoc1 is 0000000000000000000000000000000000000000000000001000000000000000
 17 the assoc1 is 0000000000000000000000000000000000000000000000010000000000000000
 18 the assoc1 is 0000000000000000000000000000000000000000000000100000000000000000
 19 the assoc1 is 0000000000000000000000000000000000000000000001000000000000000000
 20 the assoc1 is 0000000000000000000000000000000000000000000010000000000000000000
 21 the assoc1 is 0000000000000000000000000000000000000000000100000000000000000000
 22 the assoc1 is 0000000000000000000000000000000000000000001000000000000000000000
 23 the assoc1 is 0000000000000000000000000000000000000000010000000000000000000000
 24 the assoc1 is 0000000000000000000000000000000000000000100000000000000000000000
 25 the assoc1 is 0000000000000000000000000000000000000001000000000000000000000000
 26 the assoc1 is 0000000000000000000000000000000000000010000000000000000000000000
 27 the assoc1 is 0000000000000000000000000000000000000100000000000000000000000000
 28 the assoc1 is 0000000000000000000000000000000000001000000000000000000000000000
 29 the assoc1 is 0000000000000000000000000000000000010000000000000000000000000000
 30 the assoc1 is 0000000000000000000000000000000000100000000000000000000000000000
 31 the assoc1 is 0000000000000000000000000000000001000000000000000000000000000000
 32 the assoc1 is 0000000000000000000000000000000010000000000000000000000000000000
 33 the assoc1 is 0000000000000000000000000000000100000000000000000000000000000000
 34 the assoc1 is 0000000000000000000000000000001000000000000000000000000000000000
 35 the assoc1 is 0000000000000000000000000000010000000000000000000000000000000000
 36 the assoc1 is 0000000000000000000000000000100000000000000000000000000000000000
 37 the assoc1 is 0000000000000000000000000001000000000000000000000000000000000000
 38 the assoc1 is 0000000000000000000000000010000000000000000000000000000000000000
 39 the assoc1 is 0000000000000000000000000100000000000000000000000000000000000000
 40 the assoc1 is 0000000000000000000000001000000000000000000000000000000000000000
 41 the assoc1 is 0000000000000000000000010000000000000000000000000000000000000000
 42 the assoc1 is 0000000000000000000000100000000000000000000000000000000000000000
 43 the assoc1 is 0000000000000000000001000000000000000000000000000000000000000000
 44 the assoc1 is 0000000000000000000010000000000000000000000000000000000000000000
 45 the assoc1 is 0000000000000000000100000000000000000000000000000000000000000000
 46 the assoc1 is 0000000000000000001000000000000000000000000000000000000000000000
 47 the assoc1 is 0000000000000000010000000000000000000000000000000000000000000000
 48 the assoc1 is 0000000000000000100000000000000000000000000000000000000000000000
 49 the assoc1 is 0000000000000001000000000000000000000000000000000000000000000000
 50 the assoc1 is 0000000000000010000000000000000000000000000000000000000000000000
 51 the assoc1 is 0000000000000100000000000000000000000000000000000000000000000000
 52 the assoc1 is 0000000000001000000000000000000000000000000000000000000000000000
 53 the assoc1 is 0000000000010000000000000000000000000000000000000000000000000000
 54 the assoc1 is 0000000000100000000000000000000000000000000000000000000000000000
 55 the assoc1 is 0000000001000000000000000000000000000000000000000000000000000000
 56 the assoc1 is 0000000010000000000000000000000000000000000000000000000000000000
 57 the assoc1 is 0000000100000000000000000000000000000000000000000000000000000000
 58 the assoc1 is 0000001000000000000000000000000000000000000000000000000000000000
 59 the assoc1 is 0000010000000000000000000000000000000000000000000000000000000000
 60 the assoc1 is 0000100000000000000000000000000000000000000000000000000000000000
 61 the assoc1 is 0001000000000000000000000000000000000000000000000000000000000000
 62 the assoc1 is 0010000000000000000000000000000000000000000000000000000000000000
 63 the assoc1 is 0100000000000000000000000000000000000000000000000000000000000000
 64 the assoc1 is 1000000000000000000000000000000000000000000000000000000000000000
 65 123, the assoc2 is 00000000000000000000000001111011
 66 124, the assoc2 is 00000000000000000000000001111100
 67 125, the assoc2 is 00000000000000000000000001111101
 68 126, the assoc2 is 00000000000000000000000001111110
 69 127, the assoc2 is 00000000000000000000000001111111
 70 128, the assoc2 is 00000000000000000000000010000000
 71 129, the assoc2 is 00000000000000000000000010000001
 72 130, the assoc2 is 00000000000000000000000010000010
 73 131, the assoc2 is 00000000000000000000000010000011
 74 132, the assoc2 is 00000000000000000000000010000100
 75 133, the assoc2 is 00000000000000000000000010000101
 76 134, the assoc2 is 00000000000000000000000010000110
 77 135, the assoc2 is 00000000000000000000000010000111
 78 136, the assoc2 is 00000000000000000000000010001000
 79 137, the assoc2 is 00000000000000000000000010001001
 80 138, the assoc2 is 00000000000000000000000010001010
 81 139, the assoc2 is 00000000000000000000000010001011
 82 140, the assoc2 is 00000000000000000000000010001100
 83 141, the assoc2 is 00000000000000000000000010001101
 84 142, the assoc2 is 00000000000000000000000010001110
 85 143, the assoc2 is 00000000000000000000000010001111
 86 144, the assoc2 is 00000000000000000000000010010000
 87 145, the assoc2 is 00000000000000000000000010010001
 88 146, the assoc2 is 00000000000000000000000010010010
 89 147, the assoc2 is 00000000000000000000000010010011
 90 148, the assoc2 is 00000000000000000000000010010100
 91 149, the assoc2 is 00000000000000000000000010010101
 92 150, the assoc2 is 00000000000000000000000010010110
 93 151, the assoc2 is 00000000000000000000000010010111
 94 152, the assoc2 is 00000000000000000000000010011000
 95 153, the assoc2 is 00000000000000000000000010011001
 96 154, the assoc2 is 00000000000000000000000010011010
 97 155, the assoc2 is 00000000000000000000000010011011
 98 156, the assoc2 is 00000000000000000000000010011100
 99 157, the assoc2 is 00000000000000000000000010011101
100 158, the assoc2 is 00000000000000000000000010011110
101 159, the assoc2 is 00000000000000000000000010011111
102 160, the assoc2 is 00000000000000000000000010100000
103 161, the assoc2 is 00000000000000000000000010100001
104 162, the assoc2 is 00000000000000000000000010100010
105 163, the assoc2 is 00000000000000000000000010100011
106 164, the assoc2 is 00000000000000000000000010100100
107 165, the assoc2 is 00000000000000000000000010100101
108 166, the assoc2 is 00000000000000000000000010100110
109 167, the assoc2 is 00000000000000000000000010100111
110 168, the assoc2 is 00000000000000000000000010101000
111 169, the assoc2 is 00000000000000000000000010101001
112 170, the assoc2 is 00000000000000000000000010101010
113 171, the assoc2 is 00000000000000000000000010101011
114 172, the assoc2 is 00000000000000000000000010101100
115 173, the assoc2 is 00000000000000000000000010101101
116 174, the assoc2 is 00000000000000000000000010101110
117 175, the assoc2 is 00000000000000000000000010101111
118 176, the assoc2 is 00000000000000000000000010110000
119 177, the assoc2 is 00000000000000000000000010110001
120 178, the assoc2 is 00000000000000000000000010110010
121 179, the assoc2 is 00000000000000000000000010110011
122 180, the assoc2 is 00000000000000000000000010110100
123 181, the assoc2 is 00000000000000000000000010110101
124 182, the assoc2 is 00000000000000000000000010110110
125 183, the assoc2 is 00000000000000000000000010110111
126 184, the assoc2 is 00000000000000000000000010111000
127 185, the assoc2 is 00000000000000000000000010111001
128 186, the assoc2 is 00000000000000000000000010111010
129            V C S   S i m u l a t i o n   R e p o r t 
130 Time: 0
131 CPU Time:      0.120 seconds;       Data structure size:   0.0Mb

 

Logical judgment of merging arrays and x

module tee ();
reg [7:0] mem[7]='{7{4'b0011}};
bit [7:0] mem2[2][3]='{'{3{15}}, '{3,4,5}};

initial begin
    foreach(mem[i])
    begin
        $display("reg mem %b", mem[i]);
    end
end

initial begin
    for(int i=0;i<2;i++)
        for(int j=0;j<3;j++)
            $display("reg mem2 %b", mem2[i][j]);
    mem[0]=1;
    mem[1]=2;
    $display("the sum value is %d",mem.sum() with(item<3));
    $display("the sum value is %d",mem.sum with((item<3) *1));
    $display("logic value %b", 4'b0100>4'bxxzz);
end
endmodule

output

 1 reg mem 00000011
 2 reg mem 00000011
 3 reg mem 00000011
 4 reg mem 00000011
 5 reg mem 00000011
 6 reg mem 00000011
 7 reg mem 00000011
 8 reg mem2 00001111
 9 reg mem2 00001111
10 reg mem2 00001111
11 reg mem2 00000011
12 reg mem2 00000100
13 reg mem2 00000101
14 the sum value is 0
15 the sum value is          2
16 logic value x

 

Structure, flow operator, merge structure

In addition, union is a union and enum is an enumeration. It is better to use typedef for definition_ s,_ u,_ e is the type suffix, indicating structure, union, enumeration, etc.

The structure can be integrated and can be used on contents such as pixels.

module tff ();
int adx[];
initial begin
    adx = new[5];
end

initial begin
    typedef struct{
        logic valid;
    logic [7:0] tags;
    logic [31:0] data;
    } data_word;
    data_word st1='{1'b1, 2'b10, 3'b111};
    //data_word st1={1'b1, 2'b10, 3'b111};
    
    data_word a1;
    a1.valid=1;
    a1.tags=7'b1100_0011;
    a1.data=8'hfa;
    $display("a1 is %b, %b, %b", a1.valid, a1.tags, a1.data);
    a1={>>{st1}};
    $display("after a1 is %b, %b, %b", a1.valid, a1.tags, a1.data);
end

initial begin
    typedef struct packed{
        logic valid;
    logic [7:0] tags;
    logic [31:0] data;
    } data_word2_s;
    data_word2_s st={1'b1, 2'b10, 3'b111};
    data_word2_s a2;
    a2.valid=1;
    a2.tags=7'b100_0011;
    a2.data=8'hfa;
    $display("a2 is %b, %b, %b", a2.valid, a2.tags, a2.data);
    a2={>>{st}};
    $display("after a2 is %b, %b, %b", a2.valid, a2.tags, a2.data);
end

endmodule

output

When the merge structure is output, through the data flow, the final data is crowded on the last unit.

For non merged structures, i.e. ordinary structures, the assignment constant needs to be added ', otherwise it cannot be compiled.

Add a ' before concatenation operator to convert it to a valid assignment pattern.

a1 is 1, 01000011, 00000000000000000000000011111010
after a1 is 1, 00000010, 00000000000000000000000000000111
a2 is 1, 01000011, 00000000000000000000000011111010
after a2 is 0, 00000000, 00000000000000000000000000110111

  

Enumeration, exponent of real number, logical or of inside, equal sign wildcard, cast cast cast, cast of n ', property, unique

The logic or of inside should be distinguished from random constraints, which are different concepts. For another example, the assign statement is a different concept at the behavior level and the data flow level. For another example, the < = statement in the behavior level is sometimes a non blocking assignment, and sometimes a logical judgment, which is a different concept.

inside: https://blog.csdn.net/qq_22742971/article/details/115466654

When property and unique are used in case statements, the former must have a valid expression, and the latter means that there must be only one matching expression. Otherwise, the compilation process generates a warning.

module tgg ();
initial begin
    real a=3.1;
    typedef enum{
        R,G,B
        } color_e;
        color_e e1=R;
        int c;
        $display("the a is %.4f", a);
        $display("the squire of a is %4.f",a**2);

        $display("the squire of a is %.4f",a**2);
        $display("the inside method %b",2 inside{3,4});
        $display("the inside method %b",2 inside{3,2,4});
        $display("the compare ans is %b ", 4'b1011==?4'bxz11);
        $display("the compare ans is %b ", 4'b0011!=?4'b1x11);

        $display("typeof is %s", e1);
        e1++;
        $display("typeof is %s %d", e1, e1);
        c=e1;
        $cast(e1, c);
        $display("typeof is %s %d", e1, e1);
        $display("other cast method : %h",int'("a"));
        $display("other cast method : %h",8'(int'("a")));



end

    int irq;
always_comb
    priority case (10'd8)
    0: irq = 4'b0001;
    1: irq = 4'b0010;
    0: irq = 4'b0100;
    3: irq = 4'b1000;
endcase

always_comb
    unique case (10'd8)
    0: irq = 4'b0001;
    1: irq = 4'b0010;
    0: irq = 4'b0100;
    3: irq = 4'b1000;
endcase

endmodule

  

always_ff,always_comb,always_latch

Source: https://www.cnblogs.com/zeushuang/p/7966679.html

The first is trigger (sequential logic), the second is combinational logic, and the third is combinational logic that allows latch. It is said that it will warn, but I didn't.

module thh ();
int a=1,b=2,out;
always_comb
    if(a > b)
        out = 1;
    else
        out = 0;

int c=1,d=2,out2;
always_latch
   if(c > d)
        out2 = 1;

reg clk;
reg en;
reg in, out3;
always_ff @(posedge clk)
    if(en)
        out3 <= in;
endmodule

  

Added by aarons123 on Fri, 31 Dec 2021 11:56:58 +0200