[Verilog HDL] VERILOG分配语句

[复制链接]
9679|1
 楼主| gaochy1126 发表于 2023-5-29 14:52 | 显示全部楼层 |阅读模式

类型为wire或类似数据类型的wire的信号需要连续分配值。 例如,考虑用于连接电路板上的部件的电线。 只要将+ 5V电池应用于导线的一端,连接到导线另一端的组件将获得所需的电压。
在Verilog中,这个概念是通过一个赋值语句实现的,其中任何连接或其他类似的连接(比如数据类型)都可以使用一个值连续驱动。该值可以是常量,也可以是由一组信号组成的表达式。

赋值语法

以关键字Assign开头,后跟信号名,可以是单个信号,也可以是不同信号网络的串联。驱动强度和延迟是可选的,主要用于数据流建模,而不是集成到实际硬件中。对右侧的表达式或信号求值并赋值给左侧的nets的表达式。

  1. assign <net_expression> = [drive_strength] [delay] <expression of different signals or constant value>


延迟值对于为gates指定延迟非常有用,并且用于在真实的硬件中建模定时行为,因为该值指示了何时应该用所评估的值分配网络(net)。

规则

使用assign语句时,需要遵循一些规则:
【1】LHS应该始终是标量或向量网络,或者标量或向量网络的串联,而绝对不能是标量或向量寄存器;
【2】RHS可以包含标量或向量寄存器和函数调用;
【3】只要RHS上的任何操作数的值发生变化,LHS就会使用新值进行更新;
【4】Assign语句也称为连续分配,并且始终处于活动状态。
在下面的例子中,一个叫out的网络是由一个信号表达式连续驱动的,i1和i2用逻辑&构成表达式。

如果将wire转换为端口并进行合成,我们将在合成后得到如下所示的RTL示意图。

  1. module xyz (input [3:0]   x,    // x is a 4-bit vector net
  2.             input         y,     // y is a scalar net (1-bit)
  3.             output [4:0]   z );   // z is a 5-bit vector net

  4. wire [1:0]   a;
  5. wire         b;

  6. // Assume one of the following assignments are chosen in real design
  7. // If x='hC and y='h1 let us see the value of z  

  8. // Case #1: 4-bits of x and 1 bit of y is concatenated to get a 5-bit net
  9. // and is assigned to the 5-bit nets of z. So value of z='b11001 or z='h19
  10. assign z = {x, y};

  11. // Case #2: 4-bits of x and 1 bit of y is concatenated to get a 5-bit net
  12. // and is assigned to selected 3-bits of net z. Remaining 2 bits of z remains
  13. // undriven and will be high-imp. So value of z='bZ001Z
  14. assign z[3:1] = {x, y};

  15. // Case #3: The same statement is used but now bit4 of z is driven with a constant
  16. // value of 1. Now z = 'b1001Z because only bit0 remains undriven
  17. assign z[3:1] = {x, y};
  18. assign z[4] = 1;

  19. // Case #4: Assume bit3 is driven instead, but now there are two drivers for bit3,
  20. // and both are driving the same value of 0. So there should be no contention and
  21. // value of z = 'bZ001Z
  22. assign z[3:1] = {x, y};
  23. assign z[3] = 0;

  24. // Case #5: Assume bit3 is instead driven with value 1, so now there are two drivers
  25. // with different values, where the first line is driven with the value of X which
  26. // at the time is 0 and the second assignment where it is driven with value 1, so
  27. // now it becomes unknown which will win. So z='bZX01Z
  28. assign z[3:1] = {x, y};
  29. assign z[3] = 1;

  30. // Case #6: Partial selection of operands on RHS is also possible and say only 2-bits
  31. // are chosen from x, then z = 'b00001 because z[4:3] will be driven with 0
  32. assign z = {x[1:0], y};

  33. // Case #7: Say we explicitly assign only 3-bits of z and leave remaining unconnected
  34. // then z = 'bZZ001
  35. assign z[2:0] = {x[1:0], y};

  36. // Case #8: Same variable can be used multiple times as well and z = 'b00111
  37. // 3{y} is the same as {y, y, y}
  38. assign z = {3{y}};

  39. // Case #9: LHS can also be concatenated: a is 2-bit vector and b is scalar
  40. // RHS is evaluated to 11001 and LHS is 3-bit wide so first 3 bits from LSB of RHS
  41. // will be assigned to LHS. So a = 'b00 and b ='b1
  42. assign {a, b} = {x, y};

  43. // Case #10: If we reverse order on LHS keeping RHS same, we get a = 'b01 and b='b0
  44. assign {a, b} = {x, y};

  45. endmodule


 楼主| gaochy1126 发表于 2023-5-29 14:53 | 显示全部楼层
分配REG变量

用assign语句驱动或分配reg类型变量是非法的。 这是因为reg变量能够存储数据,并且不需要连续驱动。 reg信号只能在程序块(例如initial和always)中驱动。

隐式连续分配

当使用assign语句为给定net分配一些值时,这称为显式分配。 Verilog还允许在声明net时完成分配,这称为隐式分配。

  1. wire [1:0] a;
  2. assign a = x & y;       // Explicit assignment

  3. wire [1:0] a = x & y;   // Implicit assignment

  • 1
  • 2
  • 3
  • 4
  • 5
组合逻辑设计

考虑以下由组合门和相应的Verilog代码构成的数字电路。

组合逻辑要求连续驱动输入以维持输出,这与诸如触发器之类的顺序元件不同,在后者中,值被捕获并存储在时钟的边缘。 因此,assign语句非常适合此目的,因为只要右侧的任何输入发生更改,输出o都会更新。

  1. // 该模块接受四个输入并执行布尔运算,然后将输出分配给o。组合逻辑是使用assign语句实现的。

  2. module combo
  3. (  input   a, b, c, d,
  4.                 output  o);

  5.   assign o = ~((a & b) | c ^ d);

  6. endmodule

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
硬件原理图

经过详细的设计和综合后,我们确实看到了一个组合电路,其行为与assign语句建模的方式相同。

可以看出,只要RHS上的组合表达式为真,信号o就会变为1。同样,当RHS为假时,o变为0。输出o是从0ns到10ns的X,因为输入在同一时间是X。


您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:这个社会混好的两种人:一是有权有势,二是没脸没皮的。

1205

主题

11937

帖子

26

粉丝
快速回复 在线客服 返回列表 返回顶部