Class: Amaterasu::GameBoy::Cpu::Instructions::And
- Defined in:
- lib/amaterasu/game_boy/cpu/instructions/and.rb,
sig/akane/game_boy/cpu/instructions/and.rbs
Overview
Handles the logic related to all possible AND instructions
- AND A, r8
- AND A, [HL]
- AND A, n8
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#and_a(value) ⇒ void
Performs a bitwise AND between a given value and the register A.
-
#build_logic(source) ⇒ Proc
Builds the logic for all AND instructions.
-
#initialize(cpu:, source:) ⇒ And
constructor
A new instance of And.
Methods inherited from Base
Constructor Details
#initialize(cpu:, source:) ⇒ And
15 16 17 18 19 20 |
# File 'lib/amaterasu/game_boy/cpu/instructions/and.rb', line 15 def initialize(cpu:, source:) super(cpu:) @mnemonic = "AND A, #{source}" @logic = build_logic(source) end |
Instance Method Details
#and_a(value) ⇒ void
This method returns an undefined value.
Performs a bitwise AND between a given value and the register A.
- Sets the Zero flag if the result is zero, otherwise clears it.
- Always clears the Subtraction flag.
- Always sets the Half Carry flag.
- Always clears the Carry flag.
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/amaterasu/game_boy/cpu/instructions/and.rb', line 48 def and_a(value) result = @registers.a & value @registers.z_flag = result.nobits?(0xFF) @registers.n_flag = false @registers.h_flag = true @registers.c_flag = false @registers.a = result end |
#build_logic(source) ⇒ Proc
Builds the logic for all AND 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/and.rb', line 26 def build_logic(source) case source when :a then -> { and_a(@registers.a) } when :b then -> { and_a(@registers.b) } when :c then -> { and_a(@registers.c) } when :d then -> { and_a(@registers.d) } when :e then -> { and_a(@registers.e) } when :h then -> { and_a(@registers.h) } when :l then -> { and_a(@registers.l) } when :mem_hl then -> { and_a(@cpu.bus_read(address: @registers.hl)) } when :imm8 then -> { and_a(@cpu.fetch_next_byte) } else raise ArgumentError, 'Unknown And source' end end |