10 REM > KeySave/s
   20 REM Source for *KeySave - save function key definitions
   30 REM Based on *Show/*KeyList
   40 :
   50 OSARGS=&FFDA:OSFIND=&FFCE:OSFILE=&FFDD:OSBPUT=&FFD4
   60 OS_CLI=&FFF7:OSBYTE=&FFF4:OSWRCH=&FFEE:BRKV=&202
   70 load%=&FFFF0900:DIM mcode% &200
   80 zp=&A8:kptr=&A8:kend=&AA:num=&AC:host=&AD:tmp=&AE
   90 :
  100 FOR P=0 TO 1
  110   P%=load%:O%=mcode%
  120   [OPT P*3+4
  130   .errCantSave
  140   BRK:EQUB 192:EQUS "Can't save file":EQUB 0
  150   :
  160   .go%
  170   LDA #0:LDX #1:JSR OSBYTE:STX host     :\ Find MOS system
  180   TAY:LDA #1:LDX #zp:JSR OSARGS         :\ Find parameters
  190   LDA zp:STA ctrl:LDA zp+1:STA ctrl+1   :\ Point to filename
  200   LDA #0:JSR CallOsfile                 :\ Save initial file
  210   LDX ctrl:LDY ctrl+1:LDA #&80          :\ Point to filename
  220   JSR OSFIND:TAY:BEQ errCantSave        :\ Try to open file
  230   STY handle+1:JSR brkSwap:LDX #0       :\ Set up BRK handler
  240   .ShowFX
  250   LDA FXString,X:JSR OSBPUT:INX         :\ Output "FX18" string
  260   CMP #13:BNE ShowFX:LDX #0
  270   .ShowAll
  280   JSR ShowKey:INX:CPX #16:BNE ShowAll   :\ Loop through 0 to 15
  290   JSR TidyUp:LDA #5:JSR CallOsfile      :\ Close file, read file info
  300   LDA #&EB:STA ctrl+3:LDX #&FF          :\ Set filetype=Obey
  310   STX ctrl+5:STX ctrl+4:INX:STX ctrl+2
  320   LDA #2:JMP CallOsfile                 :\ Write load address and exit
  330   :
  340   .TidyUp
  350   JSR brkSwap:JSR RomRestore            :\ Restore BRK, ROM state
  360   .handle:LDY #0:LDA #0:JMP OSFIND      :\ Close file
  370   :
  380   .brkSwap
  390   LDA BRKV+0:LDX newBRKV+0:STA newBRKV+0:STX BRKV+0
  400   LDA BRKV+1:LDX newBRKV+1:STA newBRKV+1:STX BRKV+1:RTS
  410   .brkTrap
  420   JSR TidyUp:JMP (BRKV)                 :\ Close, etc., pass error on
  430   .newBRKV
  440   EQUW brkTrap
  450   :
  460   .ShowKey                              :\ Show key X
  470   LDA &F4:STA rom+1                     :\ Save ROM state
  480   ORA #128:JSR RomSelect                :\ Page workspace in
  490   LDA host:CMP #3:BCS ShowMaster        :\ Master
  500   PHP:SEI:LDA &B10:SEC:SBC &B00,X       :\ Copied from MOS 1.20
  510   STA tmp:TYA:PHA:LDY #16               :\ Find length of string
  520   .LE3B7
  530   LDA &B00,Y:SEC:SBC &B00,X
  540   BCC LE3C8:BEQ LE3C8
  550   CMP tmp:BCS LE3C8:STA tmp
  560   .LE3C8
  570   DEY:BPL LE3B7:PLA:TAY:PLP
  580   LDA #1:ADC &B00,X:STA kptr+0
  590   ADC tmp:STA kend+0
  600   LDA #&B:STA kptr+1:STA kend+1         :\ kptr/kend=>BBC definition
  610   BNE KeyDisplay                        :\ Show key definition
  620   .ShowMaster
  630   LDA &8000,X:STA kptr+0:LDA &8011,X:STA kptr+1
  640   LDA &8001,X:STA kend+0:LDA &8012,X:STA kend+1
  650   .KeyDisplay
  660   LDA kptr+0:CMP kend+0:BNE KeyNotEmpty
  670   LDA kptr+1:CMP kend+1:BEQ KeyEmpty
  680   .KeyNotEmpty
  690   TXA:PHA:LDY handle+1:LDX #0
  700   .PrKey
  710   LDA KeyString,X:JSR OSBPUT:INX        :\ Print "Key "
  720   CMP #ASC" ":BNE PrKey
  730   PLA:TAX:CMP #10:BCC KeyDigit          :\ num<10
  740   LDA #ASC"1":JSR OSBPUT:TXA:CLC:SBC #9 :\ Print "1", A=next digit
  750   .KeyDigit
  760   ORA #ASC"0":JSR OSBPUT                :\ Print units digit
  770   LDA #32:JSR OSBPUT:JSR PrQuote        :\ Print <spc><quot>
  780   :
  790   .KeyPrintLp
  800   LDA kptr+0:CMP kend+0:BNE KeyChar     :\ Not at end of defintion
  810   LDA kptr+1:CMP kend+1:BEQ KeyEnd      :\ End of definition
  820   .KeyChar
  830   LDY #0:LDA (kptr),Y:BPL KeyLowChar    :\ Char<128
  840   PHA:JSR PrBar:LDA #ASC"!":JSR OSBPUT  :\ Print "|!" for char>127
  850   PLA:AND #127                          :\ Print as low char
  860   .KeyLowChar
  870   LDY handle+1:EOR #64
  880   CMP #ASC"""" EOR 64:BEQ KeyCtrlChar   :\  "  -> |"
  890   CMP #ASC"|" EOR 64:BEQ KeyCtrlChar    :\  |  -> |"
  900   EOR #64:CMP #127:BEQ KeyCtrlChar      :\ Del -> |?
  910   CMP #32:BCS KeyPrintChar              :\ <32 -> |X
  920   .KeyCtrlChar
  930   PHA:JSR PrBar:PLA:EOR #64
  940   .KeyPrintChar
  950   JSR OSBPUT:INC kptr+0:BNE KeyPrintLp  :\ Print and loop back
  960   INC kptr+1:BNE KeyPrintLp
  970   .KeyEnd
  980   JSR PrQuote:LDA #13:JSR OSBPUT
  990   .KeyEmpty
 1000   .RomRestore
 1010   .rom:LDA #0
 1020   .RomSelect
 1030   STA &F4:STA &FE30:RTS
 1040   .CallOsfile
 1050   LDX #ctrl AND 255:LDY #ctrl DIV 256:JMP OSFILE
 1060   :
 1070   .PrBar  :LDA #ASC"|":LDY handle+1:JMP OSBPUT
 1080   .PrQuote:LDA #34:JMP OSBPUT
 1090   .KeyString:EQUS "*Key "
 1100   .FXString :EQUS "*FX 18":EQUB 13
 1110   EQUS "v1.01":BRK
 1120   .ctrl
 1130   EQUW 0:EQUD &FFFFEB00:EQUD &FFFF0000:EQUD &FFFF0900:EQUD &FFFF0D00
 1140   ]
 1150 NEXT
 1160 PRINT"*SAVE KeySave ";~mcode%;" ";~O%;" ";~go%OR&FFFF0000;" ";~load%