/// Ripple Adder Demonstration

// For LSU EE 2720-2 Fall 2011

////////////////////////////////////////////////////////////////////////////////
/// Binary Full Adder

//
// Binary Full Adder Explicit Structural Description
//

module bfa_structural(sum,cout,a,b,cin);
   input a,b,cin;
   output sum,cout;

   wire   term001, term010, term100,term111;
   wire   ab, bc, ac;
   wire   na, nb, nc;

   or o1(sum,term001,term010,term100,term111);
   or o2(cout,ab,bc,ac);

   and a1(term001,na,nb,cin);
   and a2(term010,na,b,nc);
   and a3(term100,a,nb,nc);
   and a4(term111,a,b,cin);

   not n1(na,a);
   not n2(nb,b);
   not n3(nc,cin);

   and a10(ab,a,b);
   and a11(bc,b,cin);
   and a12(ac,a,cin);

endmodule


module bfa_implicit(sum,cout,a,b,cin);
   input a,b,cin;
   output sum,cout;

   assign sum =
          ~a & ~b &  cin |
          ~a &  b & ~cin |
           a & ~b & ~cin |
           a &  b &  cin;

   assign cout = a & b | b & cin | a & cin;

endmodule

////////////////////////////////////////////////////////////////////////////////
///  Four-Bit Ripple Adder

module ripple_4(sum,cout,a,b);
   input [3:0] a, b;
   output [3:0] sum;
   output       cout;

   wire         c0, c1, c2;

   bfa_implicit bfa0(sum[0],c0,a[0],b[0],1'b0);
   bfa_implicit bfa1(sum[1],c1,a[1],b[1],c0);
   bfa_implicit bfa2(sum[2],c2,a[2],b[2],c1);
   bfa_implicit bfa3(sum[3],cout,a[3],b[3],c2);

endmodule



////////////////////////////////////////////////////////////////////////////////
/// Adder Demonstration

// exemplar translate_off

module demoadd();

   wire [4:0] sum;
   reg [4:0] shadow_sum;
   reg [3:0]  a, b;
   integer    i;

   ripple_4 adder1(sum[3:0],sum[4],a,b);

   task check_sum;
      input [4:0] s;
      input [79:0] name;

      if( s != shadow_sum ) begin
         $display("Wrong sum in %s: %d + %d = %d != %d\n",
                  name, a, b, shadow_sum, s);
         $stop;
         end

   endtask

   initial begin

      $display("Starting the ripple adder demonstration.");
      $display("To see what's going on drag signals into the wave window");
      $display("before running.");

      for(i=0; i<255; i=i+1) begin
         a = i[3:0];
         b = i[7:4];
         shadow_sum = a + b;
         #10;
         check_sum(sum,"Ripple");
      end

      $display("Demonstration completed.");

   end

endmodule