;TERMINAL EMULATION PROGRAM WRITTEN FOR THE OSBORNE 1
;DOUBLE DENSITY -  SERIAL PORT BAUD RATE AS FIXED BY 
;UTILITY PROGRAM "SETUP". ORIGINALLY WRITTEN 3/1/84
;
;MARK MERRILL AALDERING
;12301 Luna Pl.
;Granada Hills, Ca. 91344
;
;----USE ^C TO EXIT TO CPM-----
;
;----USE ^P TO TOGGLE PARALLEL PRINTER OUTPUT----
;	(12K Print Buffering)
;
;----USE ^O TO OUTPUT AN ASSEMBLED PROGRAM TO THE
;	Z8671 (ASHLEY'S ASMBZ8)
;
;----USE ^T TO RECONFIGURE TERMINAL COMMUNICATION CHARACTERISTICS---
;
;----USE ^Z TO ERASE SCREEN---
;
;******************************************************************************
;
;ORIGINALLY WRITTEN FOR USE WITH THE MICROMINT
;Z8 DEDICATED CONTROLLER AND THE 
; 
; ALLEN ASHLEY ASMB Z8 CROSS ASSEMBLER
;	395 Sierra Madre Villa
;	Pasadena, Calif.
;			91107
;
; $75.00 as of 3/1/84
;
; My gratitude extends to Mr. Ashley for being
;able to provide his excellent cross assembler
;in a format readable by My Osborne 1 DD.
;
;******************************************************************************
;	
;RS-232C WIRING DETAILS:
;
;OSBORNE RS232				MICROMINT Z8
;2=============      ------------------------2
;              =  -
;		=_
;	       -   =	
;3<------------      =========================>3
;
;7<-------------------------------------------->7
;
;******************************************************************************
;
;
;
ORG 	100H
BDOS	EQU 	0005H
SWPOS	EQU 	0EF08H	;USED FOR BANK SWITCH
MASK1	EQU	01BH	;MASK OUT INCOMING ESCAPE SEQUENCE
MASK2	EQU	07FH	;MASK OUT BIT 7
CR	EQU	0DH	;
LF	EQU	0AH	;
CLEAR	EQU	26	;
;
;
;
;
;************ OSBORNE 1 DD CONSOLE I/O ROUTINES *******************************
;
;
	LXI	D,MESSG1;TYPE SIGN ON MESSAGE	
	MVI	C,9	;
	CALL	BDOS	;
	MVI	E,094H	;SET I/OBYTE
	MVI	C,08H	;
	CALL	BDOS	;
	MVI	A,00H	;INITIALIZE PRINTER TOGGLE FLAG
	STA	FLAG1	;
	STA	FLAG2	;    Z8671 LOAD ROUTINE FLAG
	STA	PNTR	;    PRINTER BUFFER POINTER 
	STA	PNTR+1	;
	JMP	INPUT	;BEGIN MONITOR PGM WITH KEYBOARD INPUT CHECK
RCVD:	LDA	RCVDAT	;LOAD INCOMING DATA
	CPI	00H	;  NEW DATA?
	JNZ	NXT	;YES - SKIP PRINTER BUFFER ACTIVITY TEST
	LDA	CNTR	;LOAD CURRENT NO ACTIVITY COUNT
	INR 	A	; INCREMENT COUNT
	STA	CNTR	;  SAVE VALUE
	CPI	80H	; RECENT ACTIVITY?
	JM	INPUT	;YES - DON'T PRINT BUFFER YET
	LDA	FLAG1	;PRINTER OUTPUT DESIRED?
	CPI	00H	;
	JNZ	PRINT	; YES - PRINT NOW
	JMP	INPUT	; NO  - GOTO KEYBOARD INPUT
NXT:	CPI	MASK1	;DON'T PRINT ESCAPE SEQUENCES
	JZ	INPUT	;
	ANI	MASK2	;DON'T UNDERLINE
	STA	RCVDAT	; SAVE THE MASKED INPUT VALUE
	MOV 	E,A	;  TYPE INPUT TO SCREEN
	MVI 	C,2	;
	CALL 	BDOS	;
	MVI	A,00H	;CLEAR THE PRINTER ACTIVITY COUNTER
	STA	CNTR	;
	LDA	FLAG2	;IS THE Z8671 LOAD SUBROUTINE RUNNING?
	CPI	00H	;
	RNZ		; YES - RETURN TO IT
	LDA	FLAG1	; IS THE OUPUT BEING PRINTED?
	CPI	00H	;
	JZ	INPUT	;  NO - GOTO INPUT
	LXI	D,00H	;***** PRINTER BUFFERING ROUTINE **********************
	LXI	H,PBFFR	;
	LDA	PNTR	;
	MOV	E,A	;
	ADI	01H	;
	STA	PNTR	;
	LDA	PNTR+1	;
	MOV	D,A
	ACI	00H	;
	STA	PNTR+1	;
	DAD	D	;
	XCHG		;
	LDA	RCVDAT	;
	STAX	D	;
	JMP 	INPUT	;
PRINT:	LDA	PNTR	;**** PRINTER OUTPUT ROUTINE ***************
	CPI	00H	;
	JZ	INPUT	;
	LXI	B,0H	;
POUT:	LXI	H,PBFFR	;
	DAD	B	;
	XCHG		;
	LDAX	D	;
	INX	B	;
	PUSH	B	;
	MOV	E,A	;
	MVI	C,5	;
	CALL	BDOS	;
	POP	B	;
	LDA	PNTR	;
	SUI	01H	;
	STA	PNTR	;
	JNZ	POUT	;
	LDA	PNTR+1	;
	CPI	00H	;
	JZ	NXT2	;
	SUI	01H	;
	STA	PNTR+1	;
	MVI	A,0FFH	;
	STA	PNTR	;
	JMP	POUT	;
NXT2:	MVI	A,00H	;
	STA 	CNTR	;**** END PRINTER OUTPUT ROUTINE **********************
INPUT:	MVI	A,00H	;SET RECEIVED DATA STORAGE LOCATION
	STA 	RCVDAT	;  TO 0
	MVI	C,06	;CHECK
	MVI	E,0FFH	;  KEYBOARD
	CALL	BDOS	;        FOR INPUT
	CPI	00H	;ANY KEY DEPRESSED?				
	JZ	READ	;  NO - READ I/O PORT INSTEAD
	CPI	03H	;^C RECEIVED?
	JZ	00H	;  YES - WARM BOOT
	CPI	0FH	;^O RECEIVED?
	JZ	ZLOAD	;  YES - OFFLOAD RESIDENT COMPILED Z8 PROGRAM
	CPI	14H	;^T RECEIVED?
	JZ	PSLCT	;  YES - SELECT NEW TERMINAL CONFIGURATION
	STA	XMTDAT	;STORE KEYBOARD INPUT RECEIVED
	CPI	10H	;^P RECEIVED?
	JZ	PTGL	; YES - JUMP TO PRINTER TOGGLE
	CPI	1AH	;^Z RECEIVED?
	JZ	CLRSC	; YES - JUMP TO CLEAR SCREEN
	STA	XMTDAT	;STORE	KEYBOARD INPUT TO BE SENT OUT
	JMP	WRITE	;SEND OUT SERIAL I/O
PTGL:	LDA	FLAG1	;TOGGLE PRINTER OUTPUT FLAG
	XRI	01H	;
	STA	FLAG1	;
	JMP	READ	;GO TO READ SERIAL I/O
CLRSC:	MOV	E,A	;CLEAR SCREEN WHEN ^Z REC'D
	MVI	C,2	;
	CALL	BDOS	;
	JMP	READ	;
;
;
;
;****************************** Z8671 LOAD ROUTINE ****************************
;
;
;
ZLOAD:	LXI	D,MESSG2
	MVI	C,9
	CALL	BDOS
	CALL	BFREAD
	LXI	D,MESSG3
	MVI	C,9
	CALL	BDOS
	CALL	BFREAD
	LXI	D,MESSG4
	MVI	C,9
	CALL	BDOS
	CALL	BFREAD
	MVI	A,0FFH
	STA	FLAG2
	POP	H
	POP	B
	POP	D
ZLOOP:	MOV	A,B
	ORA	C
	JZ	ENDZ
	INX	D
	LDAX	D
	CALL	ZXMT
	INX	H
	DCX	B
	JMP	ZLOOP
ENDZ:	MVI	A,00H
	STA 	FLAG2
	JMP	INPUT
BFREAD:	LXI	D,BUFFER
	MVI	A,04H
	STA	BUFFER
	MVI	C, 0AH
	CALL	BDOS
	LDA	BUFFER+2
	CALL	XFRM2
	RLC! RLC! RLC! RLC!
	ANI	0F0H
	MOV	H,A
	LDA	BUFFER+3
	CALL	XFRM2
	ANI	0FH
	ORA	H
	MOV	H,A
	LDA	BUFFER+4
	CALL	XFRM2
	RLC! RLC! RLC! RLC!
	ANI	0F0H
	MOV	L,A
	LDA	BUFFER+5
	CALL	XFRM2
	ANI	0FH
	ORA	L
	MOV	L,A
	XTHL
	PUSH	H
	RET
ZXMT:	STA	Z8DAT
	MVI	A,40H
	STA	XMTDAT
	CALL	ZWRITE
	MVI	A,25H
	STA	XMTDAT
	CALL 	ZWRITE
	MOV	A,H
	RLC! RLC! RLC! RLC!
	CALL	XFRM
	STA	XMTDAT
	CALL	ZWRITE
	MOV 	A,H
	CALL	XFRM
	STA	XMTDAT
	CALL	ZWRITE
	MOV	A,L
	RLC! RLC! RLC! RLC!
	CALL 	XFRM
	STA 	XMTDAT
	CALL	ZWRITE
	MOV	A,L
	CALL	XFRM
	STA	XMTDAT
	CALL	ZWRITE
	MVI	A,3DH
	STA	XMTDAT
	CALL	ZWRITE
	MVI	A,25H
	STA	XMTDAT
	CALL	ZWRITE
	LDA	Z8DAT
	RLC! RLC! RLC! RLC!
	CALL	XFRM
	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DA	XMTDAT
	ORI	41H
	STA	XMTDAT
	JMP	PTC2
PTB2:	LDA	XMTDAT
	ORI	42H
	STA 	XMTDAT
PTC2:	LXI	D,MESSG7
	MVI	C,9
	CALL	BDOS
	JMP	PWRITE
;
;
;
;
;******************************************************************************
;
;
;
;
MESSG1: DB	CLEAR,'OSBORNE 1 TERMINAL EMULATION PROGRAM',CR,LF,LF
	DB	'Use ^C to exit to CP/M',CR,LF
	DB	'    ^P to toggle buffered printer output',CR,LF
	DB	'    ^O to load an assembled Z8 program',CR,LF
	DB	'    ^T to reconfigure terminal',CR,LF
	DB	'    ^Z to clear screen',CR,LF
	DB	CR,LF,'                     Mark Aaldering- - 3/84',CR,LF,'$'
MESSG2:	DB	CLEAR,'Hex Loc. in OSBORNE memory of pgm (####) ?',CR,LF,LF,'$'
MESSG3:	DB	CLEAR,'Program Length (Hex Bytes, ####) ?',CR,LF,LF,'$'
MESSG4:	DB	CLEAR,'Load to Z8 memory @% (Hex ####) ?',CR,CR,LF,'$'
MESSG5:	DB	CLEAR,'Protocol Selection',CR,LF,LF
 	DB	'A . . 7 Bits + Even Parity + 2 Stop bits',CR,LF,LF
	DB	'B . . 7 Bits + Odd Parity  + 2 Stop bits',CR,LF,LF
	DB	'C . . 7 Bits + Even Parity + 1 Stop bit',CR,LF,LF
	DB	'D . . 7 Bits + Odd Parity  + 1 Stop bit',CR,LF,LF
	DB	'E . . 8 Bits + 2 Stop bits',CR,LF,LF
	DB	'F . . 8 Bits + 1 Stop bit',CR,LF,LF
	DB	'G . . 8 Bits + Even Parity + 1 Stop bit',CR,LF,LF
	DB	'H . . 8 Bits + Odd Parity  + 1 Stop bit',CR,LF,'$'
MESSG6:	DB	CLEAR,'Baud Rate Selection',CR,LF,LF
	DB	'A . . 1,200 Baud',CR,LF,LF
	DB	'B . . 300 Baud',CR,LF,LF,'$'
MESSG7:	DB	CLEAR,'          ....terminal reconfigured....',CR,LF,'$'
;
;
;
;
;******************************************************************************
;
;
;
;
FLAG1	DS	01	;PRINTER OUTPUT TOGGLE FLAG
FLAG2	DS	01	;Z8671 LOAD ROUTINE FLAG
Z8DAT	DS	01	;Z8 STORAGE LOCATION
BUFFER	DS	06	;INPUT LINE BUFFER
PNTR	DS	02	;PRINTER BUFFER LOC. OFFSET
CNTR	DS	01	;# OF READS W/NO NEW INFORMATION RETURNED
PBFFR	DS	3000H	;PRINTER BUFFER MEMORY(256 CHAR.)
;
;
;
;
;******* OSBORNE 1 MC6850 INTERFACE ADAPTER I/O ROUTINES **********************
;
;
;
;
	ORG	4000H; COMMON BLOCK OF OSBORNE 1 MEMORY
RCVDAT	DS	1    ; BETWEEN BANK1 & BANK2-- ALL SERIAL
XMTDAT	DS	1    ; I/O MUST BE DONE IN BANK2!!!
;
WRITE:	CALL	SWITCH
	LDA	2A00H
	ANI	02H
	JZ	WRITE
	LDA	XMTDAT
	ANI	MASK2
	STA 	2A01H
DUPLEX:	LDA	2A00H
	ANI	01H
	JZ	DUPLEX
	LDA	2A01H
	STA	RCVDAT
	CALL	RETURN	
	JMP 	RCVD
READ:	CALL 	SWITCH
	LDA	2A00H
	ANI	01H
	JZ 	ENDR
	LDA 	2A01H
	STA	RCVDAT
ENDR: 	CALL	RETURN
	JMP 	RCVD
PWRITE:	CALL	SWITCH
	LDA	XMTDAT
	STA	2A00H
	CALL	RETURN
	JMP	INPUT
SWITCH: DI		;SWITCH TO BANK2 MEMORY
	OUT 	00H
	MVI 	A,00H
	STA	SWPOS
	RET
RETURN: OUT	01H	;SWITCH TO BANK1 MEMORY
	MVI 	A,01H
	STA 	SWPOS
	EI
	RET
	END
;******************************************************************************