Possible 6502 optimization
I am evaluating using separate lo/hi arrays for 16-bit values vs using 16-bit arrays directly. I saw this while trying to write TRSE code that could be better optimized, and it touches the subject of integer array accessing when using the same index for many things. Assume that newX, newY, ballX, ballY, ballDXInt and ballDYInt are integer arrays. I first tried this:
newX := ballX[ballNo] + ballDXInt[angle];
newY := ballY[ballNo] + ballDYInt[angle];
but saw that accessing arrays with different indexes generates index calculation four times. Then I rearranged the code like this:
newX := ballX[ballNo];
newY := ballY[ballNo];
newX += ballDXInt[angle];
newY += ballDYInt[angle];
and got this assembly code:
lda localVariable_Breakout_MoveBall_Breakout_ballNo
asl
tax
lda Breakout_ballX,x
ldy Breakout_ballX+1,x
; Calling storevariable on generic assign expression
sta localVariable_Breakout_MoveBall_Breakout_newX
; LineNumber: 172
; Load Integer array
; CAST type NADA
lda localVariable_Breakout_MoveBall_Breakout_ballNo asl tax
lda Breakout_ballY,x
ldy Breakout_ballY+1,x
; Calling storevariable on generic assign expression
sta localVariable_Breakout_MoveBall_Breakout_newY
; LineNumber: 173
; HandleVarBinopB16bit
; RHS is pure, optimization
; Load Integer array
; CAST type NADA
lda localVariable_Breakout_MoveBall_Breakout_angle
asl
tax
lda Breakout_ballDXInt,x
ldy Breakout_ballDXInt+1,x
clc
adc localVariable_Breakout_MoveBall_Breakout_newX
; Testing for byte: #0
; RHS is byte, optimization
bcc Breakout_MoveBall_skip90
iny
Breakout_MoveBall_skip90
; Calling storevariable on generic assign expression
sta localVariable_Breakout_MoveBall_Breakout_newX
; LineNumber: 174
; HandleVarBinopB16bit
; RHS is pure, optimization
; Load Integer array
; CAST type NADA
lda localVariable_Breakout_MoveBall_Breakout_angle asl tax
lda Breakout_ballDYInt,x
ldy Breakout_ballDYInt+1,x
clc
adc localVariable_Breakout_MoveBall_Breakout_newY
; Testing for byte: #0
; RHS is byte, optimization
bcc Breakout_MoveBall_skip92
iny
Breakout_MoveBall_skip92
; Calling storevariable on generic assign expression
sta localVariable_Breakout_MoveBall_Breakout_newY
I think that bolded instructions can be removed if somehow it could be detected that register used for array indexing did not change value (it looks to me that this type of detection could work only before ASM generation, but I may be wrong). I do not know if TRSE already have optimizations of this type, and if not, now tricky would be to implement something like this...