implemented Stack (#10)
This commit is contained in:
@@ -1,14 +0,0 @@
|
|||||||
import commands.Command;
|
|
||||||
import decoder.CommandDecoder;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
class HelloWorld {
|
|
||||||
public static void main(String[] args) {
|
|
||||||
//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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
33
de/darkress/pic16f84sim/Main.java
Normal file
33
de/darkress/pic16f84sim/Main.java
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
package de.darkress.pic16f84sim;
|
||||||
|
|
||||||
|
import de.darkress.pic16f84sim.commands.Command;
|
||||||
|
import de.darkress.pic16f84sim.decoder.CommandDecoder;
|
||||||
|
import de.darkress.pic16f84sim.registers.Memory;
|
||||||
|
import de.darkress.pic16f84sim.registers.Stack;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
class Main
|
||||||
|
{
|
||||||
|
public static void main(String[] args) {
|
||||||
|
/*
|
||||||
|
ArrayList<Command> program = new ArrayList<>();
|
||||||
|
int input1 = 0x3EFF;
|
||||||
|
program.add(CommandDecoder.decode(input1));
|
||||||
|
program.get(0).execute();
|
||||||
|
*/
|
||||||
|
|
||||||
|
for(int i = 0; i < 8; i++)
|
||||||
|
{
|
||||||
|
Stack.push(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
for(int i = 0; i < 16; i++)
|
||||||
|
{
|
||||||
|
System.out.println(Stack.peek());
|
||||||
|
Stack.pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
package commands;
|
package de.darkress.pic16f84sim.commands;
|
||||||
|
|
||||||
import registers.Memory;
|
import de.darkress.pic16f84sim.registers.Memory;
|
||||||
|
|
||||||
public class Addlw extends CommandUtils implements Command
|
public class Addlw extends CommandUtils implements Command
|
||||||
{
|
{
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
package commands;
|
package de.darkress.pic16f84sim.commands;
|
||||||
|
|
||||||
import registers.Memory;
|
import de.darkress.pic16f84sim.registers.Memory;
|
||||||
|
|
||||||
public class Andlw extends CommandUtils implements Command
|
public class Andlw extends CommandUtils implements Command
|
||||||
{
|
{
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package commands;
|
package de.darkress.pic16f84sim.commands;
|
||||||
|
|
||||||
public interface Command
|
public interface Command
|
||||||
{
|
{
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
package commands;
|
package de.darkress.pic16f84sim.commands;
|
||||||
|
|
||||||
import registers.Memory;
|
import de.darkress.pic16f84sim.registers.Memory;
|
||||||
|
|
||||||
public class CommandUtils
|
public class CommandUtils
|
||||||
{
|
{
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
package commands;
|
package de.darkress.pic16f84sim.commands;
|
||||||
|
|
||||||
import registers.Memory;
|
import de.darkress.pic16f84sim.registers.Memory;
|
||||||
|
|
||||||
public class Iorlw extends CommandUtils implements Command
|
public class Iorlw extends CommandUtils implements Command
|
||||||
{
|
{
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
package commands;
|
package de.darkress.pic16f84sim.commands;
|
||||||
|
|
||||||
import registers.Memory;
|
import de.darkress.pic16f84sim.registers.Memory;
|
||||||
|
|
||||||
public class Movlw extends CommandUtils implements Command
|
public class Movlw extends CommandUtils implements Command
|
||||||
{
|
{
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
package commands;
|
package de.darkress.pic16f84sim.commands;
|
||||||
|
|
||||||
import registers.Memory;
|
import de.darkress.pic16f84sim.registers.Memory;
|
||||||
|
|
||||||
public class Sublw extends CommandUtils implements Command
|
public class Sublw extends CommandUtils implements Command
|
||||||
{
|
{
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
package decoder;
|
package de.darkress.pic16f84sim.decoder;
|
||||||
|
|
||||||
import commands.*;
|
import de.darkress.pic16f84sim.commands.*;
|
||||||
|
|
||||||
public class CommandDecoder
|
public class CommandDecoder
|
||||||
{
|
{
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package parser;
|
package de.darkress.pic16f84sim.parser;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package registers;
|
package de.darkress.pic16f84sim.registers;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
@@ -12,7 +12,7 @@ public class Memory
|
|||||||
|
|
||||||
public static int getRegister(int address)
|
public static int getRegister(int address)
|
||||||
{
|
{
|
||||||
if(address + 128 > 255) //Guard statement to check for early errors in command decoder or implementation
|
if(address + 128 > 255) //Guard statement to check for early errors in command de.darkress.pic16f84sim.decoder or implementation
|
||||||
{
|
{
|
||||||
System.err.println("Guard statement triggered. The address must be 7Bit long and can therefore not exceed" +
|
System.err.println("Guard statement triggered. The address must be 7Bit long and can therefore not exceed" +
|
||||||
" 127");
|
" 127");
|
||||||
@@ -35,7 +35,7 @@ public class Memory
|
|||||||
|
|
||||||
public static void setRegister(int address, int data)
|
public static void setRegister(int address, int data)
|
||||||
{
|
{
|
||||||
if(address + 128 > 255) //Guard statement to check for early errors in command decoder or implementation
|
if(address + 128 > 255) //Guard statement to check for early errors in command de.darkress.pic16f84sim.decoder or implementation
|
||||||
{
|
{
|
||||||
System.err.println("Guard statement triggered. The address must be 7Bit long and can therefore not exceed" +
|
System.err.println("Guard statement triggered. The address must be 7Bit long and can therefore not exceed" +
|
||||||
" 127");
|
" 127");
|
||||||
51
de/darkress/pic16f84sim/registers/Stack.java
Normal file
51
de/darkress/pic16f84sim/registers/Stack.java
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
package de.darkress.pic16f84sim.registers;
|
||||||
|
|
||||||
|
|
||||||
|
public class Stack
|
||||||
|
{
|
||||||
|
private static int[] stack = new int[8];
|
||||||
|
private static int stackPointer = 0;
|
||||||
|
|
||||||
|
public static void push(int data)
|
||||||
|
{
|
||||||
|
stack[stackPointer] = data;
|
||||||
|
pointNext();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int pop()
|
||||||
|
{
|
||||||
|
int tmp = stack[stackPointer];
|
||||||
|
pointPrevious();
|
||||||
|
return tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int peek()
|
||||||
|
{
|
||||||
|
return stack[(stackPointer + 7) % 8]; //Get TopOfStack -1 +8 = 7 and modulo 8 to avoid IndexOutOfBound
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void pointNext()
|
||||||
|
{
|
||||||
|
if(stackPointer == 7)
|
||||||
|
{
|
||||||
|
stackPointer = 0;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
stackPointer++;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void pointPrevious()
|
||||||
|
{
|
||||||
|
if(stackPointer == 0)
|
||||||
|
{
|
||||||
|
stackPointer = 7;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
stackPointer--;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int getStackPointer()
|
||||||
|
{
|
||||||
|
return stackPointer;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user