From 7af1145ad4abbd5978378a454b8d5d798bfb900e Mon Sep 17 00:00:00 2001 From: darkress <30271678+darkressx@users.noreply.github.com> Date: Wed, 31 May 2023 19:34:56 +0200 Subject: [PATCH] implemented Addwf Instruction (#7) Co-authored-by: Darkress <30271678+DarkressX@users.noreply.github.com> Reviewed-on: https://git.darkress.xyz/darkress/pic16f84-sim/pulls/7 --- de/darkress/pic16f84sim/Main.java | 9 ++-- de/darkress/pic16f84sim/commands/Addlw.java | 2 +- de/darkress/pic16f84sim/commands/Addwf.java | 29 +++++++++++ de/darkress/pic16f84sim/commands/Andlw.java | 2 +- de/darkress/pic16f84sim/commands/Call.java | 2 +- .../commands/FileRegisterCommandUtils.java | 51 +++++++++++++++++++ de/darkress/pic16f84sim/commands/Goto.java | 2 +- de/darkress/pic16f84sim/commands/Iorlw.java | 2 +- ...andUtils.java => LiteralCommandUtils.java} | 2 +- de/darkress/pic16f84sim/commands/Movlw.java | 4 +- de/darkress/pic16f84sim/commands/Retlw.java | 2 +- de/darkress/pic16f84sim/commands/Return.java | 2 +- de/darkress/pic16f84sim/commands/Sublw.java | 2 +- de/darkress/pic16f84sim/commands/Xorlw.java | 2 +- .../pic16f84sim/decoder/CommandDecoder.java | 3 +- 15 files changed, 98 insertions(+), 18 deletions(-) create mode 100644 de/darkress/pic16f84sim/commands/Addwf.java create mode 100644 de/darkress/pic16f84sim/commands/FileRegisterCommandUtils.java rename de/darkress/pic16f84sim/commands/{CommandUtils.java => LiteralCommandUtils.java} (95%) diff --git a/de/darkress/pic16f84sim/Main.java b/de/darkress/pic16f84sim/Main.java index 35901c5..0c8f367 100644 --- a/de/darkress/pic16f84sim/Main.java +++ b/de/darkress/pic16f84sim/Main.java @@ -13,11 +13,10 @@ class Main ArrayList program = new ArrayList<>(); 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.add(CommandDecoder.decode(0x3011)); //Write 0x11 to W + Memory.setRegister(0x14, 0x14); + program.add(CommandDecoder.decode(0x0714)); + for(int i = 0; i < 2; i++) { program.get(ProgramCounter.getPc()).execute(); } diff --git a/de/darkress/pic16f84sim/commands/Addlw.java b/de/darkress/pic16f84sim/commands/Addlw.java index f630022..ef1e8e4 100644 --- a/de/darkress/pic16f84sim/commands/Addlw.java +++ b/de/darkress/pic16f84sim/commands/Addlw.java @@ -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 CommandUtils implements Command +public class Addlw extends LiteralCommandUtils implements Command { private final int literal; diff --git a/de/darkress/pic16f84sim/commands/Addwf.java b/de/darkress/pic16f84sim/commands/Addwf.java new file mode 100644 index 0000000..c54e435 --- /dev/null +++ b/de/darkress/pic16f84sim/commands/Addwf.java @@ -0,0 +1,29 @@ +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(); + } +} diff --git a/de/darkress/pic16f84sim/commands/Andlw.java b/de/darkress/pic16f84sim/commands/Andlw.java index a4ff8a1..b5d1547 100644 --- a/de/darkress/pic16f84sim/commands/Andlw.java +++ b/de/darkress/pic16f84sim/commands/Andlw.java @@ -2,7 +2,7 @@ package de.darkress.pic16f84sim.commands; import de.darkress.pic16f84sim.microcontroller.Memory; -public class Andlw extends CommandUtils implements Command +public class Andlw extends LiteralCommandUtils implements Command { private final int literal; diff --git a/de/darkress/pic16f84sim/commands/Call.java b/de/darkress/pic16f84sim/commands/Call.java index cd68115..67c86f9 100644 --- a/de/darkress/pic16f84sim/commands/Call.java +++ b/de/darkress/pic16f84sim/commands/Call.java @@ -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 CommandUtils implements Command +public class Call extends LiteralCommandUtils implements Command { private final int literal; diff --git a/de/darkress/pic16f84sim/commands/FileRegisterCommandUtils.java b/de/darkress/pic16f84sim/commands/FileRegisterCommandUtils.java new file mode 100644 index 0000000..72d7487 --- /dev/null +++ b/de/darkress/pic16f84sim/commands/FileRegisterCommandUtils.java @@ -0,0 +1,51 @@ +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; + } +} diff --git a/de/darkress/pic16f84sim/commands/Goto.java b/de/darkress/pic16f84sim/commands/Goto.java index 23a1c1f..f77392e 100644 --- a/de/darkress/pic16f84sim/commands/Goto.java +++ b/de/darkress/pic16f84sim/commands/Goto.java @@ -2,7 +2,7 @@ package de.darkress.pic16f84sim.commands; import de.darkress.pic16f84sim.microcontroller.ProgramCounter; -public class Goto extends CommandUtils implements Command +public class Goto extends LiteralCommandUtils implements Command { private final int literal; diff --git a/de/darkress/pic16f84sim/commands/Iorlw.java b/de/darkress/pic16f84sim/commands/Iorlw.java index 84842b7..d564e87 100644 --- a/de/darkress/pic16f84sim/commands/Iorlw.java +++ b/de/darkress/pic16f84sim/commands/Iorlw.java @@ -2,7 +2,7 @@ package de.darkress.pic16f84sim.commands; import de.darkress.pic16f84sim.microcontroller.Memory; -public class Iorlw extends CommandUtils implements Command +public class Iorlw extends LiteralCommandUtils implements Command { private final int literal; diff --git a/de/darkress/pic16f84sim/commands/CommandUtils.java b/de/darkress/pic16f84sim/commands/LiteralCommandUtils.java similarity index 95% rename from de/darkress/pic16f84sim/commands/CommandUtils.java rename to de/darkress/pic16f84sim/commands/LiteralCommandUtils.java index d43da2c..71d3574 100644 --- a/de/darkress/pic16f84sim/commands/CommandUtils.java +++ b/de/darkress/pic16f84sim/commands/LiteralCommandUtils.java @@ -2,7 +2,7 @@ package de.darkress.pic16f84sim.commands; import de.darkress.pic16f84sim.microcontroller.Memory; -public class CommandUtils +public class LiteralCommandUtils { protected void checkZeroBit(int result) { diff --git a/de/darkress/pic16f84sim/commands/Movlw.java b/de/darkress/pic16f84sim/commands/Movlw.java index 0c8d18c..3635120 100644 --- a/de/darkress/pic16f84sim/commands/Movlw.java +++ b/de/darkress/pic16f84sim/commands/Movlw.java @@ -1,8 +1,9 @@ package de.darkress.pic16f84sim.commands; import de.darkress.pic16f84sim.microcontroller.Memory; +import de.darkress.pic16f84sim.microcontroller.ProgramCounter; -public class Movlw extends CommandUtils implements Command +public class Movlw extends LiteralCommandUtils implements Command { private final int literal; @@ -15,5 +16,6 @@ public class Movlw extends CommandUtils implements Command public void execute() { Memory.workingRegister = literal; + ProgramCounter.incPC(); } } diff --git a/de/darkress/pic16f84sim/commands/Retlw.java b/de/darkress/pic16f84sim/commands/Retlw.java index 0e73aeb..b4dc2c4 100644 --- a/de/darkress/pic16f84sim/commands/Retlw.java +++ b/de/darkress/pic16f84sim/commands/Retlw.java @@ -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 CommandUtils implements Command +public class Retlw extends LiteralCommandUtils implements Command { private final int literal; diff --git a/de/darkress/pic16f84sim/commands/Return.java b/de/darkress/pic16f84sim/commands/Return.java index 9426725..9afe719 100644 --- a/de/darkress/pic16f84sim/commands/Return.java +++ b/de/darkress/pic16f84sim/commands/Return.java @@ -3,7 +3,7 @@ package de.darkress.pic16f84sim.commands; import de.darkress.pic16f84sim.microcontroller.ProgramCounter; import de.darkress.pic16f84sim.microcontroller.Stack; -public class Return extends CommandUtils implements Command +public class Return extends LiteralCommandUtils implements Command { @Override public void execute() diff --git a/de/darkress/pic16f84sim/commands/Sublw.java b/de/darkress/pic16f84sim/commands/Sublw.java index 39127ea..ed28bbd 100644 --- a/de/darkress/pic16f84sim/commands/Sublw.java +++ b/de/darkress/pic16f84sim/commands/Sublw.java @@ -2,7 +2,7 @@ package de.darkress.pic16f84sim.commands; import de.darkress.pic16f84sim.microcontroller.Memory; -public class Sublw extends CommandUtils implements Command +public class Sublw extends LiteralCommandUtils implements Command { private final int literal; diff --git a/de/darkress/pic16f84sim/commands/Xorlw.java b/de/darkress/pic16f84sim/commands/Xorlw.java index a3e21af..e93d615 100644 --- a/de/darkress/pic16f84sim/commands/Xorlw.java +++ b/de/darkress/pic16f84sim/commands/Xorlw.java @@ -2,7 +2,7 @@ package de.darkress.pic16f84sim.commands; import de.darkress.pic16f84sim.microcontroller.Memory; -public class Xorlw extends CommandUtils implements Command +public class Xorlw extends LiteralCommandUtils implements Command { private final int literal; diff --git a/de/darkress/pic16f84sim/decoder/CommandDecoder.java b/de/darkress/pic16f84sim/decoder/CommandDecoder.java index 83ab994..3e692f6 100644 --- a/de/darkress/pic16f84sim/decoder/CommandDecoder.java +++ b/de/darkress/pic16f84sim/decoder/CommandDecoder.java @@ -9,8 +9,7 @@ public class CommandDecoder switch(input & 0x3F00) { case 0x700: - //addwf(); - break; + return new Addwf(input); case 0x500: //andwf(); break;