文章目录
1.wire
实现以下电路:
module top_module (
input in,
output out);
assign out = in;
endmodule
2.GND
module top_module (
output out);
assign out = 1'b0;
endmodule
3.NOR
module top_module (
input in1,
input in2,
output out);
assign out = ~(in1 | in2);
endmodule
4.Another gate
module top_module (
input in1,
input in2,
output out);
assign out = in1 & ~in2;
endmodule
5.Two gates
module top_module (
input in1,
input in2,
input in3,
output out);
assign out = ~(in1^in2)^in3;
endmodule
6.More logic gates
There are 7 outputs, each with a logic gate driving it:
out_and: a and b
out_or: a or b
out_xor: a xor b
out_nand: a nand b
out_nor: a nor b
out_xnor: a xnor b
out_anotb: a and-not b
module top_module(
input a, b,
output out_and,
output out_or,
output out_xor,
output out_nand,
output out_nor,
output out_xnor,
output out_anotb
);
assign out_and = a&b;
assign out_or = a|b;
assign out_xor = a^b;
assign out_nand = ~(a&b);
assign out_nor = ~(a|b);
assign out_xnor = ~(a^b);
assign out_anotb = a&~b;
endmodule
7. 7420 chip
module top_module(
input p1a,p1b,p1c,p1d,
output p1y,
input p2a,p2b,p2c,p2d,
output p2y);
assign p1y = ~(p1a&p1b&p1c&p1d);
assign p2y = ~(p2a&p2b&p2c&p2d);
endmodule
8. Truth tables
module top_module(
input x3,
input x2,
input x1, // three inputs
output f // one output
);
assign f = (!x3&x2) | (x3&x1);
endmodule
9. Two-bit equality
创建一个具有两个 2 位输入A[1:0]和B[1:0] 的电路,并产生一个输出z的值z应为1,如果A = B,否则z应该是0
module top_module ( input [1:0] A, input [1:0] B, output z );
always@(*)begin
if(A == B)
z = 1;
else
z = 0;
end
endmodule
10. Simple circuit A
模块 A 应该实现函数z = (x^y) & x。实现这个模块。
module top_module (input x, input y, output z);
assign z = (x^y)&x;
endmodule
11. Simple circuit B
module top_module ( input x, input y, output z );
assign z = x ~^y;
endmodule
12. Combine circuit A and B
module top_module (input x, input y, output z);
wire z1,z2,z3,z4;
assign z1 = (x^y)&x;
assign z2 = x^~y;
assign z3 = (x^y)&x;
assign z4 = x^~y;
assign z = (z1 | z2)^(z2 & z3);
endmodule
13. Ring or vibrate
module top_module (
input ring,
input vibrate_mode,
output ringer, // Make sound
output motor // Vibrate
);
assign ringer = ring &(~vibrate_mode);
assign motor = ring & vibrate_mode;
endmodule
14.Thermostat
加热/冷却恒温器同时控制加热器(冬季)和空调(夏季)。实施一个电路,根据需要打开和关闭加热器、空调和鼓风机。
恒温器可以处于两种模式之一:加热 ( mode = 1) 和冷却 ( mode = 0)。在制热模式下,天气太冷时打开加热器(too_cold = 1),但不要使用空调。在制冷模式下,当温度过高时 ( too_hot = 1)打开空调,但不要打开加热器。当加热器或空调打开时,还要打开风扇使空气流通。此外,用户还可以要求风扇开启(fan_on = 1),即使加热器和空调关闭。
module top_module (
input too_cold,
input too_hot,
input mode,
input fan_on,
output heater,
output aircon,
output fan
);
assign heater = mode & too_cold;
assign aircon = ~mode & too_hot;
assign fan = (mode & too_cold) | (~mode & too_hot) | fan_on;
endmodule
15. 3-bit population count
为 3 位输入向量构建人口计数电路。
module top_module(
input [2:0] in,
output [1:0] out );
always@(*)begin
out = 2'b00;
for(int i=0;i<3;i++) begin
out = out +in[i];
end
end
endmodule
16. Gates and vectors
在[3:0] 中,您将获得一个四位输入向量。我们想知道每个位与其邻居之间的一些关系:
out_both:本输出向量的每一位应该指示是否两个相应的输入位和它的邻居到左(较高的指数)是“1”。例如,out_both[2]应该表明in[2]和in[3]是否都为 1。由于in[3]左边没有邻居,所以答案很明显,所以我们不需要知道out_both[3 ]。
out_any:此输出向量的每一位都应指示是否有任何相应的输入位及其右侧的邻居为“1”。例如,out_any[2]应该指示in[2]或in[1]是否为 1。由于in[0]右边没有邻居,答案很明显,所以我们不需要知道out_any[0 ]。
out_different:该输出向量的每一位应指示相应的输入位是否与其左侧的相邻位不同。例如,out_different[2]应该指示in[2]是否与in[3] 不同。对于这部分,将向量视为环绕,因此in[3]左侧的邻居是in[0]。
module top_module(
input [3:0] in,
output [2:0] out_both,
output [3:1] out_any,
output [3:0] out_different );
always@(*)begin
for(int i=0;i<3;i++)begin
out_both[i] = in[i] & in[i+1];
out_any[i+1] = in[i] | in[i+1];
out_different[i] = in[i] ^ in[i+1];
end
out_different[3] = in[0] ^ in[3];
end
endmodule
17. Even longer vectors
在[99:0] 中,您将获得一个 100 位的输入向量。我们想知道每个位与其邻居之间的一些关系:
out_both:本输出向量的每一位应该指示是否两个相应的输入位和它的邻居到左是“1”。例如,out_both[98]应该表明in[98]和in[99]是否都是 1。由于in[99]左边没有邻居,所以答案很明显,所以我们不需要知道out_both[99 ]。
out_any:此输出向量的每一位都应指示是否有任何相应的输入位及其右侧的邻居为“1”。例如,out_any[2]应该指示in[2]或in[1]是否为 1。由于in[0]右边没有邻居,答案很明显,所以我们不需要知道out_any[0 ]。
out_different:该输出向量的每一位应指示相应的输入位是否与其左侧的相邻位不同。例如,out_different[98]应该指示in[98]是否与in[99] 不同。对于这部分,将向量视为环绕,因此in[99]左侧的邻居是in[0]。
module top_module(
input [99:0] in,
output [98:0] out_both,
output [99:1] out_any,
output [99:0] out_different );
always@(*)begin
for(int i=0;i<99;i++)begin
out_both[i] = in[i] & in[i+1];
out_any[i+1] = in[i] | in[i+1];
out_different[i] = in[i] ^ in[i+1];
end
out_different[99] = in[0] ^ in[99];
end
endmodule