From 94d5ae52643affc829e80f020860278929006796 Mon Sep 17 00:00:00 2001 From: Eggbertx Date: Mon, 7 Oct 2024 11:08:32 -0700 Subject: [PATCH] Fix stack issues --- src/chip8.c | 8 +++----- src/chip8.h | 2 +- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/chip8.c b/src/chip8.c index 994361a..d059913 100755 --- a/src/chip8.c +++ b/src/chip8.c @@ -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); @@ -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; @@ -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: diff --git a/src/chip8.h b/src/chip8.h index b7fc8e0..fa626fd 100755 --- a/src/chip8.h +++ b/src/chip8.h @@ -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 */