HDLBits第七章练习及答案

1、2对1多路选择器

拓展:

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

练习:

创建一个 1 位宽的 2 对 1 多路选择器。当sel=0时,选择a。当 sel=1 时,选择 b。

代码实现:

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

验证结果:
在这里插入图片描述

2、2对1总线多路选择器

创建一个 100 位宽的 2 对 1 多路选择器。当sel=0时,选择a。当 sel=1 时,选择 b。

代码实现:

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

验证结果:
在这里插入图片描述

3、9 对 1 多路选择器

创建一个 16 位宽的 9 对 1 多路选择器。sel=0 选择a,sel=1 选择b,等等。对于未使用的情况(sel=9 到15),将所有输出位设置为’1’。

代码实现:

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'b0000: out = a;
                4'b0001: out = b;
                4'b0010: out = c;
                4'b0011: out = d;
                4'b0100: out = e;
                4'b0101: out = f;
                4'b0110: out = g;
                4'b0111: out = h;
                4'b1000: out = i;
                default: out = 16'hffff;
            endcase
        end

endmodule

验证结果:
在这里插入图片描述

4、256对1多路选择器

创建一个 1 位宽、256 对1 的多路选择器。256 个输入全部打包成一个 256 位的输入向量。sel=0 应该选择in[0], sel=1 选择位in[1], sel=2 选择位in[2]等。

代码实现:

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

    assign out = in[sel];
    
endmodule

验证结果:
在这里插入图片描述

5、256对1的4位多路选择器

创建一个 4 位宽、256 比 1 的多路复用器。256 个 4 位输入全部打包成一个 1024 位输入向量。sel=0 应该选择[3:0] 中的位, sel=1 选择[7:4] 中的位, sel=2 选择[11:8] 中的位,依此类推。

代码实现:

module top_module( 
    input [1023:0] in,
    input [7:0] sel,
    output [3:0] out );

    assign out = {in[4*sel+3],in[4*sel+2],in[4*sel+1],in[4*sel]};
    
endmodule

验证结果:
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值