NB: Please note that the original version of this message contained URLs to the relevant sections of the Z-machine standard, but the forum engine wouldn’t allow me to include them. I sincerely apologize for any inconvenience caused by this.
Hello everyone!
Recently, I’ve been working on an attempt to implement a Z-machine interpreter in .NET Core, as a hobby project. This is mostly to practice my programming skills in my spare time, as well as to scratch a certain itch - I have never played any IF game, but the concept of the Z-machine has always seemed fascinating to me, ever since I learned about it. So I’m not looking to use an existing interpreter because my primary interest is coding. And I’ve been trying to avoid looking at any existing interpreter’s source code, if possible, and instead use the Z-machine 1.1 standard (at inform-fiction dot org) as my primary reference, complemented by sample Z-machine story files for unit and integration testing.
It appears, however, that there are certain things in the aforementioned standard that are not explained very clearly; there also seem to be some ambiguities here or there. So I’m posting this message in hopes that other developers out there could help me clarify these ambiguities, if possible. In case the standard I linked to is outdated, I will also be grateful if someone could provide me a link to the latest version (I couldn’t find anything newer than 1.1).
I apologize in advance if anything I’m going to ask has already been discussed elsewhere on this site. I’ve been mostly using Google to look for additional information, and I have already been able to clarify a few less obscure moments, some of them with the help of this site. I also think it’s best to collect all the questions in a single thread so that I can easily look up the answers later. But please don’t hesitate to provide a link in case there is an existing thread where a specific issue has already been properly clarified.
With all that in mind, here’s the list of things I could use help with, in no particular order:
1. Table locations
Throughout the entire standard, there are not too many mentions of what kind of table can be stored where:
- An example memory map at the end of S.1 shows that the abbreviation table might be located in dynamic memory, which means it can theoretically be altered by the game during play. Looking at the sample files, this actually seems to be the case.
- S.13.1 also mentions that the default dictionary is stored in static memory, which is also confirmed by examining samples.
- It is also quite clear that certain tables (e.g. the object table) must be located in dynamic memory for the game to properly function.
However, as far as I’m aware, there is no mention of what kind of memory the custom alphabet table (address in byte $34 of the header) or the Unicode translation table (word 3 in header extension table) can be stored in. Am I to assume that they can also be located in dynamic memory, and so it is theoretically not safe to deserialize them in advance? Unfortunately, none of my sample files feature any of these two tables, so I’m unable to confirm or deny that.
2. What’s legal to do during an interrupt routine
S.6.1.1.3 states:
It is illegal to save the game (either with
save
orsave_undo
) during an “interrupt routine” (one coming about through timed input, sound effect termination or newline interrupts).
It does not mention, however, whether it is legal to perform some other action that could alter the entire game state, e.g. restore
(_undo), restart
, or quit
. Am I to assume these kinds of operations are legal? Also, is it legal to use partial save
(the version with 3-4 operands)? I’d assume it is because it only reads a chunk of memory and does not mess with the state of the Z-machine itself, but the standard does not make it clear.
3. User stack in version 6
S.6.6 states:
In Version 6, the Z-machine understands a third kind of stack: a “user stack”, which is a table of words in dynamic memory. The first word in this table always holds the number of spare slots on the stack (so the initial value is the capacity of the stack).
It would be logical to assume that values are pushed onto a user stack in right-to-left order (e.g. for a stack with capacity N at address A, the first word pushed will be at address A + 2*N
, the second one at A + 2*(N-1)
, and so on), but it’s not explicitly stated anywhere. Is my assumption correct?
4. Echoed input and newline intterupt (version 6)
S.7.1.1.1 states that in all versions, including version 6, the player’s input should be echoed to output stream 1 (the screen).
The question: If the current window has a newline intterupt routine set up, and wrapping is enabled, should the newline interrupt routine be triggered when the cursor reaches the right margin while player input is being echoed?
5. Output stream 3 and header word $30
S7.1.2.1 states that in version 6, after stream 3 is closed, the total width of printing (in units) must then be stored in the word at $30 in the header.
The question: What kind of width should be written in case the output was formatted (by giving the output_stream
opcode a third operand): the width of a single line, or the total width of all lines combined?
6. Line count and interrupt countdown
S8.8.3.2 mentions, among others, a “line count” and an “interrupt countdown” as window properties for version 6. Then, S8.8.3.2.2 mentions (quote):
…if the interrupt countdown is set to a non-zero value (which by default it is not), then the line count is decremented on each new-line, and when it hits zero the routine whose packed address is stored in the “newline interrupt routine” property is called before text printing resumes.
It seems as if there is a typo there - it probably should be “interrupt countdown” instead of “line count” that gets decremented. This is only logical, as it is a countdown, after all - i.e. a counter which counts down. Am I correct?
Assuming the answer to my previous question is “yes”, it is also not clear how exactly “line count” is supposed to be made use of: S.8.8.3.2.6 says that the interpreter should use it to see when it should print “[MORE]”, and a line count of -999 means “never print [MORE]”. But what should be done if line count is not -999? The standard does not seem to mention that at all.
7. Non-existing sound effects
The standard explains well that the game needs to make sure a picture if available before attempting to draw it, and that attempting to draw a non-existent picture is illegal. It does not say anything similar for sound effects, though. Am I to assume that if a sound effect is not available, any call to sound_effect
(if it’s not a beep) should simply be ignored?
8. read_mouse
and menu word
The specification for the read_mouse
opcode dictates that one of the words written to the resulting table should contain the number of the selected menu and its item. But it does not say what should be written there if the mouse pointer does not point to a menu item, or if it points to a menu heading but not the menu contents. Should the word be left unchanged then, or zeroed out?
9. Purpose of exposing system menu numbers (0-2)
S10.4.2 states:
Menus are numbered from 0 upwards. 0, 1 and 2 are reserved for the interpreter to manage (this system has only been implemented on the Macintosh, wherein 0 is the Apple menu, 1 the File menu and 2 the Edit menu).
But what is the exact purpose of making the game aware these menus even exist? The game cannot make an assumption of what the contents of these menus might be (it could differ between OS versions, for example), and there is no way to communicate this information to the game. Is it just a quirk of the original implementation?
10. Undefined EXT opcodes
The following notes in S.14.2 and 14.2.1 are quite perplexing:
Formally, it is illegal for a game to contain an opcode not specified for its version. <…> However, extended opcodes in the range EXT:29 to EXT:255 should be simply ignored.
The big problem here is that the exact format of each instruction, like the presence of store and/or branch data, depends on the particular opcode. This means that if an unknown opcode is encountered, it is not possible to properly deserialize an instruction from the data stream because there is no way to know whether we’ve read it all, or if there’s additional store/branch data remaining - which might lead to invalid data being read when the next instruction is deserialized. Ergo, I have no idea how am I supposed to ignore illegal opcodes.
11. Leftover input from read
The spec for the read
opcode, in particular, the timed input part, says:
In Version 4 and later, if the operands
time
androutine
are supplied (and non-zero) then the routine callroutine()
is made everytime/10
seconds during the keyboard-reading process. If this routine returnstrue
, all input is erased (to zero) and the reading process is terminated at once.
Pay attention to the second part: if the interrupt routine returns true, then all the input already in the buffer should be erased. And compare to what is says next, when mentioning the new format of the text buffer in version 5 and later (emphasis mine):
Moreover, if byte 1 contains a positive value at the start of the input, then read assumes that number of characters are left over from an interrupted previous input, and writes the new characters after those already there.
However, I’m unable to understand how this might be possible - won’t the buffer always be empty because all input gets erased in case it is interrupted? The logical assumption is that it should only be erased in version 4 and kept in later versions, but this is not expliclitly stated anywhere. Further clarification is necessary.
12. scan_table
Two things here:
- The spec for the
scan_table
opcode says that an additional argument can be used to configure whether the table to be scanned contains words or bytes. Now, suppose the first operand (the value to search for) resolves to a word (i.e. it is anything but a “small constant”). In this case, should we immediately return 0 if the high byte of this word is non-zero, or should we truncate the word to a byte and perform the search as usual? - From the default value of the third argument ($82 = 10000010b), it appears that the field length of the table is always given in bytes, even if we’re searching for a word. Is it legal in this case to have a field length of 1, and if it is, what should be done in this scenario?
13. Empty object names
In my sample files, some objects in the object table have a zero-length name (i.e. the value of the first byte in the property table is 0). Is it legal to print these objects with print_obj
?
14. Some dictionary words have invalid format
In some of my sample files, the default dictionary contains words which do not have the high bit set in the final word of the Z-encoded string. Some of them appear to be exact duplicates of the neighboring word, while a few others are unique. In theory, such words are not able to be looked up, and they also break the binary search algorithm because they violate S13.5, which states that all words must come in numerical order of encoded text. Even though the latter is not a problem for any modern software (and I use a hash table instead of binary search anyway), the purpose of these entries is still a bit unclear. What I’d like to know is whether I’m correct in my assumption that such a word should never be found by dictionary lookup. If necessary, I can provide concrete examples as well.
15. store
opcode corner case
The store
opcode features two operands, the first of which is a variable number indicating where to put the result, while the second operand provides the value to put in the location refered to by the first operand.
Now, suppose the first operand indicates that the value should be put into the top of the stack (NB: not pushed), and the second operand is of type “variable” with value 0, which indicates a value should be popped from the stack. There seems to be a conflict here, as the value we’re updating literally ceases to exist during the update process. If this is legal, what should happen? The logical course of action would be to replace the new value now on top of the stack (e.g. we essentialy “pop” a value from under the top of the stack with such an operation), but this is not adequately explained - clarification is needed.
Final note
So far, that’s all I’ve found, but I guess there’s likely to be more as I continue to develop my personal project (I don’t even have a working backend yet). I will post further questions here in this thread as they appear, but for now I will be very grateful if someone is able to help me clarify at least some of the above issues.
Thank you very much.