亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Industrial programming >> Verilog

理解 Verilog assign 语句:连续信号赋值详解

wire 类型或类似线网数据类型的信号需要连续赋值。例如面包板上连接元件的电线,只要一端接 +5V 电池,另一端连接的元件即获得所需电压。

Verilog 中通过 assign 语句实现此概念:任何 wire 或类似线网数据类型可被表达式持续驱动。值可以是常量或由一组信号组成的表达式。

assign 语法

赋值语法以关键字 assign 开始,后跟信号名(可以是单个信号或不同信号线网的拼接)。驱动强度 和 延迟 可选,主要用于数据流建模而非综合成真实硬件。右侧表达式或信号求值后赋给左侧线网或线网表达式。

assign  = [drive_strength] [delay] 

延迟值用于指定门延迟,建模真实硬件时序行为,因为该值决定何时将求值结果赋给线网。

规则

示例 #1

下例中,线网 out 由信号 i1 和 i2 的逻辑与 & 表达式持续驱动。

若将线网转为端口并综合,综合后得到如下 RTL 原理图。

连续赋值语句可用于在 Verilog 中表示组合逻辑门。

示例 #2

下面模块接受两个输入,用 assign 语句通过部分选择和多位拼接驱动输出 z。将每种情况视为模块中唯一代码,否则多条 assign 驱动同一信号会导致输出变 X。

module xyz (
    input [3:0] x, // x 是 4位向量线网
    input y,       // y 是标量线网 (1-bit)
    output [4:0] z // z 是 5位向量线网
);
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 concatenated to get 5-bit net assigned to z. z='b11001 or 'h19
assign z = {x, y};
// Case #2: Same concatenation assigned to selected 3-bits of z. Remaining 2 bits undriven -> high-z. z='bZ001Z
assign z[3:1] = {x, y};
// Case #3: bit4 of z driven with constant 1. Now z = 'b1001Z (only bit0 undriven)
assign z[3:1] = {x, y};
assign z[4] = 1;
// Case #4: bit3 driven with 0, two drivers same value 0 -> no contention. z='bZ001Z
assign z[3:1] = {x, y};
assign z[3] = 0;
// Case #5: bit3 driven with 1, two drivers different values (first line drives X=0, second drives 1) -> unknown. z='bZX01Z
assign z[3:1] = {x, y};
assign z[3] = 1;
// Case #6: Partial selection of operands on RHS, only 2-bits from x -> z = 'b00001 (z[4:3] driven with 0)
assign z = {x[1:0], y};
// Case #7: Explicitly assign only 3-bits of z, leave rest unconnected -> z = 'bZZ001
assign z[2:0] = {x[1:0], y};
// Case #8: Same variable used multiple times, z = 'b00111 (3{y} same as {y, y, y})
assign z = {3{y}};
// Case #9: LHS can also be concatenated: a is 2-bit vector, b is scalar. RHS evaluates to 11001, LHS 3-bit wide so first 3 bits from LSB of RHS assigned to LHS. a='b00, b='b1
assign {a, b} = {x, y};
// Case #10: Reverse order on LHS keeping RHS same -> a='b01, b='b0
assign {a, b} = {x, y};
endmodule

赋值 reg 变量

用 assign 语句驱动或赋值 reg 类型变量是非法的。因为 reg 变量能存储数据,不需要持续驱动。reg 信号只能在 initial 和 always 等过程块中驱动。

隐式连续赋值

用 assign 语句给线网赋值称为 显式 赋值。Verilog 也允许声明线网时赋值,称为 隐式 赋值。

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

组合逻辑设计

考虑由组合逻辑门构成的数字电路及对应 Verilog 代码。

组合逻辑要求输入持续驱动以维持输出,不同于触发器等时序元件在时钟沿捕获存储值。assign 语句完美契合此目的,因为当右侧任何输入变化时输出 o 更新。

// This module takes four inputs and performs a boolean operation and assigns output to o.
module combo (
    input a, b, c, d,
    output o
);
assign o = ~((a & b) | c ^ d);
endmodule

硬件原理图

设计综合后可见行为与 assign 建模相同的组合电路。

信号 o 在 RHS 组合表达式为真时变 1,为假时变 0。0ns 到 10ns 输入为 X 时 o 为 X。

点击查看带仿真示例的幻灯片!

Verilog

  1. 掌握 C# switch 语句:语法、示例与最佳实践
  2. C# break 语句:用法、时机与实战示例
  3. 掌握 C# continue 语句:高效跳过循环迭代的实战指南
  4. 掌握 Verilog 拼接操作:实用指南
  5. 深入理解 Verilog 赋值语句:类型、语法与最佳实践
  6. Verilog阻塞与非阻塞赋值:关键区别详解
  7. 掌握Verilog函数:高效RTL设计的复用利器
  8. 理解Verilog任务:函数vs任务、语法与用法全解析
  9. Verilog 时钟发生器设计与实现指南
  10. 掌握 Verilog 数学函数:高效硬件设计的利器
  11. 掌握 Verilog $timeformat:精度控制与显示格式化
  12. 理解与打印 Verilog 任意作用域下的 Timescale