]> cloudbase.mooo.com Git - avrcpm.git/blame - avr/heap.asm
* cpm/Makefile
[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:
31 push temp4
32 push temp3
33 push temp2
34 push temp
35 printstring ", bytes free: "
36 lds temp3,hp_top
37 lds temp4,hp_top+1
38 ldi temp,0
39 ldi temp2,0
40 sub temp,temp3
41 sbc temp2,temp4
42 ldi temp3,0
43 ldi temp4,0
44 rcall print_ultoa
45 printstring " "
46 pop temp
47 pop temp2
48 pop temp3
49 pop temp4
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:
83 push temp4
84 push temp3
85.if HEAP_DEBUG
86 push temp2
87 push temp
88 printnewline
89 printstring "Heap get: "
90 ldi temp3,0
91 ldi temp4,0
92 rcall print_ultoa
93 pop temp
94 pop temp2
95.endif
96 lds temp3,hp_top
97 lds temp4,hp_top+1
98 add temp,temp3
99 adc temp2,temp4
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,temp3
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
117 mov temp3,temp ;restore zero flag
118 or temp3,temp2
119.endif
120 pop temp3
121 pop temp4
122 ret
123
124
125; vim:set ts=8 noet nowrap
126