File: interpre.c

package info (click to toggle)
xzip 1:1.8.2-4
  • links: PTS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, sid, stretch
  • size: 656 kB
  • ctags: 1,556
  • sloc: ansic: 11,942; makefile: 171; sh: 11
file content (223 lines) | stat: -rw-r--r-- 9,637 bytes parent folder | download
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
/*
 * interpre.c
 *
 * Main interpreter loop
 *
 */

#include "ztypes.h"

static int halt = FALSE;

/*
 * interpret
 *
 * Interpret Z code
 *
 */

#ifdef __STDC__
int interpret ()
#else
int interpret ()
#endif
{
    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)
                switch ((char) opcode) {

                    /* Extended operand instructions */

                    case 0x00: save (count, operand); break;
                    case 0x01: restore (count, operand); break;
                    case 0x02: shift (operand[0], operand[1]); break;
                    case 0x03: arith_shift (operand[0], operand[1]); break;
                    case 0x04: set_font_attribute (operand[0]); break;

                    case 0x09: undo_save (); break;
                    case 0x0a: undo_restore (); break;

                    default: fatal ("Illegal operation");
                }
            else
                switch ((char) opcode) {

                    /* Two or multiple operand instructions */

                    case 0x01: compare_je (count, operand); break;
                    case 0x02: compare_jl (operand[0], operand[1]); break;
                    case 0x03: compare_jg (operand[0], operand[1]); break;
                    case 0x04: decrement_check (operand[0], operand[1]); break;
                    case 0x05: increment_check (operand[0], operand[1]); break;
                    case 0x06: compare_parent_object (operand[0], operand[1]); break;
                    case 0x07: test (operand[0], operand[1]); break;
                    case 0x08: or (operand[0], operand[1]); break;
                    case 0x09: and (operand[0], operand[1]); break;
                    case 0x0a: test_attr (operand[0], operand[1]); break;
                    case 0x0b: set_attr (operand[0], operand[1]); break;
                    case 0x0c: clear_attr (operand[0], operand[1]); break;
                    case 0x0d: store_variable (operand[0], operand[1]); break;
                    case 0x0e: insert_object (operand[0], operand[1]); break;
                    case 0x0f: load_word (operand[0], operand[1]); break;
                    case 0x10: load_byte (operand[0], operand[1]); break;
                    case 0x11: load_property (operand[0], operand[1]); break;
                    case 0x12: load_property_address (operand[0], operand[1]); break;
                    case 0x13: load_next_property (operand[0], operand[1]); break;
                    case 0x14: add (operand[0], operand[1]); break;
                    case 0x15: subtract (operand[0], operand[1]); break;
                    case 0x16: multiply (operand[0], operand[1]); break;
                    case 0x17: divide (operand[0], operand[1]); break;
                    case 0x18: z_remainder (operand[0], operand[1]); break;
                    case 0x19: call (count, operand, FUNCTION); break;
                    case 0x1a: call (count, operand, PROCEDURE); break;
                    case 0x1b: set_colour_attribute (operand[0], operand[1]); break;
                    case 0x1c: unwind (operand[0], operand[1]); break;

                    /* Multiple operand instructions */

                    case 0x20: call (count, operand, FUNCTION); break;
                    case 0x21: store_word (operand[0], operand[1], operand[2]); break;
                    case 0x22: store_byte (operand[0], operand[1], operand[2]); break;
                    case 0x23: store_property (operand[0], operand[1], operand[2]); break;
                    case 0x24: read_line (count, operand); break;
                    case 0x25: print_character (operand[0]); break;
                    case 0x26: print_number (operand[0]); break;
                    case 0x27: ziprandom (operand[0]); break;
                    case 0x28: push_var (operand[0]); break;
                    case 0x29: pop_var (operand[0]); break;
                    case 0x2a: set_status_size (operand[0]); break;
                    case 0x2b: select_window (operand[0]); break;
                    case 0x2c: call (count, operand, FUNCTION); break;
                    case 0x2d: erase_window (operand[0]); break;
                    case 0x2e: erase_line (operand[0]); break;
                    case 0x2f: set_cursor_position (operand[0], operand[1]); break;

                    case 0x31: set_video_attribute (operand[0]); break;
                    case 0x32: set_format_mode (operand[0]); break;
                    case 0x33: set_print_modes (operand[0], operand[1]); break;
                    case 0x34: open_playback (operand[0]); break;
                    case 0x35: sound (count, operand); break;
                    case 0x36: read_character (count, operand); break;
                    case 0x37: scan_data (count, operand); break;
                    case 0x38: not (operand[0]); break;
                    case 0x39: call (count, operand, PROCEDURE); break;
                    case 0x3a: call (count, operand, PROCEDURE); break;
                    case 0x3b: tokenise (count, operand); break;
                    case 0x3c: encode (operand[0], operand[1], operand[2], operand[3]); break;
                    case 0x3d: move_data (operand[0], operand[1], operand[2]); break;
                    case 0x3e: print_window (count, operand); break;
                    case 0x3f: check_argument (operand[0]); break;

                    default: fatal ("Illegal operation");
                }
        } else {

            /* Single operand class, load operand and execute instruction */

            if (opcode < 0xb0) {
                operand[0] = load_operand ((opcode >> 4) & 0x03);
                switch ((char) opcode & 0x0f) {
                    case 0x00: compare_zero (operand[0]); break;
                    case 0x01: load_next_object (operand[0]); break;
                    case 0x02: load_child_object (operand[0]); break;
                    case 0x03: load_parent_object (operand[0]); break;
                    case 0x04: load_property_length (operand[0]); break;
                    case 0x05: increment (operand[0]); break;
                    case 0x06: decrement (operand[0]); break;
                    case 0x07: print_offset (operand[0]); break;
                    case 0x08: call (1, operand, FUNCTION); break;
                    case 0x09: remove_object (operand[0]); break;
                    case 0x0a: print_object (operand[0]); break;
                    case 0x0b: ret (operand[0]); break;
                    case 0x0c: jump (operand[0]); break;
                    case 0x0d: print_address (operand[0]); break;
                    case 0x0e: load (operand[0]); break;
                    case 0x0f:
                        if (h_type > V4)
                            call (1, operand, PROCEDURE);
                        else
                            not (operand[0]);
                        break;
                }
            } else {

                /* Zero operand class, execute instruction */

                switch ((char) opcode & 0x0f) {
                    case 0x00: ret (TRUE); break;
                    case 0x01: ret (FALSE); break;
                    case 0x02: print_literal (); break;
                    case 0x03: println_return (); break;

                    case 0x05: save (count, operand); break;
                    case 0x06: restore (count, operand); break;
                    case 0x07: restart (); break;
                    case 0x08: ret (stack[sp++]); break;
                    case 0x09: get_fp (); break;
                    case 0x0a: halt = TRUE; break;
                    case 0x0b: new_line (); break;
                    case 0x0c: display_status_line (); break;
                    case 0x0d: verify (); break;

                    case 0x0f: conditional_jump (TRUE); break;

                    default: fatal ("Illegal operation");
                }
            }
        }
    }

    return (interpreter_status);

}/* interpret */