Class: Amaterasu::GameBoy::Cpu::Instructions::Push

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

Overview

Holds the logic of all the PUSH instructions.

Instance Attribute Summary

Attributes inherited from Base

#mnemonic

Instance Method Summary collapse

Methods inherited from Base

#execute, #format_operand

Constructor Details

#initialize(cpu:, reg16:) ⇒ Push

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

Parameters:

  • cpu: (Cpu)
  • reg16: (Symbol)


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

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

  @mnemonic = "PUSH #{format_operand(reg16)}"
  @logic    = build_logic(reg16)
end

Instance Method Details

#build_logic(reg16) ⇒ Proc

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

Parameters:

  • reg16 (Symbol)

Returns:

  • (Proc)


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

def build_logic(reg16)
  case reg16
  when :bc then -> { push(@registers.bc) }
  when :de then -> { push(@registers.de) }
  when :hl then -> { push(@registers.hl) }
  when :af then -> { push(@registers.af) }
  else
    raise ArgumentError, 'Unknown Push reg16'
  end
end

#push(reg16) ⇒ void

This method returns an undefined value.

M-cycle 1: Fetches opcode. M-cycle 2: Internal processing (dead cycle). M-cycle 3: Writes msb from reg16 into the stack. M-cycle 4: Writes lsb from reg16 into the stack.

Parameters:

  • value (Integer)


35
36
37
38
# File 'lib/amaterasu/game_boy/cpu/instructions/push.rb', line 35

def push(reg16)
  @cpu.internal_processing
  @cpu.stack_push(value: reg16)
end