Conditional
Verilog 有一个条件运算符(? :) ,很像 C:
条件 ? 条件为真 : 条件为假
这可以用来根据条件(mux!)选择两个值中的一个在一行中,不使用组合 always 块内的 if-then。
例子:
(0? 3:5) //这是5,因为条件是假的。
(sel? b: a) //由 sel 选择的 a 和 b 之间的2对1多路复用器。
A Bit of Practice
Given four unsigned numbers, find the minimum. Unsigned numbers can be compared with standard comparison operators (a < b). Use the conditional operator to make two-way min circuits, then compose a few of them to create a 4-way min circuit. You'll probably want some wire vectors for the intermediate results.
给出四个未签名的号码,找出最小值。无符号数可以与标准比较运算符(a < b)进行比较。使用条件运算符制作双向最小电路,然后组合其中几个来制作一个4向最小电路。
中间结果可能需要一些线向量。
Module Declaration
module top_module ( input [7:0] a, b, c, d, output [7:0] min);
答案:
module top_module (
input [7:0] a, b, c, d,
output [7:0] min);//// assign intermediate_result1 = compare? true: false;
wire [7:0] temp1;
wire [7:0] temp2;
assign temp1 = a<b?a:b;
assign temp2 = c<d?c:d;
assign min = temp1<temp2?temp1:temp2;
endmodule