TST Macro for 65xx compatible consoles - Is this right?

This is an archive of a topic from NESdev BBS, taken in mid-October 2019 before a server upgrade.
View original topic
TST Macro for 65xx compatible consoles - Is this right?
by on (#137040)
I'm working on a macro,

This is because I'm trying my hand at more macro writing for anyone to convert PC-Engine code to other 65xx systems, SNES especially!

Code:
;This take up two Zeropage RAM peices, so make sure there's free ram in ZeroPage!
.macro tst arg, ram
       lda ram
       and #arg
       sta tstval
       bit tstval
.endmacro


The question is: Am I doing it right?
Re: TST Macro for 65xx compatible consoles - Is this right?
by on (#137180)
HuC6280 opcode TST #ARG, EA is the following:
sta temp
lda EA
and #ARG
php
lda temp
plp

EA (effective address) is ZP, ZP+X, ABS, and ABS+X.

It sets the zero flag accordingly, but TST also sets bit #7 (N flag) and bit #6 (V flag) of the processor flag as well. Which, IIRC and according to my cribsheet, means bits 7 and 6 of the ARG is what will be in those corresponding flag bits (N and V). You can't use PHA/PLA because PLA sets bit #7 of the processor flag on most (all?) 65x processors.

Edit: Hmm, looks like even that code would work. AND doesn't set bit #6 (V) in the flag register.

Edit2:

If you know for a fact that the code you're replacing, with a macro, is not testing or branching on the N and V flags from the TST opcode, then you can get away with this:
sta temp
lda EA
and #ARG
php
lda temp
plp

But if it does use N/V, then you'll have to write a macro to extract that state and still keep Z bit state. Or replace the logic with some other code.

Edit 3: Hmm...

Probably use BIT opcode. I think would behave exactly the same as TST. According my cribsheet (though for 6280), Z results from BIT operation, and N/V are loaded from the operand of BIT. I assume this is the same behavior on other 65x processors. So...

sta temp
lda EA
bit #nn
php
lda temp
plp
Re: TST Macro for 65xx compatible consoles - Is this right?
by on (#137194)
BIT # works on 65C02 derivatives such as the HuC6280, but it's a 2-byte NOP (doesn't write back to flags) on the original 6502 and the 2A03/2A07.
Re: TST Macro for 65xx compatible consoles - Is this right?
by on (#137200)
Oh, that's true. On original versions and ones based on pre-65C02, missing that opcode:

sta temp1
lda #ARG
sta temp2
lda EA
bit temp2
php
lda temp1
plp

With #ARG and EA being passed directly to the macro, and two byte variables in ram (temp1 and temp2).
Re: TST Macro for 65xx compatible consoles - Is this right?
by on (#137211)
Thanks for helping me, I will give credit for your help!

I guess I did do it wrong, I was trying to read the opcode description out of a webpage, (of course it was inaccurate, didn't know where).