; a program to convert the computer into a terminal
;
;    this program will send any ASCII character combination
;    but the sequence escape-control t, which causes this
;    program to return to CP/M
;
cpm     equ     0000h
bdos    equ     0005h
bios    equ     0e800h
pfm     equ     0f000h
const   equ     bios+6
conin   equ     bios+9
conout  equ     bios+12
siost   equ     pfm+18
sioin   equ     pfm+21
sioout  equ     pfm+24
prtmsg  equ     9
eot     equ     24h
lf      equ     0ah
cr      equ     0dh
ctlt    equ     14h
esc     equ     1bh
port    equ     0ch             ; baud rate port
;
        org     100h
;
; prompt for baud rate
;
start   lxi     sp,stack
        lxi     d,baudrt
        mvi     c,prtmsg
        call    bdos
        jmp     b1
;
baudrt  db      'Baud rate? ',eot
;
; get baud rate
;
b1      call    conin
        push    psw
b2      cpi     cr
        jz      b3
        mov     c,a
        call    conout
        call    conin
        jmp     b2
b3      mov     c,a
        call    conout
        mvi     a,lf
        mov     c,a
        call    conout
        pop     psw
        push    psw
        cpi     '1'
        jz      r1
        cpi     '2'
        jz      r2
        cpi     '3'
        jz      r3
        cpi     '4'
        jz      r4
        cpi     '6'
        jz      r6
        cpi     '9'
        jz      r9
        lxi     d,illegl
        mvi     c,prtmsg
        call    bdos
        jmp     b1
illegl  db      'Illegal baud rate',cr,lf,eot
;
; baud rate 1200
;
r1      mvi     a,07h
        out     port
        mvi     a,8
        sta     count
        call    rbegin
        pop     psw
        mov     c,a
        call    conout
        mvi     a,'2'
        mov     c,a
        call    conout
        jmp     rfini
;
; baud rate 2400
;
r2      mvi     a,0ah
        out     port
        mvi     a,4
        sta     count
        call    rbegin
        pop     psw
        mov     c,a
        call    conout
        mvi     a,'4'
        mov     c,a
        call    conout
        jmp     rfini
;
; baud rate 300
;
r3      mvi     a,05h
        out     port
        mvi     a,32
        sta     count
        call    rbegin
        pop     psw
        mov     c,a
        call    conout
        jmp     rfini
;
; baud rate 4800
;
r4      mvi     a,0ch
        out     port
        mvi     a,2
        sta     count
        call    rbegin
        pop     psw
        mov     c,a
        call    conout
        mvi     a,'8'
        mov     c,a
        call    conout
        jmp     rfini
;
; baud rate 600
;
r6      mvi     a,06h
        out     port
        mvi     a,16
        sta     count
        call    rbegin
        pop     psw
        mov     c,a
        call    conout
        jmp     rfini
;
; baud rate 9600
;
r9      mvi     a,0eh
        out     port
        mvi     a,1
        sta     count
        call    rbegin
        pop     psw
        mov     c,a
        call    conout
        mvi     a,'6'
        mov     c,a
        call    conout
        jmp     rfini
;
rbegin  lxi     d,brset
        mvi     c,prtmsg
        call    bdos
        ret
brset   db      'Baud rate set at ',eot
;
rfini   lxi     d,termod
        mvi     c,prtmsg
        call    bdos
        jmp     t1
termod  db      '00',cr,lf
        db      'In TERMINAL mode',cr,lf,eot
;
; start terminal operation
;
t1      call    const           ; check console
        jz      t4              ; go check SIO if no character
        call    conin           ; get character from console
        cpi     esc             ; check for ESC
        jnz     t3              ;   and output it if no ESC
t2      call    const           ; otherwise check console and
        jz      t2              ;   loop until next character is ready
        call    conin           ; get next console character
        cpi     ctlt            ; check for ESC-CTL T
        jz      cpm             ;   and return to CPM if it is
        push    psw             ; otherwise save it
        mvi     a,esc           ; get ESC
        call    sioout          ; and output it to SIO
        call    wait
        pop     psw             ; get saved character
t3      call    sioout          ; output character to SIO
t4      call    siost           ; check SIO
        jz      t1              ; check console if no character
        call    sioin           ; go get character from SIO
        mov     c,a
        call    conout          ;   and output it to the console
        jmp     t1
;
; subroutine to generate delay
;    timing correct for 2.5 Mh Z-80 clock
;
wait    push    psw
        lda     count
w1      call    t120
        dcr     a
        jnz     w1
        pop     psw
        ret
;
; inner delay loop
;
t120    push    psw
        mvi     a,58        ; set to 93 for 4 Mh
t120a   xthl
        xthl
        dcr     a
        jnz     t120a
        pop     psw
        ret
;
count   db      0
        ds      64
stack   ds      1
        end     start


