@echo   ""
@echo   " Programmgroesse (ROM) ~ $",endofromcode-startofromcode, "Bytes"
@echo	" Programmgroesse (IRAM)= $",endofiwramcode-startofiwramcode-(endofeiramdata-startofeiramdata), "Bytes"
@echo   ""
@echo   " Datengroesse    (ROM) ~ $",endofromdata-startofromdata, "Bytes"
@echo   " Datengroesse    (WRAM)= $",endofewramcode-startofewramcode, "Bytes"
@echo   " Datengroesse    (IRAM)= $",endofeiramdata-startofeiramdata, "Bytes"

	
	
@macro exg RegisterA, RegisterB

	eor 	RegisterA, RegisterA, RegisterB
	eor 	RegisterB, RegisterB, RegisterA
	eor 	RegisterA, RegisterA, RegisterB

	@endm	

@macro exgne RegisterA, RegisterB

	eorne 	RegisterA, RegisterA, RegisterB
	eorne 	RegisterB, RegisterB, RegisterA
	eorne 	RegisterA, RegisterA, RegisterB

	@endm		

@macro exgle RegisterA, RegisterB

	eorle 	RegisterA, RegisterA, RegisterB
	eorle 	RegisterB, RegisterB, RegisterA
	eorle 	RegisterA, RegisterA, RegisterB

	@endm		
	
@define DMAMODE			;für batgba ausschalten

	b	startofromcode		;skip header
	@include nintendologo.s		;nintendo logo (sonst macht original ärger)
	@dcb	"vector test3"		;spiel name (12 byte)
	@dcb	"vect"			;game code
	@dcb	"vt"			;marer code :)
	@dcb	$96			;must be $96
	@dcb	0			;main unit code
	@dcb	0			;device type
	@dcb	0,0,0,0,0,0,0		;resv. area
	@dcb	0			;software version
	@dcb	0			;complement check (wird mit gbarm gesetzt), sonst gehts nicht auf original gba
	@dcb	0,0			;reserved area
	
	
	
;*************************** ROM CODE ***********************************
startofromcode
            
	;**** wichtige sachen

	;ldr	sp,=mystack
	ldr	sp,[mystackadr]		;mystack	
	
	;**** schalte prefetch an
	
	mov 	r6,#$4000000   		;dma
	orr	r5,r6,#$200
	orr	r5,r5,#$4
	mov	r0,#$4300
	orr	r0,r0,#$17
	strh	r0,[r5]
	
	;**** kopiere iramcode            
            
	ldr 	r0,[startofiwramcodeadr]
	mov	r1,#$3000000
	ldr 	r3,[endofiwramcodeadr]
	sub	r2,r3,r0
	bl	dmacpy			     		
	
	;**** schalte irqs an
	
	ldr	r0,[irqhandleradr]
	ldr	r1,[irqvectoradr]
	str	r0,[r1]

	mov 	r6,#$4000000   		;dma
	mov	r0,#$08			;v blank irq enable
	strh	r0,[r6,4]		;dispstat
	
	mov	r0,#$1			;v blank / irq master
	add	r5,r6,#$200
	strh	r0,[r5]			;v blank irq enable
	
	mov	r0,#1
	strh	r0,[r5,8]		;irq master enable
	
	;**** setze bildschirm
	
	mov 	r6,#$4000000   		;dma
	mov	r1,#$0400
	orr	r1,r1,#$04		;mode 3 bg 2	
	strh 	r1,[r6]			;store
			

	mov	r6,#$5000000
	add	r6,r6,#(256*2)
	sub	r6,r6,#2			
	mov	r0,#256
colorl
	sub	r1,r0,#1
	mov	r1,r1 lsr 3
	orr	r1,r1,r1 lsl 5
	orr	r1,r1,r1 lsl 10
	strh	r1,[r6],-2
	subs	r0,r0,#1
	bne	colorl	


	mov	r6,#$5000000
	mvn	r0,#0
	strh	r0,[r6,2]

	
	
	
;*************************** 

	ldr	pc,[nochmaladr]
	
		
;*************************** irqhandler (30.11.2002)
;*
;* r0 -r3 werden von bios/irq gesichert
;*
;**
irqhandler

	mov	r3,sp
	ldr	sp,[irqstackadr]	;mein irq stack
	stmfd 	sp!,{r3,lr}

	mov	r0,#$4000000
	orr	r0,r0,#$200
		
	ldrh	r2,[r0,2]		;irq lesen
	strh	r2,[r0,2]		;irq ausschalten

	ands	r3,r2,#1		;vblank
	blne	vblank	


	ldmfd	sp!,{r3,lr}
	mov	sp,r3
	bx	lr	
	
vblank
	stmfd	sp!,{r4-r12}

	ldr	r0,[timeradr]
	
	ldr	r1,[r0]
	cmp	r1,#60
	moveq	r1,#0
	addne	r1,r1,#1
	str	r1,[r0]
	
	bne	noCalcFps
	
	ldr	r3,[timer2iadr]
	ldr	r4,[fpsadr]
	ldr	r0,[r3]		
	str	r0,[r4]	
	mov	r0,#0
	str	r0,[r3]	
		
		
	
	;
	;
	
noCalcFps	
	
	;**** zeichne fps
	
	
	ldr	r7,[fontadr]
	add	r7,r7,#64*21
	ldr	r6,[gfxbufferiadr]
	add	r6,r6,#8*1
	
	ldr	r0,[fpsadr]
	ldr	r9,[r0]
	
	mov	r5,#2
nextfig	
	
	
	mov	r0,r9
	mov	r1,#10
	swi	$60000
	mov	r9,r0
	
	add	r8,r7,r1 lsl 6	;*64	
	
	mov	r10,#8
drawfig
	ldmia	r8!,{r3,r4}
	stmia	r6,{r3,r4}

	add	r6,r6,#240

	subs	r10,r10,#1
	bne	drawfig	
	
	
	sub	r6,r6,#240*8
	sub	r6,r6,#8	
	
	subs	r5,r5,#1
	bne	nextfig
	
	
	ldmfd	sp!,{r4-r12}
	mov	pc,lr		
		
;*********************** Unterprogramme (extern) *******************

	@include	simple.s 
			
	
;******************************************************************************************
;******************************************************************************************
;**                                                                                      **
;**                                 Variablenadressen                                    **
;**                                                                                      **
;******************************************************************************************
;******************************************************************************************	
		      
mystackadr		@dcd	mystack	
nochmaladr		@dcd	nochmal

irqhandleradr		@dcd	irqhandler   
irqvectoradr		@dcd	$03007ffc   
irqstackadr		@dcd	irqstack

timeradr		@dcd	timer
timer2iadr		@dcd	timer2
fontadr			@dcd	font
gfxbufferiadr		@dcd	gfxbuffera
fpsadr			@dcd	fps

startofiwramcodeadr	@dcd	startofiwramcode
endofiwramcodeadr	@dcd	endofiwramcode

; @pool
			@include	sin.s
			
endofromcode		
	
;*************************** IRAM CODE ************************************
startofiwramcode	
		@textarea $03000000
		
	
sintabadr	@dcd	sintab
timeradr2	@dcd	timer2

nochmal

	;****************************************************************
	;*los gehts
	;****************************************************************

	bl	generateDiffTab

loop	

	;****	rotate points	

	ldr	r0,[pointsadr]		
	ldr	r1,[pointscnt]		
	ldr	r2,[points2adr]		
	ldr	r10,[winkel_x]
	ldr	r11,[winkel_y]
	ldr	r12,[winkel_z]
	
	bl	rotatePoints
	
	;****   berechne Polygontiefen
	
	ldr	r10,[polycnt]
	ldr	r11,[points2adr]
	ldr	r12,[polysadr]
	
	bl	calcDepth

	;****   sortiere Polygontiefen
	
	ldr	r0,[polysadr]		;polygone
	ldr	r2,[polycnt]		
	
	mov	r1,#0			;start = 0
	sub	r2,r2,#1		;ende  = anzahl-1
	
	ldr	r10,[getcontadr]	;get[x]
	ldr	r11,[swapcontadr]	;swap[x]
	bl	qsort_back
	
	;**** 	zeichne Polygone

	ldr	r6,[polyzoom]
	ldr	r10,[polycnt]
	ldr	r11,[points2adr]
	ldr	r12,[polysadr]
	
	bl	drawPolyFlatMode3

	;****	ändere x,y,z winkel

	ldr	r10,[winkel_x]
	ldr	r11,[winkel_y]
	ldr	r12,[winkel_z]

	sub	r10,r10,#9
	add	r11,r11,#8
	add	r12,r12,#6
	
	str	r10,[winkel_x]
	str	r11,[winkel_y]
	str	r12,[winkel_z]
 
	;****	kopiere auf Bildschirm,....

	;mov	r2,#160
	;bl	waiti
	bl	copyScreen

	ldr	r0,[polyzoom]
	
	mov	r10,#0x04000000
	orr	r10,r10,#$130
	ldrh	r11,[r10]	;alte taste	
	
	ands	r12,r11,#%1		;taste a
	addeq	r0,r0,#4		
	
	ands	r12,r11,#%10		;taste b
	subeq	r0,r0,#4			

	str	r0,[polyzoom]	

	ldr	r0,[timeradr2]
	ldr	r1,[r0]
	add	r1,r1,#1
	str	r1,[r0]

	b	loop
	
;*******************************************************************
;*
;* copyScreen	(Puffer ins Bild kopieren und Puffer initialisieren)
;*
;*******************************************************************

copyScreen

	; to vram

	ldr	r0,[gfxbufferadr]	
	mov	r1,#$4000000
	add	r1,r1,#$d4
	mov	r2,#$6000000		
	mov	r3,#$8400<<16
	orr	r3,r3,#240*160/4
	stmia	r1,{r0,r2,r3}		;dma 0 start
	
	; clear buffer
	
	
	mov	r0,#0
	stmfd	sp!,{r0}		;store for dma and save color
	
	mov	r0,sp	
	mov	r1,#$4000000
	add	r1,r1,#$d4
	ldr	r2,[gfxbufferadr]		
	mov	r3,#$8500<<16
	orr	r3,r3,#240*160/4
	stmia	r1,{r0,r2,r3}		;dma 0 start

	ldmfd	sp!,{r0}		;restore color

	mov	pc,lr	
	
;*******************************************************************
;*
;* waiti	(warte auf Zeile r2)
;*
;******************************************************************* 

waiti
	stmfd 	sp!,{r0,r1}

	mov	r1,#$4000000
        
waitvi	ldrh	r0,[r1,6]
	cmp	r0,r2
	bne	waitvi	 

	ldmfd	sp!,{r0,r1}
	move	pc,lr

;*******************************************************************
;*
;* includes
;*
;******************************************************************* 	

	@include	3d.s			;Punkt/Poly Funktionen
	@include	qsort.s			;qsort
	@include	polyDraw.s		;Zeichenfunktionen

;**************************** IRAM DATA ****************************

startofeiramdata
		
;**********************************

winkel_x	@dcd	$60
winkel_y	@dcd	$320
winkel_z	@dcd	$280
polyzoom	@dcd	$200
	

gfxbufferadr	@dcd	gfxbuffera
gfxbgradr	@dcd	gfxbgr


@define	POLY_CNT	512;512
@define POINT_CNT	258

pointscnt	@dcd	POINT_CNT
pointsadr	@dcd	points
points2adr	@dcd	points2

polysadr	@dcd	polys
polycnt		@dcd	POLY_CNT

;**********************************		
	
;****	Ball

		@include	woman.s
		


endofeiramdata
		@endarea
		
endofiwramcode 		
 	    
 
;*************************** ROM DATA ***********************************
startofromdata

gfxbgr		@incbin		background.gfx
font		@incbin		font.gfx
		
              
endofromdata 
                         
;*************************** WRAM DATA ***********************************

startofewramcode
		@textarea $02000000
		
fps		@dcd	0		
timer		@dcd	0
timer2		@dcd	0

		@DCD	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
		@DCD	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
irqstack	@DCD	0;
		
		@dcb	"HALLO"
		@dup	dcd	8192,0
mystack		@DCD	0;	32k Byte Stack + (4Byte sec)

pfb_start	@dup	dcb	160,0
pfb_stop	@dup	dcb	160,0

pfb_y1		@dcw	0
pfb_y2		@dcw	0

diff_tab_r3	@dup	dcw	512,0
gfxbuffera	@dup	dcb	240*160*2,0

points2		@dup	dcd	(3*POINT_CNT),0	;polygone punkte	



		@endarea
endofewramcode




