;---------------------------------------------------------- ; IndirectSort Routine ;---------------------------------------------------------- ; ; Sorts the index table from Low to High ; ; (change the "bcc" commands to "bcs" ; to sort from High to Low) ; ; USES: ; IndexTable An array of indexes. Indexes for the DataArray ; DataTable An array with 16 bit data. The index table has indexes to this array ; ArrayLength A variable filled with the number of elements (not bytes) in the data array. ; DoMoreSort A flag which is set to "true" if more sorting is needed ; ;init index table ldx #$00 lda #$00 clc IS_nextIndexInit sta IS_IndexTable,x adc #$02 inx cpx #IS_ArrayLength bne IS_nextIndexInit IS_startOfSortPass ;init table index ldy #$00 ;init DoMoreSort flag lda #false sta IS_DoMoreSort IS_checkSwapOnMSB ;get y-pos (msb) ldx IS_IndexTable,y ;index to y-pos-table lda IS_DataTable,x ;load y pos (msb) ;compare element with next element (msb) ldx IS_IndexTable + 1,y ;index to y-pos-table cmp IS_DataTable,x ;compare y pos (msb) ;check which is higher beq IS_checkSwapOnLSB bcc IS_nextIndex ;carry is clear when the second is higher then the first ;swap indexes in index table lda IS_IndexTable,y sta IS_IndexTable + 1,y stx IS_IndexTable,y ;set DoMoreSort flag lda #true sta IS_DoMoreSort ;jump to next index jmp IS_nextIndex IS_checkSwapOnLSB ;get y-pos (lsb) ldx IS_IndexTable,y ;index to y-pos-table lda IS_DataTable + 1,x ;load y pos (lsb) ;compare element with next element (lsb) ldx IS_IndexTable + 1,y ;index to y-pos-table cmp IS_DataTable + 1,x ;compare y pos (lsb) ;check which is higher beq IS_nextIndex bcc IS_nextIndex ;carry is clear when the second is higher then the first ;swap indexes in index table lda IS_IndexTable,y sta IS_IndexTable + 1,y stx IS_IndexTable,y ;set DoMoreSort flag lda #true sta IS_DoMoreSort IS_nextIndex iny cpy #IS_ArrayLength - 1 bne IS_checkSwapOnMSB ;check if sort is ready lda IS_DoMoreSort cmp #true beq IS_startOfSortPass ;done rts
lda msb lsr lda lsb ror sta smallYPos