]> cloudbase.mooo.com Git - avrcpm.git/blame - avr/heap.asm
* cpm/BIOS.MAC
[avrcpm.git] / avr / heap.asm
CommitLineData
a17b9bbb
L
1; Simple memory management module.
2;
3; Copyright (C) 2010 Leo C.
4;
5; This file is part of avrcpm.
6;
7; avrcpm is free software: you can redistribute it and/or modify it
8; under the terms of the GNU General Public License as published by
9; the Free Software Foundation, either version 3 of the License, or
10; (at your option) any later version.
11;
12; avrcpm is distributed in the hope that it will be useful,
13; but WITHOUT ANY WARRANTY; without even the implied warranty of
14; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15; GNU General Public License for more details.
16;
17; You should have received a copy of the GNU General Public License
18; along with avrcpm. If not, see <http://www.gnu.org/licenses/>.
19;
20; $Id$
21;
22
23
24 .dseg
25hp_top: .byte 2
26
27 .cseg
28
29.if HEAP_DEBUG
30hp_print_free:
825ecc9d
L
31 push r15
32 push r14
a17b9bbb
L
33 push temp2
34 push temp
35 printstring ", bytes free: "
825ecc9d
L
36 lds r14,hp_top
37 lds r15,hp_top+1
a17b9bbb
L
38 ldi temp,0
39 ldi temp2,0
825ecc9d
L
40 sub temp,r14
41 sbc temp2,r15
42 clr r14
43 clr r15
a17b9bbb
L
44 rcall print_ultoa
45 printstring " "
46 pop temp
47 pop temp2
825ecc9d
L
48 pop r14
49 pop r15
a17b9bbb
L
50 ret
51.endif
52
53
54; Init heap
55; temp:temp2 = first free memory location (heap start)
56
57heap_init:
58.if HEAP_DEBUG
59 printnewline
60 printstring "Heap init: Start: "
61 rjmp hp_dbg1
62.endif
63heap_release:
64.if HEAP_DEBUG
65 printnewline
66 printstring "Heap release: Start: "
67hp_dbg1:
68.endif
69 sts hp_top,temp
70 sts hp_top+1,temp2
71.if HEAP_DEBUG
72 rcall printhexw
73 rcall hp_print_free
74.endif
75 ret
76
77; Get memory block from heap.
78; temp2:temp = size of block
79; return temp2:temp = pointer to allocated block
80; return 0 if not enough space
81
82heap_get:
825ecc9d
L
83 push r15
84 push r14
a17b9bbb
L
85.if HEAP_DEBUG
86 push temp2
87 push temp
88 printnewline
89 printstring "Heap get: "
825ecc9d
L
90 clr r14
91 clr r15
a17b9bbb
L
92 rcall print_ultoa
93 pop temp
94 pop temp2
95.endif
825ecc9d
L
96 lds r14,hp_top
97 lds r15,hp_top+1
98 add temp,r14
99 adc temp2,r15
a17b9bbb
L
100 brcs hp_full
101
102; zero flag clear here
103
104 sts hp_top,temp
105 sts hp_top+1,temp2
825ecc9d 106 movw temp,r14
a17b9bbb
L
107 rjmp hp_get_ex
108hp_full:
109 clr temp
110 clr temp2 ;(sets zero flag)
111hp_get_ex:
112.if HEAP_DEBUG
113 brne hp_get_dbg1
114 printstring "Error: "
115hp_get_dbg1:
116 rcall hp_print_free
825ecc9d
L
117 mov r14,temp ;restore zero flag
118 or r14,temp2
a17b9bbb 119.endif
825ecc9d
L
120 pop r14
121 pop r15
a17b9bbb
L
122 ret
123
124
125; vim:set ts=8 noet nowrap
126