recomment bg map update functions

This commit is contained in:
yenatch 2013-08-31 21:13:05 -04:00
parent 2bae1797c4
commit 0a821fe964
1 changed files with 164 additions and 200 deletions

364
main.asm
View File

@ -141,153 +141,120 @@ INCLUDE "engine/text.asm"
DMATransfer: ; 15d8 DMATransfer: ; 15d8
; DMA transfer ; Return carry if the transfer is completed.
; return carry if successful
; anything to transfer?
ld a, [hDMATransfer] ld a, [hDMATransfer]
and a and a
ret z ret z
; start transfer
; Start transfer
ld [rHDMA5], a ld [rHDMA5], a
; indicate that transfer has occurred
; Execution is halted until the transfer is complete.
xor a xor a
ld [hDMATransfer], a ld [hDMATransfer], a
; successful transfer
scf scf
ret ret
; 15e3 ; 15e3
UpdateBGMapBuffer: ; 15e3 UpdateBGMapBuffer: ; 15e3
; write [$ffdc] 16x8 tiles from BGMapBuffer to bg map addresses in BGMapBufferPtrs ; Copy [$ffdc] 16x8 tiles from BGMapBuffer
; [$ffdc] must be even since this is done in 16x16 blocks ; to bg map addresses in BGMapBufferPtrs.
; return carry if successful ; [$ffdc] must be even since this is done in pairs.
; Return carry on success.
; any tiles to update?
ld a, [hBGMapUpdate] ld a, [hBGMapUpdate]
and a and a
ret z ret z
; save wram bank
ld a, [rVBK] ld a, [rVBK]
push af push af
; save sp
ld [hSPBuffer], sp ld [hSPBuffer], sp
; temp stack
ld hl, BGMapBufferPtrs ld hl, BGMapBufferPtrs
ld sp, hl ld sp, hl
; we can now pop the addresses of affected spots in bg map
; We can now pop the addresses of affected spots on the BG Map
; get pal and tile buffers
ld hl, BGMapPalBuffer ld hl, BGMapPalBuffer
ld de, BGMapBuffer ld de, BGMapBuffer
.loop
; draw one 16x16 block
; top half: .next
; Copy a pair of 16x8 blocks (one 16x16 block)
; get bg map address rept 2
; Get our BG Map address
pop bc pop bc
; update palettes
ld a, $1 ; Palettes
ld a, 1
ld [rVBK], a ld [rVBK], a
; tile 1
ld a, [hli] ld a, [hli]
ld [bc], a ld [bc], a
inc c inc c
; tile 2
ld a, [hli] ld a, [hli]
ld [bc], a ld [bc], a
dec c dec c
; update tiles
ld a, $0
ld [rVBK], a
; tile 1
ld a, [de]
inc de
ld [bc], a
inc c
; tile 2
ld a, [de]
inc de
ld [bc], a
; bottom half:
; get bg map address ; Tiles
pop bc ld a, 0
; update palettes
ld a, $1
ld [rVBK], a ld [rVBK], a
; tile 1
ld a, [hli]
ld [bc], a
inc c
; tile 2
ld a, [hli]
ld [bc], a
dec c
; update tiles
ld a, $0
ld [rVBK], a
; tile 1
ld a, [de] ld a, [de]
inc de inc de
ld [bc], a ld [bc], a
inc c inc c
; tile 2
ld a, [de] ld a, [de]
inc de inc de
ld [bc], a ld [bc], a
endr
; we've done 2 16x8 blocks
; We've done 2 16x8 blocks
ld a, [$ffdc] ld a, [$ffdc]
dec a dec a
dec a dec a
ld [$ffdc], a ld [$ffdc], a
; if there are more left, get the next 16x16 block jr nz, .next
jr nz, .loop
; restore sp
ld a, [hSPBuffer] ld a, [hSPBuffer]
ld l, a ld l, a
ld a, [$ffda] ld a, [hSPBuffer + 1]
ld h, a ld h, a
ld sp, hl ld sp, hl
; restore vram bank
pop af pop af
ld [rVBK], a ld [rVBK], a
; we don't need to update bg map until new tiles are loaded
xor a xor a
ld [hBGMapUpdate], a ld [hBGMapUpdate], a
; successfully updated bg map
scf scf
ret ret
; 163a ; 163a
WaitTop: ; 163a WaitTop: ; 163a
; Wait until the top third of the BG Map is being updated.
ld a, [hBGMapMode] ld a, [hBGMapMode]
and a and a
ret z ret z
; wait until top third of bg map can be updated
ld a, [hBGMapThird] ld a, [hBGMapThird]
and a and a
jr z, .quit jr z, .done
call DelayFrame call DelayFrame
jr WaitTop jr WaitTop
.quit .done
xor a xor a
ld [hBGMapMode], a ld [hBGMapMode], a
ret ret
@ -295,133 +262,141 @@ WaitTop: ; 163a
UpdateBGMap: ; 164c UpdateBGMap: ; 164c
; get mode ; Update the BG Map, in thirds, from TileMap and AttrMap.
ld a, [hBGMapMode] ld a, [hBGMapMode]
and a and a
ret z ret z
; don't save bg map address ; BG Map 0
dec a ; 1 dec a ; 1
jr z, .tiles jr z, .Tiles
dec a ; 2 dec a ; 2
jr z, .attr jr z, .Attr
dec a ; ?
; BG Map 1
; save bg map address dec a
ld a, [hBGMapAddress] ld a, [hBGMapAddress]
ld l, a ld l, a
ld a, [$ffd7] ld a, [hBGMapAddress + 1]
ld h, a ld h, a
push hl push hl
; bg map 1 (VBGMap1)
xor a xor a
ld [hBGMapAddress], a ld [hBGMapAddress], a
ld a, $9c ld a, VBGMap1 >> 8
ld [$ffd7], a ld [hBGMapAddress + 1], a
; get mode again
ld a, [hBGMapMode] ld a, [hBGMapMode]
push af push af
cp 3 cp 3
call z, .tiles call z, .Tiles
pop af pop af
cp 4 cp 4
call z, .attr call z, .Attr
; restore bg map address
pop hl pop hl
ld a, l ld a, l
ld [hBGMapAddress], a ld [hBGMapAddress], a
ld a, h ld a, h
ld [$ffd7], a ld [hBGMapAddress + 1], a
ret ret
.attr
; switch vram banks .Attr
ld a, 1 ld a, 1
ld [rVBK], a ld [rVBK], a
; bg map 1
ld hl, AttrMap ld hl, AttrMap
call .getthird call .update
; restore vram bank
ld a, 0 ld a, 0
ld [rVBK], a ld [rVBK], a
ret ret
.tiles
; bg map 0 .Tiles
ld hl, TileMap ld hl, TileMap
.getthird
; save sp .update
ld [hSPBuffer], sp ld [hSPBuffer], sp
; # tiles to move down * 6 (which third?) ; Which third?
ld a, [hBGMapThird] ld a, [hBGMapThird]
and a ; 0 and a ; 0
jr z, .top jr z, .top
dec a ; 1 dec a ; 1
jr z, .middle jr z, .middle
; 2
; .bottom ; 2
; move 12 tiles down THIRD_HEIGHT EQU SCREEN_HEIGHT / 3
ld de, $00f0 ; TileMap(0,12) - TileMap
.bottom
ld de, 2 * THIRD_HEIGHT * SCREEN_WIDTH
add hl, de add hl, de
; stack now points to source
ld sp, hl ld sp, hl
; get bg map address
ld a, [$ffd7] ld a, [hBGMapAddress + 1]
ld h, a ld h, a
ld a, [hBGMapAddress] ld a, [hBGMapAddress]
ld l, a ld l, a
; move 12 tiles down
ld de, $0180 ; bgm(0,12) ld de, 2 * THIRD_HEIGHT * BG_MAP_WIDTH
add hl, de add hl, de
; start at top next time
; Next time: top third
xor a xor a
jr .start jr .start
.middle .middle
; move 6 tiles down ld de, THIRD_HEIGHT * SCREEN_WIDTH
ld de, $0078 ; TileMap(0,6) - TileMap
add hl, de add hl, de
; stack now points to source
ld sp, hl ld sp, hl
; get bg map address
ld a, [$ffd7] ld a, [hBGMapAddress + 1]
ld h, a ld h, a
ld a, [hBGMapAddress] ld a, [hBGMapAddress]
ld l, a ld l, a
; move 6 tiles down
ld de, $00c0 ; bgm(0,6) ld de, THIRD_HEIGHT * BG_MAP_WIDTH
add hl, de add hl, de
; start at bottom next time
; Next time: bottom third
ld a, 2 ld a, 2
jr .start jr .start
.top .top
; stack now points to source
ld sp, hl ld sp, hl
; get bg map address
ld a, [$ffd7] ld a, [hBGMapAddress + 1]
ld h, a ld h, a
ld a, [hBGMapAddress] ld a, [hBGMapAddress]
ld l, a ld l, a
; start at middle next time
; Next time: middle third
ld a, 1 ld a, 1
.start .start
; which third to draw next update ; Which third to update next time
ld [hBGMapThird], a ld [hBGMapThird], a
; # rows per third
ld a, 6 ; SCREEN_HEIGHT / 3 ; Rows of tiles in a third
; # tiles from the edge of the screen to the next row ld a, SCREEN_HEIGHT / 3
ld bc, $000d ; BG_WIDTH + 1 - SCREEN_WIDTH
; Discrepancy between TileMap and BGMap
ld bc, BG_MAP_WIDTH - (SCREEN_WIDTH - 1)
.row .row
; write a row of 20 tiles ; Copy a row of 20 tiles
rept 9 rept SCREEN_WIDTH / 2 - 1
pop de pop de
ld [hl], e ld [hl], e
inc l inc l
@ -432,16 +407,15 @@ endr
ld [hl], e ld [hl], e
inc l inc l
ld [hl], d ld [hl], d
; next row
add hl, bc add hl, bc
; done?
dec a dec a
jr nz, .row jr nz, .row
; restore sp
ld a, [hSPBuffer] ld a, [hSPBuffer]
ld l, a ld l, a
ld a, [$ffda] ld a, [hSPBuffer + 1]
ld h, a ld h, a
ld sp, hl ld sp, hl
ret ret
@ -449,47 +423,45 @@ endr
SafeLoadTiles2: ; 170a SafeLoadTiles2: ; 170a
; only execute during first fifth of vblank ; Only call during the first fifth of VBlank
; any tiles to draw?
ld a, [$cf6c] ld a, [$cf6c]
and a and a
ret z ret z
; abort if too far into vblank
; Back out if we're too far into VBlank
ld a, [rLY] ld a, [rLY]
; ly = 144-145?
cp 144 cp 144
ret c ret c
cp 146 cp 146
ret nc ret nc
GetTiles2: ; 1717 GetTiles2: ; 1717
; load [$cf6c] tiles from [$cf6d-e] to [$cf6f-70] ; Copy [$cf6c] 1bpp tiles from [$cf6d-e] to [$cf6f-70]
; save sp
ld [hSPBuffer], sp ld [hSPBuffer], sp
; sp = [$cf6d-e] tile source ; Source
ld hl, $cf6d ld hl, $cf6d
ld a, [hli] ld a, [hli]
ld h, [hl] ld h, [hl]
ld l, a ld l, a
ld sp, hl ld sp, hl
; hl = [$cf6f-70] tile dest ; Destination
ld hl, $cf6f ld hl, $cf6f
ld a, [hli] ld a, [hli]
ld h, [hl] ld h, [hl]
ld l, a ld l, a
; # tiles to draw ; # tiles to copy
ld a, [$cf6c] ld a, [$cf6c]
ld b, a ld b, a
; clear tile queue
xor a xor a
ld [$cf6c], a ld [$cf6c], a
.loop .next
; put 1 tile (16 bytes) into hl from sp
rept 3 rept 3
pop de pop de
@ -510,25 +482,22 @@ endr
ld [hl], d ld [hl], d
inc l inc l
ld [hl], d ld [hl], d
; next tile
inc hl inc hl
; done?
dec b dec b
jr nz, .loop jr nz, .next
; update $cf6f-70
ld a, l ld a, l
ld [$cf6f], a ld [$cf6f], a
ld a, h ld a, h
ld [$cf70], a ld [$cf70], a
; update $cf6d-e
ld [$cf6d], sp ld [$cf6d], sp
; restore sp
ld a, [hSPBuffer] ld a, [hSPBuffer]
ld l, a ld l, a
ld a, [$ffda] ld a, [hSPBuffer + 1]
ld h, a ld h, a
ld sp, hl ld sp, hl
ret ret
@ -536,56 +505,54 @@ endr
SafeLoadTiles: ; 1769 SafeLoadTiles: ; 1769
; only execute during first fifth of vblank ; Only call during the first fifth of VBlank
; any tiles to draw?
ld a, [$cf67] ld a, [$cf67]
and a and a
ret z ret z
; abort if too far into vblank
; Back out if we're too far into VBlank
ld a, [rLY] ld a, [rLY]
; ly = 144-145?
cp 144 cp 144
ret c ret c
cp 146 cp 146
ret nc ret nc
jr GetTiles jr GetTiles
LoadTiles: ; 1778 LoadTiles: ; 1778
; use only if time is allotted
; any tiles to draw?
ld a, [$cf67] ld a, [$cf67]
and a and a
ret z ret z
; get tiles
GetTiles: ; 177d
; load [$cf67] tiles from [$cf68-9] to [$cf6a-b]
; save sp GetTiles: ; 177d
; Copy [$cf67] 2bpp tiles from [$cf68-9] to [$cf6a-b]
ld [hSPBuffer], sp ld [hSPBuffer], sp
; sp = [$cf68-9] tile source ; Source
ld hl, $cf68 ld hl, $cf68
ld a, [hli] ld a, [hli]
ld h, [hl] ld h, [hl]
ld l, a ld l, a
ld sp, hl ld sp, hl
; hl = [$cf6a-b] tile dest ; Destination
ld hl, $cf6a ld hl, $cf6a
ld a, [hli] ld a, [hli]
ld h, [hl] ld h, [hl]
ld l, a ld l, a
; # tiles to draw ; # tiles to copy
ld a, [$cf67] ld a, [$cf67]
ld b, a ld b, a
; clear tile queue
xor a xor a
ld [$cf67], a ld [$cf67], a
.loop .next
; put 1 tile (16 bytes) into hl from sp
rept 7 rept 7
pop de pop de
ld [hl], e ld [hl], e
@ -597,25 +564,22 @@ endr
ld [hl], e ld [hl], e
inc l inc l
ld [hl], d ld [hl], d
; next tile
inc hl inc hl
; done?
dec b dec b
jr nz, .loop jr nz, .next
; update $cf6a-b
ld a, l ld a, l
ld [$cf6a], a ld [$cf6a], a
ld a, h ld a, h
ld [$cf6b], a ld [$cf6b], a
; update $cf68-9
ld [$cf68], sp ld [$cf68], sp
; restore sp
ld a, [hSPBuffer] ld a, [hSPBuffer]
ld l, a ld l, a
ld a, [$ffda] ld a, [hSPBuffer + 1]
ld h, a ld h, a
ld sp, hl ld sp, hl
ret ret