题解 | #四选一多路器#

四选一多路器

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


两种解法:
1. 在always块内赋值,使用case语句或者if...else语句
注意always块内赋值,要把变量声明为reg类型
`timescale 1ns/1ns
module mux4_1(
input [1:0]d1,d2,d3,d0,
input [1:0]sel,
output [1:0]mux_out
);
//*************code***********//
    // 1. case ,always @ (*) always @ (sel, d0, d1, d2, d3)
    // 2. begin...end, reg类型变量 <=, <

    reg [1:0] mux_out_reg;
always @ (*)
begin
    case(sel)
    2'b00:mux_out_reg = d3;
    2'b01:mux_out_reg = d2;
    2'b10:mux_out_reg = d1;
    2'b11:mux_out_reg = d0;
    default : mux_out_reg = d0;
    endcase
end 
    
assign mux_out = mux_out_reg;

//*************code***********//
endmodule

2. 用assign赋值,使用?:三目运算符
`timescale 1ns/1ns
module mux4_1(
input [1:0]d1,d2,d3,d0,
input [1:0]sel,
output [1:0]mux_out
);
//*************code***********//
    
assign mux_out = (sel == 2'b00) ? d3 : ((sel == 2'b01) ? d2 : (sel == 2'b10) ? d1 : d0);

//*************code***********//
endmodule



全部评论
该牛油正在参与牛客写题解薅羊毛的活动,牛币,周边,京东卡超多奖品放送,活动进入倒计时!快来捡漏啦https://www.nowcoder.com/discuss/888949?source_id=profile_create_nctrack&channel=-1
点赞 回复 分享
发布于 2022-04-20 16:05

相关推荐

05-07 20:52
吉林大学 Java
点赞 评论 收藏
分享
Beeee0927:正确的建议
点赞 评论 收藏
分享
03-31 18:02
门头沟学院 Java
白日梦想家_等打包版:不要的哦佛给我
点赞 评论 收藏
分享
评论
9
2
分享

创作者周榜

更多
牛客网
牛客企业服务