Skip to main content

coloX Memory API

coloX provides direct virtual machine memory access in bytes, words (2 bytes), and double words (4 bytes).


Contents​

  1. Read functions
  2. Write functions
  3. Block operations
  4. Memory address constants
  5. Complete API index

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:

ParameterDefaultDescription
dest0Destination memory address
source0Source data offset in the file
len0x4300Length to load
filenameCurrent applicationSpecify 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:

ConstantDescriptionAccess
__vm_GFXSprite image dataRead/write
__vm_MAPMap dataRead/write
__vm_FLAGSSprite flag dataRead/write
__vm_MUSICMusic dataRead/write
__vm_SFXSound effect dataRead/write
__vm_USERDATAUser data areaRead/write
__vm_CARTDATAApplication storage data mappingRead/write
__vm_VRAMVideo memory (canvas)Read/write
__vm_DRAWSTATDrawing stateRead/write
__vm_HWSTATHardware stateLogically 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​

FunctionReturnsDescription
peek(addr [, n])byte(s)Read bytes
peek2(addr)numberRead a short (16-bit, signed)
peek4(addr)numberRead 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