HDLBits答案(7)_Verilog多路选择器

Verilog多路选择器

HDLBits链接

定义

多路选择器(Multiplexer)简称多路器,它是一个多输入、单输出的组合逻辑电路,在数字系统中有着广泛的应用。它可以根据地址码(选择码)的不同,从多个输入数据流中选取一个,让其输出到公共的输出端。


部分练习题

题目描述1:实现一个位宽为1的2-1的多路选择器。当sel=0,选择a,当sel=1,选择b。

Solution1

module top_module( 
    input a, b, sel,
    output out ); 
	assign out = sel?b:a;
endmodule

题目描述2:实现一个位宽为100的2-1的多路选择器。当sel=0,选择a,当sel=1,选择b。

Solution2

module top_module( 
    input [99:0] a, b,
    input sel,
    output [99:0] out );
	assign out = sel?b:a;
endmodule

题目描述3:创建一个位宽为16的9-1的多路选择器。sel=0选择a, sel=1选择b,以此类推。对于未使用的sel值(sel=9~15),将所有输出位设置为1。

Solution3

module top_module( 
    input [15:0] a, b, c, d, e, f, g, h, i,
    input [3:0] sel,
    output [15:0] out );

    always @(*) begin
        case(sel)
            4'd0: out = a;
            4'd1: out = b;
            4'd2: out = c;
            4'd3: out = d;
            4'd4: out = e;
            4'd5: out = f;
            4'd6: out = g;
            4'd7: out = h;
            4'd8: out = i;
            default:out = 16'hffff;
        endcase
    end
    
endmodule

题目描述4:创建一个位宽为1的256-1的多路选择器。256个输入都被打包成一个256位的输入向量。sel=0表示选择in[0],sel=1表示选择in[1],以此类推。

Solution4

module top_module( 
    input [255:0] in,
    input [7:0] sel,
    output out );
    assign out = in[sel];
endmodule

题目描述5:创建一个位宽为4的256-1的多路选择器。256个4位输入都被打包成一个1024位的输入向量。sel=0选择in[3:0],sel=1选择in[7:4],sel=2选择in[11:8],以此类推。

Solution5

module top_module( 
    input [1023:0] in,
    input [7:0] sel,
    output [3:0] out );
    assign out = {in[sel*4+3],in[sel*4+2],in[sel*4+1],in[sel*4]};
endmodule

[David说]:题5中不能使用assign out = in[sel*4+3:sel*4],此时会报错,因为该等式不能证明选择的这个位宽是个常数。所以我们继续按bit进行选取然后进行拼接。


总结

  • 学习了多路选择器的相关知识。
  • 学习了可以根据向量索引进行数据的选择,要注意的是被选取数据的位宽是否为常数。
  • 9
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

日拱一卒_未来可期

若复习顺利望有闲钱的同学支持下

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值