class Instruction

Instruction is an instruction shape that we're going to match to Commands to find out what we should write in to memory.

Attributes

machine[RW]
opcode[RW]
parameters[RW]

Public Class Methods

new(message, machine) click to toggle source
# File lib/wilson.rb, line 457
def initialize message, machine
  self.machine = machine
  self.opcode, *self.parameters = message
  self.opcode = opcode.to_s.upcase

  self.machine = parameters[1].machine unless machine

  self.parameters.map! { |each| Proc === each ? each.call.m : each }

  self.parameters.each do |each|
    each.machine = self.machine if each.operand?
  end
end
on_message(machine, message) click to toggle source
# File lib/wilson.rb, line 453
def self.on_message machine, message # TODO: remove
  self.new message, machine
end

Public Instance Methods

assemble() click to toggle source
# File lib/wilson.rb, line 483
def assemble
  instructions = machine.instructions.select { |command|
    command.instruction_applies? self
  }

  return false if instructions.empty?

  bytes = instructions.map { |instruction| instruction.assemble self }

  sorted_bytes = bytes.sort_by {|byte| [byte.size, (byte[0]||0), (byte[1]||0)]}

  machine.stream.push(*sorted_bytes.first)

  true
end
first() click to toggle source
# File lib/wilson.rb, line 471
def first
  parameters.first
end
second() click to toggle source
# File lib/wilson.rb, line 475
def second
  parameters.second
end
theAddress() click to toggle source
# File lib/wilson.rb, line 479
def theAddress
  parameters.detect { |e| e.address? }
end
theImmediate() click to toggle source
# File lib/wilson.rb, line 503
def theImmediate
  parameters.reverse.detect { |e| e.immediate_value? }
end
theSecondImmediate() click to toggle source
# File lib/wilson.rb, line 499
def theSecondImmediate
  parameters.detect { |e| e.immediate_value? }
end