package br.com.leandromoreira.jdcpu16br;

public class CPU {

    private static final int MASK_16BIT = 0xF;
    private static final int OxFFFF = 0xFFFF;
    private static final int WORD_SIZE = 16;
    public static final int A = 0x0;
    public static final int B = 0x1;
    public static final int C = 0x2;
    public static final int X = 0x3;
    public static final int Y = 0x4;
    public static final int Z = 0x5;
    public static final int I = 0x6;
    public static final int J = 0x7;
    private int[] register = new int[0x8];
    private int programCounter, stackPointer;
    private int overflow;
    private final Instruction[] instructions;
    private final ParameterDecoder[] decoders;
    private final Memory memory;
    private ParameterDecoder a, b;

    public CPU() {
        memory = new Memory();
        programCounter = stackPointer = overflow = 0x0000;
        instructions = new InstructionTable().instructionSet(this);
        decoders = new AllParametersDecoder(this).all();
    }

    public ParameterDecoder parameterA() {
        return a;
    }

    public ParameterDecoder parameterB() {
        return b;
    }

    public Memory memory() {
        return memory;
    }

    public int getOverflow() {
        return overflow;
    }

    public void setOverflow(final int overflow) {
        this.overflow = overflow;
    }

    public int getProgramCounter() {
        return programCounter;
    }

    public int getNextProgramCounter() {
        return ++programCounter;
    }

    public void setProgramCounter(final int programCounter) {
        this.programCounter = programCounter;
    }

    public int getStackPointer() {
        return stackPointer;
    }

    public int getStackPointerAndIncrement() {
        final int sp = stackPointer;
        stackPointer++;
        return sp;
    }

    public int getStackPointerAndDecrement() {
        return --stackPointer;
    }

    public void setStackPointer(final int stackPointer) {
        this.stackPointer = stackPointer;
    }

    public int register(final int index) {
        return register[index];
    }

    public void setRegister(final int index, final int value) {
        register[index] = value;
    }

    public void step() {
        final Word word = new Word(memory.readFrom(programCounter));
        final Instruction instruction = instructions[word.code()];
        decodeValuesParameter(word);
        instruction.execute(word);
        programCounter += instruction.sumToPC();
    }

    private void decodeValuesParameter(final Word parameter) {
        a = decodeA(parameter.a());
        b = decodeB(parameter.b());
    }
    
    public ParameterDecoder decodeA(final int a){
        return decoders[a];
    }
    
    public ParameterDecoder decodeB(final int b){
        return decoders[b];
    }
}
@@ -63,149 +63,8 @@
 %0D%0A%0D%0A
-    private static final int MASK_16BIT = 0xF;%0D%0A    private static final int OxFFFF = 0xFFFF;%0D%0A    private static final int WORD_SIZE = 16;%0D%0A
     
@@ -2536,17 +2536,16 @@
 = decode
-A
 (paramet
@@ -2572,17 +2572,16 @@
 = decode
-B
 (paramet
@@ -2589,36 +2589,32 @@
 r.b());%0D%0A    %7D%0D%0A
-    
 %0D%0A    public Par
@@ -2633,17 +2633,16 @@
 r decode
-A
 (final i
@@ -2648,10 +2648,15 @@
 int 
-a)
+value) 
 %7B%0D%0A 
@@ -2682,102 +2682,13 @@
 ers%5B
-a%5D;%0D%0A    %7D%0D%0A    %0D%0A    public ParameterDecoder decodeB(final int b)%7B%0D%0A        return decoders%5Bb
+value
 %5D;%0D%0A

refactor to cpu, clean some unused constants and remove uncessary method