;*********************************************************************	
; 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
;*********************************************************************

start	
	;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	r0,fontData
	movei	r1,>>fontData
	moveih	r0,>fontData
	moveih	r1,>>>fontData		;destination 
		
	movei	r6,sdCardReadBlocks
	nop
	moveih	r6,>sdCardReadBlocks
	
	gpci	r7,2		;
	jmpi	r6,0
	movei	r4,$2		;nblocks-1	delay slot
	nop			;delay slot
	moveih	r4,$0		;delay slot
	nop			;delay slot

	;setup irq vector
	
	movei	r0,irq
	nop
	moveih	r0,>irq
	nop
	setssr	r0,2
	
	;debug
	dexti	>UcRSwichtWLedG
	movei	r7,UcRSwichtWLedG
	movei	r1,1
	nop
	st	r7,r1
	
	;

	;setup video (720x450)
	
	gpci	r7,2		;next 4 + X
	br	setupVideo
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
	;debug
	dexti	>UcRSwichtWLedG
	movei	r7,UcRSwichtWLedG
	movei	r1,2
	nop
	st	r7,r1
	
	;
	
	;clear screen
	
	movei	r2,$00	;destination 
	movei	r3,$00
	moveih	r2,$00
	moveih	r3,$d0	;p(0,0) = f000 0000
	
	gpci	r7,2	;next 4 + X
	br	memset
	movei	r1,$33	; count		delay slot	
	movei	r0,$00	; value		delay slot
	moveih	r1,$9e	; count		delay slot
	nop		;		delay slot
	
	;debug
	dexti	>UcRSwichtWLedG
	movei	r7,UcRSwichtWLedG
	movei	r1,3
	nop
	st	r7,r1
	
	;
	
	;draw text
	
	;fedc ba98 7654 3210
	;0rrr rrgg gggb bbbb
	
	dexti	$03
	movei	r0,$e0	;green
	dexti	$7c
	movei	r1,$1f	;lila
	
	push	r0
	push	r1
	
	gpci	r7,2		;next 4 + X
	br	drawTextFormated
	movei	r0,text	;delay slot
	movei	r1,0		;delay slot
	moveih	r0,>text	;delay slot
	movei	r2,0		;delay slot
	
	rqpop
	rqpop
	nop
	nop
	nop
	
	;debug
	dexti	>UcRSwichtWLedG
	movei	r7,UcRSwichtWLedG
	movei	r1,4
	nop
	st	r7,r1
	
	;
	
end	
	br	end
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	nop			;delay slot
	
;***********************************************************************
;* irq code start
;***********************************************************************
	
irq
	cli
	rti
	nop		;delay slot
	nop		;delay slot
	nop		;delay slot
	sei		;delay slot
		
;*********************************************************************
;* functions
;*********************************************************************

	.include	"components\drawText.s"
	.include	"components\drawChar1Bit.s"
	.include	"components\sdCardLoader.s"
		
;******************************
;*
;* 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
	
;******************************
;*
;* 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	$d000	;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
	
	
drawFrame	
	.data	$d000,$0000
	
screenWidth
	word	720
	
fontAddress
	.data	$f000,$0000
		
text	

	.string	"Hallo! %aI can change %acolors!"
	word	0
	
	.def	fontData	$f0000000

	
;*********************************************************************
;* mod data
;*********************************************************************

	.org	16384
	
	.incdir		"F:\git\goldmomo_endlos\gfx"
	.incbin		"topaz_1bit.dat.raw"
	
	