; MIO.ASM - IMSAI MIO serial console driver for CP/M 2.2 ; # Created by S.E. Craswell ; ; The MIO's jumpers decide where everything is. The four functions can sit in ; any order within the board's group of four ports, and the control port's ; input bits are wired by hand in the Input Jumper Area to any of TRDY, RRDY, ; their complements, PE, OE, FE or SIOS. The defaults suit the IMSAI test ; arrangement: serial data at 42H, status at 43H, status bit 0 TRDY and bit 1 ; RRDY, both high when ready. MIOTEST shows how a board is really wired. ; ; Run MIO once after booting CP/M. It installs a small resident driver and ; moves the CP/M console (BIOS CONST, CONIN and CONOUT) to the MIO's serial ; connector, so the terminal must be plugged in there. First it checks that ; the transmitter says ready and that nothing seems to be waiting; if not, it ; says so on the old console and changes nothing. ; ; MIO chooses 6100H when it is free; with a large CP/M it uses ; E800H above the Disk Jockey, or one page below CP/M when ; only 56K RAM is fitted (and lowers the TPA through 0006) ; MIO 80 installs at 8000H (also accepts MIO 8000 or MIO 8000H) ; MIO S=43 D=42 status and data ports (hex) ; MIO T=0 R=1 the status bits for transmitter ready and receiver ready; ; MIO T=7- a trailing - means the bit is low when ready ; ; The options can be combined in any order: MIO 80 S=13 D=12 T=7- R=6- ; MIOSIO EQU 42H ; default serial data port MIOCTL EQU 43H ; default status port RESBASE EQU 0DE00H ; MAKE.PY assembles at two pages for relocation RESLEN EQU RESEND-RESBASE DEFPAGE EQU 61H HIPAGE EQU 0E8H BDOS EQU 5 CONOUT EQU 2 PRINT EQU 9 ORG 100H START: LHLD 1 ; page-zero WBOOT target = BIOS+3 INX H INX H INX H ; HL -> JMP CONST (BIOS+6) SHLD TABLE INX H MOV E,M INX H MOV D,M XCHG ; inspect the current CONST target: MOV A,M ; this driver's starts IN st / XRI p / ANI m CPI 0DBH JNZ NOTYET INX H INX H MOV A,M CPI 0EEH JNZ NOTYET INX H INX H MOV A,M CPI 0E6H JZ ALREADY NOTYET: LDA 2 ; the CCP begins 1600H below the BIOS SUI 16H MOV B,A LDA 7 ; current top of TPA (normally the BDOS) CMP B JC TOPOK MOV A,B TOPOK: STA TOPPG CALL PARSE LXI D,USAGE JC SAY LDA PAGE LXI H,ASMPAGE SUB M STA DELTA ; Clear the control register first, as IMSAI's own MIO test programs do ; (XRA A / OUT CNT): PIO port 1, cassette off, SIOS showing any error. LDA STPORT STA CTLCLR+1 XRA A CTLCLR: OUT 0 ; Report the ports and what the status port reads, on the old console. LXI D,SHOW1 CALL PSTR LDA STPORT CALL PHEX LXI D,SHOW2 CALL PSTR CALL RDSTAT CALL PHEX LXI D,SHOW3 CALL PSTR LDA DTPORT CALL PHEX LXI D,SHOW4 CALL PSTR ; The transmitter is idle now, so it should say ready at once; allow 8192 ; tries (about 0.6 s at 2 MHz) before deciding the bit is wrong. LXI B,2000H TXWAIT: CALL TXRDY JNZ TXOK DCX B MOV A,B ORA C JNZ TXWAIT LXI D,NOTX JMP SAY TXOK: ; Nothing should be waiting. Read the data port once to clear any old byte; ; if the receiver still says ready, the bit is wrong (an empty bus reads FFH). CALL RXRDY JZ RXOK LDA DTPORT STA RXCLR+1 RXCLR: IN 0 CALL RXRDY LXI D,RXSTUCK JNZ SAY RXOK: LXI D,CHECK1 CALL PSTR LDA PAGE CALL PHEX LXI D,CHECK2 CALL PSTR LDA PAGE ; a page below the CCP is guarded through 0006 MOV B,A LDA TOPPG CMP B JC ABOVE JZ ABOVE MVI A,1 STA GUARDED JMP PLACED ABOVE: LDA 2 ; above CP/M: leave its BIOS and buffers alone ADI 0DH MOV B,A LDA PAGE CMP B LXI D,INCPM JC SAY LXI D,CLASH CPI 0FFH JZ SAY CPI 0E0H JC PLACED CPI 0E8H JC SAY ; Disk Jockey E000-E7FF CPI 0F8H JC PLACED CPI 0FCH JC SAY ; optional Poly VTI F800-FBFF PLACED: LDA PAGE MOV H,A MVI L,0 SHLD BASE LXI B,100H CALL RAMTST LXI D,NORAM JNZ SAY LHLD RELTAB ; MAKE.PY replaces FFFF with relocation entries MOV A,H ANA L INR A JNZ COPYIT LDA DELTA ORA A LXI D,NOREL JNZ SAY COPYIT: LHLD BASE XCHG LXI H,RESIMG LXI B,RESLEN COPY: MOV A,M STAX D INX H INX D DCX B MOV A,B ORA C JNZ COPY LXI D,RELTAB RELOOP: XCHG MOV E,M INX H MOV D,M INX H MOV A,D ANA E INR A JZ PATCH PUSH H LHLD BASE DAD D LDA DELTA ADD M MOV M,A POP D JMP RELOOP ; Put the chosen ports, bits and polarities into the resident code. PATCH: LXI H,PTAB PAT1: MOV E,M INX H MOV D,M INX H MOV A,D ANA E INR A JZ HOOKUP MOV C,M ; which setting INX H PUSH H MVI B,0 LXI H,CONFIG DAD B MOV A,M LHLD BASE DAD D MOV M,A POP H JMP PAT1 HOOKUP: LDA GUARDED ORA A JZ HOOKCB LHLD 6 XCHG ; save the real BDOS entry in the resident code LHLD BASE LXI B,BDOSJ+1-RESBASE DAD B MOV M,E INX H MOV M,D LHLD BASE LXI B,REALBD-RESBASE DAD B MOV M,E INX H MOV M,D LHLD BASE SHLD 6 ; programs now see the driver as the TPA ceiling HOOKCB: LXI D,MOVING ; the last words on the old console CALL PSTR LHLD TABLE LXI D,NEWST-RESBASE CALL HOOKR LXI D,NEWIN-RESBASE CALL HOOKR LXI D,NEWOUT-RESBASE CALL HOOKR LXI D,HELLO1 ; now printed through the MIO itself CALL PSTR LDA PAGE CALL PHEX LXI D,HELLO2 CALL PSTR LDA GUARDED ORA A RZ LXI D,TPAMSG JMP SAY ALREADY: LXI D,AGAIN SAY: MVI C,PRINT JMP BDOS PSTR: MVI C,PRINT JMP BDOS ; Print A as two hexadecimal digits. PHEX: PUSH PSW RRC RRC RRC RRC CALL PNIB POP PSW PNIB: ANI 0FH ADI 90H DAA ACI 40H DAA MOV E,A MVI C,CONOUT JMP BDOS ; A = the status port. RDSTAT: LDA STPORT STA RDS1+1 RDS1: IN 0 RET ; NZ if the transmitter says ready (with the configured bit and polarity). TXRDY: CALL RDSTAT LXI H,TXPOL XRA M LXI H,TXMASK ANA M RET ; NZ if the receiver says a byte is waiting. RXRDY: CALL RDSTAT LXI H,RXPOL XRA M LXI H,RXMASK ANA M RET ; Reads the command tail: a page number and the S=, D=, T= and R= settings, ; in any order. Carry set if something in it doesn't make sense. PARSE: XRA A STA GOTPG LXI H,80H MOV B,M ; B = characters left INX H PNEXT: MOV A,B ORA A JZ PEND MOV A,M CPI ' ' JNZ PTOKEN INX H DCR B JMP PNEXT PTOKEN: MOV C,A ; a letter followed by = is a setting MOV A,B CPI 2 JC PNUM INX H MOV A,M DCX H CPI '=' JNZ PNUM INX H INX H DCR B DCR B MOV A,C CPI 'S' JZ OPTS CPI 'D' JZ OPTD CPI 'T' JZ OPTT CPI 'R' JZ OPTR STC RET OPTS: CALL PORTNUM RC STA STPORT JMP PNEXT OPTD: CALL PORTNUM RC STA DTPORT JMP PNEXT OPTT: CALL BITNUM RC STA TXMASK MOV A,C STA TXPOL JMP PNEXT OPTR: CALL BITNUM RC STA RXMASK MOV A,C STA RXPOL JMP PNEXT ; A page: "61", "6100" or "6100H". PNUM: CALL HEXNUM RC MOV A,D ORA A MOV A,E JZ PNUM1 ; up to FF: a page ORA A STC RNZ ; an address must end in 00H MOV A,D PNUM1: STA PAGE MVI A,1 STA GOTPG JMP PNEXT PEND: LDA GOTPG ORA A RNZ ; (carry is clear) LDA 2 ; no page given: choose one ADI 0DH CPI DEFPAGE+1 MVI A,DEFPAGE JC PEND1 LXI H,HIPAGE*256 LXI B,100H CALL RAMTST MVI A,HIPAGE JZ PEND1 LDA TOPPG DCR A PEND1: STA PAGE ORA A 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,1 BIT1: DCR E JM BIT2 RLC JMP BIT1 BIT2: 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 bit BIT3: 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 seen HEX1: 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 HEXEND HEX2: SUI '0' RC CPI 10 JC HEX3 SUI 'A'-'0'-10 ; A-F CPI 10 RC CPI 16 CMC RC HEX3: 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 HEX1 HEXEND: MOV A,C ORA A RNZ ; (carry is clear) STC RET ; Set one BIOS jump target. HL enters on a JMP opcode and leaves on the next. HOOKR: PUSH H LHLD BASE DAD D XCHG POP H INX H MOV M,E INX H MOV M,D INX H RET ; Verify BC bytes at HL are writable, restoring every byte immediately. RAMTST: MOV A,M CMA MOV M,A CMP M CMA MOV M,A RNZ INX H DCX B MOV A,B ORA C JNZ RAMTST RET TABLE: DW 0 BASE: DW 0 PAGE: DB 0 GOTPG: DB 0 DELTA: DB 0 TOPPG: DB 0 GUARDED:DB 0 ASMPAGE:DB HIGH RESBASE ; The settings, in the order PTAB numbers them. CONFIG: STPORT: DB MIOCTL ; 0 status port DTPORT: DB MIOSIO ; 1 data port TXMASK: DB 01H ; 2 transmitter-ready bit TXPOL: DB 0 ; 3 ... its mask if low when ready RXMASK: DB 02H ; 4 receiver-ready bit RXPOL: DB 0 ; 5 ... its mask if low when ready ; Where each setting goes in the resident code: offset from RESBASE, setting. PTAB: DW PS1+1-RESBASE DB 0 DW PS2+1-RESBASE DB 0 DW PS3+1-RESBASE DB 0 DW PD1+1-RESBASE DB 1 DW PD2+1-RESBASE DB 1 DW PTM1+1-RESBASE DB 2 DW PTP1+1-RESBASE DB 3 DW PRM1+1-RESBASE DB 4 DW PRM2+1-RESBASE DB 4 DW PRP1+1-RESBASE DB 5 DW PRP2+1-RESBASE DB 5 DW 0FFFFH SHOW1: DB 'MIO: status port $' SHOW2: DB 'H reads $' SHOW3: DB 'H, data port $' SHOW4: DB 'H.',13,10,'$' NOTX: DB 'The MIO transmitter never says ready on that status bit, so',13,10 DB 'the console has not been moved. Check the board jumpers with',13,10 DB 'MIOTEST, then give MIO the right T= (and R=, S=, D=).',13,10,'$' RXSTUCK:DB 'The receiver-ready status bit stays on with nothing typed, so',13,10 DB 'the console has not been moved. Check the board jumpers with',13,10 DB 'MIOTEST, then give MIO the right R= (and S=, D=).',13,10,'$' CHECK1: DB 'MIO: checking for RAM at $' CHECK2: DB '00H...',13,10,'$' MOVING: DB 'Moving the console to the MIO serial connector.',13,10,'$' INCPM: DB 'That is inside the CP/M BIOS and its buffers: pick a higher page.',13,10,'$' CLASH: DB 'That overlaps the Disk Jockey or optional Poly VTI.',13,10,'$' NORAM: DB 'No RAM there for the driver: MIO not installed.',13,10,'$' NOREL: DB 'This MIO.COM has no relocation table: build it with MAKE.PY.',13,10,'$' USAGE: DB 'Usage: MIO [page] [S=status port] [D=data port] [T=bit[-]] [R=bit[-]]',13,10 DB 'e.g. MIO 80 S=43 D=42 T=0 R=1 (a - means the bit is low when ready)',13,10,'$' HELLO1: DB 'IMSAI MIO serial console installed at $' HELLO2: DB '00H.',13,10,'$' TPAMSG: DB 'The TPA now ends there so CP/M programs leave the driver intact.',13,10,'$' AGAIN: DB 'The IMSAI MIO console driver is already installed.',13,10,'$' ; ---------------------------------------------------------------- resident driver RESIMG: .PHASE RESBASE BDOSJ: JMP 0 ; filled with the real BDOS when below CP/M ; CP/M BIOS CONST: return FFH if the UART has a received byte, otherwise zero. ; The port numbers, bits and polarities are filled in when installed. NEWST: PS1: IN MIOCTL PRP1: XRI 0 PRM1: ANI 2 RZ MVI A,0FFH RET ; CP/M BIOS CONIN: wait for and return one MIO UART byte. NEWIN: CALL GUARD NEWIN1: PS2: IN MIOCTL PRP2: XRI 0 PRM2: ANI 2 JZ NEWIN1 PD1: IN MIOSIO RET ; CP/M BIOS CONOUT: wait for the transmitter and send the character in C. NEWOUT: PUSH PSW CALL GUARD NEWOU1: PS3: IN MIOCTL PTP1: XRI 0 PTM1: ANI 1 JZ NEWOU1 MOV A,C PD2: OUT MIOSIO POP PSW RET ; Warm boot restores 0006 (and LPT, if below CP/M too, its own entry). ; Anything there above this driver is the next thing up, so jump to it and put ; the guard back. Lower values (a program such as DDT deliberately moving the ; TPA's top down) are left alone. GUARD: PUSH D PUSH H LHLD 6 XCHG ; DE = 0006 LXI H,BDOSJ ; HL = this driver MOV A,L SUB E MOV A,H SBB D JNC GDONE ; 0006 is at or below the driver XCHG SHLD BDOSJ+1 XCHG SHLD 6 GDONE: POP H POP D RET REALBD: DW 0 RESEND: .DEPHASE ; MAKE.PY replaces this terminator with resident-address relocation offsets. RELTAB: DW 0FFFFH END