Class: Amaterasu::GameBoy::Cpu::Instructions::Or

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

Overview

Handles the logic related to all possible OR instructions

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

Returns a new instance of Or.

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/or.rb', line 15

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

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

Instance Method Details

#build_logic(source) ⇒ Proc

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

Parameters:

Returns:



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

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

#or_a(value) ⇒ void

This method returns an undefined value.

Performs a Bitwise OR between a given value and the A register.

Parameters:



44
45
46
47
48
49
50
51
# File 'lib/amaterasu/game_boy/cpu/instructions/or.rb', line 44

def or_a(value)
  result = @registers.a | value

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

  @registers.a = result
end