TRIS/DATA-Latch

This commit is contained in:
2024-04-29 16:59:15 +02:00
parent b4f49dbb97
commit 3c406378b2
2 changed files with 94 additions and 1 deletions

View File

@ -7,6 +7,8 @@ public class DataRegister {
private static final int PCL = 0x2;
private static final int STATUS = 0x3;
private static final int FSR = 0x4;
private static final int PORTA = 0x5;
private static final int PORTB = 0x6;
private static final int PCLATH = 0xA;
private static final int INTCON = 0xB;
@ -21,6 +23,7 @@ public class DataRegister {
private static final int [] dataRegister = new int[0xFF];
private static final int [] syncedRegisters = {INDF, PCL, STATUS, FSR, PCLATH, INTCON};
public static final int [] ioRegisters = {PORTA, PORTB, TRISA, TRISB};
public static void initDataRegister() {
dataRegister[PCL] = 0b0;
@ -74,6 +77,10 @@ public class DataRegister {
public static void setRegister(int fileAddress, int content){
int address = determineIndirectAndChange (fileAddress);
if (Arrays.stream(ioRegisters).anyMatch(i -> i == address)){
IOPorts.setRegister(address, content);
return;
}
if (fileAddress == PCL || fileAddress == 0x80 + PCL){
programCounter = content + (dataRegister[PCLATH] << 8);
}
@ -90,12 +97,16 @@ public class DataRegister {
return (dataRegister[bank() + address] >> bit) & 1;
}
private static int getDirectBit(int address, int bit){
public static int getDirectBit(int address, int bit){
return (dataRegister[address] >> bit) & 1;
}
public static void clearBit(int fileAddress, int bit) {
int address = determineIndirectAndChange (fileAddress);
if (Arrays.stream(ioRegisters).anyMatch(i -> i == address)){
IOPorts.clearBit(address, bit);
return;
}
if (!isSyncedRegister(address)) {
if (getBit(address, bit) == 1) {
dataRegister[bank() + address] -= (int) Math.pow(2, bit);
@ -111,6 +122,10 @@ public class DataRegister {
public static void setBit(int fileAddress, int bit) {
int address = determineIndirectAndChange (fileAddress);
if (Arrays.stream(ioRegisters).anyMatch(i -> i == address)){
IOPorts.setBit(address, bit);
return;
}
if (!isSyncedRegister(address)) {
if (getBit(address, bit) == 0) {
dataRegister[bank() + address] += (int) Math.pow(2, bit);
@ -231,4 +246,28 @@ public class DataRegister {
return digitCarryFlag;
}
public static void setDirectRegister(int fileAddress, int content) {
dataRegister[fileAddress] = content;
}
public static int getPCL() {
return PCL;
}
public static int getSTATUS() {
return STATUS;
}
public static int getPCLATH() {
return PCLATH;
}
public static int getINTCON() {
return INTCON;
}
public static int getDirectRegister(int address) {
return dataRegister[address];
}
}

View File

@ -0,0 +1,54 @@
package fabrik.simulator.pic16f84;
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;
private static int [] trisLatch = new int[2];
private static int [] dataLatch = new int[2];
public static void setBit (int address, int bit){
if (address < 7) {
dataLatch[address - 5] |= (1 << bit);
}
else{
trisLatch[address - 0x85] |= (1 << bit);
}
refreshPorts();
}
public static void clearBit(int address, int bit) {
if (address < 7) {
if (DataRegister.getDirectBit(address, bit) == 1) {
int register = DataRegister.getDirectRegister(address);
dataLatch[address-A] = register - (int) Math.pow(2, bit);
}
}
else{
if (DataRegister.getDirectBit(address, bit) == 1) {
int register = DataRegister.getDirectRegister(address);
trisLatch[address-A] = register - (int) Math.pow(2, bit);
}
}
refreshPorts();
}
public static void setRegister(int address, int content) {
if (address < 7) {
dataLatch[address - 5] = content;
}
else{
trisLatch[address - 0x85] = content;
}
refreshPorts();
}
private static void refreshPorts() {
DataRegister.setDirectRegister(PORTA, (~((~dataLatch[A])&0xFF | trisLatch[A])) & 0x1F);
DataRegister.setDirectRegister(PORTB, (~((~dataLatch[B])&0xFF | trisLatch[B])) & 0xFF);
}
}