; VTI.ASM - PolyMorphic VTI console driver for CP/M 2.2 ; on an IMSAI 8080 with a Morrow Disk Jockey 2D at E000 ; # Created by S.E. Craswell ; Run VTI once after booting CP/M. It copies a small resident driver (398 ; bytes) into free RAM and points the BIOS console entries at it: ; ; CONOUT writes to the VTI screen as well as the serial terminal ; CONST reports a key waiting on either the VTI keyboard or the serial port ; CONIN reads from whichever has a key ; ; VTI installs the driver at 6100H, or at E800H above the Disk ; Jockey if CP/M reaches 6100H (a 56K CP/M on a 64K IMSAI), or ; with no RAM there (56K fitted) just below the CCP, where it ; lowers the BDOS address at 0006 so programs leave it alone ; (and lowers it again after each warm boot) ; VTI 80 installs it at 8000H (any page: VTI C0, VTI C000H, ...) ; VTIRAM EQU 0F800H ; VTI display memory, 1K = 64 x 16 VTIEND EQU VTIRAM+400H VTIKEY EQU 0F8H ; keyboard data port (reading clears the latch) VTISTA EQU 0F9H ; keyboard status: bit 0 low = key waiting RESBASE EQU 0DE00H ; where the resident code is assembled (MAKE.PY varies it) RESLEN EQU RESEND-RESBASE DEFPAGE EQU 61H ; default page: 6100H HIPAGE EQU 0E8H ; default for a big CP/M: E800H, above the Disk Jockey COLS EQU 64 ROWS EQU 16 BLANK EQU 0A0H ; a space (bit 7 set selects characters) CURSOR EQU 00H ; graphics code with all six blocks lit BDOS EQU 5 CONOUT EQU 2 ; BDOS console output PRINT EQU 9 ; BDOS print string ORG 100H START: LHLD 1 ; HL = BIOS warm boot entry (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 ; HL = where CONST goes now MOV A,M ; this driver's CONST starts IN VTISTA / RAR CPI 0DBH JNZ NOTYET INX H MOV A,M CPI VTISTA JNZ NOTYET INX H MOV A,M CPI 1FH JZ ALREADY NOTYET: LDA 2 ; BIOS page SUI 16H ; the CCP starts 1600H below the BIOS MOV B,A LDA 7 ; the top of the TPA (the BDOS, or something below it) CMP B JC TOPOK MOV A,B TOPOK: STA TOPPG ; the driver can go anywhere below this page CALL GETPG ; A = the page asked for LXI D,USAGE JC SAY STA PAGE LXI H,ASMPAGE SUB M STA DELTA LXI D,CHECK1 ; say what is happening before touching anything CALL PSTR LDA PAGE CALL PHEX LXI D,CHECK2 CALL PSTR LDA PAGE ; all below the CCP: in the TPA, guarded through 0006 INR A ; its last page MOV B,A LDA TOPPG CMP B JZ ABOVE JC ABOVE MVI A,1 STA GUARDED JMP PLACED ABOVE: LDA 2 ; BIOS page (from the warm boot jump at 0000) ADI 0DH ; the BIOS and its buffers end below BIOS+0D00H MOV B,A LDA PAGE CMP B LXI D,INCPM JC SAY LXI D,CLASH CPI 0FFH JZ SAY INR A ; the driver's lst page CPI 0E0H JC PLACED ; all below the DJ thng CPI 0F8H JNC SAY ; reaches the VTI DCR A CPI 0E8H JC SAY ; overlaps summut PLACED: LDA PAGE MOV H,A MVI L,0 SHLD BASE LXI B,RESLEN ; chk ram CALL RAMTST LXI D,NORAM JNZ SAY LXI H,VTIRAM ; chk vti mem? LXI B,VTIEND-VTIRAM CALL RAMTST LXI D,NOVTI JNZ SAY LHLD RELTAB ; run wherever unless tbl MOV A,H ANA L INR A JNZ COPYIT LDA DELTA ORA A LXI D,NOREL JNZ SAY COPYIT: LHLD BASE ; blit code 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 ; add offset to byte RELOOP: XCHG ; HL in2 table MOV E,M INX H MOV D,M INX H MOV A,D ANA E INR A ; FFFF ends the table JZ HOOKUP PUSH H LHLD BASE DAD D LDA DELTA ADD M MOV M,A POP D ; DE -> table JMP RELOOP HOOKUP: LDA GUARDED ; in the TPA: 0006 -> the driver's JMP BDOS ORA A JZ HOOKCB LHLD 6 XCHG ; DE = the BDOS entry 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 ; BDOSJ is the first thing in the driver SHLD 6 HOOKCB: LHLD TABLE ; hook cpm calls in2 CONST, CONIN and CONOUT into here LXI D,NEWST-RESBASE LXI B,OLDST+1-RESBASE CALL HOOKR LXI D,NEWIN-RESBASE LXI B,OLDIN+1-RESBASE CALL HOOKR LXI D,NEWOUT-RESBASE LXI B,OLDOUT+1-RESBASE CALL HOOKR LXI D,VCLEAR-RESBASE ; CLS LHLD BASE DAD D LXI D,CLEARED PUSH D PCHL CLEARED: LXI H,VTIRAM MVI M,CURSOR LXI D,HELLO1 ; pass 2 driver CALL PSTR LDA PAGE CALL PHEX LXI D,HELLO2 CALL PSTR LDA GUARDED ORA A RZ ; back to the CCP LXI D,TPAMSG JMP SAY ALREADY: LXI D,AGAIN SAY: MVI C,PRINT JMP BDOS ; BDOS returns to the CCP PSTR: MVI C,PRINT JMP BDOS ; PHEX: prints A as two hex 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 ; GETPG: the page from the command tail ("61", "6100", "6100H"), or DEFPAGE ; (HIPAGE when the CP/M BIOS and its buffers reach DEFPAGE, or just below the ; CCP if there is no RAM at HIPAGE) ; if there is none. Carry set if it isn't a hex page. GETPG: LXI H,80H MOV B,M ; length of the tail INX H GP1: MOV A,B ORA A JZ GPDEF MOV A,M CPI ' ' JNZ GP2 INX H DCR B JMP GP1 GPDEF: LDA 2 ; BIOS page ADI 0DH ; where its buffers end, as at NOTYET CPI DEFPAGE+1 MVI A,DEFPAGE JC GPRET ; all below 6100H LXI H,HIPAGE*256 ; RAM above the Disk Jockey (64K fitted)? LXI B,200H CALL RAMTST MVI A,HIPAGE JZ GPRET LDA TOPPG ; no: the two pages just below the CCP SUI 2 GPRET: ORA A RET GP2: LXI D,0 ; DE = the number GP3: MOV A,B ORA A JZ GPEND MOV A,M CPI 'H' JZ GPEND CPI ' ' JZ GPEND SUI '0' RC CPI 10 JC GP4 SUI 'A'-'0'-10 ; A-F CPI 10 RC CPI 16 CMC RC GP4: 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 JMP GP3 GPEND: MOV A,D ORA A MOV A,E RZ ; up to FF: a page ORA A ; an address tht starts a page STC RNZ MOV A,D ORA A RET ; HOOKR: HOOK with DE and BC given as offsets into the resident code. HOOKR: PUSH H LHLD BASE DAD D XCHG ; DE = the new routine LHLD BASE DAD B MOV B,H MOV C,L ; BC = its OLDxx jump operand POP H ; HOOK: HL -> a BIOS "JMP target". Stores the old target at (BC), ; jmp to DE instead, and leaves HL ont the next jump. HOOK: INX H MOV A,M STAX B MOV M,E INX H INX B MOV A,M STAX B MOV M,D INX H RET ; RAMTST: chcks BC bytes from HL are RAM by writing each complement. RAMTST: MOV A,M CMA MOV M,A CMP M CMA ; (CMA and MOV leave the flags alone) MOV M,A RNZ ; not in her dip sht INX H DCX B MOV A,B ORA C JNZ RAMTST RET TABLE: DW 0 BASE: DW 0 PAGE: DB 0 DELTA: DB 0 TOPPG: DB 0 GUARDED:DB 0 ASMPAGE:DB HIGH RESBASE ; the page put resident code was assembled for CHECK1: DB 'VTI: checking for RAM at $' CHECK2: DB '00H and the VTI at F800H...',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 (E000-E7FF) or the VTI (F800-FBFF).',13,10,'$' NORAM: DB 'No RAM there for the driver: VTI not installed.',13,10,'$' NOVTI: DB 'No VTI display memory at F800H: VTI not installed.',13,10,'$' NOREL: DB 'This VTI.COM has no relocation table: build it with MAKE.PY.',13,10,'$' USAGE: DB 'Usage: VTI [page], e.g. VTI 80 or VTI 8000H (default 6100H, E800H or below CP/M).',13,10,'$' HELLO1: DB 'PolyMorphic VTI console driver installed at $' HELLO2: DB '00H.',13,10,'$' TPAMSG: DB 'That is below CP/M, so the TPA now ends there.',13,10,'$' AGAIN: DB 'The VTI driver is already installed.',13,10,'$' ; ---------------------------------------------------------------- resident bit RESIMG: .PHASE RESBASE ; In the TPA, 0006 points here so programs see a TPA ending below the driver. BDOSJ: JMP 0 ; the BDOS (filled in when installed) ; Console status: A = 0FFH if a key is waiting on either keyboard. NEWST: IN VTISTA RAR JNC KEYWAIT OLDST: JMP 0 ; serial CONST (filled in when installed) KEYWAIT: MVI A,0FFH RET ; Console input: wait for a key from either keyboard. NEWIN: CALL GUARD NEWIN1: IN VTISTA RAR JNC VTIGET CALL OLDST ORA A JZ NEWIN1 OLDIN: JMP 0 ; serial CONIN (filled in when installed) VTIGET: IN VTIKEY ANI 7FH CPI 7FH ; the Poly keyboard sends DEL for backspace RNZ MVI A,08H RET ; Console output: character -> C, to the VTI -> serial port. NEWOUT: PUSH B PUSH D PUSH H CALL GUARD CALL VOUT POP H POP D POP B OLDOUT: JMP 0 ; serial CONOUT ; GUARD: a warm boot points 0006 back at the BDOS; point it at BDOSJ again. ; (A program that has moved 0006 itself, like DDT, is left alone.) GUARD: PUSH D PUSH H LHLD 6 XCHG LHLD REALBD ; 0 unless the driver is in the TPA MOV A,L CMP E JNZ GDONE MOV A,H CMP D JNZ GDONE LXI H,BDOSJ SHLD 6 GDONE: POP H POP D RET ; VOUT: push the chr in C on the VTI screen. VOUT: LHLD CURPOS LDA UNDER MOV M,A ; stp cursor LDA ESCST ORA A JNZ ESCSEQ MOV A,C ANI 7FH CPI ' ' JC CONTROL ORI 80H ; characters have bit 7 set MOV M,A INX H MOV A,L ANI COLS-1 JNZ VDONE ; still on the same line CALL CHKEND ; wrapped: ran off the bottom JMP VDONE CONTROL: CPI 0DH JZ CRET CPI 0AH JZ LFEED CPI 08H JZ BACK CPI 09H JZ TAB CPI 0CH JZ CLEARS CPI 1AH JZ CLEARS CPI 1BH JNZ VDONE ; forgt evrythng else MVI A,1 STA ESCST JMP VDONE CRET: MOV A,L ANI 0FFH-(COLS-1) ; back to col 0 MOV L,A JMP VDONE LFEED: LXI D,COLS DAD D CALL CHKEND JMP VDONE BACK: MOV A,L ANI COLS-1 JZ VDONE ; already at col 0 DCX H JMP VDONE TAB: MOV A,L ANI COLS-1 CPI COLS-8 JNC VDONE ; no tab stops after col 56 MOV A,L ORI 07H INR A MOV L,A JMP VDONE CLEARS: CALL VCLEAR HOME: LXI H,VTIRAM JMP VDONE ; Escape sequences: ESCST 1 = after ESC, 2 = waiting for row, 3 = for column. ESCSEQ: CPI 1 JNZ ESC2 XRA A STA ESCST MOV A,C CPI '=' JZ ESCADDR CPI 'Y' JZ ESCADDR CPI 'E' JZ CLEARS CPI 'H' JZ HOME CPI 'J' JZ CLREOS CPI 'K' JZ CLREOL JMP VDONE ESCADDR: MVI A,2 STA ESCST JMP VDONE ESC2: CPI 2 JNZ ESC3 MOV A,C SUI ' ' STA ESCROW MVI A,3 STA ESCST JMP VDONE ESC3: XRA A STA ESCST LDA ESCROW CPI ROWS JNC VDONE ; not on the screen MOV E,A MOV A,C SUI ' ' CPI COLS JNC VDONE MOV L,E ; HL = row * 64 MVI H,0 DAD H DAD H DAD H DAD H DAD H DAD H MOV E,A ; + column MVI D,0 DAD D LXI D,VTIRAM DAD D JMP VDONE CLREOL: PUSH H CLR1: MVI M,BLANK INX H MOV A,L ANI COLS-1 JNZ CLR1 POP H JMP VDONE CLREOS: PUSH H CLR2: MVI M,BLANK INX H MOV A,H CPI HIGH VTIEND JNZ CLR2 POP H ; flag for position VDONE: SHLD CURPOS MOV A,M STA UNDER MVI M,CURSOR RET ; CHKEND: if HL has gone past the last line scrl and move up CHKEND: MOV A,H CPI HIGH VTIEND RC ; still on the screen PUSH H LXI H,VTIRAM+COLS LXI D,VTIRAM LXI B,(ROWS-1)*COLS SCROLL: MOV A,M STAX D INX H INX D DCX B MOV A,B ORA C JNZ SCROLL XCHG ; HL is last line MVI B,COLS SCRCLR: MVI M,BLANK INX H DCR B JNZ SCRCLR POP H LXI D,-COLS DAD D RET ; VCLEAR: CLS VCLEAR: LXI H,VTIRAM VCL1: MVI M,BLANK INX H MOV A,H CPI HIGH VTIEND JNZ VCL1 LXI H,VTIRAM SHLD CURPOS MVI A,BLANK STA UNDER RET CURPOS: DW VTIRAM UNDER: DB BLANK ESCST: DB 0 ESCROW: DB 0 REALBD: DW 0 ; the BDOS entry 0006 held, when in the TPA RESEND: .DEPHASE ; Offsets (from RESBASE) of the address high bytes in the resident code, ; ended by 0FFFFH. MAKE.PY fills this in. RELTAB: DW 0FFFFH END