tepples wrote:You could use a LUT if the X register is free
Y is free, and it's convenient here because it's already the number of the byte.
This is what I have now:
Code: Select all
VramAdress = 0
Size = 0
BankNumber = 2
TileAdress = 4
start_vram_finder:
rep #$30 ; A=16, X/Y=16
sep #$20 ; A=8
stz 32x32ColumnSkipperCounter
ldx #$0000
ldy #$0000
cpx 32x32TileRequestCounter
beq 16x16_vram_finder_start
look_for_32x32_vram:
cpy #$0020 ;128, because there are 128 slots for sprites
beq vram_finder_done ;no space left for sprite
lda VramTable,y
beq 32x32_slot_found
iny
bra look_for_32x32_vram
32x32_slot_found:
lda #$FF
sta VramTable,y ;say that four of the slots are now taken
rep #$30 ; A=16, X/Y=16
lda VramAdressConverterTable,y
sta 32x32TileRequestTable,x
sep #$20 ; A=8
cpx 32x32TileRequestCounter
beq 16x16_vram_finder_start
inx
inx
inx
inx
inx
inx
bra look_for_32x32_vram
32x32_vram_finder_start:
ldx #$0000
ldy #$0000
cpx 32x32TileRequestCounter
beq 16x16_vram_finder_start
look_for_16x16_vram:
cpy #$0020 ;128, because there are 128 slots for sprites
beq vram_finder_done ;no space left for sprite
lda VramTable,y
bit #$11000000
bne 16x16_slot0_found ;there is space for another sprite
bit #$00110000
bne 16x16_slot1_found ;there is space for another sprite
bit #$00001100
bne 16x16_slot2_found ;there is space for another sprite
bit #$00000011
bne 16x16_slot3_found ;there is space for another sprite
iny
bra look_for_16x16_vram ;look again if the space is already occupied
16x16_slot0_found:
inc VramTable,y ;say that one of the slots is now taken
rep #$30 ; A=16, X/Y=16
lda VramAdressConverterTable,y
sta 16x16TileRequestTable,x
sep #$20 ; A=8
cpx 16x16TileRequestCounter
beq vram_finder_done
inx
inx
inx
inx
inx
inx
bra look_for_16x16_vram
16x16_slot1_found:
inc VramTable,y ;say that one of the slots is now taken
rep #$30 ; A=16, X/Y=16
lda VramAdressConverterTable,y
clc
adc #$0002
sta 16x16TileRequestTable,x
sep #$20 ; A=8
cpx 16x16TileRequestCounter
beq vram_finder_done
inx
inx
inx
inx
inx
inx
bra look_for_16x16_vram
16x16_slot2_found:
inc VramTable,y ;say that one of the slots is now taken
rep #$30 ; A=16, X/Y=16
lda VramAdressConverterTable,y
clc
adc #$0020
sta 16x16TileRequestTable,x
sep #$20 ; A=8
cpx 16x16TileRequestCounter
beq vram_finder_done
inx
inx
inx
inx
inx
inx
bra look_for_16x16_vram
16x16_slot3_found:
inc VramTable,y ;say that one of the slots is now taken
rep #$30 ; A=16, X/Y=16
lda VramAdressConverterTable,y
clc
adc #$0022
sta 16x16TileRequestTable,x
sep #$20 ; A=8
cpx 16x16TileRequestCounter
beq vram_finder_done
inx
inx
inx
inx
inx
inx
bra look_for_16x16_vram
vram_finder_done:
rts
VramAdressConverterTable:
.word 0,4,8,12,64,68,72,76,128,132,136,140,192,196,200,204
.word 256,260,264,268,320,324,328,332,384,388,392,396,448,452,456,460