]>
Commit | Line | Data |
---|---|---|
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 | |
25 | hp_top: .byte 2 | |
26 | ||
27 | .cseg | |
28 | ||
29 | .if HEAP_DEBUG | |
30 | hp_print_free: | |
31 | push r15 | |
32 | push r14 | |
33 | push temp2 | |
34 | push temp | |
35 | printstring ", bytes free: " | |
36 | lds r14,hp_top | |
37 | lds r15,hp_top+1 | |
38 | ldi temp,0 | |
39 | ldi temp2,0 | |
40 | sub temp,r14 | |
41 | sbc temp2,r15 | |
42 | clr r14 | |
43 | clr r15 | |
44 | rcall print_ultoa | |
45 | printstring " " | |
46 | pop temp | |
47 | pop temp2 | |
48 | pop r14 | |
49 | pop r15 | |
50 | ret | |
51 | .endif | |
52 | ||
53 | ||
54 | ; Init heap | |
55 | ; temp:temp2 = first free memory location (heap start) | |
56 | ||
57 | heap_init: | |
58 | .if HEAP_DEBUG | |
59 | printnewline | |
60 | printstring "Heap init: Start: " | |
61 | rjmp hp_dbg1 | |
62 | .endif | |
63 | heap_release: | |
64 | .if HEAP_DEBUG | |
65 | printnewline | |
66 | printstring "Heap release: Start: " | |
67 | hp_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 | ||
82 | heap_get: | |
83 | push r15 | |
84 | push r14 | |
85 | .if HEAP_DEBUG | |
86 | push temp2 | |
87 | push temp | |
88 | printnewline | |
89 | printstring "Heap get: " | |
90 | clr r14 | |
91 | clr r15 | |
92 | rcall print_ultoa | |
93 | pop temp | |
94 | pop temp2 | |
95 | .endif | |
96 | lds r14,hp_top | |
97 | lds r15,hp_top+1 | |
98 | add temp,r14 | |
99 | adc temp2,r15 | |
100 | brcs hp_full | |
101 | ||
102 | ; zero flag clear here | |
103 | ||
104 | sts hp_top,temp | |
105 | sts hp_top+1,temp2 | |
106 | movw temp,r14 | |
107 | rjmp hp_get_ex | |
108 | hp_full: | |
109 | clr temp | |
110 | clr temp2 ;(sets zero flag) | |
111 | hp_get_ex: | |
112 | .if HEAP_DEBUG | |
113 | brne hp_get_dbg1 | |
114 | printstring "Error: " | |
115 | hp_get_dbg1: | |
116 | rcall hp_print_free | |
117 | mov r14,temp ;restore zero flag | |
118 | or r14,temp2 | |
119 | .endif | |
120 | pop r14 | |
121 | pop r15 | |
122 | ret | |
123 | ||
124 | ||
125 | ; vim:set ts=8 noet nowrap |