HDL Bits---Multiplexer

3.2.1 ~3.2.2

 

3.2.3 9-to-1 Multiplexer

创建一个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
out = '1;       // '1 是所有位都设为1的特殊文字语法 '0, 'x, 'z 也是有效的
case (sel)
4'h0: out = a;
4'h1: out = b;
4'h2: out = c;
4'h3: out = d;
4'h4: out = e;
4'h5: out = f;
4'h6: out = g;
4'h7: out = h;
4'h8: out = i;
endcase
end
endmodule


方法二:

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

3.2.4 

 

3.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+3], in[sel*4+2], in[sel*4+1], in[sel*4+0]};     //只要综合器可以确定所选位的宽度是恒定的,则向量索引可以是可变的。但他并不总是擅长于此。有error "... is not a constant",                                                                                                           意味着它不能证明选择宽度是恒定的。特别是 in[ sel*4+3 : sel*4 ] 不起作用。

endmodule

或者,片选多个比特的方法有两种:

assign out = in[sel*4 +: 4]; //选择从索引“sel*4”开始,然后选择总宽度为 4 位的 (+:) 索引号递增。
assign out = in[sel*4+3 -: 4]; // 选择从索引 "sel*4+3" 开始,然后选择总宽度为 4 位的 (-:) 索引号递减。

posted @ 2022-07-06 16:42  糖甜  阅读(111)  评论(0)    收藏  举报