Blame | Last modification | View Log | RSS feed
/*** Support gates for CS365 Project #1.* Supplied by Prof. L. Yang*///Basic unit: 2-input and gatemodule AND_2 (in1, in2, out);input in1, in2;output out;assign out = in1 & in2;endmodule//Basic unit: 2-input or gatemodule OR_2 (in1, in2, out);input in1, in2;output out;assign out = in1 | in2;endmodule//Basic unit: 3-input or gatemodule OR_3(in1, in2, in3, out);input in1, in2, in3;output out;assign out = in1 | in2 | in3;endmodule//Basic unit: 3-input and gatemodule AND_3 (in1, in2, in3, out);input in1, in2, in3;output out;assign out = in1 & in2 & in3;endmodule//Basic unit: 4-input or gatemodule OR_4 (in1, in2, in3, in4, out);input in1, in2, in3, in4;output out;assign out = in1 | in2 | in3 | in4;endmodule//Basic unit: Invertmodule NOT_1 (in, out);input in;output out;assign out = ~in;endmodule//Basic unit: module for a 2x1 multiplexermodule Mux2 (a, b, op, out);input a, b, op;output out;wire w1, w2, w3;NOT_1 not1 (op, w1);AND_2 and2 (a, w1, w2);AND_2 and1 (b, op, w3);OR_2 or1 (w2, w3, out);endmodule//basic unit: 3-input NAND gatemodule NAND_3 (in1, in2, in3, out);input in1, in2, in3;output out;assign out = ~(in1 & in2 & in3);endmodule//basic unit: 4-input NAND gatemodule NAND_4 (in1, in2, in3, in4, out);input in1, in2, in3, in4;output out;assign out = ~(in1 & in2 & in3 & in4);endmodule// basic unit: 2-input NAND gatemodule NAND_2 (in1, in2, out);input in1, in2;output out;assign out = ~(in1 & in2);endmodule// basic unit: 2-input NOR gatemodule NOR_2 ( in1, in2, out);input in1, in2;output out;assign out = ~(in1 | in2);endmodule// module for a 4x1 multiplexermodule Mux4 (a, b, c, d, op, out);input a, b, c, d;input [0:1]op;output out;wire w1, w2, f1, f2, f3, f4;//instantiate two NOT gatesNOT_1 not1 (op[0:0], w1);NOT_1 not2 (op[1:1], w2);//instantiate four 3-input NAND gatesNAND_3 nand3_1 (a, w1, w2, f1);NAND_3 nand3_2 (b, w1, op[1:1], f2);NAND_3 nand3_3 (c, op[0:0], w2, f3);NAND_3 nand3_4 (d, op[0:0], op[1:1], f4);//instantiate one 4-input NAND gateNAND_4 nand4_1 (f1, f2, f3, f4, out);endmodule// CarryOut module for the 1-bit Adder//cout = cin . a + cin . b + a . bmodule CarryOut (carryIn, a, b, carryOut);input carryIn, a, b;output carryOut;wire w1, w2, w3;AND_2 and1 (carryIn, a, w1);AND_2 and2 (carryIn, b, w2);AND_2 and3 (a, b, w3);OR_3 or1 (w1, w2, w3, carryOut);endmodule//Sum module for 1-bit ALUmodule Sum (carryIn, a, b, sum);input a, b, carryIn;wire w1, w2, w3, f1, f2, f3, f4;output sum;NOT_1 not1 (carryIn, w1);NOT_1 not2 (a, w2);NOT_1 not3 (b, w3);AND_3 and1 (a, w3, w1, f1);AND_3 and2 (w2, b, w1, f2);AND_3 and3 (w2, w3, carryIn, f3);AND_3 and4 (a, b, carryIn, f4);OR_4 or1 (f1, f2, f3, f4, sum);endmodule//module a 1-bit addermodule OneBitAdder (carryIn, a, b, carryOut, result);input carryIn, a, b;output carryOut, result;Sum sum (carryIn, a, b, result);CarryOut cout (carryIn, a, b, carryOut);endmodule