implement some more instructions and branching?

This commit is contained in:
Vineet K 2024-06-09 16:02:04 +05:30
parent 858cbfba48
commit 4db0a8163e

89
cpu.c
View File

@ -76,6 +76,7 @@ opcode_arg(enum addressing_mode mode)
switch (mode) { switch (mode) {
case AM_IMM: case AM_IMM:
case AM_REL:
val = arg; val = arg;
break; break;
case AM_ZP: case AM_ZP:
@ -128,13 +129,23 @@ adc(uint8_t arg)
static void static void
and(uint8_t arg) and(uint8_t arg)
{ {
/* TODO: complete this */ regs.a &= arg;
STATUS_UPDATE_ZERO(regs.a);
STATUS_UPDATE_NEGATIVE(regs.a);
} }
static void static void
asl(uint8_t arg) asl(uint8_t arg)
{ {
/* TODO: complete this */ uint16_t tmp;
tmp = regs.a << 1;
regs.a = tmp & 0xFF;
regs.status.carry = tmp > 0xFF;
STATUS_UPDATE_ZERO(regs.a);
STATUS_UPDATE_NEGATIVE(regs.a);
} }
static void static void
@ -152,43 +163,51 @@ bbs(uint8_t arg)
static void static void
bcc(uint8_t arg) bcc(uint8_t arg)
{ {
/* TODO: complete this */ if (regs.status.carry == 0)
regs.pc += arg;
} }
static void static void
bcs(uint8_t arg) bcs(uint8_t arg)
{ {
/* TODO: complete this */ if (regs.status.carry == 1)
regs.pc += arg;
} }
static void static void
beq(uint8_t arg) beq(uint8_t arg)
{ {
/* TODO: complete this */ if (regs.status.zero == 1)
regs.pc += arg;
} }
static void static void
bit(uint8_t arg) bit(uint8_t arg)
{ {
/* TODO: complete this */ regs.status.zero = (regs.a & arg) == 0;
regs.status.overflow = (arg & (1 << 6)) > 0;
regs.status.negative = (arg & (1 << 7)) > 0;
} }
static void static void
bmi(uint8_t arg) bmi(uint8_t arg)
{ {
/* TODO: complete this */ if (regs.status.negative == 1)
regs.pc += arg;
} }
static void static void
bne(uint8_t arg) bne(uint8_t arg)
{ {
/* TODO: complete this */ if (regs.status.zero == 0)
regs.pc += arg;
} }
static void static void
bpl(uint8_t arg) bpl(uint8_t arg)
{ {
/* TODO: complete this */ if (regs.status.negative == 0)
regs.pc += arg;
} }
static void static void
@ -198,7 +217,7 @@ bra(uint8_t arg)
} }
static void static void
brk() brk(void)
{ {
/* TODO: push regs.pc and regs.status to stack and load IRQ vector */ /* TODO: push regs.pc and regs.status to stack and load IRQ vector */
regs.status.brk = 1; regs.status.brk = 1;
@ -208,55 +227,75 @@ brk()
static void static void
bvc(uint8_t arg) bvc(uint8_t arg)
{ {
/* TODO: complete this */ if (regs.status.overflow == 0)
regs.pc += arg;
} }
static void static void
bvs(uint8_t arg) bvs(uint8_t arg)
{ {
/* TODO: complete this */ if (regs.status.overflow == 1)
regs.pc += arg;
} }
static void static void
clc(uint8_t arg) clc(void)
{ {
/* TODO: complete this */ regs.status.carry = 0;
} }
static void static void
cld(uint8_t arg) cld(void)
{ {
/* TODO: complete this */ regs.status.decimal_mode = 0;
} }
static void static void
cli(uint8_t arg) cli(void)
{ {
/* TODO: complete this */ regs.status.interrupt_disable = 0;
} }
static void static void
clv(uint8_t arg) clv(void)
{ {
/* TODO: complete this */ regs.status.overflow = 0;
} }
static void static void
cmp(uint8_t arg) cmp(uint8_t arg)
{ {
/* TODO: complete this */ uint8_t tmp;
tmp = regs.a - arg;
regs.status.carry = regs.a >= arg;
regs.status.zero = regs.a == arg;
regs.status.negative = (tmp & (1 << 7)) > 0;
} }
static void static void
cpx(uint8_t arg) cpx(uint8_t arg)
{ {
/* TODO: complete this */ uint8_t tmp;
tmp = regs.x - arg;
regs.status.carry = regs.x >= arg;
regs.status.zero = regs.x == arg;
regs.status.negative = (tmp & (1 << 7)) > 0;
} }
static void static void
cpy(uint8_t arg) cpy(uint8_t arg)
{ {
/* TODO: complete this */ uint8_t tmp;
tmp = regs.y - arg;
regs.status.carry = regs.y >= arg;
regs.status.zero = regs.y == arg;
regs.status.negative = (tmp & (1 << 7)) > 0;
} }
static void static void
@ -290,7 +329,7 @@ inc(uint8_t arg)
} }
static void static void
inx() inx(void)
{ {
regs.x++; regs.x++;
@ -496,7 +535,7 @@ stz(uint8_t arg)
} }
static void static void
tax() tax(void)
{ {
regs.x = regs.a; regs.x = regs.a;