Class: GBRb::CPU::Arithmetic

Inherits:
Instruction show all
Defined in:
lib/gbrb/cpu/instruction.rb

Direct Known Subclasses

Add, Sub

Instance Attribute Summary

Attributes inherited from Instruction

#i, #m, #t

Instance Method Summary collapse

Methods inherited from Instruction

#immediate_count

Constructor Details

#initialize(target, m, t, indirect, immediates = 0) ⇒ Arithmetic

Returns a new instance of Arithmetic.



346
347
348
349
350
# File 'lib/gbrb/cpu/instruction.rb', line 346

def initialize target, m, t, indirect, immediates=0
  super m, t, immediates
  @target = target
  @indirect = indirect
end

Instance Method Details

#call(r, mem, right_value) ⇒ Object



356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/gbrb/cpu/instruction.rb', line 356

def call r, mem, right_value
  right_value ||= r.public_send(@target.to_sym).read
  right_value = mem.read_byte(right_value) if @indirect
  a_value = r.a.read
  if carry? a_value, right_value, r.a.half_mask
    r.set_half_carry_flag
  else
    r.clear_half_carry_flag
  end
  if carry? a_value, right_value, r.a.mask
      r.set_carry_flag
  else
    r.clear_carry_flag
  end
  a_value = a_value.public_send(@op, right_value) & 0xff
  r.a.store a_value unless @skip_store
  a_value == 0x00 ? r.set_zero_flag : r.clear_zero_flag
end

#carry?(left, right, mask) ⇒ Boolean

Returns:



352
353
354
# File 'lib/gbrb/cpu/instruction.rb', line 352

def carry? left, right, mask
  (mask-1 & left).public_send(@op, mask-1 & right) & mask == mask
end