;TESTPROKO2.ASM: SIMPLE PORT-TO-PORT TRANSFER WITH FIFO
; DELIBERATELY BRIEF FOR FRONT PANEL TOGGLING
; VERS. 1.1 MARTIN EBERHARD 10 DEC 2019
;   PORTED FROM TESTOP80 (JUST REMOVED WRITING BUFFER
;   STATUS TO THE STATUS LEDS BECAUSE THE PROKO MOD 2
;   HAS NO STATUS LEDS.)
;
; THIS PROGRAM READS A BINARY FILE FROM A PIO
; AND WRITES IT TO 2SIO PORT 0.
; INPUT DATA IS STASHED IN A 256-BYTE FIFO.
; THIS PROGRAM RUNS AT 00H. ITS FIFO IS AT 100H-1FFH.
;
; OPERATION:
;   TOGGLE-IN PROGRAM
;   RUN PROGRAM AT 0000H
;   SEND FILE WITH PROKO MOD 2 PAPER TAPE READER
;     (NOT TOO FAST...)


;2SIO PORT 0 ADDRESSES

A0CTRL	EQU	10H	;ACIA 0 CONTROL OUTPUT PORT
A0STAT	EQU	10H	;ACIA 0 STATUS INPUT PORT
A0TDR	EQU	11H	;ACIA 0 TX DATA REGISTER
A0RDR	EQU	11H	;ACIA 0 RX DATA REGISTER

;PIO PORT ADDRESSES
PIOSTA	EQU	04H	;PIO STATUS PORT
PIODAT	EQU	05H	;PIO DATA PORT

;ACIA CONTROL REGISTER BITS

ACCDS1	EQU	00000001B	;COUNTER DIVIDE SEL 1
ACCDS2	EQU	00000010B	;COUNTER DIVIDE SEL 2
ACWS1	EQU	00000100B	;WORD SELECT 1
ACWS2	EQU	00001000B	;WORD SELECT 2
ACWS3	EQU	00010000B	;WORD SELECT 3
ACTC1	EQU	00100000B	;TX CONTROL 1
ACTC2	EQU	01000000B	;TX CONTROL 2
ACRXIE	EQU	10000000B	;RX INT ENABLE

ACRSET	EQU	00000011B	;MASTER RESET

;COMPOUND ACIA COMMANDS

AC0INIT	EQU	ACCDS1+ACWS3	;/16 CLOCK, 8 BITS+2 STOP

;ACIA STATUS REGISTER BITS

ASRDRF	EQU	00000001B	;RX DATA REG FULL
ASTDRE	EQU	00000010B	;TX DATA REG EMPTY
ASDCD	EQU	00000100B	;DATA CARRIER DETECT
ASCTS	EQU	00001000B	;CLEAR TO SEND
ASFE	EQU	00010000B	;FRAMING ERROR
ASOVRN	EQU	00100000B	;RX OVERRUN ERROR
ASPE	EQU	01000000B	;PARITY ERROR
ASIRQ	EQU	10000000B	;INTERRUPT REQUEST

;PROGRAM EQUATES

FIFO	EQU	100H		;BUFF ADDR

ORG 00H

;INITIALIZE 256-BYTE FIFO

	LXI	D,FIFO		;DE IS OUTPUT POINTER
	MOV	H,D		;HL IS INPUT POINTER
	MOV	L,E
	MOV	C,E		;BYTE COUNT=0

;INITIALIZE ACIA

	MVI	A,ACRSET
	OUT	A0CTRL
	MVI	A,AC0INIT
	OUT	A0CTRL

;INITIALIZE PROKO MOD 2 HANDSHAKE FLIP-FLOP

	IN PIODAT

; MAIN LOOP - 2 ENTRY POINTS

MAIN0:	INR	C		;C HAS BYTE COUNT+1
MAIN1:

;GET ANY PENDING PROKO MOD 2 DATA AND PUT IT IN THE FIFO

CHKIN:	IN	PIOSTA		;BYTE AVAILABLE?
	RRC
	RRC
	JNC	CHKOUT		;NO OP80 DATA NOW

	IN	PIODAT		;GET INPUT BYTE
	MOV	M,A		;STASH BYTE IN FIFO
	INR	L		;BUMP FIFO POINTER
	INR	C		;BUMP BYTE COUNT

;SEND A FIFO BYTE TO THE TTY IF IT'S READY

CHKOUT:	IN	A0STAT		;TX DATA REG EMPTY?
	RRC			;ASTDRE IS BIT 1
	RRC
	JNC	CHKIN		;TDR STILL FULL

	DCR	C		;ANY BYTES IN FIFO?
	JZ	MAIN0		;NO: GO UNDO DCR C

	LDAX	D		;GET A FIFO BYTE
	OUT	A0TDR		;SEND BYTE TO OP80
	INR	E		;BUMP FIFO POINTER

	JMP	MAIN1		;GO CHECK INPUT
	
	END
