; Drivrutin f|r ABC800 att styra parallel-
; eller seriell-printer.
;
; Prog. made by Jan-]ke Larsson.
;               PR-data Eskilstuna HB
;
; 1986-02-09  ->  1986-02-13
;
; Ny lagring av parametrar, 86-03-30
;
DEVTBA:         EQU 65403   ; Word
RAM:            EQU 65508   ; Byte
BAUD:           EQU 65509   ; Word
BUFF:           EQU 65472   ; 11 bytes
;
;
                ORG 28672
;
; JP f|rst i option-prommet.
;
                JP INIT
;
; L{gg in PR: i enhetslistan.
;
INIT:           LD HL,DEV
                LD BC,DEVTBA
                CALL DEV_F
                JP OKEY
;
DEV_F:          LD A,H
                OR L
                RET Z
                LD E,(HL)
                INC HL
                LD D,(HL)
                INC HL
                PUSH DE
                EX DE,HL
                EXX
                POP HL
                POP BC
                POP DE
                EXX
                LD HL,65526
                ADD HL,SP
                LD SP,HL
                EX DE,HL
                PUSH DE
                PUSH BC
                LD BC,2
                EX (SP),HL
                LDIR
                EX (SP),HL
                LD C,8
                LDIR
                POP HL
                POP DE
                DEC HL
                LD (HL),D
                DEC HL
                LD (HL),E
                EXX
                PUSH DE
                PUSH BC
                EXX
                PUSH HL
                EXX
                POP BC
                JR DEV_F
;
OKEY:           POP DE
                LD B,2
LOOP:           LD HL,65524
                ADD HL,SP
                LD SP,HL
                LD (65500),HL
                LD (HL),0
                DJNZ LOOP
                PUSH DE
                LD HL,30208
;
; St{ll standard parametrar vid RESET.
;
                PUSH BC
                PUSH DE
                PUSH HL
                LD HL,S_PAR
                LD DE,BUFF
                LD BC,11
                LDIR
                POP HL
                POP DE
                POP BC
                RET
;
DEV:            DEFW 0
                DEFM 'PR '
                DEFW DR_PR
;
; Drivrutin f|r PR:
;
DR_PR:          AND A
                JR Z,OPEN
                DEC A
                JR Z,OPEN
                DEC A
                JP Z,CLOSE
                SUB 2
                JP Z,PRINT
                LD A,52
                SCF
                RET
;
OPEN:           LD HL,(65500)       ; Opt.prommets RAM-area.
                LD BC,BUFF
                LD (IX+21),A
                LD (IX+18),L
                LD (IX+19),H
;
; /// 1 ///  typ  (C V)
;
                LD A,(DE)              ; F|rsta tkn. i parameter-str{ng.
                INC DE
                CP 32
                JR NZ,LADDA
                LD A,(HL)
                AND A
                JR Z,FIX
                EX DE,HL
                CALL GET_CH
                JR TYP
FIX:            PUSH DE
                LD L,C
                LD H,B
                INC HL
                LD BC,10
                LDIR
                POP DE
                LD C,86        ; "V"
                LD A,188
                OUT (1),A
                OUT (3),A
                IN A,(1)
                CPL
                AND A
                JR Z,S[TT
                LD C,67        ; "C"
S[TT:           LD A,C
                DEC DE
                LD (DE),A
                INC DE
LADDA:          LD HL,11
                LD BC,(65500)
                ADD HL,BC
                LD (HL),0
TYP:            LD HL,D_TYP
                CALL CHECK
                LD B,255
                PUSH BC
;
; /// 2 ///  paritet.
;
                CALL GET_CH
                LD HL,PARITET
                CALL CHECK
                LD (IX+15),A      ; Paritet (MSEO)
;
; /// 3 ///  nulls efter LF.
;
                CALL GET_CH
                SUB 65
                CP 26
                JP NC,ERR21       ; "[" max.
                ADD A,A
                LD (IX+14),A    ; Nulls.
;
; /// 4 ///  tecken / rad
;
                CALL GET_CH
                CP 49
                JP C,ERR21      ; <1
                CP 56
                JP NC,ERR21     ; >7
                AND 15
                LD HL,LINE-1
                ADD A,L
                LD L,A
                LD A,H
                ADC A,0
                LD H,A
                POP BC
                LD C,(HL)       ; tkn./rad
                PUSH BC
;
; /// 5 ///  perforeringsskipp
;
                CALL GET_CH
                CALL KOLL
                LD (IX+16),A        ; Antal rader vid perforeringsskipp
;
; /// 6 ///  |verf|r.typ / format
;
                CALL GET_CH
                LD HL,FORMAT
                CALL CHECK
;
; /// 7 & 8 ///  rader / sida
;
                CALL GET_CH
                CALL KOLL
                LD C,A
                CALL GET_CH
                CALL KOLL
                PUSH DE
                LD L,C
                LD H,0
                ADD HL,HL
                LD D,H
                LD E,L
                ADD HL,HL
                ADD HL,HL
                ADD HL,DE
                ADD A,L
                JR Z,ERR21
                POP DE
                LD (IX+13),A      ; Rader/sida.
;
                PUSH DE
                LD HL,65528
                ADD HL,DE
                LD A,(HL)
                LD E,(IX+18)
                LD D,(IX+19)
                LD BC,11
                LDIR
                POP DE
                CP 86              ; V-OPT. ?
                JR NZ,DONE
;
; St{ll baud-rate.
;
                CALL GET_CH
                CALL KOLL
                CP 8
                JR NC,ERR21
                LD DE,RAM
                PUSH AF
                OR 48
                LD (DE),A
                POP AF
                DEC A
                ADD A,A
                LD E,A
                LD D,0
                LD HL,DATA
                ADD HL,DE
                LD A,(HL)
                INC HL
                LD H,(HL)
                LD L,A
                LD (BAUD),HL
;
DONE:           POP BC
                LD D,B
                XOR A
                RET
;
; Kolla om parameter ok, j{mf|r med parameter-tabell.
;
CHECK:          LD B,(HL)          ; Antal i tabell.
N[STA:          INC HL
                CP (HL)
                RET Z              ; Parameter okey.
                DJNZ N[STA
;
ERR21:          RST 16             ; Parameterfel.
                DEFB 21
;
; H{mta n{sta parameter fr}n str{ngen.
;
GET_CH:         LD A,(DE)
                INC DE             ; Peka p} n{sta tkn.
                AND 127
                CP 32
                RET NZ
                JR ERR21
;
; Kolla om tkn. i accumulatorn {r >1 <9
;
KOLL:           CP 48
                JR C,ERR21
                CP 58
                JR NC,ERR21
                AND 15
                RET
;
; Kolla om CTRL-C har tryckts, is}fall BREAK.
;
BREAK:          BIT 0,(IY+35)
                RET Z
                XOR A
                JP 51
;
; Baud-rate adjustment.
;
B_RATE:         LD HL,(BAUD)
WAIT:           DEC HL
                LD A,H
                OR L
                JR NZ,WAIT
                RET
;
; Skriv rad till printern.
;
PRINT:          CALL F_2
                LD A,C
                OR B
                RET Z      ; RET om slut p} str{ng
                LD E,(HL)  ; tkn. i E
                INC HL     ; peka p} n{sta tkn.
                DEC BC     ; ett mindre kvar
                CALL UPP
                CALL PAGE
                JR PRINT   ; n{sta tkn.
;
; Kolla om dags f|r perforeringsskipp.
;
PAGE:           CALL F_4
                DEC A
                CP (IX+16)
                RET NC
                LD E,12
                JP UPP
;
; Kolla kontroll tkn. m.m.
;
UPP:            LD A,12
                CP E
                JR NZ,S_K
                CALL F_3
                DEC A
                BIT 0,A
                JR NZ,LF2
                XOR A
                CALL F_5
S_K:            LD A,E
                AND 127
                CP 32
                JR C,AHA
                INC (IX+6)
                JR Z,KOL
                LD A,(IX+6)
                DEC A
                CP (IX+8)
                JR C,AHA
KOL:            CALL F_3
                DEC A
                BIT 1,A
                JR NZ,KOL2
                LD (IX+6),1
                JR AHA
KOL2:           PUSH DE
                LD E,13
                CALL UPP
                LD E,10
                CALL UPP
                CALL PAGE
                POP DE
AHA:            PUSH DE
                CALL S[ND     ; s{nd tkn.
                POP DE
                LD A,E
                AND 127
                CP 13
                JR NZ,BS
                LD (IX+6),0
BS:             CP 8
                JR NZ,LF
                DEC (IX+6)
                JP P,LF
                INC (IX+6)
LF:             CP 10
                RET NZ
                CALL F_4
                AND A
                JR NZ,UPP2
                LD A,(IX+13)
UPP2:           DEC A
                CALL F_5
                LD A,(IX+14)
                AND A
                RET Z
NULL:           PUSH AF
                LD E,0
                CALL UPP
                POP AF
                DEC A
                JR NZ,NULL
                RET
;
LF2:            LD E,10
                CALL UPP
                CALL F_4
                AND A
                JR NZ,LF2
                RET
;
; S{nd tkn. i E till printer,
; via V-24 eller Centronics-snitt.
;
S[ND:           CALL F_2
                CP 86          ; "V" ?
                JR NZ,C_S[ND
V_S[ND:         PUSH BC
                PUSH HL
                CALL PARITY
                LD C,A
                LD B,10
                LD A,(RAM)
                CP 49
                JR NZ,C_CTS
                INC B
C_CTS:          IN A,(58)
                OR 8
                AND 239
                OUT (58),A
CTS:            CALL BREAK
                IN A,(58)
                AND 2
                JR NZ,CTS
LOOP2:          IN A,(58)
                JR C,HOPP
                AND 247
                JR S[ND_V
HOPP:           OR 8
S[ND_V:         OUT (58),A
                CALL B_RATE
                SCF
                RR C
                DJNZ LOOP2
                LD B,10
LOOP3:          CALL B_RATE
                DJNZ LOOP3
                IN A,(58)
                OR 16
                OUT (58),A
                POP HL
                POP BC
                RET
;
C_S[ND:         CALL BREAK
                LD A,188
                OUT (1),A
                OUT (3),A
                IN A,(1)
                AND 112
                JR NZ,C_S[ND
                CALL PARITY
                OUT (0),A
                RET
;
; Kolla paritet.
;
PARITY:         LD A,(IX+15)
                CP 83
                JR NZ,C_M
                LD A,E
                RET
C_M:            CP 77
                JR NZ,C_O
                LD A,128
                OR E
                RET
C_O:            CP 79
                LD A,E
                JR NZ,E_PAR
                AND A
                RET PO
                XOR 128
                RET
E_PAR:          AND A
                RET PE
                XOR 128
                RET
;
; St{ng filen.
;
CLOSE:          CALL F_2
                CP 86      ; "V" ?
                JR Z,F_1
                CALL F_1
                LD A,60
                OUT (1),A
                OUT (3),A
                RET
;
F_1:            LD A,(IX+21)
                XOR 1
                RET NZ
                CALL F_4
                AND A
                LD E,12
                CALL NZ,UPP
                XOR A
                RET
;
F_2:            PUSH IX
                CALL FIXA
                LD A,(IX+0)
                POP IX
                RET
;
F_3:            PUSH IX
                CALL FIXA
                LD A,(IX+5)
                POP IX
                RET
;
F_4:            PUSH IX
                CALL FIXA
                LD A,(IX+11)
                POP IX
                RET
;
F_5:            PUSH IX
                CALL FIXA
                LD (IX+11),A
                POP IX
                RET
;
FIXA:           PUSH HL
                LD L,(IX+18)
                LD H,(IX+19)
                EX (SP),HL
                POP IX
                RET
;
; Data f|r antalet tkn./rad
;
LINE:           DEFB  40
                DEFB  72
                DEFB  80
                DEFB 120
                DEFB 132
                DEFB 158
                DEFB 255
;
; Data f|r typ av |verf|ring.
;
D_TYP:          DEFB 2
                DEFM 'CV'
;
; Data f|r parameter.
;
PARITET:        DEFB 4
                DEFM 'MSEO'
;
; Data f|r |verf|r. format
;
FORMAT:         DEFB 4
                DEFM 'ABCD'
;
; Standard inst{llning av parametrar.
; (exakt 11 tkn.)
;
S_PAR:          DEFM 'VSA30D724  '
;
; Data f|r BAUD-RATE.
;
DATA:           DEFB  18
                DEFB   4
                DEFB 123
                DEFB   1
                DEFB 187
                DEFB   0
                DEFB  92
                DEFB   0
                DEFB  44
                DEFB   0
                DEFB  20
                DEFB   0
                DEFB   8
                DEFB   0
;
                DEFB 7
                DEFB 12
                DEFB 7
                DEFB 7
                DEFB 7
                DEFM 'JL Eskilstuna 1986'
                DEFB 7
                DEFB 7
                DEFB 7
                DEFB 7
                DEFB 7
                DEFB 7
                DEFB 7
                DEFB 7
                DEFB 7
                DEFB 7
;
