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
相关推荐
泡海椒2 分钟前
Java PDF 报表开发:一页简历的两种版式风格对比与折行避坑
java·开发语言·pdf
2601_962885724 分钟前
如何用 Python 计算终极振荡器(Ultimate Oscillator, UOS)?
开发语言·python
wuminyu9 分钟前
OpenJDK学习笔记:Native事件队列与Java回调背压
java·linux·c语言·jvm·c++
源码_空间20 分钟前
基于Springboot在线考试系统(源码+文档)
java·spring boot·后端
@SmartSi26 分钟前
AgentScope Java 入门 14:如何使用 PlanNotebook 让智能体学会“先规划,后执行“
java·agentscope
AI开源软件1 小时前
用MQTT跑通一台模拟设备:看懂IoT产品、物模型和告警
java·vue.js·spring boot·物联网·mqtt·iot·企业管理
benchmark_cc2 小时前
一次获取多只股票实时行情:QuantDash `quotes.get(symbols=[...])` 批量快照
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
geovindu2 小时前
python: PCA
开发语言·后端·python·算法
__万波__2 小时前
嵌入式存储技术--1.Flash物理存储
java·开发语言
智购科技无人售货机工厂店3 小时前
自动售货机振动与运输可靠性测试实战:跌落、振动与包装~YH
java·大数据·服务器·人工智能·科技