r16/emu/cpu/stack.c
2024-04-19 10:08:32 +00:00

32 lines
675 B
C

#include "./stack.h"
#include <stdlib.h>
#include <stdio.h>
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 });
}