Class: Amaterasu::GameBoy::Cpu::Instructions::CbRrc

Inherits:
Base
  • Object
show all
Includes:
Utils::BitOps
Defined in:
lib/amaterasu/game_boy/cpu/instructions/cb_rrc.rb,
sig/akane/game_boy/cpu/instructions/cb_rrc.rbs

Overview

Holds the logic of all the RRC (Rotate Right Circular) instructions.

  • RRC r8
  • RRC [HL]

Instance Attribute Summary

Attributes inherited from Base

#mnemonic

Instance Method Summary collapse

Methods included from Utils::BitOps

bit, clear_bit, #self?.bit, #self?.clear_bit, #self?.set_bit, set_bit

Methods inherited from Base

#execute, #format_operand

Constructor Details

#initialize(cpu:, target:) ⇒ CbRrc



14
15
16
17
18
19
# File 'lib/amaterasu/game_boy/cpu/instructions/cb_rrc.rb', line 14

def initialize(cpu:, target:)
  super(cpu:)

  @mnemonic = "RRC #{format_operand(target)}"
  @logic    = build_logic(target)
end

Instance Method Details

#build_logic(target) ⇒ Proc



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/amaterasu/game_boy/cpu/instructions/cb_rrc.rb', line 23

def build_logic(target)
  case target
  when :b      then -> { @registers.b = rrc_reg8(@registers.b) }
  when :c      then -> { @registers.c = rrc_reg8(@registers.c) }
  when :d      then -> { @registers.d = rrc_reg8(@registers.d) }
  when :e      then -> { @registers.e = rrc_reg8(@registers.e) }
  when :h      then -> { @registers.h = rrc_reg8(@registers.h) }
  when :l      then -> { @registers.l = rrc_reg8(@registers.l) }
  when :mem_hl then -> { rrc_mem_hl }
  when :a      then -> { @registers.a = rrc_reg8(@registers.a) }
  else
    raise ArgumentError, 'Unknown CbRrc target'
  end
end

#rrc_mem_hlvoid

This method returns an undefined value.

Takes 2 extra cycles due to the Bus read and write operations.



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/amaterasu/game_boy/cpu/instructions/cb_rrc.rb', line 53

def rrc_mem_hl
  value_at_mem_hl = @cpu.bus_read(address: @registers.hl)
  old_bit0 = bit(value_at_mem_hl, 0)
  result = (old_bit0 << 7) | (value_at_mem_hl >> 1)

  @registers.clear_flags
  @registers.z_flag = result.nobits?(0xFF)
  @registers.c_flag = old_bit0 == 1

  @cpu.bus_write(address: @registers.hl, value: result)
end

#rrc_reg8(reg8_value) ⇒ void

This method returns an undefined value.

[0] -> [7][6][5][4][3][2][1][0] -> [C] [0][7][6][5][4][3][2][1] -> [C=0]



41
42
43
44
45
46
47
48
49
50
# File 'lib/amaterasu/game_boy/cpu/instructions/cb_rrc.rb', line 41

def rrc_reg8(reg8_value)
  old_bit0 = bit(reg8_value, 0)
  result = (old_bit0 << 7) | (reg8_value >> 1)

  @registers.clear_flags
  @registers.z_flag = result.nobits?(0xFF)
  @registers.c_flag = old_bit0 == 1

  result
end