
    	.global     _start
    	.align
    	.arm

	.include	"sound_mod.i"
	.include	"sound_mix.i"


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

_start


	b	startofromcode		;skip header
	.include "nintendologo.i"
	.ascii	"modplayer   "		;spiel name (12 byte)
	.ascii	"mdpl"			;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 bildschirm
	
	mov 	r6,#$4000000   		;dma
	mov	r1,#$0100
	strh 	r1,[r6]			;store

	;**** copy font
	
	mov	r1,#$0200
	strh	r1,[r6,#8]		;gb0 = map=4096
	
	addr	r0,font_16
	mov	r1,#$6000000
	mov	r2,#(3296)
	bl	dmacpy	
	
	addr	r0,font_16pal
	mov	r1,#$5000000
	mov	r2,#16*2
	bl	dmacpy	

	;**** setze irq

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

	;************************************************ MODSACHEN

	addr	r0,mod
	bl	init_mod		;init_mod
	
	bl	start_mod		;start_mod
	

	
	;**** textausgabe
	
loop	swi	$20000

	mov	r2,#160
	bl	wait
		
	addr	r12,modplayer
	addr	r11,modadr
	ldr	r11,[r11] 

	;**** get rows & patterns

	ldr	r1,[r12,#MOD_ROW_POS]
	ldr	r2,[r12,#MOD_PATTERN_POS]
	
	;**** get pattern
	
	add	r10,r11,r2		;+patternpos
	ldrb	r3,[r10,#952]		;patterindexarray
	
	str	r3,[r12,#MOD_AKT_PATTERN]
	
	;****

	add	r10,r11,#$400
	add	r10,r10,#$03c		;mod+1084 = patternarray

	add	r10,r10,r3, lsl #10	;adr = (pattern*1024) + (mod+1084)
	add	r10,r10,r1, lsl #4	;adr = rowpos * 16

	;**** clrscr
	
	ldr	r1,=$6001000
	addr	r4,clrscr
	mov	lr,pc
	bx	r4
	
	;**** Überschrift

	;ldr	r0,=stringbuffer
	;ldr	r1,=chaneltxt
	;ldr	r4,=sprintf
	;mov	lr,pc
	;bx	r4

	;ldr	r0,=stringbuffer
	;ldr	r1,=$6001000+(4*2)+(1*64)	
	;bl	b2wchar	

	;mov	r0,#$5000000
	;mov	r1,#$1f<<10
	;strh	r1,[r0]

	;**** Chanel Noten

	;ldr	r0,=stringbuffer
	;ldr	r1,=chanelnotes
	
	;ldr	r2,[r10]		;cmd0
	;addr	r3,sampleX1
	;ldr	r3,[r3,#INSTR_VOLUME]	;vol0
	;mov	r3,r3, lsr #3
	
	;ldr	r4,[r10,#4]		;cmd1
	;addr	r5,sampleX2
	;ldr	r5,[r5,#INSTR_VOLUME]	;vol1
	;mov	r5,r5, lsr #3
	
	;ldr	r6,[r10,#8]		;cmd2
	;addr	r7,sampleX3
	;ldr	r7,[r7,#INSTR_VOLUME]	;vol2
	;mov	r7,r7, lsr #3
	
	;ldr	r8,[r10,#12]		;cmd3
	;addr	r9,sampleX1
	;ldr	r9,[r9,#INSTR_VOLUME]	;vol3
	;mov	r9,r9, lsr #3	
	
	;stmfd	sp!,{r4,r5,r6,r7,r8,r9}
	
	;ldr	r4,=sprintf
	;mov	lr,pc
	;bx	r4
	
	;add	sp,sp,#4*6


	;ldr	r0,=stringbuffer
	;ldr	r1,=$6001000+(3*64)	
	;bl	b2wchar	
	
	;mov	r0,#$5000000
	;mov	r1,#$0
	;strh	r1,[r0]

	ldr	r0,=
	ldr	r1,=$6001000
	mov	r2,#4
	mov	r3,#1
	addr	r4,puts
	mov	lr,pc
	bx	r4

	ldr	r0,[r10]
	ldr	r1,=$6001000
	mov	r2,#4
	mov	r3,#3
	addr	r4,puts_hex
	mov	lr,pc
	bx	r4
	
	ldr	r0,[r10,#4]
	ldr	r1,=$6001000
	mov	r2,#4
	mov	r3,#4
	addr	r4,puts_hex
	mov	lr,pc
	bx	r4
	
	ldr	r0,[r10,#8]
	ldr	r1,=$6001000
	mov	r2,#4
	mov	r3,#5
	addr	r4,puts_hex
	mov	lr,pc
	bx	r4
	
	ldr	r0,[r10,#12]
	ldr	r1,=$6001000
	mov	r2,#4
	mov	r3,#6
	addr	r4,puts_hex
	mov	lr,pc
	bx	r4
	
	addr	r0,sampleX1
	ldr	r0,[r0,#INSTR_VOLUME]
	mov	r0,r0, lsr #3
	ldr	r1,=$6001000
	mov	r2,#16
	mov	r3,#3
	addr	r4,puts_dec
	mov	lr,pc
	bx	r4
	
	addr	r0,sampleX2
	ldr	r0,[r0,#INSTR_VOLUME]
	mov	r0,r0, lsr #3
	ldr	r1,=$6001000
	mov	r2,#16
	mov	r3,#4
	addr	r4,puts_dec
	mov	lr,pc
	bx	r4
	
	addr	r0,sampleX3
	ldr	r0,[r0,#INSTR_VOLUME]
	mov	r0,r0, lsr #3
	ldr	r1,=$6001000
	mov	r2,#16
	mov	r3,#5
	addr	r4,puts_dec
	mov	lr,pc
	bx	r4
	
	addr	r0,sampleX4
	ldr	r0,[r0,#INSTR_VOLUME]
	mov	r0,r0 ,lsr #3
	ldr	r1,=$6001000
	mov	r2,#16
	mov	r3,#6
	addr	r4,puts_dec
	mov	lr,pc
	bx	r4

	addr	r0,patterntxt
	ldr	r1,=$6001000
	mov	r2,#4
	mov	r3,#8
	addr	r4,puts
	mov	lr,pc
	bx	r4

	ldr	r0,[r12,#MOD_ROW_POS]
	ldr	r1,=$6001000
	mov	r2,#4
	mov	r3,#10
	addr	r4,puts_dec
	mov	lr,pc
	bx	r4
	
	ldr	r0,[r12,#MOD_PATTERN_POS]
	ldr	r1,=$6001000
	mov	r2,#8
	mov	r3,#10
	addr	r4,puts_dec
	mov	lr,pc
	bx	r4
	
	ldr	r0,[r12,#MOD_AKT_PATTERN]
	ldr	r1,=$6001000
	mov	r2,#13
	mov	r3,#10
	addr	r4,puts_dec
	mov	lr,pc
	bx	r4

	addr	r0,mastertxt
	ldr	r1,=$6001000
	mov	r2,#4
	mov	r3,#12
	addr	r4,puts
	mov	lr,pc
	bx	r4

	addr	r0,sampleXAll
	ldr	r0,[r0,#MASTER_VOLUME]
	ldr	r1,=$6001000
	mov	r2,#20
	mov	r3,#12
	addr	r4,puts_hex
	mov	lr,pc
	bx	r4

	addr	r0,cputxt
	ldr	r1,=$6001000
	mov	r2,#4
	mov	r3,#14
	addr	r4,puts
	mov	lr,pc
	bx	r4
	
	addr	r0,lineL
	addr	r1,lineH

	ldr	r0,[r0]
	ldr	r1,[r1]
	

	
	cmp	r0,r1
	subhi	r0,r0,#228
	
	sub	r0,r1,r0
	mov	r10,r0	
		
	ldr	r1,=(1232*50*100)
	mul	r0,r0,r1	
	mov	r0,r0, lsr#24
		
	ldr	r1,=$6001000
	cmp	r0,#10
	movge	r2,#19
	movlt	r2,#20
	mov	r3,#14
	addr	r4,puts_dec
	mov	lr,pc
	bx	r4

	mov	r0,r10
	ldr	r1,=$6001000
	mov	r2,#10
	mov	r3,#14
	addr	r4,puts_dec
	mov	lr,pc
	bx	r4
		
	;Change MasterVolume

	addr	r0,sampleXAll
	ldr	r1,[r0,#MASTER_VOLUME]
	
	mov	r2,#$4000000
	ldr	r2,[r2,#$130]
	
	tst	r2,#1<<8
	addeq	r1,r1,#1
	tst	r2,#1<<9
	subeq	r1,r1,#1
	
	cmp	r1,#0
	movlt	r1,#0
	cmp	r1,#$200
	movgt	r1,#$200

	str	r1,[r0,#MASTER_VOLUME]	


	b	loop


	
	.pool
	
chaneltxt:	.asciz	"Chanelcmds Volume"	
patterntxt:	.ascii	"ROW PPOS PATTERN\000"
mastertxt:	.ascii	"Master Volume = \000"
cputxt:		.asciz	"Lines      CPU   %"

			;0123456789abcdef0123456789abcdef
chanelnotes	.ascii	"     %8.8x    %2.2d             "
		.ascii	"     %8.8x    %2.2d             "
		.ascii	"     %8.8x    %2.2d             "
		.asciz	"     %8.8x    %2.2d             "
		


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


		.align	4

font_16		.include	"font_16_2.gfx.i"
font_16pal	.include	"font_16_2.pal.i"

		.global	mod

mod		.include	"test.mod.i"  

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

irq	

	stmfd	sp!,{lr}
	
	mov	r0,#$4000000
	add	r1,r0,#$100
	add	r2,r0,#$200
				
	;stop irq

	ldrh	r3,[r2,#2]		;irq lesen
	strh	r3,[r2,#2]		;irq ausschalten

	cmp	r3,#$10			;Timer 1 Overflow (Sound)
	bleq	irq_mod			;go to mod
	
	
	ldmfd	sp!,{lr}
	bx	lr

	.bss
		
myirqstack:	.zero	256			;irqstack
myirqstackend:	
	

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

	.section    	.ewram,"ax"
	
 						;bss
    	.global		mystack
  	.global		mystackend
  	.global		stacksave
  	.global		stringbuffer	
  
stacksave	.long	0
mystack		.fill	8192/4,4,$abcddcba		;STACK
mystackend
stringbuffer	.fill	256	
		
	.end
		