;/* depack2.s */

;****************************************************************************
;*
;*  Copyright (C) 2000-2001 Eli-Jean R. Leyssens, aka Pervect of Topix
;*
;*  This file is part of the CodePressor package.
;*
;*  CodePressor is free software; you can redistribute it and/or modify
;*  it under the terms of the GNU General Public License as published by
;*  the Free Software Foundation; either version 2 of the License, or
;*  (at your option) any later version.
;*
;*  CodePressor is distributed in the hope that it will be useful,
;*  but WITHOUT ANY WARRANTY; without even the implied warranty of
;*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;*  GNU General Public License for more details.
;*
;*  You should have received a copy of the GNU General Public License
;*  along with CodePressor; if not, write to the Free Software
;*  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
;*
;*  Eli-Jean R. Leyssens can be reached via email at eli@dnd.utwente.nl
;*  snail: E-J.R. Leyssens, Schivelbeinerstr. 5, 10439 Berlin, Germany
;*
;***************************************************************************/

		AREA 	|C$$code|, CODE, READONLY

r0	RN 0
r1	RN 1
r2	RN 2
r3	RN 3
r4	RN 4
r5	RN 5
r6	RN 6
r7	RN 7
r8	RN 8
r9	RN 9
r10	RN 10
r11	RN 11
r12	RN 12
r13	RN 13
link	RN 14
		EXPORT	|depack2_part0_begin|
		EXPORT	|depack2_part0_start|
		EXPORT	|depack2_part0_jump_program|
		EXPORT	|depack2_part0_end|

		EXPORT	|depack2_part0_begin|
		EXPORT	|depack2_part0_start|
		EXPORT	|depack2_part0_adrdata|
		EXPORT	|depack2_part0_main_loop|
		EXPORT	|depack2_part0_end|

		EXPORT	|depack2_part1_begin|
		EXPORT	|depack2_part1_compare|
		EXPORT	|depack2_part1_jump_program|
		EXPORT	|depack2_part1_end|

		EXPORT	|depack2_part2_begin|
		EXPORT	|depack2_part2_call_get_bits1|
		EXPORT	|depack2_part2_call_get_bits2|
		EXPORT	|depack2_part2_end|

		EXPORT	|depack2_part3_begin|
		EXPORT	|depack2_part3_jump_program|
		EXPORT	|depack2_part3_end|
		EXPORT	|depack2_part4_begin|
		EXPORT	|depack2_part4_jump_main_loop|
		EXPORT	|depack2_part4_get_bits|
		EXPORT	|depack2_part4_data|
		EXPORT	|depack2_part4_end|


|depack2_part0_begin|
; /////////////////////////////////////////////////////////////////////////
|depack2_part0_start|
;		adr	r4, |depack2_part0_begin|
|depack2_part0_adrdata|
		adr	r8, |depack2_part4_data|
		mov	r9, #1<<27
|depack2_part0_main_loop|
|depack2_part0_end|

|depack2_part1_begin|
|depack2_part1_compare|
		cmp	r4, #&8000
		bne     |depack2_part1_end|
;		mov	r0, #0
		swi	"XOS_SynchroniseCodeAreas"
|depack2_part1_jump_program|
		b	|depack2_part1_jump_program|
|depack2_part1_end|

|depack2_part2_begin|
		mov 	r5, #6-4
|depack2_part2_call_get_bits1|
		bl 	|depack2_part4_get_bits|
		movs 	r3, r3, lsr #1
		ldr 	r10, [ r4, r3, lsl #2]
		movcs 	r5, #4
|depack2_part2_call_get_bits2|
		bl 	|depack2_part4_get_bits|
		eor     r10, r10, r3 ; Undo unwanted EOR from get_bits
|depack2_part2_end|

|depack2_part3_begin|
		cmp	r3, #&fe
		bne     |depack2_part3_end|
;		mov	r0, #0
		swi	"XOS_SynchroniseCodeAreas"
|depack2_part3_jump_program|
		b	|depack2_part3_jump_program|
|depack2_part3_end|

|depack2_part4_begin|
		orr 	r0, r3, #1<<8
|depack2_decompress_nibbles_loop|
		movs 	r0, r0, lsr #1
		blhi	depack2_part4_get_bits
		cmp     r0, #0
		movne 	r10, r10, ror #4
		bne	depack2_decompress_nibbles_loop
		str	r10, [ r4, #-4]!
|depack2_part4_jump_main_loop|
		b 	|depack2_part0_main_loop|

|depack2_part4_get_bits|
		add	r5, r5, #4
		mov	r3, #0
|depack2_get_bits_loop|
		subs	r9, r9, #1<<27
		ldreq	r7, [ r8], #4
		movs	r7, r7, lsr #1
		adc	r3, r3, r3
		subs	r5, r5, #1
		bgt	depack2_get_bits_loop
		eor 	r10, r10, r3
		mov	pc, r14
|depack2_part4_data|
; /////////////////////////////////////////////////////////////////////////
|depack2_part4_end|

		END
