This doc is pretty much a WIP and incomplete reference of all the GBVM commands.
Important notes:
- Not all of them are available in the latest version of GB Studio.
- https://github.com/chrismaltby/gbvm/tree/master/include/vm.i should still be considered the source of truth as this list might not be updated regularly.
Compares two variables or values on stack (IDXA
and IDXB
) using a CONDITION
. If the comparision is true the execution will jump to LABEL
, if false continue execution. Then, clear N
arguments from stack.
.EQ = 1 - Equal
.LT = 2 - Less Than
.LTE = 3 - Less Than or Equal
.GT = 4 - Greater Than
.GTE = 5 - Greather Than or Equal
.NE = 6 - Not Equal
.AND = 7 - Binary And
.OR = 8 - Binary Or
Compares a variable or value on stack (IDXA
) with a constant (B
) using a CONDITION
. If the comparision is true the execution will jump to LABEL
, if false continue execution. Then, clear N
arguments from stack.
.EQ = 1 - Equal
.LT = 2 - Less Than
.LTE = 3 - Less Than or Equal
.GT = 4 - Greater Than
.GTE = 5 - Greather Than or Equal
.NE = 6 - Not Equal
.AND = 7 - Binary And
.OR = 8 - Binary Or
Compares a variable or value on stack (IDX
) with a set of values, and if equal jump to the specified label. Then, clear N
arguments from stack.
SIZE
sets the number of values to compare, and each of the values are defined with:
.dw A, LABEL
Where A
is the value and LABEL
the label to jump to.
Jumps to near address.
Loops by near address, counter is on stack, counter is removed on exit.
Loops by relative (one-byte) offset, counter is on stack, counter is removed on exit.
Calls by near address.
Calls by relative (one-byte) offset.
Calls native code by far pointer.
Returns from relative or near call.
Returns from relative or near call and clear ARG0
arguments on stack.
Calls far (inter-bank call).
Returns from far call.
Retursn from far call and clear ARG0
arguments on stack.
Invokes BANK:ADDR
C function until it returns true.
Valid functions are:
_wait_frames
_camera_shake
Stops execution of context
Signals runner that context is in a waitable state
Locks VM
Unlocks VM
Spawns a thread in a separate context
Joins a thread
Kills a thread
Raises an exception CODE
.
Valid values for CODE
are:
EXCEPTION_RESET
: Resets the device. Always called withSIZE
equal to0
.EXCEPTION_CHANGE_SCENE
: Changes to a new scene. See Scene for more details.EXCEPTION_SAVE
: Saves the state of the game. See Save State for more details.EXCEPTION_LOAD
: Loads the saved state of the game. See Save State for more details.
Terminates unit-testing immediately
.TEST_WAIT_VBL = 1
Checks that VM state was restored and reset the restore flag
Pushes immediate value VAL
to the top of the VM stack.
Pushes a value on VM stack or a global onto VM stack
Pushes a value on VM stack or a global indirectly from an index in the variable on VM stack or in a global onto VM stack
Translates IDX
into absolute index and pushes result to VM stack
Removes the top N
values from VM stack.
Similar to pop
Assigns a value on VM stack or a global to a value on VM stack or a global
Assigns a constant value VAL
to a variable in the stack. A non-negative index IDX
points to a global variable. A negative index points to the variable in the position relative to the top of the stack.
Assigns a value on VM stack or a global indirectly to a value on VM stack ar a global
Assigns a value on VM stack or a global to a value on VM stack ar a global indirectly
Gets thread local variable. Non-negative index of IDXB
points to a thread local variable (parameters passed into thread).
Sets unsigned int8 in WRAM. ADDR
is an address of unsigned int8
Sets unsigned int8 in WRAM. ADDR
is an address of int8
Gets unsigned int8 from WRAM. ADDR
is an address of unsigned int8
Sets int8 in WRAM. ADDR
is an address of int8
Sets int8 in WRAM. ADDR
is an address of int8
Gets int8 from WRAM. ADDR
is an address of int8
Sets int16 in WRAM. ADDR
is an address of int16
Sets int16 in WRAM. ADDR
is an address of int16
Gets int16 from WRAM. ADDR
is an address of int16
To load a new scene raise a EXCEPTION_CHANGE_SCENE
exception using VM_RAISE
.
The scene to load is defined using IMPORT_FAR_PTR_DATA
followed by the scene symbol.
For example to load scene 10
:
VM_RAISE EXCEPTION_CHANGE_SCENE, 3
IMPORT_FAR_PTR_DATA _scene_10
Pushes the current scene to the scene stack.
Removes the last scene from the scene stack an loads it.
Removes all scenes from the scene stack and loads the first one.
Removes all the scenes from the scene stack.
Actor struct:
INT16 ID;
INT16 X, Y;
Actor struct:
INT16 ID;
INT16 X, Y;
Actor struct:
UBYTE ID;
UBYTE _pad0;
INT16 X;
INT16 Y;
UBYTE ATTR;
Actor Attributes
.ACTOR_ATTR_H_FIRST = 0x01
.ACTOR_ATTR_CHECK_COLL = 0x02
.ACTOR_ATTR_DIAGONAL = 0x04
DIR
.DIR_DOWN = 0
.DIR_RIGHT = 1
.DIR_UP = 2
.DIR_LEFT = 3
VM_ACTOR_SET_HIDDEN ACTOR, HIDDEN
Actor struct
INT16 ID;
INT16 FRAME;
Actor struct
INT16 ID;
INT16 FRAME;
_text_draw_speed
: Defines the global draw speed for text fields. The value range is0
to7
. A value of0
will render the text as fast as possible. Any other value will be the number of frames between each character rendering._text_in_speed
: Defines the speed at which the dialogue window will exit the screen. The value ranges is0
to7
. A value of0
will make the dialogue to move at2px
per frame. Any other value will be number of pixels per frame._text_out_speed
: Defines the speed at which the dialogue window will exit the screen. The value ranges is0
to7
. A value of0
will make the dialogue to move at2px
per frame. Any other value will be number of pixels per frame._text_ff_joypad
: When set to1
the dialogue text will be fast forwarded if any button is pressed.
Those fields can be set using VM_SET_CONST_INT8
.
Loads a text in memory that contains N
variables.
The text string is defined using the .asciz
command:
VM_LOAD_TEXT 0
.asciz "text to render"
The following format specifiers allow to render variables as part of the text:
%d
Render a variable value%Dn
Render a variable value withn
length%c
Render a character based on the variable value
The variables need to be defined before the .asciz
call using .dw
followed by a list of N
variables in the order they'll be rendered.
VM_LOAD_TEXT 3
.dw VAR_0, VAR_1, VAR_1
.asciz "Var 0 is %d, Var 1 is %d, Var 2 is %d"
The text string can contain escape sequence that modify the behavior or apparence of the text.
\001\x
Sets the text speed for the next characters in the current text.x
is a value between1
and8
that represents the number of frames between the render of a character using2^(x-2)
.\002\x
Sets the text font\003\x\y
Sets the position for the next character\004\x\y
Sets the position for the next character relative to the last character\005\
TBD\006\mask
Wait for input to continue to the next character.\007\n
Inverts the colors of the following characters.\n
Next line\r
Scroll text one line up
Renders the text in the defined layer (overlay, by default)
Changes the layer where the text will be rendered.
.TEXT_LAYER_BKG = 0 - Render text in the background layer
.TEXT_LAYER_WIN = 1 - Render text in the overlay layer
.UI_COLOR_BLACK = 0
.UI_COLOR_WHITE = 1
.UI_DRAW_FRAME = 1
.UI_AUTO_SCROLL = 2
SPEED
.OVERLAY_IN_SPEED = -1
.OVERLAY_OUT_SPEED = -2
.OVERLAY_SPEED_INSTANT = -3
.UI_NONMODAL = 0
.UI_MODAL = 1
.UI_WAIT_NONE = 0 - No wait
.UI_WAIT_WINDOW = 1 - Wait until the window moved to its final position
.UI_WAIT_TEXT = 2 - Wait until all the text finished rendering
.UI_WAIT_BTN_A = 4 - Wait until "A" is pressed
.UI_WAIT_BTN_B = 8 - Wait until "B" is pressed
.UI_WAIT_BTN_ANY = 16 - Wait until any button is pressed
COLOR
.UI_COLOR_BLACK = 0
.UI_COLOR_WHITE = 1
OPTIONS
.UI_DRAW_FRAME = 1
.UI_AUTO_SCROLL = 2
ID
.FRAME_TILE_ID = 0xC0
.CURSOR_TILE_ID = 0xCB
LEN
.FRAME_LENGTH = 9
.CURSOR_LENGTH = 1
DIRECTION
.UI_PRINT_LEFTTORIGHT = 0
.UI_PRINT_RIGHTTOLEFT = 1
Scrolls the rectangle area defined by X
, Y
, W
and H
up by one row and fills the lower row with COLOR
.
COLOR
.UI_COLOR_BLACK = 0
.UI_COLOR_WHITE = 1
Defines the scrollable area (in tiles, using X
, Y
, W
and H
) for the overlay. When the text overflows that area it'll scroll up by 1 row and fill the lower row with COLOR
.
Using .UI_AUTO_SCROLL
in VM_OVERLAY_SHOW
sets the scrollable area to the whole overlay.
.UI_COLOR_BLACK = 0
.UI_COLOR_WHITE = 1
Renders a tileset from the background VRAM starting from a given tile IDX
into the given X
and Y
Copies a section of tiles from the scene background (in tiles, using SX
, SY
) to the given overlay coordinates (in tiles, using X_IDX
, Y_IDX
, W
and H
).
OPTIONS
.UI_MENU_STANDARD = 0
.UI_MENU_LAST_0 = 1
.UI_MENU_CANCEL_B = 2
.MENUITEM X, Y, iL, iR, iU, iD
Moves the camera to a position set in the stack at a given SPEED
and configures the lock status of the camera.
AFTER_LOCK
.CAMERA_LOCK = 0b00000011
.CAMERA_LOCK_X = 0b00000001
.CAMERA_LOCK_Y = 0b00000010
.CAMERA_UNLOCK = 0b00000000
.CAMERA_SHAKE_X = 1
.CAMERA_SHAKE_Y = 2
FLAGS
.FADE_NONMODAL = 0x00
.FADE_MODAL = 0x01
.FADE_OUT = 0x00
.FADE_IN = 0x02
IS_MODAL
.FADE_NONMODAL = 0x00
.FADE_MODAL = 0x01
IS_MODAL
.FADE_NONMODAL = 0x00
.FADE_MODAL = 0x01
SLOT
.OVERRIDE_DEFAULT = 0x80
JOYID
.JOY0 = 0
.JOY1 = 1
.JOY2 = 2
.JOY3 = 3
OPERANDS
.R_INT8 ARG0
.R_INT16 ARG0
.R_REF ARG0
.R_REF_IND ARG0
.R_REF_SET ARG0
.R_OPERATOR ARG0
OPERATORS
.ADD = '+'
.SUB = '-'
.MUL = '*'
.DIV = '/'
.MOD = '%'
.B_AND = '&'
.B_OR = '|'
.B_XOR = '^'
.B_NOT = '~'
.ABS = '@'
.MIN = 'm'
.MAX = 'M'
.ISQRT = 'Q'
.EQ = 1
.LT = 2
.LTE = 3
.GT = 4
.GTE = 5
.NE = 6
.AND = 7
.OR = 8
.NOT = 9
.R_STOP
Initializes RNG seed
Returns random value between MIN
and MIN
+ LIMIT
Load script into timer context
Start a timer calling once every INTERVAL
* 16 frames
Stops a timer.
Resets a timer countdown to 0.
Starts playing a music track.
.MUSIC_NO_LOOP = 0
.MUSIC_LOOP = 1
Stops any music track currently playing.
Sets master volume to VOL
Mutes/unmutes channels using MASK
. The 4 lower bits of the mask represent the 4 audio channels.
Mask |
Channel 1 | Channel 2 | Channel 3 | Channel 4 |
---|---|---|---|---|
0b0000 |
Muted | Muted | Muted | Muted |
0b0001 |
Muted | Muted | Muted | Not Muted |
0b0010 |
Muted | Muted | Not Muted | Muted |
0b0011 |
Muted | Muted | Not Muted | Not Muted |
0b0100 |
Muted | Not Muted | Muted | Muted |
0b0101 |
Muted | Not Muted | Muted | Not Muted |
0b0110 |
Muted | Not Muted | Not Muted | Muted |
0b0111 |
Muted | Not Muted | Not Muted | Not Muted |
0b1000 |
Not Muted | Muted | Muted | Muted |
0b1001 |
Not Muted | Muted | Muted | Not Muted |
0b1010 |
Not Muted | Muted | Not Muted | Muted |
0b1011 |
Not Muted | Muted | Not Muted | Not Muted |
0b1100 |
Not Muted | Not Muted | Muted | Muted |
0b1101 |
Not Muted | Not Muted | Muted | Not Muted |
0b1110 |
Not Muted | Not Muted | Not Muted | Muted |
0b1111 |
Not Muted | Not Muted | Not Muted | Not Muted |
Sets music playback position
Play a sound effect asset
Set sound effect asset for text
Attach script to music event
To save or load the state of the game an EXCEPTION_SAVE
or EXCEPTION_LOAD
exception should be raised using VM_RAISE
.
There's three save slots (0
, 1
and 2
) available to save. .SAVE_SLOT n
is used to set which one will be used.
For example, to save on slot 0
:
VM_RAISE EXCEPTION_SAVE, 1
.SAVE_SLOT 0
And to load from slot 0
:
VM_RAISE EXCEPTION_LOAD, 1
.SAVE_SLOT 0
Reads COUNT
variables from save slot SLOT
into variable DEST
and puts the result of the operation into RES
.
Erases data in save slot SLOT
.
MODE
.SPRITES_SHOW = 0
.SPRITES_HIDE = 1
MODE
.MODE_8X8 = 0
.MODE_8X16 = 1
Loads a new tileset into the background VRAM tiles starting at a given tile id (IDX
).
.PALETTE_COMMIT = 1
.PALETTE_BKG = 2
.PALETTE_SPRITE = 4
.DMG_PAL COL1, COL2, COL3, COL4
.CGB_PAL R1,G1,B1 R2,G2,B2 R3,G3,B3 R4,G4,B4
MODE
.SIO_MODE_NONE = 0
.SIO_MODE_MASTER = 1
.SIO_MODE_SLAVE = 2
Latch RTC value for access
Read RTC value from a variable.
WHAT
.RTC_SECONDS = 0x00
.RTC_MINUTES = 0x01
.RTC_HOURS = 0x02
.RTC_DAYS = 0x03
Write RTC value into a variable.
WHAT
.RTC_SECONDS = 0x00
.RTC_MINUTES = 0x01
.RTC_HOURS = 0x02
.RTC_DAYS = 0x03
Start or stop RTC
START
.RTC_STOP = 0
.RTC_START = 1
Transfers SGB packet(s). Data of variable length is placed after this instruction, for example:
VM_SGB_TRANSFER
.db ((0x04 << 3) | 1), 1, 0x07, ((0x01 << 4) | (0x02 << 2) | 0x03), 5,5, 10,10, 0, 0, 0, 0, 0, 0, 0, 0 ; ATTR_BLK packet
SGB packet size is a multiple of 16 bytes and encoded in the packet itself, see SGB developer manual.
Enables or disables rumble on a cart that has that function.