Class: Amaterasu::GameBoy::Cpu::Instructions::Ldh

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

Overview

Holds the logic of all the LDH (Load High RAM) instructions.

Constant Summary collapse

IO_BASE_ADDRESS =

Returns:

  • (Integer)
0xFF00

Instance Attribute Summary

Attributes inherited from Base

#mnemonic

Instance Method Summary collapse

Methods inherited from Base

#execute, #format_operand

Constructor Details

#initialize(cpu:, target:, source:) ⇒ Ldh

Returns a new instance of Ldh.

Parameters:

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


11
12
13
14
15
16
17
18
# File 'lib/amaterasu/game_boy/cpu/instructions/ldh.rb', line 11

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

  @mnemonic = "LDH #{format_operand(target)}, #{format_operand(source)}"
  @bytes    = 2
  @m_cycles = 2
  @logic    = build_logic(target, source)
end

Instance Method Details

#build_logic(target, source) ⇒ Proc

Parameters:

  • target (Symbol)
  • source (Symbol)

Returns:

  • (Proc)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/amaterasu/game_boy/cpu/instructions/ldh.rb', line 22

def build_logic(target, source)
  case target
  when :a
    case source
    when :mem_unsig8 then -> { ldh_a_mem_unsig8 }
    when :mem_c then -> { ldh_a_mem_c }
    else
      raise ArgumentError, 'Unknown Ldh source for :a target'
    end
  when :mem_unsig8 then -> { ldh_mem_unsig8_a }
  when :mem_c then -> { ldh_mem_c_a }
  else
    raise ArgumentError, 'Unknown Ldh target'
  end
end

#ldh_a_mem_cvoid

This method returns an undefined value.



54
55
56
# File 'lib/amaterasu/game_boy/cpu/instructions/ldh.rb', line 54

def ldh_a_mem_c
  @registers.a = @cpu.bus_read(address: IO_BASE_ADDRESS + @registers.c)
end

#ldh_a_mem_unsig8void

This method returns an undefined value.



44
45
46
47
48
# File 'lib/amaterasu/game_boy/cpu/instructions/ldh.rb', line 44

def ldh_a_mem_unsig8
  unsigned_byte = @cpu.fetch_next_byte

  @registers.a = @cpu.bus_read(address: IO_BASE_ADDRESS + unsigned_byte)
end

#ldh_mem_c_avoid

This method returns an undefined value.



50
51
52
# File 'lib/amaterasu/game_boy/cpu/instructions/ldh.rb', line 50

def ldh_mem_c_a
  @cpu.bus_write(address: IO_BASE_ADDRESS + @registers.c, value: @registers.a)
end

#ldh_mem_unsig8_avoid

This method returns an undefined value.



38
39
40
41
42
# File 'lib/amaterasu/game_boy/cpu/instructions/ldh.rb', line 38

def ldh_mem_unsig8_a
  unsigned_byte = @cpu.fetch_next_byte

  @cpu.bus_write(address: IO_BASE_ADDRESS + unsigned_byte, value: @registers.a)
end