题解 | #四选一多路器#

四选一多路器

http://www.nowcoder.com/practice/cba4617e1ef64e9ea52cbb400a0725a3

answer one:

数据类型皆为wire,考虑assign赋值语句。

sel 00 : d3

sel 01 : d2

sel 10 : d1

sel 11 :d0

使用条件运算符(? :),需要定义两个中间变量sel_1 sel_2。

首先判断sel低位即sel[0],在判断sel[1];

将判断结果分别赋值给sel_1 sel_2;

最后再对sel_1 sel_2 进行判断,代码如下:

wire    [1:0]    sel_1,sel_2;

assign sel_1 = (sel[0]) ? d2 : d3;

assign sel_2 = (sel[0]) ? d0 : d1;

assign mux_out = (sel[1]) ? sel_2 :sel_1;

answer two:

使用case语句,定义一个中间变量reg [1:0] out;

代码如下:

`timescale 1ns/1ns
module mux4_1(
input [1:0]d1,d2,d3,d0,
input [1:0]sel,
output[1:0]mux_out
);
//--------answer one---------
   // wire    [1:0]    sel_1,sel_2;
   // assign sel_1 = (sel[0]) ? d2 : d3;
   // assign sel_2 = (sel[0]) ? d0 : d1;
   // assign mux_out = (sel[1]) ? sel_2 :sel_1;
//--------answer two-------
    reg [1:0]    out;
    always@(*)
        begin 
            case(sel)
                2'b00 : out = d3;
                2'b01 : out = d2;
                2'b10 : out = d1;
                2'b11 : out = d0;
                default : out = d0;
            endcase
        end
    assign mux_out = out;
                
endmodule
全部评论
感谢!
点赞 回复 分享
发布于 04-27 17:38 四川

相关推荐

零OFFER战士:另一个版本查看图片
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
06-11 13:34
offe从四面八方来:我真的没时间陪你闹了
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
14
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务