* CONSOLE I/O DRIVER PACKAGE for Altair 680
* Mike Douglas, Nov 2022

SIOCSR	equ	$F000		;mainboard control/status
SIODR	equ	$F001		;mainboard data 
UIOCSR	equ	$F006		;UIO control/status
UIODR	equ	$F007		;UIO data register

RDRF	equ	$01		;receive data available
TDRE	equ	$02		;ready to transmit

* I/O Vector Table at $B3E5

	org	$B3E5

	fdb	inChar		;input character, no echo
	fdb	rtiInst		;IRQ interrupt handler
	fdb	$FF00		;SWI vector, write does nothing
	fdb	$FF00		;IRQ vector, write does nothing
	fdb	return		;timer off
	fdb	return		;timer on
	fdb	return		;timer initialization
	fdb	monitor		;jump to ROM monitor
	fdb	initSio		;serial port initialization
	fdb	inStat		;check terminal status
	fdb	outChar		;output character from A
	fdb	inEcho		;input character and echo

	org	$B390		;code starts at $B390	
*-------------------------------------------------------------
* initSio - Init ACIA with receive interrupts off (the 680
*   monitor enabled receive interrupts in the 6850)
*-------------------------------------------------------------
initSio	ldaa	#3		;initialize ACIA on the UIO
	staa	UIOCSR		
	ldaa	#$11		;init for 8N2, interrupts off
	staa	UIOCSR		;UIO ACIA
	staa	SIOCSR		;and system ACIA
return	rts			
				
*-------------------------------------------------------------
* inStat - Return non-zero if character present
*-------------------------------------------------------------
inStat	ldaa	SIOCSR		;character present?
	anda	#RDRF		
	rts			

*-------------------------------------------------------------
* inChar - Return character from console in A
*-------------------------------------------------------------
inChar	bsr	inStat		;wait for character
	beq	inChar

	ldaa	SIODR		;return character
	anda	#$7F		
	rts			

*-------------------------------------------------------------
* inEcho - Input character with echo
*-------------------------------------------------------------
inEcho	bsr	inChar		;get character
*				;fall into outChar to echo

*-------------------------------------------------------------
* outChar - Output character from A to console
*-------------------------------------------------------------
outChar	psha			;preserve character to send

ocWait	ldaa	SIOCSR		;wait for OK to xmit
	bita	#TDRE		
	beq	ocWait		

	pula			;output character
	staa	SIODR		
	rts			

*-------------------------------------------------------------
* monitor - Enter the Altair 680 monitor
*-------------------------------------------------------------
monitor	sei			;interrupts must be masked for
	jmp	$FFD8		;  serial port in 680 monitor

rtiInst	rti			;timer interrupt not implemented

*-------------------------------------------------------------
* MEMEND - Memory end addres
*-------------------------------------------------------------
	org	$AC2B		
	fdb	$9FFF		;end of RAM

	end	$AD00		;include entry address of FLEX	
				