
                 processor 6502

;Memory map:
                  ;$80-$ff Zero page

                  ;$7000-$9600 Code+graphics
gub_rut = $b000   ;$b000-$bc19 Unrolled code for the display
gub_list = $bf01  ;$bf00-$bfff The list of lines to show
                  ;$c000-$ffff Bank, screens and graphics as shown on screen

  include globals.s

  seg.u zp
  org $0080
zp_start:

  seg code
  org $7000

start: subroutine
;  lda #0
  sta textno


;Wait until bottom of screen:
  lda loaderirq_cou
.wait_f:
  cmp loaderirq_cou
  beq .wait_f

  jsr init_irq_init

  ldx #0
.clr_screen:
  lda #'
  sta $0400,x
  sta $0500,x
  sta $0600,x
  sta $06f7,x
  lda #1
  sta $d800,x
  sta $d900,x
  sta $da00,x
  sta $daf8,x
  inx
  bne .clr_screen


.init_screens:
;Here we initialize the screen data $c000-$dbff
  lda #$c0
  sta gub_poi
  ldx #5
.l2:
  ldy #0
.l1:
  lda #$34
;  sei
  sta $01
  txa
gub_poi = * + 2
  sta $c000,y
  inc $01
;  cli
  inx
  iny
  cpy #$28
  bne .l1
  lda gub_poi
  clc
  adc #$04
  sta gub_poi
  cmp #$cc
  bne .nonewx
  ldx #$85
.nonewx:
  cmp #$d8
  bne .l2
;  lda #$35
;  sta $01

;.make_gublist:
;;Here we make a dummy linear list of what to show
;  lda #0
;  ldx #0
;.lo3:
;  pha
;  and #$fe
;  sta gub_list,x
;  pla
;  clc
;  adc #$01
;  cmp #$5c      ;### This should be $60 ???
;  bne .now
;  lda #$0
;.now:
;  inx
;  bne .lo3

  lda #1
  sta part_finished
  lda #$ff
  sta part_finished_cou+1

the_next_screen:
  lda #2
  sta part_finished_cou
  lda #0
  sta gub_move_cou
  lda textno
  jsr gub_plot
  jsr make_gub_rut
  jsr gub_move

.wait_init:
  lda init_finished
  bne .wait_init

  sei
  lda #$35
  sta $01
;  lda #%00000011
;  sta $dd00
;  lda #$15  ;spr data at $07f8
;  sta $d018

  lda #$00
.wait_til_top
  cmp $d012
  bne .wait_til_top

  lda #$00
  sta $d01b
  lda #0
  sta $d015
  lda #$c8
  sta $d016

  lda #<irq_gub1
  sta $fffe
  lda #>irq_gub1
  sta $ffff
  lda #<nmi                       ;Set NMI, both the hardware
  sta $fffa                       ;KERNAL
  lda #>nmi
  sta $fffb
;  lda #$3b
;  sta $d011     ; set the raster interrupt location
  lda #irq_gub1_raster
  sta $d012

;  lda #$7f
;  sta $dc0d     ; disable timer interrupts
;  sta $dd0d
;  ldx #1
;  stx $d01a     ; enable raster interrupt
;  lda $dc0d     ; acknowledge CIA interrupts
;  lsr $d019     ; and video interrupts
;  lda #0
;  sta $d020
;  sta $d021
;  lda #13
;  sta $d027
;  sta $d028
  lda #0
  sta irq_done
  cli

.once:
  lda irq_done
  beq .once
  lda #0
  sta irq_done

ever:
  jsr gub_move
.we:
  lda irq_done
  beq .we
  lda #0
  sta irq_done

  inc part_finished_cou
  bne ever
  inc part_finished_cou+1
  bne ever

;  sei
  inc textno
textno = * + 1
  lda #0
  cmp #2
  beq .this_is_the_end
  cmp #4
  beq .this_is_the_end

  jsr mid_irq_init

;  lda #0
;  sta $d020
;  sta $d021
;  lda #$73
;  sta $d011

  ldx #$ff
  lda textno
  and #3
  cmp #3
  bne .nolonger
  ldx #$fd
.nolonger:
  stx part_finished_cou+1
  jmp the_next_screen

.this_is_the_end:
  jsr mid_irq_init
  lda #8
  ldx #32
.hidebuf:
  sta $db80,x
  dex
  bpl .hidebuf

;Here, decrunch the new image to show when exiting
  jsr decrunchimage

  lda irq_done
.synn:
  cmp irq_done
  beq .synn

  lda #0
  sta Init_nEnd
  sei
  lda #<init_irq2
  sta $fffe
  lda #>init_irq2
  sta $ffff
  lda #init_irq2_raster
  sta $d012
  lda #$7f
  sta $dc0d     ; disable timer interrupts
  lda #1
  sta $d01a     ; enable raster interrupt
  sta $d019     ; and video interrupts
  lda #$35
  sta $01
  lda #$1b
  sta init_finished
  cli

.wait_init2:
  lda init_finished
  bne .wait_init2
  lda #$a0
.ww:
  cmp $d012
  bne .ww
  rts

nmi:
  rti

 seg zp
gubplot_poi: dc.b 0,0
gub_orig: dc.b 0,0
part_finished: dc.b 0
part_finished_cou: dc.b 0,0
irq_done: dc.b 0
gub_imageno: dc.b 0
 seg code


 seg zp
savea: dc.b 0
savex: dc.b 0
savey: dc.b 0
 seg code

;$4200- precalced circles
;$7400- code
;$7f00- line table
;$8800- set all lines to $00

;linevalues used:
;$00     = empty line
;$04-$fe = $7e lines: Line $00-$7d
;$89-$ef = $34 lines: Line $7e-$b1
;$f9-$ff = $04 lines: Line $b2-$b5
;In total, 182 lines can be shown.

;gub_structure:

;In the gub_list, these are the corresponding positions:
;bit  76543210
;    %0        = nothing
;      000     = $d018 screen_mem
;         000  = $d018 char_mem
;            0 = $dd00 bank switch

;              bank + screen   screen ->    char + char_mem ->
;%00000000 -> $c000 +  $0000 = $c000     $05-$2c   $0000 -> Pixels at $c028, $c030, $c038...  ($05*8= $28)
;%00000010 -> $c000 +  $0000 = $c000     $05-$2c + $0800 -> Pixels at $c828, $c830, $c838...
;%00000100 -> $c000 +  $0000 = $c000     $05-$2c + $1000 -> Pixels at $d028, $d030, $d038...
;%00000110 -> $c000 +  $0000 = $c000     $05-$2c + $1800 -> Pixels at $d828, $d830, $d838...
;%00001000 -> $c000 +  $0000 = $c000     $05-$2c + $2000 -> Pixels at $e028, $e030, $e038...
;..
;%00001110 -> $c000 +  $0000 = $c000     $05-$2c + $3800 -> Pixels at $f828, $f830, $f838...
;%00010000 -> $c000 +  $0400 = $c400     $2d-$54 + $0000 -> Pixels at $c168, $c170, $c178...  ($2d*8=$168)
;%00010010 -> $c000 +  $0400 = $c400     $2d-$54 + $0800 -> Pixels at $c968, $c970, $c978...  ($2d*8=$168)
;..
;%00011110 -> $c000 +  $0400 = $c400     $2d-$54 + $3800 -> Pixels at $f968, $f970, $f978...  ($2d*8=$168)
;%00100000 -> $c000 +  $0800 = $c800     $55-$7c + $0000 -> Pixels at $c2a8, $c2b0, $c2b8...  ($55*8=$2a8)
;..
;%00101110 -> $c000 +  $0800 = $c800     $55-$7c + $3800 -> Pixels at $faa8, $fab0, $fab8...  ($55*8=$2a8)

;%00110000 -> $c000 +  $0c00 = $cc00     $85-$ac + $0000 -> Pixels at $c428, $c430, $c438...  ($85*8=$428)
;%01000000 -> $c000 +  $1000 = $d000     $ad-$d4 + $0000 -> Pixels at $c568, $c430, $c438...  ($ad*8=$568)
;%01010000 -> $c000 +  $1400 = $d400     $d5-$fc + $0000 -> Pixels at $c6a8, $c430, $c438...  ($d5*8=$6a8)

;%01011100 -> $c000 +  $1400 = $d400     $d5-$fc + $3000 -> Pixels at $f6a8, $f630, $f638...  ($d5*8=$6a8)
;%01011110 -> $c000 +  $1400 = $d400     $d5-$fc + $3800 -> Pixels at $fea8, $fe30, $fe38...  ($d5*8=$6a8)

;This is a total of 48 lines, fully movable and linear coded.

gub_images:
  dc.w image1
  dc.w image1+$780
  dc.w image1+($780*2)
  dc.w image1+($780*3)

gub_plot:
;  lda #2
  sta gub_imageno
  and #3
  asl
  tax
  lda gub_images,x
  sta gub_orig
  lda gub_images+1,x
  sta gub_orig+1



 seg zp
current_row: dc.b 0
 seg code

;Here we plot something that may be recognized:
  ldx #0
  stx current_row
.lo5:
  lda current_row
  asl
  tax
  lda gub_rows,x
  sta gubplot_poi
  lda gub_rows+1,x
  sta gubplot_poi+1

  ldx #0
  ldy #0
.lo6:
  lda #$34
  ldx #$35
;  sei
  sta $01
  lda (gub_orig),y
  eor #$ff
  sta (gubplot_poi),y
  stx $01
;  cli
  tya
  clc
  adc #8
  tay
  bne .lo6

  inc gub_orig+1
  inc gubplot_poi+1
.lo66:
  lda #$34
  ldx #$35
;  sei
  sta $01
  lda (gub_orig),y
  eor #$ff
  sta (gubplot_poi),y
  stx $01
;  cli
  tya
  clc
  adc #8
  tay
  cmp #$40
  bne .lo66
  dec gub_orig+1
  dec gubplot_poi+1

  inc gub_orig
  bne .noo
  inc gub_orig+1
.noo:

  lda current_row
  and #$07
  cmp #$07
  bne .no9
  lda gub_orig
  clc
  adc #$38
  sta gub_orig
  lda gub_orig+1
  adc #1
  sta gub_orig+1
.no9:

  inc current_row
  lda current_row
  cmp #$30
  bne .lo5

  lda #$35
  sta $01

  lda #0
  sta gub_move_cou
  sta gub_baserow
  sta gub_frameno
  lda #1
  sta rows_to_skip
  rts

gub_rows:
  dc.w $c028
  dc.w $c828
  dc.w $d028
  dc.w $d828
  dc.w $e028
  dc.w $e828
  dc.w $f028
  dc.w $f828

  dc.w $c168
  dc.w $c968
  dc.w $d168
  dc.w $d968
  dc.w $e168
  dc.w $e968
  dc.w $f168
  dc.w $f968

  dc.w $c2a8
  dc.w $caa8
  dc.w $d2a8
  dc.w $daa8
  dc.w $e2a8
  dc.w $eaa8
  dc.w $f2a8
  dc.w $faa8

  dc.w $c428
  dc.w $cc28
  dc.w $d428
  dc.w $dc28
  dc.w $e428
  dc.w $ec28
  dc.w $f428
  dc.w $fc28

  dc.w $c568
  dc.w $cd68
  dc.w $d568
  dc.w $dd68
  dc.w $e568
  dc.w $ed68
  dc.w $f568
  dc.w $fd68

  dc.w $c6a8
  dc.w $cea8
  dc.w $d6a8
  dc.w $dea8
  dc.w $e6a8
  dc.w $eea8
  dc.w $f6a8
  dc.w $fea8

  dc.w $8000 ;dummy
  dc.w $8000 ;dummy




irq_gub1_raster = $35
irq_gub1: subroutine
  sta savea_nzp
  stx savex_nzp
  sty savey_nzp
  asl $d019     ; acknowledge the raster interrupt
  lda #<irq_gub2
  ldx #>irq_gub2
  ldy #irq_gub2_raster
  sta $fffe
  stx $ffff
  sty $d012
  tsx
  cli
  nop
  nop
  nop
  nop
  nop
  nop
  nop
  nop
  nop
  nop
  nop
  nop
  nop
  nop
  nop
  nop
  nop
.should_not_happen
  jmp .should_not_happen

irq_gub2_raster = irq_gub1_raster + 1

irq_gub2:
;irq fffe, ffff jumps here:
  txs
  ldx #$08
.lo
  dex
  bne .lo
  bit $ea
  nop
  lda #irq_gub2_raster
  cmp $d012
  beq .fix_timing
.fix_timing:
  ldy #$03
.lo2:
  dey       ;2 cycles
  bpl .lo2  ;2 + 1if taken +2 if another page
  nop
  nop
  lda #$8   ;2
  sta $d021 ;4
  lda #$0   ;2
  sta $dd00 ;4
  jsr gub_rut
  ldx #8
.ll:
  dex
  bne .ll
  nop
  lda #$73
  sta $d011
  lda #0
  sta $d020
  stx $d021
;  lda #0
  lda #$15
  sta $d018
  lda #3
  sta $dd00
  ldy #$ff
  sty $d015

  ldx exitbounce
  lda sintbl,x
  clc
  adc #$80
  lsr
  lsr
  lsr
  lsr
  clc
  adc #$18
  sta $d000
  clc
  adc #$18
  sta $d002
  inc exitbounce

  lda #<irq_gub3
  sta $fffe
  lda #>irq_gub3
  sta $ffff
  lda #irq_gub3_raster
  sta $d012
;  ldy #$00
;  sty $d015     ; turn off all sprites
  inc irq_done
  jsr MUSIC_PLAY
  asl $d019     ; acknowledge the raster interrupt
  lda savea_nzp
  ldx savex_nzp
  ldy savey_nzp
  rti

irq_gub3_raster = $00
irq_gub3: subroutine
  sta savea_nzp
  lda #0
  sta $d015
  lda #<irq_gub1
  sta $fffe
  lda #>irq_gub1
  sta $ffff
  lda #irq_gub1_raster
  sta $d012
  asl $d019     ; acknowledge the raster interrupt
  lda savea_nzp
  rti

savea_nzp: dc.b 0
savex_nzp: dc.b 0
savey_nzp: dc.b 0

gub_rut_template:
gub_rut_d011 = * + 1
  ldy #$70
gub_rut_listpoi = * + 1
  lax gub_list
gub_rut_change2:
  and #$08
  sty $d011
  stx $d018
gub_rut_change1:
;  sta $dd00
  sta $d021
gub_rut_template_end:
  lda #1
  sta $d020
  sta $d021
  rts
gub_rut_template_end_end:


make_gub_rut: subroutine
  lda #1
  sta gub_rut_times

  lda #<gub_rut
  sta gub_rut_poi
  lda #>gub_rut
  sta gub_rut_poi+1
  lda #$18
  sta gub_rut_d011
  lda #<gub_list
  sta gub_rut_listpoi

;Start with "and #$08"
  lda #$29
  sta gub_rut_change2
  lda #$08
  sta gub_rut_change2+1
;"lda $d021"
;  lda #$ad ;=lda $xxxx
  lda #$8d ;=sta $xxxx
  sta gub_rut_change1

  lda gub_imageno
  cmp #1
  beq .scroll
;"lda $d021"
  lda #$ad ;=lda $xxxx
;  lda #$8d ;=sta $xxxx
  sta gub_rut_change1
.scroll:


  ldx #0
.lo2:
  ldy #0
.lo:
  lda gub_rut_template,y
  sta (gub_rut_poi),y
  iny
  cpy #gub_rut_template_end - gub_rut_template
  bne .lo
  lda gub_rut_poi
  clc
  adc #<(gub_rut_template_end - gub_rut_template)
  sta gub_rut_poi
  lda gub_rut_poi+1
  adc #>(gub_rut_template_end - gub_rut_template)
  sta gub_rut_poi+1
  ldy gub_rut_d011
  iny
  cpy #$72
  bne .no72
  ldy #$12
.no72:
  cpy #$20
  bne .no18
  ldy #$18
.no18:
  sty gub_rut_d011

  inc gub_rut_listpoi

  inx
  cpx #$c1
  bne .lo2
  ldy #0
.more:
  lda gub_rut_template_end,y
  sta (gub_rut_poi),y
  iny
  cpy #gub_rut_template_end_end - gub_rut_template_end
  bne .more

;Change first sta $dd00 into sty $d020
  lda #$8c
  sta gub_rut + gub_rut_change1 - gub_rut_template
  lda #$20
  sta gub_rut + gub_rut_change1 - gub_rut_template + 1
  lda #$d0
  sta gub_rut + gub_rut_change1 - gub_rut_template + 2
  rts

 seg zp
gub_rut_poi: dc.b 0,0
gub_move_cou: dc.b 0
gub_baserow: dc.b 0
gub_frameno: dc.b 0
 seg code

gub_move: subroutine
  lda gub_imageno
  cmp #1
  bne .no_presents

  inc gub_baserow
  inc gub_baserow
  lda gub_baserow
  cmp #$30
  bne .nowra
  lda #0
  sta gub_baserow
.nowra:
  asl
  sta gub_now
  tax
  inc gub_move_cou
  jmp .cont

.no_presents:
  cmp #0
  bne .no_mahoney
  dec gub_baserow
  lda gub_baserow
  cmp #$ff
  bne .nowra2
  lda #$2f
  sta gub_baserow
.nowra2:
  asl
  sta gub_now
  tax
  inc gub_move_cou
  jmp .cont

.no_mahoney:
  cmp #2
  bne .no_exit
  dec gub_baserow
  lda gub_baserow
  cmp #$ff
  bne .nowra3
  lda #$2f
  sta gub_baserow
.nowra3:
  asl
  sta gub_now
  tax
  inc gub_move_cou

;Original - too slow:
;.cont:
;  lda #$c0
;  sta gub_sm1
;  lda #0
;.lo:
;  adc gub_move_cou
;  bcc .now
;  dec gub_now
;  dec gub_now
;  bpl .nowr
;  ldx #$2f*2
;  stx gub_now
;.nowr:
;gub_now = * + 1
;  ldx #0
;.now:
;gub_sm1 = * + 1
;  stx gub_list
;  dec gub_sm1
;  bne .lo
;  rts


;Optimized version: - ok
.cont:
gub_now = * + 1
  ldx #0

  lda #$c0
  sta gub_sm1
  lda #0
.lo:
  adc gub_move_cou
  bcc .now
  dex
  dex
  bpl .nowr
  ldx #$2f*2
.nowr:
.now:
gub_sm1 = * + 1
  stx gub_list
  dec gub_sm1
  bne .lo
  rts

 seg zp
gub_rut_this_times: dc.b 0
gub_rut_times: dc.b 0
 seg code


;This is how we move the upwards scroller:
.no_exit:
  inc gub_frameno
  lda gub_frameno
  tax
  lda costbl,x
  clc
  adc #$80
  lsr
  lsr
;  lda #$0
;  clc
;  adc #$0
  sta gub_move_cou

  inc gub_baserow
  lda gub_baserow
  cmp #$c0
  bne .nowra6
  lda #$00
  sta gub_baserow
  inc gub_rut_times
.nowra6:
  lda gub_rut_times
  sta gub_rut_this_times
  cmp #4
  bne .no_skipadd
  inc rows_to_skip
  lda rows_to_skip
  cmp #$c1
  bne .no_skipadd
  lda #$c0
  sta rows_to_skip
.no_skipadd:

  lda #0
  ldy #$c0
rows_to_skip = * + 1
  ldx #1
  cpx #1
  beq .noskip
.moreskip:
  dex
  beq .no_more_skip
  sta gub_list-1,y
  dey
  jmp .moreskip


.no_more_skip:
  lda gub_move_cou
  clc
  adc rows_to_skip
  sec
  sbc #3
  sta gub_move_cou

  lda #$c4
  ldx #$2f*2
  jmp .groo
.noskip:
  lda gub_baserow
  lsr
  lsr
  asl
  tax
  lda gub_baserow
  and #$03
  eor #$03
  asl
  asl
  asl
  asl
  asl
  asl
  clc
  adc #$04

.groo:
  sty gub_sm1Q
;  lda #0
.loQ:
  adc gub_move_cou
  bcc .nowQ
  dex
  dex
  bpl .nowrQ
  dec gub_rut_this_times
  beq .no_more_times
  ldx #$2f*2
.nowrQ:
.nowQ:
gub_sm1Q = * + 1
  stx gub_list
  inc gub_move_cou
  dec gub_sm1Q
  bne .loQ
  rts

.no_more_times:
  lda gub_sm1Q
  sta gub_sm2Q
  ldx #0
.clrlo:
gub_sm2Q = * + 1
  stx gub_list
  dec gub_sm2Q
  bne .clrlo
  rts


init_irq_init:
  sei
;  lda $dc0d     ; acknowledge CIA interrupts
;  lda $dd0d     ; acknowledge CIA interrupts
;  lsr $d019     ; and video interrupts
  lda #<init_irq2
  sta $fffe
  lda #>init_irq2
  sta $ffff
  lda #init_irq2_raster
  sta $d012
;  lda #$1b
;  sta $d011
  lda #$7f
  sta $dc0d     ; disable timer interrupts
;  sta $dd0d
  lda #1
  sta $d01a     ; enable raster interrupt
;  lda $dc0d     ; acknowledge CIA interrupts
  sta $d019     ; and video interrupts
  lda #$35
  sta $01
  lda #$1b
  sta init_finished
  lda #$36-$1c-$1c+$100
  sta init_irq3_raster
  lda #$f9+$1c+$1c-$100
  sta init_irq1_wait
  lda #$1a
  sta init_irq4_raster
  lda #1
  sta Init_nEnd
  cli
  rts

init_finished:
  dc.b 0

init_irq1_raster = $f7
init_irq1:
  pha
  txa
  pha
  tya
  pha
  lda $01
  pha
  lda #$35
  sta $01
  ldx #1
  stx $d01a     ; enable raster interrupt
  asl $d019
  lda #init_irq2_raster
  sta $d012
  nop
  nop
  nop
  nop
  nop
  nop
  lda #1
  sta $d021
  sta $d020
  nop
  nop
  nop
  lda #$ff
  sta $d015

;  ldx #$05
  ldx #$07
.noo2:
  dex
  bne .noo2

  lda #$13
  sta $d011
  lda #$15
  sta $d018
  lda #$03
  sta $dd00

  ldx #0
init_irq1_wait = * + 1
  lda #$12
.waitt:
  cmp $d012
  bne .waitt
  stx $d020
  stx $d021

  dec init_finished
  lda #<init_irq2
  sta $fffe
  lda #>init_irq2
  sta $ffff
;  cli
  jsr MUSIC_PLAY
  pla
  sta $01
  pla
  tay
  pla
  tax
  pla
  rti


init_irq2_raster = $00
init_irq2:
  pha
  lda $01
  pha
  lda #$35
  sta $01
  lda #1
  lda $d01a     ; enable raster interrupt
  asl $d019
init_irq3_raster = * + 1
  lda #$06       ;This shall end at $36
  bpl .noo
  lda #$10    ;If too large, saturate at $10
.noo:
  cmp #$10
  bcs .noo3
  lda #$10
.noo3:
  sta $d012
  lda #0
  sta $d021
  sta $d020
  sta $d015
  lda #<init_irq3
  sta $fffe
  lda #>init_irq3
  sta $ffff
  lda #$1b
  sta $d011

  lda #0
  sta $3fff
  sta $7fff
  sta $bfff

Init_nEnd = * + 1
  lda #0
  beq End
  inc init_irq3_raster
  inc init_irq3_raster
  lda init_irq4_raster
  clc
  adc #$8
  sta init_irq4_raster
  dec init_irq1_wait
  dec init_irq1_wait
  pla
  sta $01
  pla
  rti
End:
  dec init_irq3_raster
  dec init_irq3_raster
  lda init_irq4_raster
  sec
  sbc #$8
  sta init_irq4_raster
  inc init_irq1_wait
  inc init_irq1_wait
  pla
  sta $01
  pla
  rti


init_irq3:
  pha
  lda $01
  pha
  lda #$35
  sta $01
  lda #1
  sta $d01a     ; enable raster interrupt
  asl $d019
init_irq4_raster = * + 1
  lda #$80
  sta $d012
  lda #<init_irq4
  sta $fffe
  lda #>init_irq4
  sta $ffff
  nop
  nop
  nop
  lda #8
  sta $d021
  sta $d020
  pla
  sta $01
  pla
  rti

.upper:
  lda #$32
.wa3
  cmp $d012
  bne .wa3
  lda #6
.waitt2:
  sec
  sbc #1
  bne .waitt2
  nop
  nop
  jmp .back

init_irq4:
  pha
  lda $01
  pha
  txa
  pha
  lda #$35
  sta $01
  lda #1
  sta $d01a     ; enable raster interrupt
  asl $d019
  lda #<init_irq1
  sta $fffe
  nop
;  nop
  lda #$85  ;screen at $6000, spr data at $63f8
  sta $d018
  lda #$02
  sta $dd00
  lda #1
  sta $d021
  sta $d020

  lda #$31
  cmp $d012
  bcs .upper
.back:
  lda #$3b
  sta $d011
  lda #$c8
  sta $d016
  lda #init_irq1_raster
  sta $d012
  lda #>init_irq1
  sta $ffff

  ldx exitbounce
  lda sintbl,x
  clc
  adc #$80
  lsr
  lsr
  lsr
  lsr
  clc
  adc #$18
  sta $d000
  clc
  adc #$18
  sta $d002
  inc exitbounce

  pla
  tax
  pla
  sta $01
  pla
  rti


mid_irq_init: subroutine
  sei
  lda #<mid_irq2
  sta $fffe
  lda #>mid_irq2
  sta $ffff
  lda #mid_irq2_raster
  sta $d012
  lda #$7f
  sta $dc0d     ; disable timer interrupts
;  lda #$35
;  sta $01
  cli
  rts

mid_irq1_raster = $f7
mid_irq1:
  pha
  txa
  pha
  tya
  pha
  lda $01
  pha
  lda #$35
  sta $01
  lda #1
  sta $d01a     ; enable raster interrupt
  asl $d019
  lda #mid_irq2_raster
  sta $d012
  nop
  nop
  nop
  nop
  lda #1
  sta $d021
  sta $d020
  nop
  nop
  nop
  lda #$ff
  sta $d015
  ldx #$04
.noo2:
  dex
  bne .noo2
  lda #$13
  sta $d011
  lda #$15
  sta $d018
  lda #$03
  sta $dd00
  ldx #0
  stx $d020
  stx $d021
  lda #<mid_irq2
  sta $fffe
  lda #>mid_irq2
  sta $ffff
  jsr MUSIC_PLAY
  inc irq_done
  pla
  sta $01
  pla
  tay
  pla
  tax
  pla
  rti


mid_irq2_raster = $00
mid_irq2:
  pha
  lda $01
  pha
  lda #$35
  sta $01
  lda #1
  lda $d01a     ; enable raster interrupt
  asl $d019
  lda #mid_irq3_raster
  sta $d012
  lda #0
  sta $d021
  sta $d020
  sta $d015
  lda #<mid_irq3
  sta $fffe
  lda #>mid_irq3
  sta $ffff
  lda #$1b
  sta $d011

  lda #0
  sta $3fff
  sta $7fff
  sta $bfff
  pla
  sta $01
  pla
  rti

mid_irq3_raster = $37
mid_irq3:
  pha
  lda $01
  pha
  txa
  pha
  lda #$35
  sta $01
  lda #1
  sta $d01a     ; enable raster interrupt
  asl $d019
  lda #mid_irq1_raster
  sta $d012
  lda #<mid_irq1
  sta $fffe
  lda #>mid_irq1
  sta $ffff
  nop
;  nop
  lda #8
  sta $d021
  sta $d020
  ldx exitbounce
  lda sintbl,x
  clc
  adc #$80
  lsr
  lsr
  lsr
  lsr
  clc
  adc #$18
  sta $d000
  clc
  adc #$18
  sta $d002
  inc exitbounce
  pla
  tax
  pla
  sta $01
  pla
  rti


image1:
 incbin c4_text_bitmap.raw
image1_end:

