!to "bin\rainbow-hd.prg",cbm

*=$0800
; here I start the screen, I misused the first 8x8 charcs for 
; a small 8 char charset, so it is in the screendata.
; but the first and the last two lines are switched off
font:
!binary "bin/ecm-font-fontdata.bin",,
;last char/8/9 = temp space one char/8 byte for charcopy

;spritepointer cache for the scrolltext multiplexer
old_spritepointer:
!byte sprites_start, sprites_start, sprites_start, sprites_start
!byte sprites_start, sprites_start, sprites_start, sprites_start

;!byte $40,$40 ; FREE filled for better RLE compression

*=$0850
screenpart:
!byte $40,$41,$42,$43,$44,$45,$46,$47
!byte $80,$81,$82,$83,$84,$85,$86,$87
!byte $c0,$c1,$c2,$c3,$c4,$c5,$c6,$c7
!byte $00,$01,$02,$03,$04,$05,$06,$07

;!fi 808, $07 ;SCREEN, filled for better RLE compression
!source "screencopy_speedcode_generator.asm"

*=$0b98
;scrolltext sprite position for re/init
sprite_init_positions:
!byte $17,$d1,$47,$d1,$77,$d1,$A7,$d1
!byte $D7,$d1,$07,$d1,$37,$d1,$67,$d1      

;scrolltext position cache for scrolltext multiplexer
sprite_positions:
!byte $17,$d1,$47,$d1,$77,$d1,$A7,$d1
!byte $D7,$d1,$07,$d1,$37,$d1,$67,$d1

;colortable for screen lines        
colortable:
!byte $0a,$0a,$0a,$0a
!byte $04,$04,$04,$04
!byte $04,$04,$04,$04
!byte $0e,$0e,$0e,$0e
!byte $0e,$0e,$0e,$0e
!byte $0d,$0d,$0d,$0d
!byte $0d,$0d,$0d,$0d
!byte $0a,$0a,$0a,$0a

;offset for the colortable (repeats every 8th line)
colortable_offset:
!byte $03,$02,$01,$00,$07,$06,$05,$04

*=$0bf8
;spritepointer on the screen
!byte sprites_start, sprites_start, sprites_start, sprites_start
!byte sprites_start, sprites_start, sprites_start, sprites_start

*=$0c00
;init
        sei

        jsr init_vic
        lda #$7f
        sta $dc0d  ;disable timer interrupts which can be generated by the two CIA chips
        sta $dd0d  ;the kernal uses such an interrupt to flash the cursor and scan the keyboard, so we better
                   ;stop it.

loop1:  lda $d012
loop2:  cmp $d012
        beq loop2
        bmi loop1
        and #$03
        sta SYSTEM_TYPE
        cmp #$03
        beq +
        ;NTSC Patch
        jsr patch_ntsc
        
+       lda #$bf
wait    cmp $d012
        bne wait

        lax #$00
        tay
        jsr sidinit
        
        jsr speedcode_generator

        ;8 x shift_colors = init screen
-
        jsr shift_colors
        jsr hardscroll_screen
        dec please_copy_logo_flag        
        bne -
        
        ;jsr copy_logo ; the first copy does copy the shit from the drawing area to the visible sprites and clears the drawing area
        jsr copy_logo ; the second copy does copy the cleared area to the visible sprites
        jsr set_draw
        
        lda $dc0d  ;by reading this two registers we negate any pending CIA irqs.
        lda $dd0d  ;if we don't do this, a pending CIA irq might occur after we finish setting up our irq.
                   ;we don't want that to happen.

        lda #$01   ;this is how to tell the VICII to generate a raster interrupt
        sta $d01a

        lda #irq_line8   ;this is how to tell at which rasterline we want the irq to be triggered
        sta $d012

        ;lda #%00011100   ;as there are more than 256 rasterlines, the topmost bit of $d011 serves as
        ;sta $d011  ;the 8th bit for the rasterline we want our irq to be triggered.
        ;           ;here we simply set up a character screen, leaving the topmost bit 0.

        lda #$35   ;we turn off the BASIC and KERNAL rom here
        sta $01    ;the cpu now sees RAM everywhere except at $d000-$e000, where still the registers of
                   ;SID/VICII/etc are visible

        lda #<irq8 ;this is how we set up
        sta $fffe   ;the address of our interrupt code
        lda #>irq8
        sta $ffff

        cli        ;enable maskable interrupts again
loop:
        lda wait_worker
        cmp #$00
        bne +
        
        lda worker_flag
        cmp #$01
        bne +
        lda #$00
        sta worker_flag

        jsr draw_step
        lda #$01
        sta please_copy_logo_flag
+
        lda worker_flag
        cmp #$02
        bne +
        lda #$00
        sta worker_flag
        jsr copy_logo
+
        jmp loop      ;we better don't RTS, the ROMS are now switched off, there's no way back to the system
wait_worker:
!byte $7a

worker_flag:
!byte $00
please_copy_logo_flag:
!byte $08 ;is initially misused to shift_colors 8x

;copy the drawn sprites to the real/visible ones
copy_logo:
    lda #$00
    sta please_copy_logo_flag
; had to split it in to parts otherwise, 
; the copy would be visible on screen :(
    ldx #63    
-   
!for SPRITES = 0 to 2
    lda sprite_logo_painting_area+(SPRITES*64)-1,x
    sta sprite_logo_visible_area+(SPRITES*64)-1,x
!end
    
    dex
    bne-
    
    ldx #63    
-   
!for SPRITES = 3 to 8
    lda sprite_logo_painting_area+(SPRITES*64)-1,x
    sta sprite_logo_visible_area+(SPRITES*64)-1,x
!end
    lda #$00
!for SPRITES = 0 to 8
    sta sprite_logo_painting_area+(SPRITES*64)-1,x
!end
    
    dex
    bne-

    lda #$01
    sta worker_flag
    
    rts
    
!source "irqs.asm"
!source "keypress.asm"
!source "screen.asm"
!source "sprite_scroller.asm"
!source "sprite_logo_multiplexer.asm"
!source "scrolltext.asm"
!source "points_delta.asm"
!source "sprite_logo_sinus_mover.asm"
!source "sprite_logo_drawer.asm"

patch_ntsc:
        lda #$ea ;nop
        sta ntsc_sprite_position_patch
        sta ntsc_sprite_position_patch+1
        sta ntsc_sprite_position_patch+2
        
        lda #$c1 
        sta ntsc_sprite_rasterbar_patch   
        sta ntsc_sprite_rasterbar_patch+2   
        sta ntsc_sprite_rasterbar_patch+4
        lda #$00
        sta ntsc_sprite_rasterbar_patch+1
        sta ntsc_sprite_rasterbar_patch+3
        sta ntsc_sprite_rasterbar_patch+5
        
        rts
ntsc_frameskip:
!byte    

!align 63,0,0
sprite_data:
!fill 64, $ff
!binary "bin/spite-font.bin",,
sprite_logo_painting_area= *
!fill 576, $00
sprite_logo_visible_area = sprite_logo_painting_area+576
!fill 576, $00
;demo_end= sprite_logo_visible_area+576

*=$3a00
sidinit:
sidplay = *+3
!binary "bin/Cheesetro_Tune3a00.sid",, 0x7E
demo_end