;note also test for dexti (decoder immediate extend (used for est))	
	
;*********************************************************************	
; includes
;*********************************************************************	

	.incdir	"F:\git\goldmomo_endlos\assembler_files\ucore\"
	.include "include\ucore_ctrl.i"

;*********************************************************************	
; sd header is placed here
;*********************************************************************	

	.include "boot_level_sdHeader.s"

;*********************************************************************
;* start at address 32 here
;*********************************************************************

	.def	frame		$d0000000
	.def	hcoreStartPc	$d0100000

start	
	cli

	;setup video (360x225)
	
	gpci	r7,2		;next 4 + X
	br	setupVideo
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;test simulation (MODELSIM)
	
	dexti	>UuLedR
	movei	r0,UuLedR
	nop
	rqldi	r0,0
	nop
	nop
	ld	r0
	nop
	
	extri	r0,1	
	brts	ModelSim
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;clear screen on emulator and hardware
	
	dexti	>>>frame	;delay slot
	movei	r3,>>frame     	;delay slot
	dexti	>frame         	;delay slot
	movei	r2,frame       	;delay slot
	
	gpci	r7,2	;next 4 + X
	br	memset
	movei	r1,$8c	; count		delay slot	
	movei	r0,$00	; value		delay slot
	moveih	r1,$27	; count		delay slot
	nop		;		delay slot

ModelSim
	
	;copy hcore programm
	
	dexti	1
	movei	r6,255
	dexti	>hcoreProgram
	movei	r1,hcoreProgram
	gpci	r7,2
	br	copyToHcore
	dexti	>>>hcoreStartPc		;delay slot
	movei	r3,>>hcoreStartPc     	;delay slot
	dexti	>hcoreStartPc         	;delay slot
	movei	r2,hcoreStartPc       	;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			;
	
	;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
;*********************************************************************	
	
	;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		
	
;******************************
;*
;* 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
	
;******************************
;*
;* memset
;*
;* 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

;******************************
;*
;* copyToHcore
;*
;* r1 	 = source address
;* r3:r2 = dest address
;* r6    = cnt-1
;*
	
copyToHcore	

	nop
	esadr	r3,r2

copyToHcoreLoop

	rqldi	r1,0	;high
	rqldi	r1,1	;low
	addi	r1,2	;next word
	ld	r4
	ld	r5
	
	addi	r2,2
	addtqi	r3,r3,0

	subi	r6,1
	brts	copyToHcoreLoop
	est	r4,0		;delay slot
	est	r5,1		;delay slot	
	nop			;delay slot
	esadr	r3,r2		;delay slot

	;sync (est are done)
	
	erqldi	0
	eld	r6
	
	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	$0040	;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	$00bc	;21 HLOADSTOP					360+8				
	word	$001e	;22 VLOADSTART 					31-1				
	word	$03a2	;23 VLOADSTOP 					931-1				
	word	$0004	;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	$0003	;29 MEM_MODE_store 				3	------rrrrggggbbbb----rrrrggggbbbb
	word	$0000	;2a LATCH MEM_STARTADR/LINEOFFSET/MODE		X	
	
	;
	
	.data	$ffff,$ffff
		
hcoreProgram

	.incdir		"F:\git\goldmomo_endlos\assembler_files\ucore\hcore"
	;.asmhcore 	"doNothing.s"
	;.asmhcore 	"simpleWritePixels.s"
	;.asmhcore 	"simpleWritePixels_multi.s"
	;.asmhcore 	"simpleReadWritePixels_multi.s"
	;.asmhcore 	"simpleReadWritePixels_multi2.s"
	;.asmhcore 	"simpleReadWritePixels_multi3.s"
	;.asmhcore 	"uctrlIoTest2CallFunction.s"
	;.asmhcore 	"uctrlIoTest3AccessUctrlRegs.s"
	;.asmhcore 	"loadHazard.s"
	.asmhcore 	"simpleReadWritePixels_regRotate.s"
	