       
*
* Editor commands
*

RASTFLAG ENT
         DFB 00
SHARPVAL DFB 00           ;Raise note by 12

* These flags are now stored with the player
         DO 0
SINGLEKEY DFB 00          ;Single keypress mode
BASEFLAG DFB 00           ;Base 10/16 toggle
DISPFLAG DFB 00           ;Change display
MACFLAG  DFB 00           ;Toggle macro name display
         FIN

EDIT     
         JSR ISEDIT
         TXA
         ASL
         TAX
         LDA EDLOC,X
         STA :JMP+1
         LDA EDLOC+1,X
         STA :JMP+2
:JMP     JMP $C000

ISEDIT   
         LDX ISFLAG
         BNE :RTS
         LDX #36
:LOOP    CMP EDTAB,X
         BEQ :RTS
         DEX
         BPL :LOOP
:RTS     RTS

EDTAB    
         DFB 17           ;down
         DFB 29           ;right
         DFB 145          ;up
         DFB 157          ;left
         DFB 147          ;shift-clr
         DFB 148          ;insert
         DFB 19           ;home
         DFB 20           ;delete
         DFB 13           ;return
         TXT '.'
         TXT '<'
         TXT '>'
         TXT '@'
         DFB 186          ;shift-@
         DFB 189          ;C=-x
         DFB 188          ;C=-c
         DFB 175          ;C=-p
         TXT '+'
         TXT '-'
         DFB 222          ;Shift-^
         DFB 187          ;C=-f
         DFB '%'          ;shift-5
         DFB 152          ;C=-5
         TXT '*'
         DFB 166          ;C=-+
         DFB 220          ;C=--
         TXT '='
         TXT '!'          ;Single-keypress mode
         TXT '#'
         DFB 219          ;shift-+
         DFB 221          ;shift--
         TXT '['          ;dec default macro
         TXT ']'          ;inc macro
         DFB $22          ;" -- Toggle macro display
         TXT '$'          ;Change number base
         TXT '&'          ;Change main display
         DFB 39           ;single quote

EDLOC    
         DA DOWN
         DA RIGHT
         DA UP
         DA LEFT
         DA SHIFTCLR
         DA INS
         DA HOME
         DA DEL
         DA RET
         DA DOT
         DA GBEGIN
         DA GEND
         DA GROW
         DA GOMARK
         DA CUT
         DA COPY
         DA PASTE
         DA INCOCT
         DA DECOCT
         DA CLRSID
         DA MARKFOLD
         DA FOLD
         DA DELFOLD
         DA GATETOG
         DA DOUBTIME
         DA HALFTIME
         DA TOGRAST
         DA SINGLEK
         DA SETSHARP
         DA INCDUR
         DA DECDUR
         DA DECMAC
         DA INCMAC
         DA MACTOG
         DA BASETOG
         DA DISPTOG
         DA RETTOG

RETTOG   
         LDA #$FF
         EOR RETFLAG
         STA RETFLAG
         RTS

DECMAC   
         LDA LASTMAC
         BEQ :RTS
         DEC LASTMAC
         LDX $028D        ;C=[ to dec by 6
         CPX #1
         BEQ :RTS
         SBC #6
         BCS :C1
         LDA #00
:C1      STA LASTMAC
:RTS     RTS

INCMAC   
         LDA LASTMAC
         CMP #23
         BEQ :RTS
         INC LASTMAC
         LDX $028D
         DEX
         BEQ :RTS
         CLC
         ADC #6
         CMP #24
         BCC :C1
         LDA #23
:C1      STA LASTMAC
:RTS     RTS

MACTOG   
         LDA MACFLAG
         EOR #$FF
         STA MACFLAG
         INC REDRAWFLAG
         RTS

BASETOG  
         LDA BASEFLAG
         EOR #$FF
         STA BASEFLAG
         INC REDRAWFLAG
         RTS

DISPTOG  
         DEC DISPFLAG
         BPL :RTS
         LDA #2           ;Three display modes
         STA DISPFLAG
:RTS     RTS

INCDUR   
         LDA LASTDUR
         CMP #23
         BEQ :RTS
         INC LASTDUR
:RTS     RTS

DECDUR   
         LDA LASTDUR
         BEQ :RTS
         DEC LASTDUR
:RTS     RTS

SINGLEK                   ;Single keypress toggle
         LDA #$FF
         EOR SINGLEKEY
         STA SINGLEKEY
         RTS

SETSHARP 
         LDA #12          ;Add twelve if # pressed
         STA SHARPVAL     ;ahead of time
         RTS

TOGRAST  
         LDA RASTFLAG
         EOR #1
         STA RASTFLAG
         RTS

DOUBTIME 
         LDA PLAYFLAG
         BEQ RTS1
         LSR PLAYTIME+1
         ROR PLAYTIME
SETINT   LDA #00
         STA $DC0E
         LDA PLAYTIME
         STA $DC04
         LDA PLAYTIME+1
         STA $DC05
         LDA #01
         STA $DC0E
RTS1     RTS

HALFTIME 
         LDA PLAYFLAG
         BEQ RTS1
         ASL PLAYTIME
         ROL PLAYTIME+1
         JMP SETINT

GATETOG  
         LDA CURFLD
         ASL
         ASL
         ASL
         SEC
         SBC CURFLD       ;*7
         TAX
         LDA SHADOW+4,X
         EOR #01
         STA SHADOW+4,X
         STA $D404,X
         RTS

INCOCT   
         LDA LASTOCT
         CMP #7
         BCS :RTS
         INC LASTOCT
:RTS     RTS

DECOCT   
         LDA LASTOCT
         BEQ :RTS
         DEC LASTOCT
:RTS     RTS

GBEGIN   
         LDA $028D
         AND #2
         BEQ :PAGEUP
:BEGIN   LDA #00
         TAX
         JMP GOROW
:PAGEUP  
         LDA CURFLD
         ASL
         TAX
         LDA POINT1
         SEC
         SBC BFIELD1,X
         STA TEMP
         LDA POINT1+1
         SBC BFIELD1+1,X
         TAX
         LDA TEMP
         SEC
         SBC #40
         BCS :C1
         DEX
:C1      CPX #$FF
         BEQ :BEGIN
         JMP GOROW

GEND     
         LDA $028D
         AND #2
         BEQ :PAGEDN
         LDA CURFLD
         ASL
         TAY
         LDA BFIELD1+2,Y
         SEC
         SBC BFIELD1,Y
         STA TEMP
         LDA BFIELD1+3,Y
         SBC BFIELD1+1,Y
         TAX
         LDA TEMP
         SEC
         SBC #2
         BCS :CONT
         DEX
:CONT    JMP GOROW

:PAGEDN  
         JSR INFOLD
         BNE :C2
         LDA OPENFOLD,X
         BEQ :C2

         LDA FOLDHI1,X    ;If closed, move to end of
         SEC              ;field
         SBC #1
         STA POINT1
         LDA FOLDHI2,X
         SBC #00
         STA POINT1+1

:C2      LDA CURFLD
         ASL
         TAX
         LDA POINT1
         SEC
         SBC BFIELD1,X
         STA TEMP
         LDA POINT1+1
         SBC BFIELD1+1,X
         TAX
         LDA TEMP
         CLC
         ADC #40
         BCC :C1
         INX
:C1      JMP GOROW

GROW                      ;Goto specified line
         LDX #23
         LDY #7
         JSR PLOPXY
         JSR PLOPSTR
         TXT 'Go to line #',00
         JSR CLEARMSG
         JSR READARG
         LDA STRBUF
         BNE :CONT
:RTS     RTS
:CONT    LDA #<STRBUF
         LDX #>STRBUF
         LDY #10          ;Base 10
         JSR ASCTONUM
         LDA ACC
         ORA ACC+1
         BEQ :RTS
         ASL ACC          ;x2 gives mem offset
         ROL ACC+1
         LDX ACC+1
         LDA ACC
         SEC
         SBC #2           ;Start at line 1
         BCS GOROW
         DEX

*
* Go to the line number in the current field
* specified in (.A,.X) = (lo,hi) = memory OFFSET
* (line number * 2)
*
* line number = 1, 2, ...
*
GOROW    
         STA ACC
         STX ACC+1
         LDA CURFLD
         ASL
         TAY
         LDA ACC
         CLC
         ADC BFIELD1,Y
         STA EXT
         TXA
         ADC BFIELD1+1,Y
         STA EXT+1

         LDA EXT          ;Make sure not at end
         SEC
         SBC BFIELD1+2,Y
         LDA EXT+1
         SBC BFIELD1+3,Y
         BCS :EXIT

         LDA #00
         STA BLFLAG
         STA FOLDFLAG

         LDA EXT
         STA POINT1
         LDA EXT+1
         STA POINT1+1
         JSR INFOLD
         BNE :CONT
         LDA OPENFOLD,X
         BEQ :CONT
         LDA FOLDLO1,X
         STA POINT1
         LDA FOLDLO2,X
         STA POINT1+1
:CONT    JSR ZPTOF1P

         LSR ACC+1        ;This gives the row number
         ROR ACC

         LDX #11
         LDA ACC
         CMP #11          ;Center the field
         BCS :SETROW
         LDA ACC+1
         BNE :SETROW
         LDX ACC          ;Row 11 or row ACC+1 if ACC<11
         INX
:SETROW  TXA
         LDY CURFLD
         STA F1ROW,Y

:EXIT    RTS

GOMARK   
         JSR ZPTOF1P
         LDX #23
         LDY #7
         JSR PLOPXY
         JSR PLOPSTR
         TXT 'Go to marker #',00
         JSR CLEARMSG
         JSR READARG
         LDA STRBUF
         BNE :CONT
:RTS     RTS
:CONT    LDA #00
         STA BLFLAG
         JSR HILIGHT
         LDA #<STRBUF
         LDX #>STRBUF
         LDY #10          ;Base 10
         JSR ASCTONUM
         LDA ACC
         ASL
         TAY
         LDX #06
:LOOP    LDA MARKERS,Y
         SEC
         SBC BFIELD1,X
         STA TEMP
         LDA MARKERS+1,Y
         SBC BFIELD1+1,X
         BCS :YUP
:NOPE    DEX
         DEX
         BPL :LOOP
:YUP     CPX #6           ;Range check
         BCS :RTS
         STX CURFLD
         LSR CURFLD
         TAX
         LDA TEMP
         JMP GOROW

DOT      
         JSR TRNSPOSE     ;Transpose if ok
         JSR PLNOTE       ;Fall through to DOWN!

DOWN     
         JSR INFOLD
         BNE :C1

         LDA #00
         STA FOLDFLAG
         STA BLFLAG

:C1      JSR ADD2P        ;Add a ROW to pointer
         JSR ATEND
         BNE :CONT
         LDX #2
         JMP BACKP
:CONT    JSR INCCLIP
         LDX CURFLD
         LDA F1ROW,X
         CMP #21
         BCS :RTS
         INC F1ROW,X
:RTS     RTS

RIGHT    
         LDA #00
         STA BLFLAG
         STA FOLDFLAG
         LDX CURFLD
         CPX #2
         BEQ :RTS
         JSR PRFIELDS     ;Erase hilight, cut/paste
         JSR ZPTOF1P      ;Copy point1 to F1P
         INC CURFLD
         JMP F1PTOZP
:RTS     RTS

UP       
         JSR ATBEGIN
         BEQ :RTS
         LDX #2
         JSR BACKP

         JSR INFOLD
         BNE :C1
         LDA #00
         STA BLFLAG
         STA FOLDFLAG

:C1      JSR DECCLIP
         LDX CURFLD
         LDA F1ROW,X
         CMP #1
         BEQ :RTS
         DEC F1ROW,X
:RTS     RTS

LEFT     
         LDA #00
         STA BLFLAG
         STA FOLDFLAG
         LDX CURFLD
         BEQ :RTS
         JSR PRFIELDS
         JSR ZPTOF1P
         DEC CURFLD
         JMP F1PTOZP
:RTS     RTS

SHIFTCLR 
         LDA $028D        ;See if C=-Home or shift-clr
         AND #2
         BEQ :CLR

         LDA #00
         STA BLFLAG
         STA FOLDFLAG
         LDA CURFLD
         ASL
         TAX
         LDA BOOKMARK,X
         PHA
         LDA BOOKMARK+1,X
         TAX
         PLA
         JSR GOROW

         JMP ODONE

:CLR     LDA #00
         JSR SETACT       ;Clear action box
         LDA #1
         STA COLOR
         LDX #23
         LDY #7
         JSR PLOPXY
         JSR PLOPSTR
         TXT 'Clear all data? y/n',00
         JSR CLEARMSG
:WAIT    JSR GETIN
         BEQ :WAIT
         CMP #'y'
         BNE :RTS

         LDA MACBEGIN
         STA BFIELD1
         LDA MACBEGIN+1
         STA BFIELD1+1
         LDY #0
:LOOP    LDA BFIELD1,Y
         CLC
         ADC #2
         STA BFIELD1+2,Y
         LDA BFIELD1+1,Y
         ADC #00
         STA BFIELD1+3,Y
         INY
         INY
         CPY #8
         BNE :LOOP
         LDA FIELDEND
         STA FREBOT
         LDA FIELDEND+1
         STA FREBOT+1

         JSR SAVEPP
         JSR MUSINIT

         LDY #5
:LOOP2   LDA BFIELD1,Y
         STA F1P,Y
         LDA #$88         ;VSTOP code
         STA (MACBEGIN),Y
         DEY
         BPL :LOOP2

         LDY #63
         LDA #00
         STA NUMFOLDS
         STA BLFLAG
         STA FOLDFLAG
:LOOP3   STA MARKERS,Y
         DEY
         BPL :LOOP3

         PLA
         PLA
         JMP MUSICED

:RTS     RTS

HOME                      ;Set bookmark
         LDA CURFLD
         ASL
         TAX
         LDA POINT1
         SEC
         SBC BFIELD1,X
         STA BOOKMARK,X
         LDA POINT1+1
         SBC BFIELD1+1,X
         STA BOOKMARK+1,X
         RTS

*
* Move everything back 2 bytes.
*
DELDIST  DA 0
DEL      
         LDA #2
         STA DELDIST
         LDA #00
         STA DELDIST+1

*
* Move everything back DELDIST bytes
*
DELETE   
         JSR ZPTOF1P      ;Save POINT1

         LDA CURFLD
         ASL
         TAX
         LDA POINT1
         STA EXT
         CLC
         ADC DELDIST
         STA POINT1
         LDA POINT1+1
         STA EXT+1
         ADC DELDIST+1
         STA POINT1+1
         CMP BFIELD1+3,X  ;Out of range?
         BCC :OK
         BNE :JDONE
         LDA POINT1
         CMP BFIELD1+2,X
         BCC :OK
:JDONE   JMP F1PTOZP

:OK      
         LDX NUMFOLDS     ;Fix up the folds!
         BEQ :NOFOLD
:FLOOP   DEX
         LDA EXT          ;Old POINT1
         CMP FOLDLO1,X
         LDA EXT+1
         SBC FOLDLO2,X
         BCS :CONT3
         JSR TWELVEX
         LDY #00
         LDA (ACC),Y
         SEC
         SBC DELDIST
         STA (ACC),Y
         INY
         LDA (ACC),Y
         SBC DELDIST+1
         STA (ACC),Y
:CONT3   
         LDA FOLDHI1,X    ;High address
         CMP EXT
         LDA FOLDHI2,X
         SBC EXT+1
         BCC :CONT4

         LDA FOLDHI1,X    ;Don't delete if end=beginning+1
         SBC FOLDLO1,X
         CMP #1
         BNE :BLAH
         LDA FOLDHI2,X
         CMP FOLDLO2,X
         BEQ :CONT4

:BLAH    JSR TWELVEX
         LDY #2
         LDA (ACC),Y
         SEC
         SBC DELDIST
         STA (ACC),Y
         INY
         LDA (ACC),Y
         SBC DELDIST+1
         STA (ACC),Y
:CONT4   TXA
         BNE :FLOOP

:NOFOLD  LDA POINT1
         STA BLSTART
         LDA POINT1+1
         STA BLSTART+1
         LDA EXT          ;Old POINT1
         STA BLDEST
         LDA EXT+1
         STA BLDEST+1

         LDA FREBOT
         STA BLEND
         LDA FREBOT+1
         STA BLEND+1
         JSR BLMOVE

         LDA FREBOT
         SEC
         SBC DELDIST
         STA FREBOT
         LDA FREBOT+1
         SBC DELDIST+1
         STA FREBOT+1

         LDA CURFLD
         ASL
         STA TEMP
         LDX #6           ;Decrement pointers
:LOOP    LDA BFIELD1,X    ;Both zero page...
         SEC
         SBC DELDIST
         STA BFIELD1,X
         LDA BFIELD1+1,X
         SBC DELDIST+1
         STA BFIELD1+1,X
:CONT    
         DEX
         DEX
         CPX TEMP
         BEQ :DONE
         LDA F1P,X        ;...and editor
         SEC
         SBC DELDIST
         STA F1P,X
         LDA F1P+1,X
         SBC DELDIST+1
         STA F1P+1,X
         JMP :LOOP

:DONE    JSR F1PTOZP      ;Restore POINT1

         LDX #62          ;Fix up the markers
:LOOP2   LDA MARKERS,X
         CLC              ;Subtract one more, to skip
         SBC POINT1       ;any marker=POINT1
         LDA MARKERS+1,X
         SBC POINT1+1
         BCC :CONT2
         LDA MARKERS,X
         SBC DELDIST
         STA MARKERS,X
         LDA MARKERS+1,X
         SBC DELDIST+1
         STA MARKERS+1,X

         CMP POINT1+1     ;Maybe we deleted a marker
         BCS :CONT2
         LDA MARKERS,X
         CMP POINT1
         BCS :CONT2
         LDA #00
         STA MARKERS,X
         STA MARKERS+1,X

:CONT2   DEX
         DEX
         BPL :LOOP2

         JMP HASHFOLD

*
* RET -- move everything forward 2 bytes and advance
*        pointers.
RET      
         JSR INS
         JSR INCCLIP
         JSR ADD2P
         LDX CURFLD
         LDA F1ROW,X
         CMP #21
         BCS :DONE
         INC F1ROW,X
:DONE    RTS              ;And advance the pointer


*
* INS -- Move everything forwards by two
*
INSDIST  DA 00
INS      
         LDA #2
         STA INSDIST
         LDA #00
         STA INSDIST+1

*
* INSERT -- Move everything forwards by INSDIST
*
INSERT   
         LDX #62          ;Fix up the markers
:LOOP2   LDA MARKERS,X
         SEC
         SBC POINT1
         LDA MARKERS+1,X
         SBC POINT1+1
         BCC :CONT2
         LDA MARKERS,X
         CLC
         ADC INSDIST
         STA MARKERS,X
         LDA MARKERS+1,X
         ADC INSDIST+1
         STA MARKERS+1,X
:CONT2   DEX
         DEX
         BPL :LOOP2

         LDX NUMFOLDS     ;Fix up the folds!
         BEQ :FDONE
:FLOOP   DEX
         LDA FOLDLO1,X
         CMP POINT1
         LDA FOLDLO2,X
         SBC POINT1+1
         BCC :CONT3
         JSR TWELVEX
         LDY #00
         LDA (ACC),Y
         CLC
         ADC INSDIST
         STA (ACC),Y
         INY
         LDA (ACC),Y
         ADC INSDIST+1
         STA (ACC),Y
:CONT3   
         LDA FOLDHI1,X    ;High address
         CMP POINT1
         LDA FOLDHI2,X
         SBC POINT1+1
         BCC :CONT4
         JSR TWELVEX
         LDY #2
         LDA (ACC),Y
         CLC
         ADC INSDIST
         STA (ACC),Y
         INY
         LDA (ACC),Y
         ADC INSDIST+1
         STA (ACC),Y
:CONT4   TXA
         BNE :FLOOP

:FDONE   
         LDA POINT1       ;Move the stuff
         STA BLSTART
         CLC
         ADC INSDIST
         STA BLDEST
         LDA POINT1+1
         STA BLSTART+1
         ADC INSDIST+1
         STA BLDEST+1

         LDA FREBOT
         STA BLEND
         LDA FREBOT+1
         STA BLEND+1
         JSR BLMOVE

         LDA CURFLD
         ASL
         STA TEMP
         LDA FREBOT
         CLC
         ADC INSDIST
         STA FREBOT
         LDA FREBOT+1
         ADC INSDIST+1
         STA FREBOT+1

         LDX #6           ;Increment pointers
:LOOP    LDA BFIELD1,X    ;Both zero page...
         CLC
         ADC INSDIST
         STA BFIELD1,X
         LDA BFIELD1+1,X
         ADC INSDIST+1
         STA BFIELD1+1,X
:CONT    
         DEX
         DEX
         CPX TEMP
         BEQ :DONE
         LDA F1P,X        ;...and editor
         CLC
         ADC INSDIST
         STA F1P,X
         LDA F1P+1,X
         ADC INSDIST+1
         STA F1P+1,X
         JMP :LOOP

:DONE    JMP HASHFOLD     ;Reset fold stuff

* Increment the clip range pointers
* Piggyback FOLD stuff into here too.
INCCLIP  
         LDA CLIPHI
         CLC
         ADC #2
         STA CLIPHI
         BCC :CONT
         INC CLIPHI+1
:CONT    LDA FOLDTOP
         CLC
         ADC #2
         STA FOLDTOP
         BCC :RTS
         INC FOLDTOP+1
:RTS     RTS

* Decrement the same, and kill if zero
DECCLIP  
         LDA CLIPHI
         SEC
         SBC #2
         STA CLIPHI
         BCS :CONT
         DEC CLIPHI+1
:CONT    LDA CLIPHI
         CMP CLIPLO
         LDA CLIPHI+1
         SBC CLIPLO+1
         BCS :FOLD
         LDA #00
         STA BLFLAG

:FOLD    LDA FOLDTOP
         SEC
         SBC #2
         STA FOLDTOP
         BCS :C2
         DEC FOLDTOP+1
:C2      LDA FOLDTOP
         CMP FOLDBOT
         LDA FOLDTOP+1
         SBC FOLDBOT+1
         BCS :RTS
         LDA #00
         STA FOLDFLAG
:RTS     RTS

*
* CUT text
*
BLFLAG   DFB 00
CUT      
         JSR INFOLD
         BNE :C1
         LDA #00
         STA BLFLAG
         RTS

:C1      LDA BLFLAG
         EOR #$FF
         STA BLFLAG
         BEQ :CUT
         LDA POINT1
         STA CLIPLO
         CLC
         ADC #1
         STA CLIPHI
         LDA POINT1+1
         STA CLIPLO+1
         ADC #00
         STA CLIPHI+1
         RTS

:CUT     
         JSR COPYCLIP

         LDA CLIPLO
         STA POINT1
         LDA CLIPLO+1
         STA POINT1+1

         LDA CLIPDIST
         CLC
         ADC #1
         STA DELDIST
         LDA CLIPDIST+1
         ADC #00
         STA DELDIST+1
         JSR DELETE
         JMP FCENTER

COPY     
         JSR INFOLD
         BNE :C1
         LDA #00
         STA BLFLAG
         RTS

:C1      LDA BLFLAG
         EOR #$FF
         STA BLFLAG
         BEQ COPYCLIP
         LDA POINT1
         STA CLIPLO
         CLC
         ADC #1
         STA CLIPHI
         LDA POINT1+1
         STA CLIPLO+1
         ADC #00
         STA CLIPHI+1
         RTS

* Copy text to clipboard
COPYCLIP 
         LDA POINT1
         CLC
         ADC #1
         STA CLIPHI
         STA BLEND
         LDA POINT1+1
         ADC #00
         STA CLIPHI+1
         STA BLEND+1

         LDA CLIPLO
         STA BLSTART
         LDA CLIPLO+1
         STA BLSTART+1

         LDA CLIPHI
         SEC
         SBC CLIPLO
         STA CLIPDIST
         LDA CLIPHI+1
         SBC CLIPLO+1
         STA CLIPDIST+1

         LDA FRETOP
         SEC
         SBC CLIPDIST
         STA BLDEST
         STA CLIP
         LDA FRETOP+1
         SBC CLIPDIST+1
         STA BLDEST+1
         STA CLIP+1

         JMP BLMOVE

* Paste clipboard -> music
PASTE    
         LDA CLIP+1
         CMP #$FF
         BEQ :EXIT

         LDA CLIPDIST
         CLC
         ADC #1
         STA INSDIST
         LDA CLIPDIST+1
         ADC #00
         STA INSDIST+1
         JSR INSERT

         LDA CLIP
         STA BLSTART
         LDA CLIP+1
         STA BLSTART+1

         LDA FRETOP
         STA BLEND
         LDA FRETOP+1
         STA BLEND+1

         LDA POINT1
         STA BLDEST
         LDA POINT1+1
         STA BLDEST+1
         JMP BLMOVE
:EXIT    RTS

*
* CLRSID
*
* Clear all SID registers.
*
CLRSID   
         LDA #00
         JSR SETACT       ;Clear action box
         LDA #1
         STA COLOR
         LDX #23
         LDY #7
         JSR PLOPXY
         JSR PLOPSTR
         TXT 'Clear SID registers? y/n',00
         JSR CLEARMSG
:WAIT    JSR GETIN
         BEQ :WAIT
         CMP #'y'
         BNE :RTS
         LDA #00
         LDX #24
:LOOP    STA SHADOW,X
         STA SID,X
         DEX
         BPL :LOOP
:RTS     RTS

*
* FOLD and MARKFOLD
*
* Folds are initiated in much the same way as block
* operations.  When a new fold is created it is
* placed at the bottom of free mem, i.e. right after
* the field data.
*
FOLDFLAG DFB 00
NUMFOLDS DFB 00
FOLDBOT  DA 0
FOLDTOP  DA 0

MARKFOLD 
         JSR INFOLD
         BNE :C0
         LDA #00
         STA FOLDFLAG
         RTS
:C0      
         LDA FOLDFLAG
         EOR #$FF
         STA FOLDFLAG
         BEQ :NEWFOLD

         LDA POINT1
         STA FOLDBOT
         CLC
         ADC #1
         STA FOLDTOP
         LDA POINT1+1
         STA FOLDBOT+1
         ADC #00
         STA FOLDTOP+1
         RTS

:NEWFOLD 
         LDA FREBOT
         STA ACC
         LDA FREBOT+1
         STA ACC+1

         LDY #00
:L1      LDA FOLDBOT,Y
         STA (ACC),Y
         INY
         CPY #4
         BNE :L1

         LDA #01
         STA (ACC),Y      ;Close

         LDA #00
         JSR SETACT       ;Clear

         LDA #10          ;Lt. red
         STA COLOR
         LDX #23
         LDY #7
         JSR PLOPXY
         CLC
         JSR PLOT
         JSR CLEARMSG
         JSR STROUT
         DFB 150          ;Light red
         TXT 'Fold name:',00
         LDA $0286
         STA $0287

         LDA #00
         STA TEMP
         LDA #5
         CLC
         ADC ACC          ;Now points to name
         STA ACC
         BCC :GETKEY
         INC ACC+1

:GETKEY  LDA #00
         STA BLNSW
:WAIT    JSR GETIN
         BEQ :WAIT
         CMP #13
         BEQ :CONT
         CMP #20
         BEQ :CONT
         CMP #32
         BCC :WAIT
         TAX
         BPL :CONT
         CMP #192
         BCC :WAIT

:CONT    
         LDX #1
         STX BLNCT
:WAIT2   LDX BLNON
         BNE :WAIT2
         INC BLNSW

         CMP #20
         BEQ :DEL
         CMP #13
         BEQ :DONE

         LDY TEMP
         CPY #7
         BEQ :GETKEY
         STA (ACC),Y
         JSR CHROUT
         INC TEMP
         BNE :GETKEY

:DEL     LDY TEMP
         BEQ :GETKEY
         LDA #157
         JSR CHROUT
         LDA #32
         JSR CHROUT
         LDA #157
         JSR CHROUT
         DEC TEMP
         BPL :GETKEY

:DONE    LDA FREBOT
         CLC
         ADC #12
         STA FREBOT
         BCC :C1
         INC FREBOT+1
:C1      LDY TEMP
         LDA #32
:C2      CPY #7
         BEQ :C3
         STA (ACC),Y
         INY
         BNE :C2
:C3      JSR HASHFOLD
         LDA CURFLD
         ASL
         TAX
         LDA FOLDBOT      ;Find offset
         SEC
         SBC BFIELD1,X
         PHA
         LDA FOLDBOT+1
         SBC BFIELD1+1,X
         TAX
         PLA
         JMP GOROW


*
* HASHFOLD
*   This routine computes the tables that make
*   fold checks and such go quickly.  There are
*   six tables, starting at $0800 -- open/closed,
*   fold lo, fold hi, and foldfiel.
*
HASHFOLD 
         LDA FIELDEND
         STA ACC
         LDA FIELDEND+1
         STA ACC+1
         LDX #00

:LOOP    LDA ACC
         CMP FREBOT
         LDA ACC+1
         SBC FREBOT+1
         BCS :DONE

         LDY #00
         LDA (ACC),Y
         STA FOLDLO1,X
         INY
         LDA (ACC),Y
         STA FOLDLO2,X
         INY
         LDA (ACC),Y
         STA FOLDHI1,X
         INY
         LDA (ACC),Y
         STA FOLDHI2,X
         INY
         LDA (ACC),Y
         STA OPENFOLD,X

         LDY #4
:LOOP2   LDA FOLDLO1,X
         CMP BFIELD1,Y
         LDA FOLDLO2,X
         SBC BFIELD1+1,Y
         BCC :C2
         LDA FOLDHI1,X
         CMP BFIELD1+2,Y
         LDA FOLDHI2,X
         SBC BFIELD1+3,Y
         BCC :HAH
:C2      DEY
         DEY              ;If we hit zero, then must
         BNE :LOOP2       ;be in 1st field!
:HAH     TYA
         LSR
         STA FOLDFIEL,X   ;Field fold is contained in

         LDA ACC
         CLC
         ADC #12
         STA ACC
         BCC :C1
         INC ACC+1

:C1      INX
         BNE :LOOP
:DONE    STX NUMFOLDS
         RTS

*
* FOLD/UNFOLD
*
FOLD     
         JSR INFOLD       ;Are we even in a fold?
         BEQ :CONT
         RTS

:CONT    LDA OPENFOLD,X
         EOR #01
         STA OPENFOLD,X

         JSR TWELVEX
         LDY #4
         LDA OPENFOLD,X
         STA (ACC),Y

         LDY #0           ;Go to beginning
         LDA CURFLD
         ASL
         TAX
         LDA (ACC),Y
         SEC
         SBC BFIELD1,X
         PHA
         INY
         LDA (ACC),Y
         SBC BFIELD1+1,X
         TAX
         PLA
         JMP GOROW

TWELVEX  STX ACC          ;Compute 12*x
         LDA #00
         STA ACC+1
         TXA
         ASL ACC
         ROL ACC+1
         ADC ACC
         BCC :C1
         INC ACC+1
:C1      ASL
         ROL ACC+1
         ASL
         ROL ACC+1
         ADC FIELDEND
         STA ACC
         LDA ACC+1
         ADC FIELDEND+1
         STA ACC+1

:RTS     RTS

*
* DELFOLD
*   If inside of a fold, then remove it
*
DELFOLD  
         JSR INFOLD
         BEQ :C1
:RTS     RTS

:C1      LDA #00
         JSR SETACT
         LDX #23
         LDY #7
         JSR PLOPXY
         JSR PLOPSTR
         TXT 'Dissolve this fold? (y/n)',00
         JSR CLEARMSG
:WAIT    JSR GETIN
         BEQ :WAIT
         CMP #'y'
         BNE :RTS
         JSR INFOLD
         JSR TWELVEX
         LDA ACC
         STA BLDEST
         CLC
         ADC #12
         STA BLSTART
         LDA ACC+1
         STA BLDEST+1
         ADC #00
         STA BLSTART+1

         LDA FREBOT
         STA BLEND
         LDA FREBOT+1
         STA BLEND+1

         JSR BLMOVE
         LDA FREBOT
         SEC
         SBC #12
         STA FREBOT
         BCS :CONT
         DEC FREBOT+1
:CONT    JMP HASHFOLD

*
* INFOLD
*   Check to see if we are inside a fold.  If so,
*   X is set to the fold number and Z is set.
*   Z clear otherwise.
*
INFOLD   
         LDX NUMFOLDS
         BEQ :RTS
:LOOP    DEX
         LDA CURFLD
         CMP FOLDFIEL,X
         BEQ :C1
:NOPE    TXA
         BNE :LOOP
:RTS     DEX
         RTS

:C1      LDA POINT1
         CMP FOLDHI1,X
         LDA POINT1+1
         SBC FOLDHI2,X
         BCS :NOPE
         LDA POINT1
         CMP FOLDLO1,X
         LDA POINT1+1
         SBC FOLDLO2,X
         BCC :NOPE
         LDA #00
         RTS
