赋值操作(Assignment operators)
- +=
- -=
- *=
- /=
- %=
- &=
- |=
- ^=
- <<=
- >>=
- <<<=
- >>>=
左移<<=等同于<<<=,右移>>>=,高位补符号位,>>=补0
module Assignment();
//integer a_init = 12;
integer a_init = -12;
integer a = a_init;
initial begin
$display ("a := %b", a);
a <<= 2;
$display ("a <<=2 := %b", a);
a = a_init;
a <<<= 2;
$display ("a <<<= 2 := %b", a);
a = a_init;
a >>= 2;
$display ("a >>= 2 := %b", a);
a = a_init;
a >>>= 2;
$display ("a >>>= 2 := %b", a);
#1 $finish;
end
endmodule
11_1111111111111111111111111101_00(a)
11_1111111111111111111111110100_00(a <<= 2)
11_1111111111111111111111110100_00(a <<<= 2)
00_1111111111111111111111111111_01(a >>= 2)
11_1111111111111111111111111111_01(a >>>= 2)
模糊相等/不等(Wild equality and wild inequality)
module wild_equality_operator ();
bit [7:0] data = 8'hFF;
initial begin
// Compare with wild equality
if (data =?= 8'b1xxx_z1xz) begin
$display ("Data %b matches with %b", data, 8'b1xxx_z1xz);
end
// Compare with wild non-equality
if (data ! ?= 8'b1xxx_z1x0) begin
$display ("Data %b does not matches with %b", data, 8'b1xxx_z1x0);
end
#1 $finish;
end
endmodule
流操作(Streaming operators)
这个有点难理解,类似c++中的cout << data1 << data2; cin >> data3 >> data4,不过感觉很灵活
dule streaming();
//-------------------------------
// PACK Example
//-------------------------------
int j = { "A", "B", "C", "D" };
bit [31:0] stream;
initial begin
$display(" PACK");
$display("Value of j %0x",j);
$monitor("@%0dns stream value is %x",$time, stream);
#1 stream = { << byte {j}};
#1 stream = { >> {j}} ;
#1 stream = { << { 8'b0011_0101 }};
#1 stream = { << 16 {j}};
#1 stream = { << 4 { 6'b11_0101 }};
#1 stream = { >> 4 { 6'b11_0101 }} ;
#1 stream = { << 2 { { << { 4'b1101 }} }};
end
//-------------------------------
// UNPACK Example
//-------------------------------
int a, b, c;
logic [10:0] up [3:0];
logic [11:1] p1, p2, p3, p4;
bit [96:1] y;
int j ;
bit [99:0] d;
initial begin
#20 ;
$display(" UNPACK");
// Below line should give compile error
//{>>{ a, b, c }} = 23'b1;
{>>{ a, b, c }} = 96'b1;
$display("@%0dns a %x b %x c %x",$time,a,b,c);
{>>{ a, b, c }} = 100'b1;
$display("@%0dns a %x b %x c %x",$time,a,b,c);
{ >> {p1, p2, p3, p4}} = up;
$display("@%0dns p1 %x p2 %x p3 %x p4 %x",$time,p1,p2,p3,p4);
y = {>>{ a, b, c }};
$display("@%0dns y %x",$time,y);
// Below line should give compile error
//j = {>>{ a, b, c }};
d = {>>{ a, b, c }};
$display("@%0dns d %x",$time,d);
end
endmodule
输出
PACK
Value of j 41424344
@0ns stream value is 00000000
@1ns stream value is 44434241
@2ns stream value is 41424344
@3ns stream value is 000000ac
@4ns stream value is 43444142
@5ns stream value is 0000004d
@6ns stream value is d4000000
@7ns stream value is 0000000e
UNPACK
@20ns a 00000000 b 00000000 c 00000001
@20ns a 00000000 b 00000000 c 00000000
@20ns p1 xxx p2 xxx p3 xxx p4 xxx
@20ns y 000000000000000000000000
@20ns d 0000000000000000000000000
成员(Set membership)
module set_member();
int array [$] = {1,2,3,4,5,6,7};
int check = 0;
initial begin
if (check inside {array}) begin
$display("check is inside array");
end else begin
$display("check is not inside array");
end
check = 5;
if (check inside {array}) begin
$display("check is inside array");
end else begin
$display("check is not inside array");
end
check = 1;
// Constant range
if (check inside {[0:10]}) begin
$display("check is inside array");
end else begin
$display("check is not inside array");
end
end
endmodule
check is not inside array
check is inside array
check is inside array