;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
;*********************************************************************

;                       wordcnt
;
;frame buffer 0		162000	d0000000		360*225 32bit			BSS
;frame buffer 1		162000	d00278d0		360*225 32bit			BSS
;font1Bit	         376    d004f1a0		font 8x8_1bit (752x16)	DATA START
;texture512x512		524288	d004f318

;hcore program			d00cf318

	.def	frame0				$d0000000
	.def	frame1				$d00278d0
	.def	font1Bit			$d004f1a0
	.def	texture512x512x8888		$d004f318
	.def	hcoreStartPc			$d00cf318

start	
	cli

	;video off
	
	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,>>font1Bit
	movei	r0,font1Bit
	moveih	r1,>>>font1Bit
	moveih	r0,>font1Bit	;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
	
	;setup irq vector
	
	dexti	>irq
	movei	r0,irq	
	nop
	setssr	r0,2
	
	;setup hcore irq
	
	movei	r0,UcIrqMsk
	movei	r1,$10			;enable hcore irq
	moveih	r0,>UcIrqMsk
	nop
	st	r0,r1			;
	
	;get hcores count
	
	gpci	r7,2
	br	getHCoreCount
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;over all hcores
	
	subqi	r1,r0,1		;hcore count - 1
hcoreInitLoop

	;set hcore(r1) pc
	
	gpci	r7,2
	br	setHCorePC
	dexti	>>>hcoreStartPc		;delay slot
	movei	r3,>>hcoreStartPc	;delay slot	high	
	dexti	>hcoreStartPc		;delay slot
	movei	r2,hcoreStartPc		;delay slot	low
	
	;

	subi	r1,1
	brts	hcoreInitLoop
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;release hcore reset
	
	gpci	r7,2
	br	setHCoresReset
	movei	r1,0		;release reset	delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;enable irq
	
	sei
	
	;
	
end	br	end	
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
;*********************************************************************
;* functions
;*********************************************************************	
	
	;standard
	
	.incdir	"F:\git\goldmomo_endlos\assembler_files\ucore\"
	.include "components\sdCardLoader.s"
	
	;hcore specific
	
	.incdir	"F:\git\goldmomo_endlos\assembler_files\ucore\hcore\"
	.include "ucomponents\ucore_hcore_process.s"

;***********************************************************************
;* irq code start
;***********************************************************************
	
irq
	cli
	
	;save all register
	
	epushsadrl
	epushsadrh
	push	r0
	push	r1
	push	r2
	push	r3
	push	r4
	push	r5
	push	r6
	push	r7
	
	;check type of irq
	
	dexti	>UcIrqMemory
	movei	r0,UcIrqMemory
	nop
	rqldi	r0,0
	nop
	nop
	ld	r1		;memory
	nop
	extri	r1,4		;hcore
	gpci	r7,2		
	brts	irqHcore
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	

	;restore all used register and go back
	
	rqpop		
	rqpop	
	rqpop	
	poparqp	r7
	poparqp	r6
	poparqp	r5	
	poparqp	r4
	poparqp	r3
	poparqp	r2
	poparqp	r1
        pop	r0
	epopsadrh
	epopsadrl
	
	rti
	nop		;delay slot
	nop		;delay slot		
	nop		;delay slot
	sei		;delay slot	
	
;******************************
;*
;* setHCorePC
;* 	
;* r1		hcore num
;* r3:r2	pc
;

setHCorePC

	push	r0
	push	r4

	dexti	>UcHcoreWSelRHCnt
	movei	r0,UcHcoreWSelRHCnt
	
	movei	r4,0
	stinc	r0,r1	;sel
	nop
	stinc	r0,r4	;adr = 0
	movei	r4,1
	stinc	r0,r3	;data = high pc
	nop
	stinc	r0,r4	;push to fifo
	
	dexti	>UcHcoreAdr
	movei	r0,UcHcoreAdr
	
	nop
	stinc	r0,r4	;adr = 1
	nop
	stinc	r0,r2	;data = low pc
	nop
	stinc	r0,r4	;push to fifo
	
	rqpop
	rqpop
	jmpi	r7,0
	pop	r4	;delay slot
	pop	r0	;delay slot
	nop		;delay slot
	nop		;delay slot
	
;******************************
;*
;* setHCoresReset
;* 	
;* r1		reset (bit 0 => reset)
;

setHCoresReset

	push	r0
	push	r3

	dexti	>UcHcoreWSelRHCnt
	movei	r0,UcHcoreWSelRHCnt
	
	movei	r3,$ff	;select all (special code)
	
	nop
	st	r0,r3	
	
	dexti	>UcHcoreData
	movei	r0,UcHcoreData
	
	movei	r3,1	
	stinc	r0,r1	;data = reset
	nop
	st	r0,r3	;push to fifo
	
	rqpop
	rqpop	
	jmpi	r7,0
	pop	r3	;delay slot
	pop	r0	;delay slot
	nop		;delay slot
	nop		;delay slot

;******************************
;*
;* getHCoreCount
;* 
;* return
;*	
;* r0		HCoreCount
;

getHCoreCount

	dexti	>UcHcoreWSelRHCnt
	movei	r0,UcHcoreWSelRHCnt
	nop
	rqldi	r0,0
	jmpi	r7,0
	nop		;delay slot
	ld	r0	;delay slot
	nop		;delay slot
	nop		;delay slot
		
;*********************************************************************
;* sd data after code segment
;*********************************************************************		
		
	.org	16384
		
	.incdir		"F:\git\goldmomo_endlos\gfx"
	.incbin		"font_8x8_1bit.dat.raw"
	.incdir		"F:\git\goldmomo_endlos\assembler_files\ucore\hcore\gfx"
	.incbin		"hydra512x512_8888.dat.raw"
	.incdir		"F:\git\goldmomo_endlos\assembler_files\ucore\hcore"
	;.asmhcore 	"uctrlIoTest4SetVideoDrawTextPerformaceCounter3.s"
	.asmhcore 	"uctrlIoTest4SetVideoDrawTextPerformaceCounterFilterd3.s"
	
		
	