;--------------------------------------------------------
; File Created by SDCC : FreeWare ANSI-C Compiler
; Version 2.5.0 #1020 (Mar  4 2006)
; This file generated Sat Mar  4 14:58:51 2006
;--------------------------------------------------------
	.module malloc
	.optsdcc -mmcs51 --model-small
	
;--------------------------------------------------------
; Public variables in this module
;--------------------------------------------------------
	.globl _init_dynamic_memory_PARM_2
	.globl __sdcc_first_memheader
	.globl _init_dynamic_memory
	.globl _malloc
;--------------------------------------------------------
; special function registers
;--------------------------------------------------------
	.area RSEG    (DATA)
;--------------------------------------------------------
; special function bits 
;--------------------------------------------------------
	.area RSEG    (DATA)
;--------------------------------------------------------
; overlayable register banks 
;--------------------------------------------------------
	.area REG_BANK_0	(REL,OVR,DATA)
	.ds 8
;--------------------------------------------------------
; internal ram data
;--------------------------------------------------------
	.area DSEG    (DATA)
__sdcc_first_memheader::
	.ds 2
;--------------------------------------------------------
; overlayable items in internal ram 
;--------------------------------------------------------
	.area	OSEG    (OVR,DATA)
_init_dynamic_memory_PARM_2::
	.ds 2
	.area	OSEG    (OVR,DATA)
_malloc_size_1_1::
	.ds 2
;--------------------------------------------------------
; indirectly addressable internal ram data
;--------------------------------------------------------
	.area ISEG    (DATA)
;--------------------------------------------------------
; bit data
;--------------------------------------------------------
	.area BSEG    (BIT)
;--------------------------------------------------------
; paged external ram data
;--------------------------------------------------------
	.area PSEG    (PAG,XDATA)
;--------------------------------------------------------
; external ram data
;--------------------------------------------------------
	.area XSEG    (XDATA)
;--------------------------------------------------------
; external initialized ram data
;--------------------------------------------------------
	.area XISEG   (XDATA)
	.area CSEG    (CODE)
	.area GSINIT0 (CODE)
	.area GSINIT1 (CODE)
	.area GSINIT2 (CODE)
	.area GSINIT3 (CODE)
	.area GSINIT4 (CODE)
	.area GSINIT5 (CODE)
;--------------------------------------------------------
; global & static initialisations
;--------------------------------------------------------
	.area CSEG    (CODE)
	.area GSINIT  (CODE)
	.area GSFINAL (CODE)
	.area GSINIT  (CODE)
;--------------------------------------------------------
; Home
;--------------------------------------------------------
	.area HOME    (CODE)
	.area CSEG    (CODE)
;--------------------------------------------------------
; code
;--------------------------------------------------------
	.area CSEG    (CODE)
;------------------------------------------------------------
;Allocation info for local variables in function 'init_dynamic_memory'
;------------------------------------------------------------
;size                      Allocated with name '_init_dynamic_memory_PARM_2'
;heap                      Allocated to registers r2 r3 
;array                     Allocated to registers r4 r5 
;------------------------------------------------------------
;malloc.c:146: void init_dynamic_memory(void xdata * heap, unsigned int size)
;	-----------------------------------------
;	 function init_dynamic_memory
;	-----------------------------------------
_init_dynamic_memory:
	ar2 = 0x02
	ar3 = 0x03
	ar4 = 0x04
	ar5 = 0x05
	ar6 = 0x06
	ar7 = 0x07
	ar0 = 0x00
	ar1 = 0x01
;     genReceive
	mov	r2,dpl
	mov	r3,dph
;malloc.c:167: char xdata * array = (char xdata *)heap;
;     genAssign
	mov	ar4,r2
	mov	ar5,r3
;malloc.c:169: if ( !array ) //Reserved memory starts at 0x0000 but that's NULL...
;     genIfx
	mov	a,r2
	orl	a,r3
;     genIfxJump
;	Peephole 109	removed ljmp by inverse jump logic
	jnz	00102$
00106$:
;malloc.c:171: array++;
;     genPlus
;     genPlusIncr
	mov	a,#0x01
;	Peephole 236.a	used r2 instead of ar2
	add	a,r2
	mov	r4,a
;	Peephole 181	changed mov to clr
	clr	a
;	Peephole 236.b	used r3 instead of ar3
	addc	a,r3
	mov	r5,a
;malloc.c:172: size--;
;     genMinus
;     genMinusDec
	dec	_init_dynamic_memory_PARM_2
	mov	a,#0xff
	cjne	a,_init_dynamic_memory_PARM_2,00107$
	dec	(_init_dynamic_memory_PARM_2 + 1)
00107$:
00102$:
;malloc.c:174: _sdcc_first_memheader = (MEMHEADER xdata * ) array;
;     genAssign
	mov	__sdcc_first_memheader,r4
	mov	(__sdcc_first_memheader + 1),r5
;malloc.c:176: _sdcc_first_memheader->next = (MEMHEADER xdata * )(array + size - sizeof(MEMHEADER xdata *));
;     genAssign
	mov	dpl,__sdcc_first_memheader
	mov	dph,(__sdcc_first_memheader + 1)
;     genPlus
	mov	a,_init_dynamic_memory_PARM_2
;	Peephole 236.a	used r4 instead of ar4
	add	a,r4
	mov	r4,a
	mov	a,(_init_dynamic_memory_PARM_2 + 1)
;	Peephole 236.b	used r5 instead of ar5
	addc	a,r5
	mov	r5,a
;     genMinus
;     genMinusDec
	mov	a,r4
	add	a,#0xfe
	mov	r4,a
	mov	a,r5
	addc	a,#0xff
	mov	r5,a
;     genPointerSet
;     genFarPointerSet
	mov	a,r4
	movx	@dptr,a
	inc	dptr
	mov	a,r5
	movx	@dptr,a
;malloc.c:177: _sdcc_first_memheader->next->next = (MEMHEADER xdata * ) NULL; //And mark it as last
;     genAssign
	mov	dpl,__sdcc_first_memheader
	mov	dph,(__sdcc_first_memheader + 1)
;     genPointerGet
;     genFarPointerGet
	movx	a,@dptr
	mov	r2,a
	inc	dptr
	movx	a,@dptr
	mov	r3,a
;     genPointerSet
;     genFarPointerSet
	mov	dpl,r2
	mov	dph,r3
;	Peephole 101	removed redundant mov
;	Peephole 181	changed mov to clr
	clr	a
	movx	@dptr,a
	inc	dptr
	movx	@dptr,a
;malloc.c:178: _sdcc_first_memheader->len        = 0;    //Empty and ready.
;     genPlus
;     genPlusIncr
	mov	a,#0x02
	add	a,__sdcc_first_memheader
	mov	dpl,a
;	Peephole 181	changed mov to clr
	clr	a
	addc	a,(__sdcc_first_memheader + 1)
	mov	dph,a
;     genPointerSet
;     genFarPointerSet
;	Peephole 101	removed redundant mov
;	Peephole 181	changed mov to clr
	clr	a
	movx	@dptr,a
	inc	dptr
	movx	@dptr,a
00103$:
	ret
;------------------------------------------------------------
;Allocation info for local variables in function 'malloc'
;------------------------------------------------------------
;size                      Allocated with name '_malloc_size_1_1'
;current_header            Allocated to registers r4 r5 
;new_header                Allocated to registers r0 r1 
;ret                       Allocated to registers r2 r3 
;------------------------------------------------------------
;malloc.c:181: void xdata * malloc (unsigned int size)
;	-----------------------------------------
;	 function malloc
;	-----------------------------------------
_malloc:
;     genReceive
	mov	_malloc_size_1_1,dpl
	mov	(_malloc_size_1_1 + 1),dph
;malloc.c:187: if (size>(0xFFFF-HEADER_SIZE)) return (void xdata *) NULL; //To prevent overflow in next line
;     genCmpGt
;     genCmp
	clr	c
	mov	a,#0xFB
	subb	a,_malloc_size_1_1
	mov	a,#0xFF
	subb	a,(_malloc_size_1_1 + 1)
;     genIfxJump
;	Peephole 108	removed ljmp by inverse jump logic
	jnc	00102$
00123$:
;     genRet
;	Peephole 182.b	used 16 bit load of dptr
	mov	dptr,#0x0000
;	Peephole 251.a	replaced ljmp to ret with ret
	ret
00102$:
;malloc.c:188: size += HEADER_SIZE; //We need a memory for header too
;     genPlus
;     genPlusIncr
	mov	a,#0x04
	add	a,_malloc_size_1_1
	mov	_malloc_size_1_1,a
;	Peephole 181	changed mov to clr
	clr	a
	addc	a,(_malloc_size_1_1 + 1)
	mov	(_malloc_size_1_1 + 1),a
;malloc.c:189: current_header = _sdcc_first_memheader;
;     genAssign
	mov	r4,__sdcc_first_memheader
	mov	r5,(__sdcc_first_memheader + 1)
;malloc.c:192: while (1)
00108$:
;malloc.c:202: if ((((unsigned int)current_header->next) -
;     genPointerGet
;     genFarPointerGet
	mov	dpl,r4
	mov	dph,r5
	movx	a,@dptr
	mov	r6,a
	inc	dptr
	movx	a,@dptr
	mov	r7,a
;     genCast
	mov	ar0,r6
	mov	ar1,r7
;malloc.c:203: ((unsigned int)current_header) -
;     genCast
	mov	ar2,r4
	mov	ar3,r5
;     genMinus
	mov	a,r0
	clr	c
;	Peephole 236.l	used r2 instead of ar2
	subb	a,r2
	mov	r0,a
	mov	a,r1
;	Peephole 236.l	used r3 instead of ar3
	subb	a,r3
	mov	r1,a
;malloc.c:204: current_header->len) >= size)
;     genPlus
;     genPlusIncr
	mov	a,#0x02
;	Peephole 236.a	used r4 instead of ar4
	add	a,r4
	mov	dpl,a
;	Peephole 181	changed mov to clr
	clr	a
;	Peephole 236.b	used r5 instead of ar5
	addc	a,r5
	mov	dph,a
;     genPointerGet
;     genFarPointerGet
	movx	a,@dptr
	mov	r2,a
	inc	dptr
	movx	a,@dptr
	mov	r3,a
;     genMinus
	mov	a,r0
	clr	c
;	Peephole 236.l	used r2 instead of ar2
	subb	a,r2
	mov	r0,a
	mov	a,r1
;	Peephole 236.l	used r3 instead of ar3
	subb	a,r3
	mov	r1,a
;     genCmpLt
;     genCmp
	clr	c
	mov	a,r0
	subb	a,_malloc_size_1_1
	mov	a,r1
	subb	a,(_malloc_size_1_1 + 1)
;     genIfxJump
;	Peephole 112.b	changed ljmp to sjmp
;	Peephole 160	removed sjmp by inverse jump logic
	jc	00104$
00124$:
;malloc.c:206: ret = current_header->mem;
;     genPlus
;     genPlusIncr
	mov	a,#0x04
;	Peephole 236.a	used r4 instead of ar4
	add	a,r4
	mov	r2,a
;	Peephole 181	changed mov to clr
	clr	a
;	Peephole 236.b	used r5 instead of ar5
	addc	a,r5
	mov	r3,a
;malloc.c:207: break;
;	Peephole 112.b	changed ljmp to sjmp
	sjmp	00109$
00104$:
;malloc.c:209: current_header = current_header->next;    //else try next
;     genAssign
	mov	ar4,r6
	mov	ar5,r7
;malloc.c:210: if (!current_header->next)
;     genPointerGet
;     genFarPointerGet
	mov	dpl,r4
	mov	dph,r5
	movx	a,@dptr
	mov	r6,a
	inc	dptr
	movx	a,@dptr
;     genIfx
;	Peephole 135	removed redundant mov
	mov	r7,a
	orl	a,r6
;     genIfxJump
;	Peephole 109	removed ljmp by inverse jump logic
;malloc.c:212: ret = (void xdata *) NULL;
;     genAssign
;	Peephole 256.c	loading r2 with zero from a
;	Peephole 256.d	loading r3 with zero from a
	jnz	00108$
00125$:
	mov	r2,a
	mov	r3,a
;malloc.c:213: break;
00109$:
;malloc.c:216: if (ret)
;     genIfx
	mov	a,r2
	orl	a,r3
;     genIfxJump
;	Peephole 110	removed ljmp by inverse jump logic
	jz	00114$
00126$:
;malloc.c:218: if (!current_header->len)
;     genPlus
;     genPlusIncr
	mov	a,#0x02
;	Peephole 236.a	used r4 instead of ar4
	add	a,r4
	mov	r6,a
;	Peephole 181	changed mov to clr
	clr	a
;	Peephole 236.b	used r5 instead of ar5
	addc	a,r5
	mov	r7,a
;     genPointerGet
;     genFarPointerGet
	mov	dpl,r6
	mov	dph,r7
	movx	a,@dptr
	mov	r0,a
	inc	dptr
	movx	a,@dptr
;     genIfx
;	Peephole 135	removed redundant mov
	mov	r1,a
	orl	a,r0
;     genIfxJump
;	Peephole 109	removed ljmp by inverse jump logic
	jnz	00111$
00127$:
;malloc.c:220: current_header->len = size; //for first allocation
;     genPointerSet
;     genFarPointerSet
	mov	dpl,r6
	mov	dph,r7
	mov	a,_malloc_size_1_1
	movx	@dptr,a
	inc	dptr
	mov	a,(_malloc_size_1_1 + 1)
	movx	@dptr,a
;	Peephole 112.b	changed ljmp to sjmp
	sjmp	00114$
00111$:
;malloc.c:224: new_header = (MEMHEADER xdata * )((char xdata *)current_header + current_header->len);
;     genPlus
;	Peephole 236.g	used r0 instead of ar0
	mov	a,r0
;	Peephole 236.a	used r4 instead of ar4
	add	a,r4
	mov	r0,a
;	Peephole 236.g	used r1 instead of ar1
	mov	a,r1
;	Peephole 236.b	used r5 instead of ar5
	addc	a,r5
	mov	r1,a
;malloc.c:225: new_header->next = current_header->next; //and plug it into the chain
;     genPointerGet
;     genFarPointerGet
	mov	dpl,r4
	mov	dph,r5
	movx	a,@dptr
	mov	r6,a
	inc	dptr
	movx	a,@dptr
	mov	r7,a
;     genPointerSet
;     genFarPointerSet
	mov	dpl,r0
	mov	dph,r1
	mov	a,r6
	movx	@dptr,a
	inc	dptr
	mov	a,r7
	movx	@dptr,a
;malloc.c:226: current_header->next  = new_header;
;     genPointerSet
;     genFarPointerSet
	mov	dpl,r4
	mov	dph,r5
	mov	a,r0
	movx	@dptr,a
	inc	dptr
	mov	a,r1
	movx	@dptr,a
;malloc.c:227: new_header->len  = size; //mark as used
;     genPlus
;     genPlusIncr
	mov	a,#0x02
;	Peephole 236.a	used r0 instead of ar0
	add	a,r0
	mov	dpl,a
;	Peephole 181	changed mov to clr
	clr	a
;	Peephole 236.b	used r1 instead of ar1
	addc	a,r1
	mov	dph,a
;     genPointerSet
;     genFarPointerSet
	mov	a,_malloc_size_1_1
	movx	@dptr,a
	inc	dptr
	mov	a,(_malloc_size_1_1 + 1)
	movx	@dptr,a
;malloc.c:228: ret = new_header->mem;
;     genPlus
;     genPlusIncr
	mov	a,#0x04
;	Peephole 236.a	used r0 instead of ar0
	add	a,r0
	mov	r2,a
;	Peephole 181	changed mov to clr
	clr	a
;	Peephole 236.b	used r1 instead of ar1
	addc	a,r1
	mov	r3,a
00114$:
;malloc.c:232: return ret;
;     genRet
	mov	dpl,r2
	mov	dph,r3
00115$:
	ret
	.area CSEG    (CODE)
	.area XINIT   (CODE)
