techbelly/elm-zmachine/1.1.0

ZMachine.Types

Types for the Z-Machine interpreter.

Import this module to pattern match on result types:

import ZMachine.Types exposing (StepResult(..), OutputEvent(..))

Machine State

type alias ZMachine =
{ memory : Memory
, originalMemory : Memory
, pc : Int
, stack : List Int
, callStack : List CallFrame
, output : List OutputEvent
, outputStreams :
{ stream1 : Bool
, stream2 : Bool
}
, stream3Stack :
List
{ tableAddr : Int
, count : Int
}
, randomState :
{ seed : Int
, count : Int
}
, currentWindow : Window
, upperWindow : UpperWindow
, playerTracking : PlayerTracking
}

The complete state of a running Z-Machine. You will receive this inside each StepResult variant.

type alias Memory =
Memory

Opaque Z-Machine memory image, re-exported so consumers can annotate ZMachine fields without a second import.

type alias CallFrame =
CallFrame

A single call stack frame, re-exported so consumers can annotate ZMachine fields without a second import.

type alias Snapshot =

Opaque snapshot of Z-Machine state, produced by the save path and consumed by the restore path. See ZMachine.Snapshot for operations.

Step Results

= Continue (List OutputEvent) ZMachine
| NeedInput LineInputInfo (List OutputEvent) ZMachine
| NeedChar (List OutputEvent) ZMachine
| NeedSave Snapshot (List OutputEvent) ZMachine
| NeedRestore (List OutputEvent) ZMachine
| Halted (List OutputEvent) ZMachine
| Error ZMachineError (List OutputEvent) ZMachine

Result of executing one or more instructions.

Every variant carries a List OutputEvent containing the output accumulated during the call that produced this result, followed by the current ZMachine. The returned machine's output buffer is always empty — the run-loop drains it into the variant, so callers never need to manage output state themselves.

  • Continue — step budget exhausted, call ZMachine.runSteps again to keep going.
  • NeedInput — the machine is waiting for a line of player input. Call ZMachine.provideInput to resume.
  • NeedChar — the machine is waiting for a single keypress. Call ZMachine.provideChar to resume.
  • NeedSave — the story executed a save opcode. The attached snapshot captures the state to be persisted; call ZMachine.provideSaveResult to resume.
  • NeedRestore — the story executed a restore opcode. Call ZMachine.provideRestoreResult with a snapshot (or Nothing on failure) to resume.
  • Halted — the story called quit.
  • Error — an unrecoverable error occurred.
= DivisionByZero
| StackUnderflow
| InvalidOpcode Int
| InvalidVariable Int

Errors that can halt the machine.

  • DivisionByZero — a div or mod instruction divided by zero.
  • StackUnderflow — a pop from an empty evaluation stack.
  • InvalidOpcode — an unrecognised opcode number.
  • InvalidVariable — a variable reference outside 0–255.
type alias LineInputInfo =
{ maxLength : Int
, textBufferAddr : Int
, parseBufferAddr : Int
}

Metadata about a line-input request from sread/aread. Carried by the NeedInput variant of StepResult.

Output

= PrintText String
| PrintObject String
| ShowStatusLine StatusLine
| SetBufferMode Bool
| SetTextStyle Int
| SetColour Int Int
| PlaySound Int

Structured output events for the host to render.

  • PrintText — display a string. Line breaks are embedded as \n in the string; consecutive prints are coalesced into a single event, so a rendered paragraph typically arrives as one PrintText.
  • PrintObject — display a string that the game identified as an object's short name (via print_obj or print_addr). Rendered identically to PrintText; split out so clients can learn the names of the objects the game refers to.
  • ShowStatusLine — update the status bar with a StatusLine.
  • SetBufferMode — enable or disable word-wrap buffering.
  • SetTextStyle — change text style (bold, italic, etc.).
  • SetColour — change foreground/background colours.
  • PlaySound — play a sound effect by number.
type alias StatusLine =
{ locationId : Int
, locationName : String
, mode : StatusLineMode
}

Status line data shown at the top of the screen.

  • locationId — the object number of the current location.
  • locationName — the short name of the current location.
  • mode — score/turns, time, or raw screen rows depending on version.
= ScoreAndTurns Int Int
| TimeOfDay Int Int
| ScreenRows (List String)

How the status line content is provided.

  • ScoreAndTurns score turns — V3 score game.
  • TimeOfDay hours minutes — V3 time game (hours 0–23, minutes 0–59).
  • ScreenRows rows — V5+ game that draws its own status area. Each string is one rendered row of the upper window, trimmed.
type Window
= Upper
| Lower

Screen window identifier. Z-Machine v3 has two windows:

  • Upper — the fixed-size status area at the top.
  • Lower — the scrolling main text area.
type alias UpperWindow =
{ height : Int
, rows : Dict Int (Array Char)
, cursorRow : Int
, cursorCol : Int
, width : Int
, firstPrintedObj : Int
}

Virtual upper window state. V5 games draw the status line by writing directly to the upper window with cursor positioning. The interpreter tracks this internally and emits a ShowStatusLine event when the game switches back to the lower window.