-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjump.c
374 lines (320 loc) · 8.01 KB
/
jump.c
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*
** File: jump.c
**
** (C)opyright 1987-1992 InfoTaskforce.
*/
#include "infocom.h"
Void
std_gosub ()
{
extern word pc_offset ;
extern word pc_page ;
extern word *stack_base ;
extern word *stack_var_ptr ;
extern word *stack ;
extern word param_stack[] ;
register word *param_ptr = ¶m_stack[0] ;
register word address ;
register word parameter ;
register int num_params ;
int vars ;
num_params = (int) *param_ptr++ ;
if (( address = *param_ptr++ ) == 0 )
store ( address ) ;
else
{
--num_params ;
*(--stack) = pc_page ;
*(--stack) = pc_offset ;
/*
** Push offset of old stack_var_ptr from stack_base onto stack
*/
*(--stack) = (word)(stack_var_ptr - stack_base) ;
pc_page = STD_PAGE ( address ) ;
pc_offset = STD_OFFSET ( address ) ;
fix_pc () ;
/*
** The value of the current stack pointer is the
** new value of stack_var_ptr.
*/
stack_var_ptr = stack ;
/*
** In interpreters prior to VERSION_5, the "stack_var_ptr"
** pointed to the first local variable on the z_code stack.
** In VERSION_5 and subsequent interpreters, the "stack_var_ptr"
** points to the stack entry above the first local variable on
** the z_code stack.
**
** However, there is no reason for us not to adopt the VERSION_5
** method for all versions of the interpreter. This decision
** affects the following opcodes:
** gosub, std_rtn, load_var & put_var.
**
** Interpreters prior to VERSION_5 contained the following
** statement at this point:
**
** --stack_var_ptr ;
*/
/*
** Global variables 1 to 15 are Local variables, which
** reside on the stack (and so are local to each procedure).
**
** There are words reserved after the gosub opcode in the
** calling procedure to initialise all local variables.
** However, if there are any parameters on the parameter
** stack, these are used instead.
**
** The use of parameters also allows procedures to be
** passed variables by value.
*/
vars = (int) next_byte () ;
while ( vars-- )
{
parameter = next_word () ;
if ( (--num_params) >= 0 )
parameter = *param_ptr++ ;
*(--stack) = parameter ;
}
}
}
Void
plus_gosub ()
{
extern word pc_offset ;
extern word pc_page ;
extern word *stack_base ;
extern word *stack_var_ptr ;
extern word *stack ;
extern word param_stack[] ;
register word *param_ptr = ¶m_stack[0] ;
register word address ;
register word parameter ;
register int num_params ;
int vars ;
num_params = (int) *param_ptr++ ;
if (( address = *param_ptr++ ) == 0 )
store ( address ) ;
else
{
--num_params ;
*(--stack) = pc_page ;
*(--stack) = pc_offset ;
/*
** Push offset of old stack_var_ptr from stack_base onto stack
*/
*(--stack) = (word)(stack_var_ptr - stack_base) ;
pc_page = PLUS_PAGE ( address ) ;
pc_offset = PLUS_OFFSET ( address ) ;
fix_pc () ;
/*
** The value of the current stack pointer is the
** new value of stack_var_ptr.
*/
stack_var_ptr = stack ;
/*
** In interpreters prior to VERSION_5, the "stack_var_ptr"
** pointed to the first local variable on the z_code stack.
** In VERSION_5 and subsequent interpreters, the "stack_var_ptr"
** points to the stack entry above the first local variable on
** the z_code stack.
**
** However, there is no reason for us not to adopt the VERSION_5
** method for all versions of the interpreter. This decision
** affects the following opcodes:
** gosub, std_rtn, load_var & put_var.
**
** Interpreters prior to VERSION_5 contained the following
** statement at this point:
**
** --stack_var_ptr ;
*/
/*
** Global variables 1 to 15 are Local variables, which
** reside on the stack (and so are local to each procedure).
**
** There are words reserved after the gosub opcode in the
** calling procedure to initialise all local variables.
** However, if there are any parameters on the parameter
** stack, these are used instead.
**
** The use of parameters also allows procedures to be
** passed variables by value.
*/
vars = (int) next_byte () ;
while ( vars-- )
{
parameter = next_word () ;
if ( (--num_params) >= 0 )
parameter = *param_ptr++ ;
*(--stack) = parameter ;
}
}
}
Void
adv_gosub ()
{
call ( FUNCTION ) ;
}
Void
std_rtn ( value )
word value ;
{
extern word pc_offset ;
extern word pc_page ;
extern word *stack_base ;
extern word *stack_var_ptr ;
extern word *stack ;
extern boolean stop ;
extern byte local_params ;
stack = stack_var_ptr ;
/*
** In interpreters prior to VERSION_5, the "stack_var_ptr"
** pointed to the first local variable on the z_code stack.
** In VERSION_5 and subsequent interpreters, the "stack_var_ptr"
** points to the stack entry above the first local variable on
** the z_code stack.
**
** However, there is no reason for us not to adopt the VERSION_5
** method for all versions of the interpreter. This decision
** affects the following opcodes:
** gosub, std_rtn, load_var & put_var.
**
** Interpreters prior to VERSION_5 contained the following
** statement at this point:
**
** ++stack ;
*/
stack_var_ptr = stack_base + (signed_word)*stack++ ;
pc_offset = *stack++ ;
pc_page = *stack++ ;
/*
** Interpreters prior to VERSION_4 did not contain the following
** statements:
**
** if ( pc_page == EMPTY_PAGE )
** {
** stop = TRUE ;
** return ;
** }
**
** Only the "special_gosub ()" function pushes a value of EMPTY_PAGE
** onto the stack in place of "pc_page", and this function is never
** called by an interpreter prior to VERSION_4. Hence there is no
** reason for us not to include these statements in all versions.
*/
if ( pc_page == EMPTY_PAGE )
{
stop = TRUE ;
return ;
}
fix_pc () ;
store ( value ) ;
}
Void
adv_rtn ( value )
word value ;
{
extern word pc_offset ;
extern word pc_page ;
extern word *stack_base ;
extern word *stack_var_ptr ;
extern word *stack ;
extern boolean stop ;
extern byte local_params ;
register word type ;
stack = stack_var_ptr ;
/*
** In interpreters prior to VERSION_5, the "stack_var_ptr"
** pointed to the first local variable on the z_code stack.
** In VERSION_5 and subsequent interpreters, the "stack_var_ptr"
** points to the stack entry above the first local variable on
** the z_code stack.
**
** However, there is no reason for us not to adopt the VERSION_5
** method for all versions of the interpreter. This decision
** affects the following opcodes:
** gosub, std_rtn, load_var & put_var.
**
** Interpreters prior to VERSION_5 contained the following
** statement at this point:
**
** ++stack ;
*/
stack_var_ptr = stack_base + (signed_word)*stack++ ;
type = *stack++ ;
local_params = (byte)type ;
pc_offset = *stack++ ;
pc_page = *stack++ ;
/*
** Interpreters prior to VERSION_4 did not contain the following
** statements:
**
** if ( pc_page == EMPTY_PAGE )
** {
** stop = TRUE ;
** return ;
** }
**
** Only the "special_gosub ()" function pushes a value of EMPTY_PAGE
** onto the stack in place of "pc_page", and this function is never
** called by an interpreter prior to VERSION_4. Hence there is no
** reason for us not to include these statements in all versions.
*/
if ( pc_page == EMPTY_PAGE )
{
stop = TRUE ;
return ;
}
fix_pc () ;
if ( type & FUNCTION )
store ( value ) ;
}
Void
ret_true ()
{
extern proc_ptr jmp_op1[] ;
/*
** Call the current "rtn" opcode ...
*/
(*jmp_op1[0x0B])( TRUE ) ;
}
Void
ret_false ()
{
extern proc_ptr jmp_op1[] ;
/*
** Call the current "rtn" opcode ...
*/
(*jmp_op1[0x0B])( FALSE ) ;
}
Void
jump ( offset )
word offset ;
{
extern word pc_offset ;
pc_offset += offset - 2 ;
fix_pc () ;
}
Void
rts ()
{
extern proc_ptr jmp_op1[] ;
extern word *stack ;
/*
** Call the current "rtn" opcode ...
*/
(*jmp_op1[0x0B])( *stack++ ) ;
}
Void
std_pop_stack ()
{
extern word *stack ;
++stack ;
}
Void
adv_pop_stack ()
{
extern word *stack_base ;
extern word *stack_var_ptr ;
store ( (word)( stack_var_ptr - stack_base ) ) ;
}