
    	.global     _start
    	.align
    	.arm


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

_start


	b	startofromcode		;skip header
	.include "nintendologo.i"
	.ascii	"anim1dmaThX3"		;spiel name (12 byte)
	.ascii	"anim"			;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  
	ldr	r1,=irq
	str	r1,[r0]
	
	ldr	r0,=$4000200
	mov	r1,#1
	strh	r1,[r0]			;vbl
	strh	r1,[r0,#8]		;irqm
	
	;**** setze bildschirm
	
	mov 	r6,#$4000000   		;dma
	ldr	r1,=$0100		;mode 0 bg 1
	mov	r2,#$8
	strh 	r1,[r6]			;store
	strh	r2,[r6,#4]		;vb an

	ldr	r1,=$1380		;38912 = $9800
	str	r1,[r6,#8]		;bg0 ctrl

	;**** generiere map

	ldr	r0,=$6009800
	
	mov	r2,#0
	mov	r3,#20
tfilly	mov	r1,#30
tfillx	strh	r2,[r0],#2
	add	r2,r2,#1
	subs	r1,r1,#1
	bne	tfillx
	add	r0,r0,#2*2	
	subs	r3,r3,#1
	bne	tfilly
end	
	
	
; ANIMFORMAT
;
; - Header
;
; 8 = "GOLDMOMO"
; 8 = "ANIMTYP(1|2)" (1 ist RLE+XOR; 2 ist LZ+RLE+XOR)
; 4 = width
; 4 = height
; 4 = depth
; 4 = type
; 4 = Framecount
;
; - Firstframe (rledec)
;
; 4 = rlelen 
; 2 = key
; 2 = pad
; X = cmap (rlelen)
; Y = padbytes to 4 alligned
;
; 4 = rlelen
; 2 = key
; 2 = pad 
; X = firstframe (rlelen)
; Y = padbytes to 4 alligned
;
; - Framecount-1 (frames rlexordec)
;
; 4 = rlelen (not long alligned)
; 2 = key
; 2 = pad
; X = cmap (rlelen)
; Y = padbytes to 4 alligned
;
; 4 = datalen (wenn TYPE 2)
; 4 = rellen
; 2 = key
; 2 = pad
; X = data (datalen TYPE 2) (rlelen TYPE 1) 
; Y = padbytes to 4 alligned
;
	
	ldr	r12,=anim
	ldrb	r10,[r12,#15]		;Version 
	add	r12,r12,#32		;skip Header

	ldr	r11,[r12],#4		;framecounter

	;** first frame colors

	ldr	r1,[r12],#4		;rlelen
	ldrh	r2,[r12],#4		;rlkey
	mov	r3,r12			;cmap
	
	mov	r6,r1
	tst	r6,#$3			;kein durch 4 teilbar?
	bicne	r6,r6,#$3		;dann rest weg
	addne	r6,r6,#4		;und 4 plus
	add	r12,r12,r6		;next chunk
	
	mov	r5,#$5000000		;dest is pallette
	ldr	r8,=rledec_th+1
	mov	lr,pc
	bx	r8

	;** first frame gfx
	
	ldr	r1,[r12],#4		;rellen	
	ldrh	r2,[r12],#4		;rlkey (+2 dummy)
	mov	r3,r12			;gfx
	
	mov	r6,r1
	tst	r6,#$3			;kein durch 4 teilbar?
	bicne	r6,r6,#$3		;dann rest weg
	addne	r6,r6,#4		;und 4 plus
	add	r12,r12,r6		;next chunk
	
	mov	r5,#$6000000		;dest is pallette
	ldr	r8,=rledec_th+1
	mov	lr,pc
	bx	r8
	
	;** play frames
	sub	r11,r11,#1		;famecounter-1
	
floop

	


	;** frame colors

	ldr	r1,[r12],#4		;rellen
	ldrh	r2,[r12],#4		;rlkey (+2dummy)
	mov	r3,r12			;cmap
	
	mov	r6,r1
	tst	r6,#$3			;kein durch 4 teilbar?
	bicne	r6,r6,#$3		;dann rest weg
	addne	r6,r6,#4		;und 4 plus
	add	r12,r12,r6		;next chunk
	
	mov	r5,#$5000000		;source is pallette
	mov	r7,#$5000000		;dest is pallette
	ldr	r8,=rlexordec_th+1
	mov	lr,pc
	bx	r8

	;**  frame gfx
	
	cmp	r10,#'2'		;nur mode
	ldreq	r1,[r12],#4		;chunklen
	ldr	r4,[r12],#4		;rlelen	
	
	ldrh	r2,[r12],#4		;rlkey
	mov	r3,r12			;gfx
	
	cmp	r10,#'2'		;nur mode
	moveq	r6,r1
	movne	r6,r4
	
	tst	r6,#$3			;kein durch 4 teilbar?
	bicne	r6,r6,#$3		;dann rest weg
	addne	r6,r6,#4		;und 4 plus
	add	r12,r12,r6		;next chunk
	
	;r3 = rlelen
	
	cmp	r10,#'2'		;nur mode
	bne	mode_1
		
		
	stmfd	sp!,{r0-r12}	
	
	;memset32(' ',tbuffer,N-F); n-f = 4080
	ldr	r1,=tbuffer
	mov	r2,#4080
	bl	memclear32		
	ldmfd	sp!,{r0-r12}
		
	stmfd	sp!,{r0-r12}
	
	;unsigned char *in,char *out,unsigned char *inmax,unsigned char *tbuffer
	mov	r0,r3			;source
	add	r2,r0,r1		;len+source
	ldr	r1,=lzbuffer		;dest
	
	ldr	r3,=tbuffer
	
	ldr	r12,=lzss_dec
	mov	lr,pc
	bx	r12

	ldmfd	sp!,{r0-r12}
	
	ldr	r3,=lzbuffer		;source = lzbuffer
	
mode_1	
	mov	r1,r4			;set rlelen
	
	mov	r5,#$6000000
	mov	r7,#$6000000	
	ldr	r8,=rlexordec_th+1	;decode
	mov	lr,pc
	bx	r8

;** speedbremse

	ldr	r4,=framecount
framewait		
	ldr	r2,[r4]
	cmp	r2,#4
	bne	framewait
	mov	r2,#0
	str	r2,[r4]


	subs	r11,r11,#1
	bne	floop	

	b	end

	.pool


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

anim	.include	"anim1.s"	

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


framebufferadr1	.long	$6000000
framebufferadr2	.long	$600A000

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
	
	ldr	r0,=framecount
	ldr	r1,[r0]
	add	r1,r1,#1
	str	r1,[r0]

	
	ldmfd	sp!,{lr}
	bx	lr
	

	
	.bss
		
myirqstack:	.zero	256			;irqstack
myirqstackend:	



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

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

		.align	4
lzbuffer	.zero	240*160
		.align	4
tbuffer		.zero   4116	


		
	.end
		