Heap handling
Mem_GetStackAddress
u16 Mem_GetStackAddress()

Get the current address of the stack top (lower address).

Return

Current stack bottom address

Mem_GetHeapAddress
inline u16 Mem_GetHeapAddress()

Get the current address of the heap top (higher addresse).

Return

Current heap top address

Mem_GetHeapSize
inline u16 Mem_GetHeapSize()

Get the amount of free space in the heap.

Return

Get the current size of the heap (between heap top address and stack bottom one)

Mem_HeapAlloc
void* Mem_HeapAlloc(
u16 size
)

Alloc a part of the heap.

Parameters
size
u16

The size of data to allocate

Return

Pointer to allocated heap chunk

Mem_HeapFree
void Mem_HeapFree(
u16 size
)

Free the last allocated area of the heap.

Parameters
size
u16

The size of data to allocate

Memory content modification
Mem_Copy
void Mem_Copy(
const voidsrc,
voiddest,
u16 size
)

Copy a memory block from a source address to an other (minimal size of 1 byte).

Parameters
src
const void*

Source address

dst

Destination address

size
u16

The size of data to copy. Note: A size of 0 mean 65536

Mem_Copy_16b
inline void Mem_Copy_16b(
const voidsrc,
voiddest,
u16 size
)

Copy a 16-bits memory block from a source address to an other (minimal size of 1 word / 2 byte).

Parameters
src
const void*

Source address

dst

Destination address

size
u16

The size of 16-bits data to copy. Note: size can be 1 to 32767. 0 mean 32768

Mem_FastCopy
void Mem_FastCopy(
const voidsrc,
voiddest,
u16 size
)

Fast copy a memory block from a source address to a destination.  Use 16 unrolled-LDI loop.

Parameters
src
const void*

Source address

dst

Destination address

size
u16

The size of data to copy. Note: A size of 0 mean 65536

Mem_FastCopy_16b
inline void Mem_FastCopy_16b(
const voidsrc,
voiddest,
u16 size
)

Fast copy a 16-bits memory block from a source address to a destination.  Use 16 unrolled-LDI loop.

Parameters
src
const void*

Source address

dst

Destination address

size
u16

The size of data to copy. Note: size can be 1 to 32767. 0 mean 32768

Mem_Set
void Mem_Set(
u8 val,
voiddest,
u16 size
)

Fill a memory block with a given 8-bits value (minimal size of 2 bytes).

Parameters
val
u8

Value to write

dst

Destination address

size
u16

The size of data to fill. Note: A size of 0 mean 65536

Mem_Set_16b
void Mem_Set_16b(
u16 val,
voiddest,
u16 size
)

Fill a memory block with a given 16-bits value (minimal size of 2 bytes).

Parameters
val
u16

Value to write

dst

Destination address

size
u16

The size of data to fill. Note: size can be 1 to 32767. 0 mean 32768

Mem_FastSet
void Mem_FastSet(
u8 val,
voiddest,
u16 size
)

Fast fill a memory chunk with a given value (minimal size of 2 bytes).  Use 16 unrolled-LDI loop.

Parameters
val
u8

Value to write

dst

Destination address

size
u16

The size of data to fill. Note: A size of 0 mean 65536

Dynamic allocator
Mem_DynamicInitialize
void Mem_DynamicInitialize(
voidbase,
u16 size
)

Allocates a static memory chunk which can then be used to allocate chunks dynimically.  Memory chunk can be in any pages but: - The memory space have to be continuous from a Z80 perspective.  - You have to ensure that all memory segments are selected in their proper place when allocating or releasing memory.

Parameters
base
void*

Base address of the dynamic allocator memory buffer

size
u16

Size of the dynamic allocator buffer (4 bytes chunk headers will consume some of this space)

Mem_DynamicInitializeHeap
inline void Mem_DynamicInitializeHeap(
u16 size
)

Allocates a static memory chunk in the heap which can then be used to allocate chunks dynimically.

Parameters
size
u16

Size of the dynamic allocator buffer (4 bytes chunk headers will consume some of this space)

Mem_DynamicAlloc
void* Mem_DynamicAlloc(
u16 size
)

Allocate a memory chunk from the dynamic memory buffer

Parameters
size
u16

Size of the memory chunk to allocate. Note: Even if you can allocate 0 sized chunk, this is useless

Return

Address of the allocated memory chunk (or NULL if no enough continuous space have been found)

Mem_DynamicFree
void Mem_DynamicFree(
voidptr
)

Free a memory chunk from the dynamic memory buffer

Parameters
ptr
void*

Address of the allocated memory chunk

Mem_GetDynamicSize
inline u16 Mem_GetDynamicSize(
voidptr
)

Get the size of a dynamically allocated memory chunk

Parameters
ptr
void*

Address of the allocated memory chunk

Return

Size of the allocated memory chunk