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

View File

@ -38,7 +38,7 @@ import java.util.Set;
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 [][] read;
@ -165,7 +165,7 @@ public class Controller_Frontend extends PICComponent implements FrontendControl
private void updateExecutionTimeMultiplier() {
String selectedOption = executionTimeComboBox.getValue();
commands.setExecutionTimeMultiplier(selectedOption);
executionTime.setExecutionTimeMultiplier(selectedOption);
}
@FXML
@ -226,10 +226,9 @@ public class Controller_Frontend extends PICComponent implements FrontendControl
ioPorts.refreshUI(getTRISbuttons(), getPORTbuttons());
updateStack();
updateWatchdog();
double totalExecutionTime = commands.getTotalExecutionTime();
totalExecutionTimeLabel.setText("Total Execution Time: " + totalExecutionTime + "µs");
}
private void markSelectedRow(int currentIndex, String selectedRowStyle) {
lstContentListView.setCellFactory(column -> new ListCell<String>() {
@Override
@ -284,7 +283,7 @@ public class Controller_Frontend extends PICComponent implements FrontendControl
ExecutionState.wakeUp();
breakpoints.clear();
ioPorts.refreshUI(getTRISbuttons(), getPORTbuttons());
commands.resetTotalExecutionTime();
executionTime.resetTotalExecutionTime();
watchdogTimer.reset();
wdtCheck.setSelected(false);
table.refresh();
@ -570,5 +569,10 @@ public class Controller_Frontend extends PICComponent implements FrontendControl
this.picComponents = locator;
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.*;
public class EEPROM extends PICComponent implements EEPROMInterface {
public class EEPROM extends PICComponent implements EEPROMInterface, ExecutionTimeObserver {
private final int EEDATA = 0x08;
private final int EEADR = 0x09;
private final int EECON1 = 0x88;
@ -83,13 +83,13 @@ public class EEPROM extends PICComponent implements EEPROMInterface {
dataRegister.setDirectBit(EECON1, WRERR, 1);
return;
}
registerTime(true);
registerTime(executionTime.getTotalExecutionTime(), true);
}
public void registerTime(boolean reset) {
public void registerTime(double executionTime, boolean reset) {
if (reset)
startTime = commands.getTotalExecutionTime();
else if ((commands.getTotalExecutionTime() >= (startTime + 1000)) && writeControl) {
startTime = executionTime;
else if ((executionTime >= (startTime + 1000)) && writeControl) {
eecon2stages = new boolean[]{false, false};
dataRegister.setDirectBit(EECON1, EEIF, 1);
dataRegister.setDirectBit(EECON1, WR, 0);
@ -175,4 +175,9 @@ public class EEPROM extends PICComponent implements EEPROMInterface {
public void initialize(PICComponentLocator 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
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(EEPROMInterface.class, new EEPROM());
picComponents.registerComponent(InterruptInterface.class, new Interrupts());

View File

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

View File

@ -1,8 +1,9 @@
package fabrik.simulator.pic16f84;
import fabrik.simulator.pic16f84.interfaces.ExecutionTimeObserver;
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 T0SE = 0x4;
private final int T0CS = 0x5;
@ -67,4 +68,9 @@ public class Timer extends PICComponent implements TimerInterface {
public void initialize(PICComponentLocator 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 (realtimer >= (watchdogTime + lastReset - 1)) {
dataRegister.clearBit(3, 4);
lastReset = commands.getTotalExecutionTime();
lastReset = executionTime.getTotalExecutionTime();
frontendController.stopRunFromBackend("Watchdog Timer");
}
else {
rawtimer++;
realtimer = (long) (rawtimer * commands.getExecutionTimeMultiplier());
realtimer = (long) (rawtimer * executionTime.getExecutionTimeMultiplier());
}
}
}
public void reset (){
lastReset = commands.getTotalExecutionTime();
lastReset = executionTime.getTotalExecutionTime();
rawtimer = 0;
realtimer = 0;
preScaler.reset();

View File

@ -3,17 +3,10 @@ package fabrik.simulator.pic16f84.interfaces;
public interface CommandInterface extends PICComponentInterface {
void CALL(int isr);
double getTotalExecutionTime();
int get_wRegister();
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;
public interface EEPROMInterface extends PICComponentInterface {
void registerTime(boolean b);
void registerTime(double executionTime, boolean b);
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);
picComponents.registerComponent(EEPROMInterface.class, eepromMock);
CommandInterface commands = new Commands();
Commands commands = new Commands();
picComponents.registerComponent(CommandInterface.class, commands);
picComponents.initAll();
@ -47,7 +47,7 @@ class CommandsTests {
EEPROMInterface eepromMock = mock(EEPROMInterface.class);
picComponents.registerComponent(EEPROMInterface.class, eepromMock);
CommandInterface commands = new Commands();
Commands commands = new Commands();
picComponents.registerComponent(CommandInterface.class, commands);
picComponents.initAll();
@ -68,7 +68,7 @@ class CommandsTests {
EEPROMInterface eepromMock = mock(EEPROMInterface.class);
picComponents.registerComponent(EEPROMInterface.class, eepromMock);
CommandInterface commands = new Commands();
Commands commands = new Commands();
picComponents.registerComponent(CommandInterface.class, commands);
picComponents.initAll();

View File

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

View File

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