Class: Amaterasu::GameBoy::Cpu::Instructions::Ld16

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

Overview

Holds the logic of all the 16-bit Load instructions.

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:) ⇒ Ld16



9
10
11
12
13
14
# File 'lib/amaterasu/game_boy/cpu/instructions/ld16.rb', line 9

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

  @mnemonic = "LD #{format_operand(target)}, #{format_operand(source)}"
  @logic    = build_logic(target, source)
end

Instance Method Details

#build_logic(target, source) ⇒ Proc



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/amaterasu/game_boy/cpu/instructions/ld16.rb', line 18

def build_logic(target, source)
  return -> { ld_mem_imm16_sp } if target == :mem_imm16

  case target
  when :bc then -> { @registers.bc = @cpu.fetch_next_word }
  when :de then -> { @registers.de = @cpu.fetch_next_word }
  when :hl
    case source
    when :imm16 then -> { @registers.hl = @cpu.fetch_next_word }
    when :sp_plus_sig8
      lambda do
        unsigned_byte = @cpu.fetch_next_byte
        signed_value = @cpu.sign_value(unsigned_byte)
        sp = @registers.sp
        result = @cpu.add16(@registers.sp, signed_value)

        @registers.clear_flags
        @registers.h_flag = (sp & 0x0F) + (unsigned_byte & 0x0F) > 0x0F
        @registers.c_flag = (sp & 0xFF) + (unsigned_byte & 0xFF) > 0xFF

        @registers.hl = result
      end
    else
      raise ArgumentError, 'Unknown Ld16 source for LD HL'
    end
  when :sp
    case source
    when :imm16 then -> { @registers.sp = @cpu.fetch_next_word }
    when :hl
      lambda do
        @cpu.internal_processing
        @registers.sp = @registers.hl
      end
    else
      raise ArgumentError, 'Unknown Ld16 source for LD SP'
    end
  else
    raise ArgumentError, 'Unknown Ld16 target'
  end
end

#ld_mem_imm16_spvoid

This method returns an undefined value.

Loads the LSB from SP to the imm16 address. Loads the MSB from SP to the imm16 address + 1.

M-cycle 1: Fetches the opcode. M-cycle 2: Fetches the LSB of the address to be used. M-cycle 3: Fetches the MSB of the address to be used. M-cycle 4: Writes the LSB of SP into the address. M-cycle 5: Writes the MSB of SP into the address + 1.



68
69
70
71
72
73
74
# File 'lib/amaterasu/game_boy/cpu/instructions/ld16.rb', line 68

def ld_mem_imm16_sp
  imm16 = @cpu.fetch_next_word
  sp = @registers.sp

  @cpu.bus_write(address: imm16, value: sp & 0xFF)
  @cpu.bus_write(address: imm16 + 1, value: (sp >> 8) & 0xFF)
end