Please tell me about flag changes in 16-bit operations on the MC6800.
I am writing a compiler for the Tiny language for the old MC6800 CPU.
This language handles 16-bit numbers, so there are many 16-bit comparisons. Executing this using only AccAB will make the program longer and slower.
Fortunately, the MC6800 has the X register, so I would like to optimize the program using the results (flag changes) of the CPX/LDX/STX instructions as well.
The N, Z, and V flags are affected by the MC6800 CPX/LDX/STX instructions. As is well known, the only correct result of a 16-bit comparison is the Z flag. N and V only reflect the results of the upper 8-bit comparison.
For this reason, most old programs branched (BEQ or BNE) by looking only at the Z flag.
Even if only the upper 8 bits are compared, there are cases where flag changes can be effectively utilized. If the result of CPX #0 is N=1, then X<0, and if N=0, then X>=0. The same applies to LDX/STX.
(If my understanding is incorrect, I would appreciate it if you could point it out.)
Also, in some MC6800 emulators, the changes in the CCR flag as a result of these instructions are incorrect. The execution results of multiple emulators do not match. This is because the flag changes in MC6800 16-bit comparisons are difficult to understand, making it difficult to implement correctly.
I am looking at M6800 Microprocessor Applications Manual (1975), p.1-19. I also looked at Hitachi's manual, but there is no big difference. In this manual, the N flag change of the CPX instruction is written as follows.
(Bit N) Test: Sign bit of most significant (MS) byte of result = I?
I think it is difficult to understand this is an 8-bit comparison. Of course, this description is different from LDX/STX, so we can guess that something strange is going on.
(Bit N)Test: Result less than zero? (Bit 15 = 1)
I would like a document that clearly explains these differences.
Question: Are there any articles that clearly explain the flag changes of MC6800 CPX/LDX/STX instructions? From what I have found, all the articles use the Z flag. I have not found any that use the remaining N and V flags.