Skip to content

Commit ddda9cd

Browse files
committed
Implement memoryAvailable
1 parent c1a00b3 commit ddda9cd

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

src/00/jumptable.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
calloc
1414
memset
1515
memcheck
16+
memoryAvailable
1617
memSeekToStart
1718
memSeekToEnd
1819
reassignMemory

src/00/memory.asm

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,61 @@ _: pop af
216216
or 1
217217
ret
218218

219+
;; memoryAvailable [System]
220+
;; Finds the amount of memory available for use.
221+
;; Outputs:
222+
;; BC: Total memory availble
223+
;; DE: Largest allocatable sum
224+
memoryAvailable:
225+
#define total_mem kernelGarbage
226+
#define max_alloc kernelGarbage + 2
227+
push hl
228+
push af
229+
ld a, i
230+
push af
231+
di
232+
ld hl, 0
233+
ld (total_mem), hl
234+
ld (max_alloc), hl
235+
ld hl, userMemory
236+
.loop:
237+
ld a, (hl) \ inc hl ; Owner ID to A
238+
ld c, (hl) \ inc hl
239+
ld b, (hl) \ inc hl ; Size to BC
240+
add hl, bc
241+
inc hl \ inc hl ; Move to next block
242+
cp 0xFF
243+
jr nz, .loop
244+
; Free block
245+
push hl
246+
ld hl, (total_mem)
247+
add hl, bc
248+
ld (total_mem), hl
249+
250+
ld hl, (max_alloc)
251+
or a ; reset c
252+
sbc hl, bc
253+
jr nc, _
254+
ld h, b \ ld l, c
255+
ld (max_alloc), hl
256+
_: pop hl
257+
xor a
258+
cp h
259+
jr nz, .loop
260+
pop af
261+
jp po, _
262+
ei
263+
_: pop af
264+
265+
ld hl, (total_mem)
266+
ld b, h \ ld c, l
267+
ld hl, (max_alloc)
268+
ex de, hl
269+
pop hl
270+
ret
271+
#undefine total_mem
272+
#undefine max_alloc
273+
219274
;; malloc [System]
220275
;; Allocates the specified amount of memory.
221276
;; Inputs:

0 commit comments

Comments
 (0)