//
// Timer
//
// An Objective-C class for simulating a timer circuit.
//


#import "Timer.h"


@implementation Timer


- initNumBits:(int)nbits max:(bit *)maxval out:outnode
{
    id		xnorinputs[2];
    id		muxsources[2];
    
    [super init];
    
    numbits = nbits;
    
    INITDEVTYPE("Timer");
    
    TESTNODE("OUT", outnode, 1);
    
    OUT = outnode;
    
    one		= [[Node alloc] initNumBits:1 fill:1];
    max		= [[Node alloc] initNumBits:numbits contents:maxval];
    zero16	= [[Node alloc] initNumBits:numbits];

//    fprintf(stderr, "Timer: zero16 node = %p.\n", zero16);
    
    reg_out	= [[Node alloc] initNumBits:nbits];
    inc_out	= [[Node alloc] initNumBits:nbits];
    xnor_out	= [[Node alloc] initNumBits:nbits];
    mux_out	= [[Node alloc] initNumBits:nbits];

    //
    // Allocate and Initialize Internal Functional Units:
    //
    
    fprintf(stderr, "Timer: Allocating Internal Functional Units.\n");
    
    reg		= [[Register alloc] initNumBits:numbits in:mux_out out:reg_out load:one];
    
    inc		= [[Incrementor alloc] initNumBits:numbits in:reg_out out:inc_out];
    
    xnorinputs[0] = max;
    xnorinputs[1] = inc_out;
    
    xnor	= [[XNORGate alloc] initNumInputs:2 inputs:xnorinputs
    			numBits:numbits
			out:xnor_out];
    
    and		= [[ANDGate alloc] initNumBits:numbits in:xnor_out out:OUT];
    
    muxsources[0] = inc_out;
    muxsources[1] = zero16;
    
    mux		= [[MUX alloc] initNumBits:numbits numselectbits:1 select:OUT
    			sources:muxsources out:mux_out];
    
    return self;
}


- free
{
    [one free];
    [max free];
    [zero16 free];

    [reg_out free];
    [inc_out free];
    [xnor_out free];
    [mux_out free];
    
    [reg free];
    [inc free];
    [xnor free];
    [and free];
    [mux free];
    
    return [super free];
}


- cycle
{
    [inc cycle];
    [xnor cycle];
    [and cycle];
    [mux cycle];
    [reg cycle];
    
    return self;
}


@end


//
// End of file.
//