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
|
# File 'lib/gruesome/z/processor.rb', line 83
def execute(instruction)
operands = instruction.operands.each_with_index.map do |operand, idx|
if idx == 0 and Opcode.is_variable_by_reference?(instruction.opcode, @header.version)
operand
elsif instruction.types[idx] == OperandType::VARIABLE
@memory.readv(operand)
else
operand
end
end
case instruction.opcode
when Opcode::ART_SHIFT
places = unsigned_to_signed(operands[1])
if places < 0
@memory.writev(instruction.destination, unsigned_to_signed(operands[0]) >> places.abs)
else
@memory.writev(instruction.destination, operands[0] << places)
end
when Opcode::CALL, Opcode::CALL_1N
routine_call(@memory.packed_address_to_byte_address(operands[0]), operands[1..-1], instruction.destination)
when Opcode::CLEAR_ATTR
@object_table.object_clear_attribute(operands[0], operands[1])
when Opcode::DEC
@memory.writev(operands[0], unsigned_to_signed(@memory.readv(operands[0])) - 1)
when Opcode::INC
@memory.writev(operands[0], unsigned_to_signed(@memory.readv(operands[0])) + 1)
when Opcode::INC_CHK
new_value = unsigned_to_signed(@memory.readv(operands[0])) + 1
@memory.writev(operands[0], new_value)
result = new_value > unsigned_to_signed(operands[1])
branch(instruction.branch_to, instruction.branch_on, result)
when Opcode::DEC_CHK
new_value = unsigned_to_signed(@memory.readv(operands[0])) - 1
@memory.writev(operands[0], new_value)
result = new_value < unsigned_to_signed(operands[1])
branch(instruction.branch_to, instruction.branch_on, result)
when Opcode::INSERT_OBJ
@object_table.object_insert_object(operands[1], operands[0])
when Opcode::GET_CHILD
child = @object_table.object_get_child(operands[0])
@memory.writev(instruction.destination, child)
result = child != 0
branch(instruction.branch_to, instruction.branch_on, result)
when Opcode::GET_NEXT_PROP
next_prop = @object_table.object_get_next_property(operands[0], operands[1])
@memory.writev(instruction.destination, next_prop)
when Opcode::GET_PARENT
parent = @object_table.object_get_parent(operands[0])
@memory.writev(instruction.destination, parent)
when Opcode::GET_PROP
prop = @object_table.object_get_property_word(operands[0], operands[1])
@memory.writev(instruction.destination, prop)
when Opcode::GET_PROP_ADDR
prop = @object_table.object_get_property_addr(operands[0], operands[1])
@memory.writev(instruction.destination, prop)
when Opcode::GET_PROP_LEN
if operands[0] == 0
result = 0
else
result = @object_table.object_get_property_data_size_from_address(operands[0])
end
@memory.writev(instruction.destination, result)
when Opcode::GET_SIBLING
sibling = @object_table.object_get_sibling(operands[0])
@memory.writev(instruction.destination, sibling)
result = sibling != 0
branch(instruction.branch_to, instruction.branch_on, result)
when Opcode::JUMP, Opcode::PIRACY
@memory.program_counter += unsigned_to_signed(operands[0])
@memory.program_counter -= 2
when Opcode::JE
result = operands[1..-1].inject(false) { |result, element|
result | (operands[0] == element)
}
branch(instruction.branch_to, instruction.branch_on, result)
when Opcode::JG
result = operands[1..-1].inject(false) { |result, element|
result | (unsigned_to_signed(operands[0]) > unsigned_to_signed(element))
}
branch(instruction.branch_to, instruction.branch_on, result)
when Opcode::JIN
result = @object_table.object_get_parent(operands[0]) == operands[1]
branch(instruction.branch_to, instruction.branch_on, result)
when Opcode::JL
result = operands[1..-1].inject(false) { |result, element|
result | (unsigned_to_signed(operands[0]) < unsigned_to_signed(element))
}
branch(instruction.branch_to, instruction.branch_on, result)
when Opcode::JZ
result = operands[0] == 0
branch(instruction.branch_to, instruction.branch_on, result)
when Opcode::LOAD
if operands[0] != instruction.destination
@memory.writev(instruction.destination, @memory.readv(operands[0]))
if operands[0] == 0
@memory.writev(0, @memory.readv(instruction.destination))
end
end
when Opcode::LOADB
@memory.writev(instruction.destination, @memory.readb(operands[0] + unsigned_to_signed(operands[1])))
when Opcode::LOADW
@memory.writev(instruction.destination, @memory.readw(operands[0] + unsigned_to_signed(operands[1])*2))
when Opcode::LOG_SHIFT
places = unsigned_to_signed(operands[1])
if places < 0
@memory.writev(instruction.destination, operands[0] >> places.abs)
else
@memory.writev(instruction.destination, operands[0] << places)
end
when Opcode::NOP
when Opcode::NEW_LINE
puts
when Opcode::POP
@memory.readv(0)
when Opcode::PRINT
print operands[0]
when Opcode::PRINT_ADDR
print ZSCII.translate(0, @header.version, @memory.force_readzstr(operands[0])[1], @abbreviation_table)
when Opcode::PRINT_CHAR
print ZSCII.translate_Zchar(operands[0])
when Opcode::PRINT_NUM
print unsigned_to_signed(operands[0]).to_s
when Opcode::PRINT_OBJ
print @object_table.object_short_text(operands[0])
when Opcode::PRINT_PADDR
str_addr = @memory.packed_address_to_byte_address(operands[0])
print ZSCII.translate(0, @header.version, @memory.force_readzstr(str_addr)[1], @abbreviation_table)
when Opcode::PRINT_RET
puts operands[0]
routine_return(1)
when Opcode::PULL
if @header.version == 6
else
@memory.writev(operands[0], @memory.readv(0))
end
when Opcode::PUSH
@memory.writev(0, operands[0])
when Opcode::PUT_PROP
object_id = operands[0]
prop_id = operands[1]
prop_value = operands[2]
@object_table.object_set_property_word(object_id, prop_id, prop_value)
when Opcode::RANDOM
value = unsigned_to_signed(operands[0])
if value < 0
srand(value)
result = 0
elsif value == 0
srand()
result = 0
else
result = rand(value-1) + 1
end
@memory.writev(instruction.destination, result)
when Opcode::REMOVE_OBJ
@object_table.object_remove_object(operands[0])
when Opcode::RESTORE
if @header.version <= 4
@memory.restore
if @header.version >= 4
@memory.writev(instruction.destination, 1)
end
branch(instruction.branch_to, instruction.branch_on, true)
end
when Opcode::RET
routine_return(operands[0])
when Opcode::RET_POPPED
routine_return(@memory.readv(0))
when Opcode::RTRUE
routine_return(1)
when Opcode::RFALSE
routine_return(0)
when Opcode::SAVE
if @header.version <= 4
@memory.save
if @header.version >= 4
@memory.writev(instruction.destination, 1)
end
branch(instruction.branch_to, instruction.branch_on, true)
end
when Opcode::SET_ATTR
@object_table.object_set_attribute(operands[0], operands[1])
when Opcode::SREAD
max_bytes = @memory.force_readb(operands[0])
addr = operands[0] + 1
line = $stdin.readline[0..-2]
if line.length > max_bytes
line = line[0..max_bytes]
end
lexed = @dictionary.tokenize(line)
parsed = @dictionary.parse(lexed)
str = ZSCII.encode_to_zchars(line, @header.version)
num_bytes = 1
first_position = 1
if @header.version >= 5
num_bytes += 1
@memory.force_writeb(addr, line.length)
first_position = 2
addr += 1
end
str.each do |zchr|
num_bytes += 1
if num_bytes > max_bytes
break
end
@memory.force_writeb(addr, zchr)
addr += 1
end
if @header.version < 5
if num_bytes <= max_bytes
@memory.force_writeb(addr, 0)
end
end
max_bytes = @memory.force_readb(operands[1])
max_bytes = 2 + max_bytes * 4
addr = operands[1] + 1
num_bytes = 1
if num_bytes <= max_bytes
@memory.force_writeb(addr, lexed.length)
addr += 1
end
parsed.each do |token, index|
num_bytes += 4
if num_bytes > max_bytes
break
end
@memory.force_writew(addr, token[:address])
addr += 2
@memory.force_writeb(addr, token[:size])
addr += 1
@memory.force_writeb(addr, token[:position]+first_position)
addr += 1
end
when Opcode::TEST
result = (operands[0] & operands[1]) == operands[1]
branch(instruction.branch_to, instruction.branch_on, result)
when Opcode::TEST_ATTR
result = @object_table.object_has_attribute?(operands[0], operands[1])
branch(instruction.branch_to, instruction.branch_on, result)
when Opcode::ADD
@memory.writev(instruction.destination,
unsigned_to_signed(operands[0]) + unsigned_to_signed(operands[1]))
when Opcode::SUB
@memory.writev(instruction.destination,
unsigned_to_signed(operands[0]) - unsigned_to_signed(operands[1]))
when Opcode::MUL
@memory.writev(instruction.destination,
unsigned_to_signed(operands[0]) * unsigned_to_signed(operands[1]))
when Opcode::DIV
result = unsigned_to_signed(operands[0]).to_f / unsigned_to_signed(operands[1]).to_f
if result < 0
result = -(result.abs.floor)
else
result = result.floor
end
@memory.writev(instruction.destination, result.to_i)
when Opcode::MOD
a = unsigned_to_signed(operands[0])
b = unsigned_to_signed(operands[1])
result = a.abs % b.abs
if a < 0
result = -result
end
@memory.writev(instruction.destination, result.to_i)
when Opcode::NOT
@memory.writev(instruction.destination, ~(operands[0]))
when Opcode::OR
@memory.writev(instruction.destination, operands[0] | operands[1])
when Opcode::AND
@memory.writev(instruction.destination, operands[0] & operands[1])
when Opcode::STORE
if operands[0] == 0
@memory.readv(operands[0])
end
@memory.writev(operands[0], operands[1])
when Opcode::STOREB
@memory.writeb(operands[0] + unsigned_to_signed(operands[1]), operands[2])
when Opcode::STOREW
@memory.writew(operands[0] + unsigned_to_signed(operands[1])*2, operands[2])
else
puts instruction.opcode
raise "opcode " + Opcode.name(instruction.opcode, @header.version) + " not implemented"
end
end
|