r16/emu/cpu/stack.c
Rose Apollo b30a4b1832 logging
2024-04-22 11:08:57 +00:00

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