
    	.global     _start
    	.align
    	.arm

	.include	"car.i"
	.include	"sound_mod.i"
	.include	"sound_mix.i"
	.include	"mem_macros.i"
	.include	"road.i"
	.include	"playfield.i"

;*****************************************************************************************************************
;*
;*	ROM start 
;*
;*****************************************************************************************************************

_start


	b	startofromcode		;skip header
	.include "nintendologo.i"
	.ascii	"testgame    "		;spiel name (12 byte)
	.ascii	"game"			;game code
	.ascii	"sp"			;marer code :)
	.byte	$96			;must be $96
	.byte	0			;main unit code
	.byte	0			;device type
	.byte	0,0,0,0,0,0,0		;resv. area
	.byte	0			;software version
	.byte	0			;complement check (wird mit gbarm gesetzt), sonst gehts nicht auf original gba
	.byte	0,0			;reserved area

;*****************************************************************************************************************
;*
;*	PRG start 
;*
;*****************************************************************************************************************

	
startofromcode

	;**** wichtige sachen

        mov     r0, #$12               	;supervisor
        mov     r1, #$1f               	;system 
        msr     cpsr, r0
        ldr     sp,=myirqstackend     	;myirqstack
        msr     cpsr, r1
	ldr	sp,=mystackend		;mystack	
	
	;**** schalte prefetch an
	
	ldr 	r6,=$4000204   		;dma
	ldr	r0,=$4317
	strh	r0,[r6]
	
	
	;**** lösche bss
	
	ldr     r1,=__bss_start
        ldr     r2,=__bss_end
        sub     r2,r2,r1
        bl      memclear32
	
	;**** kopiere iramcode       

	ldr	r0,=__iwram_lma
	ldr	r1,=__iwram_start
	ldr	r2,=__iwram_end
	sub	r2,r2,r1
	bl	dmacpy
	
	;**** kopiere wram 
	
	ldr	r0,=__ewram_lma
	ldr	r1,=__ewram_start
	ldr	r2,=__ewram_end
	sub	r2,r2,r1
	bl	dmacpy

	;**** kopiere data

	ldr     r0,=__data_lma
        ldr     r1,=__data_start
        ldr     r2,=__data_end
	sub	r2,r2,r1
	bl	dmacpy
	

	;**** setze irq

	ldr	r0,=$03007ffc  
	addr	r1,irq
	str	r1,[r0]

	;**** setze bildschirm
	
	mov 	r6,#$4000000   		;
	mov	r1,#$1c00		;mode 0 bg 2/3 + sprites
	strh 	r1,[r6]			;store

	;set irq (vblank)

	mov	r4,#$4000000
	add	r5,r4,#$200
	mov	r3,#1			;vblank
	strh	r3,[r5]
	mov	r3,#$8
	strh	r3,[r4,#4]		;vblank
	
	;**** dynamischen Speicher initalisieren
	
	mem_init_mc	membaseW_adr,#131072		;init 128k 
	
	
	;**** show logo
	
	bl	show_logo
	
	;**** show logo
	
	bl	show_titel
	
	;**** INITIALISIERE ROAD
	
	mov	r0,#R_LENGTH
	
	stack_alloc_mc	r0		;get mem for Strecken_strukt (auf stack)
	
	ldr	r2,=drive_gfx1		
	ldr	r3,=drive_pal1		
	ldr	r4,=drive_map1a		
	ldr	r5,=drive_map1b		
	ldr	r6,=drive_map1a_spez	
	ldr	r7,=drive_map1b_spez_index	
	ldr	r8,=sprite_anim		
	ldr	r9,=sprite_anim_pal		
	
	str	r2,[r1,#R_GFX]
	str	r3,[r1,#R_PAL]
	str	r4,[r1,#R_MAP_1]
	str	r5,[r1,#R_MAP_2]
	str	r6,[r1,#R_MAP_SP]
	str	r7,[r1,#R_MAP_SPI]
	str	r8,[r1,#R_SPRITE]
	str	r9,[r1,#R_SPRITE_PAL]
	
	ldr	r2,=drive_map1c
	str	r2,[r1,#R_MAP_3]
	
	mov	r11,r1			;gib struct
	ldr	r1,=drive_struct
	str	r11,[r1]
	bl	init_road		;init 
	
							
	ldr	r11,=drive_struct
	ldr	r11,[r11]
	
	ldr	r0,[r11,#R_PLAYFIELD]	;get playfield
	ldr	r1,[r11,#R_CARS]	;get the 1. car
	
	ldr	r11,[r1,#CAR_X_POS]	;get cx
	ldr	r12,[r1,#CAR_Y_POS]	;get cy
		
	bl	set_screen_centered_to_sprite	
	
	ldr	r0,=drive_struct	
	ldr	r0,[r0]
	bl	recalc_sprite_positions	

	ldr	r11,=drive_struct
	ldr	r11,[r11]
	bl	test_drive
	
h	b	h	
	
	;**** delete road
	ldr	r1,=drive_struct
	ldr	r11,[r1]
	bl	delete_road
	
	stack_free_mc			;free road
	
end	b	end

	.pool

;*****************************************************************************************************************
;*
;*	Constante Variablen
;*
;*****************************************************************************************************************

	.align	4


	
	
;*****************************************************************************************************************
;*
;*	ROM DATA 
;*
;*****************************************************************************************************************


		.align	4

drive_gfx1		.include 	"gfx/tiles.gfx.lz.i"
drive_pal1		.include 	"gfx/tiles.pal.i"

drive_map1a		.include	"maps/mapcars_test_bg0.i"
drive_map1b		.include	"maps/mapcars_test_bg1.i"
drive_map1c		.include	"maps/mapcars_test_bg2.i"

drive_map1a_spez	.include	"maps/mapcars_test_bg_spez.i"
drive_map1b_spez_index	.include	"maps/mapcars_test_bg_spez_index.i"

;car_sprite1		.include	"gfx/spritecar.gfx.lz.i"
;car_sprite1pal		.include	"gfx/spritecar.pal.i"

sprite_anim		.include	"gfx/fisch_anim.gfx.s"
sprite_anim_pal 	.include	"gfx/fisch_anim.pal.s"

;*****************************************************************************************************************
;*
;*	IRAM 
;*
;*****************************************************************************************************************
	
	
  	.SECTION    .iwram,"ax"  
    	.ARM
    	.align		4

irq	

	add	r1,r0,#$200
								
	ldrh	r2,[r1,#2]		;irq lesen
	strh	r2,[r1,#2]		;irq ausschalten
	strh	r2,[r0,#-8]		;save irq bits fürs bios $30007ff8 
					;(mirror auf $3ffffff8 )

	mov	r0,r2
	ldr	r1,=do_irq_th+1		;irq handler
	bx	r1          

	.pool
	.bss
		
myirqstack:	.zero	512			;512 Byte irqstack
myirqstackend:	
	

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

	.section    	.ewram,"ax"
	
 						;bss
    	.global		mystack
  	.global		mystackend
  	.global		stacksave
  	.global		stringbuffer	
  
stacksave	.long	0
mystack		.fill	8192/4,4,$abcddcba		;8K STACK
mystackend
	
drive_struct	.long	0
	
		.global	membaseW_adr	
		.align	4
		.ascii	"MEMW"
membaseW_adr	.fill	65536				;64K HEAP	
		
	.end
		