Introduce ExecutionTimeSubject

This commit is contained in:
2025-05-26 15:32:36 +02:00
parent 06e9348016
commit 85bc6e9eba
14 changed files with 131 additions and 40 deletions

View File

@ -1,7 +1,8 @@
package fabrik.simulator.pic16f84; package fabrik.simulator.pic16f84;
import fabrik.simulator.pic16f84.interfaces.*; import fabrik.simulator.pic16f84.interfaces.*;
public class Commands extends PICComponent implements CommandInterface { public class Commands extends ExecutionTimeSubject implements CommandInterface {
private int wRegister; private int wRegister;
private long totalExecutionTime; private long totalExecutionTime;
private double executionTimeMultiplier = 1; private double executionTimeMultiplier = 1;
@ -18,16 +19,17 @@ public class Commands extends PICComponent implements CommandInterface {
public void addExecutionTime(int i) { public void addExecutionTime(int i) {
totalExecutionTime += i; totalExecutionTime += i;
timer.cycles(i); super.notifyObservers();
eeprom.registerTime(false);
} }
@Override
public double getTotalExecutionTime() { public double getTotalExecutionTime() {
return (totalExecutionTime * getExecutionTimeMultiplier()); return (totalExecutionTime * getExecutionTimeMultiplier());
} }
public void resetTotalExecutionTime() { public void resetTotalExecutionTime() {
totalExecutionTime = 0; totalExecutionTime = 0;
super.notifyObservers();
} }
@ -556,10 +558,12 @@ public class Commands extends PICComponent implements CommandInterface {
} }
} }
@Override
public double getExecutionTimeMultiplier(){ public double getExecutionTimeMultiplier(){
return executionTimeMultiplier; return executionTimeMultiplier;
} }
@Override
public void setExecutionTimeMultiplier(String option){ public void setExecutionTimeMultiplier(String option){
switch (option) { switch (option) {
case "8 MHZ": case "8 MHZ":
@ -584,11 +588,6 @@ public class Commands extends PICComponent implements CommandInterface {
executionTimeMultiplier = 125; executionTimeMultiplier = 125;
break; break;
} }
} super.notifyObservers();
@Override
public void initialize(PICComponentLocator locator) {
super.initialize(locator);
} }
} }

View File

@ -38,7 +38,7 @@ import java.util.Set;
import static java.lang.Math.max; import static java.lang.Math.max;
public class Controller_Frontend extends PICComponent implements FrontendControllerInterface { public class Controller_Frontend extends PICComponent implements FrontendControllerInterface, ExecutionTimeObserver {
private int [] prog; private int [] prog;
private int [][] read; private int [][] read;
@ -165,7 +165,7 @@ public class Controller_Frontend extends PICComponent implements FrontendControl
private void updateExecutionTimeMultiplier() { private void updateExecutionTimeMultiplier() {
String selectedOption = executionTimeComboBox.getValue(); String selectedOption = executionTimeComboBox.getValue();
commands.setExecutionTimeMultiplier(selectedOption); executionTime.setExecutionTimeMultiplier(selectedOption);
} }
@FXML @FXML
@ -226,10 +226,9 @@ public class Controller_Frontend extends PICComponent implements FrontendControl
ioPorts.refreshUI(getTRISbuttons(), getPORTbuttons()); ioPorts.refreshUI(getTRISbuttons(), getPORTbuttons());
updateStack(); updateStack();
updateWatchdog(); updateWatchdog();
double totalExecutionTime = commands.getTotalExecutionTime();
totalExecutionTimeLabel.setText("Total Execution Time: " + totalExecutionTime + "µs");
} }
private void markSelectedRow(int currentIndex, String selectedRowStyle) { private void markSelectedRow(int currentIndex, String selectedRowStyle) {
lstContentListView.setCellFactory(column -> new ListCell<String>() { lstContentListView.setCellFactory(column -> new ListCell<String>() {
@Override @Override
@ -284,7 +283,7 @@ public class Controller_Frontend extends PICComponent implements FrontendControl
ExecutionState.wakeUp(); ExecutionState.wakeUp();
breakpoints.clear(); breakpoints.clear();
ioPorts.refreshUI(getTRISbuttons(), getPORTbuttons()); ioPorts.refreshUI(getTRISbuttons(), getPORTbuttons());
commands.resetTotalExecutionTime(); executionTime.resetTotalExecutionTime();
watchdogTimer.reset(); watchdogTimer.reset();
wdtCheck.setSelected(false); wdtCheck.setSelected(false);
table.refresh(); table.refresh();
@ -570,5 +569,10 @@ public class Controller_Frontend extends PICComponent implements FrontendControl
this.picComponents = locator; this.picComponents = locator;
System.out.println("Frontend"); System.out.println("Frontend");
} }
@Override
public void executionTimeChanged() {
totalExecutionTimeLabel.setText("Total Execution Time: " + executionTime.getTotalExecutionTime() + "µs");
}
} }

View File

@ -13,7 +13,7 @@ import java.nio.file.Paths;
import fabrik.simulator.pic16f84.interfaces.*; import fabrik.simulator.pic16f84.interfaces.*;
public class EEPROM extends PICComponent implements EEPROMInterface { public class EEPROM extends PICComponent implements EEPROMInterface, ExecutionTimeObserver {
private final int EEDATA = 0x08; private final int EEDATA = 0x08;
private final int EEADR = 0x09; private final int EEADR = 0x09;
private final int EECON1 = 0x88; private final int EECON1 = 0x88;
@ -83,13 +83,13 @@ public class EEPROM extends PICComponent implements EEPROMInterface {
dataRegister.setDirectBit(EECON1, WRERR, 1); dataRegister.setDirectBit(EECON1, WRERR, 1);
return; return;
} }
registerTime(true); registerTime(executionTime.getTotalExecutionTime(), true);
} }
public void registerTime(boolean reset) { public void registerTime(double executionTime, boolean reset) {
if (reset) if (reset)
startTime = commands.getTotalExecutionTime(); startTime = executionTime;
else if ((commands.getTotalExecutionTime() >= (startTime + 1000)) && writeControl) { else if ((executionTime >= (startTime + 1000)) && writeControl) {
eecon2stages = new boolean[]{false, false}; eecon2stages = new boolean[]{false, false};
dataRegister.setDirectBit(EECON1, EEIF, 1); dataRegister.setDirectBit(EECON1, EEIF, 1);
dataRegister.setDirectBit(EECON1, WR, 0); dataRegister.setDirectBit(EECON1, WR, 0);
@ -175,4 +175,9 @@ public class EEPROM extends PICComponent implements EEPROMInterface {
public void initialize(PICComponentLocator locator) { public void initialize(PICComponentLocator locator) {
super.initialize(locator); super.initialize(locator);
} }
@Override
public void executionTimeChanged() {
registerTime(executionTime.getTotalExecutionTime(), false);
}
} }

View File

@ -0,0 +1,75 @@
package fabrik.simulator.pic16f84;
import java.util.HashSet;
import java.util.Set;
import fabrik.simulator.pic16f84.interfaces.ExecutionTimeObserver;
import fabrik.simulator.pic16f84.interfaces.PICComponentInterface;
public abstract class ExecutionTimeSubject extends PICComponent implements PICComponentInterface{
private Set<ExecutionTimeObserver> observers;
public ExecutionTimeSubject(){
super();
this.observers = new HashSet<>();
}
public void registerObserver(ExecutionTimeObserver observer){
observers.add(observer);
}
public void unregisterObserver(ExecutionTimeObserver observer){
observers.remove(observer);
}
protected void notifyObservers(){
for (final ExecutionTimeObserver each : observers){
notify(each);
}
}
protected void notify(final ExecutionTimeObserver observer) {
createNotifierFor(observer).start();
}
protected Thread createNotifierFor(ExecutionTimeObserver observer) {
return new Thread(){
public void run(){
try {
observer.executionTimeChanged();
} catch (Exception e){
e.printStackTrace();
ExecutionTimeSubject.this.observers.remove(observer);
}
}
};
}
public double getTotalExecutionTime(){
throw new UnsupportedOperationException("No class implements ExecutionTimeSubject correctly!");
}
public void addExecutionTime(int i){
throw new UnsupportedOperationException("No class implements ExecutionTimeSubject correctly!");
}
public double getExecutionTimeMultiplier(){
throw new UnsupportedOperationException("No class implements ExecutionTimeSubject correctly!");
}
public void setExecutionTimeMultiplier(String option){
throw new UnsupportedOperationException("No class implements ExecutionTimeSubject correctly!");
}
public void resetTotalExecutionTime(){
throw new UnsupportedOperationException("No class implements ExecutionTimeSubject correctly!");
}
@Override
public void initialize(PICComponentLocator locator) {
super.initialize(locator);
}
}

View File

@ -6,7 +6,9 @@ public class Main {
private static final PICComponentLocator picComponents = new PICComponentLocator(); // Subjekt private static final PICComponentLocator picComponents = new PICComponentLocator(); // Subjekt
public static void main(String[] args) { public static void main(String[] args) {
picComponents.registerComponent(CommandInterface.class, new Commands()); Commands commands = new Commands();
picComponents.registerComponent(CommandInterface.class, commands);
picComponents.registerComponent(ExecutionTimeSubject.class, commands);
picComponents.registerComponent(DataRegisterInterface.class, new DataRegister()); picComponents.registerComponent(DataRegisterInterface.class, new DataRegister());
picComponents.registerComponent(EEPROMInterface.class, new EEPROM()); picComponents.registerComponent(EEPROMInterface.class, new EEPROM());
picComponents.registerComponent(InterruptInterface.class, new Interrupts()); picComponents.registerComponent(InterruptInterface.class, new Interrupts());

View File

@ -14,6 +14,7 @@ public abstract class PICComponent {
WatchdogTimerInterface watchdogTimer; WatchdogTimerInterface watchdogTimer;
ProgramStackInterface programStack; ProgramStackInterface programStack;
CommandInterface commands; CommandInterface commands;
ExecutionTimeSubject executionTime;
ToggleButtonInterface toggleButtonExt; ToggleButtonInterface toggleButtonExt;
public void initialize(PICComponentLocator locator) { public void initialize(PICComponentLocator locator) {
@ -29,5 +30,6 @@ public abstract class PICComponent {
programStack = locator.getComponent(ProgramStackInterface.class); programStack = locator.getComponent(ProgramStackInterface.class);
commands = locator.getComponent(CommandInterface.class); commands = locator.getComponent(CommandInterface.class);
table = locator.getComponent(TableInterface.class); table = locator.getComponent(TableInterface.class);
executionTime = locator.getComponent(ExecutionTimeSubject.class);
} }
} }

View File

@ -1,8 +1,9 @@
package fabrik.simulator.pic16f84; package fabrik.simulator.pic16f84;
import fabrik.simulator.pic16f84.interfaces.ExecutionTimeObserver;
import fabrik.simulator.pic16f84.interfaces.TimerInterface; import fabrik.simulator.pic16f84.interfaces.TimerInterface;
public class Timer extends PICComponent implements TimerInterface { public class Timer extends PICComponent implements TimerInterface, ExecutionTimeObserver {
private final int TIMERREG = 0x1; private final int TIMERREG = 0x1;
private final int T0SE = 0x4; private final int T0SE = 0x4;
private final int T0CS = 0x5; private final int T0CS = 0x5;
@ -67,4 +68,9 @@ public class Timer extends PICComponent implements TimerInterface {
public void initialize(PICComponentLocator locator) { public void initialize(PICComponentLocator locator) {
super.initialize(locator); super.initialize(locator);
} }
@Override
public void executionTimeChanged() {
cycles((int) executionTime.getTotalExecutionTime());
}
} }

View File

@ -21,18 +21,18 @@ public class WatchdogTimer extends PICComponent implements WatchdogTimerInterfac
if (enabled) { if (enabled) {
if (realtimer >= (watchdogTime + lastReset - 1)) { if (realtimer >= (watchdogTime + lastReset - 1)) {
dataRegister.clearBit(3, 4); dataRegister.clearBit(3, 4);
lastReset = commands.getTotalExecutionTime(); lastReset = executionTime.getTotalExecutionTime();
frontendController.stopRunFromBackend("Watchdog Timer"); frontendController.stopRunFromBackend("Watchdog Timer");
} }
else { else {
rawtimer++; rawtimer++;
realtimer = (long) (rawtimer * commands.getExecutionTimeMultiplier()); realtimer = (long) (rawtimer * executionTime.getExecutionTimeMultiplier());
} }
} }
} }
public void reset (){ public void reset (){
lastReset = commands.getTotalExecutionTime(); lastReset = executionTime.getTotalExecutionTime();
rawtimer = 0; rawtimer = 0;
realtimer = 0; realtimer = 0;
preScaler.reset(); preScaler.reset();

View File

@ -3,17 +3,10 @@ package fabrik.simulator.pic16f84.interfaces;
public interface CommandInterface extends PICComponentInterface { public interface CommandInterface extends PICComponentInterface {
void CALL(int isr); void CALL(int isr);
double getTotalExecutionTime();
int get_wRegister(); int get_wRegister();
void decode(int i); void decode(int i);
void resetTotalExecutionTime();
void addExecutionTime(int i);
double getExecutionTimeMultiplier();
void setExecutionTimeMultiplier(String option);
} }

View File

@ -1,7 +1,7 @@
package fabrik.simulator.pic16f84.interfaces; package fabrik.simulator.pic16f84.interfaces;
public interface EEPROMInterface extends PICComponentInterface { public interface EEPROMInterface extends PICComponentInterface {
void registerTime(boolean b); void registerTime(double executionTime, boolean b);
void parse(int i, int content, int i1); void parse(int i, int content, int i1);

View File

@ -0,0 +1,5 @@
package fabrik.simulator.pic16f84.interfaces;
public interface ExecutionTimeObserver {
void executionTimeChanged();
}

View File

@ -27,7 +27,7 @@ class CommandsTests {
EEPROMInterface eepromMock = mock(EEPROMInterface.class); EEPROMInterface eepromMock = mock(EEPROMInterface.class);
picComponents.registerComponent(EEPROMInterface.class, eepromMock); picComponents.registerComponent(EEPROMInterface.class, eepromMock);
CommandInterface commands = new Commands(); Commands commands = new Commands();
picComponents.registerComponent(CommandInterface.class, commands); picComponents.registerComponent(CommandInterface.class, commands);
picComponents.initAll(); picComponents.initAll();
@ -47,7 +47,7 @@ class CommandsTests {
EEPROMInterface eepromMock = mock(EEPROMInterface.class); EEPROMInterface eepromMock = mock(EEPROMInterface.class);
picComponents.registerComponent(EEPROMInterface.class, eepromMock); picComponents.registerComponent(EEPROMInterface.class, eepromMock);
CommandInterface commands = new Commands(); Commands commands = new Commands();
picComponents.registerComponent(CommandInterface.class, commands); picComponents.registerComponent(CommandInterface.class, commands);
picComponents.initAll(); picComponents.initAll();
@ -68,7 +68,7 @@ class CommandsTests {
EEPROMInterface eepromMock = mock(EEPROMInterface.class); EEPROMInterface eepromMock = mock(EEPROMInterface.class);
picComponents.registerComponent(EEPROMInterface.class, eepromMock); picComponents.registerComponent(EEPROMInterface.class, eepromMock);
CommandInterface commands = new Commands(); Commands commands = new Commands();
picComponents.registerComponent(CommandInterface.class, commands); picComponents.registerComponent(CommandInterface.class, commands);
picComponents.initAll(); picComponents.initAll();

View File

@ -69,8 +69,8 @@ class EEPROMTests {
DataRegisterInterface mockDataRegister = Mockito.mock(DataRegisterInterface.class); DataRegisterInterface mockDataRegister = Mockito.mock(DataRegisterInterface.class);
picComponents.registerComponent(DataRegisterInterface.class, mockDataRegister); picComponents.registerComponent(DataRegisterInterface.class, mockDataRegister);
CommandInterface mockCommands = mock(CommandInterface.class); ExecutionTimeSubject mockExecutionTime = mock(ExecutionTimeSubject.class);
picComponents.registerComponent(CommandInterface.class, mockCommands); picComponents.registerComponent(ExecutionTimeSubject.class, mockExecutionTime);
EEPROMInterface eeprom = new EEPROM(); EEPROMInterface eeprom = new EEPROM();
picComponents.registerComponent(EEPROMInterface.class, eeprom); picComponents.registerComponent(EEPROMInterface.class, eeprom);

View File

@ -29,13 +29,13 @@ class WatchDogTimerTests {
picComponents.registerComponent(PreScalerInterface.class, mockPreScaler); picComponents.registerComponent(PreScalerInterface.class, mockPreScaler);
CommandInterface mockCommands = mock(CommandInterface.class); ExecutionTimeSubject mockExecutionTime = mock(ExecutionTimeSubject.class);
when( when(
mockCommands.getExecutionTimeMultiplier() mockExecutionTime.getExecutionTimeMultiplier()
).thenReturn( ).thenReturn(
1.0 1.0
); );
picComponents.registerComponent(CommandInterface.class, mockCommands); picComponents.registerComponent(ExecutionTimeSubject.class, mockExecutionTime);
WatchdogTimerInterface watchDogTimer = new WatchdogTimer(); WatchdogTimerInterface watchDogTimer = new WatchdogTimer();
picComponents.registerComponent(WatchdogTimerInterface.class, watchDogTimer); picComponents.registerComponent(WatchdogTimerInterface.class, watchDogTimer);