Dependency inversions
This commit is contained in:
@ -1,39 +1,58 @@
|
||||
package fabrik.simulator.pic16f84;
|
||||
|
||||
import com.gluonhq.charm.glisten.control.ToggleButtonGroup;
|
||||
import fabrik.simulator.pic16f84.interfaces.*;
|
||||
import javafx.collections.ObservableList;
|
||||
import javafx.scene.control.ToggleButton;
|
||||
import javafx.scene.paint.Color;
|
||||
import javafx.scene.paint.RadialGradient;
|
||||
import javafx.scene.paint.Stop;
|
||||
import javafx.scene.shape.Circle;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class IOPorts {
|
||||
private static final int A = 0;
|
||||
private static final int B = 1;
|
||||
private static final int PORTA = 5;
|
||||
private static final int PORTB = 6;
|
||||
private static final int TRISA = 0x85;
|
||||
private static final int TRISB = 0x86;
|
||||
public class IOPorts implements IOPortInterface {
|
||||
private final int A = 0;
|
||||
private final int B = 1;
|
||||
private final int PORTA = 5;
|
||||
private final int PORTB = 6;
|
||||
private final int TRISA = 0x85;
|
||||
private final int TRISB = 0x86;
|
||||
|
||||
private static int [] trisLatch = {0xFF, 0xFF};
|
||||
private static int [] dataLatch = new int[2];
|
||||
private static boolean isLEDenabledA = false;
|
||||
private static boolean isLEDenabledB = false;
|
||||
private static Circle[] allLEDsA;
|
||||
private static Circle[] allLEDsB;
|
||||
private int [] trisLatch = {0xFF, 0xFF};
|
||||
private int [] dataLatch = new int[2];
|
||||
private boolean isLEDenabledA = false;
|
||||
private boolean isLEDenabledB = false;
|
||||
private Circle[] allLEDsA;
|
||||
private Circle[] allLEDsB;
|
||||
|
||||
|
||||
private static int RB4 = 0x10;
|
||||
private static int RB5 = 0x20;
|
||||
private static int RB6 = 0x40;
|
||||
private static int RB7 = 0x80;
|
||||
private int RB4 = 0x10;
|
||||
private int RB5 = 0x20;
|
||||
private int RB6 = 0x40;
|
||||
private int RB7 = 0x80;
|
||||
|
||||
|
||||
public static void setBit (int address, int bit){
|
||||
private final DataRegisterInterface dataRegister;
|
||||
private final TimerInterface timer;
|
||||
private final InterruptInterface interrupts;
|
||||
private final TableInterface table;
|
||||
private final WindowManagement createWindow;
|
||||
private final FrontendControllerInterface frontendController;
|
||||
|
||||
public IOPorts (DataRegisterInterface dataRegister, FrontendControllerInterface frontendController, TimerInterface timer,
|
||||
InterruptInterface interrupts, TableInterface table, WindowManagement createWindow){
|
||||
|
||||
this.dataRegister = dataRegister;
|
||||
this.timer = timer;
|
||||
this.frontendController = frontendController;
|
||||
this.interrupts = interrupts;
|
||||
this.createWindow = createWindow;
|
||||
this.table = table;
|
||||
}
|
||||
|
||||
|
||||
public void setBit (int address, int bit){
|
||||
if (address < 7) {
|
||||
dataLatch[address - PORTA] |= (1 << bit);
|
||||
}
|
||||
@ -43,7 +62,7 @@ public class IOPorts {
|
||||
refreshPorts();
|
||||
}
|
||||
|
||||
public static void clearBit(int address, int bit) {
|
||||
public void clearBit(int address, int bit) {
|
||||
if (address < 7) {
|
||||
if (((dataLatch[address-PORTA] >> bit)&1) == 1){
|
||||
dataLatch[address-PORTA] -= (int) Math.pow(2, bit);
|
||||
@ -57,7 +76,7 @@ public class IOPorts {
|
||||
refreshPorts();
|
||||
}
|
||||
|
||||
public static void setRegister(int address, int content) {
|
||||
public void setRegister(int address, int content) {
|
||||
if (address < 7) {
|
||||
dataLatch[address - PORTA] = content;
|
||||
}
|
||||
@ -67,24 +86,24 @@ public class IOPorts {
|
||||
refreshPorts();
|
||||
}
|
||||
|
||||
private static void refreshPorts() {
|
||||
DataRegister.setDirectRegister(PORTA, ((~((~dataLatch[A])&0x1F | trisLatch[A])) | (trisLatch[A] & DataRegister.getDirectRegister(PORTA))) & 0xFF);
|
||||
DataRegister.setDirectRegister(PORTB, ((~((~dataLatch[B])&0xFF | trisLatch[B])) | (trisLatch[B] & DataRegister.getDirectRegister(PORTB))) & 0xFF);
|
||||
DataRegister.setDirectRegister(TRISA, trisLatch[A]);
|
||||
DataRegister.setDirectRegister(TRISB, trisLatch[B]);
|
||||
private void refreshPorts() {
|
||||
dataRegister.setDirectRegister(PORTA, ((~((~dataLatch[A])&0x1F | trisLatch[A])) | (trisLatch[A] & dataRegister.getDirectRegister(PORTA))) & 0xFF);
|
||||
dataRegister.setDirectRegister(PORTB, ((~((~dataLatch[B])&0xFF | trisLatch[B])) | (trisLatch[B] & dataRegister.getDirectRegister(PORTB))) & 0xFF);
|
||||
dataRegister.setDirectRegister(TRISA, trisLatch[A]);
|
||||
dataRegister.setDirectRegister(TRISB, trisLatch[B]);
|
||||
if (((trisLatch[A] >> 4) & 1 )== 1)
|
||||
Timer.incrementFromPin(DataRegister.getDirectRegister(PORTA));
|
||||
timer.incrementFromPin(dataRegister.getDirectRegister(PORTA));
|
||||
ToggleButtonGroup[] buttons = Controller_Frontend.getPORTbuttons();
|
||||
for (int i = 0; i < buttons.length; i++){
|
||||
int port = (i < 8) ? PORTA : PORTB;
|
||||
int bit = i % 8;
|
||||
boolean value = ((DataRegister.getDirectRegister(port) >> bit) & 1) == 1;
|
||||
boolean value = ((dataRegister.getDirectRegister(port) >> bit) & 1) == 1;
|
||||
buttons[i].getToggles().get(0).setSelected(!value);
|
||||
buttons[i].getToggles().get(1).setSelected(value);
|
||||
}
|
||||
}
|
||||
|
||||
public static void refreshUI(ToggleButtonGroup[] buttonsTRIS, ToggleButtonGroup[] buttonsPORT) {
|
||||
public void refreshUI(ToggleButtonGroup[] buttonsTRIS, ToggleButtonGroup[] buttonsPORT) {
|
||||
for (int i = 0; i< buttonsTRIS.length; i++){
|
||||
int tris = (i < 8) ? trisLatch[A] : trisLatch[B];
|
||||
boolean val = isInput(tris, i%8);
|
||||
@ -95,18 +114,18 @@ public class IOPorts {
|
||||
}
|
||||
}
|
||||
|
||||
private static void updateLEDs(boolean updateAll, int index, boolean val) {
|
||||
private void updateLEDs(boolean updateAll, int index, boolean val) {
|
||||
if (updateAll) {
|
||||
if (index < 8) {
|
||||
if (isLEDenabledA && !val) {
|
||||
if (DataRegister.getDirectBit(PORTA, index) == 1)
|
||||
if (dataRegister.getDirectBit(PORTA, index) == 1)
|
||||
allLEDsA[index].setFill(new RadialGradient(0, 0, 0.5, 0.5, 0.7, true, null, new Stop(0, Color.BLACK), new Stop(1, Color.DARKGRAY)));
|
||||
else
|
||||
allLEDsA[index].setFill(new RadialGradient(0, 0, 0.5, 0.5, 0.7, true, null, new Stop(0, Color.BLACK), new Stop(1, Color.DARKGRAY)));
|
||||
}
|
||||
} else {
|
||||
if (isLEDenabledB && !val) {
|
||||
if (DataRegister.getDirectBit(PORTB, index - 8) == 1)
|
||||
if (dataRegister.getDirectBit(PORTB, index - 8) == 1)
|
||||
allLEDsB[index - 8].setFill(new RadialGradient(0, 0, 0.5, 0.5, 0.7, true, null, new Stop(0, Color.RED), new Stop(1, Color.DARKGRAY)));
|
||||
else
|
||||
allLEDsB[index - 8].setFill(new RadialGradient(0, 0, 0.5, 0.5, 0.7, true, null, new Stop(0, Color.BLACK), new Stop(1, Color.DARKGRAY)));
|
||||
@ -115,23 +134,23 @@ public class IOPorts {
|
||||
}
|
||||
else {
|
||||
if (index < 8)
|
||||
if (val || DataRegister.getDirectBit(PORTA, index) == 0)
|
||||
if (val || dataRegister.getDirectBit(PORTA, index) == 0)
|
||||
allLEDsA[index].setFill(new RadialGradient(0, 0, 0.5, 0.5, 0.7, true, null, new Stop(0, Color.BLACK), new Stop(1, Color.DARKGRAY)));
|
||||
else
|
||||
allLEDsA[index].setFill(new RadialGradient(0, 0, 0.5, 0.5, 0.7, true, null, new Stop(0, Color.RED), new Stop(1, Color.DARKGRAY)));
|
||||
else
|
||||
if (val || DataRegister.getDirectBit(PORTB, index - 8) == 0)
|
||||
if (val || dataRegister.getDirectBit(PORTB, index - 8) == 0)
|
||||
allLEDsB[index - 8].setFill(new RadialGradient(0, 0, 0.5, 0.5, 0.7, true, null, new Stop(0, Color.BLACK), new Stop(1, Color.DARKGRAY)));
|
||||
else
|
||||
allLEDsB[index - 8].setFill(new RadialGradient(0, 0, 0.5, 0.5, 0.7, true, null, new Stop(0, Color.RED), new Stop(1, Color.DARKGRAY)));
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isInput(int i, int bit) {
|
||||
private boolean isInput(int i, int bit) {
|
||||
return (i >> bit & 1) == 1;
|
||||
}
|
||||
|
||||
public static void setTRISfromUI(ToggleButtonGroup parent) throws IOException {
|
||||
public void setTRISfromUI(ToggleButtonGroup parent) throws IOException {
|
||||
int [] params = getToggleParams(parent);
|
||||
int tris = params[0];
|
||||
int bit = params[1];
|
||||
@ -150,23 +169,23 @@ public class IOPorts {
|
||||
refreshTable(parent);
|
||||
}
|
||||
|
||||
public static void setPORTfromUI(ToggleButtonGroup parent) throws IOException {
|
||||
public void setPORTfromUI(ToggleButtonGroup parent) throws IOException {
|
||||
int [] params = getToggleParams(parent);
|
||||
int port = params[0];
|
||||
int bit = params[1];
|
||||
int value = params [2];
|
||||
value = (value == 1) ? 0 : 1;
|
||||
int oldValue = DataRegister.getDirectBit(port, bit);
|
||||
DataRegister.setDirectBit(port, bit, value);
|
||||
int oldValue = dataRegister.getDirectBit(port, bit);
|
||||
dataRegister.setDirectBit(port, bit, value);
|
||||
refreshPorts();
|
||||
refreshTable(parent);
|
||||
if (port == PORTB && bit >= 4)
|
||||
Interrupts.triggerRBInterrupt(oldValue, value);
|
||||
interrupts.triggerRBInterrupt(oldValue, value);
|
||||
else if (port == PORTB && bit == 0)
|
||||
Interrupts.triggerRB0Interrupt(oldValue, value);
|
||||
interrupts.triggerRB0Interrupt(oldValue, value);
|
||||
}
|
||||
|
||||
public static void setLEDs (boolean[] leds) {
|
||||
public void setLEDs (boolean[] leds) {
|
||||
boolean isAnowDisabled = isLEDenabledA && !leds[0];
|
||||
isLEDenabledA = leds[0];
|
||||
boolean isBnowDisabled = isLEDenabledB && !leds[1];
|
||||
@ -183,17 +202,17 @@ public class IOPorts {
|
||||
}
|
||||
}
|
||||
|
||||
public static void setLEDs (Circle[] a, Circle[] b){
|
||||
public void setLEDs (Circle[] a, Circle[] b){
|
||||
allLEDsA = a;
|
||||
allLEDsB = b;
|
||||
}
|
||||
|
||||
public static void refreshTable(ToggleButtonGroup parent) {
|
||||
Table.refresh();
|
||||
CreateWindow.refreshTable();
|
||||
public void refreshTable(ToggleButtonGroup parent) {
|
||||
table.refresh();
|
||||
createWindow.refreshTable();
|
||||
}
|
||||
|
||||
private static int[] getToggleParams(ToggleButtonGroup parent) {
|
||||
private int[] getToggleParams(ToggleButtonGroup parent) {
|
||||
String group = parent.getId();
|
||||
ObservableList<ToggleButton> toggles = parent.getToggles();
|
||||
int fileAddress;
|
||||
@ -213,13 +232,13 @@ public class IOPorts {
|
||||
return new int[]{fileAddress, bit, value};
|
||||
}
|
||||
|
||||
public static void reset() {
|
||||
public void reset() {
|
||||
trisLatch = new int[]{0xFF, 0xFF};
|
||||
dataLatch = new int[2];
|
||||
refreshPorts();
|
||||
}
|
||||
|
||||
public static void resetTRIS (){
|
||||
public void resetTRIS (){
|
||||
trisLatch = new int[] {0xFF, 0xFF};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user