Class: Amaterasu::GameBoy::Cpu::Instructions::CbSwap
- Includes:
- Utils::BitOps
- Defined in:
- lib/amaterasu/game_boy/cpu/instructions/cb_swap.rb,
sig/akane/game_boy/cpu/instructions/cb_swap.rbs
Overview
Holds the logic of all the SWAP instructions.
- SWAP r8
- SWAP [HL]
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #build_logic(target) ⇒ Proc
-
#initialize(cpu:, target:) ⇒ CbSwap
constructor
A new instance of CbSwap.
-
#swap(target) ⇒ void
Swaps the positions of the Upper and Lower 4 bits.
-
#swap_mem_hl ⇒ void
Takes 2 extra cycles due to the Bus read and write operations.
Methods included from Utils::BitOps
bit, clear_bit, #self?.bit, #self?.clear_bit, #self?.set_bit, set_bit
Methods inherited from Base
Constructor Details
#initialize(cpu:, target:) ⇒ CbSwap
Returns a new instance of CbSwap.
14 15 16 17 18 19 |
# File 'lib/amaterasu/game_boy/cpu/instructions/cb_swap.rb', line 14 def initialize(cpu:, target:) super(cpu:) @mnemonic = "SWAP #{format_operand(target)}" @logic = build_logic(target) end |
Instance Method Details
#build_logic(target) ⇒ Proc
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/amaterasu/game_boy/cpu/instructions/cb_swap.rb', line 23 def build_logic(target) case target when :b then -> { @registers.b = swap(@registers.b) } when :c then -> { @registers.c = swap(@registers.c) } when :d then -> { @registers.d = swap(@registers.d) } when :e then -> { @registers.e = swap(@registers.e) } when :h then -> { @registers.h = swap(@registers.h) } when :l then -> { @registers.l = swap(@registers.l) } when :mem_hl then -> { swap_mem_hl } when :a then -> { @registers.a = swap(@registers.a) } else raise ArgumentError, 'Unknown CbSwap target' end end |
#swap(target) ⇒ void
This method returns an undefined value.
Swaps the positions of the Upper and Lower 4 bits.
11110000 -> 00001111
42 43 44 45 46 47 48 49 50 |
# File 'lib/amaterasu/game_boy/cpu/instructions/cb_swap.rb', line 42 def swap(target) upper4 = (target >> 4) & 0x0F result = (target << 4) | upper4 @registers.clear_flags @registers.z_flag = result.nobits?(0xFF) result end |
#swap_mem_hl ⇒ void
This method returns an undefined value.
Takes 2 extra cycles due to the Bus read and write operations.
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/amaterasu/game_boy/cpu/instructions/cb_swap.rb', line 53 def swap_mem_hl byte = @cpu.bus_read(address: @registers.hl) upper4 = (byte >> 4) & 0x0F result = (byte << 4) | upper4 @registers.clear_flags @registers.z_flag = result.nobits?(0xFF) @cpu.bus_write(address: @registers.hl, value: result) end |