
	.align	4
	.arm

;**** set sprite
;*
;* r3 = prio 0..3
;* r4 = sprite nummer
;*

	.global	setsprite_prio
	.type	setsprite_prio,function

setsprite_prio

	stmfd 	sp!,{r5,r7}

	mov	r7,#$7000000
	add	r7,r7,r4, lsl #3
	ldrh	r5,[r7,#4]
	bic	r5,r5,#$0c00		;clear old prio
	orr	r5,r5,r3, lsl #10		;set new prio
	strh	r5,[r7,#4]
	
	ldmfd 	sp!,{r5,r7}
	mov	pc,lr
	
;**** set sprite
;*
;* r0 = x
;* r1 = y
;* r2 = sprite nummer
;* r6 = rotatios nummer
;* r5 = tilenummer
;* r7 = 0 sprite prio löschen/ 1 prio lassen
;*
setsprite

	stmfd 	sp!,{r0,r1,r3-r5,r6}
	
	mvn	r3,#$ff			;ffffff00
	
	bic	r0,r0,r3,lsl #1		;x 0..511
	bic	r1,r1,r3		;y 0..255
		
	mov	r3,#$2100		;256,rotscal 		 
        mov	r4,#$8000		;32x32, rotscal nummer 0
	
	orr	r4,r4,r6,lsl #9		;rot nummer
	
	orr	r3,r3,r1		;orr y
	orr	r4,r4,r0		;orr x
	
	mov	r6,#$7000000		;sprite base
	
	add	r6,r6,r2,lsl #3		;spritenummer
	add	r5,r5,r5		;r5<<1 (bit 0 ist undefiniert)
	
	strh	r3,[r6]			;word 0
	strh	r4,[r6,#2]		;word 1
	
	cmp	r7,#0			;save prio
	beq	no_priosave
	
	;**** sichere alte sprite prio
	
	ldrh	r3,[r6,#4]		;lese 
	and	r3,r3,#$0c00		;nur prio retten		
	orr	r5,r3,r5		;
		
no_priosave	
	strh	r5,[r6,#4]		;word 2

	
	ldmfd 	sp!,{r0,r1,r3-r5,r6}
	mov	pc,lr

;**** spr_calc_rotation
;*
;* r0 = zoom x&y
;* r1 = winkel 0..ffff (0..360)
;* r4 = rot nummer 0..31
;*

	.global	spr_calc_rotation
	.type	spr_calc_rotation,function

spr_calc_rotation

	stmfd 	sp!,{r0-r4}
	
	addr	r2,spr_objectbuf

	strh	r0,[r2]
	strh	r0,[r2,#2]
	strh	r1,[r2,#4]
	
	mov	r0,r2
	mov	r1,#$7000000
	add	r1,r1,#6
	add	r1,r1,r4, lsl #5		;*32
	mov	r2,#1
	mov	r3,#8
	swi	#$f0000			;obj
	
	ldmfd 	sp!,{r0-r4}
	mov	pc,lr
	

;**** new_sprite
;*
;* r0.w = x	0..511
;* r1.w = y	0..255
;* r2.b = rotatios nummer
;* r3.b = tilenummer
;* r4.b = prio
;* r5.l = mode
;* r6.b = rotnummer
;* 
;* return:
;*
;* r7.l = spritedata	
;*

	.global	spr_new_sprite
	.type	spr_new_sprite,function

spr_new_sprite	

	

	stmfd	sp!,{r0-r6,r8-r12}
	
	;mov	r6,#0
	
	;**** wenn zu weit dann setze ausserhal des screens
	
	cmp	r0,#240
	movge	r0,#255
	movge	r1,#255
	
	cmp	r1,#255
	movge	r0,#255
	movge	r1,#255
	
	addr	r11,spr_mg_data
	
	mov	r12,#1
	mov	r9,#0
spr_l
	ldr	r8,[r11,r9, lsr #5]	;

	and	r10,r9,#$1f		;mod 32
	tst	r8,r12,lsl r10
	beq	spr_leer
	
	add	r9,r9,#1
	cmp	r9,#128
	bne	spr_l	
		
	;**** keine leere stelle
	
	mov	r7,#0
	
	b	new_spite_end		

spr_leer	;**** sprite an stelle

	orr	r8,r8,r12, lsl r10	;setze belegt bit
	str	r8,[r11,r9, lsr #5]	;save

	mov	r10,#$7000000
	add	r10,r10,r9, lsl #3	;&sprite[r9] (8 byte pro sprite)

	;**** set daten

	mvn	r8,#$ff			;ffffff00

	
	bic	r0,r0,r8, lsl #1	;x 0..511
	bic	r1,r1,r8		;y 0..255
	
	orr	r0,r0,r5, lsr #16	;orr y
	bic	r5,r5,r8, lsl #8	;r7 = r5 & ffff
	orr	r1,r1,r5 		;orr x

	orr	r1,r1,r6, lsl #9	;rot nummer
		
	;add	r6,r6,r2, lsl #3	;spritenummer
	
	add	r3,r3,r3		;r5<<1 (bit 0 ist undefiniert)
	orr	r3,r3,r4, lsl #10	;set new prio
	
	strh	r0,[r10]			;word 0
	strh	r1,[r10,#2]		;word 1
	strh	r3,[r10,#4]		;word 2

new_spite_end	

	mov	r7,r9			;sprite cnt ist rts

	ldmfd	sp!,{r0-r6,r8-r12}
	bx	lr
	
	
;**** rem_sprite
;*
;* r0.l = spritedata	
;*

	.global	spr_rem_sprite
	.type	spr_rem_sprite,function

spr_rem_sprite

	stmfd	sp!,{r1,r3,r4,r5}
	
	addr	r1,spr_mg_data

	and	r3,r0,#$1f		;mod 32
	mov	r4,#1
	
	ldr	r5,[r1,r0, lsr #5]
	bic	r5,r5,r4,lsl r3
	str	r5,[r1,r0, lsr #5]
	
	mov	r5,#$7000000
	add	r5,r5,r0, lsl #3	;&sprite[r9] (8 byte pro sprite)
		
	mov	r0,#$00a00000		;y=160	
	mov	r1,#0
		
	str	r0,[r5],#4
	strh	r1,[r5]
		
	ldmfd	sp!,{r1,r3,r4,r5}
	bx	lr
	
;**** spr_change_xy
;*
;* r0.l = spritedata	
;* r1   = x
;* r2   = y
;*

	.global	spr_change_xy
	.type	spr_change_xy,function


spr_change_xy

	stmfd	sp!,{r3,r4,r5,r6,r7}
	
	cmp	r1,#-64
	movle	r1,#240
	movle	r2,#255

	cmp	r2,#-64
	movle	r1,#240
	movle	r2,#255
	
	cmp	r1,#240		
	movgt	r1,#240
	movgt	r2,#255
	
	cmp	r2,#160		
	movgt	r1,#240
	movgt	r2,#255
	
	
	addr	r7,spr_mg_data

	ldr	r6,[r7,r0, lsr #5]	;r2 / 32
	and	r3,r0,#$1f		;mod 32
	mov	r4,#1
	
	tst	r6,r4, lsl r3
	beq	spr_not_sprcxy_allocated
	
	mov	r6,#$7000000
	add	r6,r6,r0, lsl #3	;&sprite[r9] (8 byte pro sprite)
	
	;$01ff 00ff
	;$fe00 ff00
	
	
	
	mvn	r3,#$0
	and	r1,r1,r3, lsr #23
	and	r2,r2,r3, lsr #24
	

	orr	r5,r2,r1, lsl #16

	mov	r3,#$fe000000
	orr	r3,r3,#$ff00	
	
	ldr	r4,[r6]			;0000 000y yyyy yyyy 0000 0000 xxxx xxxx
	and	r4,r4,r3		;& maske
	orr	r4,r4,r5 		;or x,y
	str	r4,[r6]
	
spr_not_sprcxy_allocated
	
	ldmfd	sp!,{r3,r4,r5,r6,r7}
	bx	lr	
	
;**** spr_change_prio
;*
;* r0.l = spritedata	
;* r1   = new prio
;*

	.global	spr_change_prio
	.type	spr_change_prio,function

spr_change_prio

	stmfd 	sp!,{r3,r4,r6,r7}
	
	addr	r7,spr_mg_data

	ldr	r6,[r7,r0, lsr #5]	;r2 / 32
	and	r3,r0,#$1f		;mod 32
	mov	r4,#1
	
	tst	r6,r4,lsl r3
	beq	spr_not_sprcpr_allocated
	
	mov	r6,#$7000000
	add	r6,r6,r0, lsl #3	;&sprite[r9] (8 byte pro sprite)

	ldrh	r3,[r6,#4]
	bic	r3,r3,#$0c00		;clear old prio
	orr	r3,r3,r1, lsl #10	;set new prio
	strh	r3,[r6,#4]
	
spr_not_sprcpr_allocated	

	ldmfd 	sp!,{r3,r4,r6,r7}
	bx	lr


;**** spr_get_xy
;*
;* r0.l = spritedata	
;*	
;* return:
;*
;* r3 = x
;* r4 = y
;*

	.global	spr_get_xy
	.type	spr_get_xy,function	

spr_get_xy	

	stmfd	sp!,{r0-r2,lr}

	bl	spr_getall		;get datas
	
	mvn	r0,#$0
	and	r3,r2,r0, lsr #23	;$1ff
	and	r4,r1,r0, lsr #24	;$ff

	ldmfd	sp!,{r0-r2,lr}
	bx	lr


;**** spr_get_getall
;*
;* r0.l = spritedata	
;*	
;* return:
;*
;* r1 = W1
;* r2 = W2
;* r3 = W3
;*
	
	.global	spr_getall
	.type	spr_getall,function	
	
spr_getall	

	stmfd 	sp!,{r0}
	
	addr	r2,spr_mg_data

	ldr	r1,[r2,r0, lsr #5]	;r2 / 32
	and	r3,r0,#$1f		;mod 32
	mov	r2,#1
	
	tst	r1,r2, lsl r3
	beq	spr_not_sprga_allocated
	
	mov	r0,r0, lsl #3
	add	r0,r0,#$7000000		;&sprite[r9] (8 byte pro sprite)
	
	ldrh	r1,[r0],#2
	ldrh	r2,[r0],#2
	ldrh	r3,[r0]

spr_not_sprga_allocated	

	ldmfd 	sp!,{r0}
	bx	lr

;**** spr_setall
;*
;* r0.l = spritedata	
;*
;* return:
;*
;* r1.w = W1
;* r2.w = W2
;* r3.w = W3
;*
	.global	spr_setall
	.type	spr_setall,function
	
spr_setall	

	stmfd 	sp!,{r4,r5,r6}
	
	addr	r4,spr_mg_data

	ldr	r5,[r4,r0, lsr #5]	;r2 / 32
	and	r6,r0,#$1f		;mod 32
	mov	r4,#1
	
	tst	r5,r4,lsl r6
	beq	spr_not_sprsa_allocated
	
	mov	r6,#$7000000
	add	r6,r6,r0,lsl #3		;&sprite[r9] (8 byte pro sprite)
	
	strh	r1,[r6],#2
	strh	r2,[r6],#2
	strh	r3,[r6]
	
	ldmfd 	sp!,{r4,r5,r6}
	bx	lr
	
spr_not_sprsa_allocated	

	ldmfd 	sp!,{r3,r4,r6,r7}
	bx	lr
	
	
;**** spr_get_hwsprnum (gets hardware spritenum!! NUR FÜR NOTFÄLLE BENUTZEN)
;*
;* r0.l = spritedata	
;*
;* return:
;*
;* r1   = hardware sprite nummer (or -1 if not allocated)
;*	
;spr_get_hwsprnum
;
	;stmfd 	sp!,{r3,r4,r5}

	;addr	r3,spr_mg_data

	;ldr	r5,[r3,r0 lsr 5]	;r2 / 32
	;and	r3,r0,#$1f		;mod 32
	;mov	r4,#1
	
	;tst	r5,r4 lsl r3
	;mvneq	r1,#0			;-1
	;movne	r1,r0			;
	
	;ldmfd 	sp!,{r3,r4,r5}
	;bx	lr
	
;*****************************************************************************************************************
;*
;*	WRAM 
;*
;*****************************************************************************************************************

	.section    	.ewram,"ax"
	.align	4
	
spr_mg_data	.zero	4*4
spr_objectbuf	.hword	0,0,0
	
	.end

		
	