6502 Advanced Tips

From 8BitDev.org - Atari 7800 Development Wiki
Revision as of 19:52, 22 December 2015 by MSaarna (talk | contribs) (Created page with "==jump table instead of comparison branch chains== When you need to choose between 4 or more destinations, it's more efficient to use a jump table instead of a sequence of CM...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

jump table instead of comparison branch chains

When you need to choose between 4 or more destinations, it's more efficient to use a jump table instead of a sequence of CMP and BEQ. Here's a jump table example that uses RTS instead of JMP (ADDRESS) to save even more ROM and cycles...


  LDX FunctionIndex
  LDA FunctionJumptablehi,x
  PHA
  LDA FunctionJumptablelo,x
  PHA
  RTS
FunctionJumptablehi
  .byte >(ClearRam-1)
  .byte >(SetRam-1)
  .byte >(MemCpy-1)
  .byte >(MemCmp-1)
FunctionJumptablelo
  .byte <(ClearRam-1)
  .byte <(SetRam-1)
  .byte <(MemCpy-1)
  .byte <(MemCmp-1)


fast range test

For all of these we assume that the byte to be tested is in A and that the start and end values, n and m, are already defined. Also that 0 < n < m < $FF.

If you don't need to preserve the byte in A then testing the byte can be done in five bytes and only six cycles. This sets the carry if A is in the range n to m.


CLC		; clear carry for add
ADC	#$FF-m	; make m = $FF
ADC	#m-n+1	; carry set if in range n to m

Courtesy Lee Davidson. [1]

References

  1. Lee Davidson's 6502 Shorts