*-----------------------------------------------------------------------
* APATCH.ASM - Patch the editor and assembler to allow use of a separate
*   high speed reader/punch connected to the console port through an
*   A/B switch. The editor "E" command (punch edit buffer to tape) and 
*   the assembler "2T" command (punch S-record tape) are patched. There 
*   is a 3 second delay before punching starts to allow the operator to 
*   flip an A/B port selection switch. During the punch operation, a
*   per-character delay is implemented to reduce the output rate to 
*   about 49 cps. After the punch operation is complete, another 3
*   second delay is provided to switch back to the console terminal
*   to avoid printing the terminal prompt onto the tape.
*
*   Mike Douglas, Nov 23, 2023
*-----------------------------------------------------------------------

* Set CPSRATE for the desired punch rate

CPS49	EQU	1276		;49 CPS RATE
CPS69	EQU	893		;69 CPS RATE

CPSRATE	EQU	CPS49

*-------------------------------------------------------------------------
* This patch code is located in spare space made by starting the
*   edit buffer at $1CD8 instead of $1C81.
*-------------------------------------------------------------------------

	ORG	$1C81
PUNFLAG	FCB	0		;NON-ZERO IF PUNCHING
SAVEX	FDB	0		;SAVE AREA FOR X		

* PACEOUT - Paced character output based on PUNDLY value

PACEOUT	JSR	$FF81		;OUTPUT B (IN MONITOR)
	LDAB	PUNFLAG
	BEQ	NODLY

	STX	SAVEX		;PRESERVE X
	LDX	#CPSRATE	;E.G., 49 CPS

PUNWAIT	DEX			;8US
	BNE	PUNWAIT		;8US

	LDX	SAVEX		;RESTORE X AND EXIT
NODLY	JMP	$0230		;JSR/RTS IS TOO SLOW FOR 9600 BAUD

* STARTE - Start of the editor E command (punch source tape). Set PUNFLAG
*   true for 49 cps output rate and delay three seconds to allow operator 
*   to switch an A/B port switch.

STARTE	INC	PUNFLAG		SET PUNCH FLAG TRUE
	BSR	DELAY3		;DELAY 3 SECONDS
	JSR	$0167		;PUNCH NULL LEADER
	LDX	#1		;PERFORM INSTRUCTION CLOBBERED AT $1AC6
	RTS

* EXITE - Exiting the editor E command. Restore output rate to full
*   speed and delay three seconds to allow the operator to switch an
*   A/B port switch back to the terminal.

EXITE	CLR	PUNFLAG		;BACK TO FULL OUTPUT SPEED
	BSR	DELAY3		;DELAY3 3 SECONDS FOR A/B SWITCH
	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 PUNFLAG for 49 cps output rate and
*   delay three seconds to allow operator to switch an A/B port switch.

START2T	LDAA	$001A		;PUNCH S-RECORD TAPE (2T)?
	BEQ	NOT2T		;NO

	INC	PUNFLAG		;SET PUNCH FLAG TRUE
	BSR	DELAY3		;DELAY 3 SECONDS FOR A/B SWITCH

NOT2T	CLRA			;PERFORM INSTRUCTIONS CLOBBERED AT $974
	STAA	$0026
	RTS

* EXIT2T - Exiting the assembler pass 2 command. If it was the 2T
*   command to punch a tape, Restore output rate to full speed and
*   delay three seconds to allow operator the to switch an A/B port
*   switch back to the terminal.

EXIT2T	CLR	PUNFLAG		;BACK TO FULL OUTPUT SPEED
	LDAA	$001A		;PUNCH S-RECORD TAPE (2T)?
	BEQ	EXIT2		;NO
	BSR	DELAY3		;ELSE, DELAY 3 SECONDS FOR A/B SWITCH

EXIT2	LDX	#$04D9		;PERFORM INSTRUCTION CLOBBERED AT $0B8A
	RTS

* DELAY3 - Delay three seconds (680b runs at 500KHz, not 1MHz)

DELAY3	LDAA	#3		;DELAY 3 SECONDS
ONESEC	LDX	#62500		;1S = 62500*16US
LOOP	DEX			;8US
	BNE	LOOP		;8US
	DECA
	BNE	ONESEC
	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 (INIT)
	LDX	#$1CD6

	ORG	$088B		;IN ASSEMBLER
	LDX	#$1CD9

	ORG	$1648		;IN EDITOR
	LDX	#$1CD8

* Re-direct character out to PACEOUT which outputs data to the console
*   port at full speed or with a delay to give about 49 CPS based on
*   the value 0 or 5 in PUNDLY.

	ORG	$022D
	JMP	PACEOUT		;CALL PACED CHARACTER OUT

* 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

* 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

	END
