46 lines
1.0 KiB
C
46 lines
1.0 KiB
C
#include "./stack.h"
|
|
#include "./register.h"
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <stdbool.h>
|
|
|
|
void LogStack(bool onlyUpToSP, bool pointOutSP)
|
|
{
|
|
char *pointer = "->";
|
|
char *nointer = " ";
|
|
|
|
for (int i = 0; i < (onlyUpToSP ? GetRegister(SP).u : STACK_SIZE); i++)
|
|
{
|
|
printf("%s 0x%x: 0x%x\n", ((pointOutSP && i == GetRegister(SP).u) ? pointer : nointer),
|
|
i, GetValue((r16_int){ STACK_LOCATION + i }).u);
|
|
}
|
|
}
|
|
|
|
r16_int StackPop()
|
|
{
|
|
#ifdef DO_STACK_CHECKS
|
|
if (GetRegister(SP).u == 0)
|
|
{
|
|
fprintf(stderr, "STACK UNDERFLOW\n");
|
|
exit(-1);
|
|
}
|
|
#endif
|
|
|
|
SetRegister(SP, (r16_int){ GetRegister(SP).u - 1 });
|
|
return GetValue((r16_int){ GetRegister(SP).u + STACK_LOCATION });
|
|
}
|
|
|
|
void StackPush(r16_int value)
|
|
{
|
|
#ifdef DO_STACK_CHECKS
|
|
if (GetRegister(SP).u >= STACK_SIZE)
|
|
{
|
|
fprintf(stderr, "STACK OVERFLOW\n");
|
|
exit(-1);
|
|
}
|
|
#endif
|
|
|
|
SetValue((r16_int){ GetRegister(SP).u + STACK_LOCATION }, value);
|
|
SetRegister(SP, (r16_int){ GetRegister(SP).u + 1 });
|
|
}
|