Class: Amaterasu::GameBoy::Cpu::Instructions::Jp

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

Overview

Holds the logic of all the 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:, location:, condition: nil) ⇒ Jp

Returns a new instance of Jp.

Parameters:

  • cpu: (Cpu)
  • location: (Symbol)
  • condition: (Symbol) (defaults to: nil)


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

def initialize(cpu:, location:, condition: nil)
  super(cpu:)

  @mnemonic = "JP #{format_operand(condition)}, #{format_operand(location)}"
  @logic    = build_logic(location, condition)
end

Instance Method Details

#build_logic(location, condition) ⇒ Proc

Parameters:

  • location (Symbol)
  • (Symbol ?condition)

Returns:

  • (Proc)


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

def build_logic(location, condition)
  if condition.nil?
    case location
    when :imm16 then -> { @cpu.jump_to(address: @cpu.fetch_next_word) }
    when :hl then -> { @registers.pc = @registers.hl } # No extra cycle
    else
      raise ArgumentError, 'Unknown Jp location'
    end
  else
    case condition
    when :nz then -> { jp(condition: @registers.z_flag.zero?) }
    when :nc then -> { jp(condition: @registers.c_flag.zero?) }
    when :z  then -> { jp(condition: @registers.z_flag == 1) }
    when :c  then -> { jp(condition: @registers.c_flag == 1) }
    else
      raise ArgumentError, 'Unknown Jp condition'
    end
  end
end

#jp(condition:) ⇒ void

This method returns an undefined value.

M-cycle 1: Fetches the opcode. M-cycle 2: Fetches next immediate byte (lsb). M-cycle 3: Fetches next immediate byte (msb). --------- Returns early if condition is not met. M-cycle 4: Jumps to the address (takes 1 internal cycle).

Parameters:

  • condition: (Boolean)


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

def jp(condition:)
  address = @cpu.fetch_next_word
  return unless condition

  @cpu.jump_to(address:)
end