implemented Stack (#10)

This commit is contained in:
DarkressX
2023-05-28 19:30:07 +02:00
committed by GitHub
parent 5c4aee2da6
commit 29ec2afe04
13 changed files with 103 additions and 33 deletions

View File

@@ -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();
}
}

View 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();
}
}
}

View File

@@ -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
{ {

View File

@@ -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
{ {

View File

@@ -1,4 +1,4 @@
package commands; package de.darkress.pic16f84sim.commands;
public interface Command public interface Command
{ {

View File

@@ -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
{ {

View File

@@ -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
{ {

View File

@@ -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
{ {

View File

@@ -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
{ {

View File

@@ -1,6 +1,6 @@
package decoder; package de.darkress.pic16f84sim.decoder;
import commands.*; import de.darkress.pic16f84sim.commands.*;
public class CommandDecoder public class CommandDecoder
{ {

View File

@@ -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;

View File

@@ -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");

View 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;
}
}