Class: Amaterasu::GameBoy::Cpu::Instructions::CbRl

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

Overview

Holds the logic of all the RL (Rotate Left Through Carry) instructions.

  • RL r8
  • RL [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:) ⇒ CbRl



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

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

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

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

#rl_mem_hlvoid

This method returns an undefined value.

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



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

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

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

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

#rl_reg8(reg8_value) ⇒ void

This method returns an undefined value.

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



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

def rl_reg8(reg8_value)
  carry_in = @registers.c_flag
  old_bit7 = bit(reg8_value, 7)
  result = (reg8_value << 1) | carry_in

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

  result
end