//
// Adder
//
// An Objective-C class for simulating digital logic which
// simulates a two-input, n-stage full adder with carry-in
// and carry-out.
//


#import "Adder.h"


@implementation Adder


- initNumBits:(int)nbits a:anode b:bnode cin:cinnode cout:coutnode out:outnode
{
    [super initNumBits:nbits];
    
    INITDEVTYPE("Adder");
    
    TESTNODE("A", anode, numbits);
    TESTNODE("B", bnode, numbits);
    TESTNODE("CIN", cinnode, 1);
    TESTNODE("COUT", coutnode, 1);
    TESTNODE("OUT", outnode, numbits);
    
    A = anode;
    B = bnode;
    CIN = cinnode;
    COUT = coutnode;
    OUT = outnode;
    
    return self;
}


- cycle
{
    int i;
    int sum;
    bit	carry = *([CIN getBits]);
    bit	*abits = [A getBits];
    bit	*bbits = [B getBits];
    
    for(i = 0; i < numbits; i++) {
        sum = abits[i] + bbits[i] + carry;
	carry = (sum > 1);
	sum = sum - (2 * carry);
	data[i] = sum;
    }
    
    [COUT setBits:&carry];
    [OUT setBits:data];
    
    return self;
}


@end


//
// End of file.
//