From cf1bc4fad427052eae7ce4d9077715459d938e89 Mon Sep 17 00:00:00 2001 From: "Matthew D. Steele" Date: Fri, 7 Jan 2022 09:56:46 -0500 Subject: [PATCH] Fix Pop() implementation in src/sim65/paravirt.c (fixes #1625) The Pop() function was not handling stack pointer wrap around correctly. Also, change the simulated RTS implementation in ParaVirtHooks() to explicitly sequence the two Pop() calls in the correct order. --- src/sim65/paravirt.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/sim65/paravirt.c b/src/sim65/paravirt.c index e73bd3400..b3ec8fa37 100644 --- a/src/sim65/paravirt.c +++ b/src/sim65/paravirt.c @@ -105,7 +105,7 @@ static void SetAX (CPURegs* Regs, unsigned Val) static unsigned char Pop (CPURegs* Regs) { - return MemReadByte (0x0100 + ++Regs->SP); + return MemReadByte (0x0100 + (++Regs->SP & 0xFF)); } @@ -327,5 +327,7 @@ void ParaVirtHooks (CPURegs* Regs) Hooks[Regs->PC - PARAVIRT_BASE] (Regs); /* Simulate RTS */ - Regs->PC = Pop(Regs) + (Pop(Regs) << 8) + 1; + unsigned lo = Pop(Regs); + unsigned hi = Pop(Regs); + Regs->PC = lo + (hi << 8) + 1; }