Verilog - || 之间的混淆和 + 运算符
Verilog - confusion between || and + operator
在布尔代数中,加法对应一个或门,而乘法对应一个与门。
假设我想要一个恒温器的鼓风机,其工作原理如下:
The fan should turn on if either the heater or air-conditioner are on. Alternatively if the user requests the fan to turn on (by turning on an input fan_on), the fan should turn on even if the heater or air conditioner are off.
基于这些需求,我将Verilog代码中的逻辑语句表述为:
assign blower_fan = fan_on + heater + aircon;
然而,根据模拟,这会产生不正确的解决方案。然而这有效:
assign blower_fan = fan_on || (heater + aircon);
和
一样
assign blower_fan = fan_on || (heater || aircon);
我的问题:
我对 + 运算符有什么误解?另外,我对后两种确实有效的解决方案感到困惑——为什么它们都有效,而最后一个我只使用逻辑 OR 运算符的解决方案是一种更正确(或首选)的方式来做我想做的事情?
编辑#1:这是我声明输入和输出的整个模块
module top_module (
input too_cold,
input too_hot,
input mode,
input fan_on,
output heater,
output aircon,
output fan
);
assign heater = (mode&&too_cold);
assign aircon = (!mode&&too_hot);
assign fan = (fan_on) || (heater || aircon);
endmodule
OR 和 AND 的布尔表达式分别为 ||
和 &&
。
+
符号其实就是算术表达式
a = 2'b01 // 1
b = 2'b01 // 1
a + b = 2'b10 // 1 + 1 = 2
a || b = 2'b01 // 1 OR 1 = 1
来源:https://www.utdallas.edu/~akshay.sridharan/index_files/Page5212.htm
编辑:
下面两个语句在逻辑上是等价的
assign fan = (fan_on) || (heater || aircon);
assign fan = fan_on || heater || aircon;
的问题
assign blower_fan = fan_on + heater + aircon;
是如果两个输入为高(例如heater = 1,aircon = 1,fan_on = 0),则blower_fan(假设为1位)已溢出,并且因此将为 0 (1'b1 + 1'b1 = 1'b0).
Verilog +
运算符不是 OR 运算符,它是加法运算符。 Verilog中有两个OR运算符:
| bitwise OR
|| logical OR
对于向量,按位 运算分别处理向量操作数的各个位。相反,使用 logical 运算符时,标量或向量在至少包含一个 1 时被认为是 TRUE,而当每一位都为 0 时被认为是 FALSE。Xs 和 Zs 被认为是未知的(既不正确也不错误)。
您还可以将 |
运算符用作 归约运算符。
例如:
Expression Result Comment
=========================================
1'b0 | 1'b0 1'b0 bitwise OR
1'b0 | 1'b1 1'b1 bitwise OR
1'b1 | 1'b0 1'b1 bitwise OR
1'b1 | 1'b1 1'b1 bitwise OR
4'b0101 | 4'b1100 4'b1101 bitwise OR
4'b0000 || 4'b0000 1'b0 logical OR
4'b0000 || 4'b1100 1'b1 logical OR
4'b0101 || 4'b0000 1'b1 logical OR
4'b0101 || 4'b1100 1'b1 logical OR
| 4'b0000 1'b0 reduction
| 4'b0101 1'b1 reduction
| 4'b1111 1'b1 reduction
如果您使用 +
运算符而不是 |
运算符并分配给一个位,则实际上是在使用异或而不是或。比较这些真值表:
OR XOR +
A B F F F
==============================
1'b0 1'b0 1'b0 1'b0 1'b0
1'b0 1'b1 1'b1 1'b1 1'b1
1'b1 1'b0 1'b1 1'b1 1'b1
1'b1 1'b1 1'b1 1'b0 1'b0
这个表达式:
assign blower_fan = fan_on || (heater + aircon);
当 fan_on
是 1'b0
并且 heater
和 aircon
都是 1'b1
时, 会失败,而这个不会:
assign blower_fan = fan_on | heater | aircon;
在布尔代数中,加法对应一个或门,而乘法对应一个与门。
假设我想要一个恒温器的鼓风机,其工作原理如下:
The fan should turn on if either the heater or air-conditioner are on. Alternatively if the user requests the fan to turn on (by turning on an input fan_on), the fan should turn on even if the heater or air conditioner are off.
基于这些需求,我将Verilog代码中的逻辑语句表述为:
assign blower_fan = fan_on + heater + aircon;
然而,根据模拟,这会产生不正确的解决方案。然而这有效:
assign blower_fan = fan_on || (heater + aircon);
和
一样assign blower_fan = fan_on || (heater || aircon);
我的问题:
我对 + 运算符有什么误解?另外,我对后两种确实有效的解决方案感到困惑——为什么它们都有效,而最后一个我只使用逻辑 OR 运算符的解决方案是一种更正确(或首选)的方式来做我想做的事情?
编辑#1:这是我声明输入和输出的整个模块
module top_module (
input too_cold,
input too_hot,
input mode,
input fan_on,
output heater,
output aircon,
output fan
);
assign heater = (mode&&too_cold);
assign aircon = (!mode&&too_hot);
assign fan = (fan_on) || (heater || aircon);
endmodule
OR 和 AND 的布尔表达式分别为 ||
和 &&
。
+
符号其实就是算术表达式
a = 2'b01 // 1
b = 2'b01 // 1
a + b = 2'b10 // 1 + 1 = 2
a || b = 2'b01 // 1 OR 1 = 1
来源:https://www.utdallas.edu/~akshay.sridharan/index_files/Page5212.htm
编辑:
下面两个语句在逻辑上是等价的
assign fan = (fan_on) || (heater || aircon);
assign fan = fan_on || heater || aircon;
的问题
assign blower_fan = fan_on + heater + aircon;
是如果两个输入为高(例如heater = 1,aircon = 1,fan_on = 0),则blower_fan(假设为1位)已溢出,并且因此将为 0 (1'b1 + 1'b1 = 1'b0).
Verilog +
运算符不是 OR 运算符,它是加法运算符。 Verilog中有两个OR运算符:
| bitwise OR
|| logical OR
对于向量,按位 运算分别处理向量操作数的各个位。相反,使用 logical 运算符时,标量或向量在至少包含一个 1 时被认为是 TRUE,而当每一位都为 0 时被认为是 FALSE。Xs 和 Zs 被认为是未知的(既不正确也不错误)。
您还可以将 |
运算符用作 归约运算符。
例如:
Expression Result Comment
=========================================
1'b0 | 1'b0 1'b0 bitwise OR
1'b0 | 1'b1 1'b1 bitwise OR
1'b1 | 1'b0 1'b1 bitwise OR
1'b1 | 1'b1 1'b1 bitwise OR
4'b0101 | 4'b1100 4'b1101 bitwise OR
4'b0000 || 4'b0000 1'b0 logical OR
4'b0000 || 4'b1100 1'b1 logical OR
4'b0101 || 4'b0000 1'b1 logical OR
4'b0101 || 4'b1100 1'b1 logical OR
| 4'b0000 1'b0 reduction
| 4'b0101 1'b1 reduction
| 4'b1111 1'b1 reduction
如果您使用 +
运算符而不是 |
运算符并分配给一个位,则实际上是在使用异或而不是或。比较这些真值表:
OR XOR +
A B F F F
==============================
1'b0 1'b0 1'b0 1'b0 1'b0
1'b0 1'b1 1'b1 1'b1 1'b1
1'b1 1'b0 1'b1 1'b1 1'b1
1'b1 1'b1 1'b1 1'b0 1'b0
这个表达式:
assign blower_fan = fan_on || (heater + aircon);
当 fan_on
是 1'b0
并且 heater
和 aircon
都是 1'b1
时,会失败,而这个不会:
assign blower_fan = fan_on | heater | aircon;