HDL—Verilog Language—More Verliog Features—Conditional ternary operator

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值