☰
HDLbits 2.2 学习笔记 · 多路复用器
2026/10/29 11:10:47 网站建设 项目流程

多路复用器(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

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询