Skip to content
@deckyard/zmachine

Cheatsheet

Every export. Names are written bare; after import "@deckyard/zmachine/" as zmachine; they read zmachine.run(...). Every failure throws an Error with kind "zmachine". Functions that return a Machine leave their m argument unchanged.

Types

Wait

What a machine needs before it can go on. Zero value Line.

VariantNotes
Linea line of input: send
Keyone key: send
Savea save file to store: saveData, then resume
Restorea save file to load: restore, or resume to refuse
Quitthe game has ended

Info

A story's identity, from its header.

FieldNotes
version as int3, 4, 5, 7 or 8
release as intrelease number
serial as stringsix characters, usually the compile date
checksum as intheader checksum

Machine

A running story. Opaque: read it through the functions below. The fields are listed because the type is exported, not for use.

FieldNotes
mem as bytesstory memory
story as bytesthe story file as loaded
version as intZ-machine version
pc as intprogram counter
stack as list of intlocals and evaluation stack
sp as intnext free stack slot
bp as intfirst local of the current frame
frames as list of intfive ints per call frame
fp as intints of frames in use
ops as list of intoperands of the current instruction
nops as inthow many of ops are in use
globals as intaddress of the global variables
objects as intaddress of the object table
abbrevs as intaddress of the abbreviations table
dict as intaddress of the dictionary
staticBase as intfirst byte of static memory
packShift as intpacked address multiplier, as a shift
routineOffset as intversion 7 routine offset
stringOffset as intversion 7 string offset
alphabet as list of intalphabets A0, A1, A2 as ZSCII
zscii as list of stringZSCII code to text
waitOn as intwhat the machine waits for
readText as inttext buffer of the pending read
readParse as intparse buffer of the pending read
readStore as intstore variable of the pending read
resultPc as intresult byte of a pending save or restore
out as list of stringlower window text of the last run
screen as booloutput stream 1 selected
transcript as booloutput stream 2 selected
tables as list of intoutput stream 3 tables
window as intselected window
upper as list of stringupper window rows
row as intupper window cursor row
col as intupper window cursor column
width as intscreen width
height as intscreen height
font as intcurrent font
rng as intrandom generator state
rngMode as intrandom, seeded or counting
rngCount as intnext value when counting
rngTop as intcycle length when counting
undos as list of Snapsave_undo states
saved as bytesQuetzal file of a pending save
limit as intinstructions per call

Snap

A save_undo state, held in Machine.undos.

FieldNotes
dynamic as bytesdynamic memory
stack as list of intthe used stack
frames as list of intthe used frames
pc as intthe save_undo's store byte

Loading

FunctionReturnsNotes
load(story)Machinefrom the story file's bytes; throws unless it is version 3, 4, 5, 7 or 8
open(path)Machinefrom a file
info(m)Infoversion, release, serial, checksum

Settings

Set them before the first run; games read them when they start.

FunctionReturnsNotes
setScreen(m, columns, rows)Machine20-255 columns, 1-255 rows; default 80 by 255, which never fills
seed(m, n)Machinerepeatable random numbers, n at least 1
setLimit(m, steps)Machineinstructions one call may run before it throws; default 50 million

Playing

FunctionReturnsNotes
run(m)Machinestart the game; runs to the first wait
waiting(m)Waitwhat the game needs; throws before run
send(m, text)Machineanswer Line or Key; a key is the first character, "" is Return

Screen

FunctionReturnsNotes
output(m)stringlower window text since the last call that ran the game
upperWindow(m)list of stringthe upper window's rows; up to version 3 the first is the status line

Saving

FunctionReturnsNotes
saveData(m)bytesthe Quetzal file of a Save wait
resume(m, ok)Machineanswer Save with whether storing worked, or refuse a Restore
restore(m, data)Machineanswer Restore with a Quetzal file; throws on a bad file and leaves m waiting

Constants

NameValueWhat it is
ERROR_KIND"zmachine"the kind every Error here carries
VERSIONgeneratedthe deck version, injected from the git tag