1、Basic Gates
目录
1.1、Wire
problem statement:
solution:
module top_module (
input in,
output out);
assign out=in;
endmodule
1.2、GND
problem statement:
solution:
module top_module (
output out);
assign out=1'h0;
endmodule
1.3、NOR
problem statement:
solution:
module top_module (
input in1,
input in2,
output out);
assign out=~(in1|in2);
endmodule
1.4、Another gate
problem statement:
solution:
module top_module (
input in1,
input in2,
output out);
assign out=in1&(~in2);
endmodule
1.5 Two gates
problem statement:
solution:
module top_module (
input in1,
input in2,
input in3,
output out);
wire w1;
assign w1=~in1^in2;
assign out=w1^in3;
endmodule
1.6 More logic gates
problem statement:
Ok, let's try building several logic gates at the same time. Build a combinational circuit with two inputs, a and b.
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
solution:
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
1.7 7420 chip
problem statement:
The 7400-series integrated circuits are a series of digital chips with a few gates each. The 7420 is a chip with two 4-input NAND gates.
Create a module with the same functionality as the 7420 chip. It has 8 inputs and 2 outputs.
solution:
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
1.8 Truth tables
problem statement:
Suppose we want to build the above circuit, but we're limited to using only the set of standard logic gates. How would you build arbitrary logic functions (expressed as a truth table)?
One simple method to create a circuit that implements the truth table's function is to express the function in sum-of-products form. Sum (meaning OR) of products (meaning AND) means using one N-input AND gate per row of the truth table (to detect when the input matches each row), followed by an OR gate that chooses only those rows that result in a '1' output.
For the above example, the output is '1' if the input matches row 2 or row 3 or row 5 or row 7 (This is a 4-input OR gate). The input matches row 2 if x3=0 and x2=1 and x1=0 (This is a 3-input AND gate). Thus, this truth table can be implemented in canonical form by using 4 AND gates that are ORed together.
solution:
module top_module(
input x3,
input x2,
input x1, // three inputs
output f // one output
);
always@(*)
if({{x3},{x2},{x1}}==2||{{x3},{x2},{x1}}==3||{{x3},{x2},{x1}}==5||{{x3},{x2},{x1}}==7)
f=1;
else
f=0;
endmodule
1.9 Two bit equality
problem statement:
Taken from 2015 midterm question 1k
Create a circuit that has two 2-bit inputs A[1:0] and B[1:0], and produces an output z. The value of z should be 1 if A = B, otherwise z should be 0.
solution:
module top_module ( input [1:0] A, input [1:0] B, output z );
always@(*)
if(A==B)
z=1;
else
z=0;
endmodule
1.10 Simple circuit A
problem solution:
Taken from 2015 midterm question 4
Module A is supposed to implement the function z = (x^y) & x. Implement this module.
solution:
module top_module (input x, input y, output z);
assign z=(x^y)&x;
endmodule
1.11 Simple circuit B
problem statement:
solution:
module top_module ( input x, input y, output z );
assign z=~(x^y);
endmodule
1.12 Combine circuits A and B
problem statement:
Taken from 2015 midterm question 4
See mt2015_q4a and mt2015_q4b for the submodules used here. The top-level design consists of two instantiations each of subcircuits A and B, as shown below.
solution:
module top_module (input x, input y, output z);
wire zA1,zB1,zA2,zB2;
A IA1(x,y,zA1);
B IB1(x,y,zB1);
A IA2(x,y,zA2);
B IB2(x,y,zB2);
wire out1,out2;
assign out1=zA1|zB1;
assign out2=zA2&zB2;
assign z=out1^out2;
endmodule
module A(input x, input y, output z);
assign z=(x^y)&x;
endmodule
module B( input x, input y, output z );
assign z=~(x^y);
endmodule
1.13 Ring or vibrate
problem statement:
Suppose you are designing a circuit to control a cellphone's ringer and vibration motor. Whenever the phone needs to ring from an incoming call (
input ring
), your circuit must either turn on the ringer (output ringer = 1
) or the motor (output motor = 1
), but not both. If the phone is in vibrate mode (input vibrate_mode = 1
), turn on the motor. Otherwise, turn on the ringer.Try to use only
assign
statements, to see whether you can translate a problem description into a collection of logic gates.Design hint: When designing circuits, one often has to think of the problem "backwards", starting from the outputs then working backwards towards the inputs. This is often the opposite of how one would think about a (sequential, imperative) programming problem, where one would look at the inputs first then decide on an action (or output). For sequential programs, one would often think "If (inputs are ___ ) then (output should be ___ )". On the other hand, hardware designers often think "The (output should be ___ ) when (inputs are ___ )".
The above problem description is written in an imperative form suitable for software programming (if ring then do this), so you must convert it to a more declarative form suitable for hardware implementation (
assign ringer = ___
). Being able to think in, and translate between, both styles is one of the most important skills needed for hardware design.
solution:
module top_module (
input ring,
input vibrate_mode,
output ringer, // Make sound
output motor // Vibrate
);
always@(*)
if(vibrate_mode==1&&ring==1)
motor=1;
else if(vibrate_mode==0&&ring==1)
ringer=1;
else begin
ringer=0;
motor=0;
end
endmodule
1.14 Thermostat
problem statement;
A heating/cooling thermostat controls both a heater (during winter) and an air conditioner (during summer). Implement a circuit that will turn on and off the heater, air conditioning, and blower fan as appropriate.
The thermostat can be in one of two modes: heating (
mode = 1
) and cooling (mode = 0
). In heating mode, turn the heater on when it is too cold (too_cold = 1
) but do not use the air conditioner. In cooling mode, turn the air conditioner on when it is too hot (too_hot = 1
), but do not turn on the heater. When the heater or air conditioner are on, also turn on the fan to circulate the air. In addition, the user can also request the fan to turn on (fan_on = 1
), even if the heater and air conditioner are off.Try to use only
assign
statements, to see whether you can translate a problem description into a collection of logic gates.
solution:
module top_module (
input too_cold,
input too_hot,
input mode,
input fan_on,
output heater,
output aircon,
output fan
);
always@(*) begin
if(mode==1)
if(too_cold==1) begin
heater=1;fan=1;aircon=0;end
else if(fan_on==1) begin
heater=0;fan=1;aircon=0;end
else begin
heater=0; fan=0;aircon=0; end
else if(mode==0)
if(too_hot==1) begin
aircon=1;fan=1;heater=0;end
else if(fan_on==1) begin
aircon=0;fan=1;heater=0;end
else begin
aircon=0;fan=0;heater=0;end
else begin
heater=0; aircon=0;fan=0; end
end
endmodule
1.15 3 bit population count
problem statement:
A "population count" circuit counts the number of '1's in an input vector. Build a population count circuit for a 3-bit input vector.
solution:'
module top_module(
input [2:0] in,
output [1:0] out );
assign out=in[0]+in[1]+in[2];
endmodule
1.16 Gates and vectors
problem statment:
You are given a four-bit input vector in[3:0]. We want to know some relationships between each bit and its neighbour:
- out_both: Each bit of this output vector should indicate whether both the corresponding input bit and its neighbour to the left (higher index) are '1'. For example, out_both[2] should indicate if in[2] and in[3] are both 1. Since in[3] has no neighbour to the left, the answer is obvious so we don't need to know out_both[3].
- out_any: Each bit of this output vector should indicate whether any of the corresponding input bit and its neighbour to the right are '1'. For example, out_any[2] should indicate if either in[2] or in[1] are 1. Since in[0] has no neighbour to the right, the answer is obvious so we don't need to know out_any[0].
- out_different: Each bit of this output vector should indicate whether the corresponding input bit is different from its neighbour to the left. For example, out_different[2] should indicate if in[2] is different from in[3]. For this part, treat the vector as wrapping around, so in[3]'s neighbour to the left is in[0].
solution:
module top_module(
input [3:0] in,
output [2:0] out_both,
output [3:1] out_any,
output [3:0] out_different );
assign out_both={{in[3]&in[2]},{in[1]&in[2]},{in[1]&in[0]}};
assign out_any={{in[3]|in[2]},{in[2]|in[1]},{in[1]|in[0]}};
assign out_different={{in[3]^in[0]},{in[2]^in[3]},{in[2]^in[1]},{in[1]^in[0]}};
endmodule
1.17 Even longer vectors
problem statement:
See also the shorter version: Gates and vectors.
You are given a 100-bit input vector in[99:0]. We want to know some relationships between each bit and its neighbour:
- out_both: Each bit of this output vector should indicate whether both the corresponding input bit and its neighbour to the left are '1'. For example, out_both[98] should indicate if in[98] and in[99] are both 1. Since in[99] has no neighbour to the left, the answer is obvious so we don't need to know out_both[99].
- out_any: Each bit of this output vector should indicate whether any of the corresponding input bit and its neighbour to the right are '1'. For example, out_any[2] should indicate if either in[2] or in[1] are 1. Since in[0] has no neighbour to the right, the answer is obvious so we don't need to know out_any[0].
- out_different: Each bit of this output vector should indicate whether the corresponding input bit is different from its neighbour to the left. For example, out_different[98] should indicate if in[98] is different from in[99]. For this part, treat the vector as wrapping around, so in[99]'s neighbour to the left is in[0].
solution:
module top_module(
input [99:0] in,
output [98:0] out_both,
output [99:1] out_any,
output [99:0] out_different );
integer i;
always@(*)
for(i=0;i<=98;i++) begin
out_both[i]=in[i]&in[i+1];
out_any[i+1]=in[i+1]|in[i];
out_different[i]=in[i]^in[i+1];
end
assign out_different[99]=in[99]^in[0];
endmodule