//serb2_diag.c
#define DELAY 0xffff
main()
{
;	// prevents program from starting when compiled
#asm
; disable the watch dog timer
			ld		a, 0x51
	ioi	ld		(WDTTR), a	; ready watchdog for disable
			ld		a, 0x54
	ioi	ld	 (WDTTR), a		; disable watchdog timer

; set up timers for baud rate generation
			ld		a, 0x20		; Timer A5 to use output of Timer A1
	ioi	ld		(TACR), a			
			ld		a, 1
	ioi	ld		(TACSR), a	; enable Timer A
			ld		a, 0xF6		; for 110 baud
	ioi	ld		(TAT5R), a	; A5 divider = 246
			ld		a,0x10		; for 110
	ioi	ld 	(TAT1R), a	; A1 divider = 16

; set up serial port
			ld		a, 0x55
	ioi	ld		(PCFR), a	; enable all serial ports
			xor	a
	ioi	ld		(SBCR), a	; set serial port B to 8 bit, asynchronous

; set up parallel port A
			ld		a, 0x84
	ioi	ld		(SPCR), a	; set parallel port A to output, and disregard SMODE pins

; start of main program
			ld		e, 'a'		; load starting character

top:		ld		hl, DELAY	; initialize for delay loop
			ld		bc, 0x0001
			or		a				; clear 'c' bit
; delay loop
wait0:	nop					; for debugging only
			sbc	hl, bc	
			jr		nz,wait0			
						
			ld		a, e
	ioi	ld		(SBDR), a	; load a character into serial B data register
	ioi	ld		(PADR), a	;	and into parallel port A for visual indication
	
busy:		nop					; for debugging only
	ioi	ld		a, (SBSR)	; read serial B status register
			bit	2, a			; test if transmitter is busy
			jr		nz, busy		; jump if busy
			
			ld		a, e			; recover present character
			cp		'z'			; is it equal to the last character?
			jr		z, reset_	; if yes, then start at 'a' again
			inc	e				; if not, then increment to next character
			jr		top

reset_:	ld		e, 'a'

back:		nop					; for debugging only
			jr		top
				
	dw		0xFFFF				; end of program indicator
#endasm
}
