Class: Amaterasu::GameBoy::Cpu::Instructions::Adc
- 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
Instance Method Summary collapse
-
#adc_a(value) ⇒ void
ADCs a given value plus the Carry flag into register A.
-
#build_logic(source) ⇒ Proc
Builds the logic for all ADC instructions.
-
#initialize(cpu:, source:) ⇒ Adc
constructor
A new instance of Adc.
Methods inherited from Base
Constructor Details
#initialize(cpu:, source:) ⇒ Adc
Returns a new instance of Adc.
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.
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.
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 |