Login
You're viewing the functional.cafe public feed.
  • Nov 15, 2026, 11:09 PM

    Your Z-machine interpreter isn't complete until it can play Tetris.

    No, really. For the game "Border Zone," Infocom added the concept of timed command input, whereby if you don't press a key in a certain amount of time, the game code gets control again. So obviously as soon as people reversed engineered the Z-machine in the 1990s, @zarfeblong implemented Tetris.

    #AppleII #retrocomputing #interactivefiction

    screenshot of Tetris as implemented in Z-machine code, An, uh, Interactive Demonstration by Andrew Plotkin, Release 2, Serial 951111, Inform v1502
    💬 3🔄 14⭐ 0

Replies

  • Nov 16, 2026, 2:03 AM

    You have to understand, this is 6502 assembly language. We didn't build a main() routine that calls a memory() iterator with a decode() method that returns an Instruction object with a handler property. The parser is an infinite loop that only ends because the quit instruction calls ProDOS MLI QUIT instead of jumping back to the beginning of the loop. In the optimal case, we don't hit a single JSR or RTS while parsing and executing an entire instruction. Everything is JMP, branch, or inline.

    💬 1🔄 1⭐ 0
  • Nov 16, 2026, 2:05 AM

    Anyway, we optimized the parser for the 99% case and we were not terribly well-prepared for "by the way, reading keyboard input might SUDDENLY CALL A Z-MACHINE ROUTINE which can call other Z-machine routines which return and you have to catch the return of the last one (not the nested ones!) and go back to reading keyboard input" but we made it work.

    💬 1🔄 0⭐ 0
  • Nov 16, 2026, 2:09 AM

    We may have put a tiny bit of Z-machine bytecode in an unused field in the story header and pointed the Z-program counter there and made up a new opcode(*) that calls a routine that gets the return value that we told the callback Z-routine to push to the Z-stack then cleans up the whole mess and decides whether to keep reading keyboard input or just give up.

    (*) Technically all opcodes are made up

    💬 1🔄 0⭐ 0
  • 💬 0🔄 0⭐ 0
  • 💬 0🔄 0⭐ 0
  • 💬 1🔄 0⭐ 0
  • Nov 16, 2026, 3:01 AM

    @zarfeblong I was inspired by @a2_qkumba's solution to a problem we had in Wizard Replay. He used an unused opcode in the Apple Pascal bytecode to simulate swapping virtual floppies while loading Wizardry.

    Infocom handled it by setting the Z-program counter to 0 before initiating the callback routine, then checking for that special return address on every single call return. Is that better or worse?

    💬 1🔄 2⭐ 0
  • Nov 16, 2026, 3:04 AM

    @a2_4am @a2_qkumba Worse, because it costs more time. Better, in the sense that messing with the header will probably bite you two years from now when Stu or Marc or somebody updates the ZIL spec again.

    💬 1🔄 0⭐ 0
  • 💬 0🔄 0⭐ 0