forked from njm64/zawk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
op.awk
352 lines (339 loc) · 8.59 KB
/
op.awk
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
# Decode the next instruction into the following global variables:
# op_pc: Integer Starting address of this instruction in memory
# op_type: String ("0OP", "1OP", "2OP", or "VAR")
# op_code: Integer opcode
# A0,A1,A2,A3: Integer operands (-1 means unpopulated)
function op_decode( b, t) {
op_pc = cpu_pc
b = fetch_u8()
# Initial operand values
A0 = A1 = A2 = A3 = -1
if(b == 190 && hdr_version == 5) {
# Extended instruction (v5)
} else if(!test_bit(b, 7)) {
# Long instruction (2OP)
op_type = "2OP"
op_code = b % 32
A0 = op_decode_arg(test_bit(b, 6) + 1)
A1 = op_decode_arg(test_bit(b, 5) + 1)
} else if(test_bit(b, 6)) {
# Variable instruction (2OP or VAR arity)
op_type = test_bit(b, 5) ? "VAR" : "2OP"
op_code = b % 32
b = fetch_u8()
if((t = int(b / 64)) != 3) {
A0 = op_decode_arg(t)
if((t = int(b / 16) % 4) != 3) {
A1 = op_decode_arg(t)
if((t = int(b / 4) % 4) != 3) {
A2 = op_decode_arg(t)
if((t = int(b % 4)) != 3) {
A3 = op_decode_arg(t)
}
}
}
}
} else {
# Short instruction (0OP or 1OP)
op_code = b % 16
A0 = op_decode_arg(int(b / 16) % 4)
op_type = A0 < 0 ? "0OP" : "1OP"
}
}
function op_decode_arg(type) {
if(type == 0) {
return fetch_u16();
} else if(type == 1) {
return fetch_u8();
} else if(type == 2) {
return var_get(fetch_u8());
} else {
# Return -1 to indicate this arg is unpopulated
return -1;
}
}
function op_dispatch() {
if(debug) {
op_print()
}
if(op_type == "0OP") {
op_dispatch_0op()
} else if(op_type == "1OP") {
op_dispatch_1op()
} else if(op_type == "2OP") {
op_dispatch_2op()
} else if(op_type == "VAR") {
op_dispatch_var()
}
}
function op_unknown() {
printf("Unknown opcode: ")
op_print()
cpu_break = 1
}
function op_print() {
printf("%04X: %s %d (", op_pc, op_type, op_code)
if(A0 >= 0) {
printf("%d", A0)
if(A1 >= 0) {
printf(" %d", A1)
if(A2 >= 0) {
printf(" %d", A2)
if(A3 >= 0) {
printf(" %d", A3)
}
}
}
}
printf(")\n")
}
function op_dispatch_0op() {
if(op_code == 0) {
# true
cpu_ret(1)
} else if(op_code == 1) {
# false
cpu_ret(0)
} else if(op_code == 2) {
# print
cpu_pc = txt_print(cpu_pc)
} else if(op_code == 3) {
# print-ret
cpu_pc = txt_print(cpu_pc)
printf("\n")
cpu_ret(1)
} else if(op_code == 4) {
# nop
} else if(op_code == 5) {
# save
cpu_branch(save_game())
} else if(op_code == 6) {
# restore
cpu_branch(restore_game())
} else if(op_code == 7) {
# restart
restart_game()
} else if(op_code == 8) {
# ret-popped
cpu_ret(stack_pop())
} else if(op_code == 9) {
# pop
stack_pop()
} else if(op_code == 10) {
# quit
cpu_break = 1
} else if(op_code == 11) {
# new-line
printf("\n")
} else if(op_code == 13) {
# verify
cpu_branch(1)
} else {
op_unknown()
}
}
function op_dispatch_1op( r, t) {
if(op_code == 0) {
# jz
cpu_branch(A0 == 0)
} else if(op_code == 1) {
# get_sibling
r = fetch_u8()
t = obj_sibling(A0)
cpu_branch(t > 0)
var_set(r, t)
} else if(op_code == 2) {
# get_child
r = fetch_u8()
t = obj_child(A0)
cpu_branch(t > 0)
var_set(r, t)
} else if(op_code == 3) {
# get_parent
r = fetch_u8()
t = obj_parent(A0)
var_set(r, t)
} else if(op_code == 4) {
# get_prop_len
r = fetch_u8()
t = obj_addr_prop_len(A0)
var_set(r, t)
} else if(op_code == 5) {
# inc
var_set(A0, var_get_signed(A0) + 1)
} else if(op_code == 6) {
# dec
var_set(A0, var_get_signed(A0) - 1)
} else if(op_code == 7) {
# print_addr
txt_print(A0)
} else if(op_code == 9) {
# remove_obj
obj_remove(A0)
} else if(op_code == 10) {
# print_obj
txt_print(obj_name_addr(A0))
} else if(op_code == 11) {
# ret
cpu_ret(A0)
} else if(op_code == 12) {
# jump
cpu_pc += (to_s16(A0) - 2)
} else if(op_code == 13) {
# print_paddr
txt_print(A0 * 2)
} else if(op_code == 14) {
# load
r = fetch_u8()
if(A0 == 0) {
stack_push(stack_top()) # 6.3.4
}
t = var_get(A0)
var_set(r, t)
} else if(op_code == 15) {
# not
var_set(fetch_u8(), lognot(A0))
} else {
op_unknown()
}
}
function op_dispatch_2op( t) {
if(op_code == 1) {
# je
cpu_branch(A0 == A1 || A0 == A2 || A0 == A3)
} else if(op_code == 2) {
# jl
cpu_branch(to_s16(A0) < to_s16(A1))
} else if(op_code == 3) {
# jg
cpu_branch(to_s16(A0) > to_s16(A1))
} else if(op_code == 4) {
# dec_chk
t = var_get_signed(A0) - 1
var_set(A0, t)
cpu_branch(t < to_s16(A1))
} else if(op_code == 5) {
# inc_chk
t = var_get_signed(A0) + 1
var_set(A0, t)
cpu_branch(t > to_s16(A1))
} else if(op_code == 6) {
# jin
cpu_branch(obj_parent(A0) == A1)
} else if(op_code == 7) {
# test
cpu_branch(logand(A0, A1) == A1)
} else if(op_code == 8) {
# or
var_set(fetch_u8(), logior(A0, A1))
} else if(op_code == 9) {
# and
var_set(fetch_u8(), logand(A0, A1))
} else if(op_code == 10) {
# test_attr
cpu_branch(obj_attr(A0, A1))
} else if(op_code == 11) {
# set_attr
obj_set_attr(A0, A1)
} else if(op_code == 12) {
# clear_attr
obj_clear_attr(A0, A1)
} else if(op_code == 13) {
# store
if(A0 == 0) {
stack_pop() # 6.3.4
}
var_set(A0, A1)
} else if(op_code == 14) {
# insert_obj
obj_insert(A0, A1)
} else if(op_code == 15) {
# loadw
r = fetch_u8()
t = mem_read_u16(A0 + 2 * A1)
var_set(r, t)
} else if(op_code == 16) {
# loadb
r = fetch_u8()
t = mem_read_u8(A0 + A1)
var_set(r, t)
} else if(op_code == 17) {
# get_prop
r = fetch_u8()
t = obj_prop(A0, A1)
var_set(r, t)
} else if(op_code == 18) {
# get_prop_addr
r = fetch_u8()
t = obj_prop_addr(A0, A1)
var_set(r, t)
} else if(op_code == 19) {
# get_next_prop
r = fetch_u8()
if(A1 == 0) {
t = obj_first_prop(A0)
} else {
t = obj_next_prop(A0, A1)
}
var_set(r, t)
} else if(op_code == 20) {
# add
var_set(fetch_u8(), A0 + A1)
} else if(op_code == 21) {
# sub
var_set(fetch_u8(), A0 - A1)
} else if(op_code == 22) {
# mul
var_set(fetch_u8(), A0 * A1)
} else if(op_code == 23) {
# div
var_set(fetch_u8(), int(to_s16(A0) / to_s16(A1)))
} else if(op_code == 24) {
# mod
var_set(fetch_u8(), int(to_s16(A0) % to_s16(A1)))
} else {
op_unknown()
}
}
function op_dispatch_var( t, n) {
if(op_code == 0) {
# call
cpu_call(A0, A1, A2, A3)
} else if(op_code == 1) {
# storew
mem_write_u16(A0 + 2 * A1, A2)
} else if(op_code == 2) {
# storeb
mem_write_u8(A0 + A1, A2)
} else if(op_code == 3) {
# put_prop
obj_set_prop(A0, A1, A2)
} else if(op_code == 4) {
# read
tok_read(A0, A1)
} else if(op_code == 5) {
# print_char
printf("%c", A0)
} else if(op_code == 6) {
# print_num
printf("%d", to_s16(A0))
} else if(op_code == 7) {
# random
r = fetch_u8()
n = to_s16(A0)
t = n > 0 ? rnd_get(n) : rnd_set_seed(-n)
var_set(r, t)
} else if(op_code == 8) {
# push
stack_push(A0)
} else if(op_code == 9) {
# pull
t = stack_pop()
if(A0 == 0) {
stack_pop() # 6.3.3
}
var_set(A0, t)
} else {
op_unknown()
}
}