1 2; Optional definition for auto MALLOC init 3; it assumes we have free space between the end of 4; the compiled program and the stack pointer 5 6; It works only with a _heap pointer defined somewhere else in the crt0. 7; Such (long) pointer will hold, at startup, the (word) value of ASMTAIL 8; that points to che last used byte in the compiled program: 9 10;IF DEFINED_USING_amalloc 11;EXTERN ASMTAIL 12;PUBLIC _heap 13;._heap 14; defw ASMTAIL ; Location of the last program byte 15; defw 0 16;ENDIF 17 18 19; $Id: amalloc.def,v 1.4 2016-07-14 17:44:17 pauloscustodio Exp $ 20 21IF CRT_MAX_HEAP_ADDRESS 22 ld hl,CRT_MAX_HEAP_ADDRESS 23ELSE 24 ld hl,0 25 add hl,sp 26ENDIF 27 ; HL must hold SP or the end of free memory 28 push hl 29 30 ld hl,_heap 31 ld c,(hl) 32 inc hl 33 ld b,(hl) 34 inc bc 35 ; compact way to do "mallinit()" 36 xor a 37 ld (hl),a 38 dec hl 39 ld (hl),a 40 41 pop hl ; sp 42 sbc hl,bc ; hl = total free memory 43 ld d,h 44 ld e,l 45IF __CPU_INTEL__ 46 and a 47 ld a,d 48 rra 49 ld d,a 50 ld a,e 51 rra 52 ld e,a 53 and a 54 ld a,d 55 rra 56 ld d,a 57 ld a,e 58 rra 59 ld e,a 60ELSE 61 srl d 62 rr e 63 srl d 64 rr e 65ENDIF 66IF DEFINED_USING_amalloc_2 67 sbc hl,de ; leave 2/4 of the free memory for the stack 68IF DEFINED_USING_amalloc_1 69 sbc hl,de ; leave 3/4 of the free memory for the stack 70ENDIF 71ENDIF 72 sbc hl,de ; leave 1/4 of the free memory for the stack 73 push bc ; main address for malloc area 74 push hl ; area size 75 EXTERN sbrk_callee 76 call sbrk_callee 77