Skip to main content

tic80

MapRemapCallback

Type: Type Definition.

Description:

  Remap callback function type for map() function.

Signature:

local type MapRemapCallback = function(tile: number, x: number, y: number): number, number | nil, number | nil

Parameters:

ParameterTypeDescription
tilenumberThe tile ID at position (x, y)
xnumberThe X coordinate of the tile
ynumberThe Y coordinate of the tile

Returns:

Return TypeDescription
number, number | nil, number | nilA tuple containing [tile, flip, rotate]

btn

Type: Function.

Description:

  Checks if a button is currently pressed.

Signature:

global btn: function(id?: number): boolean

Parameters:

ParameterTypeDescription
idnumber[optional] Button ID (0-31). If omitted, checks if any button is pressed.
Button IDs for all players:
Player 1: Up 0, Down 1, Left 2, Right 3, A 4, B 5, X 6, Y 7
Player 2: Up 8, Down 9, Left 10, Right 11, A 12, B 13, X 14, Y 15
Player 3: Up 16, Down 17, Left 18, Right 19, A 20, B 21, X 22, Y 23
Player 4: Up 24, Down 25, Left 26, Right 27, A 28, B 29, X 30, Y 31

Returns:

Return TypeDescription
booleanTrue if the button is pressed, false otherwise.

btnp

Type: Function.

Description:

  Checks if a button was just pressed (on this frame).

Signature:

global btnp: function(id?: number, hold?: number, period?: number): boolean

Parameters:

ParameterTypeDescription
idnumber[optional] Button ID (0-31). If omitted, checks all buttons.
holdnumber[optional] Hold time in frames.
periodnumber[optional] Repeat period in frames.

Returns:

Return TypeDescription
booleanTrue if the button was just pressed, false otherwise.

key

Type: Function.

Description:

  Checks if a keyboard key is currently pressed.

Signature:

global key: function(code?: number): boolean

Parameters:

ParameterTypeDescription
codenumber[optional] Key code. If omitted, checks all keys.
Letters A-Z: 0-25
Numbers 0-9: 27-36
Special keys: 37-65
Minus = 37
Equals = 38
LeftBracket = 39
RightBracket = 40
Backslash = 41
Semicolon = 42
Apostrophe = 43
Grave = 44
Comma = 45
Period = 46
Slash = 47
Space = 48
Tab = 49
Return = 50
Backspace = 51
Delete = 52
Insert = 53
PageUp = 54
PageDown = 55
Home = 56
End = 57
Up = 58
Down = 59
Left = 60
Right = 61
CapsLock = 62
Ctrl = 63
Shift = 64
Alt = 65

Returns:

Return TypeDescription
booleanTrue if the key is pressed, false otherwise.

keyp

Type: Function.

Description:

  Checks if a keyboard key was just pressed (on this frame).

Signature:

global keyp: function(code?: number, hold?: number, period?: number): boolean

Parameters:

ParameterTypeDescription
codenumber[optional] Key code. If omitted, checks all keys.
holdnumber[optional] Hold time in frames.
periodnumber[optional] Repeat period in frames.

Returns:

Return TypeDescription
booleanTrue if the key was just pressed, false otherwise.

mouse

Type: Function.

Description:

  Gets the current mouse state.

Signature:

global mouse: function(): number, number, number, number, number, number, number

Returns:

Return TypeDescription
number, number, number, number, number, number, numberMultiple return values containing [x, y, left, middle, right, scrollx, scrolly]

clip

Type: Function.

Description:

  Sets the drawing clip region.

Signature:

global clip: function(x: number, y: number, w: number, h: number)

Parameters:

ParameterTypeDescription
xnumberThe X coordinate of the clip region.
ynumberThe Y coordinate of the clip region.
wnumberThe width of the clip region.
hnumberThe height of the clip region.

cls

Type: Function.

Description:

  Clears the screen with a color.

Signature:

global cls: function(color?: number)

Parameters:

ParameterTypeDescription
colornumber[optional] Color index (default: 0).

circ

Type: Function.

Description:

  Draws a filled circle.

Signature:

global circ: function(x: number, y: number, radius: number, color: number)

Parameters:

ParameterTypeDescription
xnumberThe X coordinate of the center.
ynumberThe Y coordinate of the center.
radiusnumberThe radius of the circle.
colornumberThe color index.

circb

Type: Function.

Description:

  Draws a circle outline.

Signature:

global circb: function(x: number, y: number, radius: number, color: number)

Parameters:

ParameterTypeDescription
xnumberThe X coordinate of the center.
ynumberThe Y coordinate of the center.
radiusnumberThe radius of the circle.
colornumberThe color index.

elli

Type: Function.

Description:

  Draws a filled ellipse.

Signature:

global elli: function(x: number, y: number, a: number, b: number, color: number)

Parameters:

ParameterTypeDescription
xnumberThe X coordinate of the center.
ynumberThe Y coordinate of the center.
anumberThe horizontal radius.
bnumberThe vertical radius.
colornumberThe color index.

ellib

Type: Function.

Description:

  Draws an ellipse outline.

Signature:

global ellib: function(x: number, y: number, a: number, b: number, color: number)

Parameters:

ParameterTypeDescription
xnumberThe X coordinate of the center.
ynumberThe Y coordinate of the center.
anumberThe horizontal radius.
bnumberThe vertical radius.
colornumberThe color index.

line

Type: Function.

Description:

  Draws a line.

Signature:

global line: function(x0: number, y0: number, x1: number, y1: number, color: number)

Parameters:

ParameterTypeDescription
x0numberThe X coordinate of the start point.
y0numberThe Y coordinate of the start point.
x1numberThe X coordinate of the end point.
y1numberThe Y coordinate of the end point.
colornumberThe color index.

pix

Type: Function.

Description:

  Gets or sets a pixel color.

Signature:

global pix: function(x: number, y: number, color?: number): number

Parameters:

ParameterTypeDescription
xnumberThe X coordinate.
ynumberThe Y coordinate.
colornumber?Optional color index. If provided, sets the pixel color.

Returns:

Return TypeDescription
numberThe color index at the specified position.

print

Type: Function.

Description:

  Prints text on the screen.

Signature:

global print: function(text: string, x?: number, y?: number, color?: number, fixed?: boolean, scale?: number, smallfont?: boolean): number

Parameters:

ParameterTypeDescription
textstringThe text to print.
xnumber[optional] X coordinate (default: 0).
ynumber[optional] Y coordinate (default: 0).
colornumber[optional] Color index (default: 12).
fixedboolean[optional] Fixed-width font flag (default: false).
scalenumber[optional] Scale factor (default: 1).
smallfontboolean[optional] Small font flag (default: false).

Returns:

Return TypeDescription
numberThe width of the printed text.

rect

Type: Function.

Description:

  Draws a filled rectangle.

Signature:

global rect: function(x: number, y: number, w: number, h: number, color: number)

Parameters:

ParameterTypeDescription
xnumberThe X coordinate of the top-left corner.
ynumberThe Y coordinate of the top-left corner.
wnumberThe width of the rectangle.
hnumberThe height of the rectangle.
colornumberThe color index.

rectb

Type: Function.

Description:

  Draws a rectangle outline.

Signature:

global rectb: function(x: number, y: number, w: number, h: number, color: number)

Parameters:

ParameterTypeDescription
xnumberThe X coordinate of the top-left corner.
ynumberThe Y coordinate of the top-left corner.
wnumberThe width of the rectangle.
hnumberThe height of the rectangle.
colornumberThe color index.

spr

Type: Function.

Description:

  Draws a sprite.

Signature:

global spr: function(id: number, x: number, y: number, transparent?: number, scale?: number, flip?: number, rotate?: number, w?: number, h?: number)

Parameters:

ParameterTypeDescription
idnumberThe sprite ID.
xnumberThe X coordinate.
ynumberThe Y coordinate.
transparentnumber[optional] Transparent color index (default: -1, no transparency).
scalenumber[optional] Scale factor (default: 1).
flipnumber[optional] Flip flag: 1=horizontal, 2=vertical, 3=both (default: 0).
rotatenumber[optional] Rotation: 1=90°, 2=180°, 3=270° clockwise (default: 0).
wnumber[optional] Sprite width in tiles (default: 1).
hnumber[optional] Sprite height in tiles (default: 1).

tri

Type: Function.

Description:

  Draws a filled triangle.

Signature:

global tri: function(x1: number, y1: number, x2: number, y2: number, x3: number, y3: number, color: number)

Parameters:

ParameterTypeDescription
x1numberThe X coordinate of the first vertex.
y1numberThe Y coordinate of the first vertex.
x2numberThe X coordinate of the second vertex.
y2numberThe Y coordinate of the second vertex.
x3numberThe X coordinate of the third vertex.
y3numberThe Y coordinate of the third vertex.
colornumberThe color index.

trib

Type: Function.

Description:

  Draws a triangle outline.

Signature:

global trib: function(x1: number, y1: number, x2: number, y2: number, x3: number, y3: number, color: number)

Parameters:

ParameterTypeDescription
x1numberThe X coordinate of the first vertex.
y1numberThe Y coordinate of the first vertex.
x2numberThe X coordinate of the second vertex.
y2numberThe Y coordinate of the second vertex.
x3numberThe X coordinate of the third vertex.
y3numberThe Y coordinate of the third vertex.
colornumberThe color index.

ttri

Type: Function.

Description:

  Draws a textured triangle.

Signature:

global ttri: function(x1: number, y1: number, x2: number, y2: number, x3: number, y3: number, u1: number, v1: number, u2: number, v2: number, u3: number, v3: number, texsrc?: number, chromakey?: number, z1?: number, z2?: number, z3?: number)

Parameters:

ParameterTypeDescription
x1numberThe X coordinate of the first vertex.
y1numberThe Y coordinate of the first vertex.
x2numberThe X coordinate of the second vertex.
y2numberThe Y coordinate of the second vertex.
x3numberThe X coordinate of the third vertex.
y3numberThe Y coordinate of the third vertex.
u1numberThe U texture coordinate of the first vertex.
v1numberThe V texture coordinate of the first vertex.
u2numberThe U texture coordinate of the second vertex.
v2numberThe V texture coordinate of the second vertex.
u3numberThe U texture coordinate of the third vertex.
v3numberThe V texture coordinate of the third vertex.
texsrcnumber[optional] Texture source (default: 0).
chromakeynumber[optional] Chroma key color index (default: -1).
z1number[optional] Z coordinate of the first vertex (default: 0).
z2number[optional] Z coordinate of the second vertex (default: 0).
z3number[optional] Z coordinate of the third vertex (default: 0).

font

Type: Function.

Description:

  Draws text using a custom font.

Signature:

global font: function(text: string, x: number, y: number, transparent?: number, charWidth?: number, charHeight?: number, fixed?: boolean, scale?: number): number

Parameters:

ParameterTypeDescription
textstringThe text to draw.
xnumberThe X coordinate.
ynumberThe Y coordinate.
transparentnumber[optional] Transparent color index.
charWidthnumber[optional] Character width.
charHeightnumber[optional] Character height.
fixedboolean[optional] Fixed-width font flag (default: false).
scalenumber[optional] Scale factor (default: 1).

Returns:

Return TypeDescription
numberThe width of the drawn text.

map

Type: Function.

Description:

  Draws a map tile layer.

Signature:

global map: function(x?: number, y?: number, w?: number, h?: number, sx?: number, sy?: number, colorkey?: number, scale?: number, remap?: MapRemapCallback)

Parameters:

ParameterTypeDescription
xnumber[optional] Map X coordinate (default: 0).
ynumber[optional] Map Y coordinate (default: 0).
wnumber[optional] Width in tiles (default: 30).
hnumber[optional] Height in tiles (default: 17).
sxnumber[optional] Sprite X offset (default: 0).
synumber[optional] Sprite Y offset (default: 0).
colorkeynumber[optional] Color key for transparency (default: -1).
scalenumber[optional] Scale factor (default: 1).
remapMapRemapCallback[optional] Remap callback function.

mget

Type: Function.

Description:

  Gets the tile ID at a map position.

Signature:

global mget: function(x: number, y: number): number

Parameters:

ParameterTypeDescription
xnumberThe X coordinate in tiles.
ynumberThe Y coordinate in tiles.

Returns:

Return TypeDescription
numberThe tile ID.

mset

Type: Function.

Description:

  Sets the tile ID at a map position.

Signature:

global mset: function(x: number, y: number, id: number)

Parameters:

ParameterTypeDescription
xnumberThe X coordinate in tiles.
ynumberThe Y coordinate in tiles.
idnumberThe tile ID to set.

fget

Type: Function.

Description:

  Gets a sprite flag.

Signature:

global fget: function(sprite_id: number, flag: number): boolean

Parameters:

ParameterTypeDescription
sprite_idnumberThe sprite ID.
flagnumberThe flag index (0-7).

Returns:

Return TypeDescription
booleanTrue if the flag is set, false otherwise.

fset

Type: Function.

Description:

  Sets a sprite flag.

Signature:

global fset: function(sprite_id: number, flag: number, bool: boolean)

Parameters:

ParameterTypeDescription
sprite_idnumberThe sprite ID.
flagnumberThe flag index (0-7).
boolbooleanThe flag value to set.

sfx

Type: Function.

Description:

  Plays a sound effect.

Signature:

global sfx: function(id: number, note?: number, duration?: number, channel?: number, volume?: number, speed?: number)

Parameters:

ParameterTypeDescription
idnumberThe sound effect ID.
notenumber[optional] Note number (0-95, default: uses SFX note).
durationnumber[optional] Duration in frames (default: -1, full duration).
channelnumber[optional] Channel number (0-3, default: 0).
volumenumber[optional] Volume (0-15, default: 15).
speednumber[optional] Speed (0-15, default: 0).

music

Type: Function.

Description:

  Plays music.

Signature:

global music: function(track?: number, frame?: number, row?: number, loop?: boolean)

Parameters:

ParameterTypeDescription
tracknumber[optional] Track number (default: -1, stops music).
framenumber[optional] Frame number (default: -1).
rownumber[optional] Row number (default: -1).
loopboolean[optional] Loop flag (default: true).

peek

Type: Function.

Description:

  Reads a value from memory.

Signature:

global peek: function(addr: number, bits?: number): number

Parameters:

ParameterTypeDescription
addrnumberThe memory address.
bitsnumber[optional] Bit width: 8, 16, or 32 (default: 8).

Returns:

Return TypeDescription
numberThe value read from memory.

peek1

Type: Function.

Description:

  Reads a single bit from memory.

Signature:

global peek1: function(bitaddr: number): number

Parameters:

ParameterTypeDescription
bitaddrnumberThe bit address.

Returns:

Return TypeDescription
numberThe bit value (0 or 1).

peek2

Type: Function.

Description:

  Reads a 2-byte value from memory.

Signature:

global peek2: function(addr2: number): number

Parameters:

ParameterTypeDescription
addr2numberThe memory address (must be 2-byte aligned).

Returns:

Return TypeDescription
numberThe 16-bit value read from memory.

peek4

Type: Function.

Description:

  Reads a 4-byte value from memory.

Signature:

global peek4: function(addr4: number): number

Parameters:

ParameterTypeDescription
addr4numberThe memory address (must be 4-byte aligned).

Returns:

Return TypeDescription
numberThe 32-bit value read from memory.

poke

Type: Function.

Description:

  Writes a value to memory.

Signature:

global poke: function(addr: number, val: number)

Parameters:

ParameterTypeDescription
addrnumberThe memory address.
valnumberThe value to write.

poke1

Type: Function.

Description:

  Writes a single bit to memory.

Signature:

global poke1: function(bitaddr: number, bitval: number)

Parameters:

ParameterTypeDescription
bitaddrnumberThe bit address.
bitvalnumberThe bit value (0 or 1).

poke2

Type: Function.

Description:

  Writes a 2-byte value to memory.

Signature:

global poke2: function(addr2: number, val2: number)

Parameters:

ParameterTypeDescription
addr2numberThe memory address (must be 2-byte aligned).
val2numberThe 16-bit value to write.

poke4

Type: Function.

Description:

  Writes a 4-byte value to memory.

Signature:

global poke4: function(addr4: number, val4: number)

Parameters:

ParameterTypeDescription
addr4numberThe memory address (must be 4-byte aligned).
val4numberThe 32-bit value to write.

pmem

Type: Function.

Description:

  Gets or sets a persistent memory value.

Signature:

global pmem: function(index: number, val?: number): number

Parameters:

ParameterTypeDescription
indexnumberThe persistent memory index (0-255).
valnumber[optional] Value to set. If omitted, returns the current value.

Returns:

Return TypeDescription
numberThe persistent memory value.

memcpy

Type: Function.

Description:

  Copies memory from one location to another.

Signature:

global memcpy: function(toaddr: number, fromaddr: number, len: number)

Parameters:

ParameterTypeDescription
toaddrnumberThe destination address.
fromaddrnumberThe source address.
lennumberThe number of bytes to copy.

memset

Type: Function.

Description:

  Sets a block of memory to a value.

Signature:

global memset: function(addr: number, val: number, len: number)

Parameters:

ParameterTypeDescription
addrnumberThe starting address.
valnumberThe value to set.
lennumberThe number of bytes to set.

BDR

Type: Function.

Description:

  The BDR (Between Display Rows) callback allows you to execute code between the rendering of each scan line. The primary reason to do this is to manipulate the palette. Doing so makes it possible to use a different palette for each scan line, and therefore more than 16 colors at a time.

Signature:

global BDR: function(scanline: number)

Parameters:

ParameterTypeDescription
scanlinenumberThe scan line about to be drawn (0-143).
0-3: TOP BORDER
4-139: ROW 0-135 (equiv SCN(0-135))
140-143: BOTTOM BORDER

TIC

Type: Function.

Description:

  The TIC function is the main update/draw callback and must be present in every program. It takes no parameters and is called sixty times per second (60fps). Put your update and draw code here.

Signature:

global TIC: function()

BOOT

Type: Function.

Description:

  The BOOT function is called a single time when your cartridge is booted. It should be used for startup/initialization code. For scripting languages that allow code in the global scope (Lua, etc.) using BOOT is preferred rather than including source code in the global scope.

Signature:

global BOOT: function()

exit

Type: Function.

Description:

  Exits the game.

Signature:

global exit: function()

reset

Type: Function.

Description:

  Resets the game.

Signature:

global reset: function()

sync

Type: Function.

Description:

  Synchronizes cart data.

Signature:

global sync: function(mask?: number, bank?: number, tocart?: boolean)

Parameters:

ParameterTypeDescription
masknumber[optional] Sync mask (default: 0, syncs all).
Tiles = 1 << 0
Sprites = 1 << 1
Map = 1 << 2
Sfx = 1 << 3
Music = 1 << 4
Palette = 1 << 5
Flags = 1 << 6
Screen = 1 << 7
banknumber[optional] Bank number (default: 0).
tocartboolean[optional] Flag to save to cart (default: false).

time

Type: Function.

Description:

  Gets the current time in milliseconds.

Signature:

global time: function(): number

Returns:

Return TypeDescription
numberThe current time in milliseconds.

tstamp

Type: Function.

Description:

  Gets a timestamp.

Signature:

global tstamp: function(): number

Returns:

Return TypeDescription
numberA timestamp value.

trace

Type: Function.

Description:

  Outputs a trace message.

Signature:

global trace: function(msg: string, color?: number)

Parameters:

ParameterTypeDescription
msgstringThe message to output.
colornumber[optional] Color index.

vbank

Type: Function.

Description:

  Switches the video bank.

Signature:

global vbank: function(bank: number)

Parameters:

ParameterTypeDescription
banknumberThe bank number (0 or 1).