3.1.2 Multiplexers
3.1.2.1 2-to-1 multiplexer
module top_module(
input a, b, sel,
output out );
assign out = sel ? b : a;
endmodule
3.1.2.2 2-to-1 bus multiplexer
module top_module(
input [99:0] a, b,
input sel,
output [99:0] out );
assign out = sel ? b : a;
endmodule
3.1.2.3 9-to-1 multiplexer
module top_module(
input [15:0] a, b, c, d, e, f, g, h, i,
input [3:0] sel,
output [15:0] out );
//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
这个地方有点奇怪,直接看视频教程,统一的结论是:initial 和always里的赋值语句要定义为reg型,而这里,reg [15:0] out
,加上这一句,编译就会出错,有点懵了
3.1.2.4 256-to-1 multiplexer
module top_module(
input [255:0] in,
input [7:0] sel,
output out );
assign out = in[sel];
endmodule
3.1.2.5 256-to-1 4-bit multiplexer
module top_module(
input [1023:0] in,
input [7:0] sel,
output [3:0] out );
assign out = in[sel * 4 +: 4];
endmodule
这种写法是verilog 2001标准中新增加的,是向量部分选择的意思,如果sel等于0,in[sel * 4 +: 4]代表从0开始向上数4位,即in[3:0],在sel位宽较大是可以有效减小工作量。