-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathalloc.mac
156 lines (134 loc) · 4.02 KB
/
alloc.mac
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
;**********************************************************************;
; ;
; This file is part of ZSM4, a Z80/Z180/Z280 relocatable macro- ;
; assembler written in Z80 assembly. ;
; Copyright (C) 2017-2020, Hector Peraza. ;
; ;
; This program is free software; you can redistribute it and/or ;
; modify it under the terms of the GNU General Public License as ;
; published by the Free Software Foundation; either version 2 of ;
; the License, or (at your option) any later version. ;
; ;
; This program is distributed in the hope that it will be useful, ;
; but WITHOUT ANY WARRANTY; without even the implied warranty of ;
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;
; GNU General Public License for more details. ;
; ;
; You should have received a copy of the GNU General Public License ;
; along with this program; if not, write to the Free Software ;
; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. ;
; ;
;**********************************************************************;
TITLE Z80/Z180/Z280 Macro-Assembler
SUBTTL Dynamic memory management routines
.Z80
include ZSM.INC
public MCHECK,MACCHK,MALLOC,MFREE,CMPHD,FNDREC,FNDNXT,GBCOL
extrn INCMEM,MAXMEM,DSPTR,SYMPTR,MACPTR,ERRFLG
cseg
; MCHECK - Check for available memory
; BC = size in bytes
; Returns CY flag set if not enough memory
MCHECK: push hl
ld hl,(SYMPTR)
MCHK1: add hl,bc
push de
ex de,hl
ld hl,(DSPTR)
call CMPHD
ccf
call c,INCMEM
pop de
pop hl
ret
; MACCHK - Check for available memory, this one is used when
; building a MACRO.
; BC = size in bytes
; Returns CY flag set if not enough memory
MACCHK: push hl
ld hl,(MACPTR)
jr MCHK1 ; continue via common code
; MALLOC - Allocate a block of memory
; BC = size in bytes, not including overhead
; E = record type
; Returns HL pointing to data area of allocated block
; CY flag set if not enough memory
MALLOC: inc bc
inc bc
inc bc ; overhead = 3 bytes (type + size)
call MCHECK
jr nc,M1 ; branch if OK
ld a,'W' ; else error
ld (ERRFLG),a
ret
M1: ld hl,(DSPTR) ; get updated top of available memory
or a
sbc hl,bc ; alloc block
ld (DSPTR),hl ; set new top of available memory
ld (hl),e ; store record type
inc hl
dec bc
dec bc
dec bc
ld (hl),c ; store size, not including overhead
inc hl
ld (hl),b
inc hl
ret
; MFREE - Release allocated memory
; HL = start of block to free
MFREE: push hl
ld de,(DSPTR)
or a
sbc hl,de ; get length of storage under current block
ld c,l ; BC = len
ld b,h
pop hl
push hl
inc hl
ld e,(hl) ; get record length
inc hl
ld d,(hl)
add hl,de ; point to last byte
ex de,hl ; DE = dst
pop hl
dec hl ; HL = src
ld a,b
or c
jr z,M2 ; branch if nothing to move
lddr ; move lower blocks to free space
M2: ex de,hl
inc hl
ld (DSPTR),hl ; set new top of memory
ret
; CMPHD - Compare HL:DE
; Returns Z flag set if HL == DE, CY flag set if HL > DE
CMPHD: ld a,d
cp h
ret nz
ld a,e
cp l
ret
; FNDREC - Find record of specified type in high memory storage
; C has record type
FNDREC: ld hl,(DSPTR)
FNDR1: ld a,(hl) ; get type
or a ; null?
scf
ret z ; return error - not found
cp c ; found?
ret z ; return success
FNDNXT: inc hl
ld e,(hl) ; else get length
inc hl
ld d,(hl)
inc hl
add hl,de ; point to next record
jr FNDR1
; GBCOL - Perform garbage collection, remove all deleted entries.
GBCOL: ld c,STDEL
call FNDREC
ret c
call MFREE
jr GBCOL
end