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

;--------------------------------------------------------
	.module malloc
	.optsdcc -mgbz80
	
;--------------------------------------------------------
; Public variables in this module
;--------------------------------------------------------
	.globl __sdcc_heap_init
	.globl _malloc
	.globl _free
;--------------------------------------------------------
;  ram data
;--------------------------------------------------------
	.area _DATA
;--------------------------------------------------------
; overlayable items in  ram 
;--------------------------------------------------------
	.area _OVERLAY
;--------------------------------------------------------
; external initialized ram data
;--------------------------------------------------------
;--------------------------------------------------------
; global & static initialisations
;--------------------------------------------------------
	.area _GSINIT
	.area _GSFINAL
	.area _GSINIT
;--------------------------------------------------------
; Home
;--------------------------------------------------------
	.area _HOME
	.area _CODE
;--------------------------------------------------------
; code
;--------------------------------------------------------
	.area _CODE
;malloc.c:25: _sdcc_heap_init(void)
;	genLabel
;	genFunction
;	---------------------------------
; Function _sdcc_heap_init
; ---------------------------------
__sdcc_heap_init_start::
__sdcc_heap_init:
	
;malloc.c:27: MEMHEADER *pbase = &_sdcc_heap_start;
;malloc.c:28: unsigned int size = &_sdcc_heap_end - (char *)pbase;
;	genMinus
	ld	a,#<__sdcc_heap_end
	sub	a,#<__sdcc_heap_start
	ld	c,a
	ld	a,#>__sdcc_heap_end
	sbc	a,#>__sdcc_heap_start
	ld	b,a
;	genAssign
;	(registers are the same)
;malloc.c:30: pbase->next = (MEMHEADER *)((char *)pbase + size - HEADER_SIZE);
;	genPlus
;	Can't optimise plus by inc, falling back to the normal way
	ld	hl,#__sdcc_heap_start
	add	hl,bc
	ld	c,l
	ld	b,h
;	genMinus
	ld	a,c
	add	a,#0xFA
	ld	c,a
	ld	a,b
	adc	a,#0xFF
	ld	b,a
;	genAssign (pointer)
;	isBitvar = 0
	ld	de,#__sdcc_heap_start
	ld	a,c
	ld	(de),a
	inc	de
	ld	a,b
	ld	(de),a
;malloc.c:31: pbase->next->next = NULL; //And mark it as last
;	genCast
;	genAssign (pointer)
;	isBitvar = 0
	ld	e,c
	ld	d,b
	ld	a,#0x00
	ld	(de),a
	inc	de
	ld	a,#0x00
	ld	(de),a
;malloc.c:32: pbase->prev       = NULL; //and mark first as first
;	genPlus
;	genPlusIncr
	ld	bc,#__sdcc_heap_start + 2
;	genAssign (pointer)
;	isBitvar = 0
	ld	e,c
	ld	d,b
	ld	a,#0x00
	ld	(de),a
	inc	de
	ld	a,#0x00
	ld	(de),a
;malloc.c:33: pbase->len        = 0;    //Empty and ready.
;	genPlus
;	genPlusIncr
	ld	bc,#__sdcc_heap_start + 4
;	genAssign (pointer)
;	isBitvar = 0
	ld	e,c
	ld	d,b
	ld	a,#0x00
	ld	(de),a
	inc	de
	ld	a,#0x00
	ld	(de),a
;	genLabel
00101$:
;	genEndFunction
	
	ret
__sdcc_heap_init_end::
;malloc.c:37: malloc (unsigned int size)
;	genLabel
;	genFunction
;	---------------------------------
; Function malloc
; ---------------------------------
_malloc_start::
_malloc:
	lda	sp,-10(sp)
;malloc.c:42: if (size>(0xFFFF-HEADER_SIZE))
;	genCmpGt
;	AOP_STK for 
	ld	a,#0xF9
	lda	hl,12(sp)
	sub	a,(hl)
	ld	a,#0xFF
	inc	hl
	sbc	a,(hl)
	jp	nc,00102$
;malloc.c:44: return NULL; //To prevent overflow in next line
;	genRet
; Dump of IC_LEFT: type AOP_LIT size 2
	ld	de,#0x0000
	jp	00115$
;	genLabel
00102$:
;malloc.c:47: size += HEADER_SIZE; //We need a memory for header too
;	genPlus
;	AOP_STK for 
;	genPlusIncr
;	Can't optimise plus by inc, falling back to the normal way
	lda	hl,12(sp)
	ld	e,(hl)
	inc	hl
	ld	d,(hl)
	ld	hl,#0x0006
	add	hl,de
	ld	a,l
	ld	d,h
	lda	hl,12(sp)
	ld	(hl+),a
	ld	(hl),d
;malloc.c:48: current_header = &_sdcc_heap_start;
;	genAssign
;	AOP_STK for _malloc_current_header_1_1
	lda	hl,8(sp)
	ld	(hl),#<__sdcc_heap_start
	inc	hl
	ld	(hl),#>__sdcc_heap_start
;malloc.c:50: while (1)
;	genLabel
00108$:
;malloc.c:59: if ((((unsigned int)current_header->next) -
;	genPointerGet
;	AOP_STK for _malloc_current_header_1_1
;	AOP_STK for _malloc_sloc0_1_0
	lda	hl,8(sp)
	ld	e,(hl)
	inc	hl
	ld	d,(hl)
	ld	a,(de)
	lda	hl,4(sp)
	ld	(hl+),a
	inc	de
	ld	a,(de)
;	genCast
;	AOP_STK for _malloc_sloc0_1_0
;	AOP_STK for _malloc_sloc1_1_0
	ld      (hl-),a
	ld	a,(hl)
	dec	hl
	dec	hl
	ld	(hl),a
	lda	hl,5(sp)
	ld	a,(hl)
	dec	hl
	dec	hl
	ld	(hl),a
;malloc.c:60: ((unsigned int)current_header) -
;	genCast
;	AOP_STK for _malloc_current_header_1_1
	lda	hl,8(sp)
	ld	c,(hl)
	inc	hl
	ld	b,(hl)
;	genMinus
;	AOP_STK for _malloc_sloc1_1_0
	lda	hl,2(sp)
	ld	e,(hl)
	inc	hl
	ld	d,(hl)
	ld	a,e
	sub	a,c
	ld	e,a
	ld	a,d
	sbc	a,b
	ld	b,a
	ld	c,e
;malloc.c:61: current_header->len) >= size) 
;	genPlus
;	AOP_STK for _malloc_current_header_1_1
;	AOP_STK for _malloc_sloc1_1_0
;	genPlusIncr
;	Can't optimise plus by inc, falling back to the normal way
	lda	hl,8(sp)
	ld	e,(hl)
	inc	hl
	ld	d,(hl)
	ld	hl,#0x0004
	add	hl,de
	ld	a,l
	ld	d,h
	lda	hl,2(sp)
;	genPointerGet
;	AOP_STK for _malloc_sloc1_1_0
;	AOP_STK for _malloc_sloc2_1_0
	ld	(hl+),a
	ld	(hl),d
	ld	e,a
	ld	a,(de)
	lda	hl,0(sp)
	ld	(hl+),a
	inc	de
	ld	a,(de)
;	genMinus
;	AOP_STK for _malloc_sloc2_1_0
	ld      (hl-),a
	ld	e,(hl)
	inc	hl
	ld	d,(hl)
	ld	a,c
	sub	a,e
	ld	e,a
	ld	a,b
	sbc	a,d
	ld	b,a
	ld	c,e
;	genCmpLt
;	AOP_STK for 
	ld	a,c
	lda	hl,12(sp)
	sub	a,(hl)
	ld	a,b
	inc	hl
	sbc	a,(hl)
	jp	nc,00109$
;malloc.c:65: current_header = current_header->next;    //else try next             
;	genAssign
;	AOP_STK for _malloc_sloc0_1_0
	lda	hl,4(sp)
	ld	c,(hl)
	inc	hl
	ld	b,(hl)
;	genAssign
;	AOP_STK for _malloc_current_header_1_1
	lda	hl,8(sp)
	ld	(hl),c
	inc	hl
	ld	(hl),b
;malloc.c:66: if (!current_header->next)  
;	genPointerGet
;	AOP_STK for _malloc_current_header_1_1
	dec	hl
	ld	e,(hl)
	inc	hl
	ld	d,(hl)
	ld	a,(de)
	ld	c,a
	inc	de
	ld	a,(de)
	ld	b,a
;	genIfx
	ld	a,c
	or	a,b
	jp	nz,00108$
;malloc.c:68: return NULL;  //if end_of_list reached    
;	genRet
; Dump of IC_LEFT: type AOP_LIT size 2
	ld	de,#0x0000
	jp	00115$
;	genLabel
00109$:
;malloc.c:72: if (!current_header->len)
;	genIfx
;	AOP_STK for _malloc_sloc2_1_0
	lda	hl,0(sp)
	ld	a,(hl+)
	or	a,(hl)
	jp	nz,00113$
;malloc.c:74: current_header->len = size; //for first allocation
;	genAssign (pointer)
;	AOP_STK for _malloc_sloc1_1_0
;	AOP_STK for 
;	isBitvar = 0
	inc	hl
	ld	e,(hl)
	inc	hl
	ld	d,(hl)
	lda	hl,12(sp)
	ld	a,(hl)
	ld	(de),a
	inc	de
	inc	hl
	ld	a,(hl)
	ld	(de),a
;malloc.c:75: return &current_header->mem;
;	genPlus
;	AOP_STK for _malloc_current_header_1_1
;	genPlusIncr
;	Can't optimise plus by inc, falling back to the normal way
	lda	hl,8(sp)
	ld	e,(hl)
	inc	hl
	ld	d,(hl)
	ld	hl,#0x0006
	add	hl,de
	ld	e,l
	ld	d,h
;	genRet
; Dump of IC_LEFT: type AOP_STR size 2
	jp	00115$
;	genLabel
00113$:
;malloc.c:80: new_header = (MEMHEADER * )((char *)current_header + current_header->len);
;	genPlus
;	AOP_STK for _malloc_current_header_1_1
;	AOP_STK for _malloc_sloc2_1_0
;	Can't optimise plus by inc, falling back to the normal way
	lda	hl,8(sp)
	ld	e,(hl)
	inc	hl
	ld	d,(hl)
	lda	hl,0(sp)
	ld	a,(hl+)
	ld	h,(hl)
	ld	l,a
	add	hl,de
	ld	c,l
	ld	b,h
;	genAssign
;	AOP_STK for _malloc_new_header_1_1
	lda	hl,6(sp)
	ld	(hl),c
	inc	hl
	ld	(hl),b
;malloc.c:81: new_header->next = current_header->next; //and plug it into the chain
;	genAssign (pointer)
;	AOP_STK for _malloc_new_header_1_1
;	AOP_STK for _malloc_sloc0_1_0
;	isBitvar = 0
	dec	hl
	ld	e,(hl)
	inc	hl
	ld	d,(hl)
	lda	hl,4(sp)
	ld	a,(hl)
	ld	(de),a
	inc	de
	inc	hl
	ld	a,(hl)
	ld	(de),a
;malloc.c:82: new_header->prev = current_header;
;	genPlus
;	AOP_STK for _malloc_new_header_1_1
;	genPlusIncr
	inc	hl
	ld	c,(hl)
	inc	hl
	ld	b,(hl)
	inc	bc
	inc	bc
;	genAssign (pointer)
;	AOP_STK for _malloc_current_header_1_1
;	isBitvar = 0
	ld	e,c
	ld	d,b
	inc	hl
	ld	a,(hl)
	ld	(de),a
	inc	de
	inc	hl
	ld	a,(hl)
	ld	(de),a
;malloc.c:83: current_header->next  = new_header;
;	genAssign (pointer)
;	AOP_STK for _malloc_current_header_1_1
;	AOP_STK for _malloc_new_header_1_1
;	isBitvar = 0
	dec	hl
	ld	e,(hl)
	inc	hl
	ld	d,(hl)
	lda	hl,6(sp)
	ld	a,(hl)
	ld	(de),a
	inc	de
	inc	hl
	ld	a,(hl)
	ld	(de),a
;malloc.c:84: if (new_header->next)
;	genIfx
;	AOP_STK for _malloc_sloc0_1_0
	lda	hl,4(sp)
	ld	a,(hl+)
	or	a,(hl)
	jp	z,00111$
;malloc.c:86: new_header->next->prev = new_header;
;	genPointerGet
;	AOP_STK for _malloc_new_header_1_1
	inc	hl
	ld	e,(hl)
	inc	hl
	ld	d,(hl)
	ld	a,(de)
	ld	c,a
	inc	de
	ld	a,(de)
	ld	b,a
;	genPlus
;	genPlusIncr
	inc	bc
	inc	bc
;	genAssign (pointer)
;	AOP_STK for _malloc_new_header_1_1
;	isBitvar = 0
	ld	e,c
	ld	d,b
	dec	hl
	ld	a,(hl)
	ld	(de),a
	inc	de
	inc	hl
	ld	a,(hl)
	ld	(de),a
;	genLabel
00111$:
;malloc.c:88: new_header->len  = size; //mark as used
;	genPlus
;	AOP_STK for _malloc_new_header_1_1
;	genPlusIncr
;	Can't optimise plus by inc, falling back to the normal way
	lda	hl,6(sp)
	ld	e,(hl)
	inc	hl
	ld	d,(hl)
	ld	hl,#0x0004
	add	hl,de
	ld	c,l
	ld	b,h
;	genAssign (pointer)
;	AOP_STK for 
;	isBitvar = 0
	ld	e,c
	ld	d,b
	lda	hl,12(sp)
	ld	a,(hl)
	ld	(de),a
	inc	de
	inc	hl
	ld	a,(hl)
	ld	(de),a
;malloc.c:89: return &new_header->mem;
;	genPlus
;	AOP_STK for _malloc_new_header_1_1
;	genPlusIncr
;	Can't optimise plus by inc, falling back to the normal way
	lda	hl,6(sp)
	ld	e,(hl)
	inc	hl
	ld	d,(hl)
	ld	hl,#0x0006
	add	hl,de
	ld	e,l
	ld	d,h
;	genRet
; Dump of IC_LEFT: type AOP_STR size 2
;	genLabel
00115$:
;	genEndFunction
	lda	sp,10(sp)
	ret
_malloc_end::
;malloc.c:94: free (void *p)
;	genLabel
;	genFunction
;	---------------------------------
; Function free
; ---------------------------------
_free_start::
_free:
	lda	sp,-4(sp)
;malloc.c:98: if ( p ) //For allocated pointers only!
;	genIfx
;	AOP_STK for 
	lda	hl,6(sp)
	ld	a,(hl+)
	or	a,(hl)
	jp	z,00108$
;malloc.c:100: pthis = (MEMHEADER * )((char *)  p - HEADER_SIZE); //to start of header
;	genAssign
;	AOP_STK for 
	dec	hl
	ld	c,(hl)
	inc	hl
	ld	b,(hl)
;	genMinus
;	AOP_STK for _free_pthis_1_1
	ld	de,#0x0006
	ld	a,c
	sub	a,e
	ld	e,a
	ld	a,b
	sbc	a,d
	lda	hl,1(sp)
	ld      (hl-),a
	ld	(hl),e
;malloc.c:101: if ( pthis->prev ) // For the regular header
;	genPlus
;	AOP_STK for _free_pthis_1_1
;	genPlusIncr
	ld	c,(hl)
	inc	hl
	ld	b,(hl)
	inc	bc
	inc	bc
;	genPointerGet
	ld	e,c
	ld	d,b
	ld	a,(de)
	ld	c,a
	inc	de
	ld	a,(de)
	ld	b,a
;	genIfx
	ld	a,c
	or	a,b
	jp	z,00104$
;malloc.c:103: prev_header = pthis->prev;
;	genAssign
;	(registers are the same)
;	genAssign
;	AOP_STK for _free_prev_header_1_1
	inc	hl
	ld	(hl),c
	inc	hl
	ld	(hl),b
;malloc.c:104: prev_header->next = pthis->next;
;	genPointerGet
;	AOP_STK for _free_pthis_1_1
	lda	hl,0(sp)
	ld	e,(hl)
	inc	hl
	ld	d,(hl)
	ld	a,(de)
	ld	c,a
	inc	de
	ld	a,(de)
	ld	b,a
;	genAssign (pointer)
;	AOP_STK for _free_prev_header_1_1
;	isBitvar = 0
	inc	hl
	ld	e,(hl)
	inc	hl
	ld	d,(hl)
	ld	a,c
	ld	(de),a
	inc	de
	ld	a,b
	ld	(de),a
;malloc.c:105: if (pthis->next)
;	genIfx
	ld	a,c
	or	a,b
	jp	z,00108$
;malloc.c:107: pthis->next->prev = prev_header;
;	genPlus
;	genPlusIncr
	inc	bc
	inc	bc
;	genAssign (pointer)
;	AOP_STK for _free_prev_header_1_1
;	isBitvar = 0
	ld	e,c
	ld	d,b
	dec	hl
	ld	a,(hl)
	ld	(de),a
	inc	de
	inc	hl
	ld	a,(hl)
	ld	(de),a
;	genGoto
	jp	00108$
;	genLabel
00104$:
;malloc.c:112: pthis->len = 0; //For the first header
;	genPlus
;	AOP_STK for _free_pthis_1_1
;	genPlusIncr
;	Can't optimise plus by inc, falling back to the normal way
	lda	hl,0(sp)
	ld	e,(hl)
	inc	hl
	ld	d,(hl)
	ld	hl,#0x0004
	add	hl,de
	ld	c,l
	ld	b,h
;	genAssign (pointer)
;	isBitvar = 0
	ld	e,c
	ld	d,b
	ld	a,#0x00
	ld	(de),a
	inc	de
	ld	a,#0x00
	ld	(de),a
;	genLabel
00108$:
;	genEndFunction
	lda	sp,4(sp)
	ret
_free_end::
	.area _CODE
