Class: Haxor::Vm::Core
- Inherits:
-
Object
- Object
- Haxor::Vm::Core
- Defined in:
- lib/haxor/vm/core.rb
Instance Attribute Summary collapse
-
#mem ⇒ Object
readonly
Returns the value of attribute mem.
Instance Method Summary collapse
- #execute ⇒ Object
-
#initialize ⇒ Core
constructor
A new instance of Core.
- #load_program(filename) ⇒ Object
- #register_subsystem(id, object) ⇒ Object
- #subsystem(id) ⇒ Object
Constructor Details
#initialize ⇒ Core
Returns a new instance of Core.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/haxor/vm/core.rb', line 6 def initialize @subsystems = {} register_subsystem :cpu, Cpu::Core.new register_subsystem :mem, Mem.new(Consts::RESERVED_MEM) register_subsystem :stack, Stack.new register_subsystem :os, Os.new register_subsystem :registers, Registers.new subsystem(:cpu).labels.each do |_, label| subsystem(:mem).add_label label.label, label.absolute_addr subsystem(:registers).add_register label.label, label.absolute_addr end @units = [] @opcodes = {} end |
Instance Attribute Details
#mem ⇒ Object (readonly)
Returns the value of attribute mem.
4 5 6 |
# File 'lib/haxor/vm/core.rb', line 4 def mem @mem end |
Instance Method Details
#execute ⇒ Object
32 33 34 35 36 |
# File 'lib/haxor/vm/core.rb', line 32 def execute loop do subsystem(:cpu).iterate end end |
#load_program(filename) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/haxor/vm/core.rb', line 38 def load_program(filename) exe = File.read(filename, encoding: 'ASCII-8BIT') @hdr = Header.new @hdr.parse! exe fail if @hdr.version != Consts::VERSION exe = exe[@hdr.size..-1] # cut off header subsystem(:mem).replace_region Consts::RESERVED_MEM, exe subsystem(:registers).write 'ip', @hdr.entry_point # instruction pointer subsystem(:mem).enlarge @hdr.bss_size subsystem(:mem).enlarge @hdr.stack_size subsystem(:registers).write 'sp', subsystem(:mem).size end |
#register_subsystem(id, object) ⇒ Object
23 24 25 26 |
# File 'lib/haxor/vm/core.rb', line 23 def register_subsystem(id, object) @subsystems[id] = object object.vm = self end |
#subsystem(id) ⇒ Object
28 29 30 |
# File 'lib/haxor/vm/core.rb', line 28 def subsystem(id) @subsystems[id] end |