combine macro for updating N and Z status flags

This commit is contained in:
Vineet K 2024-06-30 15:46:18 -04:00
parent 47b8fac273
commit f28733401e

84
cpu.c
View File

@ -12,6 +12,9 @@
(regs.status.zero = r == 0) (regs.status.zero = r == 0)
#define STATUS_UPDATE_NEGATIVE(r) \ #define STATUS_UPDATE_NEGATIVE(r) \
(regs.status.negative = ((r & (1 << 7)) != 0)) (regs.status.negative = ((r & (1 << 7)) != 0))
#define STATUS_UPDATE_NZ(r) \
{STATUS_UPDATE_ZERO(r); \
STATUS_UPDATE_NEGATIVE(r);}
#define STATUS_TO_INT() \ #define STATUS_TO_INT() \
(regs.status.carry | (regs.status.zero << 1) \ (regs.status.carry | (regs.status.zero << 1) \
| (regs.status.interrupt_disable << 2) | (regs.status.decimal_mode << 3) \ | (regs.status.interrupt_disable << 2) | (regs.status.decimal_mode << 3) \
@ -224,8 +227,7 @@ adc(uint8_t arg)
regs.status.overflow = (~(regs.a ^ arg) & (regs.a ^ sum) & 0x80) != 0; regs.status.overflow = (~(regs.a ^ arg) & (regs.a ^ sum) & 0x80) != 0;
regs.a = sum & 0xFF; regs.a = sum & 0xFF;
STATUS_UPDATE_ZERO(regs.a); STATUS_UPDATE_NZ(regs.a);
STATUS_UPDATE_NEGATIVE(regs.a);
} }
static void static void
@ -233,8 +235,7 @@ and(uint8_t arg)
{ {
regs.a &= arg; regs.a &= arg;
STATUS_UPDATE_ZERO(regs.a); STATUS_UPDATE_NZ(regs.a);
STATUS_UPDATE_NEGATIVE(regs.a);
} }
static void static void
@ -246,8 +247,7 @@ asl_acc(void)
regs.a = tmp & 0xFF; regs.a = tmp & 0xFF;
regs.status.carry = tmp > 0xFF; regs.status.carry = tmp > 0xFF;
STATUS_UPDATE_ZERO(regs.a); STATUS_UPDATE_NZ(regs.a);
STATUS_UPDATE_NEGATIVE(regs.a);
} }
static void static void
@ -259,8 +259,7 @@ asl(uint16_t mem)
memwrite(mem, tmp & 0xFF); memwrite(mem, tmp & 0xFF);
regs.status.carry = tmp > 0xFF; regs.status.carry = tmp > 0xFF;
STATUS_UPDATE_ZERO(tmp); STATUS_UPDATE_NZ(tmp);
STATUS_UPDATE_NEGATIVE(tmp);
} }
static void static void
@ -405,8 +404,7 @@ dec(uint16_t mem)
{ {
memwrite(mem, peek(mem) - 1); memwrite(mem, peek(mem) - 1);
STATUS_UPDATE_ZERO(peek(mem)); STATUS_UPDATE_NZ(peek(mem));
STATUS_UPDATE_NEGATIVE(peek(mem));
} }
static void static void
@ -414,8 +412,7 @@ dex(void)
{ {
regs.x--; regs.x--;
STATUS_UPDATE_ZERO(regs.x); STATUS_UPDATE_NZ(regs.x);
STATUS_UPDATE_NEGATIVE(regs.x);
} }
static void static void
@ -423,8 +420,7 @@ dey(void)
{ {
regs.y--; regs.y--;
STATUS_UPDATE_ZERO(regs.y); STATUS_UPDATE_NZ(regs.y);
STATUS_UPDATE_NEGATIVE(regs.y);
} }
static void static void
@ -432,8 +428,7 @@ eor(uint8_t arg)
{ {
regs.a ^= arg; regs.a ^= arg;
STATUS_UPDATE_ZERO(regs.a); STATUS_UPDATE_NZ(regs.a);
STATUS_UPDATE_NEGATIVE(regs.a);
} }
static void static void
@ -441,8 +436,7 @@ inc(uint16_t mem)
{ {
memwrite(mem, peek(mem) + 1); memwrite(mem, peek(mem) + 1);
STATUS_UPDATE_ZERO(peek(mem)); STATUS_UPDATE_NZ(peek(mem));
STATUS_UPDATE_NEGATIVE(peek(mem));
} }
static void static void
@ -450,8 +444,7 @@ inx(void)
{ {
regs.x++; regs.x++;
STATUS_UPDATE_ZERO(regs.x); STATUS_UPDATE_NZ(regs.x);
STATUS_UPDATE_NEGATIVE(regs.x);
} }
static void static void
@ -459,8 +452,7 @@ iny(void)
{ {
regs.y++; regs.y++;
STATUS_UPDATE_ZERO(regs.y); STATUS_UPDATE_NZ(regs.y);
STATUS_UPDATE_NEGATIVE(regs.y);
} }
static void static void
@ -488,8 +480,7 @@ lda(uint8_t arg)
{ {
regs.a = arg; regs.a = arg;
STATUS_UPDATE_ZERO(regs.a); STATUS_UPDATE_NZ(regs.a);
STATUS_UPDATE_NEGATIVE(regs.a);
} }
static void static void
@ -497,8 +488,7 @@ ldx(uint8_t arg)
{ {
regs.x = arg; regs.x = arg;
STATUS_UPDATE_ZERO(regs.x); STATUS_UPDATE_NZ(regs.x);
STATUS_UPDATE_NEGATIVE(regs.x);
} }
static void static void
@ -506,8 +496,7 @@ ldy(uint8_t arg)
{ {
regs.y = arg; regs.y = arg;
STATUS_UPDATE_ZERO(regs.y); STATUS_UPDATE_NZ(regs.y);
STATUS_UPDATE_NEGATIVE(regs.y);
} }
static void static void
@ -517,8 +506,7 @@ lsr_acc(void)
regs.a >>= 1; regs.a >>= 1;
regs.a &= ~(1 << 7); // bit 7 cleared regs.a &= ~(1 << 7); // bit 7 cleared
STATUS_UPDATE_ZERO(regs.a); STATUS_UPDATE_NZ(regs.a);
STATUS_UPDATE_NEGATIVE(regs.a);
} }
static void static void
@ -534,8 +522,7 @@ lsr(uint16_t mem)
memwrite(mem, tmp); memwrite(mem, tmp);
STATUS_UPDATE_ZERO(tmp); STATUS_UPDATE_NZ(tmp);
STATUS_UPDATE_NEGATIVE(tmp);
} }
static void static void
@ -549,8 +536,7 @@ ora(uint8_t arg)
{ {
regs.a |= arg; regs.a |= arg;
STATUS_UPDATE_ZERO(regs.a); STATUS_UPDATE_NZ(regs.a);
STATUS_UPDATE_NEGATIVE(regs.a);
} }
static void static void
@ -570,8 +556,7 @@ pla(void)
{ {
regs.a = PULL(); regs.a = PULL();
STATUS_UPDATE_ZERO(regs.a); STATUS_UPDATE_NZ(regs.a);
STATUS_UPDATE_NEGATIVE(regs.a);
} }
static void static void
@ -601,8 +586,7 @@ rol_acc(void)
regs.a |= regs.status.carry; regs.a |= regs.status.carry;
regs.status.carry = carry; regs.status.carry = carry;
STATUS_UPDATE_ZERO(regs.a); STATUS_UPDATE_NZ(regs.a);
STATUS_UPDATE_NEGATIVE(regs.a);
} }
static void static void
@ -615,8 +599,7 @@ rol(uint16_t mem)
memwrite(mem, tmp); memwrite(mem, tmp);
regs.status.carry = carry; regs.status.carry = carry;
STATUS_UPDATE_ZERO(tmp); STATUS_UPDATE_NZ(regs.a);
STATUS_UPDATE_NEGATIVE(tmp);
} }
static void static void
@ -629,8 +612,7 @@ ror_acc(void)
regs.a |= regs.status.carry << 7; regs.a |= regs.status.carry << 7;
regs.status.carry = carry; regs.status.carry = carry;
STATUS_UPDATE_ZERO(regs.a); STATUS_UPDATE_NZ(regs.a);
STATUS_UPDATE_NEGATIVE(regs.a);
} }
static void static void
@ -643,8 +625,7 @@ ror(uint16_t mem)
memwrite(mem, tmp); memwrite(mem, tmp);
regs.status.carry = carry; regs.status.carry = carry;
STATUS_UPDATE_ZERO(tmp); STATUS_UPDATE_NZ(tmp);
STATUS_UPDATE_NEGATIVE(tmp);
} }
static void static void
@ -717,8 +698,7 @@ tax(void)
{ {
regs.x = regs.a; regs.x = regs.a;
STATUS_UPDATE_ZERO(regs.x); STATUS_UPDATE_NZ(regs.x);
STATUS_UPDATE_NEGATIVE(regs.x);
} }
static void static void
@ -726,8 +706,7 @@ tay(void)
{ {
regs.y = regs.a; regs.y = regs.a;
STATUS_UPDATE_ZERO(regs.y); STATUS_UPDATE_NZ(regs.y);
STATUS_UPDATE_NEGATIVE(regs.y);
} }
static void static void
@ -736,8 +715,7 @@ tsx(void)
// regs.x = PULL(); // regs.x = PULL();
regs.x = regs.sp; regs.x = regs.sp;
STATUS_UPDATE_ZERO(regs.x); STATUS_UPDATE_NZ(regs.x);
STATUS_UPDATE_NEGATIVE(regs.x);
} }
static void static void
@ -745,8 +723,7 @@ txa(void)
{ {
regs.a = regs.x; regs.a = regs.x;
STATUS_UPDATE_ZERO(regs.a); STATUS_UPDATE_NZ(regs.a);
STATUS_UPDATE_NEGATIVE(regs.a);
} }
static void static void
@ -761,8 +738,7 @@ tya(void)
{ {
regs.a = regs.y; regs.a = regs.y;
STATUS_UPDATE_ZERO(regs.a); STATUS_UPDATE_NZ(regs.a);
STATUS_UPDATE_NEGATIVE(regs.a);
} }
static void static void