//
// Splitter
//
// Splits off set of bits from input node.
//
// A Splitter is a subclass of Node because it behaves just
// like a node except that it gets cycled (to update its value).
//
// When designing with Splitters, one should treat them like a Node
// for connections (i.e. they can be passed in as Node parameters,
// but ONLY as inputs to a device), and like a device for simulation
// (i.e. they need to be cycled before any devices which use their
// values).
//
// A splitter uses its Node data instance variable to hold the bits
// it splits from its IN Node. Of course, in most cases, this means
// that not all of that space will be used (i.e. usually Splitters
// are NOT used to replicate all the bits of the source--except the
// case where you might want to reverse the bits of another Node).
//


#import "Splitter.h"


@implementation Splitter


- initNumBits:(int)nbits			// Number of input bits.
	in:innode				// Node from which to split.
	start:(int)startbit			// Bit 0 in split out.
	stop:(int)stopbit			// Bit n in split out.
{
    if((startbit < 0) || (stopbit < 0)) {
        fprintf(stderr, "Splitter: Neither the start nor the stop bit numbers may be less than zero!\n");
	exit(-1);
    }
    
    start = startbit;
    stop = stopbit;
    
    if(start < stop) {
        numbits = stop - start + 1;
    }
    else {
        numbits = start - stop + 1;
    }
    
    [super initNumBits:numbits];			// Perform Node method.
    
    inbits = nbits;
    
    INITDEVTYPE("Splitter");
    
    TESTNODE("IN", innode, inbits);
    
    IN = innode;
    
    return self;
}


- cycle
{
    int		i;
    bit		*inbitarray = [IN getBits];
    
    for(i = 0; i < numbits; i++) {
	if(start < stop) {
	    data[i] = inbitarray[start + i];
	}
	else {
	    data[i] = inbitarray[start - i];
	}
    }
    
    return self;
}


@end


//
// End of file.
//