
.include "vcs-ca65.inc"
.importzp tmp1, tmp2
.import _reset_sprites
.import Return

.export _bitmap48_kernel
.export _bitmap48_setup
.export _bitmap48_setaddress
.export _bitmap48_setheight
.export _bitmap48_ptr_0, _bitmap48_ptr_1, _bitmap48_ptr_2, _bitmap48_ptr_3, _bitmap48_ptr_4, _bitmap48_ptr_5

LoopCount = tmp1
Temp = tmp2
FontBuf = $1000 ;unused

; when the first STA GRP1 of the sequence happens
.define B48_CYCLE 42

; this will get loaded into RAM
; so we can change the offsets
.segment "XDATA"

; Display the resulting 48x5 bitmap from FontBuf
_bitmap48_kernel:
	sta WSYNC
        sta LoopCount
        dec LoopCount
b48loop:
	ldy LoopCount	; counts backwards
        sta WSYNC	; sync to next scanline
_bitmap48_ptr_0 = *+1
        lda FontBuf+0,y	; load B0 (1st sprite byte)
        sta GRP0	; B0 -> [GRP0]
_bitmap48_ptr_1 = *+1
        lda FontBuf+5,y	; load B1 -> A
        sta GRP1	; B1 -> [GRP1], B0 -> GRP0
_bitmap48_ptr_2 = *+1
        lda FontBuf+10,y	; load B2 -> A
        sta GRP0	; B2 -> [GRP0], B1 -> GRP1
_bitmap48_ptr_5 = *+1
        lda FontBuf+25,y	; load B5 -> A
        sta Temp	; B5 -> temp
_bitmap48_ptr_4 = *+1
        ldx FontBuf+20,y	; load B4 -> X
_bitmap48_ptr_3 = *+1
        lda FontBuf+15,y	; load B3 -> A
        ldy Temp	; load B5 -> Y
        sta GRP1	; B3 -> [GRP1]; B2 -> GRP0
        stx GRP0	; B4 -> [GRP0]; B3 -> GRP1
        sty GRP1	; B5 -> [GRP1]; B4 -> GRP0
        sta GRP0	; ?? -> [GRP0]; B5 -> GRP1
b48dec:
        dec LoopCount	; go to next line
	bpl b48loop	; repeat until < 0
        jmp _reset_sprites

; only used by _bitmap48_setaddress
_bitmap48_height: .byte 8

; TODO: can we write even when RAM not selected?
.proc _bitmap48_setaddress
	sta _bitmap48_ptr_0+$400+0
        stx _bitmap48_ptr_0+$400+1
        jsr add_height_ax
	sta _bitmap48_ptr_1+$400+0
        stx _bitmap48_ptr_1+$400+1
        jsr add_height_ax
	sta _bitmap48_ptr_2+$400+0
        stx _bitmap48_ptr_2+$400+1
        jsr add_height_ax
	sta _bitmap48_ptr_3+$400+0
        stx _bitmap48_ptr_3+$400+1
        jsr add_height_ax
	sta _bitmap48_ptr_4+$400+0
        stx _bitmap48_ptr_4+$400+1
        jsr add_height_ax
	sta _bitmap48_ptr_5+$400+0
        stx _bitmap48_ptr_5+$400+1
	rts
add_height_ax:
	clc
        adc _bitmap48_height
        bcc :+
        inx
:	rts
.endproc

.proc _bitmap48_setheight
	sta _bitmap48_height+$400
        rts
.endproc

.code
.proc _bitmap48_setup
        lda #THREE_COPIES
        sta NUSIZ0
        sta NUSIZ1
        sta WSYNC
        SLEEPR B48_CYCLE-7
        sta RESP0
        sta RESP1
        sta HMCLR
        lda #$10
        sta HMP1
        sta WSYNC
        sta HMOVE
        lda #1
        sta VDELP0
        sta VDELP1
        sta REFP0
        sta REFP1
        rts
.endproc

