Class: GBRb::CPU::Rr

Inherits:
Instruction show all
Defined in:
lib/gbrb/cpu/instruction.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, carry = false, indirect = false, m = 2, t = 8) ⇒ Rr

Returns a new instance of Rr.



276
277
278
279
280
281
# File 'lib/gbrb/cpu/instruction.rb', line 276

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

Instance Method Details

#call(r, mem) ⇒ Object



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/gbrb/cpu/instruction.rb', line 283

def call r, mem
  if @indirect
    address = r.public_send(@target).read
    v = mem.read_byte(address)
  else
    v = r.public_send(@target).read
  end

  carry_out = v & 0x01

  carry_in = if @carry
               carry_out
             else
               r.carry_flag? ? 1 : 0
             end

  v >>= 1

  v += carry_in << 7

  if @indirect
    original = mem.read_byte address
    mem.write_byte(address, v)
  else
    r.public_send(@target).store v
  end

  r.clear_add_sub_flag
  r.clear_half_carry_flag
  v & 0xff == 0 ? r.set_zero_flag : r.clear_zero_flag
  carry_out == 1 ? r.set_carry_flag : r.clear_carry_flag
end