; RS232 DIAGNOSTIC PROGRAM
;
ISL1 EQU 0038H ;INTERF.SEL.LINE
ISL2 EQU 0030H ;INTERF SEL. TOOL
;
; STEUER PROGRAMMTEIL
;
ORG 100H
;
ENTR:	LXI SP,STAE+256 ;STACK AREA
	CALL CLSC	;CLEAR SCREEN
	LXI D,IN1Z
	CALL ANZ	;PRINT HAED LINES
	MVI C,1		;WAIT FOR INKEY
	CALL 5
	CPI 03H		;IF ^C IS HIT
	JZ 0		;EXIT TO OS 
BUTE:	LXI H,ISTA	;INTF. TABLE
	LXI B,0925H	;ADAP. FOR TEST 
BAAD:	MOV A,M		;GET ADDRESS
	ADI 03H
	STA EING
	ADI 04H
	STA AUSG
	MOV A,C
	DB 0D3H		;OP FOR OUT
AUSG:	DB 0
	MVI A,0		;CLEAR ACCU
	DB 0DBH		;OP FOR IN
EING:	DB 0
	CMP C
	JZ ENTT
	DCR B
	JZ BERR		;BUS TEST ERRO
	INX H
	JMP BAAD
BERR:	CALL CLSC	;CLEAR SCREEN
	LXI D,IN5Z
	CALL ANZ	;DISP. IFSEL TABLE
	MVI C,1
	CALL 5		;RESET SYSTEM
	JMP ENTR
ENTT:	SHLD ISAT
	LXI D,BSPO+6	;BASE PO ADDR.
	LXI B,2		;FOR 2 BYTES
	CALL ERDE
	CALL CLSC	;CLAER SCREEN
	LXI D,IN2Z	;PRINT BASE PO ADDR.
	CALL ANZ	
	MVI C,1
	CALL 5		;WAIT FOR INKEY
ENTS:	CALL CLSC	;CLEAR SCREEN
	LXI D,IN3Z	;CONTI. ON ERRO
	CALL INDIS
	STA COMO	;CONT. ON ERRO YES
	LXI D,IN4Z	;PROGRAM LOOP
	CALL INDIS
	STA DILO	;SAVE LOOP CODE
	CALL CRLF	;CR LF	
	CALL CRLF
ENT4:	JMP DMBT	;START
INDIS:	CALL CRLF	;SET CURS.
	CALL ANZ
	MVI C,1
	CALL 5		;GET CHR FROM KEYB.
	CPI 59H		;CHECK YES OR NO
	RZ
	MVI A,0		;IF NO	
	RET
ERRO:	LDA COMO	;CONTIN. ON ERROR
	RRC
	CNC ERDI	;DISPLAY ERROR COUNTER
	JMP ERST
ERRP:	JMP ZWES	;ERRO DISP ONLY
ERST:	LDA KBY
	CPI 4CH		;TEST IF LOOP
	JZ ERSS
	CALL CLSC	;CLEAR SCREEN
ERSX:	MVI C,2		;CURSOR HOME
	MVI E,1EH
	CALL 5
	LXI D,IN6Z	; TEXT LINE
	CALL ANZ
ERSS:	CALL KEY	;INPUT FROM KEYBOARD
	CPI 0
	JZ ERSU
	CALL CRLF	;CR/LF
	CALL ERDO	;DIPLAY ERRO COUNTER
ERSU:	LDA KBY
	CPI 4CH		;TEST LOOP
	JZ ERLO
	CPI 43H		;CONTINUE
	JZ ERSO
	CPI 51H
	JZ ERSZ		;ABORT PROGRAM
	CPI 44H
	JZ ERSY		;DISPLAY ERROR
	JMP ERSS
ERLO:	LHLD PRCO
	DCX H
	DCX H
	SHLD PRCO	;PROG. COUNTER
	LHLD STCO
	DCX H
	DCX H
	SHLD STCO	;ST COUNTER
	JMP ERRP
ERSO:	MVI A,0
	STA KBY
	JMP ERRP
ERSY:	MVI A,0
	STA KBY
	JMP ERSX
ERSZ:	MVI A,0
	STA DILO
	JMP AEND	
ZWER:	CALL ERDH
	LXI D,DI7S	;OK TO SCREEN
	CALL ANZ
ZWES:	LHLD PRCO	;PROGRAM COUNTER
	CALL ROUN	;START ADDRESS
	SHLD PRCO	;SAVE PROGRAM COUNTER
	PUSH D
	LHLD STCO	;INDIC. COUNTER
	INX H
	INX H
	SHLD STCO	;SAVE NEW COUNT.
	POP H		;GET NEXT ROUTIN
	PCHL
AEND:	LXI H,ROGR	;START PROGR. AT STEP 2
	SHLD PRCO
	LXI H,0		;SET DISP. COUNTER
	SHLD STCO
	LDA DILO
	RRC
	JC AENE		;JUMP IF PROGR. LOOP
	CALL CRLF	;CR LF. NEXT LINE
	LXI D,DI9S
	CALL ANZ	;OPERATOR LEAD THROUGH
AENE:	CALL KEY	;GET KEYB. INPUT
	CPI 0
	JZ AENF		;IF NOT KEY PRESSED
	XRA A
	STA DILO	;CLEAR RUN
	LDA KBY 
	CPI 03H		;TEST ^C FOR TERMINATE
	JZ BENE
	CPI 0DH
	JZ BNDE		;IF CR CONTINUE
	CPI 52H
	JZ ENTS		;RESTART
AENF:	LDA DIMO
	RRC
	JC AENE		;IF DISP.IS ON
	LDA DILO	;IF CONT.RUN IS ON
	RRC
	JC BNDE
	JMP AENE	
BENE:	LXI H,000EH	;CURSOR TO LAST LINE
	SHLD STCO
	CALL CRLF	;DISPLAY ENDE
	LXI D,IN7Z
	CALL ANZ
	LXI SP,0100H	;RESTORE SP
	MVI C,0
	CALL 5
BNDE:	LXI B,0FFFFH	;WAIT FOR SMOOTH SCREEN
	CALL WAITA
	CALL CLSC	;CLEAR SCREEN
	JMP ZWER	;NEXT TEST
ERDO:	CALL ERDH
	LXI D,DI8S
	CALL ANZ	;OUT ERRO COUNT
	RET
ERDI:	CALL ERDO
	XTHL
	INX H
	INX H
	INX H
	XTHL
	RET
ROUN:	MOV E,M		;GET ADDR FROM MEMO.
	INX H
	MOV D,M
	INX H
	RET
ERDH:	LXI D,PONT	;DIS.TEXT
	LHLD STCO
	DAD D		;TEXT FOR COUNTER
	CALL ROUN	;ADDR FROM MEMO
	CALL ANZ
	RET
ERDE:	MOV A,M		;GET LOW BYTE
ERDF:	ANI 0FH		;CUT LOWER BITS
	CPI 09H
	CNC ERDG	;IF GRATER AS 9
	ORI 30H		;SET ZONE BITS
	DCX D
	STAX D
	DCR C		;DEC BYTE COUNTER
	RZ
	MOV A,C		;BYTE COUNTER IN ACCU
	CPI 2
	JNZ ERDK
	INX H
	JMP ERDE	;NEXT BYTE
ERDK:	MOV A,M
	RRC
	RRC
	RRC
	RRC
	JMP ERDF
ERDG:	SUI 09H		;IF DIGI > 9
	JZ ERDM		;IF DIGI = 9
	ORI 40H		;SET ZONE BITS
ERDL:	XTHL
	INX H
	INX H
	XTHL
	RET
ERDM:	MVI A,39H	;IF DIGI =9
	JMP ERDL	
;
;INITIALISATION
;	 
INIT:	MVI C,0DEH	;MODE 1 REG
	MVI B,6
	CALL COOP
	MVI C,77H
	CALL COOP	;MODE 2. REG.
	LXI B,0700H	;CONTR.REG
	CALL COOP
	MVI B,0  	;TRANSM. REG
	CALL COIP
	LXI B,40FFH
	CALL WAITA
	MVI B,2
	CALL COIP	;TEST MOD1 REG
	CPI 0DEH
	JNZ ERRO
	CALL COIP	;TEST MOD2 REG
	CPI 77H
	JNZ ERRO
	MVI B,3		;CONTRL=0
	CALL COIP
	CPI 0
	JNZ ERRO
	JMP ZWER
DMBT:	LXI H,TDAT
	MVI M,8
	MVI E,7		;PATTERN FOR B-TEST
DMBU:	MOV C,M
	MVI B,7
	CALL COOP
	MOV A,C		;NEXT PATTERN
	RRC
	MOV M,A	
	MVI B,3
	CALL COIP	;READ FROM ADAPTER
	CMP C
	JNZ ERRO	;IF WRITE NOT READ 
	DCR E
	JNZ DMBU
	JMP ZWER
;
TDAT:	DB 0
;
CSTT:	MVI E,0FFH	;SET DELAY COUNTER
	LXI B,0100H
CSTX:	XRA A
	DCR E		;DECR. DELAY COUNTER
	RZ
	CALL COIP	;GET STATUS
	ANA D		;IF STATUS IS OK
	JZ CSTX
	RET
;
; LOCAL LOOP BACK
;	
LOBA:	LXI B,07B7H  	;STATUS.REG
	CALL COOP
	MVI D,05H
	CALL CSTT	;GET STATUS AND TEST
	CPI 0		;IF WRONG
	JZ ERRO
	LXI B,04AAH
	CALL COOP
	LXI B,81FFH	;DELAY FOR CONVERT.
	CALL WAITA
	MVI D,02H	;STATUS REFERENS
	CALL CSTT	;GET STATUS
	CPI 0
	JZ ERRO		;IF ERR. STATUS 
	LXI B,0
	CALL COIP
	CPI 0AAH
	JNZ ERRO	;TEST RECEIVE DATA
	MVI D,01H	;CHECK TRANSM.BUFF.EMTY
	CALL CSTT
	CPI 0
	JZ ERRO
	LXI B,0455H	;TRANSM. DATA
	CALL COOP
	LXI B,81FFH	;DELAY FOR CONVERT.
	CALL WAITA
	MVI D,02H	;RHR HAS DATA?
	CALL CSTT
	CPI 0
	JZ ERRO		;IF WRONG STATUS
	LXI B,0
	CALL COIP	;RECEIVE CHAR.
	CPI 55H
	JNZ ERRO
	MVI D,01H	;STATUS REFERZ.
	CALL CSTT	;TEST STATUS	
	CPI 0
	JZ ERRO
	LXI B,0710H
	CALL COOP	;CLEAR ERROR FLAGS AND DISABLE TRANS.	
	JMP ZWER
;
; REMOTE LOOP BACK
;
RELO:	LXI B,0742H	;SET COMMAND REG.
	CALL COOP
	LXI B,0100H
	CALL COIP
	CPI 0C0H   	;TEST STATUS 0
	JNZ ERRO
	LXI B,04FEH
	CALL COOP	;ENABLE TRANSM.	
	LXI B,07F7H	;SET DIAG MODE
	CALL COOP
	LXI B,03FFH	;DELAY FOR RTS
	CALL WAITA
	LXI B,07F4H	;DISABLE TRANSFER
	CALL COOP
	LXI B,30FFH	;DELAY
	CALL WAITA
	MVI D,04H	;IF TRANS EMTY
	CALL CSTT	;WAITE FOR READY
	CPI 0
	JZ ERRO
	LXI B,0100H
	CALL COIP
	ANI 38H
	JNZ ERRO
	JMP ZWER
;
; NORMAL DATA TRANSFER 
; TRASM WITH 600 BAUD
;
DATR:	LXI B,0701H	;ENABLE TRANSM.
	CALL COOP
	MVI D,05H	;WAIT IF TRANS. N READY
	CALL CSTT
	CPI 0
	JZ ERRO
	LXI B,0466H	;LOAD 1. BYTE
	CALL COOP
	LXI B,0737H	;ENABLE RECEIVER
	CALL COOP
	LXI D,0FFFFH	;-COMP VALUE
	CALL BACH	;CHECK
	CPI 0FFH	;TEST IF ERROR
	JZ ERRO
	LXI B,0707H	;STOP TRANSF.
	CALL COOP
	LXI B,0100H	;ERROR FLAGS CHECK
	CALL COIP
	ANI 38H
	JNZ ERRO
	LXI B,0
	CALL COIP
	CPI 66H		;CHECK RECEIVED BYTE
	JNZ ERRO
;
;TRASM.  WITH 50 BAUD
;
	LXI B,06DEH	;CHANGE BAUD RATE
	CALL COOP
	LXI B,0670
	CALL COOP
	LXI B,0701H
	CALL COOP
	MVI D,05H
	CALL CSTT
	CPI 0
	JZ ERRO
	LXI B,0499H
	CALL COOP
	LXI B,0737H
	CALL COOP
	LXI B,27FFH
	CALL WAITA
	LXI B,0707H
	CALL COOP
	LXI B,0FFFFH
	CALL WAITA
	MVI D,02H
	CALL CSTT
	CPI 0
	JZ ERRO
	LXI B,0100H	;ERROR FLAGS CHECK
	CALL COIP
	ANI 38H
	JNZ ERRO
	LXI B,0
	CALL COIP
	CPI 99H
	JNZ ERRO
;
;TRASM. WITH 19200 BAUD
;
	LXI B,06DEH	;CHANGE BAUD RATE
	CALL COOP
	LXI B,067FH
	CALL COOP
	LXI B,0701H
	CALL COOP
	MVI D,05H
	CALL CSTT
	CPI 0
	JZ ERRO
	LXI B,04FFH
	CALL COOP
	LXI B,0737H
	CALL COOP
	LXI B,02FFH
	CALL WAITA
	LXI B,0707H
	CALL COOP
	LXI B,17FFH
	CALL WAITA
	MVI D,02H
	CALL CSTT
	CPI 0
	JZ ERRO
	LXI B,0100H	;ERROR FLAGS CHECK
	CALL COIP
	ANI 38H
	JNZ ERRO
	LXI B,0
	CALL COIP
	CPI 0FFH
	JNZ ERRO
	JMP ZWER
;
BACH:	LXI H,0		;CLEAR HL
BACF:	LXI B,0100H
	CALL COIP	;GET STATUS
	ANI 02H		;TEST STAUS
	JNZ BACG
	INX H		;INCREM COUNTER
	JMP BACF
BACG:	DAD D		;ADD COMPAR VALUE
	MVI A,0
	XRA H
	JNZ BACE
	MVI A,0F0H
	ANA L		;TEST DIFF.
	RZ
BACE:	MVI A,0FFH	;IF ERROR
	RET
;
; TEST ERROR CONDITIONS
;
FOPE:	LXI B,0652H	;1 STOP BIT P:ODD
	CALL COOP
	LXI B,0671H	;SET 75 BAUD
	CALL COOP
	LXI B,0737H	;ENABLE TRAN REC
	CALL COOP
	MVI D,05H	;WAIT FOR STATUS
	CALL CSTT
	CPI 0
	JZ ERRO
	LXI B,0417H	;TRANSM. CHAR.
	CALL COOP
	LXI B,84FFH
	CALL WAITA	;SET DELAY
	MVI D,05H	;WAIT FOR STATUS
	CALL CSTT
	CPI 0
	JZ ERRO
	LXI B,047CH
	CALL COOP	;OUT SECOND CHAR.
	LXI B,84FFH
	CALL WAITA
	LXI B,0100H
	CALL COIP	;TEST STATUS
	ANI 10H		;TEST OVERR. ERROR
	JZ ERRO
	LXI B,0711H
	CALL COOP	;RESET ERROR FLAGS	
	MVI D,05H
	CALL CSTT
	CPI 0
	JZ ERRO
	LXI B,0456H	;TEST UNDERRUN
	CALL COOP	;AND DCD INPUT
	LXI B,0737H	;SET COMMAND
	CALL COOP
	LXI B,07FFH	;DELAY
	CALL WAITA
	LXI B,0704H	;ENABLE RECEIVE
	CALL COOP
	LXI B,0100H
	CALL COIP	;GET STATUS
	CPI 04H		;CHECK NO DATA IN RHR
	JNZ ERRO
	JMP ZWER
;
; INPUT FROM KEYBOARD
;
KEY:	MVI C,0BH       ;INPUT STATUS
	CALL 5
	CPI 0
	RZ		;IF NOT KEY PRESSED
	MVI C,01H
	CALL 5
	STA KBY		;KEY IS PRESSED
	RET
;
;
WAITA: 	DCR C
	JNZ WAITA
	MVI C,0FFH
	DCR B
	JNZ WAITA
	RET
;
; OUTPUT ROUTINE
;
COOP:	PUSH H		;SAVE HL
	LHLD ISAT	;LOAD ADDR.TABLE
	MOV A,M		;GET DEV. ADDRESS
	ADD B		;ADD REG OFFSET
	STA BYAU
	MOV A,C		;GET BYTE FOR OUTP
	DB 0D3H
BYAU:	DB 0
	POP H
	RET
;
; INPUT ROUTINE
;
COIP:	PUSH H		;SAVE HL
	LHLD ISAT	;LOAD ADDR.TABLE
	MOV A,M		;GET DEV. ADDRESS
	ADD B
	STA BYEI
	DB 0DBH
BYEI:	DB 0		;REC BYTE IN REG A
	POP H
	RET
;
; CLEAR SCREEN
;
CLSC:	MVI C,2
	MVI E,1AH
	CALL 5
	RET
;
ANZ:	PUSH H
	PUSH B
	PUSH D
	MVI C,09H
	CALL 0005H
	POP D
	POP B
	POP H
	RET
;
CRLF:	PUSH H
	PUSH D
	PUSH B
	MVI C,02H
	MVI E,0AH
	CALL 0005H
	MVI C,02H
	MVI E,0DH
	CALL 0005H
	POP B
	POP D
	POP H
	RET
;
; TEMPR. MEMORY
;
ISAT:	DW 0
KBY:	DB 0
DIMO:	DB 0
DILO:	DB 0
COMO:	DB 0
STCO:	DW 0
ISTA:	DB 60H,68H,70H,78H,30H,38H,0B0H,0B8H,0C8H,00H
POTI:	DW PONT
PONT:	DW DI1S,DI2S,DI3S,DI4S,DI5S,DI6S,DI7S,DI8S
PRCO:	DW ROGR
ROGR:	DW INIT,LOBA,RELO,DATR,FOPE,AEND
;
IN1Z:	DB'C3273-K211/212/213/801 RS232 INTERFACE DIAGNOSTIC PROGAM',0AH,0DH
        DB'========================================================',0AH,0DH
DB'                     (Version PDR 001 )',0AH,0AH,0AH,0AH,0DH
DB'Remove all other adapters from slot 2-6.',0Dh,0Ah,0AH
DB'Connect the TURNAROUND (TR) adapter to the RS-232 cable.',0ah,0dh,0AH
DB' ',0AH,0DH
DB'TR for PRINTER/MODEM  Pin# (2-3);(4-5);(6-8-20) wired together',0AH,0DH
DB'TR for PLOTTER        Pin# (2-3);(4-8);(5-6)    wired together',0AH,0DH
DB' ',0AH,0DH
DB' ',0AH,0DH
DB' ',0AH,0DH,0Ah 
DB' For details see your K801 RS-232 Installation- ',0ah,0dh
DB' and Diagnostic Discription Documentation.',0AH,0AH,0AH,0DH
DB' CONTINUE:  CR',0AH,0DH
DB' EXIT:      ^C $'
IN2Z:	DB'The RS-232 adapter has the BASE PORT ADDRESS:'
BSPO:	DB'    XX Hex. ',0AH,0DH,0AH,0DH,0AH,0DH
	DB'CONTINUE:  CR $'
IN3Z:	DB' PROGRAM   RUN-TYPE-MENUE',0DH,0AH
	DB' STOP ON ERROR: Y/N $'
IN4Z:	DB' CONTINUES RUN: Y/N $'
DI1S: 	DB' PHASE 1: DM V BUS TEST         $'
DI2S:	DB' PHASE 2: INITIALISATION        $'
DI3S:	DB' PHASE 3: LOCAL LOOP BACK       $'
DI4S:	DB' PHASE 4: REMOTE LOOP BACK      $'
DI5S:	DB' PHASE 5: BAUD RATE GEN.TEST    $'
DI6S:	DB' PHASE 6: OVERRUN STATUS BIT    $'
DI7S:	DB'   OK  ',0AH,0DH,'$'
DI8S:	DB' ERROR ',0AH,0DH,'$'
DI9S:
DB'NOT CHECKED SIGNAL LINES:  TSET,RSET,DSRS',0AH,0DH
DB'CHECK -8V, +12V  BY A MULTIMETER',0AH,0DH,0AH,0DH,0AH
DB' RUN-TYPE-MENUE     : R',0AH,0DH 
DB' CONTINUE (RESTART) : CR',0AH,0DH
DB' EXIT               : ^C $' 
IN5Z:
DB'  DM V BUS ERROR',0AH,0DH
DB'  OR WRONG BASE PORT ADDRESS VIA IFSEL SWITCH SELECTED',0AH,0AH,0DH
DB'      SWITCH     PORT ADDRESS ',0AH,0DH
DB'        0A          60 HEX    ',0ah,0dh
DB'        0B          68 HEX    ',0AH,0DH
DB'        1A          70 HEX    ',0AH,0DH
DB'        1B          78 HEX    ',0AH,0DH
DB'        2A          30 HEX    ',0AH,0DH
DB'        2B          38 HEX    ',0AH,0DH
DB'        3A          B0 HEX    ',0AH,0DH
DB'        3B          B8 HEX    ',0AH,0DH
DB'        4A          C0 HEX    Reserved for Hard Disk',0AH,0DH
DB'        4B          C8 HEX    ',0AH,0DH,0AH,0AH
DB' Check your K-801 IFSEL Switch setting.',0ah,0dh
DB' For details see your K-801 RS-232 Installation Documentation.',0ah,0dh,0AH
DB' CONTINUE : CR $'
IN6Z:
DB' STOP ON ERROR  HIT:  C=Continue  Q=Quit  L=Loop  D=Display',0AH,0DH,'$'
IN7Z:	DB' ** END ** $'        
STAE:	DS 256
;END
