Class: GBRb::InstructionSet::Swap

Inherits:
Instruction show all
Defined in:
lib/gbrb/instruction_set/swap.rb

Instance Attribute Summary

Attributes inherited from Instruction

#i, #m, #t

Instance Method Summary collapse

Methods inherited from Instruction

#carry?, #immediate_count

Constructor Details

#initialize(target, m = 2, t = 8, indirect = false) ⇒ Swap

Returns a new instance of Swap.



6
7
8
9
10
# File 'lib/gbrb/instruction_set/swap.rb', line 6

def initialize target, m=2, t=8, indirect=false
  @target = target
  @indirect = indirect
  super m, t
end

Instance Method Details

#call(r, mem) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/gbrb/instruction_set/swap.rb', line 12

def call r, mem
  initial = r.public_send(@target).read
  initial = mem.read_byte(initial) if @indirect
  high = initial >> 4
  low = initial & ((1 << 4) - 1)

  result = (low << 4) + high

  if @indirect
    mem.write_byte r.public_send(@target).read, result
    mem.read_byte(r.public_send(@target).read) == 0 ? r.set_zero_flag : r.clear_zero_flag
  else
    r.public_send(@target).store result
    r.public_send(@target).read == 0 ? r.set_zero_flag : r.clear_zero_flag
  end
  r.clear_add_sub_flag
  r.clear_half_carry_flag
  r.clear_carry_flag
end