verilog中赋值语句assign 您所在的位置:网站首页 verilogcase语句用法 verilog中赋值语句assign

verilog中赋值语句assign

#verilog中赋值语句assign| 来源: 网络整理| 查看: 265

wire类型或类似于wire数据类型的信号需要连续赋值。类比用于连接面包板上零件的电线。只要+5V电池施加在导线的一端,连接到导线另一端的部件就会获得所需的电压。

在Verilog中,这个概念是通过assign语句实现的,其中任何或其他类似的wire数据类型都可以用一个值连续驱动。该值可以是常数,也可以是由一组信号组成的表达式。

赋值语法

赋值语法以关键字assign开头,后面跟着单个信号或不同信号网的级联的信号名。驱动强度和延迟是可选的,主要用于数据流建模,而不是综合到实际硬件中。右侧的表达式或信号被评估并分配给左侧的线网wire或wire的表达式。

assign = [drive_strength][delay]

延迟值用于指定门的延迟,并用于对实际硬件中的时序行为进行建模,因为该值决定了何时应为线网wire分配评估值。

规则

使用assign语句时需要遵守一些规则:

左值应该始终是标量或向量wire线网类型数据,或者标量或向量线网的级联,而不是标量或向量寄存器。右值可以包含标量或矢量寄存器和函数调用。每当右值上的任何操作数的值发生变化时,左值都将更新为新值。assign语句也称为连续赋值,并且始终处于活动状态。

例子#1

在下面的示例中,一个名为out的线网变量由信号i1和i2的逻辑与&表达式连续驱动。

如果将导线转换为端口并进行综合,我们将在合成后得到如下所示的RTL电路图:

在verilog中,连续赋值语句可以用来表示组合逻辑门电路。

例子#2

下面的例子的模块接受两个输入,并使用assign语句进行部分选择和多位级联来驱动输出z。将每个case都视为模块中唯一的代码,否则同一信号上的许多assign语句肯定会使输出变成X。

module xyz (input [3:0] x, // x is a 4bit vector net input y, // y is a 1bit scalar net output [4:0] z); // z is a 5bit 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 vlaue z=b'11001 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 the 3-bit nets of 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 4-bits of z is driven with a constant // value of 1. Now z = 'b1001z beacuse only bit0 remains undriven assign z[3:1] = {x,y}; assign z[4] = 0; // Case #4; Assume bit3 is driven instead,but now there are two drivers for bit3, // and both are driving the same alue of 0, so there should be no contention and // value of 1. Now z = 'bZ001z assign z[3:1] = {x,y}; assign z[3] = 0; // Case #5; Assume bit3 is driven with value 1,so now there are two drivers // with different values, where the first line is driven with the value X which // at the time 0 and the second assignment where it is driven with value 1.so // now it becomes unknow which will win value of z = 'bZX01Z assign z[3:1] = {x,y}; assign z[3] = 1; // Case #6; Partical 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 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 {b,a}= {x,y}; endmodule 赋值寄存器变量

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

隐式连续赋值

当使用assign语句为给定的线网类型赋值时,它被称为显式赋值,Verilog也允许在声明线网类型时进行赋值,称为隐式赋值。

wire [1:0] a; assign a= x& y; // Explicit assignment wire [1:0] b= x&y; // plicit assignment 组合逻辑设计

考虑以下由组合门组成的数字电路并思考verilog代码实现。

组合逻辑需要连续驱动输入以保持输出,不像触发器等时序逻辑,在触发器中值被捕获并存储在时钟的边沿。因此,assign语句满足上面的要去,因为无论何时右侧的任何输入发生变化,输出o都会更新。

// the module takes four inputs and performs a boolean // operation and assigns output to o. // the combinational logic is realized using assign statement module combo (input a,b,c,d, output o); assign o=~((a & b) | c ^ d); endmodule

硬件电路图

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

在仿真的波形图中,只要右边上的组合表达式为真,信号就会变为1。当右边的组合表达是为false时,情况也会类似。从开启到10ns的输出是X,因为在同一时间内输入是X。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有