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.



544
545
546
547
548
549
550
551
552
553
554
555
556
# File 'lib/wilson.rb', line 544

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.



538
539
540
# File 'lib/wilson.rb', line 538

def machine
  @machine
end

#opcodeObject

Returns the value of attribute opcode.



538
539
540
# File 'lib/wilson.rb', line 538

def opcode
  @opcode
end

#parametersObject

Returns the value of attribute parameters.



538
539
540
# File 'lib/wilson.rb', line 538

def parameters
  @parameters
end

Class Method Details

.on_message(machine, message) ⇒ Object

TODO: remove



540
541
542
# File 'lib/wilson.rb', line 540

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

Instance Method Details

#assembleObject



570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
# File 'lib/wilson.rb', line 570

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



558
559
560
# File 'lib/wilson.rb', line 558

def first
  parameters.first
end

#secondObject



562
563
564
# File 'lib/wilson.rb', line 562

def second
  parameters.second
end

#theAddressObject



566
567
568
# File 'lib/wilson.rb', line 566

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

#theImmediateObject



590
591
592
# File 'lib/wilson.rb', line 590

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

#theSecondImmediateObject



586
587
588
# File 'lib/wilson.rb', line 586

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