Class: Amaterasu::GameBoy::Cpu::Instructions::Xor

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

Overview

Handles the logic related to all possible XOR instructions

  • XOR A, r8
  • XOR A, [HL]
  • XOR A, n8

Instance Attribute Summary

Attributes inherited from Base

#mnemonic

Instance Method Summary collapse

Methods inherited from Base

#execute, #format_operand

Constructor Details

#initialize(cpu:, source:) ⇒ Xor

Returns a new instance of Xor.

Parameters:

  • Holds a direct reference to the main Cpu object.

  • Operator, can be a register, :mem_hl, :imm8.



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

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

  @mnemonic = define_mnemonic(source)
  @logic    = build_logic(source)
end

Instance Method Details

#build_logic(source) ⇒ Proc

Parameters:

Returns:



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/amaterasu/game_boy/cpu/instructions/xor.rb', line 32

def build_logic(source)
  case source
  when :a      then -> { xor_a(@registers.a) }
  when :b      then -> { xor_a(@registers.b) }
  when :c      then -> { xor_a(@registers.c) }
  when :d      then -> { xor_a(@registers.d) }
  when :e      then -> { xor_a(@registers.e) }
  when :h      then -> { xor_a(@registers.h) }
  when :l      then -> { xor_a(@registers.l) }
  when :mem_hl then -> { xor_a(@cpu.bus_read(address: @registers.hl)) }
  when :imm8   then -> { xor_a(@cpu.fetch_next_byte) }
  else
    raise ArgumentError, 'Unknown Xor source'
  end
end

#define_mnemonic(source) ⇒ String

Parameters:

Returns:



24
25
26
27
28
29
30
# File 'lib/amaterasu/game_boy/cpu/instructions/xor.rb', line 24

def define_mnemonic(source)
  case source
  when :mem_hl then 'XOR A, [HL]'
  when :imm8 then 'XOR A, n8'
  else "XOR A, #{source.upcase}"
  end
end

#xor_a(value) ⇒ void

This method returns an undefined value.

Parameters:



48
49
50
51
52
53
54
55
# File 'lib/amaterasu/game_boy/cpu/instructions/xor.rb', line 48

def xor_a(value)
  result = @registers.a ^ value

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

  @registers.a = result
end