package fabrik.simulator.pic16f84;

public class Commands {
    private static int wRegister = 0;

    static long totalExecutionTime = 0;

    public static int get_wRegister() {
        return wRegister;
    }

    public static void decode(int instruction){
        final int jumpaddr = 0x7FF;
        final int fileregaddr = 0x7F;
        final int constant = 0xFF;
        final int bitaddr = 0x380;
        final int destinationbit = 0x80;

        int d = (instruction & destinationbit) >> 7;
        int f = instruction & fileregaddr;
        int k = instruction & constant;
        int b = (instruction & bitaddr) >> 7;
        int j = instruction & jumpaddr;

        switch (instruction & 0x3800){
            case 0b10000000000000:
                System.out.println("CALL: " + j);
                CALL (j);
                addExecutionTime(2);
                return;
            case 0b10100000000000:
                System.out.println("GOTO: " + j);
                GOTO (j);
                addExecutionTime(2);
                return;
            default:
                break;
        }

        switch (instruction & 0x3C00){
            case 0b01000000000000:
                System.out.println("BCF: " + f + " " + b);
                DataRegister.clearBit(f, b);
                addExecutionTime(1);
                return;
            case 0b01010000000000:
                System.out.println("BSF: " + f + " " + b);
                DataRegister.setBit(f, b);
                addExecutionTime(1);
                return;
            case 0b01100000000000:
                System.out.println("BTFSC: " + f + " " + b);
                BTFSC(f, b);
                addExecutionTime(1);
                return;
            case 0b01110000000000:
                System.out.println("BTFSS: " + f + " " + b);
                BTFSS(f, b);
                addExecutionTime(1);
                return;
            case 0b11000000000000:
                System.out.println("MOVLW: " + k);
                MOVLW (k);
                addExecutionTime(1);
                return;
            case 0b11010000000000:
                System.out.println("RETLW: " + k);
                RETLW (k);
                addExecutionTime(2);
                return;
            default:
                break;
        }

        switch (instruction & 0x3E00){
            case 0b11111000000000:
                System.out.println("ADDLW: " + k);
                ADDLW (k);
                addExecutionTime(1);
                return;
            case 0b11110000000000:
                System.out.println("SUBLW: " + k);
                SUBLW (k);
                addExecutionTime(1);
                return;
            default:
                break;
        }

        switch (instruction & 0x3F00){
            case 0b00011100000000:
                System.out.println("ADDWF: " + f + " " + d);
                ADDWF (f, d);
                addExecutionTime(1);
                return;
            case 0b00010100000000:
                System.out.println("ANDWF: " + f + " " + d);
                ANDWF (f, d);
                addExecutionTime(1);
                return;
            case 0b00100100000000:
                System.out.println("COMF: " + f + " " + d);
                COMF (f, d);
                addExecutionTime(1);
                return;
            case 0b00001100000000:
                System.out.println("DECF: " + f + " " + d);
                DECF (f, d);
                addExecutionTime(1);
                return;
            case 0b00101100000000:
                //1 bzw 2 Laufzeitpunkte
                System.out.println("DECFSZ: " + f + " " + d);
                DECFSZ (f, d);
                addExecutionTime(1);
                return;
            case 0b00101000000000:
                System.out.println("INCF: " + f + " " + d);
                INCF (f, d);
                addExecutionTime(1);
                return;
            case 0b00111100000000:
                System.out.println("INCFSZ: " + f + " " + d);
                INCFSZ (f, d);
                addExecutionTime(1);
                return;
            case 0b00010000000000:
                System.out.println("IORWF: " + f + " " + d);
                IORWF (f, d);
                addExecutionTime(1);
                return;
            case 0b00100000000000:
                System.out.println("MOVF: " + f + " " + d);
                MOVF (f, d);
                addExecutionTime(1);
                return;
            case 0b00110100000000:
                System.out.println("RLF: " + f + " " + d);
                RLF (f, d);
                addExecutionTime(1);
                return;
            case 0b00110000000000:
                System.out.println("RRF: " + f + " " + d);
                RRF (f, d);
                addExecutionTime(1);
                return;
            case 0b00001000000000:
                System.out.println("SUBWF: " + f + " " + d);
                SUBWF (f, d);
                addExecutionTime(1);
                return;
            case 0b00111000000000:
                System.out.println("SWAPF: " + f + " " + d);
                SWAPF (f, d);
                addExecutionTime(1);
                return;
            case 0b00011000000000:
                System.out.println("XORWF: " + f + " " + d);
                XORWF (f, d);
                addExecutionTime(1);
                return;
            case 0b11100100000000:
                System.out.println("ANDLW: " + k);
                ANDLW (k);
                addExecutionTime(1);
                return;
            case 0b11101000000000:
                System.out.println("XORLW: " + k);
                XORLW (k);
                addExecutionTime(1);
                return;
            case 0b11100000000000:
                System.out.println("IORLW: " + k);
                IORLW (k);
                addExecutionTime(1);
                return;
            default:
                break;
        }

        switch (instruction & 0x3F80){
            case 0b00000110000000:
                System.out.println("CLRF: " + f);
                CLRF (f);
                addExecutionTime(1);
                return;
            case 0b00000100000000:
                System.out.println("CLRW");
                CLRW ();
                addExecutionTime(1);
                return;
            case 0b00000010000000:
                System.out.println("MOVWF: " + f);
                MOVWF (f);
                addExecutionTime(1);
                return;
            default:
                break;
        }

        switch (instruction){
            case 0b01100100:
                System.out.println("CLRWDT");
                WatchdogTimer.reset();
                return;
            case 0b1001:
                System.out.println("RETFIE");
                RETFIE();
                addExecutionTime(2);
                return;
            case 0b1000:
                System.out.println("RETURN");
                DataRegister.setPC(ProgramStack.pop());
                return;
            case 0b01100011:
                System.out.println("SLEEP");
                SLEEP();
                addExecutionTime(1);
                return;
            default:
                break;
        }

        if (instruction == 0 || instruction == 0b0110000 || instruction == 0b01000000 || instruction == 0b00100000){
            NOP ();
        }
        else{
            System.out.println("Nicht gefunden!");
        }


    }

    public static void SLEEP() {
        Controller_Frontend.sleep ();
    }

    public static void RETFIE() {
        DataRegister.setPC(ProgramStack.pop());
    }

    public static void NOP () {
        System.out.println("NOP");
        addExecutionTime(1);
    }

    public static void GOTO(int jump) {
        DataRegister.setPC(jump-1);
    }

    public static void CALL(int jump) {
        ProgramStack.push(DataRegister.getPC()+1);
        DataRegister.setPC(jump-1);
        
        addExecutionTime(1);
    }

    public static void addExecutionTime(int i) {
        totalExecutionTime +=  i;
        Timer.cycles(i);
    }

    public static long getTotalExecutionTime() {
        return totalExecutionTime;
    }



    public static void MOVWF(int file) {
        DataRegister.setRegister(file, wRegister);
    }

    public static void CLRW() {
        wRegister = 0;
        DataRegister.determineZeroFlag(wRegister);

        addExecutionTime(1);

    }

    public static void CLRF(int file) {
        DataRegister.setRegister(file, 0);
        DataRegister.determineZeroFlag(DataRegister.getRegister(file));


    }

    public static void IORLW(int literal) {
        wRegister |= literal;
        DataRegister.determineZeroFlag(wRegister);
    }

    public static void XORLW(int literal) {
        wRegister ^= literal;
        DataRegister.determineZeroFlag(wRegister);
    }

    public static void ANDLW(int literal) {
        wRegister &= literal;
        DataRegister.determineZeroFlag(wRegister);
    }

    public static void XORWF(int file, int destination) {
        int result = wRegister ^ DataRegister.getRegister(file);
        if (destination == 0){
            wRegister = result;
        }
        else {
            DataRegister.setRegister(file, result);
        }
        DataRegister.determineZeroFlag(result);


    }

    public static void SWAPF(int file, int destination) {
        int content = DataRegister.getRegister(file);
        int result = (content & 0x0F) << 4 | (content & 0xF0) >> 4;
        if (destination == 0){
            wRegister = result;
        }
        else {
            DataRegister.setRegister(file, result);
        }

    }

    public static void SUBWF(int file, int destination) {
        int result = (DataRegister.getRegister(file) - wRegister) ;
        if (wRegister == 0 || result >= 0){
            DataRegister.setCarryFlag(1);
            DataRegister.setDigitCarryFlag(1);
        }
        else {
            DataRegister.setCarryFlag(0);
            DataRegister.setDigitCarryFlag(0);
        }
        if (destination == 0){
            wRegister = result & 0xFF;
        }
        else {
            DataRegister.setRegister(file, result & 0xFF);
        }
        DataRegister.determineZeroFlag(result);
        addExecutionTime(1);
    }

    public static void RLF(int file, int destination) {
        int fcontent = DataRegister.getRegister(file);
        int carry = DataRegister.getCarryFlag();
        int result = (fcontent << 1) | carry;
        DataRegister.determineCarryFlag(result);
        if (destination == 0){
            wRegister = result & 0xFF;
        }
        else {
            DataRegister.setRegister(file, result & 0xFF);
        }

    }

    public static void RRF(int file, int destination) {
        int fcontent = DataRegister.getRegister(file);
        int carry = DataRegister.getCarryFlag() << 7;
        int contentlow = fcontent & 0b1;
        int result = (fcontent >> 1) | carry;
        DataRegister.setCarryFlag(contentlow);
        if (destination == 0){
            wRegister = result & 0xFF;
        }
        else {
            DataRegister.setRegister(file, result & 0xFF);
        }

    }

    public static void MOVF(int file, int destination) {
        int content = DataRegister.getRegister(file);
        if (destination == 0){
            wRegister = content;
        }
        else {
            DataRegister.setRegister(file, content);
        }
        DataRegister.determineZeroFlag(content);
    }

    public static void IORWF(int file, int destination) {
        int result = wRegister | DataRegister.getRegister(file);
        if (destination == 0){
            wRegister = result;
        }
        else {
            DataRegister.setRegister(file, result);
        }
        DataRegister.determineZeroFlag(result);

    }

    public static void INCFSZ(int file, int destination) {
        int result = (DataRegister.getRegister(file) + 1) & 0xFF;
        if (destination == 0){
            wRegister = result;
            addExecutionTime(1);
        }
        else {
            DataRegister.setRegister(file, result);

        }
        if (result == 0) {
            DataRegister.increasePC();
        }
    }

    public static void INCF(int file, int destination) {
        int result = (DataRegister.getRegister(file) + 1) & 0xFF;
        if (destination == 0){
            wRegister = result;
        }
        else {
            DataRegister.setRegister(file, result);
        }
        DataRegister.determineZeroFlag(result);

    }

    public static void DECFSZ(int file, int destination) {
        int result = (DataRegister.getRegister(file) -1) & 0xFF;
        if (destination == 0){
            wRegister = result;
            addExecutionTime(1);

        }
        else {
            DataRegister.setRegister(file, result);

        }
        if (result == 0){
            DataRegister.increasePC();
        }
    }

    public static void DECF(int file, int destination) {
        int result = (DataRegister.getRegister(file) -1) & 0xFF;
        if (destination == 0){
            wRegister = result;
        }
        else {
            DataRegister.setRegister(file, result);
        }
        DataRegister.determineZeroFlag(result);


    }

    private static void COMF(int file, int destination) {
        int result = (~ DataRegister.getRegister(file)) & 0xFF;
        if (destination == 0){
            wRegister = result;
        }
        else {
            DataRegister.setRegister(file, result);
        }
        DataRegister.determineZeroFlag(result);



    }

    public static void ANDWF(int file, int destination) {
        int result = wRegister & DataRegister.getRegister(file);
        if (destination == 0){
            wRegister = result & 0xFF;
        }
        else {
            DataRegister.setRegister(file, result & 0xFF);
        }
        DataRegister.determineZeroFlag(result);
        DataRegister.determineCarryFlag(result);
        DataRegister.determineDigitCarryFlag(result);

    }

    public static void ADDWF(int file, int destination) {
        int result = wRegister + DataRegister.getRegister(file);
        if (wRegister == 0 || DataRegister.getRegister(file) == 0){
            DataRegister.setCarryFlag(0);
            DataRegister.setDigitCarryFlag(0);
        }
        else {
            DataRegister.determineCarryFlag(result);
            DataRegister.determineDigitCarryFlag(result);
        }
        if (destination == 0) {
            wRegister = result & 0xFF;
        }
        else {
            DataRegister.setRegister(file, result & 0xFF);
        }
        DataRegister.determineZeroFlag(result);


    }

    public static void SUBLW(int literal) {
        int result = literal - wRegister;
        if (wRegister == 0 || result >= 0){
            DataRegister.setCarryFlag(1);
            DataRegister.setDigitCarryFlag(1);
        }
        else {

            DataRegister.setCarryFlag(0);
            DataRegister.setDigitCarryFlag(0);
        }
        wRegister = result & 0xFF;
        DataRegister.determineZeroFlag(result);
    }

    public static void ADDLW(int literal) {
        int result = wRegister + literal;
        wRegister = result & 0xFF;
        DataRegister.determineZeroFlag(result);
        DataRegister.determineCarryFlag(result);
        DataRegister.determineDigitCarryFlag(result);
    }

    public static void RETLW(int literal) {
        wRegister = literal;
        DataRegister.setPC(ProgramStack.pop());
    }

    public static void MOVLW(int literal) {
        wRegister = literal;
    }

    public static void BTFSC(int address, int bit) {
        if (DataRegister.getBit(address, bit) == 0){
            DataRegister.increasePC();
            addExecutionTime(1);
        }
    }

    public static void BTFSS(int address, int bit) {
        if (DataRegister.getBit(address, bit) == 1){
            DataRegister.increasePC();
            addExecutionTime(1);
        }
    }
}
