Class: Amaterasu::GameBoy::Cpu::Instructions::Inc

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

Overview

Holds the logic of all the INC (Increment) instructions.

Instance Attribute Summary

Attributes inherited from Base

#mnemonic

Instance Method Summary collapse

Methods inherited from Base

#execute, #format_operand

Constructor Details

#initialize(cpu:, operand:) ⇒ Inc

Returns a new instance of Inc.

Parameters:

  • cpu: (Cpu)
  • operand: (Symbol)


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

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

  @mnemonic = "INC #{format_operand(operand)}"
  @logic    = build_logic(operand)
end

Instance Method Details

#build_logic(operand) ⇒ Proc

Parameters:

  • operand (Symbol)

Returns:

  • (Proc)


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

def build_logic(operand)
  case operand
  when :a      then -> { @registers.a = inc(@registers.a) }
  when :b      then -> { @registers.b = inc(@registers.b) }
  when :c      then -> { @registers.c = inc(@registers.c) }
  when :d      then -> { @registers.d = inc(@registers.d) }
  when :e      then -> { @registers.e = inc(@registers.e) }
  when :h      then -> { @registers.h = inc(@registers.h) }
  when :l      then -> { @registers.l = inc(@registers.l) }
  when :bc     then -> { @registers.bc = inc16(@registers.bc) }
  when :de     then -> { @registers.de = inc16(@registers.de) }
  when :hl     then -> { @registers.hl = inc16(@registers.hl) }
  when :sp     then -> { @registers.sp = inc16(@registers.sp) }
  when :mem_hl
    lambda do
      value_at_mem_hl = @cpu.bus_read(address: @registers.hl)
      @cpu.bus_write(address: @registers.hl, value: inc(value_at_mem_hl))
    end
  else
    raise ArgumentError, 'Unknown Inc operand'
  end
end

#inc(value) ⇒ Integer

M-cycle 1: Increments a 8-bit value, sets the flags.

Parameters:

  • value (Integer)

Returns:

  • (Integer)


43
44
45
46
47
48
49
50
51
# File 'lib/amaterasu/game_boy/cpu/instructions/inc.rb', line 43

def inc(value)
  result = value + 1

  @registers.z_flag = result.nobits?(0xFF)
  @registers.n_flag = false
  @registers.h_flag = value.allbits?(0x0F)

  result
end

#inc16(reg16) ⇒ void

This method returns an undefined value.

M-cycle 1: Increments a 16-bit register value. M-cycle 2: Internal processing due to the 16-bit addition. --------- Flags are untouched in the inc16.

Parameters:

  • value (Integer)


57
58
59
# File 'lib/amaterasu/game_boy/cpu/instructions/inc.rb', line 57

def inc16(reg16)
  @cpu.add16(reg16, 1)
end