-
Notifications
You must be signed in to change notification settings - Fork 4
/
overview.html
221 lines (186 loc) · 8.96 KB
/
overview.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<!DOCTYPE html>
<html>
<head>
<title>The Z-Machine Standards Document</title>
<link rel="stylesheet" type="text/css" href="zspec.css">
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
</head>
<body class="pre">
<img class="icon" src="images/iconov.gif" alt="">
<h1>Overview of Z-machine architecture</h1>
<hr>
<img src="images/diagram.gif" alt="">
<p>The Z-machine is a design for an imaginary computer: Z is for 'Zork',
the adventure game it was originally designed to play. Like any
computer, it stores its information (mostly) in an array of variables
numbered from 0 up to some large number: this is called its <strong>memory</strong>.
A stock of some 240 memory locations are set aside for easy and
quick access, and these are called <strong>global variables</strong> (since they
are available to any part of the program which is running, at any
time).</p>
<p>The two important pieces of information not stored in memory
are the <strong>program counter</strong> (PC) and the <strong>stack</strong>.
The Z-machine continuously runs a program by getting the
instruction stored at position PC in memory, acting on the
instruction and then moving the PC forward to the next. The
<strong>instruction set</strong> of the Z-machine (the range of possible
actions and how they are encoded as numbers in memory)
occupies much of this document.</p>
<p>Programs are divided into <strong>routines</strong>: the Z-machine is always
executing a particular routine, the one which the PC currently
points inside. However, some instructions cause the Z-machine
to <strong>call</strong> a new routine and then to return where the first
routine left off. The Z-machine therefore needs to remember
details of where to go back, and it stores these on the stack.</p>
<p>The stack is a second bank of memory, quite separate from the
main one, which has variable size: initially it is empty.
From time to time values are added to, or taken from, the top
of the stack. As well as being used to keep
return details, the stack is also used to store <strong>local
variables</strong> (values needed only by a particular routine) and,
for short periods only, the partial results of calculations.</p>
<p>Thus, whereas most physical processors (e.g. Z80 or 6502) have
a number of quick-access variables outside of memory (called
"registers") and a stack inside memory, the Z-machine has the
reverse: it has global variables inside memory and a stack
kept outside.</p>
<p>There is no access to hardware except by executing particular
Z-machine instructions. For instance, <strong>read</strong> and
<strong>read_char</strong> allow use of the keyboard; <strong>print</strong> and
<strong>draw_picture</strong> allow use of the screen. The screen's
image is not stored anywhere in memory. Conversely, hardware
can cause the Z-machine to <strong>interrupt</strong>, that is, to
make a spontaneous call to a particular routine, interrupting
what it was previously working on. This happens only if
the program has previously requested it: for example, by
setting a sound effect playing and asking for a routine to
be called when it finishes; or by asking for an interrupt if
thirty seconds pass while the player is thinking what to type.</p>
<hr>
<p>This simple architecture is overlaid by a number of
special structures which the Z-machine maintains inside
memory. There are around a dozen of these but the most
important are:</p>
<ul class="clean">
<li>the <strong>header</strong>, at the bottom of memory, giving details
about the program and a map of the rest of memory;</li>
<li>the <strong>dictionary</strong>, a list of English words which the
program expects that it might want to read from the keyboard;</li>
<li>the <strong>object tree</strong>, an arrangement of chunks of
memory called <strong>objects</strong>.</li>
</ul>
<p>The Z-machine is primarily used for adventure games, where the
dictionary holds names of items and verbs that the player might
type, and the objects tend to be the places and artifacts which
make up the game. Each object in the tree may have a <strong>parent</strong>,
a <strong>sibling</strong> and a <strong>child</strong>. For instance, in the start
position of 'Zork I':</p>
<blockquote>
<strong>West of House</strong>
<p>You are standing in an open field west of a white house,
with a boarded front door. There is a small mailbox here.</p>
<p>>open mailbox</p>
<p>Opening the small mailbox reveals a leaflet.</p>
</blockquote>
<p>At this point (part of) the game's object tree looks like this:</p>
<pre>
[ 41] ""
. [ 68] "West of House"
. . [ 21] "you"
. . [239] "small mailbox"
. . . [ 80] "leaflet"
. . [127] "door"
</pre>
<p>Note that objects are numbered from 1 upward. (Object 41 is a
dummy object being used by the game to contain all the "rooms"
or locations, and it has many more children besides object 68.)
The parent of the player is "West of House", whose parent is 41,
which has no parent. The sibling of the player is the mailbox;
the child of the mailbox is the leaflet; the sibling of the
mailbox is the door and so on.</p>
<p>Objects are bundled-up collections of variables, which come in
two kinds: <strong>attributes</strong> and <strong>properties</strong>. Attributes
are simply flags, that is, they can be set or unset, but have no
numerical value. Properties hold numbers, which may in turn
represent pieces of text or other information. For instance,
one of the properties of the mailbox object above contains the
information that the English word "mailbox" refers to it.
One of the attributes of the mailbox object is set to indicate
that it's a container, whereas the same attribute for the
leaflet object is unset. Here is a breakdown of the state of
the mailbox:</p>
<pre>
239. Attributes: 30, 34
Parent object: 68 Sibling object: 127 Child object: 80
Property address: 2b53
Description: "small mailbox"
Properties:
[49] 00 0a
[46] 54 bf 4a c3
[45] 3e c1
[44] 5b 1c
</pre>
<p>So the only set attributes are 30 and 34: all others are unset.
Values are given for properties 44, 45, 46 and 49. The Z-machine
itself does not know or care what this information means: that
is for the program to sort out.</p>
<p>As a final example, here is part of one of the routines in
'Zork I':</p>
<pre>
l0006: print_ret "Suicide is not the answer."
l0007: je g57 #84 ~l0008
je g48 #15 ~rfalse
print_ret "Why don't you just walk like normal people?"
l0008: je g57 #63 ~l0009
print_ret "How romantic!"
l0009: je g57 #3b ~rfalse
get_parent "mirror" local0
get_parent "mirror" sp
je g6b local0 sp ~l0010
print_ret "Your image in the mirror looks tired."
l0010: print_ret "That's difficult unless your eyes are prehensile."
</pre>
<hr>
<p>Z-machine programs are stored on disc, or archived
on the Internet, in what are called <strong>story files</strong>.
(Since they were introduced to hold interactive stories.)
A story file consists of a snapshot of main memory only.
The processor begins to run a story file by starting with an
empty stack and a PC value set according to some information
in the story file's header. Note that the story file has to be
set up with many of the structures in memory, such as the
dictionary and the object tree, already created and with
sensible contents.</p>
<p>The first byte of any story file, and so the byte at
memory address 0, always contains the <strong>version number</strong>
of the Z-machine to be used. The design was evolutionary
over a period of a decade: as version number increases, the
instruction set grows and tables are reformatted to allow
more room for larger games. All of Infocom's games can be
played using versions between 3 (the majority) and 6.
Games compiled by Inform in the 1990s mainly use versions 5
or 8.</p>
<hr>
<p>
<a href="index.html">Contents</a> /
<a href="preface.html">Preface</a> /
<a href="overview.html">Overview</a>
</p>
<p>Section
<a href="sect01.html">1</a> / <a href="sect02.html">2</a> /
<a href="sect03.html">3</a> / <a href="sect04.html">4</a> /
<a href="sect05.html">5</a> / <a href="sect06.html">6</a> /
<a href="sect07.html">7</a> / <a href="sect08.html">8</a> /
<a href="sect09.html">9</a> / <a href="sect10.html">10</a> /
<a href="sect11.html">11</a> / <a href="sect12.html">12</a> /
<a href="sect13.html">13</a> / <a href="sect14.html">14</a> /
<a href="sect15.html">15</a> / <a href="sect16.html">16</a>
</p>
<p>Appendix
<a href="appa.html">A</a> / <a href="appb.html">B</a> /
<a href="appc.html">C</a> / <a href="appd.html">D</a> /
<a href="appe.html">E</a> / <a href="appf.html">F</a>
</p>
<hr>
</body>
</html>