Class: Amaterasu::GameBoy::Cpu::Instructions::Adc

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

Overview

Handles the logic related to all possible ADC instructions

  • ADC A, r8
  • ADC A, [HL]
  • ADC 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:) ⇒ Adc

Returns a new instance of Adc.

Parameters:

  • cpu (Cpu)

    Holds a direct reference to the main Cpu object.

  • source (Symbol)

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

  • cpu: (Cpu)
  • source: (Symbol)


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

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

  @mnemonic = "ADC A, #{source}"
  @logic    = build_logic(source)
end

Instance Method Details

#adc_a(value) ⇒ void

This method returns an undefined value.

ADCs a given value plus the Carry flag into register A.

  • Sets the Zero flag if the result is zero, otherwise clears it.
  • Sets the Subtraction flag to zero.
  • Sets the Half Carry flag if there was overflow from Bit 3, otherwise clears it.
  • Sets the Carry flag if there was overflow from Bit 7, otherwise clears it.

Parameters:

  • value (Integer)


48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/amaterasu/game_boy/cpu/instructions/adc.rb', line 48

def adc_a(value)
  acc = @registers.a
  carry_in = @registers.c_flag
  result = @registers.a + value + carry_in

  @registers.z_flag = result.nobits?(0xFF)
  @registers.n_flag = false
  @registers.h_flag = (acc & 0x0F) + (value & 0x0F) + carry_in > 0x0F
  @registers.c_flag = result > 0xFF

  @registers.a = result
end

#build_logic(source) ⇒ Proc

Builds the logic for all ADC instructions. Returns a lambda object to be called by the CPU.

Parameters:

  • source (Symbol)

Returns:

  • (Proc)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/amaterasu/game_boy/cpu/instructions/adc.rb', line 26

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