
	.section    	.iwram,"ax"
	
	.align		4
	
	.global		lzss_dec
	.type		lzss_dec,function
	
	
;*********************************************************************************
;*
;* lzss_dec (lzss Decoder)
;*
;* r0 = input
;* r1 = output 
;* r2 = inputmax (input+len)
;* r3 = tbuffer (muss davor mit ' ' initalisiert werden)
;*

lzss_dec	
	stmfd	sp!, {r4, r5, r6, r7, r8,r10,ip, lr}
	
	mov	r10,#$ff	; ! optimized by goldmomo (r10 ist 12 bit und)
	orr	r10,r10,#$f00	;r10 = fff
	
	mov	r7, #4064	@  r
	add	ip, r7, #14	@  r,  r
	
	mov	r4, r0	@  in
	mov	r8, r2	@  inmax
	mov	r5, r3	@  tbuffer
	mov	r7, #0	@  flags
	
	;for begin
	
.L19:
	mov	r7, r7, lsr #1	;  flags,  flags
	cmp	r4, r8		;  in,  inmax
	beq	.L1		;
	
	tst	r7, #256	;  flags
	
	;if 1
		
	ldreqb	r0, [r4], #1	 ; zero_extendqisi2	@  c
	orreq	r7, r0, #$ff00	 ;  flags,  c
	
	;if 2

	ands	lr, r7, #1	;  k,  flags
	beq	.L7		;  goto else
	
	ldrb	r0, [r4], #1	; zero_extendqisi2	@  c
	add	r3, ip, #1	;  r,  r
	strb	r0, [r1], #1	;  c
	strb	r0, [r5, ip]	;  c, * r
	and	ip,r3,r10	; !! optimized by goldmomo
	b	.L19		; forend
.L1:
	ldmfd	sp!, {r4, r5, r6, r7, r8,r10,ip, lr}
	bx	lr
	
	; else			!! optimized by goldmomo
.L7:
	ldrb	r0, [r4], #1	; zero_extendqisi2	@  i
	cmp	r4, r8		;  in,  inmax
	bge	.L1		; !! optimized by goldmomo
	ldrb	r2, [r4], #1	; zero_extendqisi2
	
	and	r3, r2, #15
	add	r6, r3, #2	;  j
	cmp	lr, r6		;  k,  j ; SETZT FLAGS
	and	r3, r2, #240
	orr	r0, r0, r3, asl #4	;  i,  i
	bgt	.L19
	
	; !! optimized by goldmomo
.L16:
	add	r3,r0,lr	;  i,  k
	and	r3,r3,r10	; !! optimized by goldmomo
	ldrb	r2, [r5, r3]	; zero_extendqisi2	@  c
	add	lr, lr, #1	;  k,  k
	add	r3, ip, #1	;  r,  r
	strb	r2, [r1], #1	;  c
	cmp	lr, r6		;  k,  j   ; SETZT FLAGS
	strb	r2, [r5, ip]	;  c, * r
	and	ip,r3,r10	; !! optimized by goldmomo
	ble	.L16
	b	.L19
	
	.end