I've been toying around with the 6-instruction, 8-byte, 12-cycle nybble swap code found at
http://6502.org/source/general/SWN.html and I've been intrigued by the genius of this technique. I was curious if there were any other uses for this or a similar sort of pattern. Like what if you ADC different values, or used SBC instead, or shifted to the right instead of to the left. So far, I almost came up empty handed.
I did find that using SBC #$7F gives the exact same result as the ADC #$80, even the resulting overflow and carry flags are exactly the same. In addition, I found that the overflow always matches bit 0 of the result, and the carry is always the opposite. So if the result from the swap is even, the overflow will be 0 and carry will be 1. If odd, then overflow is 1 and carry is 0. I don't think this info would be of any use to anyone, but I though it was interesting enough and worth mentioning.
I have observe the flag results by making a loop, running every possible value through it, and saving both flag results for inspection. Here's my code if anyone wants to see it. The offset from location $6000 represents the resulting values from the swaps. The values of those locations represent the 2 flags: overflow (high nibble) and carry (low nibble).
Code:
.ORG $0300
LDX #$00 ;Counter
Loop
TXA ;Source bytes
ASL
ADC #$80 ;or SBC #$7F
ROL
ASL
ADC #$80 ;or SBC #$7F
ROL
TAY ;Use result byte as index
BVS V_Set
LDA #$00 ; bit3=v:clear
.byte $2C ;BIT instr to skip next LDA
V_Set
LDA #$08 ; bit3=v:set
ROL ;Flags bit4=v, bit0=c
STA $6000,Y
INX
BNE Loop
RTS
Assembled with:
https://www.masswerk.at/6502/assembler.htmlCode tested in: AppleWin
You can paste this into your emulator.
Warning: Screen will scroll!300:A2 00 8A 0A 69 80 2A 0A 69 80 2A A8 70 03 A9 00 2C A9 08 2A 99 00 60 E8 D0 E8 60 N 300G N 6000.60FF
ADC,ADC - 304:69 80 N 308:69 80 N 300G N 6000.60FF
SBC,SBC - 304:E9 7F N 308:E9 7F N 300G N 6000.60FF
ADC,SBC - 304:69 80 N 308:E9 7F N 300G N 6000.60FF
SBC,ADC - 304:E9 7F N 308:69 80 N 300G N 6000.60FF