Dependency inversions
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
package fabrik.simulator.pic16f84;
|
||||
|
||||
|
||||
import eu.hansolo.tilesfx.Command;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
import org.json.simple.parser.ParseException;
|
||||
@ -11,26 +12,34 @@ import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import static fabrik.simulator.pic16f84.Commands.getTotalExecutionTime;
|
||||
import fabrik.simulator.pic16f84.interfaces.*;
|
||||
|
||||
public class EEPROM {
|
||||
private static final int EEDATA = 0x08;
|
||||
private static final int EEADR = 0x09;
|
||||
private static final int EECON1 = 0x88;
|
||||
private static final int EECON2 = 0x89;
|
||||
public class EEPROM implements EEPROMInterface {
|
||||
private final int EEDATA = 0x08;
|
||||
private final int EEADR = 0x09;
|
||||
private final int EECON1 = 0x88;
|
||||
private final int EECON2 = 0x89;
|
||||
|
||||
private static final int RD = 0x00;
|
||||
private static final int WR = 0x01;
|
||||
private static final int WRERR = 0x03;
|
||||
private static final int EEIF = 0x04;
|
||||
private final int RD = 0x00;
|
||||
private final int WR = 0x01;
|
||||
private final int WRERR = 0x03;
|
||||
private final int EEIF = 0x04;
|
||||
|
||||
private static boolean readControl = false;
|
||||
private static boolean writeControl = false;
|
||||
private static boolean [] eecon2stages = {false, false};
|
||||
private boolean readControl = false;
|
||||
private boolean writeControl = false;
|
||||
private boolean [] eecon2stages = {false, false};
|
||||
|
||||
private final DataRegisterInterface dataRegister;
|
||||
private final CommandInterface commands;
|
||||
|
||||
private static long startTime;
|
||||
private double startTime;
|
||||
|
||||
public EEPROM (DataRegisterInterface dataRegister, CommandInterface commands){
|
||||
this.dataRegister = dataRegister;
|
||||
this.commands = commands;
|
||||
}
|
||||
|
||||
public static long read (int address) {
|
||||
public long read (int address) {
|
||||
FileReader reader;
|
||||
try {
|
||||
reader = new FileReader("eeprom.json");
|
||||
@ -47,19 +56,19 @@ public class EEPROM {
|
||||
Object requestedData = data.get(String.valueOf(address));
|
||||
try {
|
||||
readControl = false;
|
||||
DataRegister.setDirectBit(EECON1, RD, 0);
|
||||
dataRegister.setDirectBit(EECON1, RD, 0);
|
||||
return (long) requestedData;
|
||||
} catch (NullPointerException e) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public static void write (int address, long data) {
|
||||
public void write (int address, long data) {
|
||||
FileReader reader;
|
||||
try {
|
||||
reader = new FileReader("eeprom.json");
|
||||
} catch (FileNotFoundException ignored) {
|
||||
DataRegister.setDirectBit(EECON1, WRERR, 1);
|
||||
dataRegister.setDirectBit(EECON1, WRERR, 1);
|
||||
return;
|
||||
}
|
||||
JSONParser parser = new JSONParser();
|
||||
@ -67,32 +76,32 @@ public class EEPROM {
|
||||
try {
|
||||
eeprom = (JSONObject) parser.parse(reader);
|
||||
} catch (ParseException | IOException ignored) {
|
||||
DataRegister.setDirectBit(EECON1, WRERR, 1);
|
||||
dataRegister.setDirectBit(EECON1, WRERR, 1);
|
||||
return;
|
||||
}
|
||||
eeprom.put(String.valueOf(address), data);
|
||||
try {
|
||||
Files.write(Paths.get("eeprom.json"), eeprom.toJSONString().getBytes());
|
||||
} catch (IOException ignored) {
|
||||
DataRegister.setDirectBit(EECON1, WRERR, 1);
|
||||
dataRegister.setDirectBit(EECON1, WRERR, 1);
|
||||
return;
|
||||
}
|
||||
registerTime(true);
|
||||
}
|
||||
|
||||
public static void registerTime(boolean reset) {
|
||||
public void registerTime(boolean reset) {
|
||||
if (reset)
|
||||
startTime = getTotalExecutionTime();
|
||||
else if ((getTotalExecutionTime() >= (startTime + 1000)) && writeControl) {
|
||||
startTime = commands.getTotalExecutionTime();
|
||||
else if ((commands.getTotalExecutionTime() >= (startTime + 1000)) && writeControl) {
|
||||
eecon2stages = new boolean[]{false, false};
|
||||
DataRegister.setDirectBit(EECON1, EEIF, 1);
|
||||
DataRegister.setDirectBit(EECON1, WR, 0);
|
||||
dataRegister.setDirectBit(EECON1, EEIF, 1);
|
||||
dataRegister.setDirectBit(EECON1, WR, 0);
|
||||
writeControl = false;
|
||||
startTime = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public static void parse(int address, int content, int opcode) {
|
||||
public void parse(int address, int content, int opcode) {
|
||||
//OPCODE: 0b1x = set
|
||||
//OPCODE: 0b0x = clear
|
||||
//OPCODE: 0bx1 = byte
|
||||
@ -100,21 +109,21 @@ public class EEPROM {
|
||||
switch (opcode){
|
||||
case 0b00: // CLEAR BIT
|
||||
if(address == EECON1) {
|
||||
if (DataRegister.getDirectBit(address, content) == 1)
|
||||
setEECON1((int) (DataRegister.getDirectRegister(EECON1) - Math.pow(2, content)));
|
||||
if (dataRegister.getDirectBit(address, content) == 1)
|
||||
setEECON1((int) (dataRegister.getDirectRegister(EECON1) - Math.pow(2, content)));
|
||||
} else {
|
||||
DataRegister.setDirectBit(address, content, 0);
|
||||
dataRegister.setDirectBit(address, content, 0);
|
||||
}
|
||||
break;
|
||||
case 0b10: // SET BIT
|
||||
if(address == EECON1) {
|
||||
if (DataRegister.getDirectBit(address, content) == 0)
|
||||
setEECON1((int) (DataRegister.getDirectRegister(EECON1) + Math.pow(2, content)));
|
||||
if (dataRegister.getDirectBit(address, content) == 0)
|
||||
setEECON1((int) (dataRegister.getDirectRegister(EECON1) + Math.pow(2, content)));
|
||||
} else {
|
||||
if (address == EEADR && content >= 0b01000000)
|
||||
DataRegister.setDirectBit(address, content, 0);
|
||||
dataRegister.setDirectBit(address, content, 0);
|
||||
else
|
||||
DataRegister.setDirectBit(address, content, 1);
|
||||
dataRegister.setDirectBit(address, content, 1);
|
||||
}
|
||||
break;
|
||||
case 0b11: // SET BYTE
|
||||
@ -131,20 +140,20 @@ public class EEPROM {
|
||||
else
|
||||
eecon2stages = new boolean[]{false, false};
|
||||
}
|
||||
DataRegister.setDirectRegister(address, content);
|
||||
dataRegister.setDirectRegister(address, content);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private static void setEECON1(int content) {
|
||||
private void setEECON1(int content) {
|
||||
content &= 0b00011111;
|
||||
boolean writeEnabled = ((content & 0b100) >> 2) == 1;
|
||||
if ((content & 0b1) == 1) { // READ
|
||||
readControl = true;
|
||||
DataRegister.setDirectRegister(EECON1, content);
|
||||
int data = (int) read(DataRegister.getDirectRegister(EEADR));
|
||||
DataRegister.setDirectRegister(EEDATA, data);
|
||||
dataRegister.setDirectRegister(EECON1, content);
|
||||
int data = (int) read(dataRegister.getDirectRegister(EEADR));
|
||||
dataRegister.setDirectRegister(EEDATA, data);
|
||||
return;
|
||||
}
|
||||
else if ((content & 0b1) == 0 && readControl) // RD kann nicht manuell gecleart werden
|
||||
@ -152,8 +161,8 @@ public class EEPROM {
|
||||
if (((content & 0b10) >> 1) == 1) { // WRITE CONTROL
|
||||
if (writeEnabled && eecon2stages[0] && eecon2stages[1]) {
|
||||
writeControl = true;
|
||||
DataRegister.setDirectRegister(EECON1, content);
|
||||
write(DataRegister.getDirectRegister(EEADR), DataRegister.getDirectRegister(EEDATA));
|
||||
dataRegister.setDirectRegister(EECON1, content);
|
||||
write(dataRegister.getDirectRegister(EEADR), dataRegister.getDirectRegister(EEDATA));
|
||||
return;
|
||||
}
|
||||
else {
|
||||
@ -162,6 +171,6 @@ public class EEPROM {
|
||||
}
|
||||
else if ((((content & 0b10) >> 1) == 0) && writeControl) // WR kann nicht manuell gecleart werden
|
||||
content |= 0b10;
|
||||
DataRegister.setDirectRegister(EECON1, content);
|
||||
dataRegister.setDirectRegister(EECON1, content);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user