*-------------------------------
*
* instrument.s
*
* This is the first part of what will hopefully become
* my music compostion package.  It is for creating
* instruments, and experimenting with SID.  Someday
* some waveform analysis may appear but not today!
*
* Stephen L. Judd
* 8/18/96
*
* Standalone version, 9.15.96
*
* Updated: 6/98 -- Displays instruments, updated load/save,
*                  No longer copies shadow to SID -- only
*                  changes affect SID
*

* ORG $0801

         DSK '@0:insted'
         REL
         PUT 'playvars.i.s'
         PUT 'diskio.ext.s'

MUSICED  EXT
MACMAN   EXT

*
* Constants
*

CLEAR    = 147            ;Clear screen
HOME     = 19
RVOF     = 146            ;Reverse off
UP       = 145
DN       = 17
LEFT     = 157
RIGHT    = 29
LGRN     = 153
WHITE    = 5

SID      = $D400

ATK      EQU 64           ;Codes to tell which function I
DKY      EQU 65           ;am performing.  0-31 for SID
SUS      EQU 66
RLS      EQU 67
TRI      EQU 68
SAW      EQU 69
PUL      EQU 70
NOI      EQU 71
WID      EQU 72           ;pulse width
FRQ      EQU 73
GAT      EQU 74
SYN      EQU 75
RNG      EQU 76
TST      EQU 77
FIL1     EQU 78           ;Filter on
FIL2     EQU 79
FIL3     EQU 80
FLO      EQU 81
FBP      EQU 82           ;Bandpass
FHI      EQU 83
RES      EQU 84           ;Resonance
CUT      EQU 85           ;Cutoff freq
ILOAD    EQU $FA
ISAVE    EQU $FB
MUSED    EQU $FC
MACED    EQU $FD

*
* Variables
*
POINT    EQU $FE          ;A pointer to use
TEMP2    EQU $FC
TEMP1    EQU $FB
JOB      EQU $FA          ;Attack, decay, etc.
FAQ1     EQU $61          ;Used by conversion routines
*VOICE EQU COFFSET ;Current voice selected
VOICE    DFB 00

*
* Kernal
*
CHROUT   EQU $FFD2
PLOT     EQU $FFF0
KGETIN   EQU $FFE4

*-------------------------------
* Some useful macros

GOTOXY   MAC
         LDX #]1
         LDY #]2
         CLC
         JSR PLOT
         <<<

SETXY    MAC              ;Set POINT to point to a row,col
         LDA #<]1*40+1024+]2
         STA POINT
         LDA #>]1*40+1024+]2
         STA POINT+1
         <<<

*-------------------------------
*
* Basic header
         DO 0

         DA $0800         ;Sneaky line link
         DA 6581
         DFB $9E
         TXT '2073 slj 9/15/96'
         DFB 00,00,00

         FIN

*-------------------------------
*
* Init stuff
*
INIT     

INSTED   ENT

         LDX #24
:L1      LDA SHADOW,X
         STA SID,X
         DEX
         BPL :L1

         LDA SHADOW+24    ;Volume
         ORA #15
         STA SHADOW+24
         STA SID+24

         LDA #128
         STA 650          ;All keys repeat
         LDA #ATK
         STA JOB          ;Start on Attack

         LDA #00
         STA $D020
         LDA #6
         STA $D021
INIT2    LDA #147
         JSR CHROUT
         LDA #142         ;Upper case
         JSR CHROUT
         JMP SETUP

STEXT    DFB HOME,RVOF,DN,DN,DN,DN,DN,DN,DN,DN,DN,DN
         DFB LGRN
         TXT 'attack :'
         DFB WHITE
         TXT '00  '
         DFB LGRN
         TXT 'waveform:'
         DFB 13
         TXT 'decay  :'
         DFB WHITE
         TXT '00   tri saw pul noi'
         DFB 13,LGRN
         TXT 'sustain:'
         DFB WHITE
         TXT '00  '
         DFB LGRN
         TXT 'pulse width:'
         DFB WHITE
         TXT '   0'
         DFB 13,LGRN
         TXT 'release:'
         DFB WHITE
         TXT '00  '
         DFB LGRN
         TXT 'freq:'
         DFB WHITE
         TXT '    0'
         DFB 13,DN
         TXT ' gate ring'
         DFB 13
         TXT ' sync test'
         DFB 13,DN,LGRN
         TXT 'filter:'
         DFB WHITE
         TXT ' v1 v2 v3'
         DFB 13
         TXT ' low pass'
         DFB 13
         TXT ' band pass'
         DFB 13
         TXT ' high pass'
         DFB 13,DN,LGRN
         TXT 'resonance:'
         DFB WHITE
         TXT '00'
         DFB 13,LGRN
         TXT 'cutoff:'
         DFB WHITE
         TXT '0000'
         DFB HOME,RVOF,WHITE
         TXT ' v1   v2   v3   hi '
         DFB 98           ;vertical bar
         DFB HOME,DN,DN,DN,DN,DN,DN,DN,DN,WHITE
         DFB 99,99,99,99,99,99,99,99  ;horiz bar
         TXT 'sid'
         DFB 99,99,99,99,99,99,99,99,107
         DFB UP,LEFT,98
         DFB UP,LEFT,98
         DFB UP,LEFT,98
         DFB UP,LEFT,98
         DFB UP,LEFT,98
         DFB UP,LEFT,98
         DFB UP,LEFT,98
         DFB UP,LEFT,98
         DFB 00


SETUP    LDA #>STEXT      ;Now set up screen
         STA POINT+1
         LDA #<STEXT
         STA POINT
         LDY #00
:LOOP    LDA (POINT),Y
         BEQ :DONE
         JSR CHROUT
         INY
         BNE :LOOP
         INC POINT+1
         BNE :LOOP
:DONE    
         LDY #18          ;Color the SID regs
         LDA #3           ;cyan
:LUP     STA 55336,Y
         STA 55336+40,Y
         STA 55336+80,Y   ;What do you have against brute
         STA 55336+120,Y  ;force, anywas?
         STA 55336+160,Y
         STA 55336+200,Y
         STA 55336+240,Y
         DEY
         BPL :LUP

         JSR PRINST       ;Print instruments

DISPSID                   ;Display SID registers
         LDX #00
         LDA #<1024
         STA POINT
         LDA #>1024
         STA POINT+1

:LOOP    
         LDY #00
         CLC
         LDA POINT
         ADC #40
         STA POINT
         BCC :CONT
         INC POINT+1
:CONT    TXA
         JSR PRINTLOW     ;Only print the lower nibble
         INY
         LDA SHADOW,X
* STA SID,X
         JSR PRINTHEX

         INY              ;Second column
         TXA
         CLC
         ADC #7
         JSR PRINTLOW
         INY
         LDA SHADOW+7,X
* STA SID+7,X
         JSR PRINTHEX

         INY              ;Third col
         TXA
         CLC
         ADC #14
         JSR PRINTLOW
         INY
         LDA SHADOW+14,X
* STA SID+14,X
         JSR PRINTHEX

         INY
         CPX #4           ;Past row 4 yet?
         BCS :SKIP        ;Only 24 regs to print
         TXA
         CLC
         ADC #21
         JSR PRINTLOW
         INY
         LDA SHADOW+21,X
* STA SID+21,X
         JSR PRINTHEX

:SKIP    CPX #05
         BNE :SKIP2
         LDA #11
         JSR PRINTLOW
         INY
         LDA SID+27       ;read-only reg
         JSR PRINTHEX
:SKIP2   CPX #06
         BNE :NOPE
         LDA #12
         JSR PRINTLOW
         INY
         LDA SID+28
         JSR PRINTHEX
:NOPE    
         INX
         CPX #7
         BNE :LOOP
:DONE    

*
* This guy tells us which voice we're on
* and also lets you toggle the voice 3 output
*
VOICE123 
         LDA #>1024
         STA POINT+1
         LDA VOICE
         BNE :TRY7
         LDA #01
:TRY7    CMP #07
         BNE :TRY14
         LDA #06
:TRY14   CMP #14
         BNE :DUN
         LDA #11
:DUN     STA POINT
         LDY #01
         JSR HILIGHT

         LDY #01
         LDA $CB
         CMP #56          ;1 -- select voice 1
         BNE :V2
         JSR LOLIGHT
         LDA #00
         STA VOICE
         BEQ :DONE
:V2      CMP #59          ;2 -- select voice 2
         BNE :V3
         JSR LOLIGHT
         LDA #07
         STA VOICE
         BNE :DONE
:V3      CMP #8           ;3 -- select voice 3
         BNE :OUT3
         JSR LOLIGHT
         LDA #14
         STA VOICE
:OUT3    CMP #49          ;* -- toggle v3 output
         BNE :DONE
         LDA SHADOW+$18
         EOR #$80
         STA SHADOW+$18
         STA SID+$18
:DONE    

*
* The strategy is: print out your value, and if it is
* your turn to be serviced, highlight the entry and
* deal with the input.
*

ATTACK   
         >>> SETXY,10;0
         LDY #8
         LDX VOICE
         LDA SHADOW+5,X
         LSR
         LSR
         LSR
         LSR
         STA TEMP1
         JSR PRINTDEC
         LDA JOB
         CMP #ATK
         BNE DECAY
         LDY #6
         JSR GET1
         ASL
         ASL
         ASL
         ASL
         STA TEMP1
         LDX VOICE
         LDA SHADOW+5,X
         AND #$0F
         ORA TEMP1
         STA SHADOW+5,X
         STA SID+5,X

DECAY    >>> SETXY,11;0   ;Assume X=VOICE
         LDY #8
         LDA SHADOW+5,X
         AND #$0F
         STA TEMP1
         JSR PRINTDEC
         LDA JOB
         CMP #DKY
         BNE SUSTAIN
         LDY #6
         JSR GET1
         STA TEMP1
         LDX VOICE
         LDA SHADOW+5,X
         AND #$F0
         ORA TEMP1
         STA SHADOW+5,X
         STA SID+5,X

SUSTAIN  >>> SETXY,12;0
         LDY #8
         LDA SHADOW+6,X
         LSR
         LSR
         LSR
         LSR
         STA TEMP1
         JSR PRINTDEC
         LDA JOB
         CMP #SUS
         BNE RELEASE
         LDY #6
         JSR GET1
         ASL
         ASL
         ASL
         ASL
         STA TEMP1
         LDX VOICE
         LDA SHADOW+6,X
         AND #$0F
         ORA TEMP1
         STA SHADOW+6,X
         STA SID+6,X

RELEASE  >>> SETXY,13;0
         LDY #8
         LDA SHADOW+6,X
         AND #$0F
         STA TEMP1
         JSR PRINTDEC
         LDA JOB
         CMP #RLS
         BNE TRIANGLE
         LDY #6
         JSR GET1
         STA TEMP1
         LDX VOICE
         LDA SHADOW+6,X
         AND #$F0
         ORA TEMP1
         STA SHADOW+6,X
         STA SID+6,X

TRIANGLE 
         >>> SETXY,11;12
         LDA VOICE
         CLC
         ADC #4           ;Register 4
         TAX
         LDA #%00010000
         STA TEMP1
         AND SHADOW,X
         BEQ :CONT
         LDA #81-32
:CONT    CLC
         ADC #32
         LDY #00
         STA (POINT),Y
         LDA JOB
         CMP #TRI
         BNE SAWTOOTH
         LDY #2
         JSR TOGGLE

SAWTOOTH 
         >>> SETXY,11;16
         LDA VOICE
         LDA #%00100000
         STA TEMP1
         AND SHADOW,X
         BEQ :CONT
         LDA #81-32
:CONT    CLC
         ADC #32
         LDY #00
         STA (POINT),Y
         LDA JOB
         CMP #SAW
         BNE PULSE
         LDY #2
         JSR TOGGLE

PULSE    
         >>> SETXY,11;20
         LDA #%01000000
         STA TEMP1
         AND SHADOW,X
         BEQ :CONT1
         LDA #81-32
:CONT1   CLC
         ADC #32
         LDY #00
         STA (POINT),Y
         LDA JOB
         CMP #PUL
         BNE NOISE
         LDY #2
         LDA VOICE
         CLC
         ADC #4
         TAX
         JSR TOGGLE

NOISE    >>> SETXY,11;24
         LDA #%10000000
         STA TEMP1
         AND SHADOW,X
         BEQ :CONT1
         LDA #81-32
:CONT1   CLC
         ADC #32
         LDY #00
         STA (POINT),Y
         LDA JOB
         CMP #NOI
         BNE PWIDTH
         LDY #2
         JSR TOGGLE

PWIDTH   >>> SETXY,12;12
         LDX VOICE
         LDA SHADOW+2,X
         STA TEMP2
         LDA SHADOW+3,X
         AND #$0F
         STA SHADOW+3,X
         STA TEMP1
         LDY #12
         JSR BIGDEC4      ;Only four positions
         LDA JOB
         CMP #WID
         BNE FREQ
         LDY #10
         JSR HILIGHT
         JSR GETIN
         BEQ FREQ
         LDX VOICE
         CMP #'<'
         BNE :DIV2
         ASL SHADOW+2,X
         ROL SHADOW+3,X
:DIV2    CMP #'>'
         BNE :PLUS
         LSR SHADOW+3,X
         ROR SHADOW+2,X
:PLUS    CMP #'+'
         BNE :MINUS
         INC SHADOW+2,X
         BNE :DONE
         INC SHADOW+3,X
:MINUS   CMP #'-'
         BNE :BIGPLUS
         LDA SHADOW+2,X
         BNE :C1
         DEC SHADOW+3,X
:C1      DEC SHADOW+2,X
         JMP :DONE
:BIGPLUS CMP #219         ;shift-plus
         BNE :BIGMIN
         INC SHADOW+3,X
:BIGMIN  CMP #221         ;shift-minus
         BNE :UP
         DEC SHADOW+3,X
:UP      LDY #10
         CMP #UP
         BNE :DN
         JSR LOLIGHT
         DEC JOB
         BNE :DONE
:DN      CMP #DN
         BNE :DONE
         JSR LOLIGHT
         INC JOB
:DONE    LDA SHADOW+2,X
         STA SID+2,X
         LDA SHADOW+3,X
         STA SID+3,X

FREQ     >>> SETXY,13;12
         LDX VOICE
         LDA SHADOW,X
         STA TEMP2
         LDA SHADOW+1,X
         STA TEMP1
         LDY #5
         JSR BIGDEC
         LDA JOB
         CMP #FRQ
         BNE GATE
         LDY #3
         JSR HILIGHT
         JSR GETIN
         BEQ GATE
         LDX VOICE
         CMP #'+'
         BNE :MINUS
         INC SHADOW,X
         BNE :DONE
         INC SHADOW+1,X
:MINUS   CMP #'-'
         BNE :BIGPLUS
         LDA SHADOW,X
         BNE :C1
         DEC SHADOW+1,X
:C1      DEC SHADOW,X
         JMP :DONE
:BIGPLUS CMP #219         ;shift-plus
         BNE :BIGMIN
         INC SHADOW+1,X
:BIGMIN  CMP #221         ;shift-minus
         BNE :MUL2
         DEC SHADOW+1,X
:MUL2    CMP #'<'
         BNE :DIV2
         ASL SHADOW,X
         ROL SHADOW+1,X
:DIV2    CMP #'>'
         BNE :UP
         LSR SHADOW+1,X
         ROR SHADOW,X
:UP      LDY #3
         CMP #UP
         BNE :DN
         JSR LOLIGHT
         DEC JOB
         BNE :DONE
:DN      CMP #DN
         BNE :DONE
         JSR LOLIGHT
         INC JOB
:DONE    LDA SHADOW,X
         STA SID,X
         LDA SHADOW+1,X
         STA SID+1,X

GATE     >>> SETXY,15;0
         LDA #4
         CLC
         ADC VOICE
         TAX
         LDA #%00000001
         STA TEMP1
         AND SHADOW,X
         BEQ :CONT1
         LDA #81-32
:CONT1   CLC
         ADC #32
         LDY #00
         STA (POINT),Y
         LDA JOB
         CMP #GAT
         BNE SYNC
         LDY #3
         JSR TOGGLE

SYNC     >>> SETXY,16;0
         LDA #4
         CLC
         ADC VOICE
         TAX
         LDA #%00000010
         STA TEMP1
         AND SHADOW,X
         BEQ :CONT1
         LDA #81-32
:CONT1   CLC
         ADC #32
         LDY #00
         STA (POINT),Y
         LDA JOB
         CMP #SYN
         BNE RING
         LDY #3
         JSR TOGGLE

RING     >>> SETXY,15;5
         LDA #4
         CLC
         ADC VOICE
         TAX
         LDA #%00000100
         STA TEMP1
         AND SHADOW,X
         BEQ :CONT1
         LDA #81-32
:CONT1   CLC
         ADC #32
         LDY #00
         STA (POINT),Y
         LDA JOB
         CMP #RNG
         BNE TEST
         LDY #3
         JSR TOGGLE

TEST     >>> SETXY,16;5
         LDA #4
         CLC
         ADC VOICE
         TAX
         LDA #%00001000
         STA TEMP1
         AND SHADOW,X
         BEQ :CONT1
         LDA #81-32
:CONT1   CLC
         ADC #32
         LDY #00
         STA (POINT),Y
         LDA JOB
         CMP #TST
         BNE FILTER1
         LDY #3
         JSR TOGGLE

FILTER1  
         >>> SETXY,18;7
         LDX #23
         LDA #%00000001
         STA TEMP1
         AND SHADOW+23
         BEQ :CONT1
         LDA #81-32
:CONT1   CLC
         ADC #32
         LDY #00
         STA (POINT),Y
         LDA JOB
         CMP #FIL1
         BNE FILTER2
         LDY #1
         JSR TOGGLE

FILTER2  
         >>> SETXY,18;10
         LDA #%00000010
         STA TEMP1
         AND SHADOW+23
         BEQ :CONT1
         LDA #81-32
:CONT1   CLC
         ADC #32
         LDY #00
         STA (POINT),Y
         LDA JOB
         CMP #FIL2
         BNE FILTER3
         LDY #1
         JSR TOGGLE

FILTER3  
         >>> SETXY,18;13
         LDA #%00000100
         STA TEMP1
         AND SHADOW+23
         BEQ :CONT1
         LDA #81-32
:CONT1   CLC
         ADC #32
         LDY #00
         STA (POINT),Y
         LDA JOB
         CMP #FIL3
         BNE FILTERLO
         LDY #1
         JSR TOGGLE

FILTERLO 
         >>> SETXY,19;0
         LDX #$18
         LDA #%00010000
         STA TEMP1
         AND SHADOW+$18
         BEQ :CONT1
         LDA #81-32
:CONT1   CLC
         ADC #32
         LDY #00
         STA (POINT),Y
         LDA JOB
         CMP #FLO
         BNE FILTERBP
         LDY #7
         JSR TOGGLE

FILTERBP 
         >>> SETXY,20;0
         LDA #%00100000
         STA TEMP1
         AND SHADOW+24
         BEQ :CONT1
         LDA #81-32
:CONT1   CLC
         ADC #32
         LDY #00
         STA (POINT),Y
         LDA JOB
         CMP #FBP
         BNE FILTERHI
         LDY #8
         JSR TOGGLE

FILTERHI 
         >>> SETXY,21;0
         LDA #%01000000
         STA TEMP1
         AND SHADOW+24
         BEQ :CONT1
         LDA #81-32
:CONT1   CLC
         ADC #32
         LDY #00
         STA (POINT),Y
         LDA JOB
         CMP #FHI
         BNE RESON
         LDY #8
         JSR TOGGLE

RESON    
         >>> SETXY,23;0
         LDY #10
         LDA SHADOW+$17
         LSR
         LSR
         LSR
         LSR
         STA TEMP1
         JSR PRINTDEC
         LDA JOB
         CMP #RES
         BNE CUTOFF
         LDY #8
         JSR GET1
         ASL
         ASL
         ASL
         ASL
         STA TEMP1
         LDA SHADOW+$17
         AND #$0F
         ORA TEMP1
         STA SHADOW+$17
         STA SID+$17

CUTOFF   
         >>> SETXY,24;0
         LDA #00
         STA TEMP1
         LDA SHADOW+$16
         ASL
         ROL TEMP1
         ASL
         ROL TEMP1
         ASL
         ROL TEMP1
         ORA SHADOW+$15
         STA TEMP2

         LDY #7
         JSR BIGDEC4      ;Only four positions
         LDA JOB
         CMP #CUT
         BNE :JDONE
         LDY #6
         JSR HILIGHT
         JSR GETIN
         LDX VOICE
         CMP #'<'
         BNE :DIV2
         ASL SHADOW+$16
         LDA SHADOW+$15
         ASL
         STA SHADOW+$15
         CMP #8
         BCC :JDONE
         AND #$07
         STA SHADOW+$15
         INC SHADOW+$16
:JDONE   JMP :DONE
:DIV2    CMP #'>'
         BNE :PLUS
         LSR SHADOW+$16
         LDA #00
         ROL
         ROL
         ROL
         ROL
         ORA SHADOW+$15
         LSR
         STA SHADOW+$15
         BPL :JDONE
:PLUS    CMP #'+'
         BNE :MINUS
         LDA SHADOW+$15
         CLC
         ADC #01
         AND #$07
         STA SHADOW+$15
         BNE :DONE
         INC SHADOW+$16
         JMP :DONE
:MINUS   CMP #'-'
         BNE :BIGPLUS
         LDX SHADOW+$15
         BNE :C1
         DEC SHADOW+$16
         LDX #$08
:C1      DEX
         STX SHADOW+$15
         JMP :DONE
:BIGPLUS CMP #219         ;shift-plus
         BNE :BIGMIN
         INC SHADOW+$16
:BIGMIN  CMP #221         ;shift-minus
         BNE :UP
         DEC SHADOW+$16
:UP      LDY #10
         CMP #UP
         BNE :DN
         JSR LOLIGHT
         DEC JOB
         BNE :DONE
:DN      CMP #DN
         BNE :DONE
         JSR LOLIGHT
         INC JOB
:DONE    LDA SHADOW+$15
         STA SID+$15
         LDA SHADOW+$16
         STA SID+$16

LOOPDONE 
         LDA JOB
         CMP #MUSED
         BNE :C1
         LDA #0
         STA $028A        ;Disable key repeat
         JMP MUSICED
:C1      CMP #MACED
         BNE :C2
         LDA #0
         STA $028A        ;Disable key repeat
         JMP MACMAN
:C2      CMP #ILOAD
         BNE :C3
         JSR LOAD
         JMP :C4
:C3      CMP #ISAVE
         BNE :C4
         JSR SAVE

:C4      LDA JOB
         CMP #CUT+1
         BCC :CONT
         LDA #ATK
         STA JOB
:CONT    CMP #ATK
         BCS :DUN
         LDA #CUT
         STA JOB
:DUN     
         JMP DISPSID

*
* Print out the instrument list
*
LASTINST DFB $FF          ;Last instrument accessed

PRINST   
         LDA #10          ;Row
         STA TEMP2
         LDX #00          ;Instrument
:LOOP    STX TEMP1
         LDX TEMP2
         LDY #30
         CLC
         JSR PLOT
         LDA #5           ;White
         JSR CHROUT
         LDA #32
         LDX TEMP1        ;Check for highlight
         CPX LASTINST
         BNE :C1
         LDA #'*'
:C1      JSR CHROUT
         LDA #153         ;light green
         JSR CHROUT
         TXA              ;Convert to hex byte
         CMP #10
         SED
         ADC #$30
         CLD
         JSR CHROUT
         LDA #32
         JSR CHROUT
         TXA              ;Multiply by six
         ASL
         ADC TEMP1
         ASL
         TAY
         LDX #6
:LOOP2   LDA INAMES,Y     ;And print name
         JSR CHROUT
         INY
         DEX
         BNE :LOOP2
         INC TEMP2        ;Next row
         LDX TEMP1        ;And go back around!
         INX
         CPX #$0E
         BNE :LOOP
         RTS

*
* LOAD -- Load an instrument
*
LOAD     
         LDY #21
         LDX #01
         CLC
         JSR PLOT
         JSR STROUT
         DFB 05
         TXT 'load inst? (0-d)',00

         JSR GETINS
         BMI :DONE
         TAX              ;Instrument number
         STA LASTINST
         JSR SETVOICE
         LDA CVOICE
         STA CURVOICE
         LDA VOICE
         STA COFFSET
         JSR LOADINST
:DONE    PLA
         PLA
         JMP INIT2

*
* SAVE -- Save instrument
*
SAVE     
         LDY #21
         LDX #01
         CLC
         JSR PLOT
         JSR STROUT
         DFB 05
         TXT 'save inst? (0-d)',00

         JSR GETINS
         BMI :DONE
         STA TEMP1        ;Mult by 9
         STA LASTINST
         ASL
         ASL
         ASL
         ADC TEMP1
         TAX
         JSR SETVOICE
         LDY VOICE
         LDA SHADOW+2,Y
         STA INSTAB,X
         LDA SHADOW+3,Y
         STA INSTAB+1,X
         LDA SHADOW+4,Y
         ORA #$01         ;Set gate
         STA INSTAB+2,X
         LDA SHADOW+5,Y
         STA INSTAB+3,X
         LDA SHADOW+6,Y
         STA INSTAB+4,X

         LDA SHADOW+$16
         STA INSTAB+6,X
         LDA SHADOW+$15
         STA INSTAB+5,X

         LDY CVOICE
         LDA :BIT,Y
         AND SHADOW+$17   ;Check if filter is on
         BEQ :FILT
         LDA SHADOW+$17
         AND #$F0
         STA INSTAB+7,X

         LDA SHADOW+$18
         AND #$F0
:FILT    STA INSTAB+8,X   ;0 means no filter
         JSR GETNAME
:DONE    PLA
         PLA
         JMP INIT2
:BIT     DFB 0,1,2,4


*
* GETINS -- Turn on cursor, read key.  On exit,
*   .A contains the instrument # (or $FF if
*   invalid input entered, in which case N
*   is set).
*
BLNSW    = $CC
BLNCT    = $CD
BLNON    = $CF
GETINS   
         LDA #$FF
         STA JOB
         LDX #1
         JSR INPUT
         LDA STRBUF
         SEC
         SBC #48
         CMP #10
         BCC :DONE
         SBC #7
         CMP #14
         BCC :DONE
         LDA #$FF
:DONE    AND #$FF
         RTS

*
* Read in instrument name.  TEMP1 must contain
* instrument number.
*
GETNAME  
         LDY #21
         LDX #02
         CLC
         JSR PLOT
         JSR STROUT
         TXT 'name? ',00

         LDX #6
         JSR INPUT
         LDA TEMP1
         ASL
         ADC TEMP1
         ASL              ;times 6
         TAX
         LDY #00
:LOOP1   LDA STRBUF,Y
         BEQ :LOOP2
         STA INAMES,X
         INX
         INY
         BNE :LOOP1
:LOOP2   TYA              ;CR accepts current name
         BEQ :DONE
         CPY #6
         BCS :DONE
         LDA #32
         STA INAMES,X
         INX
         INY
         BNE :LOOP2
:DONE    RTS


*
* SETVOICE -- Deduce current voice from COFFSET
*
CVOICE   DFB 00
SETVOICE 
         LDA VOICE        ;0, 7, or 14
         BEQ :DONE
         SEC
         SBC #12
         BCS :DONE
         LDA #1
:DONE    CLC
         ADC #1
         STA CVOICE       ;Cvoice = 1, 2, or 3
         RTS

*
* Kludged-in routine to check for F1 etc.
*
TEMPX    DFB 00
GETIN    
         STX TEMPX
         JSR KGETIN
         PHA
         LDX #07
:LOOP    CMP :KEYTAB,X
         BNE :C1
         LDY NUMLIGHT
         JSR LOLIGHT
         LDA :JOBTAB,X
         STA JOB
         BNE :DONE

:C1      CMP :CREG,X
         BEQ :CONT
         DEX
         BPL :LOOP
         BMI :DONE

:CONT    LDA BITP,X
         LDX VOICE
         EOR SHADOW+4,X
         STA SHADOW+4,X
         STA SID+4,X

:DONE    LDX TEMPX
         PLA
         RTS

:KEYTAB  DFB 133,135,204,211
         TXT 'af'
         DFB 198          ;shift-f

:JOBTAB  DFB MUSED,MACED,ILOAD,ISAVE
         DFB ATK,FRQ,FLO

:CREG    TXT 'gyretspn'
BITP     DFB 1,2,4,8,16,32,64,128

*
* GET1 is a subroutine for inputting data for the "small"
* registers, such as attack, etc.  It expects POINT
* to be set up for hilight/lolight etc. and returns
* the new value of TEMP1 in A
* Note that it clobbers temp2
*

GET1     
         STY TEMP2
         JSR HILIGHT
         JSR GETIN
         BEQ :DUN
         LDY TEMP2
         CMP #'+'
         BNE :MINUS
         INC TEMP1
:MINUS   CMP #'-'
         BNE :UP
         DEC TEMP1
:UP      CMP #UP
         BNE :DN
         JSR LOLIGHT
         DEC JOB
         BPL :DUN
:DN      CMP #DN
         BNE :SID
         JSR LOLIGHT
         INC JOB
********************************
:SID     DO 0
:SID     CMP #'_'         ;Toggle between SID registers
         BNE :DONE
         LDA JOB
         EOR #64
         STA JOB
:DONE    LDA JOB
         CMP #ATK-1       ;Wrap around
         BNE :OK
         LDA #VOI
         STA JOB
:OK      CMP #VOL+1
         BNE :OK2
         LDA #ATK
         STA JOB
:OK2     CMP #$FF         ;Maybe we are in SID regs
         BNE :OK3
         LDA #23
         STA JOB
:OK3     CMP #24
         BNE :DUN
         LDA #00
         STA JOB
********************************
         FIN
:DUN     LDA TEMP1
         AND #$0F
         RTS

*
* TOGGLE is for handling toggle-items, similar to GET1 above
* Set up: POINT, Y, TEMP1=mask, and X=index into SID regs
*
TOGGLE   
         INC POINT        ;Starts pointing at dot
         STY TEMP2
         JSR HILIGHT
         TXA
         PHA
         JSR GETIN
         TAY
         PLA
         TAX
         TYA
         LDY TEMP2
         CMP #' '
         BNE :UP
         LDA SHADOW,X
         EOR TEMP1
         STA SHADOW,X
         STA SID,X
         RTS
:UP      CMP #UP
         BNE :DN
         JSR LOLIGHT
         DEC JOB
:DN      CMP #DN
         BNE :DONE
         JSR LOLIGHT
         INC JOB
:DONE    RTS

*
* HILIGHT will highlight some text on the screen.  It
* assumes POINT has been set to the appropriate screen
* location and that Y contains the number of characters
* to reverse from that location.
*
NUMLIGHT DFB 00           ;This part is a pretty kludge
                          ;for GETIN
HILIGHT  
         STY NUMLIGHT
:LOOP    LDA (POINT),Y
         ORA #$80
         STA (POINT),Y
         DEY
         BPL :LOOP
         RTS
*
* LOLIGHT on the other hand puts text back to normal
*
LOLIGHT  
:LOOP    LDA (POINT),Y
         AND #$7F
         STA (POINT),Y
         DEY
         BPL :LOOP
         RTS


*
* PRINTHEX converts the byte in A to hex and prints it out.
*
* Stolen and adapted from Merlin 64 demo code somewhere
* Sort-of.
*
* It expects (POINT),Y to point to the appropriate screen
* location, and advances Y at each character.
*

PRINTHEX 
         PHA
         LSR
         LSR
         LSR
         LSR
         JSR PRNIB
         PLA
PRINTLOW AND #$0F         ;Print low nibble
PRNIB    ORA #48
         CMP #58
         BCC PRIT
         SBC #57
PRIT     STA (POINT),Y
         INY
         RTS

*
* printdec will print a small number to the screen in
* decimal format.  By small I really mean <20
*
PRINTDEC 
         PHA
         CMP #10
         LDA #48
         ADC #00          ;C set if 10 or greater
         STA (POINT),Y
         INY
         ROR              ;Low bit set?
         PLA
         BCC :CONT
         SBC #10
         CLC
:CONT    ADC #48
         STA (POINT),Y
         INY
         RTS

*
* BIGDEC takes the number in TEMP1/TEMP2 and prints it out to
* the screen over five bytes.
*
* Pretty slow and inefficient, yup!
*
BIGDEC   
         LDX #48          ;X will count tens
         LDA TEMP2
         SEC
:LOOP    SBC #<10000
         PHA
         LDA TEMP1
         SBC #>10000
         BCC :C1
         INX
         STA TEMP1
:C1      PLA
         BCC :C2
         STA TEMP2
:C2      BCS :LOOP
         TXA
         STA (POINT),Y
         INY
BIGDEC4                   ;Enter here to only print 4 places
         LDX #48
         LDA TEMP2
         SEC
:LOOP    SBC #<1000
         PHA
         LDA TEMP1
         SBC #>1000
         BCC :C1
         INX
         STA TEMP1
:C1      PLA
         BCC :C2
         STA TEMP2
:C2      BCS :LOOP
         TXA
         STA (POINT),Y
         INY
BDEC3    LDX #47
         LDA TEMP2
:SLOOP   SEC
:LOOP    STA TEMP2
         SBC #100
         INX
         BCS :LOOP
         DEC TEMP1
         BPL :SLOOP
:DONE    TXA
         STA (POINT),Y
         INY
BDEC2    LDX #47
         LDA TEMP2
         SEC
:LOOP    STA TEMP2
         SBC #10
         INX
         BCS :LOOP
         TXA
         STA (POINT),Y
         INY
         LDA TEMP2
         CLC
         ADC #48
         STA (POINT),Y
         INY
         RTS
