Compare commits
15 Commits
andwfInstr
...
subwfInstr
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
eda63f0073 | ||
|
|
d25a66134a | ||
|
|
01bd5acb46 | ||
|
|
40b7a4a478 | ||
|
|
a5e3039667 | ||
|
|
537b4559c4 | ||
|
|
1a9f2436ef | ||
|
|
ab9f378d34 | ||
|
|
b23bf4c54b | ||
|
|
7eab465fff | ||
|
|
c896b4f199 | ||
|
|
46515de5f1 | ||
|
|
49ab5876b8 | ||
|
|
1f8e3aff58 | ||
|
|
c7d062cffd |
@@ -12,11 +12,10 @@ class Main
|
||||
public static void main(String[] args) {
|
||||
|
||||
ArrayList<Command> program = new ArrayList<>();
|
||||
int input1 = 0x27FF;
|
||||
program.add(CommandDecoder.decode(0x3011)); //Write 0x11 to W
|
||||
Memory.setRegister(0x14, 0x14);
|
||||
program.add(CommandDecoder.decode(0x0714));
|
||||
for(int i = 0; i < 2; i++)
|
||||
Memory.workingRegister = 0x01;
|
||||
Memory.setRegister(0x14, 0x01); //240 << 224
|
||||
program.add(CommandDecoder.decode(0x0294));
|
||||
for(int i = 0; i < program.size(); i++)
|
||||
{
|
||||
program.get(ProgramCounter.getPc()).execute();
|
||||
}
|
||||
|
||||
27
de/darkress/pic16f84sim/commands/Andwf.java
Normal file
27
de/darkress/pic16f84sim/commands/Andwf.java
Normal file
@@ -0,0 +1,27 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
27
de/darkress/pic16f84sim/commands/Clrf.java
Normal file
27
de/darkress/pic16f84sim/commands/Clrf.java
Normal file
@@ -0,0 +1,27 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
18
de/darkress/pic16f84sim/commands/Clrw.java
Normal file
18
de/darkress/pic16f84sim/commands/Clrw.java
Normal file
@@ -0,0 +1,18 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
27
de/darkress/pic16f84sim/commands/Comf.java
Normal file
27
de/darkress/pic16f84sim/commands/Comf.java
Normal file
@@ -0,0 +1,27 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
27
de/darkress/pic16f84sim/commands/Decf.java
Normal file
27
de/darkress/pic16f84sim/commands/Decf.java
Normal file
@@ -0,0 +1,27 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
33
de/darkress/pic16f84sim/commands/Decfsz.java
Normal file
33
de/darkress/pic16f84sim/commands/Decfsz.java
Normal file
@@ -0,0 +1,33 @@
|
||||
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();
|
||||
}
|
||||
ProgramCounter.incPC();
|
||||
}
|
||||
}
|
||||
27
de/darkress/pic16f84sim/commands/Incf.java
Normal file
27
de/darkress/pic16f84sim/commands/Incf.java
Normal file
@@ -0,0 +1,27 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
33
de/darkress/pic16f84sim/commands/Incfsz.java
Normal file
33
de/darkress/pic16f84sim/commands/Incfsz.java
Normal file
@@ -0,0 +1,33 @@
|
||||
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();
|
||||
}
|
||||
ProgramCounter.incPC();
|
||||
}
|
||||
}
|
||||
27
de/darkress/pic16f84sim/commands/Iorwf.java
Normal file
27
de/darkress/pic16f84sim/commands/Iorwf.java
Normal file
@@ -0,0 +1,27 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
27
de/darkress/pic16f84sim/commands/Movf.java
Normal file
27
de/darkress/pic16f84sim/commands/Movf.java
Normal file
@@ -0,0 +1,27 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
25
de/darkress/pic16f84sim/commands/Movwf.java
Normal file
25
de/darkress/pic16f84sim/commands/Movwf.java
Normal file
@@ -0,0 +1,25 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
14
de/darkress/pic16f84sim/commands/Nop.java
Normal file
14
de/darkress/pic16f84sim/commands/Nop.java
Normal file
@@ -0,0 +1,14 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
35
de/darkress/pic16f84sim/commands/Rlf.java
Normal file
35
de/darkress/pic16f84sim/commands/Rlf.java
Normal file
@@ -0,0 +1,35 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
35
de/darkress/pic16f84sim/commands/Rrf.java
Normal file
35
de/darkress/pic16f84sim/commands/Rrf.java
Normal file
@@ -0,0 +1,35 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
49
de/darkress/pic16f84sim/commands/Subwf.java
Normal file
49
de/darkress/pic16f84sim/commands/Subwf.java
Normal file
@@ -0,0 +1,49 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -11,38 +11,27 @@ public class CommandDecoder
|
||||
case 0x700:
|
||||
return new Addwf(input);
|
||||
case 0x500:
|
||||
//andwf();
|
||||
break;
|
||||
return new Andwf(input);
|
||||
case 0x900:
|
||||
//comf();
|
||||
break;
|
||||
return new Comf(input);
|
||||
case 0x300:
|
||||
//decf();
|
||||
break;
|
||||
return new Decf(input);
|
||||
case 0xB00:
|
||||
//decfsz();
|
||||
break;
|
||||
return new Decfsz(input);
|
||||
case 0xA00:
|
||||
//incf();
|
||||
break;
|
||||
return new Incf(input);
|
||||
case 0xF00:
|
||||
//incfsz();
|
||||
break;
|
||||
return new Incfsz(input);
|
||||
case 0x400:
|
||||
//iorwf();
|
||||
break;
|
||||
return new Iorwf(input);
|
||||
case 0x800:
|
||||
//movf();
|
||||
break;
|
||||
return new Movf(input);
|
||||
case 0xD00:
|
||||
//rlf();
|
||||
break;
|
||||
return new Rlf(input);
|
||||
case 0xC00:
|
||||
//rrf();
|
||||
break;
|
||||
return new Rrf(input);
|
||||
case 0x200:
|
||||
//subwf();
|
||||
break;
|
||||
return new Subwf(input);
|
||||
case 0xE00:
|
||||
//swapf();
|
||||
break;
|
||||
@@ -60,14 +49,11 @@ public class CommandDecoder
|
||||
switch(input & 0x3F80)
|
||||
{
|
||||
case 0x180:
|
||||
//clrf();
|
||||
break;
|
||||
return new Clrf(input);
|
||||
case 0x100:
|
||||
//clrw();
|
||||
break;
|
||||
return new Clrw();
|
||||
case 0x80:
|
||||
//movwf();
|
||||
break;
|
||||
return new Movwf(input);
|
||||
}
|
||||
|
||||
switch(input & 0x3C00)
|
||||
@@ -114,7 +100,7 @@ public class CommandDecoder
|
||||
|
||||
if ((input | 0x0060) == 0x0060)
|
||||
{
|
||||
//nop();
|
||||
return new Nop();
|
||||
}
|
||||
|
||||
if (input == 0x0064)
|
||||
|
||||
@@ -153,9 +153,13 @@ public class Memory
|
||||
memory[0x03] &= 0xFD;
|
||||
}
|
||||
|
||||
public static boolean getCarryBit()
|
||||
public static int getCarryBit()
|
||||
{
|
||||
return (memory[0x03] & 0x01) == 0x01;
|
||||
if((memory[0x03] & 0x01) == 0x01)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else return 0;
|
||||
}
|
||||
|
||||
public static void setCarryBit()
|
||||
|
||||
Reference in New Issue
Block a user