CP/M 2.2 CONSOLE DRIVER / IMSAI 8080

MIO.COM

Moves the CP/M console to the IMSAI MIO's serial port, on any jumper setup.

A relocatable console driver for the MIO's UART. It hooks the BIOS CONST, CONIN and CONOUT entries and survives warm boots.

The MIO's jumpers decide where everything is: the four functions can sit in any order within the board's group of ports, and each status bit is wired by hand. MIO takes the ports and status bits on its command line, checks that the transmitter says ready before moving anything, and picks its own page: 6100H under a small CP/M, E800H above the Disk Jockey, or one page below CP/M on a 56K system, guarding the TPA.

Using it

A>MIO                      defaults: status 43H, data 42H, TRDY bit 0, RRDY bit 1
A>MIO S=43 D=42 T=0 R=1    status port, data port, ready bits (hex)
A>MIO T=7- R=6-            a - means the bit is low when ready
A>MIO 80                   install at 8000H

MIO.ASM

680 LINES
; 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 portMIOCTL  EQU     43H             ; default status portRESBASE EQU     0DE00H          ; MAKE.PY assembles at two pages for relocationRESLEN  EQU     RESEND-RESBASEDEFPAGE EQU     61HHIPAGE  EQU     0E8HBDOS    EQU     5CONOUT  EQU     2PRINT   EQU     9        ORG     100HSTART:  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      ALREADYNOTYET: 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,BTOPOK:  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     ACTLCLR: 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,2000HTXWAIT: CALL    TXRDY        JNZ     TXOK        DCX     B        MOV     A,B        ORA     C        JNZ     TXWAIT        LXI     D,NOTX        JMP     SAYTXOK:; 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+1RXCLR:  IN      0        CALL    RXRDY        LXI     D,RXSTUCK        JNZ     SAYRXOK:        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     PLACEDABOVE:  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-FBFFPLACED: 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     SAYCOPYIT: LHLD    BASE        XCHG        LXI     H,RESIMG        LXI     B,RESLENCOPY:   MOV     A,M        STAX    D        INX     H        INX     D        DCX     B        MOV     A,B        ORA     C        JNZ     COPY        LXI     D,RELTABRELOOP: 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,PTABPAT1:   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     PAT1HOOKUP: 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 ceilingHOOKCB: 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     SAYALREADY:        LXI     D,AGAINSAY:    MVI     C,PRINT        JMP     BDOSPSTR:   MVI     C,PRINT        JMP     BDOS; Print A as two hexadecimal digits.PHEX:   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; A = the status port.RDSTAT: LDA     STPORT        STA     RDS1+1RDS1:   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     HPNEXT:  MOV     A,B        ORA     A        JZ      PEND        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      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        RETOPTS:   CALL    PORTNUM        RC        STA     STPORT        JMP     PNEXTOPTD:   CALL    PORTNUM        RC        STA     DTPORT        JMP     PNEXTOPTT:   CALL    BITNUM        RC        STA     TXMASK        MOV     A,C        STA     TXPOL        JMP     PNEXTOPTR:   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,DPNUM1:  STA     PAGE        MVI     A,1        STA     GOTPG        JMP     PNEXTPEND:   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     APEND1:  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,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        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        RETTABLE:  DW      0BASE:   DW      0PAGE:   DB      0GOTPG:  DB      0DELTA:  DB      0TOPPG:  DB      0GUARDED:DB      0ASMPAGE:DB      HIGH RESBASE; The settings, in the order PTAB numbers them.CONFIG:STPORT: DB      MIOCTL          ; 0 status portDTPORT: DB      MIOSIO          ; 1 data portTXMASK: DB      01H             ; 2 transmitter-ready bitTXPOL:  DB      0               ; 3 ... its mask if low when readyRXMASK: DB      02H             ; 4 receiver-ready bitRXPOL:  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      0FFFFHSHOW1:  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 driverRESIMG:        .PHASE  RESBASEBDOSJ:  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      MIOCTLPRP1:   XRI     0PRM1:   ANI     2        RZ        MVI     A,0FFH        RET; CP/M BIOS CONIN: wait for and return one MIO UART byte.NEWIN:  CALL    GUARDNEWIN1:PS2:    IN      MIOCTLPRP2:   XRI     0PRM2:   ANI     2        JZ      NEWIN1PD1:    IN      MIOSIO        RET; CP/M BIOS CONOUT: wait for the transmitter and send the character in C.NEWOUT: PUSH    PSW        CALL    GUARDNEWOU1:PS3:    IN      MIOCTLPTP1:   XRI     0PTM1:   ANI     1        JZ      NEWOU1        MOV     A,CPD2:    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    6GDONE:  POP     H        POP     D        RETREALBD: DW      0RESEND:        .DEPHASE; MAKE.PY replaces this terminator with resident-address relocation offsets.RELTAB: DW      0FFFFH        END