Verilog 有一个三元条件运算符,即(状态 ? if_true : if_false):
assign intermediate_result1 = compare? true: false;
练习题目:
给定四个无符号数字,找到最小值。无符号数字可以与标准比较运算符(a < b)进行比较。使用条件运算符创建双向最小电路,然后组合其中的一些电路以创建 4 向最小电路。您可能需要一些用于中间结果的线向量。
错误解法:
module top_module (
input [7:0] a, b, c, d,
output [7:0] min);//
// assign intermediate_result1 = compare? true: false;
wire temp1,temp2,temp3; //这里没有定义矢量,默认值为一位,从而导致错误
always @(*)
begin
temp1 = (a<b)? a:b;
temp2 = (temp1<c)? temp1:c;
min = (temp2<d)? temp2:d;
end
endmodule
可以正常编译,但是综合仿真出来的时序图错误,错误如下:
错误分析:
因为我没有定义temp1和temp2这两个中间变量的位宽,导致默认1位,因此在利用三元条件运算符的时候,进行逐一比较,直到第一位不同大小的值进行比较,并且保存到中间变量。这也是为什么时序图中我的答案只出现了0,1两种情况。
正确解法:
module top_module (
input [7:0] a, b, c, d,
output [7:0] min);//
wire [7:0] min1,min2; //区别
assign min1 = (a<b) ? a : b;
assign min2 = (c<d) ? c : d;
assign min = (min1<min2) ? min1 : min2;
// assign intermediate_result1 = compare? true: false;
endmodule