Subversion Repositories programming

Rev

Rev 197 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed


//Basic unit: 2-input and gate
module AND_2 (in1, in2, out);
   input in1, in2;
   output out;
   assign out = in1 & in2;
endmodule

//Basic unit: 2-input or gate
module OR_2 (in1, in2, out);
   input in1, in2;
   output out;
   assign out = in1 | in2;
endmodule

//Basic unit: 3-input or gate
module OR_3(in1, in2, in3, out);
   input in1, in2, in3;
   output out;
   assign out = in1 | in2 | in3;
endmodule

//Basic unit: 3-input and gate
module AND_3 (in1, in2, in3, out);
   input in1, in2, in3;
   output out;
   assign out = in1 & in2 & in3;
endmodule

//Basic unit: 4-input or gate
module OR_4 (in1, in2, in3, in4, out);
   input in1, in2, in3, in4;
   output out;
   assign out = in1 | in2 | in3 | in4;
endmodule

//Basic unit: Invert
module NOT_1 (in, out);
   input in;
   output out;
   assign out = ~in;
endmodule

//Basic unit: module for a 2x1 multiplexer
module 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 gate
module NAND_3 (in1, in2, in3, out);
  input in1, in2, in3;
  output out;
  assign out = ~(in1 & in2 & in3);
endmodule

//basic unit: 4-input NAND gate
module 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 gate
module NAND_2 (in1, in2, out);
   input in1, in2;
   output out;
   assign out = ~(in1 & in2);
endmodule

// basic unit: 2-input NOR gate
module NOR_2 ( in1, in2, out);
   input in1, in2;
   output out;
   assign out = ~(in1 | in2);
endmodule

// module for a 4x1 multiplexer
module 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 gates
   NOT_1 not1 (op[0:0], w1);
   NOT_1 not2 (op[1:1], w2);

   //instantiate four 3-input NAND gates
   NAND_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 gate
   NAND_4 nand4_1 (f1, f2, f3, f4, out);
endmodule

// CarryOut module for the 1-bit Adder
//cout = cin . a + cin . b + a . b
module 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 ALU
module 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 adder
module 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

// module for the 1-bit ALU
module ALU1 (a, b, cin, op, cout, result);
    input a, b, cin;
    input[0:1] op; /* 00=AND, 01=OR, 10=ADD, 11=ADD A+~B */
    output cout, result;

    wire w_or, w_and, w_binv, w_add_out, w_sub_out;

    /* AND, OP=00 */
    OR_2  or1  (a, b, w_or);

    /* OR, OP=01 */
    AND_2 and1 (a, b, w_and);

    /* ADD, OP=10 */
    /* SUB, OP=11 */
    NOT_1 not1 (b, w_binv);
    Mux2 m_binv (b, w_binv, op[1], w_adder_b);

    OneBitAdder add (cin, a, w_adder_b, cout, w_add_out);

    /* MUX the output together */
    Mux4 resMux (w_and, w_or, w_add_out, w_add_out, op, result);

endmodule