Skip to content

Commit

Permalink
Fix stack issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Eggbertx committed Oct 7, 2024
1 parent e306a27 commit 94d5ae5
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
8 changes: 3 additions & 5 deletions src/chip8.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ void _OC8_FASTCALL doCycle() {
ushort nnn = 0; /* -nnn */
uchar nn = 0; /* --nn */
uchar n = 0; /* ---n */
uchar event;

chip8.currentKey = -1; /* -1 if no keys are pressed, otherwise */

delay(1);
Expand Down Expand Up @@ -194,10 +194,9 @@ void _OC8_FASTCALL doCycle() {
CLEAR_CURENT_OPCODE();
strcpy(currentOpcode, "RET");
#endif
if(chip8.stackPointer)
chip8.PC = chip8.stack[chip8.stackPointer] + ROM_START_ADDR + 2;
if(chip8.stackPointer == 0)
goto stack_underflow;
chip8.PC = chip8.stack[chip8.stackPointer];
chip8.stackPointer--;
} else {
goto unrecognized_opcode;
Expand All @@ -223,11 +222,10 @@ void _OC8_FASTCALL doCycle() {
CLEAR_CURENT_OPCODE();
sprintf(currentOpcode, "CALL %#04x", nnn);
#endif
chip8.stack[chip8.stackPointer] = chip8.PC;
if(chip8.stackPointer >= 16)
goto stack_overflow;

chip8.stackPointer++;
chip8.stack[chip8.stackPointer] = chip8.PC;
chip8.PC = nnn;
break;
case 0x3000:
Expand Down
2 changes: 1 addition & 1 deletion src/chip8.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ struct Chip8 {
/* both timers down-count about 60 times per second when non-zero. */
uchar drawFlag; /* if true, run draw function, this will likely be replaced later */

uchar stack[16];
ushort stack[16];
uchar stackPointer;
uchar memory[4096]; /* 4 KB, font located at 0x8110 */

Expand Down

0 comments on commit 94d5ae5

Please sign in to comment.