;--------------------------------------------------------
; File Created by SDCC : FreeWare ANSI-C Compiler
; Version 2.4.0 Tue Oct 12 01:18:29 2004

;--------------------------------------------------------
	.module malloc
	.optsdcc -mmcs51 --model-small
	
;--------------------------------------------------------
; Public variables in this module
;--------------------------------------------------------
	.globl _init_dynamic_memory_PARM_2
	.globl _init_dynamic_memory
	.globl _malloc
	.globl _free
;--------------------------------------------------------
; special function registers
;--------------------------------------------------------
;--------------------------------------------------------
; special function bits 
;--------------------------------------------------------
;--------------------------------------------------------
; overlayable register banks 
;--------------------------------------------------------
	.area REG_BANK_0	(REL,OVR,DATA)
	.ds 8
;--------------------------------------------------------
; internal ram data
;--------------------------------------------------------
	.area DSEG    (DATA)
_FIRST_MEMORY_HEADER_PTR:
	.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
_malloc_sloc0_1_0::
	.ds 2
	.area	OSEG    (OVR,DATA)
;--------------------------------------------------------
; indirectly addressable internal ram data
;--------------------------------------------------------
	.area ISEG    (DATA)
;--------------------------------------------------------
; bit data
;--------------------------------------------------------
	.area BSEG    (BIT)
;--------------------------------------------------------
; external ram data
;--------------------------------------------------------
	.area XSEG    (XDATA)
;--------------------------------------------------------
; external initialized ram data
;--------------------------------------------------------
	.area XISEG   (XDATA)
;--------------------------------------------------------
; global & static initialisations
;--------------------------------------------------------
	.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'
;array                     Allocated to registers r2 r3 
;------------------------------------------------------------
;malloc.c:145: void init_dynamic_memory(MEMHEADER xdata * array, 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: if ( !array ) /*Reserved memory starts on 0x0000 but it's NULL...*/
;     genIfx
	mov	a,r2
	orl	a,r3
;     genIfxJump
;       Peephole 109    removed ljmp by inverse jump logic
	jnz     00102$
00106$:
;malloc.c:169: array = (MEMHEADER xdata * )((char xdata * ) array + 1) ;
;     genPlus
;     genPlusIncr
	inc	r2
	cjne	r2,#0x00,00107$
	inc	r3
00107$:
;malloc.c:170: size --;
;     genMinus
;     genMinusDec
	dec	_init_dynamic_memory_PARM_2
	mov	a,#0xff
	cjne	a,_init_dynamic_memory_PARM_2,00108$
	dec	(_init_dynamic_memory_PARM_2 + 1)
00108$:
00102$:
;malloc.c:172: FIRST_MEMORY_HEADER_PTR = array;
;     genAssign
	mov	_FIRST_MEMORY_HEADER_PTR,r2
	mov	(_FIRST_MEMORY_HEADER_PTR + 1),r3
;malloc.c:174: array->next = (MEMHEADER xdata * )(((char xdata * ) array) + size - HEADER_SIZE);
;     genPlus
	mov	a,_init_dynamic_memory_PARM_2
;       Peephole 236.a  used r2 instead of ar2
	add     a,r2
	mov	r4,a
	mov	a,(_init_dynamic_memory_PARM_2 + 1)
;       Peephole 236.b  used r3 instead of ar3
	addc    a,r3
	mov	r5,a
;     genMinus
	mov	a,r4
	add	a,#0xfa
	mov	r4,a
	mov	a,r5
	addc	a,#0xff
	mov	r5,a
;     genPointerSet
;     genFarPointerSet
	mov	dpl,r2
	mov	dph,r3
	mov	a,r4
	movx	@dptr,a
	inc	dptr
	mov	a,r5
	movx	@dptr,a
;malloc.c:175: array->next->next = (void xdata * ) NULL; //And mark it as last
;     genCast
	mov	dpl,r4
	mov	dph,r5
;     genPointerSet
;     genFarPointerSet
;       Peephole 101    removed redundant mov
;       Peephole 181    changed mov to clr
;       Peephole 181    changed mov to clr
;       Peephole 226    removed unnecessary clr
	clr     a
	movx    @dptr,a
	inc     dptr
	movx    @dptr,a
	inc     dptr
	movx	@dptr,a
;malloc.c:176: array->prev       = (void xdata * ) NULL; //and mark first as first
;     genPlus
;     genPlusIncr
	mov	a,#0x02
;       Peephole 236.a  used r2 instead of ar2
	add     a,r2
	mov	dpl,a
;       Peephole 181    changed mov to clr
	clr     a
;       Peephole 236.b  used r3 instead of ar3
	addc    a,r3
	mov	dph,a
;     genPointerSet
;     genFarPointerSet
;       Peephole 101    removed redundant mov
;       Peephole 181    changed mov to clr
;       Peephole 181    changed mov to clr
;       Peephole 226    removed unnecessary clr
	clr     a
	movx    @dptr,a
	inc     dptr
	movx    @dptr,a
	inc     dptr
	movx	@dptr,a
;malloc.c:177: array->len        = 0;    //Empty and ready.
;     genPlus
;     genPlusIncr
	mov	a,#0x04
;       Peephole 236.a  used r2 instead of ar2
	add     a,r2
	mov	dpl,a
;       Peephole 181    changed mov to clr
	clr     a
;       Peephole 236.b  used r3 instead of ar3
	addc    a,r3
	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 r2 r3 
;sloc0                     Allocated with name '_malloc_sloc0_1_0'
;------------------------------------------------------------
;malloc.c:180: 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:185: if (size>(0xFFFF-HEADER_SIZE)) return (void xdata *) NULL; //To prevent overflow in next line
;     genCmpGt
;     genCmp
	clr	c
	mov	a,#0xF9
	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$
00122$:
;     genRet
;       Peephole 182.b  used 16 bit load of dptr
	mov     dptr,#0x0000
	mov	b,#0x00
;       Peephole 251    replaced ljmp to ret with ret
	ret
00102$:
;malloc.c:186: size += HEADER_SIZE; //We need a memory for header too
;     genPlus
	mov	a,#0x06
	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:187: current_header = FIRST_MEMORY_HEADER_PTR;
;     genAssign
	mov	r4,_FIRST_MEMORY_HEADER_PTR
	mov	r5,(_FIRST_MEMORY_HEADER_PTR + 1)
;malloc.c:188: while (1)
00108$:
;malloc.c:198: 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:199: ((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:200: current_header->len) >= size) break; //if spare is more than need
;     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
;     genPointerGet
;     genFarPointerGet
	mov	dpl,r2
	mov	dph,r3
	movx	a,@dptr
	mov	_malloc_sloc0_1_0,a
	inc	dptr
	movx	a,@dptr
	mov	(_malloc_sloc0_1_0 + 1),a
;     genMinus
	mov	a,r0
	clr	c
	subb	a,_malloc_sloc0_1_0
	mov	r0,a
	mov	a,r1
	subb	a,(_malloc_sloc0_1_0 + 1)
	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 108    removed ljmp by inverse jump logic
	jnc     00109$
00123$:
;malloc.c:201: current_header = current_header->next;    //else try next             
;     genAssign
	mov	ar4,r6
	mov	ar5,r7
;malloc.c:202: if (!current_header->next)  return (void xdata *) NULL;  //if end_of_list reached
;     genPointerGet
;     genFarPointerGet
	mov	dpl,r4
	mov	dph,r5
	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     00108$
00124$:
;     genRet
;       Peephole 182.b  used 16 bit load of dptr
	mov     dptr,#0x0000
	mov	b,#0x00
;       Peephole 251    replaced ljmp to ret with ret
	ret
00109$:
;malloc.c:204: if (!current_header->len)
;     genIfx
	mov	a,_malloc_sloc0_1_0
	orl	a,(_malloc_sloc0_1_0 + 1)
;     genIfxJump
;       Peephole 109    removed ljmp by inverse jump logic
	jnz     00111$
00125$:
;malloc.c:206: current_header->len = size; //for first allocation
;     genPointerSet
;     genFarPointerSet
	mov	dpl,r2
	mov	dph,r3
	mov	a,_malloc_size_1_1
	movx	@dptr,a
	inc	dptr
	mov	a,(_malloc_size_1_1 + 1)
	movx	@dptr,a
;malloc.c:207: return ((xdata *)&(current_header->mem));
;     genPlus
	mov	a,#0x06
;       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
;     genRet
;       Peephole 112.b  changed ljmp to sjmp
	sjmp    00114$
00111$:
;malloc.c:209: new_header = (MEMHEADER xdata * )((char xdata *)current_header + current_header->len);
;     genPlus
	mov	a,_malloc_sloc0_1_0
;       Peephole 236.a  used r4 instead of ar4
	add     a,r4
	mov	r2,a
	mov	a,(_malloc_sloc0_1_0 + 1)
;       Peephole 236.b  used r5 instead of ar5
	addc    a,r5
	mov	r3,a
;malloc.c:210: new_header->next = current_header->next; //and plug it into the chain
;     genPointerSet
;     genFarPointerSet
	mov	dpl,r2
	mov	dph,r3
	mov	a,r6
	movx	@dptr,a
	inc	dptr
	mov	a,r7
	movx	@dptr,a
;malloc.c:211: new_header->prev = current_header;
;     genPlus
;     genPlusIncr
	mov	a,#0x02
;       Peephole 236.a  used r2 instead of ar2
	add     a,r2
	mov	dpl,a
;       Peephole 181    changed mov to clr
	clr     a
;       Peephole 236.b  used r3 instead of ar3
	addc    a,r3
	mov	dph,a
;     genPointerSet
;     genFarPointerSet
	mov	a,r4
	movx	@dptr,a
	inc	dptr
	mov	a,r5
	movx	@dptr,a
;malloc.c:212: current_header->next  = new_header;
;     genPointerSet
;     genFarPointerSet
	mov	dpl,r4
	mov	dph,r5
	mov	a,r2
	movx	@dptr,a
	inc	dptr
	mov	a,r3
	movx	@dptr,a
;malloc.c:213: if (new_header->next)  new_header->next->prev = new_header;
;     genIfx
	mov	a,r6
	orl	a,r7
;     genIfxJump
;       Peephole 110    removed ljmp by inverse jump logic
	jz      00113$
00126$:
;     genPointerGet
;     genFarPointerGet
	mov	dpl,r2
	mov	dph,r3
	movx	a,@dptr
	mov	r4,a
	inc	dptr
	movx	a,@dptr
	mov	r5,a
;     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
;     genPointerSet
;     genFarPointerSet
	mov	a,r2
	movx	@dptr,a
	inc	dptr
	mov	a,r3
	movx	@dptr,a
00113$:
;malloc.c:214: new_header->len  = size; //mark as used
;     genPlus
;     genPlusIncr
	mov	a,#0x04
;       Peephole 236.a  used r2 instead of ar2
	add     a,r2
	mov	dpl,a
;       Peephole 181    changed mov to clr
	clr     a
;       Peephole 236.b  used r3 instead of ar3
	addc    a,r3
	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:215: return ((xdata *)&(new_header->mem));
;     genPlus
	mov	a,#0x06
;       Peephole 236.a  used r2 instead of ar2
	add     a,r2
	mov	dpl,a
;       Peephole 181    changed mov to clr
	clr     a
;       Peephole 236.b  used r3 instead of ar3
	addc    a,r3
	mov	dph,a
;     genRet
00114$:
	ret
;------------------------------------------------------------
;Allocation info for local variables in function 'free'
;------------------------------------------------------------
;p                         Allocated to registers r2 r3 
;prev_header               Allocated to registers r4 r5 
;------------------------------------------------------------
;malloc.c:218: void free (void xdata * p)
;	-----------------------------------------
;	 function free
;	-----------------------------------------
_free:
;     genReceive
	mov	r2,dpl
	mov	r3,dph
;malloc.c:221: if ( p ) //For allocated pointers only!
;     genIfx
	mov	a,r2
	orl	a,r3
;     genIfxJump
;       Peephole 110    removed ljmp by inverse jump logic
	jz      00108$
00113$:
;malloc.c:223: p = (MEMHEADER xdata * )((char xdata *)  p - HEADER_SIZE); //to start of header
;     genMinus
	mov	a,r2
	add	a,#0xfa
	mov	r2,a
	mov	a,r3
	addc	a,#0xff
	mov	r3,a
;malloc.c:224: if ( ((MEMHEADER xdata * ) p)->prev ) // For the regular header
;     genPlus
;     genPlusIncr
	mov	a,#0x02
;       Peephole 236.a  used r2 instead of ar2
	add     a,r2
	mov	dpl,a
;       Peephole 181    changed mov to clr
	clr     a
;       Peephole 236.b  used r3 instead of ar3
	addc    a,r3
	mov	dph,a
;     genPointerGet
;     genFarPointerGet
	movx	a,@dptr
	mov	r4,a
	inc	dptr
	movx	a,@dptr
;     genIfx
;       Peephole 135    removed redundant mov
	mov     r5,a
	orl     a,r4
;     genIfxJump
;       Peephole 110    removed ljmp by inverse jump logic
	jz      00104$
00114$:
;malloc.c:226: prev_header = ((MEMHEADER xdata * ) p)->prev;
;     genAssign
;malloc.c:227: prev_header->next = ((MEMHEADER xdata * ) p)->next;
;     genPointerGet
;     genFarPointerGet
	mov	dpl,r2
	mov	dph,r3
	movx	a,@dptr
	mov	r6,a
	inc	dptr
	movx	a,@dptr
	mov	r7,a
;     genPointerSet
;     genFarPointerSet
	mov	dpl,r4
	mov	dph,r5
	mov	a,r6
	movx	@dptr,a
	inc	dptr
	mov	a,r7
	movx	@dptr,a
;malloc.c:228: if (((MEMHEADER xdata * ) p)->next)
;     genIfx
	mov	a,r6
	orl	a,r7
;     genIfxJump
;       Peephole 110    removed ljmp by inverse jump logic
	jz      00108$
00115$:
;malloc.c:229: ((MEMHEADER xdata * ) p)->next->prev = prev_header;
;     genPlus
;     genPlusIncr
	mov	a,#0x02
;       Peephole 236.a  used r6 instead of ar6
	add     a,r6
	mov	dpl,a
;       Peephole 181    changed mov to clr
	clr     a
;       Peephole 236.b  used r7 instead of ar7
	addc    a,r7
	mov	dph,a
;     genPointerSet
;     genFarPointerSet
	mov	a,r4
	movx	@dptr,a
	inc	dptr
	mov	a,r5
	movx	@dptr,a
;       Peephole 112.b  changed ljmp to sjmp
	sjmp    00108$
00104$:
;malloc.c:231: else ((MEMHEADER xdata * ) p)->len = 0; //For the first header
;     genPlus
;     genPlusIncr
	mov	a,#0x04
;       Peephole 236.a  used r2 instead of ar2
	add     a,r2
	mov	dpl,a
;       Peephole 181    changed mov to clr
	clr     a
;       Peephole 236.b  used r3 instead of ar3
	addc    a,r3
	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
00108$:
	ret
	.area CSEG    (CODE)
	.area XINIT   (CODE)
