/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

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 LRegister extends IORegister implements SignalListener{
    protected Signal enableCountDown;
    private Signal zeroSet;

    public LRegister(String name,Bus input, Bus output, Signal reset, Signal clock, Signal enableLoad, Signal outputEnable, Signal enableCountDown){
        super(name, input, output, reset, clock, enableLoad, outputEnable);
        this.enableCountDown = enableCountDown;
        enableCountDown.addSignalListener(this);
    }
    
    
    public void setLZeroSignal(Signal zeroSet){
        this.zeroSet = zeroSet;
    }
            
    @Override
    public void signalChangePerformed(Signal signal, boolean active) {
        super.signalChangePerformed(signal, active);
        
        if (signal == clock){
            if (active){
                if ((enableCountDown.isActive()) && (!enableLoad.isActive())){
                    if (getValue() <= 0)
                        dValue = 255;
                    else{
                        dValue = getValue() - 1;
                    }                    
                }
                
                if (enableLoad.isActive()){
                    dValue = input.getValue() & 0xff;
                }                
            }else{
                if (zeroSet != null) zeroSet.setState( getValue()==0 );
            }
        }
        
    }

}
