Verilog Circuit-Combination Logic-Multiplexers
Problem 60:2-to-1 multiplexer
本题中需要实现一个 2 选 1 选择器,sel 信号作为选择信号,当 sel = 1 时选择 b,反之选择 a。
module top_module(
input a, b, sel,
output out );
assign out = (sel) ? b : a;
endmodule
- 不用三元运算符
module top_module (
input a,
input b,
input sel,
output out
);
assign out = (sel & b) | (~sel & a); // Mux expressed as AND and OR
// Ternary operator is easier to read, especially if vectors are used:
// assign out = sel ? b : a;
endmodule
Problem 61:2-to-1 bus multiplexer
上一个 problem 的单比特改为 100bit,其他一样
module top_module(
input [99:0] a, b,
input sel,
output [99:0] out );
assign out=sel?b:a;//100bit为总线信号,不能用单独的门表示啦
endmodule
Problem 62:9-to-1 miltiplexer
实现一个 9 选 1 选择器,sel 作为选择信号,sel=0 时选择 a,sel=1 时选择 b,以此类推……sel 信号位宽为 4bit,当 sel>8 时,输出 16'hffff
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'hffff;
endcase
end
endmodule
Problem 63:256-to-1 multiplexer
实现一个 256 选 1 选择器,sel 信号作为选择信号,当 sel = 0 时选择 in[0],sel = 1 时选择 in[1],以此类推……
module top_module (
input [255:0] in,
input [7:0] sel,
output out
);
// Select one bit from vector in[]. The bit being selected can be variable.
assign out = in[sel];
//assign out = in >> sel;//将输入变量右移sel位,高位被截去,输出最低位上的in[sel].
endmodule
Problem 64:256-to-1 4-bit multiplexer
实现一个 256 选 1 选择器,sel 信号作为选择信号,当 sel = 0 时选择 in[3:0],sel = 1 时选择 in[7:4],以此类推。
同上一题的区别在于,位宽从 1 位变到了 4 位。
module top_module(
input [1023:0] in,
input [7:0] sel,
output [3:0] out );
assign out=in[sel*4+3-:4];
endmodule
-
这题比较考察片选操作符
-
片选多个比特的正确语法有两种:
assign out = in[sel*4 +: 4];// 从 sel*4 开始,选择比特序号大于sel*4 的 4 位比特,相当于[sel*4+3:sel*4] assign out = in[sel*4+3 -: 4]; // 从 sel*4+3 开始,选择比特序号小于 sel*4+3 的 4 位比特,相当于[sel*4+3:sel*4] assign out=in[sel*4+3:sel*4];//不能使用。语法错误 -
移位思想也可以继续使用,只需要将 in 右移 4 位
module top_module(
input [1023:0] in,
input [7:0] sel,
output [3:0] out );
assign out=in>>(sel*4);默认还是取最低位,也就是[sel+3:sel]
endmodule

浙公网安备 33010602011771号