Class: Amaterasu::GameBoy::Cpu::Instructions::Ret

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

Overview

Holds the logic of all the RET + RETI instructions.

Instance Attribute Summary

Attributes inherited from Base

#mnemonic

Instance Method Summary collapse

Methods inherited from Base

#execute, #format_operand

Constructor Details

#initialize(cpu:, condition: nil, enable_ime: false) ⇒ Ret

Creates a Ret instruction object with a mnemonic and logic to be executed.

Parameters:

  • cpu: (Cpu)
  • condition: (Symbol) (defaults to: nil)
  • enable_ime: (Boolean) (defaults to: false)


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

def initialize(cpu:, condition: nil, enable_ime: false)
  super(cpu:)

  @mnemonic = enable_ime ? 'RETI' : "RET #{format_operand(condition)}".strip
  @logic    = build_logic(condition, enable_ime)
end

Instance Method Details

#build_logic(condition, enable_ime) ⇒ Proc

Returns a Proc object to be executed by the CPU at runtime.

Parameters:

  • (Symbol ?condition)
  • (bool ?enable_ime)

Returns:

  • (Proc)


20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/amaterasu/game_boy/cpu/instructions/ret.rb', line 20

def build_logic(condition, enable_ime)
  return -> { reti } if enable_ime
  return -> { ret }  if condition.nil?

  case condition
  when :nz then -> { ret_cc(condition: @registers.z_flag.zero?) }
  when :z  then -> { ret_cc(condition: @registers.z_flag == 1) }
  when :nc then -> { ret_cc(condition: @registers.c_flag.zero?) }
  when :c  then -> { ret_cc(condition: @registers.c_flag == 1) }
  else
    raise ArgumentError, 'Unknown Ret condition'
  end
end

#retvoid

This method returns an undefined value.

M-cycle 1: Fetches opcode. M-cycle 2: Pops the LSB from the Stack. M-cycle 3: Pops the MSB from the Stack. M-cycle 4: Jumps to the return address.



52
53
54
55
# File 'lib/amaterasu/game_boy/cpu/instructions/ret.rb', line 52

def ret
  return_address = @cpu.stack_pop
  @cpu.jump_to(address: return_address)
end

#ret_cc(condition: true) ⇒ void

This method returns an undefined value.

M-cycle 1: Fetches opcode. M-cycle 2: Spends 1 cycle to evaluate condition (Only for RET cc). ---------- Returns early if condition not met. M-cycle 3: Pops the LSB from the Stack. M-cycle 4: Pops the MSB from the Stack. M-cycle 5: Jumps to the return address.

Parameters:

  • condition: (Boolean) (defaults to: true)


40
41
42
43
44
45
46
# File 'lib/amaterasu/game_boy/cpu/instructions/ret.rb', line 40

def ret_cc(condition: true)
  @cpu.internal_processing
  return unless condition

  return_address = @cpu.stack_pop
  @cpu.jump_to(address: return_address)
end

#retivoid

This method returns an undefined value.

M-cycle 1: Fetches opcode. M-cycle 2: Pops the LSB from the Stack. M-cycle 3: Pops the MSB from the Stack. M-cycle 4: Jumps to the return address Enable interrupts (same cycle).



62
63
64
65
# File 'lib/amaterasu/game_boy/cpu/instructions/ret.rb', line 62

def reti
  ret
  @cpu.enable_interrupts
end