I have a collection of 28 actors. I want to sort them by their y value. Since the actors are each 8 bytes, I figured it would be faster to sort an array of indexes to the actors. This is an implementation of the insertion sort routine.
I'm posting this question because this is my first NES project, I'm still getting used to the limitations of the 6502 (particularly all the addressing modes!), and I'm hoping that any insights people could offer could speed up all the coding I do in the future. Can anyone think of a way that I could speed up this code?
The vast majority of the time in the sorting routine is spent doing the following (at label '_jSort'):
1. y = the current position in the collection to be sorted.
2. save y
3. a = the index of the actor at the current position in the collection.
4. transfer a to y
5. a = the y value of the actor at the index in y.
>>> my comparison and swap code happens here
6. restore y to the saved value (restoring it to the current position in the collection to be sorted).
7. Decrement y.
8. loop to 1.
This is the complete routine. Any thoughts how I could optimize this code?
I'm posting this question because this is my first NES project, I'm still getting used to the limitations of the 6502 (particularly all the addressing modes!), and I'm hoping that any insights people could offer could speed up all the coding I do in the future. Can anyone think of a way that I could speed up this code?
The vast majority of the time in the sorting routine is spent doing the following (at label '_jSort'):
1. y = the current position in the collection to be sorted.
2. save y
3. a = the index of the actor at the current position in the collection.
4. transfer a to y
5. a = the y value of the actor at the index in y.
>>> my comparison and swap code happens here
6. restore y to the saved value (restoring it to the current position in the collection to be sorted).
7. Decrement y.
8. loop to 1.
This is the complete routine. Any thoughts how I could optimize this code?
Code:
; -------------------------------- [ SortActors ] --------------------------------
; In: A is the superchunk index (0-3) that contains the list of Static Actors we are sorting.
; Notes: Uses $00-$05 in zp.
SortActors:
.scope
.alias _PointerSort $00
.alias _PointerYVals $02
.alias _key $04
.alias _y $05
and #$03 ; clip A to $00-$03
`add $03 ; a = a + 3
sta [_PointerSort+1] ; _PointerSort = ptr to sort array.
sta [_PointerYVals+1] ; _PointerYVals = to y-values.
lda #StaticSortArray
sta _PointerSort
lda #StaticsY
sta _PointerYVals
; insertion sort
ldx #01
_iSort:
txa ; y = x
tay ;
lda (_PointerSort),y ; a = actor[x]
sty _y ;
tay ;
pha ; push a (index pointed to by x)
lda (_PointerYVals),y ; a = actor[x].y
sta _key ; key = actor[x].y
ldy _y
dey ; y = x - 1
_jSort: ; while (key < actor[y].y && y >= 0) {
lda (_PointerSort),y ; ; a = actor[y].y
sty _y ; ; _y = y
tay ;
lda (_PointerYVals),y ;
cmp _key ;
ldy _y ; ; y = _y
bcc _iNext ;
lda (_PointerSort),y ;
iny ; actor[y+1] = actor[y]
sta (_PointerSort),y ;
dey ;
dey ; y = y - 1
bpl _jSort ; }
_iNext:
iny ; actor[y+1] = actor[x]
pla ; pull a (index pointed to by x)
sta (_PointerSort),y ;
inx
cpx #StaticCount
bne _iSort
rts
; In: A is the superchunk index (0-3) that contains the list of Static Actors we are sorting.
; Notes: Uses $00-$05 in zp.
SortActors:
.scope
.alias _PointerSort $00
.alias _PointerYVals $02
.alias _key $04
.alias _y $05
and #$03 ; clip A to $00-$03
`add $03 ; a = a + 3
sta [_PointerSort+1] ; _PointerSort = ptr to sort array.
sta [_PointerYVals+1] ; _PointerYVals = to y-values.
lda #StaticSortArray
sta _PointerSort
lda #StaticsY
sta _PointerYVals
; insertion sort
ldx #01
_iSort:
txa ; y = x
tay ;
lda (_PointerSort),y ; a = actor[x]
sty _y ;
tay ;
pha ; push a (index pointed to by x)
lda (_PointerYVals),y ; a = actor[x].y
sta _key ; key = actor[x].y
ldy _y
dey ; y = x - 1
_jSort: ; while (key < actor[y].y && y >= 0) {
lda (_PointerSort),y ; ; a = actor[y].y
sty _y ; ; _y = y
tay ;
lda (_PointerYVals),y ;
cmp _key ;
ldy _y ; ; y = _y
bcc _iNext ;
lda (_PointerSort),y ;
iny ; actor[y+1] = actor[y]
sta (_PointerSort),y ;
dey ;
dey ; y = y - 1
bpl _jSort ; }
_iNext:
iny ; actor[y+1] = actor[x]
pla ; pull a (index pointed to by x)
sta (_PointerSort),y ;
inx
cpx #StaticCount
bne _iSort
rts