HDLBits刷题合集—8 Multiplexers
HDLBits-61 Mux2to1
Problem Statement
创建一个1位宽的2选1数据选择器。当sel=0时,选择a;当sel=1时,选择b。
代码如下:
module top_module(
input a, b, sel,
output out );
assign out = sel ? b : a;
//assign out = (sel & b) | (~sel & a);
endmodule
HDLBits-62 Mux2to1v
Problem Statement
创建一个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 ? b : a;
//跟上一题的区别就是端口定义不同
endmodule
HDLBits-63 Mux9to1v
Problem Statement
创建一个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'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
HDLBits-64 Mux256to1
Problem Statement
创建一个1位宽,256选1的数据选择器。256个输入打包到一个256位的输入向量中。当sel=0时应该选择in[0],当sel=1时选择in[1],当sel=2时选择in[2],以此类推。
代码如下:
module top_module(
input [255:0] in,
input [7:0] sel,
output out );
//The answer is perfect.
assign out = in[sel];
endmodule
HDLBits-65 Mux256to1v
Problem Statement
创建一个4位宽,256选1的多路选择器。256个4位输入被打包到一个1024位输入向量中。当sel=0时应该选择in[3:0],当sel=1时选择in[7:4],当sel=2时选择in[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+0]};
endmodule
Note
新手一枚,主要分享博客,记录学习过程,后期参考大佬代码或思想会一一列出。欢迎大家批评指正!