Class: Gruesome::Z::Machine

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

Overview

The class that initializes and maintains a Z-Machine

Instance Method Summary collapse

Constructor Details

#initialize(game_file) ⇒ Machine

Will create a new virtual machine for the game file



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

def initialize(game_file)
  file = File.open(game_file, "rb")

  # I. Create memory space

  memory_size = File.size(game_file)
  save_file_name = File.basename(file, File.extname(file)) + ".sav"
  @memory = Memory.new(file.read(memory_size), save_file_name)

  # Set flags
  flags = @memory.force_readb(0x01)
  flags &= ~(0b1110000)
  @memory.force_writeb(0x01, flags)

  # Set flags 2
  flags = @memory.force_readb(0x10)
  flags &= ~(0b11111100)
  @memory.force_writeb(0x10, flags)

  # II. Read header (at address 0x0000) and associated tables
  @header = Header.new(@memory.contents)
  @object_table = ObjectTable.new(@memory)

  # III. Instantiate CPU
  @decoder = Decoder.new(@memory)
  @processor = Processor.new(@memory)
end

Instance Method Details

#executeObject



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

def execute
  while true do
    i = @decoder.fetch
#          var = @memory.readv(0)
#          if var != nil
#            puts "var %00 = " + sprintf("%04x", var)
#            @memory.writev(0, var)
#          end
#          var = @memory.readv(1)
#          if var != nil
#            puts "var %01 = " + sprintf("%04x", @memory.readv(0x01))
#          end
#          puts "at $" + sprintf("%04x", @memory.program_counter) + ": " + i.to_s(@header.version)
    @memory.program_counter += i.length

    if i.opcode == Opcode::QUIT
      break
    end

    begin
      @processor.execute(i)
    rescue RuntimeError => fuh
      puts "error at $" + sprintf("%04x", @memory.program_counter) + ": " + i.to_s(@header.version)
    end
  end
end