
//#resource "vcs-ca65.h"

import "vcslib.ecs"

component BCDScore2
   digits: 0..$ff
   scorecolor: 0..$ff
end

component BCDScore4
   digits: 0..0xffff
end

component BCDScore6
   digits: 0..0xffffff
end

system Kernel6Digit
    locals 15
    on preframe do with [BCDScore6]
---
Digit0 = {{$0}}
Digit1 = {{$2}}
Digit2 = {{$4}}
Digit3 = {{$6}}
Digit4 = {{$8}}
Digit5 = {{$10}}
@BCD0  = {{$12}}
@BCD1  = {{$13}}
@BCD2  = {{$14}}

	lda {{get digits 0}}
        sta @BCD0
	lda {{get digits 8}}
        sta @BCD1
	lda {{get digits 16}}
        sta @BCD2
	ldx #0	; leftmost bitmap
        ldy #2	; start from most-sigificant BCD value
@Loop:
        lda @BCD0,y	; get BCD value
        and #$f0	; isolate high nibble (* 16)
        lsr		; shift right 1 bit (* 8)
        clc
        adc #<{{^FontTable}}
        sta Digit0,x	; store pointer lo byte
        lda #>{{^FontTable}}
        adc #0
        sta Digit0+1,x	; store pointer hi byte
        inx
        inx		; next bitmap pointer
        lda @BCD0,y	; get BCD value (again)
        and #$f		; isolate low nibble
        asl
        asl
        asl		; * 8
        clc
        adc #<{{^FontTable}}
        sta Digit0,x	; store pointer lo byte
        lda #>{{^FontTable}}
        adc #0
        sta Digit0+1,x	; store pointer hi byte
        inx
        inx		; next bitmap pointer
        dey		; next BCD value
        bpl @Loop	; repeat until < 0
---
    on kernel do with [BCDScore6,PFColor]
---
        lda {{<pfcolor}}
        sta COLUP0
        sta COLUP1
        lda #3
        sta NUSIZ0
        sta NUSIZ1
; set horizontal position of player objects
        sta WSYNC
        sta HMCLR
        SLEEPR 24
        sta RESP0
        sta RESP1
        lda #$10
        sta HMP1
        sta WSYNC
        sta HMOVE
        SLEEPR 24	; wait 24 cycles between write to HMOVE and HMxxx
        sta HMCLR
        lda #1
        sta VDELP0
        sta VDELP1
---
    on kernel do
    critical fit 72
    with [BCDScore6,BGColor] 
---
; Display the resulting 48x8 bitmap
; using the Digit0-5 pointers.
@LoopCount = {{$12}}
@Temp = {{$13}}

	lda {{<bgcolor}}
	sta WSYNC
        sta COLUBK
        lda #7
        sta @LoopCount
        SLEEPR 20	; TODO?
:
	ldy @LoopCount	; counts backwards
        lda (Digit0),y	; load B0 (1st sprite byte)
        sta GRP0	; B0 -> [GRP0]
        lda (Digit1),y	; load B1 -> A
        sta GRP1	; B1 -> [GRP1], B0 -> GRP0
        sta WSYNC	; sync to next scanline
        lda (Digit2),y	; load B2 -> A
        sta GRP0	; B2 -> [GRP0], B1 -> GRP1
        lda (Digit5),y	; load B5 -> A
        sta @Temp	; B5 -> temp
        lda (Digit4),y	; load B4
        tax		; -> X
        lda (Digit3),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
        dec @LoopCount	; go to next line
	bpl :-		; repeat until < 0
        
        lda #0		; clear the sprite registers
        sta WSYNC
        sta GRP0
        sta GRP1
        sta GRP0
        sta GRP1
        sta COLUBK
---
end


resource FontTable ---
; Font table for digits 0-9 (8x8 pixels)
;;{w:8,h:8,count:10,brev:1,flip:1};;
	.byte $00,$3c,$66,$66,$76,$6e,$66,$3c,$00,$7e,$18,$18,$18,$38,$18,$18
        .byte $00,$7e,$60,$30,$0c,$06,$66,$3c,$00,$3c,$66,$06,$1c,$06,$66,$3c
        .byte $00,$06,$06,$7f,$66,$1e,$0e,$06,$00,$3c,$66,$06,$06,$7c,$60,$7e
        .byte $00,$3c,$66,$66,$7c,$60,$66,$3c,$00,$18,$18,$18,$18,$0c,$66,$7e
        .byte $00,$3c,$66,$66,$3c,$66,$66,$3c,$00,$3c,$66,$06,$3e,$66,$66,$3c
---

system Kernel2Digit
    locals 3
    on preframe do once
---
   lda #0
   sta {{$1}}
   sta {{$2}}
---
    on kernel do select [BCDScore2]
---
	lda #$02
        sta CTRLPF
; TODO: should be constants
; and it's wrong, too!
        lda {{<BCDScore2:scorecolor}}+0
        sta COLUP0
        lda {{<BCDScore2:scorecolor}}+1
        sta COLUP1
---
    on kernel do
    critical fit 98
    select [BCDScore2] 
---
	lda #7
        sta {{$0}}
@Loop:
	ldx #0
	sta WSYNC
        {{!compute2digit 0}}
.if {{%ecount}}>1
        inx
        {{!compute2digit 1}}
.else
        {{!compute2digit 0}}
.endif
; playfield
	dec {{$0}}
        jpl @Loop
;        dex
;        stx PF1
---
    on compute2digit do once
---
        lda {{$1}}		; load 1st pf
        sta PF1			; store 1st pf
; first digit
	lda {{<BCDScore2:digits}} + {{#0}}
        pha
        and #$0f
	asl
        asl
        asl
        {{!fetchdigit}}
        and #$0f
        ldy {{$2}}		; load 2nd pf
        sta {{$1}} + {{#0}}
; second digit
        pla
        and #$f0
        lsr
        sty PF1			; store 2nd pf
        {{!fetchdigit}}
        and #$f0
        ora {{$1}} + {{#0}}
        sta {{$1}} + {{#0}}
---
    on fetchdigit do once
---
        adc {{$0}}
	tay
        ; TODO: select your own?
	lda {{^FontTablePF}},y
---
end

resource FontTablePF ---
; Font table for digits 0-9 (4x8 pixels)
;;{w:8,h:8,count:10,brev:1,flip:1};;
        .byte $00,$00,$EE,$AA,$AA,$AA,$EE,$00
        .byte $00,$00,$22,$22,$22,$22,$22,$00
        .byte $00,$00,$EE,$88,$EE,$22,$EE,$00
        .byte $00,$00,$EE,$22,$66,$22,$EE,$00
        .byte $00,$00,$22,$22,$EE,$AA,$AA,$00
        .byte $00,$00,$EE,$22,$EE,$88,$EE,$00
        .byte $00,$00,$EE,$AA,$EE,$88,$EE,$00
        .byte $00,$00,$22,$22,$22,$22,$EE,$00
        .byte $00,$00,$EE,$AA,$EE,$AA,$EE,$00
        .byte $00,$00,$EE,$22,$EE,$AA,$EE,$00
;;
---

resource FontTablePFFancy ---
; Font table for digits 0-9 (4x8 pixels)
;;{w:8,h:8,count:10,brev:1,flip:1};;
        .byte $00,$44,$AA,$AA,$AA,$AA,$AA,$44
        .byte $00,$EE,$44,$44,$44,$44,$CC,$44
        .byte $00,$EE,$88,$88,$44,$22,$AA,$44
        .byte $00,$CC,$22,$22,$66,$22,$22,$CC
        .byte $00,$22,$22,$22,$EE,$AA,$AA,$88
        .byte $00,$44,$AA,$22,$44,$88,$88,$EE
        .byte $00,$44,$AA,$AA,$CC,$88,$AA,$44
        .byte $00,$22,$22,$22,$22,$22,$AA,$EE
        .byte $00,$44,$AA,$AA,$44,$AA,$AA,$44
        .byte $00,$44,$AA,$22,$66,$AA,$AA,$44
;;
---

system BCDMath
	locals 1
        on AddBCD4 do once
---
.ifnblank {{arg 0}}
    lda #<{{arg 0}}
    ldy #>{{arg 0}}
.endif
---
	on AddBCD4 do with [BCDScore6]
---
; Adds value to 6-BCD-digit score.
; A = 1st BCD digit
; Y = 2nd BCD digit
        sed	; enter BCD mode
        clc	; clear carry
        adc {{get digits}}
        sta {{set digits}}
        tya
        adc {{get digits 8}}
        sta {{set digits 8}}
        lda {{get digits 16}}
        adc #0
        sta {{set digits 16}}
        cld	; exit BCD mode
---
	on AddBCD2 do once
---
.ifnblank {{arg 0}}
    lda #<{{arg 0}}
.endif
---
	on SubBCD2 do once
---
.ifnblank {{arg 0}}
    lda #<{{arg 0}}
.endif
---
	on AddBCD2 do with [BCDScore2]
---
        sed	; enter BCD mode
        clc	; clear carry
        adc {{get digits}}
        sta {{set digits}}
        cld	; exit BCD mode
	bcc :+
	lda #$99
	sta {{set digits}}
:
---
 	on SubBCD2 do with [BCDScore2]
---
; TODO?
        tay
        lda {{get digits}}
        sty {{set digits}}
        sed	; enter BCD mode
        sec	; set carry
        sbc {{get digits}}
        sta {{set digits}}
        cld	; exit BCD mode
	bcs :+
	lda #0
	sta {{set digits}}
:
---
end

demo Main

    using FrameLoop
    using Kernel6Digit
    using Kernel2Digit
    using JoyButton, BCDMath
    
    entity [Player,BCDScore6,PFColor,BGColor]
    	init digits = 0x123456
        init pfcolor = $3c
        init bgcolor = $02
    end

    entity [BCDScore2]
    	init digits = 0x24
        init scorecolor = $ce
    end
    entity [BCDScore2]
    	init digits = 0x56
        init scorecolor = $3e
    end

  system IncScore
    on joybutton do with [Player,BCDScore6]
---
    {{!AddBCD4 $0210}}
---
  end
end demo

