*      
* 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 :)
*
* Last update: 6/98
*
         DSK '@0:editor'

         REL

VERSION  = $C006          ;File ID

GOBASIC  EXT
INSTED   EXT
MACMAN   EXT
INTINIT  EXT
MACINIT  EXT

         PUT 'diskio.ext.s'
         PUT 'playvars.k.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            ;Data 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
WAIT     = $8F
SYNC     = $90
CYCTEST  = $91
SLUR     = $92
SLOFF    = $93
LOADGLOB = %11000000
LOADLOC  = %11100000

*
* MUSINIT -- Preliminary setup
*
MUSINIT  ENT
         LDA #00
         STA ISFLAG
         STA BLFLAG
         STA FOLDFLAG
         STA PLAYFLAG
         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 #2           ;Red
         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 #9
         JSR PLOPXY
         JSR PLOPSTR
         TXT 'Tunesmith v.B SLJ 6/98',00

*
* Print memory locations and variable values
*

DISPVARS 
         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

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

         LDX #8
         LDY #30
         JSR PLOPXY
         JSR PLOPSTR
         TXT 'Oct-Dur',00
INITRTS  RTS

* Flags: singlekey, macro display, sharpval,
*   number base, audible, transpose

PRFLAGS  LDX #9
         LDY #30
         JSR PLOPXY
         LDA #5           ;Green
         STA COLOR

         LDY #00
:LOOP    LDA #'-'
         LDX EDFLAGS,Y
         BPL :PR
         LDA :FLAGS,Y
:PR      JSR CHAROUT
         INY
         CPY #7
         BNE :LOOP

         LDA SHARPVAL
         BEQ INITRTS
         LDA #32
         STA COLUMN
         LDA #'#'
         JMP CHAROUT

:FLAGS   TXT '!"-$ATR'

*
* 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 4            ;Last octave
LASTNOT  DFB 4            ;Last note read in
LASTDUR  DFB 4            ;Last duration read

LASTDISP DFB $FF          ;Last display mode
REDRAWFLAG DFB 00         ;Flag to redraw fields

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

MUSICED  ENT
         JSR LOADPP
         JSR HASHFOLD
EDMAIN   
         JSR INIT
:PFIELDS LDA CURFLD
         PHA
         LDA #2
         STA CURFLD
:L1      JSR F1PTOZP      ;Set up pointers
         JSR FCENTER
         JSR PRFIELDS
         DEC CURFLD
         BPL :L1
         PLA
         STA CURFLD
         LDA #00
         STA REDRAWFLAG
         INC LASTDISP     ;Force redraw
         JSR F1PTOZP
:LOOP    
         LDA REDRAWFLAG
         BNE :PFIELDS
         LDA #10
         LDY BASEFLAG
         BPL :BASE10
         LDA #16
:BASE10  STA ARGBASE

         LDY DISPFLAG
         BEQ :DISP0
:DISP2   JSR DISPLAY2     ;1 and 2 use display1
         JMP :CONT
:DISP0   JSR DISPLAY0
:CONT    
         JSR PRFLAGS

         LDX #1           ;Print vars
         LDY #35
         JSR PLOPXY
         LDX #3
         LDA BFIELD1
         CMP MACBEGIN
         BNE :C1
         LDA BFIELD1+1
         CMP MACBEGIN+1
         BNE :CONT
         LDX #1           ;White
:C1      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

         LDA #10
         STA COLOR
         INX
         LDY #28
         JSR PLOPXY
         LDA LASTOCT
         ORA #$30         ;Conver to screen char
         JSR CHAROUT

         LDY #38
         STY COLUMN
         LDA LASTDUR
         CMP #10          ;Convert to base-24
         BCC :BLAH
         ADC #6
:BLAH    ADC #$30
         JSR CHAROUT


         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    LDA DISPFLAG
         CMP #1
         BNE :GET
         JSR DISPLAY1
:GET     JSR GETIN
         BEQ :WAIT
         CMP #'8'         ;0-7 set default octave
         BCS :NOTNUM
         CMP #'0'
         BCS :SETOCT

:NOTNUM  JSR ISMETA       ;A meta-key?
         BEQ :META
         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

:SETOCT  
         SBC #'0'
         STA LASTOCT
         RTS

:META    TXA
         ASL              ;16 chars each
         ASL
         ASL
         ASL
         TAX
         LDY #00
:LOOP    LDA METAKEYS,X
         BEQ :DONE
         STA $0277,Y      ;Keyboard buffer
         INX
         INY
         CPY #16          ;just in case...
         BNE :LOOP
:DONE    STY $C6
         RTS

DISPLAY1                  ;Display #1 -- macro status
         LDA #3
         STA COLOR
         LDX #10
         LDA LASTMAC
         CMP #12
         BCS :SECOND
         LDA #00          ;Macros 0-11
         STA AUX          ;offset
         LDA #$01
         STA TEMP         ;mask

:LOOP    LDY #35
         JSR PLOPXY
         LDX AUX
         JSR :PLOP        ;Global
         INX
         JSR :PLOP        ;field 1
         INX
         JSR :PLOP
         INX
         JSR :PLOP
         ASL TEMP
         BCC :CONT
         ROL TEMP
         LDA AUX
         ADC #07
         STA AUX
:CONT    LDX ROW
         INX
         CPX #22
         BCC :LOOP
         RTS

:SECOND  LDA #7           ;Macros 12-23
         STA AUX          ;offset
         LDA #$10
         STA TEMP         ;mask
         BNE :LOOP

:PLOP    LDA MACBYTE1,X
         LDY #'-'
         AND TEMP
         BEQ :C1
         LDY #'*'
:C1      TYA
         JMP CHAROUT


*
* Meta keys are C=-1 through C=-4
*
ISMETA   LDX #00
         CMP #129
         BEQ :RTS
         INX
         CMP #149
         BEQ :RTS
         INX
         CMP #150
         BEQ :RTS
         INX
         CMP #151
:RTS     RTS

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

*
* Display #0 -- System variables
*

CLEARDIS 
         LDX #10
:L2      LDY #28
         JSR PLOPXY
         LDY #10
         LDA #32
:LOOP    JSR CHAROUT
         DEY
         BPL :LOOP
         INX
         CPX #22
         BCC :L2
         RTS

DIS0                      ;Display text
         JSR CLEARDIS     ;Clear line
         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

         LDX #15
         LDY #29
         JSR PLOPXY
         JSR PLOPSTR
         TXT 'Last Dur:',00

         LDX #16
         LDY #29
         JSR PLOPXY
         JSR PLOPSTR
         TXT 'Last Oct:',00
         RTS

DISPLAY0 
         LDA #15
         STA COLOR
         LDA #0           ;Check if we switched to mode 0
         CMP LASTDISP     ;from elsewhere
         BEQ :SKIP
         STA LASTDISP     ;If so, print
         JSR DIS0
:SKIP    
         LDX #12
         LDY #35
         JSR PLOPXY
         LDA TRANSNOT
         JSR PHBYTE
         INX
         JSR PLOPXY
         LDA TRANSDUR
         JSR PHBYTE

         LDX #15
         LDY #38
         JSR PLOPXY
         LDA LASTDUR
         CMP #10          ;Convert to base-24
         BCC :BLAH
         ADC #6
:BLAH    ADC #$30
         JSR CHAROUT

         INX
         JSR PLOPXY
         LDA LASTOCT
         CLC
         ADC #$30
         JMP CHAROUT

*
* Display #1 -- Display macro names, 12 at a time
*

DISMAC   DFB 00           ;first 12 or last 12

DIS2     LDA #15
         STA COLOR
         JSR CLEARDIS
         LDX #00
         LDA LASTMAC
         CMP #12
         BCC :CONT
         LDX #12

:CONT    STX DISMAC       ;Offset
         LDX #11
         STX TEMP

:LOOP    LDA TEMP
         CLC
         ADC #10
         TAX
         LDY #28
         JSR PLOPXY
         LDA TEMP
         CLC
         ADC DISMAC
         PHA
         JSR PSMALL       ;Print macro number
         LDY #31
         STY COLUMN
         PLA
         ASL              ;Each name is 16 chars
         ASL
         ASL
         ASL
         TAY
         LDX #9           ;Only print 9 of them
         JSR PNAME2       ;Over in editor2
         DEC TEMP
         BPL :LOOP
         RTS

DISPLAY2 
         LDA #1
         CMP LASTDISP
         BEQ :SKIP
         STA LASTDISP
         JSR DIS2
:SKIP    LDX #12
         LDA #11          ;first 12 or last 12?
         CMP LASTMAC
         BCC :C1
         LDX #00
:C1      CPX DISMAC       ;Is it currently displayed?
         BEQ :C2
         JSR DIS2
:C2      LDA #11          ;Finally, indicate the current macro
         STA TEMP
         LDA #2
         STA COLOR

:LOOP    LDA TEMP
         CLC
         ADC #10
         TAX
         LDY #30
         JSR PLOPXY
         LDY #' '
         LDA TEMP
         CLC
         ADC DISMAC
         CMP LASTMAC
         BNE :C3
         LDY #'*'
:C3      TYA
         JSR CHAROUT
         DEC TEMP
         BPL :LOOP
         RTS

         PUT 'editor2.c.s'
         PUT 'eddirect.m.s'
         PUT 'ededit.cc.s'
         PUT 'edother.dd.s'
