Class: AVR::Instruction

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/avr/instruction.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cpu, mnemonic, args) ⇒ Instruction

Returns a new instance of Instruction.



21
22
23
24
25
26
27
28
# File 'lib/avr/instruction.rb', line 21

def initialize(cpu, mnemonic, args)
  raise "Unknown opcode #{mnemonic}" unless Opcode.opcodes.include?(mnemonic)

  @cpu = cpu
  @mnemonic = mnemonic
  @args = args
  @opcode = T.let(Opcode.opcodes.fetch(mnemonic), Opcode)
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



15
16
17
# File 'lib/avr/instruction.rb', line 15

def args
  @args
end

#cpuObject (readonly)

Returns the value of attribute cpu.



9
10
11
# File 'lib/avr/instruction.rb', line 9

def cpu
  @cpu
end

#mnemonicObject (readonly)

Returns the value of attribute mnemonic.



12
13
14
# File 'lib/avr/instruction.rb', line 12

def mnemonic
  @mnemonic
end

#opcodeObject (readonly)

Returns the value of attribute opcode.



18
19
20
# File 'lib/avr/instruction.rb', line 18

def opcode
  @opcode
end

Instance Method Details

#args_to_sObject



42
43
44
45
46
47
# File 'lib/avr/instruction.rb', line 42

def args_to_s
  #return args.join(', ') unless opcode
  return nil if opcode.arg_types.empty?

  opcode.format_args(args).join(', ')
end

#executeObject



62
63
64
65
66
67
68
69
70
# File 'lib/avr/instruction.rb', line 62

def execute
  raise 'Invalid instruction' unless valid?

  cpu.next_pc = cpu.pc + 1
  opcode.execute(cpu, nil, args)
  cpu.pc = cpu.next_pc

  nil
end

#inspectObject



57
58
59
# File 'lib/avr/instruction.rb', line 57

def inspect
  "#<#{self.class.name} {#{self}}>"
end

#to_sObject



50
51
52
53
54
# File 'lib/avr/instruction.rb', line 50

def to_s
  return mnemonic.to_s if args.empty?

  "#{mnemonic} #{args_to_s}"
end

#valid?Boolean

Returns:

  • (Boolean)


36
37
38
39
# File 'lib/avr/instruction.rb', line 36

def valid?
  validate
  true
end

#validateObject



31
32
33
# File 'lib/avr/instruction.rb', line 31

def validate
  opcode.validate(args)
end