#
# Blitz-64 Micro Blitz Echo Program
#
# Harry Porter III - 12 May 2023
#
# This program echoes characters from/to the serial port.
# It also echoes to the digital output. Instructions used:
#   andi
#   beq
#   bne
#   controlu (1,3,4, and 5)
#
# All branching is relative so it can be located anywhere in memory.

	.begin		gp=undefined

_entry:
	.export		_entry
loopA:
	controlu	r1,r0,SERIAL_STAT	# Get serial status
	andi		r1,r1,0x01		# . Look at bit [0] only
	beqz		r1,loopA		# . Wait for INPUT AVAIL = 1

	controlu	r2,r0,SERIAL_RECV	# Recv serial into r2
	controlu	r0,r2,DIGITAL_WRITE	# Write r2 to digital output

loopB:
	controlu	r1,r0,SERIAL_STAT	# Get serial status
	andi		r1,r1,0x02		# . Look at bit [1] only
	beqz		r1,loopB		# . Wait for OUTPUT READY = 1
	controlu	r0,r2,SERIAL_SEND	# Send from r2

	bnez		r1,loopA		# Repeat


DIGITAL_READ:  .equ   0     # controlu   XXX,r0,DIGITAL_READ
DIGITAL_WRITE: .equ   1     # controlu   r0,XXX,DIGITAL_WRITE
HALT:          .equ   2     # controlu   r0,r0,HALT
SERIAL_STAT:   .equ   3     # controlu   XXX,r0,SERIAL_STAT
SERIAL_RECV:   .equ   4     # controlu   XXX,r0,SERIAL_RECV
SERIAL_SEND:   .equ   5     # controlu   r0,XXX,SERIAL_SEND
ENABLE_KERNEL: .equ   6     # controlu   XXX,r0,ENABLE_KERNEL  # grab csr_status; then set mode
SET_STATUS:    .equ   7     # controlu   r0,XXX,SET_STATUS     # set csr_status from reg.
TLB_DEBUG:     .equ   8     # controlu   regD,Reg1,TLB_DEBUG   # copy TLB[r1] to RegD

