Stack
This commit is contained in:
@ -44,6 +44,7 @@ public class Controller_FileSelect {
|
||||
System.out.println("W: " + Commands.get_wRegister());
|
||||
System.out.println("C: " + DataRegister.getCarryFlag());
|
||||
System.out.println("DC: " + DataRegister.getDigitCarryFlag());
|
||||
System.out.println("StackPointer: " + ProgramStack.getStackPointer());
|
||||
System.out.println(ProgramStack.getStack());
|
||||
System.out.printf(Arrays.toString(DataRegister.getDataRegister()));
|
||||
DataRegister.increasePC();
|
||||
|
||||
@ -1,25 +1,42 @@
|
||||
package fabrik.simulator.pic16f84;
|
||||
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Deque;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ProgramStack {
|
||||
private static Deque<Integer> returnStack = new ArrayDeque<>();
|
||||
private static List<Integer> returnStack = new ArrayList<>(8);
|
||||
private static int stackIndex = 0;
|
||||
|
||||
private static void incIndex(){
|
||||
if (stackIndex == 7){
|
||||
stackIndex = 0;
|
||||
}
|
||||
else{
|
||||
stackIndex++;
|
||||
}
|
||||
}
|
||||
|
||||
public static void push(int value) {
|
||||
if (returnStack.size() != 7)
|
||||
returnStack.push(value);
|
||||
else {
|
||||
System.out.println("Stack overflow");
|
||||
if (returnStack.size() != 8) {
|
||||
returnStack.add(value);
|
||||
incIndex();
|
||||
}
|
||||
else {
|
||||
returnStack.set(stackIndex, value);
|
||||
incIndex();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static int pop() {
|
||||
return returnStack.pop();
|
||||
stackIndex = (stackIndex > 0) ? --stackIndex : 7;
|
||||
return returnStack.get(stackIndex)-1;
|
||||
}
|
||||
|
||||
public static Deque<Integer> getStack() {
|
||||
public static int getStackPointer(){
|
||||
return stackIndex;
|
||||
}
|
||||
|
||||
public static List<Integer> getStack() {
|
||||
return returnStack;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user