*      
* Initialization and interrupt routines
*

         DSK '@0:interrupt'

         REL

         PUT 'playvars.i.s'
         PUT 'diskio.ext.s'

BLNON    = $CF
SCNKEY   EQU $EA87
PLAYFLAG EXT
RASTFLAG EXT
FREBOT   EXT
FRETOP   EXT

*
* Two vectors at $8000 (warm restart) and
* $8003 (cold restart)
*
         JMP ERROR

*
* Okay... this is the routine which gets the whole
* thing moving.  It is placed into this file purely
* for convenience.  It sets up the appropriate
* variables and interrupt vectors and such and
* then jumps into the music editor.
*
* How exciting!
*
* SLJ 3/31/97
*

MACINIT  EXT
MUSINIT  EXT
MUSICED  EXT

BEGIN    
         LDA $01
         AND #$F8
         ORA #6
         STA $01
         JSR MACINIT
         JMP ERROR

NMI      
         SEI
         PHA
         LDA #$FF         ;Check for R/S key
         STA $DC02
         LDA #0
         STA $DC03
         LDA #$7F
         STA $DC00
         LDA $DC01
         BPL ERROR
         PLA
         CLI
         RTI

PERROR   JSR INTINIT
         LDA $01
         AND #$F8
         ORA #6
         STA $01
         LDA #00
         STA $D021
         LDA #2
         STA $D020
         JSR STROUT
         DFB 147,5
         TXT '** What the heck did you just do? **'
         DFB 13,13
         TXT 'Something drastic has occured.'
         DFB 13,13,00
         JSR WAITKEY

ERROR    
         LDA $01
         AND #$F8
         ORA #6
         STA $01

         LDA #<NMI        ;RESTORE can save us in a pinch
         STA $0318
         LDA #>NMI
         STA $0319
         LDA #<PERROR     ;Save our butt if something drastic
         STA $0316        ;occurs
         LDA #>PERROR
         STA $0317

         LDX #$FD         ;Reset stack
         TXS
         LDA #00          ;Zero volume
         STA $D418
         LDA #1
         STA $CC          ;Stop cursor flash

         JSR INTINIT
         JSR MUSINIT
         JMP MUSICED

*
* Exit to BASIC
*
GOBASIC  ENT
         LDA $01
         ORA #3
         STA $01
         LDA FREBOT       ;Set BASIC pointers
         CLC
         ADC #1
         STA $2B
         STA $31
         LDA FREBOT+1
         ADC #00
         STA $2C
         STA $32
         LDA FRETOP
         STA $37
         LDA FRETOP+1
         STA $38
         LDA #00
         LDY #$FF
         DEC $2C
         STA ($2B),Y      ;Start program with a 0
         INC $2C
         JSR $A65E        ;CLR
         JMP $A483

*
* Initialize music interrupt routine
*
INTINIT  
         LDA #00
         STA PLAYFLAG
         SEI
         LDA #<IRQ
         STA $0314
         LDA #>IRQ
         STA $0315
         LDA #$C6         ;Give stable raster position
         STA $DC04
         LDA #$42
         STA $DC05
         LDA #1           ;Continuous
         STA $DC0E
         CLI
         RTS

*
* Main interrupt routine
*
IRQ      
         LDA PLAYFLAG
         BEQ :NORMAL
         LDA RASTFLAG
         BNE :C1
         INC $D020
:C1      JSR MAINPLAY
         LDA RASTFLAG
         BNE :C2
         DEC $D020
:C2      JSR SCNKEY
         LDA $CC          ;Blink cursor
         BNE :DONE
         DEC $CD
         BNE :DONE
         LDA #20
         STA $CD
         LDA $CF
         EOR #01
         STA $CF
         LDY $D3
         LDA ($D1),Y
         EOR #$80
         STA ($D1),Y

:DONE    LDA $DC0D
         PLA
         TAY
         PLA
         TAX
         PLA
         RTI

:NORMAL  JMP $EA31
