BigEd wrote:
Mulling over alternative CPU designs, it feels like the four flags are not equal in usefulness. The following is a thought experiment.
If you could have a CPU that's 10% faster, 10% cheaper, would you give up one of the four flags? Which one?
If I had a fixed function project that had certain timing to meet, I was within 10% of it but just couldn't get there, then goodbye V flag.
I don't think I'd want to lose any of the other flags as it would make all reference code incompatible, and make writing code more painful. That said, if I _had_ to choose:
- 3 flags: N, Z, C
- 2 flags: Z, C
- 1 flag: C
On the last one, the C flag alone gives me both multi-byte arithmetic and BCC, BCS, which should be the basis for a lot of derived functionality. But like the "One Instruction Set Computer" approaches, simple operations can explode without the right tools. Something as simple as branching if A == some_value becomes a challenge without Z. How would I do it?... Hmmm.. Ok, here's one idea:
This is what I'd like to do, but I can't because no Z:
Code:
LDA this_variable
CMP that_variable
BEQ this_that_equal
; not equal
...
this_that_equal:
; equal
...
So instead, I'm going to make a function called cmpz, which returns equality in the C flag. If C=1 then equal, else not equal. cmpz EORs the two variables and then uses the result to branch into a 16-entry table that, at the [0] index, sets the C flag, and otherwise clear the C flag. As that comprises two instructions (e.g. SEC; RTS or CLC; RTS), I'll do it in two steps - first checking bits [3:0] of the EOR result and then [7:4]. The original pattern only uses A, and preserves it, and the function pattern will do the same.
Code:
.MACRO CMPZ OP
PHA
LDA \OP
JSR fcmpz
PLA
.ENDMACRO
; client application
LDA this_variable
CMPZ that_variable
BCS this_that_equal
; not equal
...
this_that_equal:
; equal
...
fcmpz:
PHA
TXA
PHA
TSX ; X->[-, X, that_variable, ret_lo, ret_hi, this_variable]
LDA $102,X
EOR $105,X
AND #$0F
LSL
STA cmpzcall1 + 1
cmpzcall1:
JSR cmpztab
BCS cmpzend
LDA $102,X
EOR $105,X
AND #$F0
LSR
LSR
LSR
STA cmpzcall2 + 1
cmpzcall2:
JSR cmpztab
cmpzend:
PLA
TAX
PLA
RTS
.ALIGN 256
cmpztab:
SEC
RTS
.REPEAT 15
CLC
RTS
.ENDREPEAT
Ugh, that looks awful. Can someone with more current 6502 experience save me from my 30 year atrophy?