coloX Memory API
coloX provides direct virtual machine memory access in bytes, words (2 bytes), and double words (4 bytes).
Contents
1. Read Functions
peek(addr [, n]) → value [, ...]
Reads 1 or n bytes. Returns a single value or multiple values.
local a = peek(0x1000) -- Read 1 byte
local a, b = peek(0x1000, 2) -- Read 2 bytes
peek2(addr) → number
Reads a 16-bit short (2 bytes, little-endian).
peek4(addr) → number
Reads a 32-bit integer (4 bytes, little-endian).
local tile = peek2(__vm_MAP) -- Read the first map element
local pixel = peek(__vm_VRAM + 120) -- Read a screen pixel
2. Write Functions
poke(addr, val1, val2, ...)
Writes one or more bytes.
poke(0x1000, 0xff) -- Write 1 byte
poke(0x1000, 0xaa, 0xbb) -- Write 2 bytes
poke2(addr, val1, val2, ...)
Writes one or more 16-bit short values.
poke4(addr, val1, val2, ...)
Writes one or more 32-bit integers.
poke2(__vm_MAP, 42) -- Set the first map element to 42
poke4(0x2000, 0x12345678) -- Write a 32-bit value
3. Block Operations
memcpy(dest, source, len)
Copies len bytes from source to dest. The regions may overlap (overlapping regions are handled correctly with memmove semantics).
-- Copy sprite data into video memory
memcpy(__vm_VRAM, __vm_GFX, 1024)
memset(dest, val, len)
Fills len bytes starting at dest with val.
-- Clear video memory (byte length = width*height*bpp/8)
memset(__vm_VRAM, 0, __vm_width * __vm_height * __vm_bpp / 8)
-- Fill with a specific pattern
memset(__vm_VRAM, 0x88, 0x1000)
reload(dest, source, len [, filename])
Reloads data from the application file into memory.
Parameters:
| Parameter | Default | Description |
|---|---|---|
dest | 0 | Destination memory address |
source | 0 | Source data offset in the file |
len | 0x4300 | Length to load |
filename | Current application | Specify a filename to load data from another application |
-- Reload sprite data from the current application
reload(__vm_GFX, 0, 4096)
-- Load data from another application
reload(__vm_GFX, 0, 4096, "other_app")
4. Memory Address Constants
The following global constants identify regions in virtual machine memory:
| Constant | Description | Access |
|---|---|---|
__vm_GFX | Sprite image data | Read/write |
__vm_MAP | Map data | Read/write |
__vm_FLAGS | Sprite flag data | Read/write |
__vm_MUSIC | Music data | Read/write |
__vm_SFX | Sound effect data | Read/write |
__vm_USERDATA | User data area | Read/write |
__vm_CARTDATA | Application storage data mapping | Read/write |
__vm_VRAM | Video memory (canvas) | Read/write |
__vm_DRAWSTAT | Drawing state | Read/write |
__vm_HWSTAT | Hardware state | Logically system-maintained (poke does not block writes, but the system may overwrite them) |
Canvas constants
__vm_width/__vm_height/__vm_bpp/__vm_scr_width/__vm_scr_height(see platforms.md) can also be used to calculate region sizes in bytes for a given color depth.
5. Complete API Index
| Function | Returns | Description |
|---|---|---|
peek(addr [, n]) | byte(s) | Read bytes |
peek2(addr) | number | Read a short (16-bit, signed) |
peek4(addr) | number | Read an int (32-bit, signed) |
poke(addr, val, ...) | — | Write bytes |
poke2(addr, val, ...) | — | Write shorts (16-bit) |
poke4(addr, val, ...) | — | Write ints (32-bit) |
memcpy(dest, src, len) | — | Copy memory (regions may overlap) |
memset(dest, val, len) | — | Fill memory |
reload(dest, src, len [, file]) | — | Reload data from a file |