Torlus / 6502.js

Cycle-accurate 6502 emulator in Javascript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

JSR and RTS are broken

fpgaminer opened this issue · comments

According to a 6502 programming manual (http://archive.6502.org/datasheets/synertek_programming_manual.pdf, page 106) JSR is supposed to push PC+2 (where PC points at the JSR instruction). In other words, the return address points to the byte before the next instruction. RTS is supposed to pop PC, increment PC, and then continue execution.

The current JSR/RTS implementation in this library pushes PC+3, so that the return address points at the next instruction, and RTS just pops PC.

This breaks, for example, MS-BASIC. MS-BASIC uses an address LUT for its VM. It looks up the BASIC VM opcode in an address LUT, pushes that address onto the stack, and then calls RTS. All the addresses in that table are -1, because MS-BASIC expects the 6502's RTS instruction to pop PC off the stack and increment it.

I had a heck of a time debugging that...

If you would like, I can fork and do a pull request to save you effort. However, it would be difficult for me to test the changes in my application, since I'm using a refactored version of your code (refactored into ASM.JS).

By the way, thank you for this resource. It's a really neat implementation of the 6502, and its cycle accuracy was useful in my application.

Thanks for this report. It has been fixed now.