Wire(Exams/m2014 q4h)
要求:实现下图功能。
module top_module (
input in,
output out);
assign out = in;
endmodule
GND(Exams/m2014 q4i)
要求:实现接地功能。
module top_module (
output out);
assign out = 1'b0;
endmodule
NOR(Exams/m2014 q4e)
要求:实现NOR门。
module top_module (
input in1,
input in2,
output out);
assign out = ~(in1 | in2);
endmodule
Another gate(Exams/m2014 q4f)
要求: 实现下图功能。
module top_module (
input in1,
input in2,
output out);
assign out = in1 & ~in2;
endmodule
Two gates(Exams/m2014 q4g)
要求:实现下图功能。
module top_module (
input in1,
input in2,
input in3,
output out);
wire a;
assign a = in1 ~^ in2;
assign out = a ^ in3;
endmodule
More logic gates(Gates)
要求:实现下图7种不同的逻辑功能。
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
7420 chip
要求:实现下图7420芯片的逻辑功能。
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
Truthtable1
要求:实现下图真值表的功能。
module top_module(
input x3,
input x2,
input x1, // three inputs
output f // one output
);
assign f = ((~x3&x2&~x1) | (~x3&x2&x1) | (x3&~x2&x1) | (x3&x2&x1));
endmodule
Two-bit equality(Mt2015 eq2)
要求:判断输入A、B是否相等,若输入A等于B,则输出为1,否则为0。
module top_module ( input [1:0] A, input [1:0] B, output z );
assign z= (A == B) ? 1'b1 : 1'b0;
endmodule
Simple circuit A(Mt2015 q4a)
要求:实现 z = (x^y) & x 的功能。
module top_module (input x, input y, output z);
assign z = (x ^ y) & x;
endmodule
Simple circuit B(Mt2015 q4b)
要求:实现具有下图功能的电路。、
不难看出下图波形描述的是同或逻辑。
module top_module ( input x, input y, output z );
assign z = x ~^ y;
endmodule
Combine circuits A and B(Mt2015 q4)
要求:如下图所示结合circuit A和B,circuit A和B已经在上两题实现,可以通过实例化circuit A,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) ^ (z3 & z4);
endmodule
Ringer
要求:仅使用assign语句实现以下功能:当有来电时(ring = 1),输出响铃(ringer = 1)或振动(motor = 1),如果处于振动模式(vibrate_mode = 1),则振动,否则响铃。
module top_module (
input ring,
input vibrate_mode,
output ringer, // Make sound
output motor // Vibrate
);
assign motor = (ring & vibrate_mode);
assign ringer = (ring & ~vibrate_mode);
endmodule
Thermostat
要求:在加热模式(mode = 1)时,当天气太冷时(too_cold = 1),打开加热器(heater = 1)但不使用空调(aircon = 0);在制冷模式(mode = 0)下,天气太热(too_hot = 1)打开空调但不打开加热器;空调和加热器打开时,也要打开风扇(fan = 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 = (heater | aircon) | fan_on;
endmodule
Popcount3
要求:实现计1计数器,计数3位输入中1的个数。
module top_module(
input [2:0] in,
output [1:0] out );
assign out = in[0] + in[1] + in[2];
endmodule
Gatesv
要求:给定一个4位输入.
out_both:判断输入的每一位和其高一位是否都为1,in[3]没有更高位,所以不用关心 out_both[3];
out_any:判断输入的每一位和其低一位是否都为1,in[0]没有更低位,所以不用关心 out_any[0];
out_different:判断输入的每一位和其高一位是否都为不同,将in[0]视为in[3]的高位。
module top_module(
input [3:0] in,
output [2:0] out_both,
output [3:1] out_any,
output [3:0] out_different );
assign out_any = in[3:1] | in[2:0];
assign out_both = in[2:0] & in[3:1];
assign out_different = in ^ {in[0], in[3:1]};
endmodule
Gatesv100
要求:给定一个100位输入.
out_both:判断输入的每一位和其高一位是否都为1,in[99]没有更高位,所以不用关心 out_both[99];
out_any:判断输入的每一位和其低一位是否都为1,in[0]没有更低位,所以不用关心 out_any[0];
out_different:判断输入的每一位和其高一位是否都为不同,将in[0]视为in[99]的高位。
module top_module(
input [99:0] in,
output [98:0] out_both,
output [99:1] out_any,
output [99:0] out_different );
assign out_any = in[99:1] | in[98:0];
assign out_both = in[98:0] & in[99:1];
assign out_different = in ^ {in[0], in[99:1]};
endmodule
总结:这一部分都是一些非常基本的组合逻辑电路,只要弄清楚输出和输入之间的逻辑关系,通过简单的赋值语句就可以实现。