;
;	PATCH AREA FOR ASCOM MODEM PORT DRIVER
;
;	THE FOLLOWING ROUTINES MUST BE PROVIDED FOR PROPER OPERATION OF ASCOM.
;	THE JUMP INSTRUCTIONS AT THE START OF THE PROGRAM AREA MUST BE
;	MAINTAINED AS SHOWN, ALTHOUGH THE ROUTINES THAT THE JUMPS BRANCH
;	TO SHOULD BE MODIFIED FOR THE PARTICULAR SERIAL PORT TO BE USED.
;	THE MODEM ROUTINES DO NOT HAVE TO PROTECT ANY REGISTERS.
;
;
;
;
; setup for Microbee 64
; baud rate set externally
; setup by m.j.fagan of FMS  699 9899
;
FALSE	EQU	0
TRUE	EQU	NOT FALSE
HAYES	EQU	TRUE
;
;
;
MDMDAT	EQU	02A01H	; DATA PORT
MDMSTA	EQU	02A00H	; STATUS PORT
MDMCMD	EQU	02A00H	; COMMAND PORT
MDMBAU	EQU	02A00H	; BAUD PORT
MDMRDA	EQU	01H	; RDA FLAG
MDMTBE	EQU	02H	; TBE FLAG
MDMBRK	EQU	60H	; BREAK COMMAND
RESET	EQU	03H	; MASTER RESET
;
B8	EQU	54H	;8 bit, 1 stop, No parity
;
BDOS	EQU	05H
CNSTAT	EQU	0BH 
CNIN	EQU	01H
;
CR	EQU	0DH	;ASCII CR
LF	EQU	0AH	;ASCII LF
TB	EQU	09H	;ASCII TAB
;
	ORG	103H
;
START:
	JMP	COMINI	;JUMP TO MODEM INIT
	JMP	COMSTA	;JUMP TO MODEM STATUS
	JMP	COMINP	;JUMP TO MODEM INPUT
	JMP	COMOUT	;JUMP TO MODEM OUTPUT
	JMP	COMBRK	;JUMP TO MODEM BREAK ROUTINE
	JMP	COMDIL ;JUMP TO AUTO-DIAL ROUTINE
	JMP	COMANS	;JUMP TO AUTO-ANSWER ROUTINE
	JMP	COMHNG	;JUMP TO PHONE HANG-UP ROUTINE
	JMP	BEFDMP	;JUMP TO PRE-AUTODUMP ROUTINE
	JMP	AFTDMP	;JUMP TO POST-AUTODUMP ROUTINE
	JMP	INIT	;JUMP TO USER INIT ROUTINE
	JMP	EXIT	;JUMP TO USER EXIT ROUTINE
	JMP	USER1	;JUMP TO FIRST USER ROUTINE
	JMP	USER2	;JUMP TO SECOND USER ROUTINE
	JMP	USER3	;JUMP TO THIRD USER ROUTINE
	JMP	USER4	;JUMP TO FOURTH USER ROUTINE
;
;	BAUD VALUES ALLOWED- SOME NUMBER CONTAINED IN THIS TABLE MUST MATCH
;	THE NUMBER ENTERED BY THE OPERATOR IN A "BAUD" COMMAND IN ORDER TO
;	HAVE A "BAUDSL" NUMBER CHOSEN. THE DECIMAL NUMBER ENTERED BY THE
;	OPERATOR WILL BE CONVERTED TO A BINARY INTEGER AND COMPARED AGAINS
;	ALL THE ENTRIES IN THIS TABLE. IF NO MATCH IS FOUND, AN "ARGUMENT
;	ERROR" WILL RESULT. IF A MATCH IS FOUND, THE NUMBER (0-15) WITHIN
;	THE TABLE CORRESPONDING TO THE POSITION OF THE MATCHED ENTRY
;	WILL BE PLACE AT LOCATION "BAUDSL".
;
BAUDRT:
	DW	-1	;50
	DW	-1	;75
	DW	-1	;110
	DW	-1	;134
	DW	-1	;150
	DW	300	;300
	DW	-1	;600
	DW	1200	;1200
	DW	-1	;1800
	DW	-1	;2000
	DW	-1	;2400
	DW	-1	;3600
	DW	-1	;4800
	DW	-1	;7200
	DW	-1	;9600
	DW	-1	;19200
;
BAUDSL:	DB	00		;THIS ENTRY MUST BE HERE--WHEN MODEM INIT
				; ROUTINE IS CALLED, IT WILL CONTAIN A
				; NUMBER FROM 0 TO 15 CORRESPONDING
				; TO THE SELECTED BAUD RATE
;
SIGNON:	DW	SMESSG		;ADDRESS OF SIGN-ON STRING
HELP:	DW	HMESSG		;ADDRESS OF USERHELP STRING
;
;	NO USER STUFF BEFORE THIS POINT !
;==========================================================================
;
;				SIGN-ON STRING MUST END WITH '$' CHAR!
SMESSG:	DB	CR,LF
	DB	'SETUP FOR MICROBEE - USE CONFIG ',CR,LF
        db      ' use CONFIG to set baud rate ',cr,lf
	DB	CR,LF,'$'
;	BAUD VALUE TABLE CORRESPONDS TO BAUD RATES- THESE VALUES WILL BE USED
;	BY YOUR CUSTOMIZED "INIT" ROUTINE AS THE BYTE TO BE GIVEN TO YOUR
;	I/O CONTROLLER (ASSUMING YOU HAVE PROGRAMMABLE BAUD RATES). THERE
;	MUST BE 16 BYTES IN THE TABLE EVEN IF YOU DO NOT HAVE 16 POSSIBLE
;	BAUD RATES. THE BAUD RATES THAT CORRESPOND TO EACH ENTRY IN THIS TABLE
;	ARE IN TABLE "BAUDRT". YOU SHOULD SET THESE TWO TABLES UP SO THAT
;	BYTE VALUES IN THE "BAUDVL" TABLE CORRESPOND IN RELATIVE POSITION TO
;	THE ENTRIES IN THE "BAUDRT" TABLE.
;
;	The OSBORNE uses the UART division factor to control baud rate.
;
BAUDVL:
	DB	00H	;50 BAUD
	DB	00H	;75 BAUD
	DB	00H	;110 BAUD
	DB	00H	;134.5 BAUD
	DB	00H	;150 BAUD
	DB	02H	;300 BAUD
	DB	00H	;600 BAUD
	DB	01H	;1200 BAUD
	DB	00H	;1800 BAUD 
	DB	00H	;2000 BAUD
	DB	00H	;2400 BAUD
	DB	00H	;3600 BAUD
	DB	00H	;4800 BAUD
	DB	00H	;7200 BAUD
	DB	00H	;9600 BAUD
	DB	00H	;19.2 K BAUD
;
;	MODEM INITIALIZATION ROUTINE-- THIS ROUTINE SHOULD DO EVERYTHING
;	NECESSARY TO PREPARE YOUR SERIAL PORT FOR COMMUNICATIONS. THIS
;	SETTING THE BAUD RATE IN PROGRAMMABLE BAUD RATE SYSTEMS. THE BYTE
;	IN LOCATION "BAUDSL" WILL CONTAIN A NUMBER FROM 0 TO 15 INDICATING
;	THE BAUD RATE SELECTED. THIS ROUTINE SHOULD LOOK THE BAUD RATE VALUE
;	UP IN THE "BAUDVL" TABLE AND GIVE TO THE I/O CONTROLLER.
;
	ret
COMINI:
	ret
;
;	MODEM STATUS ROUTINE- THIS ROUTINE MUST RETURN WITH THE ZERO FLAG
;	ON IF NO RECEIVED CHARACTER FROM MODEM IS AVAILABLE, AND THE ZERO
;	OFF IF A CHARACTER IS READY.
;
comsta:	call	0e030h
	cma
	ora 	a
	rz
	sta	store
	ret
COMOUT:
;
;	MODEM INPUT CHAR- THIS ROUTINE IS USED TO FETCH A RECEIVED CHARACTER
;	FROM THE MODEM PORT AND PLACE IN THE A REGISTER. IT DOES NOT HAVE
;	TO CHECK FOR RECEIVED DATA AVAILABLE SINCE ASCOM USES THE MODEM
;	STATUS ROUTINE ABOVE BEFORE ANY ATTEMPT TO FETCH A CHARACTER IS MADE.
;
	call	0e04bh
	ret
COMINP:
	lda	store
	ret
	RET			;ALL DONE
;
;	CHARACTER MUST BE WAITING AT INPUT PORT
;
;
;	MODEM BREAK ROUTINE- IF AVAILABLE, YOU MAY PLACE A ROUTINE HERE THAT
;	WILL BE USED BY ASCOM WHEN THE OPERATOR IN CONVERSATIONAL MODE
;	REQUESTS A "BREAK" BY DEPRESSING CONTROL-B. IF YOUR SERIAL DEVICE
;	DOES NOT SUPPORT A BREAK CHARACTER (DEFINED TO BE A 500ms LOW ON THE
;	TRANSMITTED DATA LINE), THEN SIMPLY RETURN.
;
COMBRK:
	ret
COMBLP:
	ret
;	USER AUTO DIAL ROUTINE. ON ENTRY, HL POINTS TO REST OF
;	COMMAND LINE, ENDING WITH A NULL.
;
;	This implementation will use the HAYES Smartmodem. It
;	makes no assumptions about the settings of the VERBOSE
;	and ECHO switches under the front cover. All characters
;	on the command line entered after the DIAL command will
;	be sent to the modem. The operator can therefore take
;	advantage of the Smartmodem's programmable features.
;
COMDIL:
;
COMDI1:
	RET			; ==>CONNECTED
;
;	USER AUTO ANSWER ROUTINE. ON ENTRY, HL POINTS TO REST OF
;	COMMAND LINE, ENDING WITH A NULL.
;
COMANS:
;
;
	RET
;
;	USER HANGUP ROUTINE. ON ENTRY, HL POINTS TO REST OF
;	COMMAND LINE ENDING WITH A NULL.
;
COMHNG:
;
	RET
;
;	ROUTINE TO BE CALLED WHEN CAPTURE IS ON AND CAPTURE BUFFER 
;	IS FULL. ON ENTRY, A WILL CONTAIN THE POSSIBLY TRANSLATED XOFF
;	CHARACTER.
;
;	POSSIBLE USE OF THIS ROUTINE WILL BE TO SEND AN XOFF 
;	CHARACTER FOLLOWED BY POSSIBLY WAITING FOR SOME MORE 
;	INPUT UNTIL THE XOFF HAS BEEN ACCEPTED BY THE SENDER. 
;	THIS ROUTINE AND THE ONE FOLLOWING WILL ONLY BE CALLED 
;	IF AUTODUMP IS ON.
;
;
BEFDMP:
	ret
;
;	Begin loop waiting for about 3 seconds of silence, buffering
;	input characters (to a maximum of 80 for this implementation).
;	If the autodump buffer gets full, we will simply exit to allow
;	ASCOM to dump its capture buffer, causing any additional 
;	incoming data to be lost.
;
BEFDM1:
BEFDM2:
	RET			;RET IF SO, REMOTE STATION QUIET
;
;	A CHARACTER HAS ARRIVED, GET IT AND SAVE IN BUFFER
;
BEFDM3:
;
;	ROUTINE TO BE CALLED AFTER THE CAPTURE BUFFER DURING 
;	AUTODUMP HAS BEEN WRITTEN TO DISK. TYPICAL USE WOULD BE 
;	TO SEND THE XON THAT WOULD ALLOW THE SENDING STATION TO 
;	CONTINUE. ON ENTRY, A WILL CONTAIN THE POSSIBLE TRANSLATED
;	XON CHARACTER.
;
;	In this implementation, no after dump routine is used 
;	because there may be characters buffered in the autodump 
;	buffer. When those characters are used up, the XON will 
;	be send.
;
AFTDMP:
	RET
;
;	USER1 will redial the last number
;
USER1:
;
	RET
;
;	USER2 will cancel auto-answer mode
;
USER2:
;
	RET
;
;	USER3 & USER4 are not used
;
USER3:
USER4:
	RET
;
;
;	SPECIAL SUPPORT ROUTINES FOR HAYES SMARTMODEM
getret:	ret
	RET
;
;	FIRST CHARACTER OF RETURN CODE WAITING, GET IT
;
GETRE1:
;
GETRE2:
GETRE3:
GETRE4:
	RET
;
;	and full duplex operation and no echo of command characters.
SETUP:
;	CONBRK: Routine to check for a console break.  If the 
;	operator enters any control code, this routine will clear 
;	the Z flag and get the character.
;
CONBRK:
	RET
;
;	SNDMSG: Routine to send characters to the modem port
;	from the string pointed to by DE up until the first null.
;
SNDMSG:
;
;	WAIT: Routine to wait 1 second on a 4MHZ Z80
;
WAIT:
WAIT1:

	RET
;
;	USER INIT ROUTINE EXECUTED AFTER INITIAL SIGNON
;
INIT:				;Initialize Shadow code
	RET
;
store:	db	00h	;temp store for rs232 input routine
INIMSG:	DB	CR,LF,LF
	DB	'Shadow code moved to high memory, re-start ASCOM...'
	DB	CR,LF,'$'
;
;	CODE TO BE MOVED TO BELOW BDOS FOR SHADOW EXECUTION
;
FAKDOS:	JMP	000H		;Jump to real BDOS, set by USER4
;
	DI			;Disable interupts
	OUT	0		;Set Shadow mode
;	MVI	A,0
;	STA	0EF08H		;Set interupt flag
	PCHL			;Address calculated previously
;
INITP:	MOV	A,B		;Get initialization byte
	STA	MDMCMD		;Send to command port
	DB	18H,GOBK-$-1	;Fake JR instruction
;
STAT:	LDA	MDMSTA		;Check input/output status
	ANA	B		;Set Zero flag if ready
	DB	18H,GOBK-$-1	;Fake JR instruction
;
INP:	LDA	MDMDAT		;Get character
	DB	18H,GOBK-$-1	;Fake JR instruction
;
OUTP:	MOV	A,C
	STA	MDMDAT		;Put character
;
GOBK:	OUT	1		;Set normal mode
	EI			;Enable interupts
;	MOV	B,A		;Preserve A
;	MVI	A,1
;	STA	0EF08H		;Set interupt flag
;	MOV	A,B		;Restore A
	RET
FAKEND:
;
;	USER EXIT ROUTINE EXECUTED BEFORE RETURNING TO O/S
;
EXIT:
	JMP	COMHNG		;'hang-up' the phone
;
;	USER HELP STRING MUST END WITH '$' CHAR !
;
HMESSG:	DB	CR,LF,LF
	DB	'USER Commands -',CR,LF,LF
	DB	'USER1 - WILL REDIAL LAST NUMBER',CR,LF
	DB	'USER2 - WILL CANCEL AUTO-ANSWER MODE',CR,LF
	DB	'USER3 - Not implemented.',CR,LF
	DB	'USER4 - Initialize for communications.'
	DB	CR,LF,LF,'$'
;
;
	END
