On 1/6/2022 at 9:20 PM, rje said:
So I was looking at the opcode set for SWEET16, and reading the descriptions, and thinking about SUB versus CPR.
SUB does a twos-complement add. CPR does a binary subtract. The code in fact overlaps significantly:
***
SUB: LDY $0 ;RESULT TO R0
CPR: SEC ;NOTE Y REG = 13*2 FOR CPR
LDA R0L
SBC R0L,X
STA R0L,Y ;R0-RX TO RY
LDA R0H
SBC R0H,X
SUB2: STA R0H,Y
TYA ;LAST RESULT REG*2
ADC $0 ;CARRY TO LSB
STA R14H
RTS
***
So my question is this: maybe I can free up an opcode slot by not having SUB? Especially if CPR puts its result in R13, and is therefore accessible as a difference value.
Haven't thought it through fully, but it seems that the two values are related enough. Of course perhaps Woz did this because there wasn't another op that he wanted badly enough to get rid of SUB?
Because the purpose of Sweet16 is most often to write very compact "setup" type code ... you are not intended to use Sweet16 inside inner loops executed many times, but in one-off startup processes that would take much more space in 6502 binary code but since its only executed once, using Sweet16 doesn't have much runtime impact.
If you are USING the result of the subtraction, you will typically need the result in the accumulator ... even if you want the result somewhere else, you will need it in the accumulator before "putting it somewhere else", so will have to follow, eg, CPR R5 with LD R13, wasting a byte in your Sweet16 code for every subtract operation.
Plus, what other SINGLE "register" operation do you need? There are already three spare non-register ops, and any of those COULD be implemented with an operand that refers to one register ... or even two registers. For instance, you could SWAP two registers with the source register index in the low nybble of the operand byte and the destination register index in the high nybble, or multiply two registers with the 32bit result replacing the operands in a similar way. Or you could have shift left and shift right with the low nybble giving the register and the high nybble giving the number of shifts, from 0 to 15.
Edit: Actually, I may have convinced myself to substitute the extensions I have in the current source WITH those three ... a register swap and binary shift left and right.