Addlw instruction (#4)
* Added addlw command * Write to program list * Included CommandUtils as helpter class. Sorted classes into packages * Revert changes on HelloWorld * Revert "Revert changes on HelloWorld" This reverts commit a08a336864fb2aa2bbc5a4e37ca360765774965e. * Added example execution of Addlw command
This commit is contained in:
@@ -1,7 +1,14 @@
|
|||||||
|
import commands.Command;
|
||||||
|
import decoder.CommandDecoder;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
class HelloWorld {
|
class HelloWorld {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
System.out.println(Parser.parser("TPicSim2.LST"));
|
//System.out.println(Parser.Parser.parser("TPicSim2.LST"));
|
||||||
|
ArrayList<Command> program = new ArrayList<>();
|
||||||
|
int input1 = 0x3EFF;
|
||||||
|
program.add(CommandDecoder.decode(input1));
|
||||||
|
program.get(0).execute();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
25
commands/Addlw.java
Normal file
25
commands/Addlw.java
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
package commands;
|
||||||
|
|
||||||
|
import registers.Memory;
|
||||||
|
|
||||||
|
public class Addlw extends CommandUtils implements Command
|
||||||
|
{
|
||||||
|
private final int literal;
|
||||||
|
|
||||||
|
public Addlw(int input)
|
||||||
|
{
|
||||||
|
literal = input & 0x00FF;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute()
|
||||||
|
{
|
||||||
|
int result = literal + Memory.workingRegister;
|
||||||
|
|
||||||
|
checkZeroBit(result);
|
||||||
|
checkCarryBit(result);
|
||||||
|
checkDigitCarryBit(result, literal);
|
||||||
|
|
||||||
|
Memory.workingRegister = result % 256;
|
||||||
|
}
|
||||||
|
}
|
||||||
6
commands/Command.java
Normal file
6
commands/Command.java
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
package commands;
|
||||||
|
|
||||||
|
public interface Command
|
||||||
|
{
|
||||||
|
void execute();
|
||||||
|
}
|
||||||
33
commands/CommandUtils.java
Normal file
33
commands/CommandUtils.java
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
package commands;
|
||||||
|
|
||||||
|
import registers.Memory;
|
||||||
|
|
||||||
|
public class CommandUtils
|
||||||
|
{
|
||||||
|
protected void checkZeroBit(int result)
|
||||||
|
{
|
||||||
|
if(result == 0){
|
||||||
|
Memory.setZeroBit();
|
||||||
|
} else{
|
||||||
|
Memory.clearZeroBit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void checkCarryBit(int result)
|
||||||
|
{
|
||||||
|
if(result > 255){
|
||||||
|
Memory.setCarryBit();
|
||||||
|
} else{
|
||||||
|
Memory.clearCarryBit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void checkDigitCarryBit(int result, int literal)
|
||||||
|
{
|
||||||
|
if(((Memory.workingRegister & 0x0F) + (literal & 0x0F)) > 15){
|
||||||
|
Memory.setDigitCarryBit();
|
||||||
|
} else{
|
||||||
|
Memory.clearDigitCarryBit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,11 @@
|
|||||||
|
package decoder;
|
||||||
|
|
||||||
|
import commands.Addlw;
|
||||||
|
import commands.Command;
|
||||||
|
|
||||||
public class CommandDecoder
|
public class CommandDecoder
|
||||||
{
|
{
|
||||||
public CommandDecoder(int input)
|
public static Command decode(int input)
|
||||||
{
|
{
|
||||||
switch(input & 0x3F00)
|
switch(input & 0x3F00)
|
||||||
{
|
{
|
||||||
@@ -90,7 +95,8 @@ public class CommandDecoder
|
|||||||
{
|
{
|
||||||
case 0x3E00:
|
case 0x3E00:
|
||||||
//addlw();
|
//addlw();
|
||||||
break;
|
return new Addlw(input);
|
||||||
|
//break;
|
||||||
case 0x3C00:
|
case 0x3C00:
|
||||||
//sublw();
|
//sublw();
|
||||||
break;
|
break;
|
||||||
@@ -141,5 +147,8 @@ public class CommandDecoder
|
|||||||
{
|
{
|
||||||
//sleep();
|
//sleep();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
System.out.println("No command matched");
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,11 +1,14 @@
|
|||||||
|
package parser;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class Parser
|
public class Parser
|
||||||
{
|
{
|
||||||
public static ArrayList<Integer> parser(String filePath)
|
public static List<Integer> parser(String filePath)
|
||||||
{
|
{
|
||||||
ArrayList<Integer> program = new ArrayList<>();
|
ArrayList<Integer> program = new ArrayList<>();
|
||||||
try {
|
try {
|
||||||
@@ -1,9 +1,11 @@
|
|||||||
|
package registers;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
public class Registers
|
public class Memory
|
||||||
{
|
{
|
||||||
private static final int MEMORY_SIZE = 0xFF; //Addressable Memory
|
private static final int MEMORY_SIZE = 0xFF; //Addressable Memory
|
||||||
public static final int workingRegister = 0;
|
public static int workingRegister = 0;
|
||||||
private static final int[] memory = new int[MEMORY_SIZE];
|
private static final int[] memory = new int[MEMORY_SIZE];
|
||||||
private static final int[] bank0UniqueSpecialRegister = new int[] {0x01,0x05,0x06,0x08,0x09}; //and many more
|
private static final int[] bank0UniqueSpecialRegister = new int[] {0x01,0x05,0x06,0x08,0x09}; //and many more
|
||||||
private static final int[] bank1UniqueSpecialRegister = new int[] {0x81,0x85,0x86,0x88,0x89}; //and many more
|
private static final int[] bank1UniqueSpecialRegister = new int[] {0x81,0x85,0x86,0x88,0x89}; //and many more
|
||||||
@@ -50,7 +52,7 @@ public class Registers
|
|||||||
{
|
{
|
||||||
return memory[address];
|
return memory[address];
|
||||||
}
|
}
|
||||||
return memory[address % 128]; // else: Registers which are mapped
|
return memory[address % 128]; // else: Registers.Registers which are mapped
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void setDataFromIndirectAddress(int address, int data)
|
private static void setDataFromIndirectAddress(int address, int data)
|
||||||
@@ -59,7 +61,7 @@ public class Registers
|
|||||||
{
|
{
|
||||||
memory[address] = data;
|
memory[address] = data;
|
||||||
}
|
}
|
||||||
memory[address % 128] = data; // else: Registers which are mapped
|
memory[address % 128] = data; // else: Registers.Registers which are mapped
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int getRegisterBank()
|
private static int getRegisterBank()
|
||||||
Reference in New Issue
Block a user