
package cz.karry.dop.core.registers;

import cz.karry.dop.core.Bus;
import cz.karry.dop.core.Signal;
import cz.karry.dop.core.SignalListener;

/**
 *
 * @author karry
 */
public class UpDownRegister extends IORegister implements SignalListener{

    protected Signal enableCounting;
    protected Signal countMode;
    

    public UpDownRegister(String name,Bus input, Bus output, Signal reset, Signal clock, Signal enableLoad, Signal outputEnable, Signal enableCounting, Signal countMode){
        super(name, input, output, reset, clock, enableLoad, outputEnable);
        this.enableCounting = enableCounting;
        this.countMode = countMode;
    }
    
    @Override
    public void signalChangePerformed(Signal signal, boolean active) {
        super.signalChangePerformed(signal, active);
        
        if (signal == clock){
            if (active){
                if ((enableCounting.isActive()) && (!enableLoad.isActive())){
                    if (countMode.isActive()){
                        if (getValue() <= 0)
                            dValue = 0xffff;
                        else{
                            dValue = getValue() - 1;
                        }                                 
                    }else{
                        if (getValue() < 0xffff)
                            dValue = getValue() + 1;
                        else{
                            dValue = 0;
                        }                        
                    }
                }
            }
        }
        
    }

}
