	
	nop
	
	;set video to 555
	
	movei	r0,2	;1x5r5g5b
	movei	r1,$29
	movei	r2,$2a
	moveih	r1,$20	;2025 - video mode
	moveih	r2,$20	;202a - latch out
	
	st	r1,r0
	st	r2,r7
	
	;set 720*450
	
	movei	r2,2	;vload next = 2
	movei	r1,$80
	moveih	r0,$0	;lc_add = $00.80 0000
	
	movei	r3,$1a	;lc adder low
	movei	r4,$1b	;lc adder high
	movei	r5,$1c	;lc latch
	movei	r6,$24	;v load next
	moveih	r3,$20
	moveih	r4,$20
	moveih	r5,$20	;lc latch
	moveih	r6,$20	;lc latch
	
	st	r3,r0
	st	r4,r1
	st	r5,r0	;latch lc
	st	r6,r2
	
	movei	r3,$21
	movei	r0,$70
	moveih	r3,$20	;HLOAD STOP
	moveih	r0,1
	
	nop
	st	r3,r0	;HLOAD STOP = 360+8 (32 bit words)
	
	; clear screen
	
	movei	r2,$00	;destination 
	movei	r3,$00
	moveih	r2,$00
	moveih	r3,$f0	;p(0,0) = f000 0000
	
	gpci	r7,2	;next 4 + X
	br	clearLoop
	movei	r1,$33	; count		delay slot	
	movei	r0,$00	; value		delay slot
	moveih	r1,$9e	; count		delay slot
	nop		;		delay slot
		
	
	; write to ps2

	;reset
	
	;gpci	r7,2	;next 4 + X
	;br	sendToPS2
	;movei	r0,$ff	;count		delay slot	
	;nop		;delay slot
	;nop		;delay slot
	;nop		;delay slot
	
	
	;push	r0	;%d test
	
	;gpci	r7,2	;next 4 + X
	;br	drawText
	;movei	r0,ps2ResetText		;text	delay slot	
	;movei	r1,0			;x      delay slot
	;moveih	r0,>ps2ResetText	;       delay slot
	;movei	r2,17			;y      delay slot

	;rqpop		;cleanup stack
	;nop
	;nop
	;nop
	
	;scan code
	
	gpci	r7,2	;next 4 + X
	br	sendToPS2
	movei	r0,$ed	;set led		delay slot	
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	push	r0	;%d test
	
	gpci	r7,2	;next 4 + X
	br	drawText
	movei	r0,ps2CmdString	;text	delay slot	
	movei	r1,0			;x      delay slot
	moveih	r0,>ps2CmdString	;       delay slot
	movei	r2,18			;y      delay slot

	rqpop		;cleanup stack
	nop
	nop
	nop
	
	;
	
	gpci	r7,2	;next 4 + X
	br	sendToPS2
	movei	r0,$07	;3		delay slot	
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	push	r0	;%d test
	
	gpci	r7,2	;next 4 + X
	br	drawText
	movei	r0,ps2CmdString	;text	delay slot	
	movei	r1,0			;x      delay slot
	moveih	r0,>ps2CmdString	;       delay slot
	movei	r2,19			;y      delay slot

	rqpop		;cleanup stack
	nop
	nop
	nop
	
	;
	
	gpci	r7,2	;next 4 + X
	br	sendToPS2
	movei	r0,$fa	;3		delay slot	
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	push	r0	;%d test
	
	gpci	r7,2	;next 4 + X
	br	drawText
	movei	r0,ps2CmdString	;text	delay slot	
	movei	r1,0			;x      delay slot
	moveih	r0,>ps2CmdString	;       delay slot
	movei	r2,20			;y      delay slot

	rqpop		;cleanup stack
	nop
	nop
	nop
	
	gpci	r7,2	;next 4 + X
	br	sendToPS2
	movei	r0,$f4	;3		delay slot	
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	push	r0	;%d test
	
	gpci	r7,2	;next 4 + X
	br	drawText
	movei	r0,ps2CmdString	;text	delay slot	
	movei	r1,0			;x      delay slot
	moveih	r0,>ps2CmdString	;       delay slot
	movei	r2,21			;y      delay slot

	rqpop		;cleanup stack
	nop
	nop
	nop
	
	
	
	;sample scan code
		
scanTest	

	;read from keyboards

	gpci	r7,2	;next 4 + X
	br	readFromPS2
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	or	r4,r0,r0
	or	r5,r0,r0
	
	cmpeqi	r0,$12		
	brtc	noShiftLeft
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	movei	r1,scanStates
	movei	r2,1
	moveih	r1,>scanStates
	nop
	st	r1,r2	
	
noShiftLeft

	cmpeqi	r0,$59		
	brtc	noShiftRight
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	movei	r1,scanStates
	movei	r2,1
	moveih	r1,>scanStates
	nop
	st	r1,r2	
	
noShiftRight

	cmpeqi	r0,$5a		;ENTER
	brtc	noEnter
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	gpci	r7,2	;next 4 + X
	br	crCursor
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
noEnter	

	cmpeqi	r0,$66		;VKSP
	brtc	noBackSpace
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	gpci	r7,2	;next 4 + X
	br	decCursor
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
noBackSpace	
	
	cmpeqi	r0,$f0		;break
	brtc	noBreakExtend
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	gpci	r7,2	;next 4 + X
	br	readFromPS2			;get after break
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	cmpeqi	r0,$12		
	brtc	noShiftLeftRelease
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	movei	r1,scanStates
	movei	r2,0
	moveih	r1,>scanStates
	nop
	st	r1,r2	
	
noShiftLeftRelease

	cmpeqi	r0,$59		
	brtc	noRightShiftRelease
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	movei	r1,scanStates
	movei	r2,0
	moveih	r1,>scanStates
	nop
	st	r1,r2	
	
noRightShiftRelease

	
	br	scanTest			
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	

noBreakExtend	
	
	;scan to ascii
	
	movei	r1,$7f	
	movei	r2,scanToAsciiLUT	;
	and	r4,r4,r1		;only 127 bit
	moveih	r2,>scanToAsciiLUT	;
	nop
	add	r2,r2,r4		;add to lut
	nop
	rqldi	r2,0
	nop
	nop
	ld	r4			;get value from lut
	
	;check shift
	
	movei	r0,scanStates
	nop
	moveih	r0,>scanStates
	nop
	rqldi	r0,0
	nop
	nop
	ld	r0
	nop
	extri	r0,0
	swptc	r4,r4			;swap at shift

	;
	
	nop
	lsri	r4,8
	nop
	cmpeqi	r4,$fd		;sentinel for not available
	brts	noValidAscii
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	;get cursor
	
	movei	r2,cursorX
	nop
	moveih	r2,>cursorX
	nop
	rqldi	r2,0	;[x]
	rqldi	r2,1	;[y]
	nop
	ld	r1	;x
	ld	r2	;y
	
	
	
	gpci	r7,2	;next 4 + X
	br	drawChar
	or	r0,r4,r4	;v delay slot
	nop			;x delay slot
	nop			;y delay slot
	subi	r0,32		;delay slot	
	
	;cursor inc
	
	gpci	r7,2	;next 4 + X
	br	incCursor
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	
noValidAscii	
	
		
	;draw scan code for debug
	
	push	r5	;%d test	
	gpci	r7,2	;next 4 + X
	br	drawText
	movei	r0,scanCodeString	;text	delay slot	
	movei	r1,0	;x      delay slot
	moveih	r0,>scanCodeString	;       delay slot
	movei	r2,22	;y      delay slot	
	rqpop		;cleanup stack
	nop
	nop
	pop	r0
	
	;


	
	br	scanTest
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
		
end	br	end
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
ps2CmdString	
	.string	"PS2 cmd return $%x"	
	word	0	
	
scanCodeString
	
	.string	"scan code $%x"
	word	0
	
;******************************
;*
;* crCursor
;*	

crCursor

	push	r0
	push	r1
	push	r2
	push	r7
	
	movei	r2,cursorX
	nop
	moveih	r2,>cursorX
	nop
	
	rqldi	r2,1	;[y]
	nop
	nop	;x
	ld	r1	;y
	
	
	movei	r0,0	;x = 0
	addi	r1,1	;y++
	nop
	

	;store back
	
	st	r2,r0	;store x
	addi	r2,1
	nop
	st	r2,r1	;store y
	
	;
	
	gpci	r7,2	;next 4 + X
	br	cursorEvaluate
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	
		
	rqpop
	rqpop
	rqpop
	pop	r7
	pop	r2
	pop	r1
	
	rqpop
	jmpi	r7,0
	nop		;delay slot
	pop	r0	;delay slot
	nop		;delay slot
	nop		;delay slot
	
;******************************
;*
;* incCursor
;*

incCursor

	push	r0
	push	r1
	push	r7
	

	movei	r1,cursorX
	nop
	moveih	r1,>cursorX
	nop
	
	rqldi	r1,0	;[x]
	nop
	nop
	ld	r0	;x
	nop
	addi	r0,1
	nop
	st	r1,r0	;store x

	;
	
	gpci	r7,2	;next 4 + X
	br	cursorEvaluate
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	;
	
	rqpop
	rqpop
	rqpop
	pop	r7
	pop	r1
	pop	r0
	
	jmpi	r7,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot

;******************************
;*
;* decCursor
;*

decCursor

	push	r0
	push	r1
	push	r7
	

	movei	r1,cursorX
	nop
	moveih	r1,>cursorX
	nop
	
	rqldi	r1,0	;[x]
	nop
	nop
	ld	r0	;x
	nop
	subi	r0,1
	nop
	st	r1,r0	;store x

	;
	
	gpci	r7,2	;next 4 + X
	br	cursorEvaluate
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	;
	
	rqpop
	rqpop
	rqpop
	pop	r7
	pop	r1
	pop	r0
	
	jmpi	r7,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot	
	
;******************************
;*
;* cursorEvaluate
;*
;*

cursorEvaluate

	push	r0
	push	r1
	push	r2

	movei	r2,cursorX
	nop
	moveih	r2,>cursorX
	nop
	
	rqldi	r2,0	;[x]
	rqldi	r2,1	;[y]
	nop
	ld	r0	;x
	ld	r1	;y
	
	;-- test x < 0 (signed)
	
	cmplosi	r0,0
	brtc	cursorEvaluateXGT0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	movei	r0,89	;x last ps
	subi	r1,1	;y--

	;-- test x < 89 (unsigned)
	
cursorEvaluateXGT0

	cmploi	r0,90
	brts	cursorEvaluateLTMAX
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	movei	r0,0	;x = 0
	addi	r1,1	;y++
	nop

cursorEvaluateLTMAX

	;-- test y < 0 (signed)
	
	cmplosi	r1,0
	brtc	cursorEvaluateYGT0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	movei	r1,17	
	nop
	
	;-- test y < 17 (unsigned)
	
cursorEvaluateYGT0

	cmploi	r1,17
	brts	cursorEvaluateYLTMAX
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	movei	r1,0	;y=0

cursorEvaluateYLTMAX

	;store back
	
	st	r2,r0	;store x
	addi	r2,1
	nop
	st	r2,r1	;store y
	
	;
	
	rqpop
	rqpop
	rqpop
	pop	r2
	pop	r1
	pop	r0
	

	jmpi	r7,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	
cursorX	
	word	0
cursorY	
	word	0
	
;******************************
;*
;* readFromPS2
;*
;* return
;*
;* r0 = value 
;*	

readFromPS2

	push	r1
	push	r2
	push	r3
	
	movei	r2,$30
	nop
	moveih	r2,$20	;read 2030 - ctrl ps data
	nop
	
readFromPS2ScanTest	

	rqldi	r2,0
	nop
	nop
	ld	r0
	nop
	extri	r0,8	;data in
	brtc	readFromPS2ScanTest
	movei	r3,0	;delay slot
	nop		;delay slot
	moveih	r3,$1	;$100 = accept data 	delay slot
	nop		;delay slot
	
	st	r2,r3	;write 2030 - accept data	
	moveih	r0,$0	;clear upper part of value
	
	rqpop
	rqpop
	rqpop
	pop	r3
	pop	r2
	pop	r1
	
	jmpi	r7,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
;******************************
;*
;* sendToPS2
;*
;* r0 = byte to send (upper 
;*
;* return
;*
;* r0 = status $FA is ok
;*
	
sendToPS2

	push	r1
	push	r2
	
	movei	r2,$30
	nop
	moveih	r2,$20	;read 2030 - ctrl ps data
	nop
	
	;check all write are finished

waitWriteFifoEmpty
	
	rqldi	r2,0
	nop
	nop
	ld	r1	;     a  9 8 76543210
			;get WF	WR A DDDDDDDD	
			;
			; A = READ DATA VALID
			;WR = WRITE READY
			;WF = WRITE FINISH
		
	nop	
	
	extri	r1,$a
	brtc	waitWriteFifoEmpty
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	
	;send data
	moveih	r0,$2	;set write data to ps2 (lower data are send byte)
	nop
	st	r2,r0
	nop
	
	;wait for ack data
	
waitReadFifoData

	rqldi	r2,0
	nop
	nop
	ld	r0	;     
	nop
	extri	r0,$8
	brtc	waitReadFifoData
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	moveih	r1,1	;remove scan code from buffer (get next if available)
	moveih	r0,0	;clear upper data of result
	st	r2,r1

	rqpop
	rqpop
	jmpi	r7,0
	pop	r2	;delay slot
	pop	r1	;delay slot
	nop		;delay slot
	nop		;delay slot
	
		
;******************************
;*
;* drawText
;*
;* r0		text
;* r1 	 	cursor x
;* r2		cursor y

drawText

	push	r0
	push	r1
	push	r4
	push	r6
	push	r7
	
	getsp	r6,5

	or	r4,r0,r0	;text address to r4
	nop
	
textLoop

	rqldi	r4,0
	nop
	nop
	ld	r0
	nop
	cmpeqi	r0,0
	brts	textLoopEnd
	addi	r4,1	;next char 	delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	cmpeqi	r0,37	;%
	brts	textProcessCmd
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	gpci	r7,2	;next 4 + X
	br	drawChar
	subi	r0,32	;map visible	delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	addi	r1,1
textProcessCmdReturn
	
	br	textLoop
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	
textLoopEnd	
	
	
	rqpop
	rqpop
	rqpop
	pop	r7
	pop	r6
	pop	r4
	
	rqpop
	rqpop
	jmpi	r7,0
	pop	r1	;delay slot
	pop	r0	;delay slot
	nop		;delay slot
	nop		;delay slot
	
;processing cmd string	
	
textProcessCmd

	rqldi	r4,0	;get next char
	nop
	nop
	ld	r0	
	nop
	
	cmpeqi	r0,37	;% again so print it
	brts	textProcessCmdProzent
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	cmpeqi	r0,99	;c -> char
	brts	textProcessCmdChar
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	cmpeqi	r0,120	;x -> hex
	brts	textProcessCmdHex
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	cmpeqi	r0,117	;u -> unsigned dec
	brts	textProcessCmdUDec
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	

	br	textProcessCmdReturn
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
;%%	
	
textProcessCmdProzent
	
	gpci	r7,2	;next 4 + X
	br	drawChar
	subi	r0,32	;map visible	delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	addi	r1,1
	
	br	textProcessCmdReturn
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
;%c	
	
textProcessCmdChar
	
	rqldi	r6,0
	addi	r6,1	;next value if exist
	nop
	ld	r0	
	
	gpci	r7,2	;next 4 + X
	br	drawChar
	subi	r0,32	;map visible	delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	addi	r4,1
	addi	r1,1
	
	br	textProcessCmdReturn
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot	
	
;%x	
	
textProcessCmdHex	
	
	push	r5
	push	r3
	
	rqldi	r6,0
	addi	r6,1	;next value if exist
	nop
	ld	r5
	
	movei	r3,3	;3 hex digits

textProcessCmdHexLoop	
	
	or	r0,r5,r5
	nop
	lsri	r0,12
	nop
	
	cmploi	r0,10
	brts	textProcessCmdHexNoABCDEF
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	addi	r0,7
	
textProcessCmdHexNoABCDEF	
	
	gpci	r7,2	;next 4 + X
	br	drawChar
	addi	r0,16	;map visible +48-32	delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
				
	subi	r3,1	
	brts	textProcessCmdHexLoop
	addi	r1,1	;x++		delay slot
	muli	r5,16	;r5 << 4	delay slot
	nop		;delay slot
	nop		;delay slot
	
	rqpop
	rqpop
	br	textProcessCmdReturn
	pop	r3	;delay slot
	pop	r5	;delay slot		
	addi	r4,1	;next input char	delay slot
	nop		;delay slot
	
;%u	
	
textProcessCmdUDec

	push	r5
	push	r4
	push	r3
	push	r7
	
	
	rqldi	r6,0
	addi	r6,1	;next value if exist
	movei	r4,$cd
	ld	r5
	moveih	r4,$cc	;r4 = $cccd
	
		
	movei	r7,4		;5 dec digits	
	
textProcessCmdUDecDecodeDigit
	
	or	r3,r5,r5
	mul	r5,r5,r4	;result = value * $cccd
	nop
	gmulhi	r5		;result >> 16
	nop
	lsrqi	r5,r5,3		;result >> 3
	nop
	or	r0,r5,r5
	nop
	muli	r0,10
	
		
	subi	r7,1
	brts	textProcessCmdUDecDecodeDigit
	nop			;delay slot
	sub	r0,r3,r0	;delay slot
	nop			;delay slot
	push	r0		;push digit ;delay slot
	
	;
	
	movei	r3,3		;4 dec digits with leading zero test	
	movei	r4,0		;leading zero
	
textProcessCmdUDecPrintDigit	
	
	rqpop	
	nop
	nop
	pop	r0
	nop
	cmpeq	r4,r0
	brts	textProcessCmdUDecSkipLeadingZero	;skip leading zero
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	movei	r4,$ff	;illegal value after first not leading zero
	
	
	gpci	r7,2	;next 4 + X
	br	drawChar
	addi	r0,16	;map visible +48-32	delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	addi	r1,1
	
textProcessCmdUDecSkipLeadingZero
	
	subi	r3,1
	brts	textProcessCmdUDecPrintDigit
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot

	;last digit
	
	rqpop	
	nop
	nop
	pop	r0
	gpci	r7,2	;next 4 + X
	br	drawChar
	addi	r0,16	;map visible +48-32	delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	addi	r1,1
	
	rqpop
	nop
	nop
	pop	r7
	
	rqpop
	rqpop
	rqpop
	pop	r3
	pop	r4	
	pop	r5	
	
	br	textProcessCmdReturn
	nop		;delay slot
	nop		;delay slot	
	addi	r4,1	;next input char	delay slot
	nop		;delay slot
	
;******************************
;*
;* drawChar
;* 
;* r0 	 	value
;* r1		x 	
;* r2		y
;*
;* trash r0

drawChar	

	push	r1
	push	r2
	push	r3
	push	r4
	push	r5
	push	r6
	push	r7
	
	;destination base (sram base 0)
	
	muli	r1,8
	
	movei	r4,$00	;destination 
	movei	r5,$00
	moveih	r4,$00
	moveih	r5,$f0	;p(0,0) = f000 0000
	
	add	r4,r4,r1
	addtqi	r5,r5,0
	
	movei	r1,$00
	nop
	moveih	r1,$2d	;720*16
	nop
	
	mul	r1,r1,r2;*y
	gmulhi	r2
	
	add	r4,r4,r1
	addt	r5,r5,r2
		
	;font source
	
	movei	r2,$68	;source 
	movei	r3,$01
	moveih	r2,$7f
	moveih	r3,$e0	;p(0,0) = e001 7f68 (color) oder e001 5068
	
	muli	r0,8		;value * 8
	nop
	add	r2,r2,r0	;source 
	addtqi	r3,r3,0		;

	;
	movei	r7,0
	movei	r0,15		;16 [y]
dcYloop	
	
	esadr	r3,r2
	erqldi	0
	erqldi	1
	erqldi	2
	erqldi	3
	erqldi	4
	erqldi	5
	erqldi	6
	erqldi	7
	
	addi	r2,8		
	addtqi	r3,r3,0	
	
	movei	r1,7		;8 [x]

dcXloop
	
	eld	r6
	nop
	extri	r6,15
	movets	r6,r6,r7	;t = 1 r6=r6 else r6=r7 (0)
		
	esadr	r5,r4
	est	r6,0
	
	subi	r1,1		;x-
	brts	dcXloop
	addi	r4,1		;delay slot
	addtqi	r5,r5,0		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	;
		
	movei	r1,$c8
	nop
	moveih	r1,$2		;r1 = 720-8
	nop
	
	add	r4,r4,r1	;dest + 720-8
	addtqi	r5,r5,0		;
	
	movei	r1,$e8
	nop
	moveih	r1,$2		;r1 = 752-8
	nop	
	
	subi	r0,1		;y-
	brts	dcYloop
	add	r2,r2,r1	;source + 752-8		delay slot
	addtqi	r3,r3,0		;			delay slot
	nop			;delay slot
	nop			;delay slot
	
	;
	
	rqpop
	rqpop
	rqpop
	pop	r7
	pop	r6
	pop	r5
	
	rqpop
	rqpop
	nop
	pop	r4
	pop	r3
	
	rqpop
	rqpop
	jmpi	r7,0
	pop	r2	;delay slot
	pop	r1	;delay slot
	nop		;delay slot
	nop		;delay slot

;******************************
;*
;* r0 	 	value
;* r1		count-1 (8 words))
;* r3:r2	dest
;
memset

clearLoop

	esadr	r3,r2
	est	r0,0
	est	r0,1
	est	r0,2
	est	r0,3
	est	r0,4
	est	r0,5
	est	r0,6
	est	r0,7
	subi	r1,1
	brts	clearLoop
	addi	r2,8	;delay slot
	addtqi	r3,r3,0	;delay slot
	nop		;delay slot
	nop		;delay slot
	
	jmpi	r7,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	
scanStates

	word	0
	
scanToAsciiLUT	
	
	.stringb "вв"   ; 00 00 = 'в'
	.stringb "вв"   ; 01 00 = 'в'
	.stringb "вв"   ; 02 00 = 'в'
	.stringb "вв"   ; 03 00 = 'в'
	.stringb "вв"   ; 04 00 = 'в'
	.stringb "вв"   ; 05 00 = 'в'
	.stringb "вв"   ; 06 00 = 'в'
	.stringb "вв"   ; 07 00 = 'в'
	.stringb "вв"   ; 08 00 = 'в'
	.stringb "вв"   ; 09 00 = 'в'
	.stringb "вв"   ; 0A 00 = 'в'
	.stringb "вв"   ; 0B 00 = 'в'
	.stringb "вв"   ; 0C 00 = 'в'
	.stringb "вв"   ; 0D 00 = 'в'
	.stringb "``"   ; 0E 0E = '`'
	.stringb "вв"   ; 0F 00 = 'в'
	.stringb "вв"   ; 10 00 = 'в'
	.stringb "вв"   ; 11 00 = 'в'
	.stringb "вв"   ; 12 00 = 'в'
	.stringb "вв"   ; 13 00 = 'в'
	.stringb "вв"   ; 14 00 = 'в'
	.stringb "Qq"   ; 15 15 = 'Q'
	.stringb "!1"   ; 16 16 = '1'
	.stringb "вв"   ; 17 00 = 'в'
	.stringb "вв"   ; 18 00 = 'в'
	.stringb "вв"   ; 19 00 = 'в'
	.stringb "Yy"   ; 1A 1A = 'Z'
	.stringb "Ss"   ; 1B 1B = 'S'
	.stringb "Aa"   ; 1C 1C = 'A'
	.stringb "Ww"   ; 1D 1D = 'W'
	.stringb "╟2"   ; 1E 1E = '2'
	.stringb "вв"   ; 1F 00 = 'в'
	.stringb "вв"   ; 20 00 = 'в'
	.stringb "Cc"   ; 21 21 = 'C'
	.stringb "Xx"   ; 22 22 = 'X'
	.stringb "Dd"   ; 23 23 = 'D'
	.stringb "Ee"   ; 24 24 = 'E'
	.stringb "$4"   ; 25 25 = '4'
	.stringb "ї3"   ; 26 26 = '3'
	.stringb "вв"   ; 27 00 = 'в'
	.stringb "вв"   ; 28 00 = 'в'
	word	$2020  	; 29 29 = ' '
	.stringb "Vv"   ; 2A 2A = 'V'
	.stringb "Ff"   ; 2B 2B = 'F'
	.stringb "Tt"   ; 2C 2C = 'T'
	.stringb "Rr"   ; 2D 2D = 'R'
	.stringb "%5"   ; 2E 2E = '5'
	.stringb "вв"   ; 2F 00 = 'в'
	.stringb "вв"   ; 30 00 = 'в'
	.stringb "Nn"   ; 31 31 = 'N'
	.stringb "Bb"   ; 32 32 = 'B'
	.stringb "Hh"   ; 33 33 = 'H'
	.stringb "Gg"   ; 34 34 = 'G'
	.stringb "Zz"   ; 35 35 = 'Y'
	.stringb "&6"   ; 36 36 = '6'
	.stringb "вв"   ; 37 00 = 'в'
	.stringb "вв"   ; 38 00 = 'в'
	.stringb "вв"   ; 39 00 = 'в'
	.stringb "Mm"   ; 3A 3A = 'M'
	.stringb "Jj"   ; 3B 3B = 'J'
	.stringb "Uu"   ; 3C 3C = 'U'
	.stringb "/7"   ; 3D 3D = '7'
	.stringb "(8"   ; 3E 3E = '8'
	.stringb "вв"   ; 3F 00 = 'в'
	.stringb "вв"   ; 40 00 = 'в'
	.stringb ",,"   ; 41 41 = ','
	.stringb "Kk"   ; 42 42 = 'K'
	.stringb "Ii"   ; 43 43 = 'I'
	.stringb "Oo"   ; 44 44 = 'O'
	.stringb "=0"   ; 45 45 = '0'
	.stringb ")9"   ; 46 46 = '9'
	.stringb "вв"   ; 47 00 = 'в'
	.stringb "вв"   ; 48 00 = 'в'
	.stringb ".."   ; 49 49 = '.'
	.stringb "//"   ; 4A 4A = '/'
	.stringb "Ll"   ; 4B 4B = 'L'
	.stringb ";;"   ; 4C 4C = ';'
	.stringb "Pp"   ; 4D 4D = 'P'
	.stringb "--"   ; 4E 4E = '-'
	.stringb "вв"   ; 4F 00 = 'в'
	.stringb "вв"   ; 50 00 = 'в'
	.stringb "вв"   ; 51 00 = 'в'
	.stringb "  "   ; 52 52 = ' '
	.stringb "вв"   ; 53 00 = 'в'
	.stringb "[["   ; 54 54 = '['
	.stringb "=="   ; 55 55 = '='
	.stringb "вв"   ; 56 00 = 'в'
	.stringb "вв"   ; 57 00 = 'в'
	.stringb "вв"   ; 58 00 = 'в'
	.stringb "вв"   ; 59 00 = 'в'
	.stringb "вв"   ; 5A 00 = 'в'
	.stringb "]]"   ; 5B 5B = ']'
	.stringb "вв"   ; 5C 00 = 'в'
	.stringb "\\"   ; 5D 5D = '\'
	.stringb "вв"   ; 5E 00 = 'в'
	.stringb "вв"   ; 5F 00 = 'в'
	.stringb "вв"   ; 60 00 = 'в'
	.stringb "вв"   ; 61 00 = 'в'
	.stringb "вв"   ; 62 00 = 'в'
	.stringb "вв"   ; 63 00 = 'в'
	.stringb "вв"   ; 64 00 = 'в'
	.stringb "вв"   ; 65 00 = 'в'
	.stringb "вв"   ; 66 00 = 'в'
	.stringb "вв"   ; 67 00 = 'в'
	.stringb "вв"   ; 68 00 = 'в'
	.stringb "11"   ; 69 69 = '1'
	.stringb "вв"   ; 6A 00 = 'в'
	.stringb "44"   ; 6B 6B = '4'
	.stringb "77"   ; 6C 6C = '7'
	.stringb "вв"   ; 6D 00 = 'в'
	.stringb "вв"   ; 6E 00 = 'в'
	.stringb "вв"   ; 6F 00 = 'в'
	.stringb "00"   ; 70 70 = '0'
	.stringb "вв"   ; 71 00 = 'в'
	.stringb "22"   ; 72 72 = '2'
	.stringb "55"   ; 73 73 = '5'
	.stringb "66"   ; 74 74 = '6'
	.stringb "88"   ; 75 75 = '8'
	.stringb "вв"   ; 76 00 = 'в'
	.stringb "вв"   ; 77 00 = 'в'
	.stringb "вв"   ; 78 00 = 'в'
	.stringb "вв"   ; 79 00 = 'в'
	.stringb "33"   ; 7A 7A = '3'
	.stringb "вв"   ; 7B 00 = 'в'
	.stringb "**"   ; 7C 7C = '*'
	.stringb "99"   ; 7D 7D = '9'
	.stringb "вв"   ; 7E 00 = 'в'
	.stringb "вв"   ; 7F 00 = 'в'