;note also test for dexti (decoder immediate extend (used for est))	
	
;*********************************************************************	
; includes
;*********************************************************************	

	.incdir	"F:\git\goldmomo_endlos\assembler_files\ucore\"
	.include "include\ucore.i"
	.include "include\ucore_ctrl.i"

;*********************************************************************	
; sd header is placed here
;*********************************************************************	

	.include "boot_level_sdHeader.s"

;*********************************************************************
;* start at address 32 here
;*********************************************************************

;frame buffer 0		81000	d0000000		720*450 32bit
;test pic	      1296000   d009e340		720*450 32bit
;hcore program			d013c680

	.def	frame		$d0000000
	.def	testPic		$d009e340
	.def	hcoreStartPc	$d013c680

start	
	cli
	
	;video off at sd card load
	
	movei	r1,0				;video off
	dexti	>UcVideoRHCountWVideoOn
	movei	r0,UcVideoRHCountWVideoOn
	nop
	st	r0,r1
	
	;load blocks
	
	movei	r0,startBlock
	nop
	moveih	r0,>startBlock
	nop
	rqldi	r0,0	;low
	rqldi	r0,1	;high
	nop
	ld	r2
	ld	r3
	
	addi	r2,64	;64 blocks offset (ucore code/data)
	addtqi	r3,r3,0
	
	;read blocks
	
	movei	r1,>>testPic
	movei	r0,testPic
	moveih	r1,>>>testPic
	moveih	r0,>testPic	;dest start
	
	movei	r6,sdCardReadBlocks
	nop
	moveih	r6,>sdCardReadBlocks
	
	gpci	r7,2		;
	jmpi	r6,0
	movei	r4,$ff		;4096 blocks-1	delay slot
	nop			;delay slot
	moveih	r4,$0f		;delay slot
	nop			;delay slot
	
	;select hcore 0..4 address 0 (set pc)
	
	movei	r5,0		;hcore
	
hcoreSetPCs	
	
	dexti	>UcHcoreSel
	movei	r0,UcHcoreSel	;4:4 sel:adr
	dexti	2
	mulqi	r1,r5,0		;sel 0..4
	nop
	addqi	r1,r1,0		;adr = 0 (set PC)
	nop
	st	r0,r1
	
	;write
	
	dexti	>UcHcoreDataHigh
	movei	r0,UcHcoreDataHigh	
	dexti	>>>hcoreStartPc	
	movei	r1,>>hcoreStartPc	;high	
	dexti	>hcoreStartPc	
	movei	r2,hcoreStartPc		;low
	stinc	r0,r1
	nop
	stinc	r0,r2

	;write to hcore
	
	dexti	>UcHcoreCtrl
	movei	r0,UcHcoreCtrl
	movei	r1,3		;we
	nop
	st	r0,r1
	
	cmpeqi	r5,4
	brtc	hcoreSetPCs
	addi	r5,1		;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;setup video (760x450x32)
	
	gpci	r7,2		;next 4 + X
	br	setupVideo
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;clear frame
	
	dexti	$9e
	movei	r0,$33
	movei	r1,0
	gpci	r7,2
	br	efill16
	movei	r3,>>frame
	movei	r2,frame
	moveih	r3,>>>frame
	moveih	r2,>frame	;dest start
	
	;release hcores reset
	
	dexti	>UcHcoreCtrl
	movei	r0,UcHcoreCtrl
	movei	r1,0		;reset to 0
	nop
	st	r0,r1
	
	
	
end	br	end	
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
;*********************************************************************
;* functions
;*********************************************************************	
	
	;standard
	
	.include	"components\sdCardLoader.s"	
	
;******************************
;*
;* efill16
;*
;* r0	 = count - 1
;* r1 	 = value to fill
;* r3:r2 = dest address
;*
	
efill16
	nop
	
clearFBLoop
	esadr	r3,r2	;destination
	addi	r2,8            ;delay slot
	addtqi	r3,r3,0         ;delay slot	
	est	r1,0
	est	r1,1
	est	r1,2
	est	r1,3
	est	r1,4
	est	r1,5
	est	r1,6
	est	r1,7
	esadr	r3,r2	;destination
	est	r1,0
	est	r1,1
	est	r1,2
	est	r1,3
	est	r1,4
	est	r1,5
	subi	r0,1
	brts	clearFBLoop
	addi	r2,8            ;delay slot
	addtqi	r3,r3,0         ;delay slot	
	est	r1,6		;delay slot
	est	r1,7            ;delay slot

	jmpi	r7,0
	nop		;delay slot
	nop		;delay slot	
	nop		;delay slot
	nop		;delay slot

;******************************
;*
;* 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									
	
	word	$0001	;10 VIDEO_ON					1				
	word	$0097	;11 HSYNC (D = 11 .. 0)				152-1				
	word	$017f	;12 HSTART (D = 11 .. 0)			384-1				
	word	$017d	;13 HMEMSTART (D = 11 .. 0)			382-1				
	word	$071f	;14 HSTOP (D = 11 .. 0)				1824-1				
	word	$076f	;15 HTOTAL (D = 11 .. 0)			1904-1				
	word	$0002	;16 VSYNC (D = 10 .. 0)				3-1				
	word	$001e	;17 VSTART (D = 10 .. 0)			31-1				
	word	$03a2	;18 VSTOP  (D = 10 .. 0)			931-1				
	word	$03a3	;19 VTOTAL  (D = 10 .. 0)			932-1				
	word	$0000	;1a LC ADDER LOW				$0000				
	word	$0080	;1b LC ADDER HIGH				$0080				
	word	$0000	;1c LC ADDER LATCH				X				
	word	$0000	;1d LC START (10 .. 0) Pixeloffset		$0000				
	word	$0000	;1e						X				
	word	$0000	;1f						X				
	word	$0008	;20 HLOADSTART 					8				
	word	$02d8	;21 HLOADSTOP					720+8 (read 720 32bit words)				
	word	$001e	;22 VLOADSTART 					31-1				
	word	$03a2	;23 VLOADSTOP 					931-1				
	word	$0002	;24 VLOADNEXT					2				
	word	$0000	;25 MEM_STARTADR_store low			$0000				
	word	$d000	;26 MEM_STARTADR_store high			$f000				
	word	$0000	;27 MEM_LINEOFFSET_store low			$0000				
	word	$0000	;28 MEM_LINEOFFSET_store high			$0000				
	word	$0000	;29 MEM_MODE_store 				32 bit
	word	$0000	;2a LATCH MEM_STARTADR/LINEOFFSET/MODE		X	
	
	;
	
;*********************************************************************
;* sd data after code segment
;*********************************************************************		
		
	.org	16384
		
	.incdir		"F:\git\goldmomo_endlos\assembler_files\ucore\hcore\gfx"
	.incbin		"pic720x450_8888.dat.raw"
	.incdir		"F:\git\goldmomo_endlos\assembler_files\ucore\hcore"	
	;.asmhcore 	"parallelTest1.s"
	;.asmhcore 	"parallelTest2.s"
	;.asmhcore 	"parallelTest1_sync.s"
	.asmhcore 	"udivTest.s"
	
	