日常记录(23)代码熟悉
'赋值过程
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
输出
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、初始化赋值{}、动态数组、队列
使用{}进行初始化赋值时候,'可以用在进行缩写的时候,而常量可以不用单引号进行说明。
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
输出
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
关联数组、itoa函数
sv的语法允许在initial中定义变量,但是似乎只能在开头定义。其它地方定义会报错。在Verilog语法中,则无关,一定在initial和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
输出

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
合并数组、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
输出

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
结构体、流操作符、合并结构
另外union为联合体、enum为枚举。定义时候用typedef比较好,以_s、_u、_e为类型后缀,表示结构体、联合体、枚举等。
结构体可综合,在如像素等内容上可用。
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
输出
合并结构在输出的时候,通过数据流,则最终数据挤在了最后的单元上。
非合并结构,即普通结构体,赋值常量需要加',否则通不过编译。
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
枚举、实数的指数、inside的逻辑或、等号通配、cast强制转换、n'的强制转换、property、unique
inside的逻辑或应该和随机约束相区分,二者是不同的概念。又如assign语句在行为级和数据流级一样是不同的概念,又如行为级中的<=有时候是非阻塞赋值,有时候又是逻辑判断,是不同的概念。
inside:https://blog.csdn.net/qq_22742971/article/details/115466654
property和unique在case语句中使用时候,前者必须有一个表达式有效,后者表示匹配的表达式必须只有一个。否则编译过程产生警告。
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
输出:
the a is 3.1000 the squire of a is 10 the squire of a is 9.6100 the inside method 0 the inside method 1 the compare ans is 1 the compare ans is 1 typeof is R typeof is G 1 typeof is G 1 other cast method : 00000061 other cast method : 61 Warning-[RT-NCMPRCS] No condition matches in statement aaa.sv, 30 No condition matches in 'priority case' statement. 'default' specification is missing, inside tgg, at time 0s. Warning-[RT-NCMUCS] No condition matches in statement aaa.sv, 38 No condition matches in 'unique case' statement. 'default' specification is missing, inside tgg, at time 0s.
always_ff、always_comb、always_latch
来源:https://www.cnblogs.com/zeushuang/p/7966679.html
第一个是触发器(时序逻辑),第二个是组合逻辑,第三个是允许latch的组合逻辑。据说会警告,但是我没有。
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
Le vent se lève! . . . il faut tenter de vivre!
Le vent se lève! . . . il faut tenter de vivre!