I'm quite excited about this: I had the idea to probe a possible side-effect of perhaps the most interesting 6502 instruction, JSR.
And I got a result! My program runs one way on a real 6502, and another way on, I think, most emulators. And those emulators do the obvious thing, which is to say, they do what most people expect them to do. And so the result should be modestly surprising to most people here - which makes it interesting. (I expect most hardware re-implementations might also do the obvious thing, but haven't yet tested any. Not the MOnSter 6502 obviously.)
The program is in BBC Basic, but the motivated experimenter won't find that an obstacle.
On a BBC Master, which has a 65C102 in it:
Code:
>L.
10 DIM code 99:P%=code
20 [ SEI:TSX:STX&70:LDX#0:TXS:JMP&00FE:]
30 P%=&FE: REM here's the vital code to test
40 [ JSR &4321:]
50 P%=&121: REM real 6502 will land here
60 [ LDX&70: TXS: LDA #ASC("P"):CLI:RTS: ]
70 P%=&4321:REM incorrect emulator will land here
80 [ LDX&70: TXS: LDA #ASC("F"):CLI:RTS: ]
90 PRINT CHR$(USR code)
>SA."jsrtest"
>RUN
0F48 78 SEI
0F49 BA TSX
0F4A 86 70 STX&70
0F4C A2 00 LDX#0
0F4E 9A TXS
0F4F 4C FE 00 JMP&00FE
00FE 20 21 43 JSR &4321
0121 A6 70 LDX&70
0123 9A TXS
0124 A9 50 LDA #ASC("P")
0126 58 CLI
0127 60 RTS
4321 A6 70 LDX&70
4323 9A TXS
4324 A9 46 LDA #ASC("F")
4326 58 CLI
4327 60 RTS
P
>
Edit: see
below for an improved version
At the time of writing, in owlet, which uses jsbeeb, an unusually high-fidelity emulation, the program prints F. Likewise in two of the three 6502 models in PiTubeDirect.
(To my slight surprise, the lib6502 model in PiTubeDirect prints a P, which doesn't match what I think lib6502 is doing, but this is probably my failure of understanding.)
What is that I'm testing, you ask? It's that JSR has two operand bytes, and it has to do two stack pushes, and in a real 6502 the sequence is that the first operand byte is read, then the two stack pushes happen, and then the second operand byte is read. So the test is to arrange that the second operand byte is overwritten by the stack. Truly self-modifying code.
There might be more to explore, both in the test program and in the emulators and re-implementations which we might be interested in.
Edit: oops, Mike (barrym95838) reminds us that he sketched exactly this kind of test only last year. I was there. And I'd forgotten.