	
	;this version is only sd card bootable	
	
;*********************************************************************
;* defines
;*********************************************************************

	.def	picDataRAM	$d0000000
	
;*********************************************************************	
; includes
;*********************************************************************	

	.incdir	"F:\git\goldmomo_endlos\assembler_files\ucore\"
	.include "boot_level_sdHeader.s"
	.include "include\ucore.i"
	.include "include\ucore_ctrl.i"
	
;***********************************************************************
;* loader
;***********************************************************************	

	;read sdcard to sram
	
	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,$00
	movei	r0,$00
	moveih	r1,$f0		;r1:r0 = $f000 0000 (dest start)
	
	movei	r6,sdCardReadBlocks
	nop
	moveih	r6,>sdCardReadBlocks
	
	gpci	r7,2		;
	jmpi	r6,0
	movei	r4,$ff		;2048 blocks-1	delay slot
	nop			;delay slot
	moveih	r4,$3		;delay slot
	nop			;delay slot
	
;***********************************************************************
;* real code
;***********************************************************************	
	
	;reset PS2 buffer
	
	gpci	r7,2	;next 4 + X
	br	PS2Reset
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
		
	;setup video (360x225)
	
	gpci	r7,2		;next 4 + X
	br	setupVideo
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	
;***********************************************************************
;* main loop
;***********************************************************************	
		
loop	
	;process ps2 keys
	
	gpci	r7,2		;next 4 + X
	br	PS2Process
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;testKeys
	
	gpci	r7,2
	br	testKeys
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;wait vpos (930)

	gpci	r7,2	;next 4 + X
	br	waitVPos
	movei	r0,$a1	;delay slot
	nop		;delay slot
	moveih	r0,$3	;delay slot
	nop		;delay slot
	
	;draw screen
	
	movei	r6,picDataRAM	;destination 
        movei	r7,>>picDataRAM
        moveih	r6,>picDataRAM
        moveih	r7,>>>picDataRAM
	nop
	
	
	movei	r0,223		;line cnt
	movei	r5,0		;y
	movei	r4,0		;du
	
yloop	
	
	dexti	>scrDUMod
	movei	r3,scrDUMod
	nop
	rqldi	r3,0
	nop
	nop
	ld	r2
	nop
	add	r4,r4,r2

	push	r0
	push	r5
	push	r4
	
	;change l2d struct
	
	;y
	
	dexti	>l2dData
	movei	r1,l2dData
	ssto	l2dvHigh
	stwo	r1,r5		;set y
	
	;du
	
	
	ssto	l2dduLow
	stwo	r1,r4
	ssto	l2ddvLow
	stwo	r1,r4
	
	;copy l2d struct to ucore sr
	
	dexti	>l2dData
	movei	r1,l2dData
	movei	r2,0		;cnt
l2dSetup
	rqldi	r1,0
	addi	r1,1
	nop
	ld	r0
	setssr	r2,5		;write sel
	setssr	r0,6		;write data
	cmpeqi	r2,l2dLast	;end?
	brtc	l2dSetup
	addi	r2,1		;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
		
	;push pipeline 
	
	exl2den
	exl2den
	exl2den
	;exl2den
	
	;45 * 8 = 360 pixel process
	
	movei	r5,44
scanloop	
	exl2den
	exl2denerqld	
	exl2denerqld    
	exl2denerqld    
	exl2denerqld    
	exl2denerqld	
	exl2denerqld    
	exl2denerqld    
	exl2derqld    
	esadr	r7,r6
	addi	r6,8
	addtqi	r7,r7,0
	eld	r0
	eld	r1
	eld	r2
	eld	r3
	est	r0,0
	est	r1,1
	est	r2,2
	est	r3,3
	eld	r0
	eld	r1
	eld	r2
	eld	r3
	subi	r5,1
	brts	scanloop
	est	r0,4		;delay slot
	est	r1,5            ;delay slot
	est	r2,6            ;delay slot
	est	r3,7            ;delay slot
		
	;
	
	rqpop
	rqpop
	rqpop
	pop	r4
	pop	r5
	pop	r0
	
	addi	r5,1
	subi	r0,1
	brts	yloop
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
		
	br	loop	
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
;*********************************************************************
;* functions
;*********************************************************************	
	
	.include	"components\sdCardLoader.s"
	.include	"components\ps2keyboard.s"	
	
;******************************
;*
;* testKeys
;* 	

testKeys	
	
	push	r7
	
	dexti	>scrDUMod
	movei	r6,scrDUMod
		
	;check key A
	
	movei	r0,$1c		;a?
	gpci	r7,2
	br	PS2TestKey
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	
	cmpeqi	r0,0
	brts	notAPressed
	rqldi	r6,0		;delay slot
	nop                     ;delay slot
	nop                     ;delay slot
	ld	r0              ;delay slot
		
	nop
	addi	r0,1
	nop
	st	r6,r0
		
notAPressed	

	;check key S
	
	movei	r0,$1b		;s?
	gpci	r7,2
	br	PS2TestKey
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;dump
	
	cmpeqi	r0,0
	brts	notSPressed
	rqldi	r6,0		;delay slot
	nop                     ;delay slot
	nop                     ;delay slot
	ld	r0              ;delay slot
	
	nop
	subi	r0,1
	nop
	st	r6,r0
		
notSPressed	

	rqpop
	nop
	nop
	pop	r7
	nop

	jmpi	r7,0
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
;******************************
;* extCopy16
;*
;* r1:r0 source
;* r3:r2	dest
;* r4 count - 1 (16 words per count)
;* 
		
extCopy16	

	push	r5
	push	r6
	
extCopy16Loop
	esadr	r1,r0
	
	erqldi	0
	erqldi	1
	erqldi	2
	erqldi	3
	erqldi	4
	erqldi	5
	erqldi	6
	erqldi	7
	erqldi	8
	erqldi	9
	erqldi	10
	erqldi	11
	erqldi	12
	erqldi	13
	erqldi	14
	erqldi	15
	
	esadr	r3,r2
	eld	r5
	eld	r6
	est	r5,0
	est	r6,1
	eld	r5
	eld	r6
	est	r5,2
	est	r6,3
	eld	r5
	eld	r6
	est	r5,4
	est	r6,5
	eld	r5
	eld	r6
	est	r5,6
	est	r6,7
	
	eld	r5
	eld	r6
	 
	dexti	1		;add 1<<3 (8) to next im3 value
	est	r5,0
	dexti	1
	est	r6,1
	eld	r5
	eld	r6
	dexti	1
	est	r5,2
	dexti	1
	est	r6,3
	eld	r5
	eld	r6
	dexti	1
	est	r5,4
	dexti	1
	est	r6,5
	eld	r5
	eld	r6
	dexti	1
	est	r5,6
	dexti	1
	est	r6,7
	

	subi	r4,1
	brts	extCopy16Loop
	addi	r0,16		;delay slot
	addtqi	r1,r1,0		;delay slot
	addi	r2,16		;delay slot
	addtqi	r3,r3,0		;delay slot
	
	rqpop
	rqpop
	jmpi	r7,0
	pop	r6	;delay slot
	pop	r7     	;delay slot
	nop		;delay slot
	nop		;delay slot

;******************************
;* waitVPos
;*
;* r0 pos
;* 

waitVPos	
	
	push	r1
	push	r2
	
	;check emulator to prevent face freez .....
	
	dexti	>UuLedR
	movei	r1,UuLedR	;bit 1 is emulator check
	nop
	rqldi	r1,0
	nop
	nop
	ld	r2
	nop
	cmpeqi	r2,1
	brts	waitVEmulator
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	
waitVRealHardWare
	
	dexti	>UcVideoRVCountWHSYNC
	movei	r1,UcVideoRVCountWHSYNC	;vcount
	nop
		
waitV	
	rqldi	r1,0
	nop
	nop
	ld	r2	;get vcount
	nop
	cmpeq	r0,r2
	brtc	waitV
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot

	rqpop
	rqpop
	jmpi	r7,0
	pop	r2	;delay slot
	pop	r1     	;delay slot
	nop		;delay slot
	nop		;delay slot
	
waitVEmulator
	
	dexti	>UcVideoRVCountWHSYNC
	movei	r1,UcVideoRVCountWHSYNC	;vcount
	nop
		
waitVE	
	rqldi	r1,0
	nop
	nop
	ld	r2	;get vcount
	nop
	cmplo	r0,r2
	brts	waitVE
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot

	rqpop
	rqpop
	jmpi	r7,0
	pop	r2	;delay slot
	pop	r1     	;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					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	$0040	;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	$00bc	;21 HLOADSTOP					360+8				180+8
	word	$001e	;22 VLOADSTART 					31-1				31-1
	word	$03a2	;23 VLOADSTOP 					931-1				931-1
	word	$0004	;24 VLOADNEXT					2				4
	word	$0000	;25 MEM_STARTADR_store low			$0000				$0000
	word	$d000	;26 MEM_STARTADR_store high			$f000				$f000
	word	$0000	;27 MEMLowINEOFFSET_store low			$0000				$0000
	word	$0000	;28 MEMLowINEOFFSET_store high			$0000				$0000
	word	$0002	;29 MEM_MODE_store 				2				2 (X1R5G5B5X1R5G5B5)
	word	$0000	;2a LATCH MEM_STARTADR/LINEOFFSET/MODE		X				X
		
	;
	
l2dData
	word	$0000	;u 	Low         			
	word	$0000	;u 	High         	
        word	$0000	;du	Low        	
        word	$0001	;du	High        	
        word	$0000	;v	Low         	
        word	$0000	;v	High         	
        word	$0000	;$cfff	;dv	Low        	
        word	$0000	;$ffff	;dv	High        	
        word	$01ff	;xmask        	
        word	$01ff	;ymask       	
        word	$0200	;xwidth      	
        word	$0000	;sadr	Low      	
        word	$f000	;sadr	High      

scrDUMod	word	$0000
		
;*********************************************************************
;* sd data after code segment
;*********************************************************************		
		
	;gfx
		
	.org	16384
	.incdir "F:\git\goldmomo_endlos\gfx"
	.incbin	"earth512x512.dat.raw"
		
