r16/emu/cpu/stack.c

32 lines
675 B
C
Raw Normal View History

2024-04-19 09:05:10 +00:00
#include "./stack.h"
2024-04-19 10:08:32 +00:00
#include <stdlib.h>
#include <stdio.h>
2024-04-19 09:05:10 +00:00
2024-04-19 10:08:32 +00:00
r16_int StackPop()
2024-04-19 09:05:10 +00:00
{
#ifdef DO_STACK_CHECKS
2024-04-19 10:08:32 +00:00
if (GetRegister(SP).u == 0)
{
fprintf(stderr, "STACK UNDERFLOW\n");
exit(-1);
}
2024-04-19 09:05:10 +00:00
#endif
2024-04-19 10:08:32 +00:00
SetRegister(SP, (r16_int){ GetRegister(SP).u - 1 });
return GetValue((r16_int){ GetRegister(SP).u + STACK_LOCATION });
2024-04-19 09:05:10 +00:00
}
2024-04-19 10:08:32 +00:00
void StackPush(r16_int value)
2024-04-19 09:05:10 +00:00
{
2024-04-19 10:08:32 +00:00
#ifdef DO_STACK_CHECKS
if (GetRegister(SP).u >= STACK_SIZE)
{
fprintf(stderr, "STACK OVERFLOW\n");
exit(-1);
}
#endif
2024-04-19 09:05:10 +00:00
2024-04-19 10:08:32 +00:00
SetValue((r16_int){ GetRegister(SP).u + STACK_LOCATION }, value);
SetRegister(SP, (r16_int){ GetRegister(SP).u + 1 });
2024-04-19 09:05:10 +00:00
}