*
* Sound F/X - 1991 - Barry Boone
*

       AORG >2000

* Geneve memory manager will bring in pages @ >A000 (using >8005)

*
* Program
*
       DEF  START,ISTART,SLOAD,SFIRST,SLAST

SLOAD
SFIRST B    @MKR
NO9938 DATA 0                 -1 = No 9938 RAM used
NOEXP  DATA 0                 -1 = No EXP VRAM used
NOCPU  DATA 0                 -1 = No CPU RAM used
NOSCRT DATA 0                 -1 = No SC/SS RAM used
NOGEN  DATA 0                 -1 = No 9640 RAM used

GENFLG DATA 0                 -1 = Geneve, else 99/4A
SCFLAG DATA 0                 -1 = Supercart, else not
SSFLAG DATA 0                 -1 = 32K Scart, else not
VDPFLG DATA 0                 -1 = 9938 present, else not
EXPFLG DATA 0                 -1 = 9938 Expansion VRAM present
SSPGS  DATA 0                 # of SS pages in use/# of Geneve pages in use
SCEND  DATA 0                 Last addr used in last page (9640 or SC)
VDPPGS DATA 0                 # of 9938 vid pages in use
VDPEND DATA 0                 last addr in last block of 9938 vram
EXPPGS DATA 0                 # of 9938 exp vid pages used
EXPEND DATA 0                 last addr in last block of exp vram
VIDSIZ DATA 0                 buffer size of block 0 vram buffer
MEMSIZ DATA 0                 Combined size of all buffers (in 1K increments)
GENMEM DATA 0                 # of 9640 memory pages available
CHNFLG DATA 0                 Chained files flag

CSR    BYTE '_'
SPC    BYTE ' '

* these can be EQU'd to the 2nd word in an LI instruction, if mem gets tight

D7     DATA 7
D8     DATA 8
D10    DATA 10
D16    DATA 16
D18    DATA 18
D24    DATA 24
D64    DATA 64
D96    DATA 96
D760   DATA 760

SSPCDT BYTE 2,8,>20,>80
NREGS  BYTE >00,>F0,>00,>0E,>01
       BYTE >06,>01,>F4
MEN1   TEXT '1 - Load F/X File'
MEN2   TEXT '2 - Play Sound in Buffer'
MEN3   TEXT '3 - Change Parameters'
MEN4   TEXT '4 - Disk Catalog'
MEN4A  TEXT '5 - Convert Sound File'
MEN5   TEXT 'Q - Exit Program'
MSG1   TEXT 'Sound F/X V1.0a 11/14/91'
MSG2   TEXT 'Copyright 1991  by Barry Boone'
NOTFXF TEXT 'Error - File not in F/X Format'
HEX5   BYTE 5
STAB   DATA 8,MSG1,24,45,MSG2,30
       DATA 290,MEN1,17,370,MEN2,24
       DATA 450,MEN3,21,530,MEN4,16
       DATA 610,MEN4A,22,690,MEN5,16,0
DIVLIN DATA >0000,>FF00,>FF00,>0000
MSGINP TEXT 'Input '
MSG4   TEXT 'Filename:'
MSGOUT TEXT 'Output Filename:'
PTHMSG TEXT 'Device:  '
MSG6   TEXT 'I/O Error, '
MSG6K  TEXT 'Press any key'
MEMTXT TEXT 'Buffer Size: '
MEMTX2 TEXT '####K'
MAXTXT TEXT 'Max. File Size: '
MAXTX2 TEXT '#### Sectors'
PROCTX TEXT 'Processing...'
LOADTX TEXT 'Loading...'
CHAINT TEXT 'Press FCTN-6 if last file is loaded.'
CHAIN2 TEXT 'Insert Next Disk and Press Any Key'
AMPTXT TEXT 'Amplification Factor: '
AMPFAC TEXT '  0%'
SPCMOD TEXT '(Spacebar to adjust, Enter to accept)'
SVA000 BYTE 0                 Saved >A000 page for Geneve
CCPAK  TEXT 'Conversion Complete. Press Any Key.'
PLYTXT TEXT '<Playing...>' 12
USATXT TEXT 'Use Arrow Keys to Move Marker' 29
LCDTXT TEXT '(S)ubdir (L)oad F/X (C)onvert F/X'  33
DSPTXT TEXT 'Press SPACEBAR to Continue Cataloging' 37

AMPLVL DATA 0                 Current Amplification Level
GIFOPL DATA >0409
CVTOPL DATA >0489
OPNFIL DATA 0                -1 = file opened!
SNDMEM DATA 0                -1 = Sound file in memory
ENDADR DATA 0                On 99/4a, is end of sound in memory
FXH    TEXT 'FX'             Header for FX files
TSTIT  DATA 0
H9938  DATA >9938

BLKDAT DATA >9090,>9090,>9090,>9090
       DATA >9090,>9090,>9090,>9090
       DATA >9191,>9191,>9191,>9191
       DATA >9191,>9191,>9191,>9191
       DATA >9292,>9292,>9292,>9292
       DATA >9292,>9292,>9292,>9292
       DATA >9393,>9393,>9393,>9393
       DATA >9393,>9393,>9393,>9393
       DATA >9494,>9494,>9494,>9494
       DATA >9494,>9494,>9494,>9494
       DATA >9595,>9595,>9595,>9595
       DATA >9595,>9595,>9595,>9595
       DATA >9696,>9696,>9696,>9696
       DATA >9696,>9696,>9696,>9696
       DATA >9797,>9797,>9797,>9797
       DATA >9797,>9797,>9797,>9797
       DATA >9898,>9898,>9898,>9898
       DATA >9898,>9898,>9898,>9898
       DATA >9999,>9999,>9999,>9999
       DATA >9999,>9999,>9999,>9999
       DATA >9A9A,>9A9A,>9A9A,>9A9A
       DATA >9A9A,>9A9A,>9A9A,>9A9A
       DATA >9B9B,>9B9B,>9B9B,>9B9B
       DATA >9B9B,>9B9B,>9B9B,>9B9B
       DATA >9C9C,>9C9C,>9C9C,>9C9C
       DATA >9C9C,>9C9C,>9C9C,>9C9C
       DATA >9D9D,>9D9D,>9D9D,>9D9D
       DATA >9D9D,>9D9D,>9D9D,>9D9D
       DATA >9E9E,>9E9E,>9E9E,>9E9E
       DATA >9E9E,>9E9E,>9E9E,>9E9E
       DATA >9F9F,>9F9F,>9F9F,>9F9F
       DATA >9F9F,>9F9F,>9F9F,>9F9F
BLKDA2 DATA >B0B0,>B0B0,>B0B0,>B0B0
       DATA >B0B0,>B0B0,>B0B0,>B0B0
       DATA >B1B1,>B1B1,>B1B1,>B1B1
       DATA >B1B1,>B1B1,>B1B1,>B1B1
       DATA >B2B2,>B2B2,>B2B2,>B2B2
       DATA >B2B2,>B2B2,>B2B2,>B2B2
       DATA >B3B3,>B3B3,>B3B3,>B3B3
       DATA >B3B3,>B3B3,>B3B3,>B3B3
       DATA >B4B4,>B4B4,>B4B4,>B4B4
       DATA >B4B4,>B4B4,>B4B4,>B4B4
       DATA >B5B5,>B5B5,>B5B5,>B5B5
       DATA >B5B5,>B5B5,>B5B5,>B5B5
       DATA >B6B6,>B6B6,>B6B6,>B6B6
       DATA >B6B6,>B6B6,>B6B6,>B6B6
       DATA >B7B7,>B7B7,>B7B7,>B7B7
       DATA >B7B7,>B7B7,>B7B7,>B7B7
       DATA >B8B8,>B8B8,>B8B8,>B8B8
       DATA >B8B8,>B8B8,>B8B8,>B8B8
       DATA >B9B9,>B9B9,>B9B9,>B9B9
       DATA >B9B9,>B9B9,>B9B9,>B9B9
       DATA >BABA,>BABA,>BABA,>BABA
       DATA >BABA,>BABA,>BABA,>BABA
       DATA >BBBB,>BBBB,>BBBB,>BBBB
       DATA >BBBB,>BBBB,>BBBB,>BBBB
       DATA >BCBC,>BCBC,>BCBC,>BCBC
       DATA >BCBC,>BCBC,>BCBC,>BCBC
       DATA >BDBD,>BDBD,>BDBD,>BDBD
       DATA >BDBD,>BDBD,>BDBD,>BDBD
       DATA >BEBE,>BEBE,>BEBE,>BEBE
       DATA >BEBE,>BEBE,>BEBE,>BEBE
       DATA >BFBF,>BFBF,>BFBF,>BFBF
       DATA >BFBF,>BFBF,>BFBF,>BFBF
BLKDA3 DATA >D0D0,>D0D0,>D0D0,>D0D0
       DATA >D0D0,>D0D0,>D0D0,>D0D0
       DATA >D1D1,>D1D1,>D1D1,>D1D1
       DATA >D1D1,>D1D1,>D1D1,>D1D1
       DATA >D2D2,>D2D2,>D2D2,>D2D2
       DATA >D2D2,>D2D2,>D2D2,>D2D2
       DATA >D3D3,>D3D3,>D3D3,>D3D3
       DATA >D3D3,>D3D3,>D3D3,>D3D3
       DATA >D4D4,>D4D4,>D4D4,>D4D4
       DATA >D4D4,>D4D4,>D4D4,>D4D4
       DATA >D5D5,>D5D5,>D5D5,>D5D5
       DATA >D5D5,>D5D5,>D5D5,>D5D5
       DATA >D6D6,>D6D6,>D6D6,>D6D6
       DATA >D6D6,>D6D6,>D6D6,>D6D6
       DATA >D7D7,>D7D7,>D7D7,>D7D7
       DATA >D7D7,>D7D7,>D7D7,>D7D7
       DATA >D8D8,>D8D8,>D8D8,>D8D8
       DATA >D8D8,>D8D8,>D8D8,>D8D8
       DATA >D9D9,>D9D9,>D9D9,>D9D9
       DATA >D9D9,>D9D9,>D9D9,>D9D9
       DATA >DADA,>DADA,>DADA,>DADA
       DATA >DADA,>DADA,>DADA,>DADA
       DATA >DBDB,>DBDB,>DBDB,>DBDB
       DATA >DBDB,>DBDB,>DBDB,>DBDB
       DATA >DCBC,>DCBC,>DCBC,>DCBC
       DATA >DCDC,>DCDC,>DCDC,>DCDC
       DATA >DDDD,>DDDD,>DDDD,>DDDD
       DATA >DDDD,>DDDD,>DDDD,>DDDD
       DATA >DEDE,>DEDE,>DEDE,>DEDE
       DATA >DEDE,>DEDE,>DEDE,>DEDE
       DATA >DFDF,>DFDF,>DFDF,>DFDF
       DATA >DFDF,>DFDF,>DFDF,>DFDF
BLKTB1 DATA >9091,>9293,>9495,>9697
       DATA >9899,>9A9B,>9C9D,>9E9F
BLKTB2 DATA >B0B1,>B2B3,>B4B5,>B6B7
       DATA >B8B9,>BABB,>BCBD,>BEBF
BLKTB3 DATA >D0D1,>D2D3,>D4D5,>D6D7
       DATA >D8D9,>DADB,>DCDD,>DEDF

* Myarc 9640 memory page table

MEM964 BYTE >28,>29,>2A,>2B,>2C,>2D          Myword memory - 8 pages
       BYTE >2E,>2F

       BYTE >C0,>C1,>C2,>C3,>C4,>C5          Memex bank 3 - 40 pages
       BYTE >C6,>C7,>C8,>C9,>CA,>CB
       BYTE >CC,>CD,>CE,>CF,>D0,>D1
       BYTE >D2,>D3,>D4,>D5,>D6,>D7
       BYTE >D8,>D9,>DA,>DB,>DC,>DD
       BYTE >DE,>DF,>E0,>E1,>E2,>E3
       BYTE >E4,>E5,>E6,>E7

       BYTE >80,>81,>82,>83,>84,>85          Memex bank 2 - 62 pages
       BYTE >86,>87,>88,>89,>8A,>8B
       BYTE >8C,>8D,>8E,>8F,>90,>91
       BYTE >92,>93,>94,>95,>96,>97
       BYTE >98,>99,>9A,>9B,>9C,>9D
       BYTE >9E,>9F,>A0,>A1,>A2,>A3
       BYTE >A4,>A5,>A6,>A7,>A8,>A9
       BYTE >AA,>AB,>AC,>AD,>AE,>AF
       BYTE >B0,>B1,>B2,>B3,>B4,>B5
       BYTE >B6,>B7,>B8,>B9,>BB,>BD
       BYTE >BE,>BF

       BYTE >40,>41,>42,>43,>44,>45          Memex bank 1 - 63 pages
       BYTE >46,>47,>48,>49,>4A,>4B
       BYTE >4C,>4D,>4E,>4F,>50,>51
       BYTE >52,>53,>54,>55,>56,>57
       BYTE >58,>59,>5A,>5B,>5C,>5D
       BYTE >5E,>5F,>60,>61,>62,>63
       BYTE >64,>65,>66,>67,>68,>69
       BYTE >6A,>6B,>6C,>6D,>6E,>6F
       BYTE >70,>71,>72,>73,>74,>75
       BYTE >76,>77,>78,>79,>7B,>7C
       BYTE >7D,>7E,>7F

       BYTE >36,>37,>3F,>3E,>3D,>3C          Cart pages/GROM 7,6,5,4  [33 pgs]
       BYTE >03,>39,>3A,>16,>17,>18          SPg 3/GROM 1,2/MEM >16+
       BYTE >19,>1A,>1B,>1C,>1D,>1E
       BYTE >1F,>20,>21,>22,>23,>24
       BYTE >25,>26,>27,>30,>31,>33
       BYTE >34,>35,>3B                      GROM 3
MEM96$ EQU  $

SNDINI DATA >8100,>A100,>C100

CLS    BLWP @VMBFD
BYT0   DATA 0,>2020,960
       RT

LDREGS LI   R0,>0080         Load vr0-7
       MOVB @1(R1),@>83D4
IVRL1  MOVB *R1+,R0
       SWPB R0
       BLWP @VWTR
       AI   R0,>100
       ANDI R0,>FF00
       CI   R0,>8800
       JEQ  IVRK2
       SWPB R0
       JMP  IVRL1
IVRK2  RT

CHOICE MOV  R11,R10          R8 = pointer to pointers
       MOV  R10,R9           R9 = saved pointer to chars
CHOICN MOVB *R10+,R1         R10 = working ptr to chars
       JNE  CHOICN           Locate "null"
       INC  R10
       ANDI R10,>FFFE        Make it even
       MOV  R10,R8
CHOICL LI   R1,' _'
       MOV  R9,R10
       MOVB *R10,R1          Select "default"
       BL   @KEYINB
       CI   R1,>0D00         <enter> key?
       JNE  CHOICA
       MOVB *R10+,R1         Place this character in default position
       CB   R1,@SPC          If space char, ignore it
       JEQ  CHOICL
       JMP  CHOICB
CHOICA INC  R10
CHOICB CB   R1,*R10          Is this it?
       JEQ  CHOICC
       MOVB *R10+,R12        Nul?
       JNE  CHOICB
       JMP  CHOICL
CHOICC S    R9,R10
       SLA  R10,1            *2
       A    R10,R8
       BLWP @VSBW            Echo char to screen
       CB   *R9,@SPC         Save new default?
       JEQ  CHOICD
       MOVB R1,*R9
CHOICD MOV  @-2(R8),R8
       B    *R8

NOF9   DATA 0
KEYIN  LI   R1,'_ '
KEYINB LI   R12,300
       BLWP @VSBW
       MOVB @HEX5,@>8374
KEYINL BLWP @KSCAN
       CB   @SPC,@>837C
       JEQ  KEYINX
       DEC  R12
       JNE  KEYINL
       SWPB R1
       JMP  KEYINB
KEYINX CLR  R1
       MOVB @>8375,R1
CFCTN9 ABS  @NOF9
       JNE  KEYINR
FCTN9  EQU  $+2
       CI   R1,>0F00
       JEQ  KEYINM
CTRLC  EQU  $+2
       CI   R1,>8300
       JNE  KEYINR
KEYINM B    @ISTART
KEYINR RT

READX  S    R8,R9
       B    *R10
READLN CLR  R8
REDLN2 MOVB *R11+,@MININC
       MOVB *R11+,@MAXINC
       MOV  R11,R10
       MOV  R9,R1
       BLWP @VMBW
       A    R8,R0    R0=scrnloc
       A    R8,R9
READL1 LI   R1,' _'  R2=maxlen
       MOVB *R9,R1   R8=curlen
       SWPB R1
       BL   @KEYINB  R9=buffer loc
MAXINC EQU  $+2
       CI   R1,>7E00
       JH   READL1
MININC EQU  $+2
       CI   R1,>2000
       JL   CTRLCK
       BLWP @VSBW
       MOVB R1,*R9+
READL2 INC  R0
       INC  R8
       C    R8,R2
       JNE  READL1
       DEC  R0
       DEC  R8
       DEC  R9
       JMP  READL1
CTRLCK CI   R1,>800
       JNE  CKENT
       MOV  R8,R8
       JEQ  READL1
       DEC  R8
       MOVB *R9,R1
       BLWP @VSBW
       DEC  R9
       DEC  R0
       JMP  READL1
CKENT  CI   R1,>D00
       JNE  CKERAS
       MOV  R8,R8
       JNE  CKENT2
       CB   *R9,@H20
       JEQ  READL1
CKENT2 MOVB *R9,R1
       BLWP @VSBW
       JMP  READX
CKERAS CI   R1,>700
       JNE  CKFOR
       S    R8,R0
       S    R8,R9
       CLR  R8
ZAPBUF MOVB @READL1+2,*R9+
       INC  R8
       C    R8,R2
       JNE  ZAPBUF
       S    R8,R9
       CLR  R8
       MOV  R9,R1
       BLWP @VMBW
       JMP  READL1
CKFOR  CI   R1,>900
       JNE  CKDEL
       MOVB *R9+,R1
       BLWP @VSBW
       JMP  READL2
CKDEL  CI   R1,>300
       JNE  CKINS
       MOV  R8,R11
       MOV  R9,R1
CKDEL1 MOVB @1(R1),*R1
       INC  R1
       INC  R11
       C    R11,R2
       JNE  CKDEL1
       DEC  R11
       A    R9,R11
       S    R8,R11
       MOVB @H20,*R11
       MOV  R9,R1
       S    R8,R1
       S    R8,R0
       BLWP @VMBW
       A    R8,R0
CKDEL2 B    @READL1
CKINS  CI   R1,>400
       JNE  CKDEL2
       MOV  R9,R1
       S    R8,R1
       MOV  R2,R11
       A    R2,R1
CKINS1 DEC  R1
       DEC  R11
       C    R11,R8
       JEQ  NOWSP
       MOVB @-1(R1),*R1
       JMP  CKINS1
NOWSP  MOVB @H20,*R1
       S    R8,R0
       MOV  R9,R1
       S    R8,R1
       BLWP @VMBW
       A    R8,R0
       JMP  CKDEL2
* convert ASCII to binary...
TEN    DATA 10
ASCBIN CLR  R7              R9 points to incoming text
ASCBN2 CB   *R9,@SPC         Locate first #
       JNE  ASCLOP
       INC  R9
       DEC  R2
       JNE  ASCBN2
ASCLOP MOVB *R9+,R8
       AI   R8,->3000
       SRL  R8,8
       A    R8,R7
       CB   *R9,@SPC
       JEQ  ASCXIT
       DEC  R2
       JEQ  ASCXIT
       MPY  @TEN,R7
       MOV  R8,R7
       JMP  ASCLOP
ASCXIT RT
*
BINASC MOVB @SPC,*R9+         clear number area
       DEC  R2
       JNE  BINASC
BINASN MOV  R7,R8
BINAS2 CLR  R7
       DIV  @TEN,R7
       SLA  R8,8
       AI   R8,>3000
       DEC  R9
       DEC  R15               R6 used to guarantee a certain length
       MOVB R8,*R9
       MOV  R7,R8
       JNE  BINAS2
       MOV  R15,R15
       JGT  BINAS2
       RT

PRSCVT LI   R1,CVTNAM
       JMP  PRSALE
PRSFNM LI   R1,DEVNAM        Now parse the filename
PRSALE MOV  R1,R0
       MOV  R11,@PRSFNX+2
       MOV  R0,R11
       AI   R11,40
GPRSIN CB   @SPC,*R1+        Second entry for CONVRT
       JEQ  EPRSIN
       C    R1,R11
       JNE  GPRSIN
EPRSIN S    R0,R1
       DEC  R1
       MOV  R1,@-42(R11)
PRSFNX B    @0
*
VECTR2 DATA >0024
TSTXXX DATA >A55A
GENR5L DATA >F00B

* Delete page from 9640 list - R0 MSB = page # to delete

GENDEL LI   R1,MEM964
GENDLL CB   R0,*R1+
       JEQ  GDELIT
       CI   R1,MEM96$
       JNE  GENDLL
       RT
GDELIT DEC  R1
       SB   *R1,*R1
       RT

* Init memory map of 9640

GNINIT MOV  R11,R10
       MOV  @D18,@DELCNT
       C    @2,@VECTR2
       JNE  DOEXEC
       CLR  R0
       MOVB R0,@>8005
       LI   R2,>BF00
GENDLP MOVB *R2+,R0
       BL   @GENDEL           kill reserved pages
       CI   R2,>BF08
       JNE  GENDLP
       JMP  ZAPPGS
DOEXEC LI   R2,>8000
EXCDLP MOVB *R2+,R0
       BL   @GENDEL           kill resv'd pages (for EXEC)
       CI   R2,>8003          don't kill page @>6000
       JNE  DODODX
       INC  R2
DODODX CI   R2,>8005
       JNE  EXCDLP

ZAPPGS LI   R2,MEM964         test all mem pages, delete any ROM areas
ZAPPLP MOVB *R2+,R0
       JEQ  SKNEXT
       MOVB R0,@>8005
       MOV  @>A000,R4
       MOV  @TSTXXX,@>A000    put in tst data
       C    @TSTXXX,@>A000    see if it wrote
       JEQ  SKNET2            yup, page exists
       BL   @GENDEL           nope, delete it
SKNET2 MOV  R4,@>A000         restore old value
SKNEXT CI   R2,MEM96$
       JNE  ZAPPLP
       CLR  R4
       LI   R2,MEM964         now fix mem list
       LI   R3,MEM964
FIXLST MOVB *R3+,R1
       JEQ  NOTHER
       MOVB R1,*R2+
       INC  R4
NOTHER CI   R3,MEM96$
       JNE  FIXLST
       MOV  R4,@GENMEM        save # of pages avail.
       SLA  R4,3
       A    R4,@MEMSIZ        add "K" to mem size

OPASUX CI   R2,MEM96$
       JEQ  OTAHER
       CLR  R1
       MOVB R1,*R2+
       JMP  OPASUX

OTAHER MOVB @SVA000,@>8005    restore A000 page
       B    *R10              and return

START  LWPI >83A0
       MOV  @D10,@MEMSIZ

* Now determine if 9640 or 99/4A
       MOV  @0,R0
       INV  @0
       CLR  @GENFLG
       C    @0,R0
       JEQ  DO994A

* 9640 Handling
       MOV  R0,@0
       LWPI >F000
       MOV  @GENR5L,@PLVLP1+2
       MOV  @GENR5L,@PLELP1+2
       SETO @GENFLG
       SETO @VDPFLG
       A    @D96,@MEMSIZ
       MOVB @>8005,@SVA000
       JMP  CHKEXT

* 99/4A handling
DO994A A    @D24,@MEMSIZ
       CLR  @SCFLAG
       MOV  @>6000,R1
       INV  @>6000
       C    R1,@>6000
       JEQ  CK9938
       SETO @SCFLAG
       A    @D8,@MEMSIZ

       LI   R12,>0800
       LDCR @SSPCDT+1,8
       MOV  @FXH,@>6000
       LDCR @SSPCDT,8
       CLR  @SSFLAG
       C    @>6000,@FXH
       JEQ  CK9938
       SETO @SSFLAG
       A    @D24,@MEMSIZ

CK9938 BLWP @VMBWD
       DATA >0400,FXH,2
       LI   R0,>0E01
       BLWP @VWTR
       BLWP @VMBRD
       DATA >0400,TSTIT,2
       CLR  @VDPFLG
       C    @TSTIT,@FXH
       JEQ  ISTART
       SETO @VDPFLG
       A    @D96,@MEMSIZ

CHKEXT LI   R0,>118E
       BLWP @VWTR
       LI   R0,>2D40
       BLWP @VWTR
       MOVB @BYT0,@>8C06
       BLWP @VMBWD
       DATA >0500,H9938,2
       BLWP @VMBRD
       DATA >0500,TSTIT,2
       CLR  @EXPFLG
       LI   R0,>2D00
       BLWP @VWTR
       C    @TSTIT,@H9938
       JNE  NOEXTR
       SETO @EXPFLG
       A    @D64,@MEMSIZ
NOEXTR ABS  @GENFLG
       JEQ  ISTART
       BL   @GNINIT

ISTART LI   R0,SNDINI
       LI   R15,>8400
SNDINL MOVB *R0+,*R15
       CI   R0,SNDINI+6
       JNE  SNDINL

       LI   R1,NREGS
       BL   @LDREGS
       BL   @CLS
ISTRT2 ABS  @OPNFIL
       JEQ  MENLPG
       BL   @CLOGIF
       LI   R0,>480
       BL   @CLOCVT
MENLPG BLWP @VMBWD
       DATA >800,DIVLIN,8
       BLWP @VMBFD
       DATA 80,0,40
       LI   R3,STAB
MENLOP MOV  *R3+,R0
       JEQ  MENFIN
       MOV  *R3+,R1
       MOV  *R3+,R2
       BLWP @VMBW
       JMP  MENLOP
MENFIN LI   R0,960
       BL   @CHOICE
       BYTE ' ','1','2','3'
       BYTE '4','5','Q',0
       DATA FXLOAD,FXPLAY,FXCNFG,DSKKAT
       DATA CONVRT,QUITPG

QUITPG BLWP @0

HEXP   BYTE 'P'
HEXCCC BYTE 'C'

*
* Routine to get a file/path from user - 3 entries
*
* BL   @GETKAT
* DATA DEVNAM
*
* BL   @GETDEV
* DATA DEVNAM
*
* LI   R0,SCRN_LOC_PROMPT
* LI   R1,PROMPT_TXT
* LI   R2,PROMPT_LEN
* BL   @GETDV
* DATA DEVNAM
*

GETKAT LI   R1,PTHMSG
       JMP  GETDVK
GETDEV LI   R1,MSG4
GETDVK LI   R0,242
       LI   R2,9
GETDV  BLWP @VMBW
       LI   R2,40
       A    R2,R0
       MOV  *R11+,R9
       MOV  R11,@GETDVX+2
       MOV  @-2(R9),R8        get length field (to place cursor)
       BL   @REDLN2
       DATA >207E
GETDVX B    @0

*
* Load Sound F/X file into memory!!!
*
D6     DATA 6
D4     DATA 4
SAVRT  DATA 0

FXLOAD BLWP @VMBFD
       DATA 120,>2020,840
       BLWP @VMBWD
       DATA 174,MEN1+4,13
*
*  First, get the data into memory!
*
       BL   @PRSFNM
       BL   @GETDEV
       DATA DEVNAM
       BL   @PRSFNM
       BLWP @VMBWD
       DATA >0400,DEVPAB,60  Now open the file
* shave off filename!!!
       MOV  @DEVLEN,R0
       JEQ  FXZZZ
       AI   R0,DEVNAM-1
FXYYY  CB   *R0,@DECMAL
       JEQ  FXZZZ
       MOVB @SPC,*R0
       DEC  R0
       DEC  @DEVLEN
       CI   R0,DEVNAM-1
       JNE  FXYYY
*
FXZZZ  MOV  @GIFOPL,@>8356
       BLWP @DSRLNK
       DATA 8
       JNE  NOPNER
       BL   @CLOGIF          If an error, do a CLOSE and exit
       CLR  @SNDMEM
       B    @ERRHDL

NOPNER LI   R0,>0400         File is now opened
       LI   R1,>0200
       BLWP @VSBW
       BLWP @VMBWD
       DATA 935,LOADTX,10
       SETO @OPNFIL
       SETO @NOGEN           Set all extended memory flags to "not used"
       SETO @NOCPU
       SETO @NOSCRT
       SETO @NOEXP
       SETO @NO9938
       BL   @VRREAD          Get 10K console vram
       BLWP @VMBWD
       DATA >0C00,>A000,>2800
XITFXL C    @>A000,@FXH
       JEQ  YUPPFX
       BL   @CLOGIF
       BLWP @VMBWD
       DATA 925,NOTFXF,30
       B    @DEATH
YUPPFX NEG  R9
       AI   R9,80            Jmp if no records loaded
       JEQ  DEATH4
       MOV  @>A002,@CHNFLG    get chain flag
       SETO @SNDMEM
       SLA  R9,7
       MOV  R9,@VIDSIZ        # of bytes in counter
       S    @D8,@VIDSIZ
       CI   R9,>2800
       JNE  DEATH4

USE38  ABS  @VDPFLG
       JEQ  USECPU
       CLR  @VDPPGS           Load 9938 VRAM
       CLR  @EXPPGS
USE38A BL   @VDREAD
       NEG  R9
       AI   R9,128
       JEQ  FIN38
       CLR  @NO9938           make sure flag is reset IF data is placed here
       INC  @VDPPGS
       MOVB @VDPPGS+1,@>8C06
       BLWP @VMBWD
       DATA >0,>A000,>4000
       MOVB @BYT0,@>8C06
       SLA  R9,7
       MOV  R9,@VDPEND
       CI   R9,>4000
       JNE  FIN38
       C    @VDPPGS,@D6
       JNE  USE38A
* try exp vram
       ABS  @EXPFLG
       JEQ  USECPU
USE38C BL   @VDREAD
       NEG  R9
       AI   R9,128
       JEQ  FIN38
       CLR  @NOEXP            ensure reset flag
       LI   R0,>2D40
       BLWP @VWTR
       MOVB @EXPPGS+1,@>8C06
       BLWP @VMBWD
       DATA 0,>A000,>4000
       MOVB @BYT0,@>8C06
       LI   R0,>2D00
       BLWP @VWTR
       SLA  R9,7
       MOV  R9,@EXPEND
       INC  @EXPPGS
       CI   R9,>4000
       JNE  FIN38
       C    @EXPPGS,@D4
       JNE  USE38C
       JMP  USECPU

FIN38
DEATH4 JMP  DEATH3

USECPU ABS  @GENFLG
       JNE  LDFXGN

       BL   @TIREAD
       CI   R9,192
       JEQ  FIN38
       CLR  @NOCPU            clear CPU used flag
       NEG  R9
       SLA  R9,7
       MOV  R9,@ENDADR
       JNE  DEATH3

       CLR  @SSPGS
       ABS  @SCFLAG
       JEQ  DEATH3
SSLODL MOV  @SSPGS,R1
       LI   R12,>800
       LDCR @SSPCDT(R1),8
       BL   @SCREAD
       CI   R9,64
       JEQ  DEATH3
       CLR  @NOSCRT           clr SCRT used flag
       INC  @SSPGS
       NEG  R9
       SLA  R9,7
       AI   R9,>8000
       MOV  R9,@SCEND
       CI   R9,>8000
       JNE  DEATH3
       ABS  @SSFLAG
       JEQ  DEATH3
       C    @SSPGS,@D4
       JNE  SSLODL

DEATH3 BLWP @VMBWD
       DATA 934,MSG6K,13
*
DEATH2 BL   @CLOGIF
*
DEATH  LI   R0,>03C0
       BL   @KEYIN
       B    @ISTART

* 9640 memory loader
LDFXGN MOV  @GENMEM,R4
       LI   R5,MEM964
       CLR  @SSPGS
LDFXG2 MOVB *R5+,@>8005
       BL   @READRC
       NEG  R9
       SLA  R9,7
       AI   R9,>C000
       CI   R9,>A000
       JEQ  DEATH3
       CLR  @NOGEN            clear 9640 mem flag
       MOV  R9,@SCEND
       INC  @SSPGS
       DEC  R4
       JEQ  DEATH3
       CI   R9,>C000
       JEQ  LDFXG2
       JMP  DEATH3

*
* Here is the F/X play module
*

FXPLAY ABS  @SNDMEM           if no sound loaded, exit
       JNE  FXPLY2
       B    @ISTART
FXPLY2 BLWP @VMBWD
       DATA 934,PLYTXT,12
       LI   R8,>8300
       LI   R9,>832A
       MOV  @DELCNT,R0        if 0, use 11 KHz player
       JEQ  FXPLY3
       LI   R8,TIPLP
       LI   R9,TIVLP
       CI   R0,1
       JEQ  FXPLY3
       DEC  @DELCNT
       LI   R8,TIPLPD
       LI   R9,TIVLPD

FXPLY3
       LI   R12,>0800
       LI   R13,>8800
       LI   R15,>8400
       LI   R0,>8300
       LI   R3,TIPLP
       LI   R4,TIPLP$-TIPLP
LODPAD MOV  *R3+,*R0+
       DECT R4
       JNE  LODPAD
       MOV  @VIDSIZ,R2
       LI   R0,>080C
       MOVB R0,@>8C02
       SWPB R0
       MOVB R0,@>8C02
       MOV  @VDPPGS,R4
       LI   R5,1
       LI   R1,>A000
       ABS  @NO9938
       JEQ  HITIT
       BL   *R9
       JMP  CKPCPU
PLVLP  LI   R2,>4000
       DEC  R4
       JNE  PLVLP1
       MOV  @VDPEND,R2
PLVLP1 MOVB @>83AB,@>8C06
       CLR  R0
       MOVB R0,@>8C02
       INC  R5
       MOVB R0,@>8C02
HITIT  BL   *R9
       MOV  R4,R4
       JNE  PLVLP

       ABS  @NOEXP
       JNE  VDRSET
       MOV  @EXPPGS,R4
       CLR  R5
       LI   R0,>40AD
       MOVB R0,@>8C02
       SWPB R0
       MOVB R0,@>8C02
PLELP  LI   R2,>4000
       DEC  R4
       JNE  PLELP1
       MOV  @EXPEND,R2
PLELP1 MOVB @>83AB,@>8C06
       CLR  R0
       MOVB R0,@>8C02
       INC  R5
       MOVB R0,@>8C02
       BL   *R9
       MOV  R4,R4
       JNE  PLELP
       LI   R0,>00AD
       MOVB R0,@>8C02
       SWPB R0
       MOVB R0,@>8C02
VDRSET MOVB @BYT0,@>8C06
CKPCPU ABS  @NOCPU
       JNE  CKP964
       MOV  @ENDADR,R2
       BL   *R8
       ABS  @NOSCRT
       JNE  SKPZIT
       MOV  @SSPGS,R4
       CLR  R5
SSPLLP LDCR @SSPCDT(R5),8
       LI   R2,>8000
       DEC  R4
       JNE  SSPLY
       MOV  @SCEND,R2
SSPLY  LI   R1,>6000
       BL   *R8
       INC  R5
       MOV  R4,R4
       JNE  SSPLLP

CKP964 ABS  @NOGEN
       JNE  SKPZIT
       MOV  @SSPGS,R4
       LI   R5,MEM964
P964LP MOVB *R5+,@>8005
       LI   R2,>C000
       DEC  R4
       JNE  P96PLY
       MOV  @SCEND,R2
P96PLY LI   R1,>A000
       BL   *R8
       MOV  R4,R4
       JNE  P964LP
       MOVB @SVA000,@>8005

SKPZIT CI   R8,TIPLPD
       JNE  SKPZT2
       INC  @DELCNT
SKPZT2 B    @ISTART

* Currently is >54 bytes long (>8300->8354) - 99/4A 11 KHz
TIPLP  MOVB *R1+,R3           >8300
       SRL  R3,8
       MOVB @BLKDAT(R3),*R15
       MOVB @BLKDA2(R3),*R15
       MOVB @BLKDA3(R3),*R15
       SRC  R3,8
       SRC  R3,8
       ANDI R3,>F
       MOVB @BLKTB1(R3),*R15
       MOVB @BLKTB2(R3),*R15
       MOVB @BLKTB3(R3),*R15
       C    R1,R2
       JNE  TIPLP
       RT
TIVLP  MOVB *R13,R3            >832A
       SRL  R3,8
       MOVB @BLKDAT(R3),*R15
       MOVB @BLKDA2(R3),*R15
       MOVB @BLKDA3(R3),*R15
       SRC  R3,8
       SRC  R3,8
       ANDI R3,>F
       MOVB @BLKTB1(R3),*R15
       MOVB @BLKTB2(R3),*R15
       MOVB @BLKTB3(R3),*R15
       DEC  R2
       JNE  TIVLP
       RT
TIPLP$

TIPLPD MOV  R11,R10
TIPLDL MOVB *R1+,R3
       SRL  R3,8
       MOVB @BLKDAT(R3),*R15
       MOVB @BLKDA2(R3),*R15
       MOVB @BLKDA3(R3),*R15
       BL   @DELAY
       ANDI R3,>F
       MOVB @BLKTB1(R3),*R15
       MOVB @BLKTB2(R3),*R15
       MOVB @BLKTB3(R3),*R15
       BL   @DELAY
       C    R1,R2
       JNE  TIPLDL
TIPLDZ B    *R10

TIVLPD MOV  R11,R10
TIVLDL MOVB *R13,R3
       SRL  R3,8
       MOVB @BLKDAT(R3),*R15
       MOVB @BLKDA2(R3),*R15
       MOVB @BLKDA3(R3),*R15
       BL   @DELAY
       ANDI R3,>F
       MOVB @BLKTB1(R3),*R15
       MOVB @BLKTB2(R3),*R15
       MOVB @BLKTB3(R3),*R15
       BL   @DELAY
       DEC  R2
       JNE  TIVLDL
       B    *R10

* Delay routine - used by 99/4A and 9640 playback routines

DELCNT DATA 0                 min. value of 1

DELAY  MOV  @DELCNT,R0
DELAYL DEC  R0
       JNE  DELAYL
       RT

*
* F/X Configuration
*

BTMSCR TEXT 'Press: 1 - Increase Delay'
BTMSC2 TEXT '2 - Decrease Delay'
BTMSC3 TEXT 'FCTN 9 - Exit To Menu'
SPDTXT TEXT 'Delay Factor:'
DLYNUM TEXT '####'

FXCNFG BLWP @VMBFD
       DATA 120,>2020,840
       LI   R9,MEMTX2
       MOV  @MEMSIZ,R7
       LI   R2,4
       CLR  R15
       BL   @BINASC
       LI   R9,MAXTX2
       LI   R2,4
       CLR  R15
       MOV  @MEMSIZ,R7
       SLA  R7,2
       INC  R7
       BL   @BINASC
       BLWP @VMBWD
       DATA 172,MEN3+4,17
       BLWP @VMBWD
       DATA 330,MEMTXT,18
       BLWP @VMBWD
       DATA 407,MAXTXT,28
       BLWP @VMBWD
       DATA 847,BTMSCR,25
       BLWP @VMBWD
       DATA 894,BTMSC2,18
       BLWP @VMBWD
       DATA 929,BTMSC3,21

CCFGLP LI   R9,DLYNUM
       MOV  @DELCNT,R7
       LI   R2,4
       CLR  R15
       BL   @BINASC
       BLWP @VMBWD
       DATA 489,SPDTXT,17
       CLR  @>8374
       LI   R0,>03C0
       BL   @KEYIN
       CB   @>837C,@SPC
       JNE  CCFGLP
       CLR  R1
       MOVB @>8375,R1
       CI   R1,>3100
       JL   CCFGLP
       CI   R1,>3200
       JEQ  DECDEL
       JH   CCFGLP
INCDEL INC  @DELCNT
       JMP  CCFGLP
DECDEL DEC  @DELCNT
       JLT  INCDEL
       JMP  CCFGLP

*
* Here is the F/X conversion module
*

VOCTX  TEXT 'Crea'
FIRSTR DATA 0

CONVRT BLWP @VMBFD
       DATA 120,>2020,840
       BLWP @VMBWD
       DATA 171,MEN4A+4,18
       CLR  @SNDMEM
*
*  First, get the data into memory!
*
CONVRG BL   @PRSFNM
       LI   R0,242
       LI   R1,MSGINP
       LI   R2,15
       BL   @GETDV
       DATA DEVNAM
       BL   @PRSFNM
       BLWP @VMBWD
       DATA >0400,DEVPAB,60  Now open the file
       MOV  @GIFOPL,@>8356
       BLWP @DSRLNK
       DATA 8
       JNE  NCVTOE
       BL   @CLOGIF          If an error, do a CLOSE and get new filename
       JMP  CONVRG

NCVTOE LI   R0,>0400         Set INPUT file for READ
       LI   R1,>0200
       BLWP @VSBW
       SETO @OPNFIL

CNVRGO BL   @PRSCVT
       LI   R0,362
       LI   R1,MSGOUT
       LI   R2,16
       BL   @GETDV
       DATA CVTNAM
       BL   @PRSCVT
       BLWP @VMBWD
       DATA >0480,CVTPAB,60  Now open output file
       MOV  @CVTOPL,@>8356
       BLWP @DSRLNK
       DATA 8
       JNE  NCVTIE
       LI   R0,>0480
       BL   @CLOCVT
       SETO @OPNFIL
       JMP  CNVRGO

NCVTIE LI   R0,>0480          Set it for WRITE
       LI   R1,>0300
       BLWP @VSBW

GETAMP MOV  @AMPLVL,R6        now get amp level
       LI   R2,>A
       MPY  R2,R6
       LI   R9,AMPFAC
       LI   R2,3
       CLR  R15
       BL   @BINASC
       BLWP @VMBWD
       DATA 482,AMPTXT,26
       BLWP @VMBWD
       DATA 522,SPCMOD,37
       LI   R0,508
       BL   @KEYIN
       CI   R1,>2000
       JNE  CKRTNK
       INC  @AMPLVL
       C    @AMPLVL,@D16
       JL   GETAMP
       CLR  @AMPLVL
CKRTNK CI   R1,>0D00
       JNE  GETAMP
       LI   R1,>2000
       BLWP @VSBW

       SETO @FIRSTR
       BLWP @VMBFD
       DATA 522,>2020,37
       BLWP @VMBWD
       DATA 934,PROCTX,13

TRNSET CLR  R0                Set up translation tables
       LI   R1,>D000
       LI   R2,>D100
TRNST1 MOVB R0,*R1+
       MOVB R0,*R2+
       CI   R1,>D080
       JL   TRNST1
       LI   R0,>0F00
       LI   R3,>F000
TRNST2 MOVB R0,*R1+
       MOVB R3,*R2+
       CI   R1,>D100
       JNE  TRNST2

       MOV  @AMPLVL,R0
       MPY  @D7,R0
       AI   R1,>D010
       MOV  R1,R2
       AI   R2,>0100
       LI   R3,16
       S    @AMPLVL,R3
       LI   R4,>0100
       LI   R5,>1000
TRNST4 MOV  R3,R6
TRNST3 MOVB R4,*R1+
       MOVB R5,*R2+
       DEC  R6
       JNE  TRNST3
       AI   R4,>0100
       AI   R5,>1000
       CI   R4,>0F00
       JNE  TRNST4

* At this point, tables are set up
       BL   @READRC
       CI   R9,64
       JEQ  CVTXIT
       C    @VOCTX,@>A000
       JNE  CVTRAW
       C    @VOCTX+2,@>A002
       JEQ  CVTVOC
       JMP  CVTRAW
* conversion for raw (SND-type) files
CVTLOP BL   @READRC           8K read from file
       CI   R9,64
       JEQ  CVTXIT
CVTRAW NEG  R9
       SLA  R9,7
       AI   R9,>C000          r9 points to end of buffer
       LI   R0,>A000          R0 = INCOMING DATA
       LI   R1,>A000          R1 = OUTGOING DATA

CVTLP1 MOVB *R0+,R2
       SRL  R2,8
       MOVB @>D100(R2),*R1
       MOVB *R0+,R2
       SRL  R2,8
       SOCB @>D000(R2),*R1+
       C    R0,R9
       JL   CVTLP1
       CI   R9,>C000
       JEQ  NONUKE
       MOV  R9,R0
CVTSET SETO *R0+
       CI   R0,>C000
       JNE  CVTSET
NONUKE AI   R1,>607F
       SRL  R1,7
       ABS  @FIRSTR
       JEQ  FIRSTS
       LI   R0,>A000
       MOV  @FXH,*R0+
       CLR  *R0+
       CLR  *R0+
       CLR  *R0
       CLR  @FIRSTR
FIRSTS BL   @WRITRC
       CI   R9,>C000
       JEQ  CVTLOP
CVTXIT BL   @CLOGIF           Close files
       LI   R0,>0480
       BL   @CLOCVT
       LI   R0,CVTLEN
       LI   R1,DEVLEN
       LI   R2,42
NEWDFT MOV  *R0+,*R1+
       DECT R2
       JNE  NEWDFT
       BLWP @VMBWD
       DATA 923,CCPAK,35
       B    @DEATH

* conversion for a VOC-type file

CVTVOC LI   R13,>C000          r1 points to outgoing data stream
       MOV  @FXH,*R13+         set up header
       CLR  *R13+
       CLR  *R13+
       CLR  *R13+
       LI   R14,>A01A          r0 = incoming data ptr
       BL   @DOVOC

CVTVC1 BL   @FETCHR
       SRL  R15,8
       MOVB @>D100(R15),*R13
       DEC  @VOCLO
       JOC  CVTVC2
       DEC  @VOCHI
       JOC  CVTVC2

       BL   @DOVOC

CVTVC2 BL   @FETCHR
       SRL  R15,8
       SOCB @>D000(R15),*R13+
       DEC  @VOCLO
       JOC  CVTVC3
       DEC  @VOCHI
       JOC  CVTVC3

       BL   @DOVOC

CVTVC3 CI   R13,>D000
       JNE  CVTVC1
       LI   R1,32
       BL   @WRITVC
       LI   R13,>C000
       JMP  CVTVC1

FETCHR MOVB *R14+,R15
       CI   R14,>C000
       JNE  FTCH1
       MOV  R11,@SAVRT
       BL   @READRC
       MOV  @SAVRT,R11
       LI   R14,>A000
FTCH1  RT

VOCHI  DATA 0
VOCLO  DATA 0

DOVOC  MOV  R11,@DOVOCX+2
       BL   @FETCHR
       ANDI R15,>FF00
       JEQ  DOVOEF
       BL   @FETCHR
       MOVB R15,@VOCLO+1
       BL   @FETCHR
       MOVB R15,@VOCLO
       BL   @FETCHR
       CLR  @VOCHI
       MOVB R15,@VOCHI+1
       BL   @FETCHR
       BL   @FETCHR
       LI   R0,3
       S    R0,@VOCLO
       JOC  DOVOCG
       DEC  @VOCHI
DOVOCG MOV  @VOCLO,R0
       JNE  DOVOCX
       DEC  @VOCHI
DOVOCX B    @0

DOVOEF SETO R1
       MOV  R13,R9
DOVOE1 MOVB R1,*R13+
       CI   R13,>D000
       JNE  DOVOE1
       MOV  R9,R1
       AI   R1,>407F
       SRL  R1,7
       BL   @WRITVC
       B    @CVTXIT


DEVPAB DATA >0004
BUFLOC DATA >0500,>8080,>0000
DEVLEN DATA 0
DEVNAM TEXT '                                          '

CVTPAB DATA >0002
CVTBUF DATA >0600,>8080,>0000
CVTLEN DATA 0
CVTNAM TEXT '                                          '

HFF    DATA 255
SAVR1  DATA 0
FSIZE  DATA 0
DFLAG  DATA 0

CLOGIF LI   R0,>0400         Close an open FX file!
CLOCVT LI   R1,>0100
       BLWP @VSBW
       AI   R0,9
       MOV  R0,@>8356
       BLWP @DSRLNK
       DATA 8
       CLR  @OPNFIL
       RT

ERRHDL BLWP @VMBWD
       DATA 928,MSG6,24
       B    @DEATH

LOOPAC DATA 0                Accumulator for loop
RPRT   DATA 0

VDREAD LI   R1,>A000
       LI   R9,128
       JMP  READRS

SCREAD LI   R1,>6000
       LI   R9,64
       JMP  READRS

VRREAD LI   R1,>A000
       LI   R9,80
       JMP  READRS

TIREAD LI   R1,>A000
       LI   R9,192
       JMP  READRS

READRC LI   R9,64
       LI   R1,>A000
READRS LI   R2,>80
READRZ LI   R0,>0500
READRL MOV  @GIFOPL,@>8356
       BLWP @DSRLNK          Read 64 128 byte records from file, store them
       DATA 8
       JNE  NREDER
       CB   R0,@HEX5
       JNE  ERRHDL
       ABS  @CHNFLG
       JEQ  NCHNF
       BLWP @VMBWD
       DATA 883,CHAINT,36
       BLWP @VMBWD
       DATA 924,CHAIN2,34
       STWP R0
       MOV  R0,@RSTR0+2
       MOV  R0,@RRSTR0+2
       LWPI >8300
       LI   R0,>03C0
       BL   @KEYIN
       BLWP @VMBFD
       DATA 880,>2020,80
       ANDI R1,>7F00
       CI   R1,>0C00
       JEQ  RSTR0
       BLWP @VMBWD
       DATA 935,LOADTX,10
       LI   R0,>409
       BLWP @VSBR
       MOV  R1,R0
       SRL  R0,8
       DEC  R0
       AI   R0,>040A
       BLWP @VSBR
       AI   R1,>0100
       BLWP @VSBW
       LI   R0,>400
       CLR  R1
       BLWP @VSBW
       MOV  @GIFOPL,@>8356
       BLWP @DSRLNK
       DATA 8
       JEQ  ERRHDL
       LI   R0,>0400
       LI   R1,>0200
       BLWP @VSBW
RRSTR0 LWPI 0
       JMP  READRZ
RSTR0  LWPI 0
NCHNF  RT
NREDER BLWP @VMBR            If an error in 1st half, flag it!
       A    R2,R1
       DEC  R9
       JNE  READRL
       RT

WRITVC MOV  R1,R8
       LI   R1,>C000
       JMP  WRITR

WRITRC MOV  R1,R8
       LI   R1,>A000
WRITR  LI   R0,>0600
       LI   R2,>80
WRITRL BLWP @VMBW
       MOV  @CVTOPL,@>8356
       BLWP @DSRLNK
       DATA 8
       JEQ  WRITER
       A    R2,R1
       DEC  R8
       JNE  WRITRL
       RT

WRITER LI   R0,>0480
       BL   @CLOCVT
       BL   @CLOGIF
       B    @ERRHDL

PRSHDR MOV  R11,R10
       LI   R0,>0400         Put file in READ mode
       LI   R1,>200
       BLWP @VSBW
       BL   @READRC
       B    *R10

*
* Universal Diskette Catalog...
*

* convert floating point to ASCII
* R9 - point to destination rightmost byte+1
* R4 - if 0, positive number, -1 for negative

FLTBUF BSS 8

FLTASC MOV  R11,@FLTAX+2
       LI   R0,FLTBUF
       ABS  *R0
FLTNRM MOVB *R0,R3
       SWPB R3
       ANDI R3,>0007    Get exponent
       INC  R3          Make it a parsing index
FLTALP MOVB @FLTBUF(R3),R7   r7 contains number to 'ascify'
       SRL  R7,8
       LI   R15,2
       CI   R3,1
       JNE  FLTNPX
       CLR  R15
FLTNPX BL   @BINASN
       DEC  R3
       JNE  FLTALP
FLTAX  B    @0

PARSDR MOV  R6,R0            Fetch current pab address
       BLWP @VSBR
       INC  R0               Adjust pab address
       SRL  R1,8
       MOV  R1,R2
       JNE  PARSDD
       INCT R11
       RT
PARSDD MOV  *R11+,R1         Get dest address
       BLWP @VMBR            Read record
       A    R2,R0
       MOV  R0,R6
       RT

ZAPIT  MOV  *R11+,R9
       MOV  *R11+,R2
ZAPIT1 MOVB @SPC,*R9+
       DEC  R2
       JNE  ZAPIT1
       RT

KATCNT DATA 120
KATPAB DATA >000D,>0500,>0000,>0000
KATTX1 TEXT 'Dir: '
KATTNM TEXT 'XXXXXXXXXX'
KATT1A TEXT ' Size:'
KATT1B TEXT 'XXXXXX Free:'
KATT1C TEXT 'XXXXXX '
FILNMX TEXT '    XXXXXXXXXX '
FIL1SZ TEXT 'XXXXX  '
FIL1A  TEXT 'DIS/FIX '
FIL1B  TEXT 'XXXXXX '
FIL1C  TEXT 'P'

FILTBL DATA DISFIX,DISVAR,INTFIX,INTVAR
       DATA PROGRM,DIRECT,EMLATE
DISFIX TEXT 'Dis/Fix'
DISVAR TEXT 'Dis/Var'
INTFIX TEXT 'Int/Fix'
INTVAR TEXT 'Int/Var'
PROGRM TEXT 'Program'
DIRECT TEXT 'Dir    '
EMLATE TEXT 'Emulate'
MSGMOR TEXT '[More]'
DZXDZX DATA 0

DSKKAT BLWP @VMBFD
D120   DATA 120,>2020,840
       BLWP @VMBWD
       DATA 174,MEN4+4,12
* shave off filename!!!
       MOV  @DEVLEN,R0
       JEQ  DSKZZZ
       AI   R0,DEVNAM-1
DSKYYY CB   *R0,@DECMAL
       JEQ  DSKZZZ
       MOVB @SPC,*R0
       DEC  R0
       DEC  @DEVLEN
       CI   R0,DEVNAM-1
       JNE  DSKYYY
*
DSKZZZ BL   @GETKAT
       DATA DEVNAM
DTLENT BL   @PRSFNM
       MOV  @DEVLEN,R1
       CI   R1,40
       JEQ  DSKKAT
       AI   R1,DEVNAM
       CB   @>FFFF(R1),@DECMAL
       JEQ  NODOT$
       MOVB @DECMAL,*R1
       INC  @DEVLEN
NODOT$ BLWP @VMBWD
       DATA >0400,KATPAB,8
       BLWP @VMBWD
       DATA >0408,DEVLEN,50
       MOV  @GIFOPL,@>8356
       BLWP @DSRLNK
       DATA 8
       JNE  NOKTE1
NOKTE2 BLWP @VMBWD
       DATA 928,MSG6,24
NOKTE3 BL   @CLOGIF
       LI   R0,>03C0
       BL   @KEYIN
       B    @ISTART
NOKTE1 SETO @OPNFIL
       LI   R0,>0400         Put catalog in READ mode
       LI   R1,>0200
       BLWP @VSBW
       MOV  @GIFOPL,@>8356   Now get diskname and size info
       BLWP @DSRLNK
       DATA 8
       JEQ  NOKTE2
       BLWP @VMBFD
       DATA 0,>2020,80
       BLWP @VMBFD
       DATA 800,0,40
       BLWP @VMBWD
       DATA 846,USATXT,29
       BLWP @VMBWD
       DATA 884,LCDTXT,33
       BLWP @VMBWD
       DATA 922,DSPTXT,37
       SETO @DZXDZX
       CLR  R0
       LI   R1,DEVNAM
       MOV  @DEVLEN,R2
       BLWP @VMBW
*** beta test
       LI   R6,>0500         read diskname
       BL   @ZAPIT
       DATA KATTNM,10
       BL   @PARSDR
       DATA KATTNM
       BL   @PARSDR
       DATA FLTBUF
       MOVB @FLTBUF+1,R0     If not a dir, skip this
       JNE  DDIRSK
       BL   @PARSDR
       DATA FLTBUF
       BL   @ZAPIT
       DATA KATT1B,6
       BL   @FLTASC
       BL   @PARSDR
       DATA FLTBUF
       BL   @ZAPIT
       DATA KATT1C,6
       BL   @FLTASC
DEC40  EQU  $+2
       LI   R2,40
       JMP  FULDIR
DDIRSK LI   R2,15
FULDIR LI   R0,40
       LI   R1,KATTX1
       BLWP @VMBW
* diskname info is now displayed!
DMAINL BLWP @VMBFD
       DATA 120,>2020,680
       MOV  @D120,@KATCNT
DSUBML MOV  @GIFOPL,@>8356
       BLWP @DSRLNK
       DATA 8
       JEQ  NOKTE2       handle error
       LI   R6,>0500
       BL   @ZAPIT       get filename
       DATA FILNMX+4,10
       BL   @PARSDR
       DATA FILNMX+4
       MOV  R2,R2
       JNE  DMAINX
       C    @KATCNT,@D120
       JEQ  DPAUSL
       S    @DEC40,@KATCNT
       LI   R0,120
       JMP  DPAUL1

* handle last file screen

KLKLKL B    @NOKTE3

DMAINX BL   @PARSDR
       DATA FLTBUF
       MOVB @PROGRM,@FIL1C
       MOV  @FLTBUF,R1
       JLT  NEGGIT
       MOVB @SPC,@FIL1C
NEGGIT ABS  R1
       ANDI R1,>FF
       JEQ  NXTPDR
       SLA  R1,1
       MOV  @FILTBL-2(R1),R1
       LI   R0,FIL1A
       MOVB *R1+,*R0+
       MOVB *R1+,*R0+
       MOVB *R1+,*R0+
       MOVB *R1+,*R0+
       MOVB *R1+,*R0+
       MOVB *R1+,*R0+
       MOVB *R1,*R0
NXTPDR BL   @PARSDR
       DATA FLTBUF
       BL   @ZAPIT
       DATA FIL1SZ,5
       BL   @FLTASC
       BL   @PARSDR
       DATA FLTBUF
       BL   @ZAPIT
       DATA FIL1B,6
       ABS  @FLTBUF
       JEQ  NXTPDZ
       BL   @FLTASC
NXTPDZ MOV  @KATCNT,R0
       LI   R1,FILNMX
       LI   R2,38
       BLWP @VMBW
       A    @DEC40,R0
D800   EQU  $+2
       CI   R0,800
       JEQ  DPAUSL
       MOV  R0,@KATCNT
       JMP  DSUBML

* process "arrow"

DPAUSL MOV  @D120,R0
       C    R0,@KATCNT
       JNE  DPAUL1            If no files on screen, wait
       ABS  @DZXDZX
       JNE  KLKLKL
KZKZKZ BL   @CLOGIF           No need to display a blank scrn @ end
       B    @ISTART
DPAUL1 CLR  @DZXDZX
       LI   R1,'>>'
       ANDI R0,>0FFC
       INCT R0
       BLWP @VSBW
       BL   @KEYINB
       ANDI R1,>7F00
       CI   R1,>0A00
       JNE  DPAUL2
DOWNIT LI   R1,>2020
       BLWP @VSBW
       DECT R0
       C    R0,@KATCNT
       JEQ  DPAUL1
       AI   R0,40
       JMP  DPAUL1
DPAUL2 CI   R1,>0B00
       JEQ  UPIT
       ANDI R1,>5F00
       CI   R1,'X'*256
       JEQ  DOWNIT
       CI   R1,'E'*256
       JNE  DPAUL3
UPIT   LI   R1,>2020
       BLWP @VSBW
       DECT R0
       C    R0,@D120
       JEQ  DPAUL1
       AI   R0,-40
       JMP  DPAUL1
DPAUL3 MOV  R1,R1
       JNE  DPAUL4
       C    @KATCNT,@D760
       JEQ  DPAL99
       B    @KZKZKZ
DPAL99 B    @DMAINL
DPAUL4 CI   R1,'L'*256
       JNE  DPAUL5
       LI   R3,FXLOAD
       JMP  MBOX
DPAUL5 CI   R1,'C'*256
       JNE  DPAUL6
       LI   R3,CONVRT
       JMP  MBOX
DPAUL6 CI   R1,'S'*256
       JNE  DPAUL1
       AI   R0,22
       CLR  R1
       BLWP @VSBR
       CI   R1,'r'*256
       JEQ  DPAUL8
       AI   R0,-24
       JMP  DPAUL1
DPAUL8 LI   R3,DTLENT
       AI   R0,-22
MBOX   INCT R0
       LI   R1,DEVNAM
       LI   R2,10
       A    @DEVLEN,R1
       CI   R1,DEVNAM+31
       JL   DPAUL7
       MOV  R1,R4
       LI   R2,DEVNAM+40
       S    R2,R4
DPAUL7 BLWP @VMBR
       BL   @CLOGIF
       BLWP @VMBFD
       DATA 0,>2020,960
       BLWP @VMBWD
       DATA 8,MSG1,24
       BLWP @VMBWD
       DATA 45,MSG2,30
       BLWP @VMBFD
       DATA 80,0,40
       B    *R3

VWTR   DATA UTILWS,VWTR1
VWTR1  MOV  *R13,R0
       MOVB @UTILWS+1,@>8C02
       ORI  R0,>8000
       MOVB R0,@>8C02
       RTWP

KSCAN  DATA UTILWS,KSCAN1
KSCAN1 LWPI >83E0
       BL   @>E
       LWPI UTILWS
       RTWP

WTAD4  LI   R1,>4000
WTAD3  MOVB *R14+,R0
       MOVB *R14+,@>8C02
       SOC  R1,R0
       MOVB R0,@>8C02
       MOV  *R14+,R1
       MOV  *R14+,R2
       RT

H4000  EQU  $+2
WTAD2  LI   R1,>4000
WTAD   MOV  *R13,R0
       MOVB @UTILWS+1,@>8C02
       SOC  R1,R0
       MOVB R0,@>8C02
       MOV  @2(R13),R1
       MOV  @4(R13),R2
       RT

VSBW   DATA UTILWS,$+2
       BL   @WTAD2
       MOVB R1,@>8C00
       RTWP

VSBR   DATA UTILWS,VSBR1
VSBR1  CLR  R1
       BL   @WTAD
       MOVB @>8800,@2(R13)
       RTWP

VMBRD  DATA UTILWS,$+2
       CLR  R1
       BL   @WTAD3
       JMP  VMBR2
VMBR   DATA UTILWS,$+2
       CLR  R1
       BL   @WTAD
VMBR2  MOVB @>8800,*R1+
       DEC  R2
       JNE  VMBR2
       RTWP

VMBFD  DATA UTILWS,$+2
       BL   @WTAD4
VMBF2  MOVB R1,@>8C00
       DEC  R2
       JNE  VMBF2
       RTWP

VMBWD  DATA UTILWS,$+2
       BL   @WTAD4
       JMP  VMBW2
VMBW   DATA UTILWS,$+2
       BL   @WTAD2
VMBW2  MOVB *R1+,@>8C00
       DEC  R2
       JNE  VMBW2
       RTWP

PNTR   EQU  >8356
SCLEN  EQU  >8355
CRULST EQU  >83D0
SADDR  EQU  >83D2
GPLWS  EQU  >83E0
H20    DATA >2000
DECMAL TEXT '.'
HZZ    BYTE >AA
SAVTKX DATA 0

DSRLNK DATA DLNKWS,DLENTR

DLENTR MOV  *R14+,R5
       MOV  @TKX,@SAVTKX
       SZCB @H20,R15
       MOV  @PNTR,R0
       MOV  R0,R9
       AI   R9,-8
       BLWP @VSBR
       MOVB R1,R3
       SRL  R3,8
       SETO R4
       LI   R2,NAMBUF
LNK$LP INC  R0
       INC  R4
       CI   R4,7
       JGT  LNKERR
       C    R4,R3
       JEQ  LNK$LN
       BLWP @VSBR
       MOVB R1,*R2+
       CB   R1,@DECMAL
       JNE  LNK$LP
LNK$LN MOV  R4,R4
       JEQ  LNKERR
       CLR  @CRULST
       MOV  R4,@SCLEN-1
       INC  R4
       A    R4,@PNTR
SROM   LWPI GPLWS
       CLR  R1
       LI   R12,>1200
       JMP  NOOFF2
NOROM  SBZ  0
NOOFF  AI   R12,>100
       CLR  @CRULST
       CI   R12,>1200
       JEQ  NODSR
       CI   R12,>2000
       JNE  NOOFF2
       LI   R12,>1000
NOOFF2 MOV  R12,@CRULST
       SBO  0
       LI   R2,>4000
       CB   *R2,@HZZ
       JNE  NOROM
       A    @TYPE,R2
       JMP  SGO2
SGO    MOV  @SADDR,R2
       SBO  0
SGO2   MOV  *R2,R2
       JEQ  NOROM
       MOV  R2,@SADDR
       INCT R2
       MOV  *R2+,R9
       MOVB @SCLEN,R5
       JEQ  NAME2
       CB   R5,*R2+
       JNE  SGO
       SRL  R5,8
       LI   R6,NAMBUF
NAME1  CB   *R6+,*R2+
       JNE  SGO
       DEC  R5
       JNE  NAME1
NAME2  INC  R1
QCKENT BL   *R9
TKX    JMP  SGO
       SBZ  0
       LWPI DLNKWS
       MOV  @SAVTKX,@TKX
       MOV  R9,R0
       BLWP @VSBR
       SRL  R1,13
       JNE  IOERRZ
       RTWP

NODSR  LWPI DLNKWS
LNKERR CLR  R1
IOERRZ SWPB R1
       MOVB R1,*R13
       SOCB @H20,R15
       RTWP
UTILWS EQU  >8380
DLNKWS DATA 0,0,0,0,0
TYPE   DATA 0,0,0,0,0,0,0,0,0,0,0
NAMBUF BSS  8
SLAST  EQU  $
MKR    LWPI >BF00
       LI   R0,>2000
       LI   R1,>A000
       LI   R2,SLAST-SFIRST
SLLLLP MOV  *R1+,*R0+
       DECT R2
       JNE  SLLLLP
       B    @START
       END
