2017-12-02 19:05:53 +00:00
|
|
|
/*
|
2017-12-09 04:12:31 +00:00
|
|
|
* mos6502.arith.c
|
|
|
|
*
|
|
|
|
* We define here the logic for arithmetic instructions for the MOS 6502
|
|
|
|
* processor.
|
2017-12-02 19:05:53 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "mos6502.h"
|
|
|
|
#include "mos6502.enums.h"
|
|
|
|
|
2017-12-06 23:03:37 +00:00
|
|
|
/*
|
|
|
|
* The adc instruction will add a number to the accumulator, "with
|
|
|
|
* carry". If the carry bit is set, we will add 1 to the accumulator as
|
|
|
|
* an after-effect.
|
|
|
|
*/
|
2017-12-02 19:05:53 +00:00
|
|
|
DEFINE_INST(adc)
|
|
|
|
{
|
2018-01-05 20:18:39 +00:00
|
|
|
MOS_CARRY_BIT();
|
2017-12-02 19:05:53 +00:00
|
|
|
cpu->A += oper + carry;
|
2017-12-09 05:26:46 +00:00
|
|
|
|
2018-01-05 20:18:39 +00:00
|
|
|
mos6502_modify_status(cpu, MOS_NEGATIVE | MOS_OVERFLOW | MOS_CARRY | MOS_ZERO, cpu->A);
|
2017-12-02 19:05:53 +00:00
|
|
|
}
|
|
|
|
|
2017-12-06 23:03:37 +00:00
|
|
|
/*
|
|
|
|
* The cmp instruction will consider the difference of the accumulator
|
|
|
|
* minus the operand. It will then set the zero, negative, or carry bits
|
|
|
|
* based upon that difference. _The accumulator is neither modified nor
|
|
|
|
* harmed by this operation._ (We have trained experts on the set to
|
|
|
|
* monitor the health of the accumulator, whom we've named George.)
|
|
|
|
*/
|
2017-12-02 19:05:53 +00:00
|
|
|
DEFINE_INST(cmp)
|
|
|
|
{
|
2018-01-05 20:18:39 +00:00
|
|
|
mos6502_modify_status(cpu, MOS_ZERO | MOS_NEGATIVE | MOS_CARRY, cpu->A - oper);
|
2017-12-02 19:05:53 +00:00
|
|
|
}
|
|
|
|
|
2017-12-06 23:03:37 +00:00
|
|
|
/*
|
|
|
|
* This instruction is functionally identical to CMP, with the exception
|
|
|
|
* that it considers the X register rather than the accumulator.
|
|
|
|
*/
|
2017-12-02 19:05:53 +00:00
|
|
|
DEFINE_INST(cpx)
|
|
|
|
{
|
2018-01-05 20:18:39 +00:00
|
|
|
mos6502_modify_status(cpu, MOS_ZERO | MOS_NEGATIVE | MOS_CARRY, cpu->X - oper);
|
2017-12-02 19:05:53 +00:00
|
|
|
}
|
|
|
|
|
2017-12-06 23:03:37 +00:00
|
|
|
/*
|
|
|
|
* Again, this is a variant of the CMP instruction, except that it works
|
|
|
|
* with the Y register.
|
|
|
|
*/
|
2017-12-02 19:05:53 +00:00
|
|
|
DEFINE_INST(cpy)
|
|
|
|
{
|
2018-01-05 20:18:39 +00:00
|
|
|
mos6502_modify_status(cpu, MOS_ZERO | MOS_NEGATIVE | MOS_CARRY, cpu->Y - oper);
|
2017-12-02 19:05:53 +00:00
|
|
|
}
|
|
|
|
|
2017-12-06 23:03:37 +00:00
|
|
|
/*
|
|
|
|
* Here we will decrement the value at the effective address in memory
|
|
|
|
* by 1. The DEC instruction is _unable_ to decrement the accumulator,
|
|
|
|
* which was a tiny oversight in the original build of the 6502.
|
|
|
|
* (Whoopsie!)
|
|
|
|
*/
|
2017-12-02 19:05:53 +00:00
|
|
|
DEFINE_INST(dec)
|
|
|
|
{
|
|
|
|
if (cpu->last_addr) {
|
|
|
|
vm_segment_set(cpu->memory, cpu->last_addr, oper - 1);
|
2018-01-05 20:18:39 +00:00
|
|
|
mos6502_modify_status(cpu, MOS_NEGATIVE | MOS_ZERO, oper - 1);
|
2017-12-02 19:05:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-06 23:03:37 +00:00
|
|
|
/*
|
|
|
|
* In contrast, this does directly decrement the X register.
|
|
|
|
*/
|
2017-12-02 19:05:53 +00:00
|
|
|
DEFINE_INST(dex)
|
|
|
|
{
|
|
|
|
cpu->X--;
|
2018-01-05 20:18:39 +00:00
|
|
|
mos6502_modify_status(cpu, MOS_NEGATIVE | MOS_ZERO, cpu->X);
|
2017-12-02 19:05:53 +00:00
|
|
|
}
|
|
|
|
|
2017-12-06 23:03:37 +00:00
|
|
|
/*
|
|
|
|
* And, again, here we decrement the Y register.
|
|
|
|
*/
|
2017-12-02 19:05:53 +00:00
|
|
|
DEFINE_INST(dey)
|
|
|
|
{
|
|
|
|
cpu->Y--;
|
2018-01-05 20:18:39 +00:00
|
|
|
mos6502_modify_status(cpu, MOS_NEGATIVE | MOS_ZERO, cpu->Y);
|
2017-12-02 19:05:53 +00:00
|
|
|
}
|
|
|
|
|
2017-12-06 23:03:37 +00:00
|
|
|
/*
|
|
|
|
* The INC instruction is basically the same as the DEC one. It, also,
|
|
|
|
* can only work with an address in memory, and it increments the value
|
|
|
|
* by 1.
|
|
|
|
*/
|
2017-12-02 19:05:53 +00:00
|
|
|
DEFINE_INST(inc)
|
|
|
|
{
|
|
|
|
if (cpu->last_addr) {
|
|
|
|
vm_segment_set(cpu->memory, cpu->last_addr, oper + 1);
|
2018-01-05 20:18:39 +00:00
|
|
|
mos6502_modify_status(cpu, MOS_NEGATIVE | MOS_ZERO, oper + 1);
|
2017-12-02 19:05:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-06 23:03:37 +00:00
|
|
|
/*
|
|
|
|
* See DEX.
|
|
|
|
*/
|
2017-12-02 19:05:53 +00:00
|
|
|
DEFINE_INST(inx)
|
|
|
|
{
|
|
|
|
cpu->X++;
|
2018-01-05 20:18:39 +00:00
|
|
|
mos6502_modify_status(cpu, MOS_NEGATIVE | MOS_ZERO, cpu->X);
|
2017-12-02 19:05:53 +00:00
|
|
|
}
|
|
|
|
|
2017-12-06 23:03:37 +00:00
|
|
|
/*
|
|
|
|
* See DEY.
|
|
|
|
*/
|
2017-12-02 19:05:53 +00:00
|
|
|
DEFINE_INST(iny)
|
|
|
|
{
|
|
|
|
cpu->Y++;
|
2018-01-05 20:18:39 +00:00
|
|
|
mos6502_modify_status(cpu, MOS_NEGATIVE | MOS_ZERO, cpu->Y);
|
2017-12-02 19:05:53 +00:00
|
|
|
}
|
|
|
|
|
2017-12-06 23:03:37 +00:00
|
|
|
/*
|
|
|
|
* This instruction will subtract the operand from the accumulator,
|
|
|
|
* again, "with carry". In this context, that means that if the carry
|
|
|
|
* bit is set, then we will subtract 1 again from the A register after
|
|
|
|
* the operand is subtracted. The result is stored in the accumulator.
|
|
|
|
*/
|
2017-12-02 19:05:53 +00:00
|
|
|
DEFINE_INST(sbc)
|
|
|
|
{
|
2018-01-05 20:18:39 +00:00
|
|
|
MOS_CARRY_BIT();
|
2017-12-04 02:19:17 +00:00
|
|
|
cpu->A = cpu->A - oper - carry;
|
2017-12-09 05:26:46 +00:00
|
|
|
|
2018-01-05 20:18:39 +00:00
|
|
|
mos6502_modify_status(cpu, MOS_NEGATIVE | MOS_OVERFLOW | MOS_CARRY | MOS_ZERO, cpu->A);
|
2017-12-02 19:05:53 +00:00
|
|
|
}
|