P*      
* EDITOR
*
* This is the song editor for the music composer.
*
* Music is entered in via keypresses, whose meaning
* is shown at the bottom of the screen.
* This is also the place from which the note frequency
* duration, and keymap tables may be altered, as well
* as the interrupt timer.
*
* SLJ
* Begun: 1/1/97  :)
* Finished: April/May depending on how one measures :)
*
         DSK 'editor'

         REL

VERSION  = $C004          ;File ID

GOBASIC  EXT
INSTED   EXT
MACMAN   EXT
INTINIT  EXT
MACINIT  EXT

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

MACTEXT  EQU LINKADDR-386


*
* Editor tables
*
OPENFOLD = $0800
FOLDLO1  = $0900
FOLDLO2  = $0A00
FOLDHI1  = $0B00
FOLDHI2  = $0C00
FOLDFIEL = $0D00

*
* Constants
*

CHRIN    = $FFCF
CHROUT   = $FFD2
GETIN    = $FFE4
PLOT     = $FFF0
STOP     = $FFE1
BLNSW    = $CC
BLNCT    = $CD
BLNON    = $CF
SHFLAG   = $028D
SID      = $D400

*
* Variables
*

MACBEGIN = $02            ;End of player/begin macros
BFIELD1  = $04            ;End of macros/field 1 begin
BFIELD2  = $06            ;End field 1/begin field 2
BFIELD3  = $08            ;End field 2/begin field 3
EFIELD3  = $0A            ;End of field 3
FIELDEND = $0A            ;End of all fields
                          ;Beginning of related text
                          ;end of text pointer?

ACC      = $69            ;(FAC2)
EXT      = $6B
AUX      = $6D

POINT1   = $FD            ;Temporary pointer
SCREENP  = $FB            ;Screen pointer
TEMP     = $FF
STRING   = $0200

* Player instructions

SETVOL   = $80
ACTGLOB  = $81
DACTGLOB = $82
ACTLOC   = $83
DACTLOC  = $84
LINST    = $85
REPEAT   = $86
ENDREP   = $87
VSTOP    = $88
GATEON   = $89
GATEOFF  = $8A
RESTART  = $8B
JUMP     = $8C
JSUB     = $8D
RSUB     = $8E
LOADGLOB = %11000000
LOADLOC  = %11100000

*
* MUSINIT -- Preliminary setup
*
MUSINIT  ENT
         LDA #00
         STA ISFLAG
         STA PLFLAG
         STA BLFLAG
         STA FOLDFLAG
         STA PLAYFLAG
         STA TRNSFLAG
         STA LASTDUR
         STA LASTOCT
         STA LASTNOTE
         STA CURFLD
         LDA #1
         STA F1ROW
         STA F2ROW
         STA F3ROW

         JSR LOADPP
         LDX #5
:LOOP    LDA BFIELD1,X
         STA F1P,X
         LDA #00
         STA BOOKMARK,X
         DEX
         BPL :LOOP

         JMP HASHFOLD

*
* INIT -- Set up screen, etc.
*
INIT     
         LDA #147         ;Clear screen
         JSR CHROUT
         LDA #14          ;lower case
         JSR CHROUT
         LDA #12
         STA $D020
         LDA #0
         STA $D021

         LDA #4           ;Purple
         STA COLOR

         LDA #64          ;bar
         LDY #38          ;Column
:LOOP    
         LDX #0           ;Row
         JSR STAXY
         LDX #22
         JSR STAXY
         LDX #24
         JSR STAXY
         DEY
         BNE :LOOP

         LDA #93          ;Vertical bar
         LDX #23
:LOOP2   
         LDY #0
         JSR STAXY
         LDY #39
         JSR STAXY
         LDY #9
         JSR STAXY
         LDY #18
         JSR STAXY
         LDY #27
         JSR STAXY
         DEX
         BNE :LOOP2

         LDA #112         ;Elbow
         LDY #00
         JSR STAXY
         LDA #110
         LDY #39
         JSR STAXY
         LDA #125
         LDX #24
         JSR STAXY
         LDA #109
         LDY #00
         JSR STAXY

         LDA #113         ;Up junction
         LDX #22
         LDY #9
         JSR STAXY
         LDY #18
         JSR STAXY
         LDY #27
         JSR STAXY

         LDA #107         ;Right junction
         LDY #0
         JSR STAXY

         LDA #115         ;Left junction
         LDY #39
         JSR STAXY

         LDX #22
         LDY #6
         LDA #114         ;Down junction
         JSR STAXY
         INX
         LDA #93          ;vbar
         JSR STAXY
         INX
         LDA #113         ;Up junction
         JSR STAXY

         LDX #00
         LDY #8
         JSR PLOPXY
         JSR PLOPSTR
         TXT 'Blahtune v.A# SLJ 6/97',00

         LDA #3
         STA COLOR
         LDX #1
         LDY #28
         JSR PLOPXY
         JSR PLOPSTR
         TXT 'Macros:',00

         LDX #2
         LDY #28
         JSR PLOPXY
         JSR PLOPSTR
         TXT 'Field1:',00

         LDX #3
         LDY #28
         JSR PLOPXY
         JSR PLOPSTR
         TXT 'Field2:',00

         LDX #4
         LDY #28
         JSR PLOPXY
         JSR PLOPSTR
         TXT 'Field3:',00

         LDX #5
         LDY #28
         JSR PLOPXY
         JSR PLOPSTR
         TXT 'End:   ',00

         LDX #6
         LDY #28
         JSR PLOPXY
         JSR PLOPSTR
         TXT 'FREBOT:',00

         LDX #7
         LDY #28
         JSR PLOPXY
         JSR PLOPSTR
         TXT 'FRETOP:',00

         LDX #8
         LDY #28
         JSR PLOPXY
         JSR PLOPSTR
         TXT 'Clip:  ',00

         LDX #10
         LDY #29
         JSR PLOPXY
         JSR PLOPSTR
         TXT 'Audible',00

         LDX #11
         LDY #29
         JSR PLOPXY
         JSR PLOPSTR
         TXT 'Transpose',00

         LDX #12
         LDY #30
         JSR PLOPXY
         JSR PLOPSTR
         TXT 'note:',00

         LDX #13
         LDY #31
         JSR PLOPXY
         JSR PLOPSTR
         TXT 'dur:',00

         RTS

*
* Some important variables
*
FREBOT   ENT
         DA 00
FRETOP   ENT
         DA 00
CLIP     ENT
         DA $FF00
CLIPLO   DA $FFFF
CLIPHI   DA $FFFF
CLIPDIST DA 00

*
* The main loop -- read keyboard, enter in data
*   if appropriate, print messages, manage
*   pointers and such, and update screen.
*

F1P      DA 0             ;field 1 pointer
F2P      DA 0
F3P      DA 0
F1ROW    DFB 1            ;current row
F2ROW    DFB 1
F3ROW    DFB 1
CURFLD   DFB 0            ;0 1 2 ... current field
LASTOCT  DFB 1            ;Last octave
LASTNOT  DFB 0            ;Last note read in
LASTDUR  DFB 0            ;Last duration read

OCTTAB                    ;Octave offset table
         DFB 0,24,48,72,96,120,144,168

MUSICED  ENT
         JSR LOADPP
         JSR HASHFOLD
EDMAIN   
         JSR INIT
         LDA CURFLD
         PHA
         LDA #2
         STA CURFLD
:L1      JSR F1PTOZP      ;Set up pointers
         JSR FCENTER
         JSR PRFIELDS
         DEC CURFLD
         BPL :L1
         PLA
         STA CURFLD
         JSR F1PTOZP
:LOOP    
         LDX #1
         LDY #35
         JSR PLOPXY
         LDX #3
         LDA BFIELD1
         CMP MACBEGIN
         BNE :CONT
         LDA BFIELD1+1
         CMP MACBEGIN+1
         BNE :CONT
         LDX #1           ;White
:CONT    STX COLOR

         LDX #0
:L2      LDA MACBEGIN+1,X
         JSR PHBYTE
         LDA MACBEGIN,X
         JSR PHBYTE
         INC ROW
         LDA #35
         STA COLUMN
         JSR PLOP
         LDA #3
         STA COLOR
         INX
         INX
         CPX #10
         BNE :L2

         LDX #6
         LDY #35
         JSR PLOPXY
         LDA FREBOT+1
         JSR PHBYTE
         LDA FREBOT
         JSR PHBYTE

         INX
         JSR PLOPXY
         LDA FRETOP+1
         JSR PHBYTE
         LDA FRETOP
         JSR PHBYTE

         INX
         JSR PLOPXY
         LDA CLIP+1
         JSR PHBYTE
         LDA CLIP
         JSR PHBYTE

         LDA #32          ;Audible flag
         LDY PLFLAG
         BEQ :CONT2
         LDA #186
:CONT2   
         LDY #28
         INX
         INX
         JSR PLOPXY
         JSR CHAROUT

         LDA #32          ;Transpose flag
         LDY TRNSFLAG
         BEQ :CONT3
         LDA #186
:CONT3   
         LDY #28
         INX
         JSR PLOPXY
         JSR CHAROUT

         INX
         LDY #35
         JSR PLOPXY
         LDA TRANSNOT
         JSR PHBYTE
         INX
         JSR PLOPXY
         LDA TRANSDUR
         JSR PHBYTE

         JSR PRFIELDS     ;Print fields

         JSR HILIGHT      ;Highlight the current row

         LDY #00
         LDA (POINT1),Y
         TAX              ;Note/data
         INY
         LDA (POINT1),Y   ;Instruction/duration

         JSR SETMSG       ;Textualize the music data

         JSR READPAIR
         JMP :LOOP

*
* HILIGHT -- Reverses current row
*
HILIGHT  
         LDY CURFLD
         LDX F1ROW,Y
         TYA
         STA TEMP
         ASL
         ASL
         ASL
         SEC
         ADC TEMP
         TAY
         INY
         JSR PLOPXY
         LDX #6
:LOOP    LDA (SCREENP),Y
         EOR #$80
         STA (SCREENP),Y
         INY
         DEX
         BPL :LOOP
         RTS

*
* Reads in a data pair.
*
ISFLAG   DFB 00           ;Disable ISNOTE etc. checks
READPAIR 
         LDA #1           ;Note
         JSR SETACT
:WAIT    JSR GETIN
         BEQ :WAIT
         JSR ISOTHER      ;Special commands
         BEQ :OTHER
         JSR ISNOTE       ;Is it A-Y?
         BEQ :NOTE
         JSR ISDIRECT     ;Is it a player directive?
         BEQ :DIRECT
         JSR ISEDIT       ;Is it an editor command?
         BNE :WAIT

:EDIT    JMP EDIT
:OTHER   JMP OTHER
:NOTE    JMP READNOTE
:DIRECT  JMP DIRECT

*
* Notes are keys a-y (x=rest)
*
ISNOTE   
         LDX ISFLAG
         BNE :RTS
         CMP #'a'
         BCC :RTS
         CMP #'y'
         BCS :RTS
         LDX #00
:RTS     RTS

         PUT 'eddirect.d.s'
         PUT 'ededit.u.s'
         PUT 'edother.z.s'
         PUT 'editor2.y.s'
