;
; Simple test for z8 cross assembler.
; (Assumes z8671 MCU with Basic/Debug Interpreter)
;
; r.bush 15-apr-1989.
;
; This program demonstrates the use of the Basic serial output routine
; from assembly language. Also shows method of polled serial input/output
; routines.
;
;
; The following demonstrates the use of defines:
;
;             source text    replaced by
;             -----------    -----------
	def	safe		#$30	;safe working reg. address
	def	rp		r:253	;the z8 "RP" register
	def	basic_out	$61	;address of basic output routine
	def	basic_rp	#$16	;default basic RP
	def	serial_port	r:240	;serial data register
	def	irq		r:250	;interrupt request register
;
	org	$1020		;a safe program origin for my z8 setup
;
; a couple of equates..
;
cr:	equ	$0d		;Note: All labels must end with ':'!!
lf:	equ	$0a
;
; *** Start of main program ***
;
start:
	push	rp		;save rp on entry
	srp	safe		;set rp to the only safe one
	ld	r0,#10		;loop counter
loop:
	ld	r2,#msg1!h	;hi order byte of message address
	ld	r3,#msg1!l	;lo order byte of message address
	call	out_msg		;send message to serial port
	djnz	r0,loop		;loop until r0 = 0
;
	ld	r2,#msg2!h	;hi order byte of message address
	ld	r3,#msg2!l	;lo order byte of message address
	call	out_msg1	;send message to serial port (our routine)
	call	inch1		;wait for user input
	pop	rp		;restore callers rp
	ret			;return to caller (basic)
;
;out_msg - Outputs message pointed to by rr2.
;          Expects message to be null terminated.
;
out_msg:
	ldc	r1,@rr2		;get character from message buffer
	cp	r1,#0		;is it the terminator?
	jr	z,endit		;if terminator end loop
	call	outch		;send char to serial port via Basic i/o
	incw	rr2		;point to next char of message
	jr	out_msg		;loop back
endit:
	ret
;
; Same as out_msg except it calls our serial output routine.
;
out_msg1:
	ldc	r1,@rr2		;get character from message buffer
	cp	r1,#0		;is it the terminator?
	jr	z,endit1	;if terminator end loop
	call	outch1		;try our serial output routine
	incw	rr2		;point to next char of message
	jr	out_msg1	;loop back
endit1:
	ret
;
; outch - Outputs value in r1 to serial port.
;	  Calls basic serial output routine.
;
outch:
	ld	r:19,r1		;store r1 to r:19 (basic_out expects this)
	push	rp
	srp	basic_rp	;set rp to the required basic default
	call	basic_out
	pop	rp
	ret
;
; Stand alone version of polled serial output routine.
; Based on disassembly of Basic character output routine.
;
outch1:
	and	irq,#$e7	;clear serial irq bits	
	ld	serial_port,r1	;send the character
chl:	tm	irq,#$10	;wait for irq4 transition to non zero	
	jr	z,chl		; (I assume this means char has been sent)
	ret
;
; Stand alone version of polled serial input routine.
; Waits for character to appear at serial port, returns character in r1.
; Based on dissassembly of Basic character input routine.
; Note: this routine does not echo character..
; 
inch1:	
	tm	irq,#$08	;check for char available (irq3)
	jr	z,inch1		;loop till char available
	ld	r1,serial_port	;read serial port
	ret
;
; storage
;
msg1:	db	'*** This is only a Test!! ***',$0d,$0a,$00
msg2:	db	'*** Press any key to end Test: ***',cr,lf,0
;
end:

