004 操作和表达式(Operators And Expressions)

赋值操作(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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值