
	.text
	.align	4
	.arm

;*****************************************************************************
;*
;* scroll_bg
;*
;*	- scrollt Bildschirm an Position x,y
;*
;* r0 = x
;* r1 = y
;* r2 = background 0..3
;* r3 = mapsrc	
;* r4 = mapdest
;*

	.global		scroll_bg
	.type		scroll_bg,function

scroll_bg_th

	stmfd	sp!,{r0-r12}

	;**** setze hardware scroll

	mov	r12,r2,lsl #2		;je 2 REGS = *4
	add	r12,r12,#$10		;BG0X = $10
	add	r11,r12,#$4000000	;
	
	and	r5,r0,#$ff
	and	r6,r1,#$ff
	strh	r5,[r11]
	strh	r6,[r11,#2]
		
		

	ldmfd	sp!,{r0-r12}
	bx	lr

;*****************************************************************************
;*
;* scroll_bg
;*
;*	- scrollt Bildschirm an Position x,y
;*
;* r0 = x
;* r1 = y
;* r2 = background 0..3
;* r3 = mapsrc	
;* r4 = mapdest
;*

	.global		scroll_bg
	.type		scroll_bg,function

scroll_bg


	stmfd	sp!,{r0-r12,lr}	

	
	ldr	r12,=bgXxposabs
	ldr	r10,=bgXxpos
	
	add	r12,r12,r2, lsl #3	;*2 longs
	add	r10,r10,r2, lsl #3	;*2 longs
	
	mov	r11,r2			;save bgnummer

	mov	r8,r3			;mapsrc
	mov	r9,r4			;mapdst

	;******************* Berechne X-Scrolling
	
	ldr	r2,[r12]		;xold
	sub	r7,r0,r2		;dx
	str	r0,[r12]		;xold=xnew	

	cmp	r7,#0
	beq	onlyxrest
dxl
	cmp	r7,#0
	addgt	r2,r2,#1
	sublt	r2,r2,#1

	stmfd	sp!,{r0-r2,r7,r8,r9}	
	
	mov	r1,r2				;x
	mov	r3,r10				;&bgXxpos
	add	r4,r10,#4			;&bgXypos
	mov	r6,r11				;Backround Nummer
						;r8 = &mapsrc
						;r9 = &mapdst
	bl	scrollx
	ldmfd	sp!,{r0-r2,r7,r8,r9}	
	
	
	
	cmp	r2,r0
	bne	dxl
onlyxrest
	
	;******************* Berechne Y-Scrolling
	
	ldr	r2,[r12,#4]		;yold
	sub	r7,r1,r2		;dy
	str	r1,[r12,#4]		;yold=xnew	

	cmp	r7,#0
	beq	onlyyrest
dyl
	cmp	r7,#0
	addgt	r2,r2,#1
	sublt	r2,r2,#1

	stmfd	sp!,{r0-r2,r7,r8,r9}	
	
	mov	r1,r2				;y
	mov	r3,r10				;&bgXxpos
	add	r4,r10,#4			;&bgXypos
	mov	r6,r11				;Backround Nummer
	 					;r8 = &mapsrc
	 					;r9 = &mapdst
	bl	scrolly
	ldmfd	sp!,{r0-r2,r7,r8,r9}	
	
	cmp	r2,r1
	bne	dyl
onlyyrest
	
	ldmfd	sp!,{r0-r12,pc}	

	.pool		;pool

;*****************************************************************************
;*
;* scrolly 
;*
;* 	- schiebt den Bildschirm bis maximal 7 Bildpunkte in y Richtung	
;*	- füllt die Hardware Map mit Daten
;*
;* r1 = ypos
;* r3 = bgXxpos_adr	
;* r4 = bgXypos_adr
;* r6 = bg (0-3)
;* r8 = map src
;* r9 = map hardware dest
;*
;* waste r0,r1,r2,r3,r4,r5,r6,r7

scrolly
	stmfd	sp!,{lr}

	ldr	r7,[r3]				;r7	 = bg1xpos
	ldr	r2,[r4]				;yold	 = bg1ypos	
	
	;**** set hardware bgx_y (0..7)
	
	mov	r0,#$4000000
	and	r5,r1,#$ff			;0..255
	add	r0,r0,r6, lsl #2			;bg0 + r6 * 4
	strh	r5,[r0,#$12]
	
	;**** test ob mehr als 8 pixel unterschied (8ter block!)
	
	mov	r5,r2, asr #3
	sub	r0,r5,r1, asr #3
	cmp	r0,#0
	ldmeqfd	sp!,{pc}
	
	str	r1,[r4]				;bg1ypos = ynew
		
	;**** kopiere HLine
	
	mov	r4,r1, lsr #3			;r4=ynew/8
	cmp	r4,#0
	ldmeqfd	sp!,{pc}
	
	cmp	r1,r2				;oben oder unten
	addgt	r4,r4,#20			;160 	(unten)
	sublt	r4,r4,#1			;-8  	(oben) 	
	and	r5,r4,#$1f			;mod 32 (für hardware) 
	
	add	r0,r8,r4, lsl #8			;=src+((y/8)*128*2)
	add	r1,r9,r5, lsl #6			;=dest+((y/8)%8*32*2))
	
	mov	r4,#128*2
	mov	r6,r7, lsr #3			;start = x/8 
	sub	r6,r6,#1			;minus 1 (wegen 4-Ecken)
	bl	copyHLine
	
	ldmfd	sp!,{pc}

;*****************************************************************************
;*
;* scrollx 
;*
;* 	- schiebt den Bildschirm bis maximal 7 Bildpunkte in x Richtung	
;*	- füllt die Hardware Map mit Daten
;*
;* r1 = xpos
;* r3 = bgXxpos_adr	
;* r4 = bgXypos_adr
;* r6 = bg (0-3)
;* r8 = map src
;* r9 = map hardware dest
;*
;* waste r0,r1,r2,r3,r4,r5,r6,r7

scrollx
	stmfd	sp!,{lr}

	ldr	r7,[r4]				;r7 = bgxypos
	ldr	r2,[r3]				;xold = bgxxos	
	
		
	;**** set hardware bgx_x (0..7)
	
	mov	r0,#$4000000
	and	r5,r1,#$ff			;0..255
	add	r0,r0,r6, lsl #2		;bg0 + r6 * 4
	strh	r5,[r0,#$10]
	
	;**** test ob mehr als 8 pixel unterschied
	
	mov	r5,r2, asr #3
	sub	r0,r5,r1, asr #3
	cmp	r0,#0
	ldmeqfd	sp!,{pc}
	
	str	r1,[r3]				;bgxxos = xnew
	
	;****

	
	mov	r4,r1, lsr #3			;r4=xnew/8
	cmp	r1,r2				;rechts oder links
	addgt	r4,r4,#30			;240 	(rechts)
	sublt	r4,r4,#1			;-8  	(links) 
	and	r5,r4,#$1f			;mod 32 (für hardware) 
	
	
	
	add	r0,r8,r4, lsl #1			;=src+((x/8)*2)
	add	r1,r9,r5, lsl #1			;=dest+((x/8)*2)
		
	mov	r6,r7, lsr #3			;ystart = y/8
	mov	r4,#128*2
	sub	r6,r6,#1
	bl	copyVLine
	
	

	ldmfd	sp!,{pc}
	
;*****************************************************************************
;*
;* copyVLine
;*
;*	- zeichne vertikale Linie (mit Bilddaten) in HWMap 
;*
;* r0 src
;* r1 dest
;* r4 src-width
;* r6 start source
;*
;* waste r0,r1,r2,r3,r5,r6,r7

copyVLine	

	mov	r3,r6 			;save	start
	mov	r7,r1			;save   src
	
	and	r6,r6,#$1f		;hardware limit 32
	
	add	r1,r1,r6, lsl #6		;32*2
	add	r0,r0,r3, lsl #8		;128*2
	rsb	r2,r6,#32		;xlänge=32-stat
	
	orr	r3,pc,#1
	bx	r3
	
	.thumb				;THUMB

	mov	r5,#64			;32*2
	
cvloop1	
	ldrh	r3,[r0]
	strh	r3,[r1]
	
	add	r0,r0,r4
	add	r1,r1,r5
	
	sub	r2,r2,#1
	bne	cvloop1

	;**** the rest

	cmp	r6,#0
	beq	nocvloop2		;if no rest dont do it
	mov	r1,r7
	;mov	r2,r6			;xrest=start
cvloop2	
	ldrh	r3,[r0]
	strh	r3,[r1]
	
	add	r0,r0,r4
	add	r1,r1,r5
	
	sub	r6,r6,#1
	bne	cvloop2

nocvloop2

	bx	lr
	
	.arm

;*****************************************************************************
;*
;* copyVLine
;*
;*	- zeichne horizontale Linie (mit Bilddaten) in HWMap 
;*
;* r0 src
;* r1 dest
;* r4 src-width
;* r6 start source
;*
;* waste r0,r1,r2,r3,r5,r6,r7

copyHLine

	

	mov	r3,r6 			;save	start
	mov	r7,r1			;save   src
	
	and	r6,r6,#$1f		;hardware limit 32
	
	add	r1,r1,r6, lsl #1		;*2
	add	r0,r0,r3, lsl #1		;*2
	
	rsb	r2,r6,#32		;kopiere 1 Teil (y = 32-start)
	
	orr	r3,pc,#1
	bx	r3

	.thumb	


chloop1	
	ldrh	r3,[r0]
	strh	r3,[r1]
	
	add	r0,#2
	add	r1,#2
	
	sub	r2,r2,#1
	bne	chloop1

	;rest

	mov	r1,r7
	
	cmp	r6,#0
	beq	nochloop2
	;mov	r2,r6
chloop2	
	ldrh	r3,[r0]
	strh	r3,[r1]
	
	add	r0,#2
	add	r1,#2
	
	sub	r6,r6,#1
	bne	chloop2

nochloop2
	bx	lr
	
	.arm
	.align	4
	
;*****************************************************************************
;*
;* copymap
;*
;*	- kopiere hart SWMap in HWMap 
;*	
;* r0 src
;* r1 dest
;* r2 mod SWMap
;*
;* waste r0,r1,r3,r4,r5
	
	.global		copymap
	.type		copymap,function
	
copymap
	
	orr	r3,pc,#1
	bx	r3

	.thumb					;THUMB
	
	mov	r5,#32
ycmloop	
	mov	r4,#32
xcmloop
	ldrh	r3,[r0]
	strh	r3,[r1]
	
	add	r0,#2
	add	r1,#2

	sub	r4,r4,#1
	bne	xcmloop	
		
	add	r0,r0,r2
	
	sub	r5,r5,#1
	bne	ycmloop	
	
	bx	lr	


;*****************************************************************************************************************
;*
;*	WRAM 
;*
;*****************************************************************************************************************

	.section    	.ewram,"ax"
	.align	4	

bgXxpos		
bg0xpos		@dcd	0
bg0ypos		@dcd	0
bg1xpos		@dcd	0
bg1ypos		@dcd	0
bg2xpos		@dcd	0
bg2ypos		@dcd	0
bg3xpos		@dcd	0
bg3ypos		@dcd	0

bgXxposabs
bg0xposabs	@dcd	0
bg0yposabs	@dcd	0
bg1xposabs	@dcd	0
bg1yposabs	@dcd	0
bg2xposabs	@dcd	0
bg2yposabs	@dcd	0
bg3xposabs	@dcd	0
bg3yposabs	@dcd	0
	
	
	
	.end	
	