Class: Wilson::Instruction

Inherits:
Object show all
Defined in:
lib/wilson.rb

Overview

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, machine) ⇒ Instruction

Returns a new instance of Instruction.



530
531
532
533
534
535
536
537
538
539
540
541
542
# File 'lib/wilson.rb', line 530

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

Instance Attribute Details

#machineObject

Returns the value of attribute machine.



524
525
526
# File 'lib/wilson.rb', line 524

def machine
  @machine
end

#opcodeObject

Returns the value of attribute opcode.



524
525
526
# File 'lib/wilson.rb', line 524

def opcode
  @opcode
end

#parametersObject

Returns the value of attribute parameters.



524
525
526
# File 'lib/wilson.rb', line 524

def parameters
  @parameters
end

Class Method Details

.on_message(machine, message) ⇒ Object

TODO: remove



526
527
528
# File 'lib/wilson.rb', line 526

def self.on_message machine, message # TODO: remove
  self.new message, machine
end

Instance Method Details

#assembleObject



556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
# File 'lib/wilson.rb', line 556

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

#firstObject



544
545
546
# File 'lib/wilson.rb', line 544

def first
  parameters.first
end

#secondObject



548
549
550
# File 'lib/wilson.rb', line 548

def second
  parameters.second
end

#theAddressObject



552
553
554
# File 'lib/wilson.rb', line 552

def theAddress
  parameters.detect { |e| e.address? }
end

#theImmediateObject



576
577
578
# File 'lib/wilson.rb', line 576

def theImmediate
  parameters.reverse.detect { |e| e.immediate_value? }
end

#theSecondImmediateObject



572
573
574
# File 'lib/wilson.rb', line 572

def theSecondImmediate
  parameters.detect { |e| e.immediate_value? }
end