; LPT.ASM - IMSAI MIO parallel printer driver for CP/M ; ; Sends the CP/M list device to a printer on one of the MIO's two parallel ; connectors, so ^P, PIP LST:=FILE and MBASIC's LPRINT and LLIST print there. ; It replaces the BIOS LIST entry, and LISTST as well on CP/M 2.2 (CP/M 1.4 ; has no LISTST). ; ; The MIO has one parallel data port for both connectors: control port bit 7 ; picks connector 2, so LPT writes the control port before every character. ; That also writes 0 to bits 0-6 (cassette off), as IMSAI's own MIO programs ; do when they start. The defaults suit the IMSAI test arrangement: parallel ; data at 41H, control and status at 43H. ; ; LPT connector 1, sending each character without waiting ; LPT P=2 connector 2 ; LPT D=41 C=43 the parallel data port and the control/status port (hex) ; LPT R=4 wait for status bit 4 before each character (R=4- if the ; bit is low when the printer is ready); LISTST reports it ; LPT 80 installs at 8000H (also LPT 8000 or LPT 8000H) ; ; The options can be combined in any order: LPT 80 P=2 R=4 ; ; LPT chooses its page as MIO does, one page higher: 6200H when it is free; ; with a large CP/M, E900H above the Disk Jockey, or one page below CP/M ; (below MIO if that is there) when only 56K RAM is fitted, lowering the TPA ; through 0006. Either driver can be installed first. ; PIODATA EQU 41H ; default parallel data port MIOCTL EQU 43H ; default control/status port RESBASE EQU 0DE00H ; MAKE.PY assembles at two pages for relocation RESLEN EQU RESEND-RESBASE DEFPAGE EQU 62H HIPAGE EQU 0E9H BDOS EQU 5 CONOUT EQU 2 PRINT EQU 9 VERSION EQU 12 ORG 100H START: LHLD 1 ; page-zero WBOOT target = BIOS+3 LXI D,0CH DAD D ; HL -> JMP LIST (BIOS+0FH) SHLD TABLE INX H MOV E,M INX H MOV D,M XCHG ; this driver's LIST entry follows 'LPT' DCX H MOV A,M CPI 'T' JNZ NOTYET DCX H MOV A,M CPI 'P' JNZ NOTYET DCX H MOV A,M CPI 'L' JZ ALREADY NOTYET: MVI C,VERSION ; 0 before CP/M 2.0 CALL BDOS MOV A,L STA CPMVER LDA 2 ; the CCP begins 1600H below the BIOS SUI 16H MOV B,A LDA 7 ; current top of TPA (the BDOS, or MIO) 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 ; Select the connector, then report what the status port reads. LDA CTPORT STA CTL1+1 STA RDS1+1 LDA CTLVAL CTL1: OUT 0 LXI D,SHOW1 CALL PSTR LDA CTLVAL ORA A MVI A,'1' JZ SHOWC MVI A,'2' SHOWC: MOV E,A MVI C,CONOUT CALL BDOS LXI D,SHOW2 CALL PSTR LDA DTPORT CALL PHEX LXI D,SHOW3 CALL PSTR LDA CTPORT CALL PHEX LXI D,SHOW4 CALL PSTR CALL RDSTAT CALL PHEX LXI D,SHOW5 CALL PSTR ; With a ready bit, the printer should say ready; allow about 0.6 s. LDA RDMASK ORA A JZ RDYOK LXI B,2000H RWAIT: CALL PRRDY JNZ RDYOK DCX B MOV A,B ORA C JNZ RWAIT LXI D,NOTRDY JMP SAY RDYOK: 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, connector, bit and polarity 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 ; Below CP/M: point 0006 at the driver, and pass CONOUT through it so the ; guard is put back after every warm boot, before the CCP loads anything. HOOKUP: LDA GUARDED ORA A JZ HOOKL LHLD 6 XCHG ; the BDOS (or MIO) behind the driver LHLD BASE INX H MOV M,E INX H MOV M,D LHLD BASE SHLD 6 LHLD TABLE ; JMP LIST, so JMP CONOUT is 3 bytes before DCX H MOV D,M DCX H MOV E,M ; DE = the current CONOUT PUSH H LHLD BASE LXI B,OLDOUT+1-RESBASE DAD B MOV M,E INX H MOV M,D POP H DCX H ; HL -> JMP CONOUT LXI D,NEWOUT-RESBASE CALL HOOKR HOOKL: LHLD TABLE LXI D,NEWLST-RESBASE CALL HOOKR LDA CPMVER CPI 20H JC DONE ; CP/M 1.4: no LISTST LHLD TABLE LXI D,2DH-0FH ; JMP LISTST is at BIOS+2DH DAD D LXI D,NEWLS-RESBASE CALL HOOKR DONE: LXI D,HELLO1 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: RDS1: IN 0 RET ; NZ if the printer says ready (with the configured bit and polarity). PRRDY: CALL RDSTAT LXI H,RDPOL XRA M LXI H,RDMASK ANA M RET ; Reads the command tail: a page number and the D=, C=, P= 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 'D' JZ OPTD CPI 'C' JZ OPTC CPI 'P' JZ OPTP CPI 'R' JZ OPTR STC RET OPTD: CALL PORTNUM RC STA DTPORT JMP PNEXT OPTC: CALL PORTNUM RC STA CTPORT JMP PNEXT OPTP: CALL PORTNUM ; connector 1 or 2: control bit 7 off or on RC CPI 1 MVI A,0 JZ OPTP1 MOV A,E CPI 2 STC RNZ MVI A,80H OPTP1: STA CTLVAL JMP PNEXT OPTR: CALL BITNUM RC STA RDMASK MOV A,C STA RDPOL JMP PNEXT ; A page: "62", "6200" or "6200H". 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 (also returned in E). 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 CPMVER: DB 0 ASMPAGE:DB HIGH RESBASE ; The settings, in the order PTAB numbers them. CONFIG: CTPORT: DB MIOCTL ; 0 control/status port DTPORT: DB PIODATA ; 1 parallel data port CTLVAL: DB 0 ; 2 control value: 00 connector 1, 80H connector 2 RDMASK: DB 0 ; 3 ready bit (0: don't wait) RDPOL: DB 0 ; 4 ... its mask if low when ready ; Where each setting goes in the resident code: offset from RESBASE, setting. PTAB: DW PC1+1-RESBASE DB 0 DW PC2+1-RESBASE DB 0 DW PS1+1-RESBASE DB 0 DW PS2+1-RESBASE DB 0 DW PD1+1-RESBASE DB 1 DW PV1+1-RESBASE DB 2 DW PV2+1-RESBASE DB 2 DW PRM1+1-RESBASE DB 3 DW PRM2+1-RESBASE DB 3 DW RDYON-RESBASE DB 3 DW PRP1+1-RESBASE DB 4 DW PRP2+1-RESBASE DB 4 DW 0FFFFH SHOW1: DB 'LPT: printer on MIO parallel connector $' SHOW2: DB ', data port $' SHOW3: DB 'H, control port $' SHOW4: DB 'H (reads $' SHOW5: DB 'H).',13,10,'$' NOTRDY: DB 'The printer never says ready on that status bit, so LPT has',13,10 DB 'not been installed. Check the R= bit (and C=, P=) against',13,10 DB 'the board jumpers with MIOTEST, or leave R= out.',13,10,'$' CHECK1: DB 'LPT: checking for RAM at $' CHECK2: DB '00H...',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: LPT not installed.',13,10,'$' NOREL: DB 'This LPT.COM has no relocation table: build it with MAKE.PY.',13,10,'$' USAGE: DB 'Usage: LPT [page] [P=1|2] [D=data port] [C=control port] [R=bit[-]]',13,10 DB 'e.g. LPT P=2 D=41 C=43 R=4 (a - means the bit is low when ready)',13,10,'$' HELLO1: DB 'IMSAI MIO printer driver installed at $' HELLO2: DB '00H: ^P, PIP LST: and LPRINT now print there.',13,10,'$' TPAMSG: DB 'The TPA now ends there so CP/M programs leave the driver intact.',13,10,'$' AGAIN: DB 'The IMSAI MIO printer driver is already installed.',13,10,'$' ; ---------------------------------------------------------------- resident driver RESIMG: .PHASE RESBASE BDOSJ: JMP 0 ; filled with the BDOS (or MIO) when below CP/M DB 'LPT' ; marks the LIST entry as this driver's ; CP/M BIOS LIST: select the connector, wait for the printer if a ready bit is ; set up, and send the character in C. Ports and bits are filled in when ; installed. NEWLST: CALL GUARD PUSH PSW PV1: MVI A,0 PC1: OUT MIOCTL LDA RDYON ORA A JZ LSEND LWAIT: PS1: IN MIOCTL PRP1: XRI 0 PRM1: ANI 0 JZ LWAIT LSEND: MOV A,C PD1: OUT PIODATA POP PSW RET ; CP/M 2.2 BIOS LISTST: FFH if the printer is ready (always, without a ready bit). NEWLS: CALL GUARD LDA RDYON ORA A MVI A,0FFH RZ PV2: MVI A,0 PC2: OUT MIOCTL PS2: IN MIOCTL PRP2: XRI 0 PRM2: ANI 0 RZ MVI A,0FFH RET ; CONOUT passes through here when the driver is below CP/M, to keep it guarded. NEWOUT: CALL GUARD OLDOUT: JMP 0 ; the CONOUT that was there before ; A warm boot puts the BDOS back in 0006 (and MIO, 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 driver back in front. Lower values (a program such as DDT ; moving the TPA's top down) are left alone. GUARD: PUSH PSW 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 POP PSW RET RDYON: DB 0 ; the ready bit's mask, 0 for none RESEND: .DEPHASE ; MAKE.PY replaces this terminator with resident-address relocation offsets. RELTAB: DW 0FFFFH END