-
Notifications
You must be signed in to change notification settings - Fork 2
/
interpre.c
485 lines (447 loc) · 14.2 KB
/
interpre.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/* $Id: interpre.c,v 1.3 2000/07/05 15:20:34 jholder Exp $
* --------------------------------------------------------------------
* see doc/License.txt for License Information
* --------------------------------------------------------------------
*
* File name: $Id: interpre.c,v 1.3 2000/07/05 15:20:34 jholder Exp $
*
* Description:
*
* Modification history:
* $Log: interpre.c,v $
* Revision 1.3 2000/07/05 15:20:34 jholder
* Updated code to remove warnings.
*
* Revision 1.2 2000/05/25 22:28:56 jholder
* changes routine names to reflect zmachine opcode names per spec 1.0
*
* Revision 1.1.1.1 2000/05/10 14:21:34 jholder
*
* imported
*
*
* --------------------------------------------------------------------
*/
/*
* interpre.c
*
* Main interpreter loop
*
*/
#include "ztypes.h"
/*#define DEBUG_TERPRE*/
static int halt = FALSE;
/*
* interpret
*
* Interpret Z code
*
*/
int interpret( )
{
zbyte_t opcode;
zword_t specifier, operand[8];
int maxoperands, count, extended, i;
interpreter_status = 1;
/* Loop until HALT instruction executed */
for ( interpreter_state = RUN; interpreter_state == RUN && halt == FALSE; )
{
/* Load opcode and set operand count */
opcode = read_code_byte( );
if ( h_type > V4 && opcode == 0xbe )
{
opcode = read_code_byte( );
extended = TRUE;
}
else
extended = FALSE;
count = 0;
/* Multiple operand instructions */
if ( ( opcode < 0x80 || opcode > 0xc0 ) || extended == TRUE )
{
/* Two operand class, load both operands */
if ( opcode < 0x80 && extended == FALSE )
{
operand[count++] = load_operand( ( opcode & 0x40 ) ? 2 : 1 );
operand[count++] = load_operand( ( opcode & 0x20 ) ? 2 : 1 );
opcode &= 0x1f;
}
else
{
/* Variable operand class, load operand specifier */
opcode &= 0x3f;
if ( opcode == 0x2c || opcode == 0x3a )
{ /* Extended CALL instruction */
specifier = read_code_word( );
maxoperands = 8;
}
else
{
specifier = read_code_byte( );
maxoperands = 4;
}
/* Load operands */
for ( i = ( maxoperands - 1 ) * 2; i >= 0; i -= 2 )
if ( ( ( specifier >> i ) & 0x03 ) != 3 )
operand[count++] = load_operand( ( specifier >> i ) & 0x03 );
else
i = 0;
}
if ( extended == TRUE )
{
#ifdef DEBUG_TERPRE
fprintf( stderr, "PC = 0x%08lx Op%s = 0x%02x %d, %d, %d\n", pc, "(EX)", opcode,
operand[0], operand[1], operand[2] );
#endif
switch ( ( char ) opcode )
{
/* Extended operand instructions */
case 0x00:
z_save( count, operand[0], operand[1], operand[2] );
break;
case 0x01:
z_restore( count, operand[0], operand[1], operand[2] );
break;
case 0x02:
z_log_shift( operand[0], operand[1] );
break;
case 0x03:
z_art_shift( operand[0], operand[1] );
break;
case 0x04:
z_set_font( operand[0] );
break;
case 0x09:
z_save_undo( );
break;
case 0x0a:
z_restore_undo( );
break;
case 0x0b:
z_print_unicode( operand[0] );
break;
case 0x0c:
z_check_unicode( operand[0] );
break;
default:
fatal( "interpret(): Illegal extended operand instruction" );
}
}
else
{
#ifdef DEBUG_TERPRE
fprintf( stderr, "PC = 0x%08lx Op%s = 0x%02x %d, %d, %d\n", pc, "(2+)", opcode,
operand[0], operand[1], operand[2] );
#endif
switch ( ( char ) opcode )
{
/* Two or multiple operand instructions */
case 0x01:
z_je( count, operand );
break;
case 0x02:
z_jl( operand[0], operand[1] );
break;
case 0x03:
z_jg( operand[0], operand[1] );
break;
case 0x04:
z_dec_chk( operand[0], operand[1] );
break;
case 0x05:
z_inc_chk( operand[0], operand[1] );
break;
case 0x06:
z_jin( operand[0], operand[1] );
break;
case 0x07:
z_test( operand[0], operand[1] );
break;
case 0x08:
z_or( operand[0], operand[1] );
break;
case 0x09:
z_and( operand[0], operand[1] );
break;
case 0x0a:
z_test_attr( operand[0], operand[1] );
break;
case 0x0b:
z_set_attr( operand[0], operand[1] );
break;
case 0x0c:
z_clear_attr( operand[0], operand[1] );
break;
case 0x0d:
z_store( operand[0], operand[1] );
break;
case 0x0e:
z_insert_obj( operand[0], operand[1] );
break;
case 0x0f:
z_loadw( operand[0], operand[1] );
break;
case 0x10:
z_loadb( operand[0], operand[1] );
break;
case 0x11:
z_get_prop( operand[0], operand[1] );
break;
case 0x12:
z_get_prop_addr( operand[0], operand[1] );
break;
case 0x13:
z_get_next_prop( operand[0], operand[1] );
break;
case 0x14:
z_add( operand[0], operand[1] );
break;
case 0x15:
z_sub( operand[0], operand[1] );
break;
case 0x16:
z_mul( operand[0], operand[1] );
break;
case 0x17:
z_div( operand[0], operand[1] );
break;
case 0x18:
z_mod( operand[0], operand[1] );
break;
case 0x19:
z_call( count, operand, FUNCTION );
break;
case 0x1a:
z_call( count, operand, PROCEDURE );
break;
case 0x1b:
z_set_colour( operand[0], operand[1] );
break;
case 0x1c:
z_throw( operand[0], operand[1] );
break;
/* Multiple operand instructions */
case 0x20:
z_call( count, operand, FUNCTION );
break;
case 0x21:
z_storew( operand[0], operand[1], operand[2] );
break;
case 0x22:
z_storeb( operand[0], operand[1], operand[2] );
break;
case 0x23:
z_put_prop( operand[0], operand[1], operand[2] );
break;
case 0x24:
z_sread_aread( count, operand );
break;
case 0x25:
z_print_char( operand[0] );
break;
case 0x26:
z_print_num( operand[0] );
break;
case 0x27:
z_random( operand[0] );
break;
case 0x28:
z_push( operand[0] );
break;
case 0x29:
z_pull( operand[0] );
break;
case 0x2a:
z_split_window( operand[0] );
break;
case 0x2b:
z_set_window( operand[0] );
break;
case 0x2c:
z_call( count, operand, FUNCTION );
break;
case 0x2d:
z_erase_window( operand[0] );
break;
case 0x2e:
z_erase_line( operand[0] );
break;
case 0x2f:
z_set_cursor( operand[0], operand[1] );
break;
case 0x31:
z_set_text_style( operand[0] );
break;
case 0x32:
z_buffer_mode( operand[0] );
break;
case 0x33:
z_output_stream( operand[0], operand[1] );
break;
case 0x34:
z_input_stream( operand[0] );
break;
case 0x35:
sound( count, operand );
break;
case 0x36:
z_read_char( count, operand );
break;
case 0x37:
z_scan_table( count, operand );
break;
case 0x38:
z_not( operand[0] );
break;
case 0x39:
z_call( count, operand, PROCEDURE );
break;
case 0x3a:
z_call( count, operand, PROCEDURE );
break;
case 0x3b:
z_tokenise( count, operand );
break;
case 0x3c:
z_encode( operand[0], operand[1], operand[2], operand[3] );
break;
case 0x3d:
z_copy_table( operand[0], operand[1], operand[2] );
break;
case 0x3e:
z_print_table( count, operand );
break;
case 0x3f:
z_check_arg_count( operand[0] );
break;
default:
fatal( "interpret(): Illegal 2 or more operand instruction" );
}
}
}
else
{
/* Single operand class, load operand and execute instruction */
if ( opcode < 0xb0 )
{
operand[0] = load_operand( ( opcode >> 4 ) & 0x03 );
#ifdef DEBUG_TERPRE
fprintf( stderr, "PC = 0x%08lx Op%s = 0x%02x %d\n", pc, "(1 )", opcode,
operand[0] );
#endif
switch ( ( char ) opcode & 0x0f )
{
case 0x00:
z_jz( operand[0] );
break;
case 0x01:
z_get_sibling( operand[0] );
break;
case 0x02:
z_get_child( operand[0] );
break;
case 0x03:
z_get_parent( operand[0] );
break;
case 0x04:
z_get_prop_len( operand[0] );
break;
case 0x05:
z_inc( operand[0] );
break;
case 0x06:
z_dec( operand[0] );
break;
case 0x07:
z_print_addr( operand[0] );
break;
case 0x08:
z_call( 1, operand, FUNCTION );
break;
case 0x09:
z_remove_obj( operand[0] );
break;
case 0x0a:
z_print_obj( operand[0] );
break;
case 0x0b:
z_ret( operand[0] );
break;
case 0x0c:
z_jump( operand[0] );
break;
case 0x0d:
z_print_paddr( operand[0] );
break;
case 0x0e:
z_load( operand[0] );
break;
case 0x0f:
if ( h_type > V4 )
z_call( 1, operand, PROCEDURE );
else
z_not( operand[0] );
break;
}
}
else
{
/* Zero operand class, execute instruction */
#ifdef DEBUG_TERPRE
fprintf( stderr, "PC = 0x%08lx Op%s = 0x%02x\n", pc, "(0 )", opcode );
#endif
switch ( ( char ) opcode & 0x0f )
{
case 0x00:
z_ret( TRUE );
break;
case 0x01:
z_ret( FALSE );
break;
case 0x02:
z_print( );
break;
case 0x03:
z_print_ret( );
break;
case 0x04:
/* z_nop */
break;
case 0x05:
z_save( 0, 0, 0, 0 );
break;
case 0x06:
z_restore( 0, 0, 0, 0 );
break;
case 0x07:
z_restart( );
break;
case 0x08:
z_ret( stack[sp++] );
break;
case 0x09:
z_catch( );
break;
case 0x0a:
halt = TRUE; /* z_quit */
break;
case 0x0b:
z_new_line( );
break;
case 0x0c:
z_show_status( );
break;
case 0x0d:
z_verify( );
break;
case 0x0f:
z_piracy( TRUE );
break;
default:
fatal( "interpret(): Illegal zero operand instruction" );
}
}
}
}
return ( interpreter_status );
} /* interpret */