文章目录
一、问题描述
Verilog 有一个三元条件运算符 ( ? : ) 很像 C语言:
(condition ? if_true : if_false)
这可用于根据一行上的条件(多路复用器!)选择两个值之一,而无需在组合 always 块中使用 if-then。
举例:
(0 ? 3 : 5) // This is 5 because the condition is false.
(sel ? b : a) // A 2-to-1 multiplexer between a and b selected by sel.
always @(posedge clk) // A T-flip-flop.
q <= toggle ? ~q : q;
always @(*) // State transition logic for a one-input FSM
case (state)
A: next = w ? B : A;
B: next = w ? A : B;
endcase
assign out = ena ? q : 1’bz; // A tri-state buffer
((sel[1:0] == 2’h0) ? a : // A 3-to-1 mux
(sel[1:0] == 2’h1) ? b :
c )
一点点练习:
给定四个无符号数字,找到最小值。无符号数字可以与标准比较运算符(a < b)进行比较。使用条件运算符创建双向最小电路,然后组合其中的一些电路以创建 4 向