Sha256: 2c5517f606ca653aff2aa75bc932ff8c86cccc4f298df33c6ab8faa00c8741b0

Contents?: true

Size: 1.42 KB

Versions: 2

Compression:

Stored size: 1.42 KB

Contents

module Haxor
  module Vm
    class Core
      attr_reader :mem

      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

      def register_subsystem(id, object)
        @subsystems[id] = object
        object.vm = self
      end

      def subsystem(id)
        @subsystems[id]
      end

      def execute
        loop do
          subsystem(:cpu).iterate
        end
      end

      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
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
haxor-0.3.0 lib/haxor/vm/core.rb
haxor-0.2.0 lib/haxor/vm/core.rb