;--------------------------------------------------------------------------
;
; PTAPE.ASM
;
;  This program duplicates the functionality of the tape loader in the 
;  Poly 88 4.0 monitor but uses the printer port at 2400 baud async
;  instead of the cassette port.
;
;  Alternatively, this program can be used to choose any other combination
;  of port and baud rate as well. Note, however, that the maximum baud
;  rate that the Poly tape load routines can reliably handle is 2400 baud.
;
;  Use the hex load command (HL) in Polex to load this program into memory.
;  Use the EX command to execute the program. The default load and start
;  address is 7000h. This fits in a 32K system and still provides enough
;  room to load BASIC. 
;
;  The program duplicates the user interface of the ROM tape loader: The
;  screen is cleared and the loader waits for P, B, or C to be typed, 
;  followed by the file name to load. This program forces asynchronous
;  format for both the P (Polyphase) and B (Byte) selections.
;
;  To change the port and/or baud rate, set the value of the BRGVAL equate
;  as desired. The first digit is the port (0 for cassette port, 1 for the
;  printer port). The second digit is the baud rate per the Poly-88
;  CPU documentation. 
;
;  Since BASIC uses it own routines for cassette load and save, neither
;  this program or the Poly-88 monitor determine how BASIC loads and
;  saves programs. The cassette image file "BASICP.CAS" is patched to
;  load and save programs through the printer port at 2400 baud. The 
;  same applies to the FORTH language, "FORTHP.CAS" is patched to load
;  and save throught the printer port at 2400 baud.
;
;  Version History
;
;  1.0  06/02/16   M. Douglas
;	Original, taken mostly from the Poly-88 4.0 Monitor
;
;--------------------------------------------------------------------------

BRGVAL	EQU	01BH	;2400 BAUD ON PRINTER PORT
LOADADR	EQU	7000H	;LOAD AND EXECUTE ADDRESS

;POLY-88 MONITOR ENTRY POINTS

USRTSR	EQU	0054H	;USART INTERRUPT SERVICE
KSR	EQU	010DH	;KEYBOARD INTERRUPT SERVICE
LCFLD	EQU	03A1H	;LOWER CASE FOLD
CLEAR	EQU	0392H	;CLEAR SCREEN
HEAD	EQU	02F3H	;SEARCH FOR TAPE HEADER
NAMER	EQU	02CFH	;GET FILE NAME TO LOAD
SETUP	EQU	02ADH	;SETUP 8251 FROM TABLE

;POLY-88 MONITOR RAM LOCATIONS

SRA4	EQU	0C16H	;UART IN VECTOR
SRA5	EQU	0C18H	;KBD INT VECTOR
POLYSP	EQU	0FFEH	;POLY-88 STACK POINTER

;MISC EQUATES

BRG	EQU	04h	;BAUD RATE GENERATOR PORT
	
;--------------------------------------------------------------------------
; PROGRAM ENTRY POINT
;--------------------------------------------------------------------------
	ORG	LOADADR

;EXIT POLEX MODE AND RESTORE THE NORMAL POLY 88 MONITOR ENVIRONMENT

 	XRA	A		;RESET BAUD RATE,
	OUT	BRG		;..ENABLE ONBOARD MEMORY

	LXI	H,KSR		;RESTORE POLY-88 INT ROUTINES
	SHLD	SRA5		;..IN ONBOARD MEMORY
	LXI	H,USRTSR
	SHLD	SRA4

	LXI	SP,POLYSP	;RESTORE ORIGINAL STACK POINTER

;DUPLICATE TAPE LOADER FROM POLY 88 ROM BUT USE DIFFERENT
;SERIAL PORT INIT PARAMETERS.
;
;TAPE IS THE BOOTSTRAP LOADER ROUTINE
;IT EXPECTS TO GET A B,P,OR C OR IT WILL
;WAIT FOR ONE
;

TAPE:	CALL	CLEAR	;CLEAR SCREEN (IN POLY ROM)
START:	CALL	LCFLD	;GET A CASE-FOLDED B OR P (IN POLY ROM)
	MOV	C,A	;SAVE THE B OR P FOR ECHOING.
	CPI	'P'	;POLYPHASE
	JZ	BITE	;DO BYTE STANDARD NO MATTER WHAT
	SUI	'B'	;BYTE STANDARD
	JZ	BITE
	DCR	A
	JZ	HEAD	;CONTINUE (IN POLY ROM)
	JMP	START	;NOT A B OR C OR P : TRY AGAIN

;
;BITE CONTAINS SETUP
;INFORMATION BYTE
;OPERATION OF THE USART
;
BITE:	CALL	SETUP	;SETUP 8251 (IN POLY ROM)
	DB	BRGVAL	;TO BRG: SEL. DEV. 1 2400 BAUD
	DB	0AAH	;FAKE SINCH CHAR IF USART EXPECTS ONE
	DB	040H	;INT. RST. GETS US TO MODE LEVEL
	DB	0CEH	;MODE: ASYNCH., 8 BITS, NO PARITY
			;2 STOP BITS, 16X CLOCK SCALING
	DB	000H	;FOR NOW COMMAND IS IDLE

	JMP	NAMER	;GET FILE NAME (IN POLY ROM)

	END
