diff --git a/src/main/java/fabrik/simulator/pic16f84/Commands.java b/src/main/java/fabrik/simulator/pic16f84/Commands.java index 6d54acd..4998a4d 100644 --- a/src/main/java/fabrik/simulator/pic16f84/Commands.java +++ b/src/main/java/fabrik/simulator/pic16f84/Commands.java @@ -3,6 +3,8 @@ package fabrik.simulator.pic16f84; public class Commands { private static int wRegister = 0; + static long totalExecutionTime = 0; + public static int get_wRegister() { return wRegister; } @@ -24,10 +26,12 @@ public class Commands { 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; @@ -37,26 +41,32 @@ public class Commands { 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; @@ -66,10 +76,12 @@ public class Commands { 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; @@ -79,71 +91,88 @@ public class Commands { 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; @@ -153,14 +182,17 @@ public class Commands { 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; @@ -174,6 +206,7 @@ public class Commands { case 0b1001: System.out.println("RETFIE"); RETFIE(); + addExecutionTime(2); return; case 0b1000: System.out.println("RETURN"); @@ -182,6 +215,7 @@ public class Commands { case 0b01100011: System.out.println("SLEEP"); //TODO + addExecutionTime(1); return; default: break; @@ -189,6 +223,7 @@ public class Commands { if (instruction == 0 || instruction == 0b0110000 || instruction == 0b01000000 || instruction == 0b00100000){ System.out.println("NOP"); + addExecutionTime(1); } else{ System.out.println("Nicht gefunden!"); @@ -208,20 +243,38 @@ public class Commands { 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; + } + + 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) { @@ -248,6 +301,8 @@ public class Commands { DataRegister.setRegister(file, result); } DataRegister.determineZeroFlag(result); + + } public static void SWAPF(int file, int destination) { @@ -259,6 +314,7 @@ public class Commands { else { DataRegister.setRegister(file, result); } + } public static void SUBWF(int file, int destination) { @@ -278,6 +334,7 @@ public class Commands { DataRegister.setRegister(file, result & 0xFF); } DataRegister.determineZeroFlag(result); + addExecutionTime(1); } public static void RLF(int file, int destination) { @@ -291,6 +348,7 @@ public class Commands { else { DataRegister.setRegister(file, result & 0xFF); } + } public static void RRF(int file, int destination) { @@ -305,6 +363,7 @@ public class Commands { else { DataRegister.setRegister(file, result & 0xFF); } + } public static void MOVF(int file, int destination) { @@ -316,6 +375,7 @@ public class Commands { DataRegister.setRegister(file, content); } DataRegister.determineZeroFlag(content); + addExecutionTime(1); } public static void IORWF(int file, int destination) { @@ -327,15 +387,18 @@ public class Commands { 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(); @@ -351,15 +414,19 @@ public class Commands { 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(); @@ -375,6 +442,8 @@ public class Commands { DataRegister.setRegister(file, result); } DataRegister.determineZeroFlag(result); + + } private static void COMF(int file, int destination) { @@ -386,6 +455,9 @@ public class Commands { DataRegister.setRegister(file, result); } DataRegister.determineZeroFlag(result); + + + } public static void ANDWF(int file, int destination) { @@ -399,6 +471,7 @@ public class Commands { DataRegister.determineZeroFlag(result); DataRegister.determineCarryFlag(result); DataRegister.determineDigitCarryFlag(result); + } public static void ADDWF(int file, int destination) { @@ -418,6 +491,8 @@ public class Commands { DataRegister.setRegister(file, result & 0xFF); } DataRegister.determineZeroFlag(result); + + } public static void SUBLW(int literal) { @@ -455,12 +530,14 @@ public class Commands { 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); } } } diff --git a/src/main/java/fabrik/simulator/pic16f84/Controller_Frontend.java b/src/main/java/fabrik/simulator/pic16f84/Controller_Frontend.java index 9975190..063c74b 100644 --- a/src/main/java/fabrik/simulator/pic16f84/Controller_Frontend.java +++ b/src/main/java/fabrik/simulator/pic16f84/Controller_Frontend.java @@ -28,6 +28,8 @@ import javafx.stage.Stage; import java.util.Arrays; import java.util.Objects; +import static fabrik.simulator.pic16f84.Commands.totalExecutionTime; + public class Controller_Frontend { @@ -36,6 +38,7 @@ public class Controller_Frontend { private int [] ind; + @FXML private Button stepintoButton; @@ -71,6 +74,9 @@ public class Controller_Frontend { @FXML private Button stopButton; + @FXML + private Label totalExecutionTimeLabel; + @@ -111,6 +117,7 @@ public class Controller_Frontend { }); autoRunThread.setDaemon(true); autoRunThread.start(); + totalExecutionTimeLabel.setText("Total Execution Time: " + Commands.getTotalExecutionTime() + " ms"); } @@ -312,6 +319,8 @@ public class Controller_Frontend { stopButton.setOnAction(this::stopAutoRun); } + + private static void setTRISbuttons(ToggleButtonGroup[] allButtons) { allTRISbuttons = allButtons; } diff --git a/src/main/java/fabrik/simulator/pic16f84/WatchdogTimer.java b/src/main/java/fabrik/simulator/pic16f84/WatchdogTimer.java new file mode 100644 index 0000000..a5cb483 --- /dev/null +++ b/src/main/java/fabrik/simulator/pic16f84/WatchdogTimer.java @@ -0,0 +1,93 @@ +package fabrik.simulator.pic16f84; + +import java.util.Timer; +import java.util.TimerTask; + +public class WatchdogTimer { + private int prescaler; + private int counter; + private final int MAX_PRESCALER = 128; + + public WatchdogTimer(int prescaler) { + + this.prescaler = Math.min(prescaler, MAX_PRESCALER); + this.counter = 0; + } + + public void start() { + + activateWatchdog(prescaler); + + int watchdogTime = 18 * prescaler; // Zeit in Millisekunden + + + Timer timer = new Timer(); + timer.schedule(new TimerTask() { + @Override + public void run() { + counter++; + if (counter >= MAX_PRESCALER) { + + reset(); + System.out.println("Watchdog Timeout!"); + + } + } + }, 0, watchdogTime); + } + + + public void reset() { + counter = 0; + } + + + private void activateWatchdog(int prescaler) { + // Teilerfaktor entsprechend einstellen + switch (prescaler) { + case 1: + + DataRegister.setBit(0x81, 0); // Setze PS2:0 auf 000 + DataRegister.setBit(0x81, 1); // Deaktiviere den Watchdog-Timer + break; + case 2: + + DataRegister.clearBit(0x81, 0); // Setze PS2:0 auf 001 + DataRegister.setBit(0x81, 1); // Deaktiviere den Watchdog-Timer + break; + case 4: + + DataRegister.setBit(0x81, 0); // Setze PS2:0 auf 010 + DataRegister.setBit(0x81, 1); // Deaktiviere den Watchdog-Timer + break; + case 8: + + DataRegister.setBit(0x81, 0); // Setze PS2:0 auf 011 + DataRegister.setBit(0x81, 1); // Deaktiviere den Watchdog-Timer + break; + case 16: + + DataRegister.clearBit(0x81, 0); // Setze PS2:0 auf 100 + DataRegister.clearBit(0x81, 1); // Aktiviere den Watchdog-Timer + break; + case 32: + + DataRegister.setBit(0x81, 0); // Setze PS2:0 auf 101 + DataRegister.clearBit(0x81, 1); // Aktiviere den Watchdog-Timer + break; + case 64: + + DataRegister.clearBit(0x81, 0); // Setze PS2:0 auf 110 + DataRegister.setBit(0x81, 1); // Aktiviere den Watchdog-Timer + break; + case 128: + + DataRegister.setBit(0x81, 0); // Setze PS2:0 auf 111 + DataRegister.setBit(0x81, 1); // Aktiviere den Watchdog-Timer + break; + default: + System.out.println("NÜCHTITTGGGGG für den Watchdog-Timer."); + break; + } + } +} diff --git a/src/main/resources/fabrik/simulator/pic16f84/frontend.fxml b/src/main/resources/fabrik/simulator/pic16f84/frontend.fxml index 8842ca4..8c0dab1 100644 --- a/src/main/resources/fabrik/simulator/pic16f84/frontend.fxml +++ b/src/main/resources/fabrik/simulator/pic16f84/frontend.fxml @@ -20,6 +20,7 @@ + @@ -30,6 +31,8 @@