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