Class: Amaterasu::GameBoy::Cpu::Instructions::Add8
- Defined in:
- lib/amaterasu/game_boy/cpu/instructions/add8.rb,
sig/akane/game_boy/cpu/instructions/add8.rbs
Overview
Handles the logic related to all possible ADD instructions
- ADD A, r8
- ADD A, [HL]
- ADD A, n8
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#add_a(value) ⇒ void
Adds a given value into register A.
-
#build_logic(source) ⇒ Proc
Builds the logic for all ADD instructions.
-
#initialize(cpu:, source:) ⇒ Add8
constructor
A new instance of Add8.
Methods inherited from Base
Constructor Details
#initialize(cpu:, source:) ⇒ Add8
Returns a new instance of Add8.
15 16 17 18 19 20 |
# File 'lib/amaterasu/game_boy/cpu/instructions/add8.rb', line 15 def initialize(cpu:, source:) super(cpu:) @mnemonic = "ADD A, #{source}" @logic = build_logic(source) end |
Instance Method Details
#add_a(value) ⇒ void
This method returns an undefined value.
Adds a given value 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 |
# File 'lib/amaterasu/game_boy/cpu/instructions/add8.rb', line 48 def add_a(value) acc = @registers.a result = @registers.a + value @registers.z_flag = result.nobits?(0xFF) @registers.n_flag = false @registers.h_flag = (acc & 0x0F) + (value & 0x0F) > 0x0F @registers.c_flag = result > 0xFF @registers.a = result end |
#build_logic(source) ⇒ Proc
Builds the logic for all ADD 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/add8.rb', line 26 def build_logic(source) case source when :a then -> { add_a(@registers.a) } when :b then -> { add_a(@registers.b) } when :c then -> { add_a(@registers.c) } when :d then -> { add_a(@registers.d) } when :e then -> { add_a(@registers.e) } when :h then -> { add_a(@registers.h) } when :l then -> { add_a(@registers.l) } when :mem_hl then -> { add_a(@cpu.bus_read(address: @registers.hl)) } when :imm8 then -> { add_a(@cpu.fetch_next_byte) } else raise ArgumentError, 'Unknown Add8 source' end end |