Class: Amaterasu::GameBoy::Cpu::Instructions::CbRlc

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

Overview

Holds the logic of all the RLC (Rotate Left Circular) instructions.

  • RLC r8
  • RLC [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:) ⇒ CbRlc



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

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

  @mnemonic = "RLC #{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_rlc.rb', line 23

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

#rlc_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_rlc.rb', line 53

def rlc_mem_hl
  value_at_mem_hl = @cpu.bus_read(address: @registers.hl)
  old_bit7 = bit(value_at_mem_hl, 7)
  result = (value_at_mem_hl << 1) | old_bit7

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

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

#rlc_reg8(reg8_value) ⇒ void

This method returns an undefined value.

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



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

def rlc_reg8(reg8_value)
  old_bit7 = bit(reg8_value, 7)
  result = (reg8_value << 1) | old_bit7

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

  result
end