; bg update queue
; this is called once during blank
- ; and updates a fixed amount of tiles
- ; from a circular buffer
- ; the buffer can hold up to 255 items and wraps around
+ ; it processess all tile updates
+ ; until the bg_update_index is 0
bg_update_queue_process:
+ ld hl, bg_update_index
+
+ ; read index
+ ld a, [bg_update_index]
+ ld e, a
+ ld a, [bg_update_index+1]
+ ld d, a
+
+
+ ; skip if index is already 0
+ ld a, d
+ xor a, e
+ jp z, @done
+
+ ld a, BG_UPDATE_MAX
+@loop:
+ push af
+ ; index--
+ dec de
+
+ ; write to vram
+ ld hl, bg_update_queue
+ add hl, de ; hl = next index to process
+
+ ld a, [hl+]
+ ld c, a
+ ld a, [hl+]
+ ld b, a
+ ; bc = vram address
+
+ ld a, [hl]
+ ld [bc], a
+
+
+ ; a--
+ pop af
+ dec a
+
+ ld b, a ; save a for after
+ cp a, 0
+ jr z, @done REL
+
+ ld a, d
+ xor a, e
+ jr z, @done REL
+
+ ld a, b ; restore a
+ jr @loop REL
+
+
+@done:
+ ; write index
+ ld a, e
+ ld [bg_update_index], a
+ ld a, d
+ ld [bg_update_index+1], a
+
ret
+
+
; pushes a new bg update to the queue
; inputs:
; hl: ptr to tile
inc de ; offset += bgu_size
inc de
inc de
+
ld a, e
ld [bg_update_index], a
ld a, d