Conditional:
条件运算符
Verilog has a ternary conditional operator ( ? : ) much like C:
(condition ? if_true : if_false)
This can be used to choose one of two values based on condition (a mux!) on one line, without using an if-then inside a combinational always block.
Examples:
(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 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.
module top_module (
input [7:0] a, b, c, d,
output [7:0] min);//
// assign intermediate_result1 = compare? true: f
wire [7:0] min1,min2;
assign min1=(a<b)?

这篇博客介绍了Verilog中的条件运算符和逻辑运算,包括如何使用条件选择器,进行位宽的单目运算,如AND、OR、XOR的位级减少操作。此外,还涉及了实践中如何构建4向最小值电路、奇偶校验计算、100输入门电路以及100位二进制加法器的实现。
最低0.47元/天 解锁文章
773

被折叠的 条评论
为什么被折叠?



