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.



458
459
460
461
462
463
464
465
466
467
468
469
470
# File 'lib/wilson.rb', line 458

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.



452
453
454
# File 'lib/wilson.rb', line 452

def machine
  @machine
end

#opcodeObject

Returns the value of attribute opcode.



452
453
454
# File 'lib/wilson.rb', line 452

def opcode
  @opcode
end

#parametersObject

Returns the value of attribute parameters.



452
453
454
# File 'lib/wilson.rb', line 452

def parameters
  @parameters
end

Class Method Details

.on_message(machine, message) ⇒ Object

TODO: remove



454
455
456
# File 'lib/wilson.rb', line 454

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

Instance Method Details

#assembleObject



484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
# File 'lib/wilson.rb', line 484

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



472
473
474
# File 'lib/wilson.rb', line 472

def first
  parameters.first
end

#secondObject



476
477
478
# File 'lib/wilson.rb', line 476

def second
  parameters.second
end

#theAddressObject



480
481
482
# File 'lib/wilson.rb', line 480

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

#theImmediateObject



504
505
506
# File 'lib/wilson.rb', line 504

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

#theSecondImmediateObject



500
501
502
# File 'lib/wilson.rb', line 500

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