!pr1
Note on the TXS instruction in the 65802...Bob Sander-Cederlof

Sandy Greenfarb wrote the other day that he had received a 65802 and plugged it into his Basis 108 with success.

He has been trying various permutations of the new opcodes and modes, and discovered some stones are better left unturned:

!lm+5
"The following programs should both print the letter "A" on the screen.  However, the one on the left works, while the one on the right hangs up the computer."

       Works           Hangs Up
       --------        --------
       CLC             CLC
       XCE             XCE
       LDA #"A         LDA #"A
       JSR $FDF0       JSR $FDED
       SEC             SEC
       XCE             XCE
       RTS             RTS

!lm-5
The only difference in the two programs is that the unsuccessful one weaves its way through DOS.  I looked at the DOS code it goes through, and at first glance it appears there should be NO PROBLEMS associated with executing all this code in 65802 mode, since both 16-bit modes are off.

However, for some reason it still hangs up.  Actually, it might not always hang:  it depends on what is in page zero at the corresponding position as the stack pointer in page one.

I do not know why, but the TXS instruction transfers the entire 16-bit value of X to S when you are in the 65802 mode, regardless of the status of the M and X bits.  Since M and X are both 1, the high byte of the X-register is 00.  Therefore the TXS instruction at $9FB9 in DOS clears the high byte of the S-register.  The RTS at $9FC4 then uses a return address from page zero, rather than page one.

I tried various experiments to see how TXS and TSX worked, and also examined TXA and TAX.  In my humble opinion, the 65802 is inconsistent here.  If you are in 65802 mode with M and X = 1, TXA does not modify the high byte of the A-register.  This is what I expect and what I want.  But TXS does modify the high byte of the S-register, contrary to my expectations.

Of course, as long as you know exactly how the chip works it really doesn't matter a lot.  The problems come when we ASSUME we know how it works, but are wrong.  The best antidote for these kind of assumptions, at least until a definitive reference manual for the chip is published, is trial and error.

I have had my 65802 for about six months now, and still have had no problems whatsoever with compatibility as long as I stay in normal 6502 mode.  If I leave it in 65802 and go charging through a program written for the 6502 mode, I expect I will run into trouble.
