[Verilog HDL]

VERILOG分配语句

[复制链接]
2454|1
手机看帖
扫描二维码
随时随地手机跟帖
gaochy1126|  楼主 | 2023-5-29 14:52 | 显示全部楼层 |阅读模式

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

赋值语法

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

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示意图。

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

wire [1:0]   a;
wire         b;

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

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

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

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

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

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

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

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

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

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

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

endmodule


使用特权

评论回复

相关帖子

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

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

隐式连续分配

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

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

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

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

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

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

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

module combo
(  input   a, b, c, d,
                output  o);

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

endmodule

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

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

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


使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

1025

主题

11271

帖子

24

粉丝