barrym95838 wrote:
I'm pretty sure that RTI expects the exact return address on the stack
Yup. And -- compared with RTI -- RTS does have a different behavior (it expects to find the address
minus one on stack). Subroutine details are interesting, but IMO they don't teach us much about BRK issues. Instead I'll offer the following useful though unsubstantiated narrative.
I believe the 65xx designers' original goal was for BRK to be truly just a simple, one-byte instruction. Managing without any signature byte was not an impediment. It wasn't part of their plan, and they didn't need it -- they were still able to satisfy their goals in regard to software debugging, and patching fuse-based PROMs.
Moreover, with BRK being just a simple, one-byte instruction, RTI would be its perfect complement. IOW, if the BRK handler needed to return, it could do so simply by using an RTI. But, as implemented, BRK does
not play nicely with RTI, and if the BRK handler needs to return then it's obliged to include extra code to alter what's on stack before the RTI can be performed.
Under pressure for time, they had to either fix the bug or make it a permanent part of the specification. So.... the signature byte got re-cast it as a
feature!
Edit: yes MOS was forced to choose between altering the behavior vs. endorsing it and making it permanent. But I need to correct what I said about adjusting what's on stack before the RTI can be performed. That's not necessary if one embraces the as-is behavior and accepts BRK as a 2-byte instruction (albeit with the second byte devoid of any defined function).
To be clear, I don't deny that clever uses can be found for the signature byte. But I believe its existence is purely an accident. I don't know if my take on the designers' intentions can be verified. But at the very least it's an explanation that's coherent... and it's also one that's helpful when it comes to trying to remember BRK's behavioral quirks.
-- Jeff
PS -- regarding quirks, the Eyes & Lichty manual wrote:
Although BRK is a one-byte instruction, the program counter (which is pushed onto the stack by the
instruction) is incremented by two; this lets you follow the break instruction with a one-byte signature byte
indicating which break caused the interrupt. Even if a signature byte is not needed, either the byte following the
BRK instruction must be padded with some value or the break-handling routine must decrement the return
address on the stack to let an RTI (return from interrupt) instruction executed correctly.
_________________
In 1988 my 65C02 got six new registers and 44 new full-speed instructions!
https://laughtonelectronics.com/Arcana/ ... mmary.html