1.Mux2to1
实现2选1电路。
module top_module(
input a, b, sel,
output out );
assign out = (sel == 1'b0) ? a : b;
endmodule
2.Mux2to1v
创建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 == 1'b0) ? a : b;
endmodule
3.Mux9to1v
创建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'b1111_1111_1111_1111;
endcase
end
endmodule
4.Mux256to1
创建1位宽、256选1的多路选择器。256个输入全部封装到单个256位输入向量中。sel=0应该选择 in[0],sel=1选择 in[1]中的位,sel=2选择[2]中的位,依此类推。
module top_module(
input [255:0] in,
input [7:0] sel,
output out );
assign out = in[sel];
endmodule
5.Mux256to1v
创建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+3] , in[sel*4+2] , in[sel*4+1] , in[sel*4]};
//直接写assign out = in[sel*4+3:sel*4]会报错,因为in[a:b]中a,b必须是常量,但是in[a]可以。
endmodule