package fabrik.simulator.pic16f84; public class Commands { private static int wRegister = 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); return; case 0b10100000000000: System.out.println("GOTO: " + j); GOTO (j); return; default: break; } switch (instruction & 0x3C00){ case 0b01000000000000: System.out.println("BCF: " + f + " " + b); DataRegister.clearBit(f, b); return; case 0b01010000000000: System.out.println("BSF: " + f + " " + b); DataRegister.setBit(f, b); return; case 0b01100000000000: System.out.println("BTFSC: " + f + " " + b); BTFSC(f, b); return; case 0b01110000000000: System.out.println("BTFSS: " + f + " " + b); BTFSS(f, b); return; case 0b11000000000000: System.out.println("MOVLW: " + k); MOVLW (k); return; case 0b11010000000000: System.out.println("RETLW: " + k); RETLW (k); return; default: break; } switch (instruction & 0x3E00){ case 0b11111000000000: System.out.println("ADDLW: " + k); ADDLW (k); return; case 0b11110000000000: System.out.println("SUBLW: " + k); SUBLW (k); return; default: break; } switch (instruction & 0x3F00){ case 0b00011100000000: System.out.println("ADDWF: " + f + " " + d); ADDWF (f, d); return; case 0b00010100000000: System.out.println("ANDWF: " + f + " " + d); ANDWF (f, d); return; case 0b00100100000000: System.out.println("COMF: " + f + " " + d); COMF (f, d); return; case 0b00001100000000: System.out.println("DECF: " + f + " " + d); DECF (f, d); return; case 0b00101100000000: System.out.println("DECFSZ: " + f + " " + d); DECFSZ (f, d); return; case 0b00101000000000: System.out.println("INCF: " + f + " " + d); INCF (f, d); return; case 0b00111100000000: System.out.println("INCFSZ: " + f + " " + d); INCFSZ (f, d); return; case 0b00010000000000: System.out.println("IORWF: " + f + " " + d); IORWF (f, d); return; case 0b00100000000000: System.out.println("MOVF: " + f + " " + d); MOVF (f, d); return; case 0b00110100000000: System.out.println("RLF: " + f + " " + d); RLF (f, d); return; case 0b00110000000000: System.out.println("RRF: " + f + " " + d); RRF (f, d); return; case 0b00001000000000: System.out.println("SUBWF: " + f + " " + d); SUBWF (f, d); return; case 0b00111000000000: System.out.println("SWAPF: " + f + " " + d); SWAPF (f, d); return; case 0b00011000000000: System.out.println("XORWF: " + f + " " + d); XORWF (f, d); return; case 0b11100100000000: System.out.println("ANDLW: " + k); ANDLW (k); return; case 0b11101000000000: System.out.println("XORLW: " + k); XORLW (k); return; case 0b11100000000000: System.out.println("IORLW: " + k); IORLW (k); return; default: break; } switch (instruction & 0x3F80){ case 0b00000110000000: System.out.println("CLRF: " + f); CLRF (f); return; case 0b00000100000000: System.out.println("CLRW"); CLRW (); return; case 0b00000010000000: System.out.println("MOVWF: " + f); MOVWF (f); return; default: break; } switch (instruction){ case 0b01100100: System.out.println("CLRWDT"); //TODO return; case 0b1001: System.out.println("RETFIE"); //TODO return; case 0b1000: System.out.println("RETURN"); //TODO return; case 0b01100011: System.out.println("SLEEP"); //TODO return; default: break; } if (instruction == 0 || instruction == 0b0110000 || instruction == 0b01000000 || instruction == 0b00100000){ System.out.println("NOP"); } else{ System.out.println("Nicht gefunden!"); } } public static void GOTO(int jump) { DataRegister.setPC(jump-1); } public static void CALL(int jump) { //TODO } public static void MOVWF(int file) { DataRegister.setRegister(file, wRegister); } public static void CLRW() { wRegister = 0; DataRegister.determineZeroFlag(wRegister); } 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); } 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; } 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; } 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; //TODO: PC vom Stack holen und laden } public static void MOVLW(int literal) { wRegister = literal; } public static void BTFSC(int address, int bit) { if (DataRegister.getBit(address, bit) == 0){ DataRegister.increasePC(); } } public static void BTFSS(int address, int bit) { if (DataRegister.getBit(address, bit) == 1){ DataRegister.increasePC(); } } }