Module: Badline::InstructionSet::Arithmetic
- Included in:
- Badline::InstructionSet
- Defined in:
- lib/badline/instruction_set/arithmetic.rb
Instance Method Summary collapse
-
#adc(_addr, value) ⇒ Object
Add memory with carry to accumulator.
-
#cmp(_addr, value) ⇒ Object
Compare memory with accumulator.
-
#cpx(_addr, value) ⇒ Object
Compare memory with X register.
-
#cpy(_addr, value) ⇒ Object
Compare memory with Y register.
-
#sbc(_addr, value) ⇒ Object
Subtract memory from accumulator with borrow.
Instance Method Details
#adc(_addr, value) ⇒ Object
Add memory with carry to accumulator.
Opcodes:
$61 - indirect_x - 6 cycles
$65 - zeropage - 3 cycles
$69 - immediate - 2 cycles
$6D - absolute - 4 cycles
$71 - indirect_y - 5+ cycles
$75 - zeropage_x - 4 cycles
$79 - absolute_y - 4+ cycles
$7D - absolute_x - 4+ cycles
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/badline/instruction_set/arithmetic.rb', line 17 def adc(_addr, value) v = resolve(value) result = a + v + status.carry status.zero = result.nobits?(0xff) if status.decimal? result = (a & 0x0f) + (v & 0x0f) + status.carry result += 0x06 if result > 0x09 c = result > 0x0f ? 1 : 0 result = (a & 0xf0) + (v & 0xf0) + (c << 4) + (result & 0x0f) update_calculation_flags(v, result) result += 0x60 if result > 0x9f else update_calculation_flags(v, result) end status.carry = result > 0xff @a = result & 0xff end |
#cmp(_addr, value) ⇒ Object
Compare memory with accumulator.
Opcodes:
$C1 - indirect_x - 6 cycles
$C5 - zeropage - 3 cycles
$C9 - immediate - 2 cycles
$CD - absolute - 4 cycles
$D1 - indirect_y - 5+ cycles
$D5 - zeropage_x - 4 cycles
$D9 - absolute_y - 4+ cycles
$DD - absolute_x - 4+ cycles
48 49 50 51 52 |
# File 'lib/badline/instruction_set/arithmetic.rb', line 48 def cmp(_addr, value) v = resolve(value) status.carry = (@a >= v) update_number_flags(@a - v) end |
#cpx(_addr, value) ⇒ Object
Compare memory with X register.
Opcodes:
$E0 - immediate - 2 cycles
$E4 - zeropage - 3 cycles
$EC - absolute - 4 cycles
60 61 62 63 64 |
# File 'lib/badline/instruction_set/arithmetic.rb', line 60 def cpx(_addr, value) v = resolve(value) status.carry = @x >= v update_number_flags(@x - v) end |
#cpy(_addr, value) ⇒ Object
Compare memory with Y register.
Opcodes:
$C0 - immediate - 2 cycles
$C4 - zeropage - 3 cycles
$CC - absolute - 4 cycles
72 73 74 75 76 |
# File 'lib/badline/instruction_set/arithmetic.rb', line 72 def cpy(_addr, value) v = resolve(value) status.carry = @y >= v update_number_flags(@y - v) end |
#sbc(_addr, value) ⇒ Object
Subtract memory from accumulator with borrow.
Opcodes:
$E1 - indirect_x - 6 cycles
$E5 - zeropage - 3 cycles
$E9 - immediate - 2 cycles
$ED - absolute - 4 cycles
$F1 - indirect_y - 5+ cycles
$F5 - zeropage_x - 4 cycles
$F9 - absolute_y - 4+ cycles
$FD - absolute_x - 4+ cycles
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/badline/instruction_set/arithmetic.rb', line 89 def sbc(_addr, value) v = resolve(value) v_inv = ~v & 0xff carry = status.carry? ? 0 : -1 result = a + v_inv + status.carry status.zero = result.nobits?(0xff) status.carry = result > 0xff update_calculation_flags(v_inv, result) if status.decimal? al = (a & 0x0f) - (v & 0x0f) + carry al = ((al - 0x06) & 0x0F) - 0x10 if al.negative? result = (a & 0xf0) - (v & 0xf0) + al result -= 0x60 if result.negative? end @a = result & 0xff end |