Class: Gruesome::Z::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/gruesome/z/processor.rb

Instance Method Summary collapse

Constructor Details

#initialize(memory) ⇒ Processor

Returns a new instance of Processor.



14
15
16
17
18
19
20
# File 'lib/gruesome/z/processor.rb', line 14

def initialize(memory)
  @memory = memory
  @header = Header.new(@memory.contents)
  @abbreviation_table = AbbreviationTable.new(@memory)
  @object_table = ObjectTable.new(@memory)
  @dictionary = Dictionary.new(@memory)
end

Instance Method Details

#branch(branch_to, branch_on, result) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/gruesome/z/processor.rb', line 70

def branch(branch_to, branch_on, result)
  if (result == branch_on)
    if branch_to == 0
      routine_return(0)
    elsif branch_to == 1
      routine_return(1)
    else
      @memory.program_counter = branch_to
#            @memory.program_counter -= 2
    end
  end
end

#execute(instruction) ⇒ Object



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)
  # Pull values from the variables if needed by the instruction
  # there are some exceptions for variable-by-reference instructions
  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]))

      # make sure to re-push the value since LOAD does not
      # change the stack but uses the value directly
      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
    # get rid of the first item on stack
    @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
      # TODO: Version 6 PULL instruction
      #
      # stack to pull from is given as operand[0]
      # instruction.destination is the destination resister
    else
      # pop value from stack
      @memory.writev(operands[0], @memory.readv(0))
    end
  when Opcode::PUSH
    # add value to stack
    @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
      # seed with value
      srand(value)
      result = 0
    elsif value == 0
      # seed with timestamp
      srand()
      result = 0
    else
      # pull random number from between 1 and value
      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
    # read the maximum number of bytes in the text-buffer
    max_bytes = @memory.force_readb(operands[0])

    # address of the next byte in the text-buffer
    addr = operands[0] + 1

    # read in a line of input from stdin
    line = $stdin.readline[0..-2]

    # truncate line to fit the max characters given by text-buffer
    if line.length > max_bytes
      line = line[0..max_bytes]
    end

    # tokenize
    lexed = @dictionary.tokenize(line)

    # parse
    parsed = @dictionary.parse(lexed)

    # encode the line into a ZChar stream
    str = ZSCII.encode_to_zchars(line, @header.version)

    # write the text to the text-buffer
    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
      # terminator
      if num_bytes <= max_bytes
        @memory.force_writeb(addr, 0)
      end
    end

    # write the parse-buffer
    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

#routine_call(address, arguments, result_variable = nil) ⇒ Object



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
# File 'lib/gruesome/z/processor.rb', line 22

def routine_call(address, arguments, result_variable = nil)
  if address == 0
    # special case, do not call, simply return false
    if result_variable != nil
      @memory.writev(result_variable, 0)
    end
  else
    return_addr = @memory.program_counter
    @memory.program_counter = address

    # read routine
    num_locals = @memory.force_readb(@memory.program_counter)
    @memory.program_counter += 1

    # create environment
    @memory.push_routine(return_addr, num_locals, result_variable)

    if @header.version <= 4
      # read initial values when version 1-4
      (1..num_locals).each do |i|
        @memory.writev(i, @memory.force_readw(@memory.program_counter))
        @memory.program_counter += 2
      end
    else
      # reset local vars to 0
      (1..num_locals).each do |i|
        @memory.writev(i, 0)
      end
    end

    # copy arguments over into locals
    idx = 1
    arguments.each do |i|
      @memory.writev(idx, i)
      idx += 1
    end
  end
end

#routine_return(result) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/gruesome/z/processor.rb', line 61

def routine_return(result)
  frame = @memory.pop_routine

  if frame[:destination] != nil
    @memory.writev(frame[:destination], result)
  end
  @memory.program_counter = frame[:return_address]
end