
	.text
	.align 4
	.thumb

;*********************************************************************************
;*
;* rledec_th (RLE Decoder)
;*
;* r1 = len
;* r2 = rle key 
;* r3 = source
;* r5 = dest
;*

	.global	rledec_th
	.type	rledec_th,function

rledec_th
	push	{r0-r7}

	add	r6,r1,r3	;max = rledata+len

rleloop_th
	ldrh	r4,[r3]
	add	r3,#2
	cmp	r4,r2		;key ?
	beq	key_th
key_cnt	strh	r4,[r5]		;store
	add	r5,#2
	cmp	r6,r3		;am ende?
	bne	rleloop_th
	
	pop	{r0-r7}
	bx	lr	
	
	
	;evtl dma support
	
key_th	
	ldrh	r0,[r3]		;load cnt (stored am ende aber 1 mal extra)
	ldrh	r4,[r3,#2]	;load value
	add	r3,#4
	cmp	r0,#0		;cnt=0 ist eins
	beq	key_cnt
	
	add	r0,r0,r0	;*2
	add	r1,r0,#2	;sub 2	
	sub	r5,#2
		
rlestr_th	
	strh	r4,[r5,r0]	;save store
	sub	r0,r0,#2
	bne	rlestr_th	
		
	add	r5,r1		;add rest
	b	key_cnt		
		
	
;*********************************************************************************
;*
;* rlexordec_th (RLE XOR Decoder)
;*
;* r1 = len
;* r2 = rle key 
;* r3 = source
;* r5 = dest
;* r7 = sourceold
;*

	.global	rlexordec_th	
	.type	rlexordec_th,function

rlexordec_th
	push	{r0-r7}
	mov	r0,r8
	push	{r0}

	add	r6,r1,r3	;max = rledata+len
	mov	r8,r6

rlexorloop_th
	ldrh	r4,[r3]
	add	r3,#2
	cmp	r4,r2		;key ?
	beq	keyxor_th
keyxor_cnt	

	ldrh	r0,[r7]		;load
	eor	r4,r4,r0
	strh	r4,[r5]		;store
	add	r5,#2
	add	r7,#2

keyxor_ade

	cmp	r8,r3		;am ende?
	bne	rlexorloop_th
	
	pop	{r0}
	mov	r8,r0
	pop	{r0-r7}
	bx	lr	
	
	
	;evtl dma support
	
keyxor_th	
	ldrh	r0,[r3]		;load cnt (stored am ende aber 1 mal extra)
	ldrh	r4,[r3,#2]	;load value
	add	r3,#4
	cmp	r0,#0		;cnt=0 ist eins
	beq	keyxor_cnt	;kopiere eins und weiter
	
	add	r0,r0,#1	;cnt=cnt+1
	
	cmp	r4,#0		;xor 0 ?
	bne	xor_needed
	
	cmp	r5,r7		;g=z ?
	beq	no_dma_copy
	
	cmp	r0,#20		
	bge	no_xor_cpy
	
xor_needed	


	add	r0,r0,r0	;*2
	add	r1,r0,#2	;sub 2
	sub	r7,#2		
	sub	r5,#2
		
rlexorstr_th	
	ldrh	r6,[r7,r0]		;load 
	eor	r6,r6,r4
	strh	r6,[r5,r0]		;store		
	sub	r0,r0,#2
	bne	rlexorstr_th
	
	add	r5,r1		;add rest
	add	r7,r1		;add rest
	
	b	keyxor_ade	
	
	;wenn xor = 0 dann kopiere einfach die daten
	
no_xor_cpy

	push	{r2,r3}	
	
	mov	r2,#$80			;16bit source 
	lsl	r2,r2,#24
	orr	r2,r2,r0		;+count
	add	r3,r0,r0
	
	mov	r0,r7			;source
	mov	r1,r5			;dest

	add	r5,r3			
	add	r7,r3

	ldr	r3,=$40000d4		;dma 0

	;r0 = source
	;r1 = dest
	;r2 = options+count
	
	stmia	r3!,{r0,r1,r2}		;start
	
	pop	{r2,r3}
	b	keyxor_ade			

	;bei gleicher quelle und ziel kopieren wir nichts

no_dma_copy

	add	r0,r0,r0
	add	r5,r0		
	add	r7,r0
	
	b	keyxor_ade	
	
	.pool
	.end
	