	
	;***********************************************************************
	;* reset start
	;***********************************************************************
	
	.org	0
	
	nop
	cli
	br	userLevel
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	.org	16
	
	;***********************************************************************
	;* irq code start
	;***********************************************************************
	
irqLevel
	cli
	
	;save all register
	
	push	r0
	push	r1
	push	r2
	push	r3
	push	r4
	push	r5
	push	r6
	push	r7
	
	;clear irq memory
	
	movei	r7,UcIrqMemory
	movei	r0,0	;
	moveih	r7,>UcIrqMemory	;irq memory
	nop
	st	r7,r0
	
	;
		
	epushsadrl	;store esadr on stack
	epushsadrh
	nop
	nop
	nop
	
	rqpop
	rqpop
	nop
	pop	r0
	pop	r1	;r1:r0 = esadr 
	
	;draw to hex	
			
	gpci	r7,2		;next 4 + X
	br	longToHexMap
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
		
	gpci	r7,2		;next 4 + X
	br	drawHex
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot

	;restore all used register and go back
	
	rqpop		
	rqpop	
	rqpop	
	pop	r7
	pop	r6
	pop	r5
	
	rqpop		
	rqpop	
	rqpop	
	pop	r4
	pop	r3
	pop	r2
	
	rqpop		
	rqpop	
	rti
	pop	r1	;delay slot
	pop	r0	;delay slot
	nop		;delay slot
	sei		;delay slot

	;*********************************************************************	
	; includes
	;*********************************************************************	

	.incdir	"F:\git\goldmomo_endlos\assembler_files\ucore\"
	.include "include\ucore_ctrl.i"
	
	;***********************************************************************
	;* user code start
	;***********************************************************************
	
userLevel
	
	gpci	r7,2
	br	setupVideo
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot

	; 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
		
	;setup timer 1 to esadr -> hex display
	
	movei	r7,UcIrqMsk
	movei	r0,1	;irq msk = 1 (only use timer 1)
	moveih	r7,>UcIrqMsk	;
	movei	r2,5	;start with autoreload
	
	movei	r3,$80	;every 200000 cycles irq
	movei	r4,$1e
	moveih	r3,$84
	moveih	r4,$00	
	
	st	r7,r0	;store irq_msk
	addi	r7,2
	nop
	st	r7,r3	;store low value
	addi	r7,1
	nop
	st	r7,r4	;store high value
	addi	r7,1
	nop
	st	r7,r2	;store start / stop / reload
	
	;enable irq
	
	sei
	
	;draw header
		
	gpci	r7,2	;next 4 + X
	br	drawText
	movei	r0,t1	;text	delay slot	
	movei	r1,0	;x      delay slot
	moveih	r0,>t1	;       delay slot
	movei	r2,0	;y      delay slot
	
	;test sram
		
	;$f004 f1a0 - $f00f ffff
	
	movei	r3,$a0
	movei	r4,$04
	moveih	r3,$f1
	moveih	r4,$f0
	
	movei	r5,$ff
	movei	r6,$0f
	moveih	r5,$ff
	moveih	r6,$f0
	
	movei	r1,0	;x
	movei	r2,2	;y
		
	gpci	r7,2	;next 4 + X
	br	memTest
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	;test sdram
		
	;$d000 0000 - $d3ff ffff
	
	movei	r3,$00
	movei	r4,$00
	moveih	r3,$00
	moveih	r4,$d0
	
	movei	r5,$ff
	movei	r6,$ff
	moveih	r5,$ff
	moveih	r6,$d3
	
	movei	r1,0	;x
	movei	r2,5	;y
		
	gpci	r7,2	;next 4 + X
	br	memTest
	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
	
	
t1	
	.string	"ucore external memory test (video is set to 720x450x15)"
	word	0
	
;******************************
;*
;* memTest
;*
;* r1 	 	text x 
;* r2		text y
;*
;* r4:r3	startaddress
;* r6:r5	stopaddress
;*		
	
memTest

	push	r0
	push	r1
	push	r2
	push	r3
	push	r4
	push	r5
	push	r6
	push	r7
	
	;store x & y for pass fail result string
	
	push	r1
	push	r2
	
	;draw string
	
	push	r5	;push values on stack
	push	r6
	push	r3
	push	r4

	movei	r0,memTestString
	nop
	moveih	r0,>memTestString
	
	gpci	r7,2	;next 4 + X
	br	drawText
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	rqpop	;cleanup message stack
	rqpop
	rqpop
	rqpop
	nop
	nop
	nop
	
	;real memtest
	
	;write first

	push	r3
	push	r4	;save start address
	
	movei	r0,$d5
	nop
	moveih	r0,$dc	;value to write
wr	
	esadr	r4,r3
	est	r0,0	;write 
	muli	r0,139
	
	cmpeq	r3,r5
	brtc	wr
	xor	r0,r0,r3	;delay slot
	addi	r3,1		;delay slot
	addtqi	r4,r4,0		;delay slot
	nop			;delay slot
	
	cmpeq	r4,r6
	brtc	wr
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	
	rqpop		;recover start address
	rqpop
	nop
	pop	r4
	pop	r3
	
	;read
	
	movei	r0,$d5
	nop
	moveih	r0,$dc	;value to write
rd	
	esadr	r4,r3
	erqldi	0	;write 
	eld	r2
	nop
	cmpeq	r2,r0
	brtc	memTestFail
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot

	muli	r0,139
	
	cmpeq	r3,r5
	brtc	rd
	xor	r0,r0,r3	;delay slot
	addi	r3,1		;delay slot
	addtqi	r4,r4,0		;delay slot
	nop			;delay slot
	
	cmpeq	r4,r6
	brtc	rd
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	
	;result print
	
	movei	r0,memTestStringPass
	nop
	moveih	r0,>memTestStringPass
	nop
	br	skipFailString
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	nop	;delay slot
	
memTestFail
	movei	r0,memTestStringFail
	nop
	moveih	r0,>memTestStringFail
	nop
skipFailString	
	
	
	rqpop
	rqpop
	nop
	pop	r2	;y
	pop	r1	;x
	addi	r2,1	;y+1
	

	push	r3	;last address (fail address on stack)
	push	r4

	gpci	r7,2	;next 4 + X
	br	drawText
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	rqpop		;cleanup
	rqpop
	nop
	nop
	nop

	
	rqpop		
	rqpop	
	rqpop	
	pop	r7
	pop	r6
	pop	r5
	
	rqpop		
	rqpop	
	rqpop	
	pop	r4
	pop	r3
	pop	r2
	
	rqpop
	rqpop
	jmpi	r7,0
	pop	r1	;delay slot
	pop	r0	;delay slot
	nop		;delay slot
	nop		;delay slot
	
memTestString

	.string	"Memtest start at $%x%x stop at $%x%x"
	word	0
	
memTestStringPass

	.string "Memtest PASS"
	word	0
	
memTestStringFail

	.string	"Memtest FAIL at $%x%x"
	word	0
	
	
;******************************
;*
;* drawText
;*
;* r0		text
;* r1 	 	x
;* r2		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
	
	

	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
	
;******************************
;*
;* drawChar
;* 
;* r0 	 	value
;* r1		x 	
;* r2		y
;*
;* trash r0

drawChar	

	push	r1
	push	r2
	push	r3
	push	r4
	push	r5
	push	r6
	
	;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,$50
	moveih	r3,$e0	;p(0,0) = e001 5068
	
	muli	r0,8		;value * 8
	nop
	add	r2,r2,r0	;source 
	addtqi	r3,r3,0		;

	;

	movei	r0,15		;16 [y]
dcYloop	
	
	movei	r1,7		;8 [x]
dcXloop
	esadr	r3,r2
	erqldi	0
	eld	r6
	nop
	
	extri	r6,15
	brts	dcNoTransp
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	
	movei	r6,0
	
dcNoTransp	
	
		
	esadr	r5,r4
	est	r6,0
	
	subi	r1,1		;x-
	brts	dcXloop
	addi	r4,1		;delay slot
	addtqi	r5,r5,0		;delay slot
	addi	r2,1		;delay slot
	addtqi	r3,r3,0		;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	r6
	pop	r5
	pop	r4
	
	rqpop
	nop
	nop
	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
	
;******************************
;* longToHexMap
;*
;* r0 = high value
;* r1 = low value
;* 	

longToHexMap

	push	r0
	push	r1
	push	r2
	push	r3
	push	r4
	push	r5
	push	r6
	push	r7
	
	
	movei	r2,hexMap
	movei	r7,hex7seg
	movei	r5,$f		;msk
	moveih	r2,>hexMap	;8 h
	moveih	r7,>hex7seg
	
	movei	r6,1

highLow
	
	or	r3,r0,r0
	or	r4,r0,r0
	lsri	r3,12
	lsri	r4,8
	and	r3,r3,r5
	and	r4,r4,r5
	
	add	r3,r3,r7
	add	r4,r4,r7
	rqldi	r3,0
	rqldi	r4,0
	nop
	ld	r3
	ld	r4
	nop
	
	st	r2,r3		;8;4
	addi	r2,1		
	nop
	st	r2,r4		;7;3
	addi	r2,1
	
	or	r3,r0,r0
	and	r4,r0,r5
	lsri	r3,4
	nop
	and	r3,r3,r5
	nop
	
	add	r3,r3,r7
	add	r4,r4,r7
	rqldi	r3,0
	rqldi	r4,0
	nop
	ld	r3
	ld	r4
	nop
	
	st	r2,r3		;6;2
	addi	r2,1		
	nop
	st	r2,r4		;5;1
	addi	r2,1
	
	or	r0,r1,r1	;next 4 digits
	subi	r6,1
	brts	highLow
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	rqpop	
	rqpop	
	rqpop	
	pop	r7
	pop	r6
	pop	r5
	
	rqpop	
	rqpop	
	rqpop	
	pop	r4
	pop	r3
	pop	r2
	
	rqpop	
	rqpop	
	nop
	pop	r1
	pop	r0
	
	jmpi	r7,0
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
		
 ;--     0
 ;--    ###
 ;--  5 # # 1
 ;--    #6#
 ;--  4 # # 2
 ;--    ###
 ;--     3	

hex7seg			
	
	word	%00111111 ; 0		
	word	%00000110 ; 1			
	word	%01011011 ; 2		
	word	%01001111 ; 3		
	word	%01100110 ; 4		
	word	%01101101 ; 5		
	word	%01111101 ; 6		
	word	%00000111 ; 7		
	word	%01111111 ; 8		
	word	%01101111 ; 9		
	word	%01110111 ; A		
	word	%01111100 ; b		
	word	%00111001 ; C		
	word	%01011110 ; d		
	word	%01111001 ; E		
	word	%01110001 ; F	
	
;******************************
;* drawHex
;* 
;*
;* 

drawHex	

	push	r0
	push	r1
	push	r2
	push	r3
	push	r4

	movei	r0,hexMap
	movei	r1,Uc7SegHex76
	moveih	r0,>hexMap
	moveih	r1,>Uc7SegHex76		;r1 = (hex7:hex6)
	
	movei	r2,0

drawHexLoop

	rqldi	r0,0
	rqldi	r0,1
	addi	r0,2		
	ld	r3		;hex n
	ld	r4		;hex n + 1
	cmplo	r3,r3		;set t = 0 (removed swp cmd now swptc have to used as replacment)
	swptc	r3,r3
	nop
	or	r4,r4,r3
	nop
	st	r1,r4
	subi	r1,1

	cmpeqi	r2,6
	brtc	drawHexLoop
	addi	r2,2		;delay slot (t is not used so hazard can ignored)
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	rqpop	
	rqpop	
	rqpop	
	pop	r4
	pop	r3
	pop	r2
	
	rqpop	
	rqpop	
	nop
	pop	r1
	pop	r0
	
	jmpi	r7,0
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
hexMap	word	$10	;h0
	word	$11
	word	0
	word	0
	word	0
	word	0
	word	0
	word	0	;h7	
	
;******************************
;*
;* setupVideo
;*

setupVideo

	
	movei	r0,videoDefault		;source
	movei	r1,UcTimer2Ctrl		;dest-1
	moveih	r0,>videoDefault
	moveih	r1,>UcTimer2Ctrl			
	movei	r2,26			;27-1
	
setupVideoLoop

	rqldi	r0,0
	addi	r0,1
	addi	r1,1
	ld	r3
	subi	r2,1
	brts	setupVideoLoop
	st	r1,r3	;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
		
	jmpi	r7,0
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot	

;*********************************************************************
;* data
;*********************************************************************
	
videoDefault		;AD NAME					NEW				DEFAULT
	
	word	$0001	;10 VIDEO_ON					1				1 (ON)
	word	$0097	;11 HSYNC (D = 11 .. 0)				152-1				152-1
	word	$017f	;12 HSTART (D = 11 .. 0)			384-1				384-1
	word	$017d	;13 HMEMSTART (D = 11 .. 0)			382-1				382-1
	word	$071f	;14 HSTOP (D = 11 .. 0)				1824-1				1824-1
	word	$076f	;15 HTOTAL (D = 11 .. 0)			1904-1				1904-1
	word	$0002	;16 VSYNC (D = 10 .. 0)				3-1				3-1
	word	$001e	;17 VSTART (D = 10 .. 0)			31-1				31-1
	word	$03a2	;18 VSTOP  (D = 10 .. 0)			931-1				931-1
	word	$03a3	;19 VTOTAL  (D = 10 .. 0)			932-1				932-1
	word	$0000	;1a LC ADDER LOW				$0000				$0000
	word	$0080	;1b LC ADDER HIGH				$0080				$0040	
	word	$0000	;1c LC ADDER LATCH				X				X
	word	$0000	;1d LC START (10 .. 0) Pixeloffset		$0000				$0000	
	word	$0000	;1e						X				X
	word	$0000	;1f						X				X
	word	$0008	;20 HLOADSTART 					8				8
	word	$0170	;21 HLOADSTOP					360+8				180+8
	word	$001e	;22 VLOADSTART 					31-1				31-1
	word	$03a2	;23 VLOADSTOP 					931-1				931-1
	word	$0002	;24 VLOADNEXT					2				4
	word	$0000	;25 MEM_STARTADR_store low			$0000				$0000
	word	$f000	;26 MEM_STARTADR_store high			$f000				$f000
	word	$0000	;27 MEM_LINEOFFSET_store low			$0000				$0000
	word	$0000	;28 MEM_LINEOFFSET_store high			$0000				$0000
	word	$0002	;29 MEM_MODE_store 				2				2 (X1R5G5B5X1R5G5B5)
	word	$0000	;2a LATCH MEM_STARTADR/LINEOFFSET/MODE		X				X	
	