        ; Design study for Hayesmaker's C64 version of "Mario's Cement Factory"
        ; to see how it would look with red frame and the title in the border
        ; written in cbm prg studio by Roman Werner @romwer roman.werner@gmail.com

*=$0801
        ; 10 SYS (4096)
        byte $0E,$08,$0A,$00,$9E,$20,$28, $34,$30,$39,$36,$29,$00,$00,$00


*=$1000

; some constants
SPRITEID  = 192
VPOS_PAL  = 22
VPOS_NTSC = 28

        ; check for pal or ntsc
        ldx #0          ; assume ntsc
@lp1    lda $d012
@lp2    cmp $d012
        beq @lp2
        bmi @lp1
        cmp #6
        beq @set
        ldx #1          ; it's actually pal
@set    stx ntscpal:

        ; set border color to red
        lda #2
        sta $d020

        ; clear last byte of the video bank to prevent behind-border-character garbage
        lda #0
        sta $3fff

        sei

        ; set irq pointers to own routine
        lda #<irq:
        sta $0314
        lda #>irq:
        sta $0315

        ; set initial raster position for raster-irq
        lda #0
        sta $d012

        ; clear hi-bit of intial raster-position
        lda $d011
        and #%01111111
        sta $d011

        ; enable raster-irq
        lda #%00000001
        sta $d01a

        ; turn off timer irqs
        lda #%01111111
        sta $dc0d

        cli

main:   ldx #14
@lp     ldy spr0xy:,x
        iny
        tya
        sta spr0xy:,x
        dex
        dex
        bpl @lp

        lda #60
@wait   cmp $d012
        bne @wait

        jmp main:


; ***** irq *****

irq:    lda $d019
rrptr:  bmi pos0: ; <= relative position of next raster-routine will be modified here
rradr:
        lda $dc0d
        cli
        jmp $ea31

; ***** raster routines

        ; raster position 0

pos0:   lda #255
        sta $d015               ; enable all sprites

        lda #49
        sta $d012               ; set next raster-line  position

        lda #pos49:-rradr:
        sta rrptr:+1
        bne irqexit:

        ; raster position 49

pos49:  lda $d011
        ora #%00001000
        sta $d011

        lda #pos249:-rradr:
        sta rrptr:+1

        lda #249
        sta $d012

        ; time waister so that background color switch happens in the border
        ldx #10
@wait   dex
        bne @wait

        lda #15
        sta $d021

        jsr restoreGameSpritePositions:

        bne irqexit:

        ; raster position 249

pos249: lda $d011
        and #%11110111
        sta $d011

        lda #0
        sta $d012
        sta $d015

        lda #pos0:-rradr:
        sta rrptr:+1

        ; time waister so that background color switch happens in the border
        ldx #9
@wait   dex
        bne @wait

        lda #2
        sta $d021

        jsr setHeaderSprites:

irqexit:
        asl $d019       ; acknowledge raster irq

        pla
        tay
        pla
        tax
        pla

        rti

restoreGameSpritePositions:

        ; restore sprite x/y-position
        ldx #15
@lp     lda spr0xy:,x
        sta $d000,x
        dex
        bpl @lp

        ldx #7
@lp2    lda spr0pt:,x
        sta 2040,x
        lda spr0cl:,x
        sta $d027,x
        dex
        bpl @lp2

        ; restore sprite x-position hi-bit
        lda sprxhi:
        sta $d010

        lda sprmc:
        sta $d01c

        lda sprwid:
        sta $d01d

        lda spract:
        sta $d015

        rts


setHeaderSprites:

        ; set 8x sprite pointers
        lda #SPRITEID+0
        sta 2040
        lda #SPRITEID+1
        sta 2041
        lda #SPRITEID+2
        sta 2042
        lda #SPRITEID+3
        sta 2043
        lda #SPRITEID+4
        sta 2044
        lda #SPRITEID+5
        sta 2045
        lda #SPRITEID+6
        sta 2046
        lda #SPRITEID+7
        sta 2047

        ; clear all sprite x-position hi-bit
        lda #0
        sta $d010

        ; set 8x sprite x position
        ldx #0
        lda #87
        clc
@lp2    sta $d000,x
        adc #24
        inx
        inx
        cpx #16
        bne @lp2
        
        ; set 8x sprite y position
        lda #VPOS_PAL
        ldx ntscpal:
        bne @pal
        lda #VPOS_NTSC
@pal    sta $d001
        sta $d003
        sta $d005
        sta $d007
        sta $d009
        sta $d00b
        sta $d00d
        sta $d00f

        ; set 8x sprite color (black)
        ldx #7
        lda #0
@lp3    sta $d027,x
        dex
        bpl @lp3

        ; no multi-color sprites
        lda #0
        sta $d01c

        ; normal x-/y-width/height
        lda #0
        sta $d01d

        rts

; ***** game sprite table

spract: byte 255
sprmc:  byte 0
sprwid: byte 0

spr0xy: byte 61,61
spr1xy: byte 85,85
spr2xy: byte 109,109
spr3xy: byte 133,133
spr4xy: byte 157,157
spr5xy: byte 181,181
spr6xy: byte 205,205
spr7xy: byte 229,229

sprxhi: byte 0

spr0pt: byte 200
spr1pt: byte 200
spr2pt: byte 200
spr3pt: byte 200
spr4pt: byte 200
spr5pt: byte 200
spr6pt: byte 200
spr7pt: byte 200

spr0cl: byte 0
spr1cl: byte 1
spr2cl: byte 2
spr3cl: byte 3
spr4cl: byte 4
spr5cl: byte 5
spr6cl: byte 6
spr7cl: byte 7

ntscpal: byte 0 ; 0=ntsc / 1=pal

; ***** sprite data

*=$3000
        
        incbin "mcf_header_sprites.bin" ; 8 sprites (192-199)

        ; sample sprite-data for sprite 200
        byte 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
        byte 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
        byte 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
        byte 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
