Rev 195 | Blame | Compare with Previous | Last modification | View Log | RSS feed
//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, sum);input carryIn, a, b;output carryOut, sum;Sum sum_gate (carryIn, a, b, sum);CarryOut cout (carryIn, a, b, carryOut);endmodule// module for the 1-bit ALUmodule ALU1 (a, b, binvert, carryIn, op, carryOut, result);input a, b, binvert, carryIn;input [0:1]op;output carryOut, result;wire w1, w2, w3, w4, w5, w6;wire dead;AND_2 and1 (a, w2, w3);OR_2 or1 (a, w2, w4);NOT_1 not1 (b, w1);Mux2 mux2 (b, w1, binvert, w2);OneBitAdder adder (carryIn, a, w2, carryOut, w5);Mux4 mux4 (w3, w4, w5, dead, op, result);endmodulemodule test_alu1;reg a, b, cin, binvert;reg[0:1] op;wire cout, result;ALU1 alu1 (a, b, binvert, cin, op, cout, result);initial begin$monitor ("time=%0d a=%b b=%b op=%b cin=%b cout=%b result=%b",$time, a, b, op, cin, cout, result);endinitial beginbinvert=0;a=0; b=0; cin=0; op=00; $display; $display("AND");#1 a=0; b=0; cin=1; op=00;#1 a=0; b=1; cin=0; op=00;#1 a=0; b=1; cin=1; op=00;#1 a=1; b=0; cin=0; op=00;#1 a=1; b=0; cin=1; op=00;#1 a=1; b=1; cin=0; op=00;#1 a=1; b=1; cin=1; op=00;#1 a=0; b=0; cin=0; op=01; $display; $display ("OR");#1 a=0; b=0; cin=1; op=01;#1 a=0; b=1; cin=0; op=01;#1 a=0; b=1; cin=1; op=01;#1 a=1; b=0; cin=0; op=01;#1 a=1; b=0; cin=1; op=01;#1 a=1; b=1; cin=0; op=01;#1 a=1; b=1; cin=1; op=01;#1 a=0; b=0; cin=0; op='b10; $display; $display ("ADD");#1 a=0; b=0; cin=1; op='b10;#1 a=0; b=1; cin=0; op='b10;#1 a=0; b=1; cin=1; op='b10;#1 a=1; b=0; cin=0; op='b10;#1 a=1; b=0; cin=1; op='b10;#1 a=1; b=1; cin=0; op='b10;#1 a=1; b=1; cin=1; op='b10; // <-- BUG HEREbinvert=1;#1 a=0; b=0; cin=0; op='b10; $display; $display ("SUB");#1 a=0; b=0; cin=1; op='b10;#1 a=0; b=1; cin=0; op='b10;#1 a=0; b=1; cin=1; op='b10;#1 a=1; b=0; cin=0; op='b10;#1 a=1; b=0; cin=1; op='b10;#1 a=1; b=1; cin=0; op='b10;#1 a=1; b=1; cin=1; op='b10;endendmodule