Loads a cassette through the MIO's cassette interface, saves it to disk and runs it.
Written by Sarah Craswell while writing and debugging Zimul80r
CLOAD finds the E6H sync as the MIO manual's test cassette loader does, but only after a leader, and not if more E6H follows, because IMSAI's own MIO test tape opens with minutes of E6H test tone. It stops at the count, after 2.5 seconds of quiet, when memory is full, or on a key.
It was checked against a recording of IMSAI's MIO test tape, loading its program byte for byte from both .mp3 and .wav captures.
Using it
A>CLOAD load to 3000H until the tape goes quiet
A>CLOAD A=0100 N=200 address and count
A>CLOAD G=3000 and run it
A>CLOAD PROG.COM and save it to disk
CLOAD.ASM
670 LINES
; CLOAD.ASM - cassette loader for the IMSAI MIO's cassette interface, for CP/M;; Loads a recording into memory, optionally saves it to a disk file, and; optionally runs it. A tape holds a leader, the sync byte E6H and then the; data, which is how IMSAI's own MIO test tape is laid out. CLOAD finds the; sync as the manual's test cassette loader does (MIO manual, Appendix A): a; bit at a time with control 60H, then whole bytes with control 20H.;; CLOAD load to 3000H until the tape goes quiet; CLOAD A=3000 N=3B2 the load address and the number of bytes (hex); CLOAD FILE.BIN also save what was loaded (B:FILE.BIN for drive B); CLOAD G=3100 and run it from this address when it has loaded; CLOAD D=40 C=43 R=2 the cassette data port, the control/status port and; the ready bit (hex; R=2- if it is low when ready);; The options can be combined in any order: CLOAD N=3B2 TEST.BIN G=3100; The defaults suit the IMSAI test arrangement.;; Differences from the manual's loader:; - It takes an E6H as the sync only straight after a leader (00H, FFH or; 3CH), and not if another E6H follows it. IMSAI's test tape starts with; minutes of nothing but E6H, a test tone that the manual's loader would; take for the data. So a recording can't begin with the byte E6H.; - It stops when the tape goes quiet, about 2.5 seconds with no byte, or; when memory runs out below the BDOS, and any key on the console stops it.;; Nothing is printed between finding the sync and the end of the data, since; the console could take long enough to lose bits from the tape. While it; loads, the programmed-output lights show the page it has reached, and at the; end the complement of the last byte, as in the manual.;; Assemble with: python tools\asm8080\asm8080.py tools\mio\CLOAD.ASMBDOS EQU 5CONIN EQU 1CONOUT EQU 2PRINT EQU 9CONST EQU 11CLOSEF EQU 16DELETEF EQU 19WRITEF EQU 21MAKEF EQU 22SETDMA EQU 26LIGHTS EQU 0FFH ; programmed-output lightsSYNC EQU 0E6HCTLBIT EQU 60H ; read, ready after every bitCTLBYTE EQU 20H ; read, ready after every eighth bit ORG 100HSTART: LXI SP,STACK LDA 7 ; leave a page below the BDOS for the padding DCR A STA TOPPG CALL PARSE LXI D,USAGE JC SAY LHLD LOADAT ; it must go between this program and the BDOS LXI D,PROGEND MOV A,H CMP D JC BADADDR JNZ ABOVE MOV A,L CMP E JC BADADDRABOVE: LDA TOPPG MOV B,A MOV A,H CMP B JC FITSBADADDR:LXI D,NOFITSAY: CALL PSTR JMP 0; Put the chosen ports, bit and polarity into the code that uses them.FITS: LDA CTPORT STA CTLOUT+1 STA STATIN+1 LDA DTPORT STA DATIN+1 LDA RDPOL STA POLIMM+1 LDA RDMASK STA MSKIMM+1 LXI D,HELLO1 CALL PSTR LHLD LOADAT CALL PHEX16 LXI D,HELLO2 CALL PSTR CALL HUNT ; A = the first byte of the data JC NOSYNC LHLD COUNT XCHG ; DE = bytes left LHLD LOADAT ; HL = where the next byte goesLOAD1: MOV M,A INX H STA LAST PUSH H LHLD SUM MOV C,A MVI B,0 DAD B SHLD SUM POP H DCX D MOV A,D ORA E LXI B,ENDCNT JZ LOADED LDA TOPPG CMP H LXI B,ENDTOP JZ LOADED MOV A,L ORA A JNZ LOAD2 MOV A,H ; a new page: show it, and look at the keyboard CMA OUT LIGHTS CALL KEY LXI B,ENDKEY JC LOADEDLOAD2: CALL WAITQ LXI B,ENDQ JZ LOADED CALL RDDATA JMP LOAD1; BC = what ended it. Turn the interface off, then say how it went.LOADED: SHLD ENDADR PUSH B XRA A CALL SETCTL LDA LAST CMA OUT LIGHTS LXI D,DONE1 CALL PSTR LHLD LOADAT ; bytes = end - start XCHG LHLD ENDADR MOV A,L SUB E MOV L,A MOV A,H SBB D MOV H,A CALL PHEX16 LXI D,DONE2 CALL PSTR LHLD LOADAT CALL PHEX16 LXI D,DONE3 CALL PSTR LHLD ENDADR DCX H CALL PHEX16 LXI D,DONE4 CALL PSTR LHLD SUM CALL PHEX16 LXI D,DONE5 CALL PSTR POP D CALL PSTR LDA HAVEF ORA A CNZ SAVEIT LDA HAVEG ORA A JZ 0 LHLD GOADR PCHLNOSYNC: XRA A CALL SETCTL LXI D,STOPPED JMP SAY; Finds the sync. Carry if a key was pressed, else A = the byte after it.; D holds the eight bits before the eight in the shift register (E).HUNT: MVI A,CTLBIT CALL SETCTL LXI D,5500H ; not a leader MVI C,0HUNT1: CALL WAITKEY RC CALL RDDATA MOV B,A MOV A,E ; the bit leaving E goes into D RAL MOV A,D RAL MOV D,A MOV E,B MOV A,E CPI SYNC JNZ HUNT2 MOV A,D ORA A JZ FOUND CPI 0FFH JZ FOUND CPI 3CH JZ FOUNDHUNT2: DCR C ; every 256 bits, just after one has been read JNZ HUNT1 CALL KEY RC JMP HUNT1; A sync after a leader. Changing the control port restarts the count of; eight, so the bytes from here on are aligned to it.FOUND: MVI A,CTLBYTE CALL SETCTL CALL WAITKEY RC CALL RDDATA CPI SYNC JZ TONE ORA A ; clear carry RET; More E6H: the test tone. Wait for it to end and hunt again.TONE: MVI C,0TONE1: CALL WAITKEY RC CALL RDDATA CPI SYNC JNZ HUNT DCR C JNZ TONE1 CALL KEY RC JMP TONE1; Waits for the cassette interface to be ready. Carry if a key was pressed.; With the tape moving it never looks at the keyboard, which HUNT and TONE do; when there is time; if nothing is arriving there is time.WAITKEY:MVI B,0WAITK1: CALL READY RNZ ; (carry is clear) DCR B JNZ WAITK1 CALL KEY RC JMP WAITKEY; Waits for the next byte: Z if the tape has gone quiet.WAITQ: LXI B,0WAITQ1: CALL READY RNZ DCX B MOV A,B ORA C JNZ WAITQ1 RET; NZ with carry clear if the cassette interface is ready.READY:STATIN: IN 0POLIMM: XRI 0MSKIMM: ANI 0 RETRDDATA:DATIN: IN 0 RETSETCTL:CTLOUT: OUT 0 RET; Carry if a key has been pressed, and swallows it. Keeps BC, DE and HL.KEY: PUSH B PUSH D PUSH H MVI C,CONST CALL BDOS ORA A ; clear carry JZ KEY1 MVI C,CONIN CALL BDOS STCKEY1: POP H POP D POP B RET; Saves what was loaded, filling out the last 128-byte record with 1AH.SAVEIT: LHLD LOADAT XCHG LHLD ENDADRPAD: MOV A,L SUB E ANI 7FH JZ PADDED MVI M,1AH INX H JMP PADPADDED: SHLD ENDADR MVI C,DELETEF LXI D,FCB CALL BDOS MVI C,MAKEF LXI D,FCB CALL BDOS INR A LXI D,NOFILE JZ PSTR LHLD LOADAT SHLD DMAPTRSAVE1: LHLD DMAPTR XCHG ; DE = the next record LHLD ENDADR MOV A,E CMP L JNZ SAVE2 MOV A,D CMP H JZ SAVEDSAVE2: MVI C,SETDMA CALL BDOS MVI C,WRITEF LXI D,FCB CALL BDOS ORA A LXI D,DISKFUL JNZ PSTR LHLD DMAPTR LXI D,80H DAD D SHLD DMAPTR JMP SAVE1SAVED: MVI C,CLOSEF LXI D,FCB CALL BDOS INR A LXI D,NOFILE JZ PSTR LXI D,WROTE JMP PSTRPSTR: MVI C,PRINT JMP BDOSPHEX16: MOV A,H PUSH H CALL PHEX POP H MOV A,LPHEX: PUSH PSW RRC RRC RRC RRC CALL PNIB POP PSWPNIB: ANI 0FH ADI 90H DAA ACI 40H DAA MOV E,A MVI C,CONOUT JMP BDOS; Reads the command tail. Carry set if something in it doesn't make sense.PARSE: LXI H,80H MOV B,M ; B = characters left INX HPNEXT: MOV A,B ORA A RZ ; (carry is clear) MOV A,M CPI ' ' JNZ PTOKEN INX H DCR B JMP PNEXTPTOKEN: MOV C,A ; a letter followed by = is a setting MOV A,B CPI 2 JC PFILE INX H MOV A,M DCX H CPI '=' JNZ PFILE INX H INX H DCR B DCR B MOV A,C CPI 'A' JZ OPTA CPI 'N' JZ OPTN CPI 'G' JZ OPTG CPI 'D' JZ OPTD CPI 'C' JZ OPTC CPI 'R' JZ OPTR STC RETOPTA: CALL HEXNUM RC XCHG SHLD LOADAT XCHG JMP PNEXTOPTN: CALL HEXNUM RC MOV A,D ORA E STC RZ ; no bytes makes no sense XCHG SHLD COUNT XCHG JMP PNEXTOPTG: CALL HEXNUM RC XCHG SHLD GOADR XCHG MVI A,1 STA HAVEG JMP PNEXTOPTD: CALL PORTNUM RC STA DTPORT JMP PNEXTOPTC: CALL PORTNUM RC STA CTPORT JMP PNEXTOPTR: CALL BITNUM RC STA RDMASK MOV A,C STA RDPOL JMP PNEXT; A file name, with an optional drive, into the FCB.PFILE: LXI D,FCB MVI C,36PFILE1: XRA A STAX D INX D DCR C JNZ PFILE1 LXI D,FCB+1 MVI C,11 MVI A,' 'PFILE2: STAX D INX D DCR C JNZ PFILE2 MOV A,B CPI 2 JC PFILE3 INX H MOV A,M DCX H CPI ':' JNZ PFILE3 MOV A,M SUI 'A'-1 STA FCB INX H INX H DCR B DCR BPFILE3: LXI D,FCB+1 MVI C,8 CALL PNAME RC MOV A,B ORA A JZ PFILE4 MOV A,M CPI '.' JNZ PFILE4 INX H DCR B LXI D,FCB+9 MVI C,3 CALL PNAME RCPFILE4: LDA FCB+1 CPI ' ' STC RZ ; no name MVI A,1 STA HAVEF JMP PNEXT; Copies up to C characters of a name to DE, up to a space, a dot or the end.; Carry set for a wildcard, which can't name a file to save.PNAME: MOV A,B ORA A RZ MOV A,M CPI ' ' RZ CPI '.' RZ CPI '*' JZ PNAMEX CPI '?' JZ PNAMEX MOV A,C ORA A JZ PNAME1 ; too long: the rest is ignored, as the CCP does MOV A,M STAX D INX D DCR CPNAME1: INX H DCR B JMP PNAMEPNAMEX: STC RET; A port number from 00 to FF.PORTNUM:CALL HEXNUM RC MOV A,D ORA A STC RNZ MOV A,E ORA A ; clear carry RET; A status bit 0-7, then an optional - for active low.; Returns A = the bit's mask and C = the polarity (the mask if active low, else 0).BITNUM: CALL HEXNUM RC MOV A,D ORA A STC RNZ MOV A,E CPI 8 CMC RC MVI A,1BIT1: DCR E JM BIT2 RLC JMP BIT1BIT2: MVI C,0 MOV D,A ; D = mask MOV A,B ORA A JZ BIT3 MOV A,M CPI '-' JNZ BIT3 INX H DCR B MOV C,D ; active low: invert this bitBIT3: MOV A,D ORA A ; clear carry RET; DE = a hex number at HL (B characters left), ending at a space, - or the; end, with an optional H. Carry set if there are no digits or a bad one.HEXNUM: LXI D,0 MVI C,0 ; digits seenHEX1: MOV A,B ORA A JZ HEXEND MOV A,M CPI ' ' JZ HEXEND CPI '-' JZ HEXEND CPI 'H' JNZ HEX2 INX H DCR B JMP HEXENDHEX2: SUI '0' RC CPI 10 JC HEX3 SUI 'A'-'0'-10 ; A-F CPI 10 RC CPI 16 CMC RCHEX3: PUSH H XCHG DAD H DAD H DAD H DAD H MOV E,A MVI D,0 DAD D XCHG POP H INX H DCR B INR C JMP HEX1HEXEND: MOV A,C ORA A RNZ ; (carry is clear) STC RETLOADAT: DW 3000HCOUNT: DW 0FFFFHGOADR: DW 0ENDADR: DW 0DMAPTR: DW 0SUM: DW 0HAVEF: DB 0HAVEG: DB 0LAST: DB 0TOPPG: DB 0DTPORT: DB 40HCTPORT: DB 43HRDMASK: DB 04H ; status bit 2RDPOL: DB 0USAGE: DB 'CLOAD [A=addr] [N=count] [G=addr] [D=port] [C=port] [R=bit[-]] [FILE]',13,10 DB 'Loads a tape from the MIO cassette interface. All numbers are in hex.',13,10 DB ' A= load address (3000) N= number of bytes (until the tape is quiet)',13,10 DB ' G= run from here afterwards FILE also save it to this disk file',13,10 DB ' D= data port (40) C= control/status port (43) R= ready bit (2)',13,10,'$'NOFIT: DB 'That address is not between CLOAD and the BDOS.',13,10,'$'HELLO1: DB 'CLOAD: loading to $'HELLO2: DB 'H. Press Play on the tape; any key stops.',13,10,'$'STOPPED:DB 'Stopped before the data on the tape was found.',13,10,'$'DONE1: DB 'Loaded $'DONE2: DB 'H bytes at $'DONE3: DB 'H-$'DONE4: DB 'H, checksum $'DONE5: DB 'H.',13,10,'$'ENDCNT: DB 'That is all the bytes asked for.',13,10,'$'ENDQ: DB 'The tape went quiet.',13,10,'$'ENDTOP: DB 'Memory is full, so the tape may hold more.',13,10,'$'ENDKEY: DB 'Stopped by a key, so the tape may hold more.',13,10,'$'NOFILE: DB 'The file could not be saved: no directory space.',13,10,'$'DISKFUL:DB 'The file could not be saved: the disk is full.',13,10,'$'WROTE: DB 'Saved to disk.',13,10,'$'FCB: DS 36 DS 64STACK:PROGEND: END