*-----------------------------------------------------------------------
* APATCH.ASM - Patch the editor and assembler to allow use of a high
*   speed reader/punch connected to the UIO serial port. The editor
*   "E" command (punch edit buffer to tape) and the assembler "2T"
*   command (punch S-record tape) are patched to output to the UIO
*   serial port as well as the console port. The console input routine
*   is patched to accept input from the UIO serial port as well as the
*   console at the same time.
*
*   Mike Douglas, Dec 18, 2023
*-----------------------------------------------------------------------

* Console and UIO Equates

CONSR	EQU	$F000		;CONSOLE SERIAL PORT
CONDR	EQU	$F001
UIOSR	EQU	$F006		;UIO SERIAL PORT
UIODR	EQU	$F007
RDRF	EQU	$01		;RECEIVE DATA READY
TDRE	EQU	$02		;TRANSMIT READY

*-------------------------------------------------------------------------
* This patch code is located in spare space made by starting the
*   edit buffer at $1CD8 instead of $1C81.
*-------------------------------------------------------------------------
	ORG	$1C81
PUNFLG	FCB	0		;NON-ZERO IF PUNCHING

* INCHAR - Return byte in B from console or the reader

INCHAR	LDAB	CONSR		;ANYTHING FROM CONSOLE?
	RORB
	BCC	CHKUIO		;NO, CHECK PUNCH
	JMP	$FF00		;ELSE, CALL CONSOLE INPUT CHAR 

CHKUIO	LDAB	UIOSR		;ANYHING FROM THE READER?
	RORB
	BCC	INCHAR		;NO, GO CHECK CONSOLE AGAIN

	LDAB	UIODR		;RETURN BYTE FROM READER
	ANDB	#$7F
	RTS

* OUTCHAR - Output byte in A (or B) to console, and if PUNFLG true,
*   output to the UIO serial port as well

OUTCHAR	JSR	$FF81		;OUTPUT B TO CONSOLE (IN MONITOR)
	TST	PUNFLG		;WRITING TO PUNCH TOO?
	BEQ	NOPUN		;NO, EXIT

WAITPUN	LDAB	UIOSR		;WAIT FOR XMIT READY
	ANDB	#TDRE
	BEQ	WAITPUN

	STAA	UIODR		;SEND BYTE TO PUNCH ON UIO

NOPUN	JMP	$0230		;JSR/RTS IS TOO SLOW FOR 9600 BAUD

* STARTE - Start of the editor E command (punch source tape). Set
*   PUNFLG true.

STARTE	INC	PUNFLG		;SET PUNFLG TRUE
	JSR	$0167		;PUNCH NULL LEADER
	LDX	#1		;PERFORM INSTRUCTION CLOBBERED AT $1AC6
	RTS

* EXITE - Exiting the editor E command. Set PUNFLG false.

EXITE	CLR	PUNFLG		;NO LONGER PUNCHING
	JMP	$167C		;THE REPLACED JUMP WENT HERE

* START2T - Start of an assembler pass 2 command. If it is the 2T
*   command to punch a tape, set PUNFLG true.

START2T	LDAA	$001A		;PUNCH S-RECORD TAPE (2T)?
	STAA	PUNFLG		;PUNFLG TRUE IF SO
	CLRA			;PERFORM INSTRUCTIONS CLOBBERED AT $974
	STAA	$0026
	RTS

* EXIT2T - Exiting the assembler pass 2 command. Set PUNFLG false

EXIT2T	CLR	PUNFLG		;NO LONGER PUNCHING
	LDX	#$04D9		;PERFORM INSTRUCTION CLOBBERED AT $0B8A
	RTS

*-------------------------------------------------------------------------
* Hooks into the Assembler/Editor
*-------------------------------------------------------------------------

* Move the start of the edit buffer from $1C81 to $1CD8 to make
*   room for the patch code.

	ORG	$0126		;IN MEMORY SIZE ROUTINE (START)
	LDX	#$1CD6

	ORG	$088B		;IN ASSEMBLER
	LDX	#$1CD9

	ORG	$1648		;IN EDITOR
	LDX	#$1CD8

* Re-direct the console input routine to INCHAR below

	ORG	$0183
	JSR	INCHAR

* Re-direct the console output routine to OUTCHAR below

	ORG	$022D
	JMP	OUTCHAR

* Jump to the init patch for the assembler 2T command

	ORG	$0974
	JSR	START2T

* Jump to the exit patch for the assembler 2T command

	ORG	$0B8A
	JSR	EXIT2T

* Jump to the init patch for the editor E command

	ORG	$1AC6
	JSR	STARTE

* Jump to the exit patch for the assembler E command

	ORG	$1AE5
	JMP	EXITE

	END
