Class: Haxor::Vm::Cpu::Core

Inherits:
Subsystem show all
Defined in:
lib/haxor/vm/cpu/core.rb

Instance Attribute Summary collapse

Attributes inherited from Subsystem

#vm

Instance Method Summary collapse

Methods inherited from Subsystem

#register, #subsystem

Constructor Details

#initialize ⇒ Core

Returns a new instance of Core.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/haxor/vm/cpu/core.rb', line 8

def initialize
  @units = []
  @opcodes = {}

  register_unit Unit::Arithmetic.new
  register_unit Unit::Jumps.new
  register_unit Unit::Logical.new
  register_unit Unit::Transfer.new
  register_unit Unit::Various.new

  build_struct
end

Instance Attribute Details

#labels ⇒ Object (readonly)

Returns the value of attribute labels.



6
7
8
# File 'lib/haxor/vm/cpu/core.rb', line 6

def labels
  @labels
end

#struct ⇒ Object (readonly)

Returns the value of attribute struct.



5
6
7
# File 'lib/haxor/vm/cpu/core.rb', line 5

def struct
  @struct
end

Instance Method Details

#bind_opcode(opcode, object, method) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/haxor/vm/cpu/core.rb', line 21

def bind_opcode(opcode, object, method)
  fail "OpCode already defined - #{opcode}." if @opcodes.key? opcode
  @opcodes[opcode] = {
    object: object,
    method: method
  }
end

#iterate ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/haxor/vm/cpu/core.rb', line 29

def iterate
  opcode = subsystem(:mem).next_cell
  cmd = opcode & Consts::OPCODE_CMD_MASK
  subsystem(:mem).write 'op', opcode

  fail "Call to not existing opcode #{cmd}." unless @opcodes.key? cmd
  #        puts 'cmd = ' + cmd.to_s(16)
  #        puts flg.to_s(16)
  @opcodes[cmd][:object].send(@opcodes[cmd][:method])
end

#operand ⇒ Object



50
51
52
# File 'lib/haxor/vm/cpu/core.rb', line 50

def operand
  operands(1)[0]
end

#operands(n = 2) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/haxor/vm/cpu/core.rb', line 54

def operands(n = 2)
  flg = subsystem(:mem).read('op') & Consts::OPCODE_FLG_MASK
  flg >> Consts::OPCODE_FLG_OFFSET

  result = []
  n.times do
    v = subsystem(:mem).next_cell
    v = subsystem(:mem).read a if (flg & Consts::OPERAND_DEREFERENCE != 0)
    result << v
    flg << Consts::OPERAND_FLAGS
  end

  result
end

#register_unit(unit) ⇒ Object



44
45
46
47
48
# File 'lib/haxor/vm/cpu/core.rb', line 44

def register_unit(unit)
  @units << unit
  unit.vm = self
  unit.register
end

#reserved_mem ⇒ Object



40
41
42
# File 'lib/haxor/vm/cpu/core.rb', line 40

def reserved_mem
  1024
end