Class: Amaterasu::GameBoy::Cpu::Instructions::Sbc
- Defined in:
- lib/amaterasu/game_boy/cpu/instructions/sbc.rb,
sig/akane/game_boy/cpu/instructions/sbc.rbs
Overview
Handles the logic related to all possible SBC instructions
- SBC A, r8
- SBC A, [HL]
- SBC A, n8
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#build_logic(source) ⇒ Proc
Builds the logic for all SBC instructions.
- #define_mnemonic ⇒ String
-
#initialize(cpu:, source:) ⇒ Sbc
constructor
A new instance of Sbc.
-
#sbc_a(value) ⇒ void
Subtracts a given value + Carry flag from register A and stores it back into A.
Methods inherited from Base
Constructor Details
#initialize(cpu:, source:) ⇒ Sbc
15 16 17 18 19 20 |
# File 'lib/amaterasu/game_boy/cpu/instructions/sbc.rb', line 15 def initialize(cpu:, source:) super(cpu:) @mnemonic = "SBC A, #{source}" @logic = build_logic(source) end |
Instance Method Details
#build_logic(source) ⇒ Proc
Builds the logic for all SBC instructions. Returns a lambda object to be called by the CPU.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/amaterasu/game_boy/cpu/instructions/sbc.rb', line 26 def build_logic(source) case source when :a then -> { sbc_a(@registers.a) } when :b then -> { sbc_a(@registers.b) } when :c then -> { sbc_a(@registers.c) } when :d then -> { sbc_a(@registers.d) } when :e then -> { sbc_a(@registers.e) } when :h then -> { sbc_a(@registers.h) } when :l then -> { sbc_a(@registers.l) } when :mem_hl then -> { sbc_a(@cpu.bus_read(address: @registers.hl)) } when :imm8 then -> { sbc_a(@cpu.fetch_next_byte) } else raise ArgumentError, 'Unknown Sbc source' end end |
#define_mnemonic ⇒ String
12 |
# File 'sig/akane/game_boy/cpu/instructions/sbc.rbs', line 12
def define_mnemonic: (Symbol source) -> String
|
#sbc_a(value) ⇒ void
This method returns an undefined value.
Subtracts a given value + Carry flag from register A and stores it back into A.
- Sets the Zero flag if the result is zero, otherwise clears it.
- Always sets the SBCtraction flag.
- Sets the Half Carry flag if it needed to borrow from Bit 4.
- Sets the Carry flag if it needed to borrow (acc < value).
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/amaterasu/game_boy/cpu/instructions/sbc.rb', line 48 def sbc_a(value) acc = @registers.a carry_in = @registers.c_flag result = @registers.a - (value + carry_in) @registers.z_flag = result.nobits?(0xFF) @registers.n_flag = true @registers.h_flag = (acc & 0x0F) < ((value & 0x0F) + carry_in) @registers.c_flag = acc < (value + carry_in) @registers.a = result end |