HDLbits 2.2 学习笔记 · 多路复用器

多路复用器(Multiplexers):从 2 选 1 到 256 选 1

1.2对1复用器

创建一个一比特宽的2对1复用器。当sel=0时,选择a。当sel=1时,选择b。

复制代码
module top_module(
    input a, b, sel,
output out );
assign out =(sel==0)?a:b;
endmodule

2.100位宽的2对1复用器

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

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

3.多路复用器

创建一个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 );
    integer j;
    always @(*) begin
        for (j = 0; j < 15; j = j + 1) begjn
if sel[j]==0
out=a;
    else sel[j]==1
out=b;
else sel[j]==2
out=c;
  else sel[j]==3
out=d;
  else sel[j]==4
out=e;
else sel[j]==5
out=f;
  else sel[j]==6
out=g;
  else sel[j]==7
out=h;
  else sel[j]==8
out=i;
  else
 		out=1;
end
endmodule

编译错误

用case语句写:

复制代码
module top_module(
    input [15:0] a, b, c, d, e, f, g, h, i,
    input [3:0] sel,
    output reg [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

也可用三目运算符来写

复制代码
module top_module(
    input [15:0] a, b, c, d, e, f, g, h, i,
    input [3:0] sel,
    output [15:0] out
);
    assign out = (sel == 4'd0) ? a :
                 (sel == 4'd1) ? b :
                 (sel == 4'd2) ? c :
                 (sel == 4'd3) ? d :
                 (sel == 4'd4) ? e :
                 (sel == 4'd5) ? f :
                 (sel == 4'd6) ? g :
                 (sel == 4'd7) ? h :
                 (sel == 4'd8) ? i :
                 16'hFFFF;
endmodule

用 for 循环来写多路复用器,最好的办法是把 9 个输入打包成一个数组,然后用 for 遍历数组:

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

    // 把 9 个输入打包成数组
    wire [15:0] mux_in [0:8];
    assign mux_in[0] = a;
    assign mux_in[1] = b;
    assign mux_in[2] = c;
    assign mux_in[3] = d;
    assign mux_in[4] = e;
    assign mux_in[5] = f;
    assign mux_in[6] = g;
    assign mux_in[7] = h;
    assign mux_in[8] = i;

    integer j;
    always @(*) begin
        out = 16'hFFFF;  // 默认值,处理 sel=9~15
        for (j = 0; j < 9; j = j + 1) begin
            if (sel == j)
                out = mux_in[j];
        end
    end

endmodule

4.256比1的复用器

创建一个1位宽、256比1的复用器。256个输入全部压缩到一个256位输入矢量中。sel=0应选择0,sel=1选择1中的位,sel=2选择2中的位,依此类推。

复制代码
module top_module(
    input [255:0] in,
    input [7:0] sel,
output reg out );
    integer j;
always @(*) begin
out = 1'b0;//一定要给一个默认值
        for (j = 0; j < 256; j = j + 1) begin
            if (sel == j)
                out = in[j];
        end
    end
endmodule

这题相对简单,不用for也能解决,以下是简略写法

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

或者用一个alway块

复制代码
module top_module(
    input [255:0] in,
    input [7:0] sel,
    output reg out
);
    always @(*) begin
        out = in[sel];
    end
endmodule

5.256对1的多路复用器

创建一个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[sel*4 +: 4];
endmodule

如果坚持用always模块

复制代码
module top_module(
    input [1023:0] in,
    input [7:0] sel,
    output reg [3:0] out
);

    integer i;

    always @(*) begin
        out = 4'b0000;  // 默认值
        for (i = 0; i < 256; i = i + 1) begin
            if (sel == i)
                out = in[i*4 +: 4];
        end
    end

endmodule
相关推荐
John_Hui1 小时前
第 3 篇:配置 JDK 17 环境
java·开发语言·ubuntu
秋天的怀念_David1 小时前
算法一千零一夜 · 第十七夜 长街点灯
java·程序人生·算法
充值内卷1 小时前
C++单文件迷宫游戏:TauriCPP 开发全记录
开发语言·c++·游戏
Antruly1 小时前
C++ Web 开发选型漫谈:从 Drogon 到 libuvcpp + uvp,八个项目的定位、兼容性与务实排名
开发语言·前端·c++
EnCi Zheng1 小时前
AI Agent工具调用防护层怎么写-一份能直接跑的Python四层拦截实现
大数据·开发语言·python
不吃鱼的羊1 小时前
如何应对恐惧
java
不吃鱼的羊2 小时前
如何建立高自尊
java·服务器·前端
Magicdata大数据2 小时前
AI辅助教学:调用大模型从教材中生成习题,方便学员练习
java·spring boot·大模型·知识图谱·neo4j·ai辅助教学
香蕉大香蕉(06敬畏市场)2 小时前
高频八股 · Java 并发编程(10):异步、ThreadLocal 与虚拟线程(收官)
java·开发语言·网络·jvm·python·ai·实习