		ASSUME	CS:CODE,DS:DATA,SS:STACK

DATA		SEGMENT	PUBLIC
DATA		ENDS

CODE		SEGMENT	PUBLIC


MAIN		PROC

			CALL	PRINT_IN

			; Timer #4 must be set to Async 1/16 for 19200bps
			; Timer #4 mode must be 2 "Rate Generator"
			; RS232C BIOS is using mode 3 "Rectangular Rate Generator"
			MOV		DX,0056H
			MOV		AL,076H  ; Valule based on BIOS disassembly
			OUT		DX,AL

			; RS232C BIOS Disassembly showed it writes two bytes in 0052H in a sequence.
			MOV 	DX,0052H
			MOV		AL,04H
			OUT		DX,AL
			MOV		AL,00H
			OUT		DX,AL

			MOV		DX,0A02H
			XOR		AL,AL

			MOV		DX,0A02H
			XOR		AL,AL
			OUT		DX,AL
			CALL	RS232C_UNIT_DELAY

			MOV		DX,0A02H
			XOR		AL,AL
			OUT		DX,AL
			CALL	RS232C_UNIT_DELAY

			MOV		DX,0A02H
			XOR		AL,AL
			OUT		DX,AL
			CALL	RS232C_UNIT_DELAY

			MOV		DX,0A02H
			MOV		AL,040H		; Internal reset
			OUT		DX,AL
			CALL	RS232C_UNIT_DELAY


			; 04EH
			; S2 S1 Ep PN L2 L1 B2 B1
			;  0  1  0  0  1  1  1  0
			; S2=0, S1=1 -> 1 stop bit
			; PN=0       -> No parity
			; L2=1, L1=1 -> 8 bit
			; B2=1, B1=0 -> 1/16 scalar
			MOV		DX,0A02H
			MOV		AL,4EH
			OUT		DX,AL
			CALL	RS232C_UNIT_DELAY


			; 0B7H
			; ON	Sync Char search (?), 
			; OFF	Internal Reset,
			; ON	RTS request
			; ON	Clear Error Flags
			; OFF	Break
			; ON	RXE Receive Enable
			; ON	DTR Treminal Ready
			; ON	TXE Transmission Enable
			MOV		DX,0A02H
			MOV		AL,0B7H
			OUT		DX,AL
			CALL	RS232C_UNIT_DELAY


			MOV		DX,0A08H
			MOV		AL,040H  ; DTR=1, Internal Clock for Rx and Tx
			OUT		DX,AL


			CALL	WAIT_TX_READY
			MOV		DX,0A00H
			MOV		AL,'Y'
			OUT		DX,AL

			CALL	WAIT_TX_READY
			MOV		DX,0A00H
			MOV		AL,'S'
			OUT		DX,AL

			CALL	WAIT_TX_READY
			MOV		DX,0A00H
			MOV		AL,0DH
			OUT		DX,AL

			CALL	WAIT_TX_READY
			MOV		DX,0A00H
			MOV		AL,0AH
			OUT		DX,AL

			CALL	PRINT_OUT

			; Termination
			MOV	AH,4CH
			MOV	AL,00H
			INT	21H

MAIN		ENDP

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

WAIT_TX_READY	PROC

WAIT_TX_READY_LOOP:
			MOV		DX,0A02H
			IN		AL,DX
			AND		AL,03H		; Just exit if RxD.
			JE		WAIT_TX_READY_LOOP
			RET

WAIT_TX_READY	ENDP

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

RS232C_UNIT_DELAY	PROC
				MOV		CX,256
RS232C_UNIT_DELAY_LOOP:
				DEC		CX
				JNE		RS232C_UNIT_DELAY_LOOP
				RET
RS232C_UNIT_DELAY	ENDP

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

PRINT_IN		PROC

		MOV	DL,'I'
		MOV	AH,06H
		INT	21H

		MOV	DL,'N'
		MOV	AH,06H
		INT	21H

		MOV	DL,0DH
		MOV	AH,06H
		INT	21H

		MOV	DL,0AH
		MOV	AH,06H
		INT	21H

		RET

PRINT_IN		ENDP

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

PRINT_OUT		PROC

		MOV	DL,'O'
		MOV	AH,06H
		INT	21H

		MOV	DL,'U'
		MOV	AH,06H
		INT	21H

		MOV	DL,'T'
		MOV	AH,06H
		INT	21H

		MOV	DL,0DH
		MOV	AH,06H
		INT	21H

		MOV	DL,0AH
		MOV	AH,06H
		INT	21H

		RET

PRINT_OUT		ENDP

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

CODE		ENDS

STACK		SEGMENT	STACK

		DB	1024 DUP(0)

STACK		ENDS

		END	MAIN


