Compare commits

..

1 Commits

Author SHA1 Message Date
Darkress
f0f6179059 implemented Retlw instruction 2023-05-31 01:43:48 +02:00
36 changed files with 74 additions and 667 deletions

View File

@@ -12,10 +12,12 @@ class Main
public static void main(String[] args) {
ArrayList<Command> program = new ArrayList<>();
Memory.workingRegister = 0x01;
Memory.setRegister(0x14, 0xA5); //240 << 224
program.add(CommandDecoder.decode(0x1114));
for(int i = 0; i < program.size(); i++)
int input1 = 0x27FF;
program.add(CommandDecoder.decode(0x3011));
program.add(CommandDecoder.decode(0x2003));
program.add(CommandDecoder.decode(0x3022));
program.add(CommandDecoder.decode(0x0008));
while(true)
{
program.get(ProgramCounter.getPc()).execute();
}

View File

@@ -3,7 +3,7 @@ package de.darkress.pic16f84sim.commands;
import de.darkress.pic16f84sim.microcontroller.Memory;
import de.darkress.pic16f84sim.microcontroller.ProgramCounter;
public class Addlw extends LiteralCommandUtils implements Command
public class Addlw extends CommandUtils implements Command
{
private final int literal;

View File

@@ -1,29 +0,0 @@
package de.darkress.pic16f84sim.commands;
import de.darkress.pic16f84sim.microcontroller.Memory;
import de.darkress.pic16f84sim.microcontroller.ProgramCounter;
public class Addwf extends FileRegisterCommandUtils implements Command
{
private final int address;
private final boolean destinationBit;
public Addwf(int input)
{
address = input & 0x007F;
destinationBit = checkDestinationBit(input);
}
@Override
public void execute()
{
int result = Memory.getRegister(address) + Memory.workingRegister;
checkZeroBit(result);
checkCarryBit(result);
checkDigitCarryBit(address);
writeToDestination(destinationBit, address, result);
ProgramCounter.incPC();
}
}

View File

@@ -2,7 +2,7 @@ package de.darkress.pic16f84sim.commands;
import de.darkress.pic16f84sim.microcontroller.Memory;
public class Andlw extends LiteralCommandUtils implements Command
public class Andlw extends CommandUtils implements Command
{
private final int literal;

View File

@@ -1,27 +0,0 @@
package de.darkress.pic16f84sim.commands;
import de.darkress.pic16f84sim.microcontroller.Memory;
import de.darkress.pic16f84sim.microcontroller.ProgramCounter;
public class Andwf extends FileRegisterCommandUtils implements Command
{
private final int address;
private final boolean destinationBit;
public Andwf(int input)
{
address = input & 0x007F;
destinationBit = checkDestinationBit(input);
}
@Override
public void execute()
{
int result = Memory.getRegister(address) & Memory.workingRegister;
checkZeroBit(result);
writeToDestination(destinationBit, address, result);
ProgramCounter.incPC();
}
}

View File

@@ -1,26 +0,0 @@
package de.darkress.pic16f84sim.commands;
import de.darkress.pic16f84sim.microcontroller.Memory;
import de.darkress.pic16f84sim.microcontroller.ProgramCounter;
public class Bcf extends BitOrientedCommandUtils implements Command
{
private final int address;
private int bitPlacement;
public Bcf(int input)
{
address = input & 0x007F;
bitPlacement = checkBitPlacement(input);
}
@Override
public void execute()
{
int result = Memory.getRegister(address);
result &= ~(1 << bitPlacement); //Mask n-th bit with 0
Memory.setRegister(address, result);
ProgramCounter.incPC();
}
}

View File

@@ -1,11 +0,0 @@
package de.darkress.pic16f84sim.commands;
import de.darkress.pic16f84sim.microcontroller.Memory;
public class BitOrientedCommandUtils
{
protected int checkBitPlacement(int input)
{
return ((input & 0x0380) >>7);
}
}

View File

@@ -3,7 +3,7 @@ package de.darkress.pic16f84sim.commands;
import de.darkress.pic16f84sim.microcontroller.ProgramCounter;
import de.darkress.pic16f84sim.microcontroller.Stack;
public class Call extends LiteralCommandUtils implements Command
public class Call extends CommandUtils implements Command
{
private final int literal;

View File

@@ -1,27 +0,0 @@
package de.darkress.pic16f84sim.commands;
import de.darkress.pic16f84sim.microcontroller.Memory;
import de.darkress.pic16f84sim.microcontroller.ProgramCounter;
public class Clrf extends FileRegisterCommandUtils implements Command
{
private final int address;
private final boolean destinationBit;
public Clrf(int input)
{
address = input & 0x007F;
destinationBit = true;
}
@Override
public void execute()
{
final int result = 0;
checkZeroBit(result);
writeToDestination(destinationBit, address, result);
ProgramCounter.incPC();
}
}

View File

@@ -1,18 +0,0 @@
package de.darkress.pic16f84sim.commands;
import de.darkress.pic16f84sim.microcontroller.Memory;
import de.darkress.pic16f84sim.microcontroller.ProgramCounter;
public class Clrw extends FileRegisterCommandUtils implements Command
{
@Override
public void execute()
{
final int result = 0;
checkZeroBit(result);
Memory.workingRegister = result;
ProgramCounter.incPC();
}
}

View File

@@ -1,27 +0,0 @@
package de.darkress.pic16f84sim.commands;
import de.darkress.pic16f84sim.microcontroller.Memory;
import de.darkress.pic16f84sim.microcontroller.ProgramCounter;
public class Comf extends FileRegisterCommandUtils implements Command
{
private final int address;
private final boolean destinationBit;
public Comf(int input)
{
address = input & 0x007F;
destinationBit = checkDestinationBit(input);
}
@Override
public void execute()
{
int result = 255 - Memory.getRegister(address); // Get inverse of 8Bit value
checkZeroBit(result);
writeToDestination(destinationBit, address, result);
ProgramCounter.incPC();
}
}

View File

@@ -2,7 +2,7 @@ package de.darkress.pic16f84sim.commands;
import de.darkress.pic16f84sim.microcontroller.Memory;
public class LiteralCommandUtils
public class CommandUtils
{
protected void checkZeroBit(int result)
{

View File

@@ -1,27 +0,0 @@
package de.darkress.pic16f84sim.commands;
import de.darkress.pic16f84sim.microcontroller.Memory;
import de.darkress.pic16f84sim.microcontroller.ProgramCounter;
public class Decf extends FileRegisterCommandUtils implements Command
{
private final int address;
private final boolean destinationBit;
public Decf(int input)
{
address = input & 0x007F;
destinationBit = checkDestinationBit(input);
}
@Override
public void execute()
{
int result = Memory.getRegister(address) + 255; // Allow underflow
checkZeroBit(result);
writeToDestination(destinationBit, address, result % 256); // Catch underflow
ProgramCounter.incPC();
}
}

View File

@@ -1,33 +0,0 @@
package de.darkress.pic16f84sim.commands;
import de.darkress.pic16f84sim.microcontroller.Memory;
import de.darkress.pic16f84sim.microcontroller.ProgramCounter;
public class Decfsz extends FileRegisterCommandUtils implements Command
{
private final int address;
private final boolean destinationBit;
public Decfsz(int input)
{
address = input & 0x007F;
destinationBit = checkDestinationBit(input);
}
@Override
public void execute()
{
int result = Memory.getRegister(address) + 255; // Allow underflow
checkZeroBit(result);
writeToDestination(destinationBit, address, result % 256); // Catch underflow
if((result % 256) == 0)
{
Nop nop = new Nop();
nop.execute(); // TODO: What happens if an interrupt gets triggered during the nop execution?
}
ProgramCounter.incPC();
}
}

View File

@@ -1,51 +0,0 @@
package de.darkress.pic16f84sim.commands;
import de.darkress.pic16f84sim.microcontroller.Memory;
public class FileRegisterCommandUtils
{
protected void checkZeroBit(int result)
{
if((result % 256) == 0){
Memory.setZeroBit();
} else{
Memory.clearZeroBit();
}
}
protected void checkCarryBit(int result)
{
if(result > 255){
Memory.setCarryBit();
} else{
Memory.clearCarryBit();
}
}
protected void checkDigitCarryBit(int address)
{
int literal = Memory.getRegister(address);
if(((Memory.workingRegister & 0x0F) + (literal & 0x0F)) > 15){
Memory.setDigitCarryBit();
} else{
Memory.clearDigitCarryBit();
}
}
protected void writeToDestination(boolean destinationBit, int fileAddress, int result)
{
if(destinationBit)
{
//Store in FileRegister
Memory.setRegister(fileAddress, result);
} else {
//Store in WRegister
Memory.workingRegister = result;
}
}
protected boolean checkDestinationBit(int input)
{
return (input & 0x0080) == 0x0080;
}
}

View File

@@ -2,7 +2,7 @@ package de.darkress.pic16f84sim.commands;
import de.darkress.pic16f84sim.microcontroller.ProgramCounter;
public class Goto extends LiteralCommandUtils implements Command
public class Goto extends CommandUtils implements Command
{
private final int literal;

View File

@@ -1,27 +0,0 @@
package de.darkress.pic16f84sim.commands;
import de.darkress.pic16f84sim.microcontroller.Memory;
import de.darkress.pic16f84sim.microcontroller.ProgramCounter;
public class Incf extends FileRegisterCommandUtils implements Command
{
private final int address;
private final boolean destinationBit;
public Incf(int input)
{
address = input & 0x007F;
destinationBit = checkDestinationBit(input);
}
@Override
public void execute()
{
int result = Memory.getRegister(address) + 1; // Allow underflow
checkZeroBit(result);
writeToDestination(destinationBit, address, result % 256); // Catch underflow
ProgramCounter.incPC();
}
}

View File

@@ -1,33 +0,0 @@
package de.darkress.pic16f84sim.commands;
import de.darkress.pic16f84sim.microcontroller.Memory;
import de.darkress.pic16f84sim.microcontroller.ProgramCounter;
public class Incfsz extends FileRegisterCommandUtils implements Command
{
private final int address;
private final boolean destinationBit;
public Incfsz(int input)
{
address = input & 0x007F;
destinationBit = checkDestinationBit(input);
}
@Override
public void execute()
{
int result = Memory.getRegister(address) + 1;
checkZeroBit(result);
writeToDestination(destinationBit, address, result % 256);
if((result % 256) == 0)
{
Nop nop = new Nop();
nop.execute(); // TODO: What happens if an interrupt gets triggered during the nop execution?
}
ProgramCounter.incPC();
}
}

View File

@@ -2,7 +2,7 @@ package de.darkress.pic16f84sim.commands;
import de.darkress.pic16f84sim.microcontroller.Memory;
public class Iorlw extends LiteralCommandUtils implements Command
public class Iorlw extends CommandUtils implements Command
{
private final int literal;

View File

@@ -1,27 +0,0 @@
package de.darkress.pic16f84sim.commands;
import de.darkress.pic16f84sim.microcontroller.Memory;
import de.darkress.pic16f84sim.microcontroller.ProgramCounter;
public class Iorwf extends FileRegisterCommandUtils implements Command
{
private final int address;
private final boolean destinationBit;
public Iorwf(int input)
{
address = input & 0x007F;
destinationBit = checkDestinationBit(input);
}
@Override
public void execute()
{
int result = Memory.getRegister(address) | Memory.workingRegister;
checkZeroBit(result);
writeToDestination(destinationBit, address, result);
ProgramCounter.incPC();
}
}

View File

@@ -1,27 +0,0 @@
package de.darkress.pic16f84sim.commands;
import de.darkress.pic16f84sim.microcontroller.Memory;
import de.darkress.pic16f84sim.microcontroller.ProgramCounter;
public class Movf extends FileRegisterCommandUtils implements Command
{
private final int address;
private final boolean destinationBit;
public Movf(int input)
{
address = input & 0x007F;
destinationBit = checkDestinationBit(input);
}
@Override
public void execute()
{
int result = Memory.getRegister(address);
checkZeroBit(result);
writeToDestination(destinationBit, address, result);
ProgramCounter.incPC();
}
}

View File

@@ -1,9 +1,8 @@
package de.darkress.pic16f84sim.commands;
import de.darkress.pic16f84sim.microcontroller.Memory;
import de.darkress.pic16f84sim.microcontroller.ProgramCounter;
public class Movlw extends LiteralCommandUtils implements Command
public class Movlw extends CommandUtils implements Command
{
private final int literal;
@@ -16,6 +15,5 @@ public class Movlw extends LiteralCommandUtils implements Command
public void execute()
{
Memory.workingRegister = literal;
ProgramCounter.incPC();
}
}

View File

@@ -1,25 +0,0 @@
package de.darkress.pic16f84sim.commands;
import de.darkress.pic16f84sim.microcontroller.Memory;
import de.darkress.pic16f84sim.microcontroller.ProgramCounter;
public class Movwf extends FileRegisterCommandUtils implements Command
{
private final int address;
private final boolean destinationBit;
public Movwf(int input)
{
address = input & 0x007F;
destinationBit = true;
}
@Override
public void execute()
{
int result = Memory.workingRegister;
writeToDestination(destinationBit, address, result);
ProgramCounter.incPC();
}
}

View File

@@ -1,14 +0,0 @@
package de.darkress.pic16f84sim.commands;
import de.darkress.pic16f84sim.microcontroller.ProgramCounter;
public class Nop extends LiteralCommandUtils implements Command
{
@Override
public void execute()
{
// Do nothing, just increment the PC
ProgramCounter.incPC();
}
}

View File

@@ -4,7 +4,7 @@ import de.darkress.pic16f84sim.microcontroller.Memory;
import de.darkress.pic16f84sim.microcontroller.ProgramCounter;
import de.darkress.pic16f84sim.microcontroller.Stack;
public class Retlw extends LiteralCommandUtils implements Command
public class Retlw extends CommandUtils implements Command
{
private final int literal;
@@ -17,6 +17,8 @@ public class Retlw extends LiteralCommandUtils implements Command
public void execute()
{
Memory.workingRegister = literal;
ProgramCounter.setPcFromStack(Stack.pop());
ProgramCounter.setPcFrom11BitLiteral(Stack.pop());
//TODO: This may need some rework since it does not really load all 13 Bit into the PC. Only 8 Bit are
// effectively set
}
}

View File

@@ -3,11 +3,13 @@ package de.darkress.pic16f84sim.commands;
import de.darkress.pic16f84sim.microcontroller.ProgramCounter;
import de.darkress.pic16f84sim.microcontroller.Stack;
public class Return extends LiteralCommandUtils implements Command
public class Return extends CommandUtils implements Command
{
@Override
public void execute()
{
ProgramCounter.setPcFromStack(Stack.pop());
ProgramCounter.setPcFrom11BitLiteral(Stack.pop());
//TODO: This may need some rework since it does not really load all 13 Bit into the PC. Only 8 Bit are
// effectively set
}
}

View File

@@ -1,35 +0,0 @@
package de.darkress.pic16f84sim.commands;
import de.darkress.pic16f84sim.microcontroller.Memory;
import de.darkress.pic16f84sim.microcontroller.ProgramCounter;
public class Rlf extends FileRegisterCommandUtils implements Command
{
private final int address;
private final boolean destinationBit;
public Rlf(int input)
{
address = input & 0x007F;
destinationBit = checkDestinationBit(input);
}
@Override
public void execute()
{
int register = Memory.getRegister(address);
int newCarry = register >>7;
int oldCarry = Memory.getCarryBit();
register = ((register <<1) + oldCarry) & 0xFF;
if(newCarry == 1)
{
Memory.setCarryBit();
} else if(newCarry == 0){
Memory.clearCarryBit();
}
writeToDestination(destinationBit, address, register);
ProgramCounter.incPC();
}
}

View File

@@ -1,35 +0,0 @@
package de.darkress.pic16f84sim.commands;
import de.darkress.pic16f84sim.microcontroller.Memory;
import de.darkress.pic16f84sim.microcontroller.ProgramCounter;
public class Rrf extends FileRegisterCommandUtils implements Command
{
private final int address;
private final boolean destinationBit;
public Rrf(int input)
{
address = input & 0x007F;
destinationBit = checkDestinationBit(input);
}
@Override
public void execute()
{
int register = Memory.getRegister(address);
int newCarry = register & 0x01;
int oldCarry = Memory.getCarryBit();
register = (oldCarry << 7) + (register >>1);
if(newCarry == 1)
{
Memory.setCarryBit();
} else if(newCarry == 0){
Memory.clearCarryBit();
}
writeToDestination(destinationBit, address, register);
ProgramCounter.incPC();
}
}

View File

@@ -2,7 +2,7 @@ package de.darkress.pic16f84sim.commands;
import de.darkress.pic16f84sim.microcontroller.Memory;
public class Sublw extends LiteralCommandUtils implements Command
public class Sublw extends CommandUtils implements Command
{
private final int literal;

View File

@@ -1,49 +0,0 @@
package de.darkress.pic16f84sim.commands;
import de.darkress.pic16f84sim.microcontroller.Memory;
import de.darkress.pic16f84sim.microcontroller.ProgramCounter;
public class Subwf extends FileRegisterCommandUtils implements Command
{
private final int address;
private final boolean destinationBit;
public Subwf(int input)
{
address = input & 0x007F;
destinationBit = checkDestinationBit(input);
}
@Override
protected void checkCarryBit(int result)
{
if(result <= 255){
Memory.clearCarryBit();
} else{
Memory.setCarryBit();
}
}
@Override
protected void checkDigitCarryBit(int literal)
{
if(((literal & 0x0F) - (Memory.workingRegister & 0x0F)) < 0){
Memory.clearDigitCarryBit();
} else{
Memory.setDigitCarryBit();
}
}
@Override
public void execute()
{
int result = Memory.getRegister(address) - Memory.workingRegister + 256;
checkZeroBit(result);
checkCarryBit(result);
checkDigitCarryBit(Memory.getRegister(address));
writeToDestination(destinationBit, address, result % 256);
ProgramCounter.incPC();
}
}

View File

@@ -1,27 +0,0 @@
package de.darkress.pic16f84sim.commands;
import de.darkress.pic16f84sim.microcontroller.Memory;
import de.darkress.pic16f84sim.microcontroller.ProgramCounter;
public class Swapf extends FileRegisterCommandUtils implements Command
{
private final int address;
private final boolean destinationBit;
public Swapf(int input)
{
address = input & 0x007F;
destinationBit = checkDestinationBit(input);
}
@Override
public void execute()
{
int result = (Memory.getRegister(address) <<4) & 0xF0;
int tmp = Memory.getRegister(address) >>4;
result += tmp;
writeToDestination(destinationBit, address, result);
ProgramCounter.incPC();
}
}

View File

@@ -2,7 +2,7 @@ package de.darkress.pic16f84sim.commands;
import de.darkress.pic16f84sim.microcontroller.Memory;
public class Xorlw extends LiteralCommandUtils implements Command
public class Xorlw extends CommandUtils implements Command
{
private final int literal;

View File

@@ -1,27 +0,0 @@
package de.darkress.pic16f84sim.commands;
import de.darkress.pic16f84sim.microcontroller.Memory;
import de.darkress.pic16f84sim.microcontroller.ProgramCounter;
public class Xorwf extends FileRegisterCommandUtils implements Command
{
private final int address;
private final boolean destinationBit;
public Xorwf(int input)
{
address = input & 0x007F;
destinationBit = checkDestinationBit(input);
}
@Override
public void execute()
{
int result = Memory.getRegister(address) ^ Memory.workingRegister;
checkZeroBit(result);
writeToDestination(destinationBit, address, result);
ProgramCounter.incPC();
}
}

View File

@@ -9,33 +9,47 @@ public class CommandDecoder
switch(input & 0x3F00)
{
case 0x700:
return new Addwf(input);
//addwf();
break;
case 0x500:
return new Andwf(input);
//andwf();
break;
case 0x900:
return new Comf(input);
//comf();
break;
case 0x300:
return new Decf(input);
//decf();
break;
case 0xB00:
return new Decfsz(input);
//decfsz();
break;
case 0xA00:
return new Incf(input);
//incf();
break;
case 0xF00:
return new Incfsz(input);
//incfsz();
break;
case 0x400:
return new Iorwf(input);
//iorwf();
break;
case 0x800:
return new Movf(input);
//movf();
break;
case 0xD00:
return new Rlf(input);
//rlf();
break;
case 0xC00:
return new Rrf(input);
//rrf();
break;
case 0x200:
return new Subwf(input);
//subwf();
break;
case 0xE00:
return new Swapf(input);
//swapf();
break;
case 0x600:
return new Xorwf(input);
//xorwf();
break;
case 0x3900:
return new Andlw(input);
case 0x3800:
@@ -47,17 +61,21 @@ public class CommandDecoder
switch(input & 0x3F80)
{
case 0x180:
return new Clrf(input);
//clrf();
break;
case 0x100:
return new Clrw();
//clrw();
break;
case 0x80:
return new Movwf(input);
//movwf();
break;
}
switch(input & 0x3C00)
{
case 0x1000:
return new Bcf(input);
//bcf();
break;
case 0x1400:
//bsf();
break;
@@ -97,7 +115,7 @@ public class CommandDecoder
if ((input | 0x0060) == 0x0060)
{
return new Nop();
//nop();
}
if (input == 0x0064)

View File

@@ -59,10 +59,6 @@ public class Memory
}
memory[address] = data;
memory[address + 128] = data; //Ensure data is written to both banks to simulate mapping
if(address == 0x2) //Check if PCL is destination
{
ProgramCounter.loadPc();
}
}
private static int getDataFromIndirectAddress(int address)
@@ -79,10 +75,6 @@ public class Memory
}
memory[address % 128] = data; // else: Registers.Registers which are mapped
memory[address % 128 + 128] = data; //Ensure data is written to both banks to simulate mapping
if(address == 0x2) //Check if PCL is destination
{
ProgramCounter.loadPc();
}
}
private static int getRegisterBank()
@@ -153,13 +145,9 @@ public class Memory
memory[0x03] &= 0xFD;
}
public static int getCarryBit()
public static boolean getCarryBit()
{
if((memory[0x03] & 0x01) == 0x01)
{
return 1;
}
else return 0;
return (memory[0x03] & 0x01) == 0x01;
}
public static void setCarryBit()

View File

@@ -20,20 +20,19 @@ public class ProgramCounter
pc = (pch) + pcl;
}
public static void setPcFromStack(int stack)
{
pc = stack;
Memory.setPCL(pc & 0x00FF);
}
public static void loadPc()
{
pc = (Memory.getPCLATH() <<8) + Memory.getPCL();
}
public static void incPC() //is called after every instruction execution
{
pc++;
Memory.setPCL(pc & 0x00FF);
int pcl = Memory.getPCL();
int pclath = Memory.getPCLATH();
if(pcl == 0xFF)
{
pclath++;
pcl = 0;
} else {
pcl++;
}
Memory.setPCL(pcl);
Memory.setPCLATH(pclath);
pc = (pclath <<8) + pcl;
}
}