2017-12-02 19:05:53 +00:00
|
|
|
/*
|
|
|
|
* mos6502.branch.c
|
2017-12-09 04:12:31 +00:00
|
|
|
*
|
|
|
|
* This is all the logic we use for branch instructions, which are used
|
|
|
|
* for conditional expressions.
|
2017-12-02 19:05:53 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "mos6502.h"
|
|
|
|
#include "mos6502.enums.h"
|
|
|
|
|
2017-12-07 00:01:13 +00:00
|
|
|
/*
|
|
|
|
* This is just a minor convenience macro to wrap the logic we use in
|
|
|
|
* branch situations, which is if `cond` is true, then we set the
|
|
|
|
* program counter to the last effective address.
|
|
|
|
*/
|
2017-12-02 19:05:53 +00:00
|
|
|
#define JUMP_IF(cond) \
|
2018-01-12 19:57:48 +00:00
|
|
|
if (cond) cpu->PC = cpu->eff_addr; else cpu->PC += 2
|
2017-12-02 19:05:53 +00:00
|
|
|
|
2017-12-07 00:01:13 +00:00
|
|
|
/*
|
|
|
|
* Branch if the carry flag is clear.
|
|
|
|
*/
|
2017-12-02 19:05:53 +00:00
|
|
|
DEFINE_INST(bcc)
|
|
|
|
{
|
2018-01-05 20:18:39 +00:00
|
|
|
JUMP_IF(~cpu->P & MOS_CARRY);
|
2017-12-02 19:05:53 +00:00
|
|
|
}
|
|
|
|
|
2017-12-07 00:01:13 +00:00
|
|
|
/*
|
|
|
|
* Branch if carry is set.
|
|
|
|
*/
|
2017-12-02 19:05:53 +00:00
|
|
|
DEFINE_INST(bcs)
|
|
|
|
{
|
2018-01-05 20:18:39 +00:00
|
|
|
JUMP_IF(cpu->P & MOS_CARRY);
|
2017-12-02 19:05:53 +00:00
|
|
|
}
|
|
|
|
|
2017-12-07 00:01:13 +00:00
|
|
|
/*
|
|
|
|
* Branch if the zero flag is set (that is, if our last instruction
|
|
|
|
* resulted in something being _equal to zero_).
|
|
|
|
*/
|
2017-12-02 19:05:53 +00:00
|
|
|
DEFINE_INST(beq)
|
|
|
|
{
|
2018-01-05 20:18:39 +00:00
|
|
|
JUMP_IF(cpu->P & MOS_ZERO);
|
2017-12-02 19:05:53 +00:00
|
|
|
}
|
|
|
|
|
2017-12-07 00:01:13 +00:00
|
|
|
/*
|
|
|
|
* Branch if the negative ("minus") flag is set.
|
|
|
|
*/
|
2017-12-02 19:05:53 +00:00
|
|
|
DEFINE_INST(bmi)
|
|
|
|
{
|
2018-01-05 20:18:39 +00:00
|
|
|
JUMP_IF(cpu->P & MOS_NEGATIVE);
|
2017-12-02 19:05:53 +00:00
|
|
|
}
|
|
|
|
|
2017-12-07 00:01:13 +00:00
|
|
|
/*
|
|
|
|
* Branch if the zero flag is not set; which is to say, that the last
|
|
|
|
* operation was _not equal_ to zero.
|
|
|
|
*/
|
2017-12-02 19:05:53 +00:00
|
|
|
DEFINE_INST(bne)
|
|
|
|
{
|
2018-01-05 20:18:39 +00:00
|
|
|
JUMP_IF(~cpu->P & MOS_ZERO);
|
2017-12-02 19:05:53 +00:00
|
|
|
}
|
|
|
|
|
2017-12-07 00:01:13 +00:00
|
|
|
/*
|
|
|
|
* Branch if the negative flag is not set (meaning the last operation
|
|
|
|
* was "plus", which includes zero).
|
|
|
|
*/
|
2017-12-02 19:05:53 +00:00
|
|
|
DEFINE_INST(bpl)
|
|
|
|
{
|
2018-01-05 20:18:39 +00:00
|
|
|
JUMP_IF(~cpu->P & MOS_NEGATIVE);
|
2017-12-02 19:05:53 +00:00
|
|
|
}
|
|
|
|
|
2017-12-07 00:01:13 +00:00
|
|
|
/*
|
|
|
|
* Branch if the overflow bit is clear.
|
|
|
|
*/
|
2017-12-02 19:05:53 +00:00
|
|
|
DEFINE_INST(bvc)
|
|
|
|
{
|
2018-01-05 20:18:39 +00:00
|
|
|
JUMP_IF(~cpu->P & MOS_OVERFLOW);
|
2017-12-02 19:05:53 +00:00
|
|
|
}
|
|
|
|
|
2017-12-07 00:01:13 +00:00
|
|
|
/*
|
|
|
|
* Branch if the overflow bit is set.
|
|
|
|
*/
|
2017-12-02 19:05:53 +00:00
|
|
|
DEFINE_INST(bvs)
|
|
|
|
{
|
2018-01-05 20:18:39 +00:00
|
|
|
JUMP_IF(cpu->P & MOS_OVERFLOW);
|
2017-12-02 19:05:53 +00:00
|
|
|
}
|