Verilog学习笔记HDLBits——Multiplexers

本文介绍了多路选择器在信号处理中的重要角色,包括2-1、2-1总线、9-1、256-1和256-1四位多路选择器的设计实例。通过Verilog代码展示了如何实现这些选择器,并讨论了片选操作和移位技巧的应用。
摘要由CSDN通过智能技术生成

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

多路选择器:
①多路选择器是常见的选通器件,主要用于通道的扩展、复用;

②多路选择器又叫多路选择开关,可以根据需要选通某一路或者某几路从而达到通道扩展或者复用的目的;

③常见的有二选一、四选一、八选一等;

一、Multiplexers

1. 2-to-1 multiplexer

Practice:Create a one-bit wide, 2-to-1 multiplexer. When sel=0, choose a. When sel=1, choose b.
翻译:创建一个二选一数据选择器,sel=0,输出为a,sel=1,输出为b。
(与上一题的区别就在于1bit变成了100bits,代码没区别,一行解决)

Solution(不唯一,仅供参考):

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

Timing Diagram
在这里插入图片描述

2. 2-to-1 bus multiplexer

Practice:Create a 100-bit wide, 2-to-1 multiplexer. When sel=0, choose a. When sel=1, choose b.
翻译:创建一个100bits的数据选择器,sel=0,输出为a,sel=1,输出为b。

Solution(不唯一,仅供参考):

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

Timing Diagram
在这里插入图片描述

3. 9-to-1 multiplexer

Practice:Create a 16-bit wide, 9-to-1 multiplexer. sel=0 chooses a, sel=1 chooses b, etc. For the unused cases (sel=9 to 15), set all output bits to ‘1’.
翻译:创建一个16位宽、9对1的多路选择器。Sel =0选择a, Sel =1选择b,一直到i。对于未使用的情况(sel=9到15),将所有输出位设置为’1’。

Solution(不唯一,仅供参考):

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)
            0: out = a;
            1: out = b;
            2: out = c;
            3: out = d;
            4: out = e;
            5: out = f;
            6: out = g;
            7: out = h;
            8: out = i;
            default: out = 16'hffff;
        endcase
    end
endmodule

Timing Diagram
在这里插入图片描述

4. 256-to-1 multiplexer

Practice:Create a 1-bit wide, 256-to-1 multiplexer. The 256 inputs are all packed into a single 256-bit input vector. sel=0 should select in[0], sel=1 selects bits in[1], sel=2 selects bits in[2], etc.
翻译:创建一个1位宽的256选1的数据选择器,sel=0,out=in[0];sel=1,out=in[1],以此类推。

Solution(不唯一,仅供参考):

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

5. 256-to-1 4-bit multiplexer

Practice:Create a 4-bit wide, 256-to-1 multiplexer. The 256 4-bit inputs are all packed into a single 1024-bit input vector. sel=0 should select bits in[3:0], sel=1 selects bits in[7:4], sel=2 selects bits in[11:8], etc.
翻译:创建一个4位宽,256选1的数据选择器,当sel=0,out=in[3:0];当sel=1,out=in[7:4];sel=2,out=in[11:8];以此类推。

Solution(不唯一,仅供参考):

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

错误assign out = in[ sel*4+3 : sel*4 ]; 但这个表达式不符合 Verilog 片选操作符的语法。参考大佬的解决方法,采用片选的用法(在书中暂时没找到)
片选多个比特的正确语法有两种:
1.assign out = in[sel*4 +: 4];

从 sel4 开始,选择比特序号大于sel4 的 4 位比特(包含 sel4),相当于[sel4+3 : sel4]
2.assign out = in[sel
4+3 -: 4];

从 sel4+3 开始,选择比特序号小于 sel4+3 的 4 位比特,相当于[sel4+3 : sel4]
改进代码
Solution(不唯一,仅供参考):

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

移位好像也行

module top_module( 
    input [1023:0] in,
    input [7:0] sel,
    output [3:0] out );
    ssign out = in>>sel*4;
endmodule

总结

1、 在使用移位符时,需要切记移位是不能够嵌套使用的,举例:
要 assign out = in1 >> ( in2<< 3); 的效果,只能通过使用一次中间变量来将 in2移位。
wire out0;
assgin out0 = in2<< 3
assign out = in1 >>out0;
2、第五题中片选的用法还需要多记忆了解。

继续加油!!!!!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值