1
0
mirror of https://github.com/pevans/erc-c.git synced 2024-10-14 09:23:54 +00:00
erc-c/src/mos6502.exec.c
Peter Evans 1abf0223c8 The value of PC by that point is correct.
Adding 2 skips us ahead farther than we should be going.
2018-01-08 22:25:37 -06:00

70 lines
1.5 KiB
C

/*
* mos6502.exec.c
*
* These instructions concern program execution; things like JMP, JSR,
* BRK, and so forth.
*/
#include "mos6502.h"
#include "mos6502.enums.h"
/*
* The BRK instruction will set the interrupt bit; will push the current
* PC address to the stack; and will advance the counter by 2 positions.
*/
DEFINE_INST(brk)
{
mos6502_push_stack(cpu, cpu->PC);
mos6502_push_stack(cpu, cpu->P);
cpu->P |= MOS_INTERRUPT;
cpu->PC += 2;
}
/*
* A jump is straight forward; whatever the effective address is, that
* is now the new value of the PC register.
*/
DEFINE_INST(jmp)
{
cpu->PC = cpu->last_addr;
}
/*
* Meanwhile, a JSR (or jump to subroutine) is a little more nuanced. We
* record our current position, plus two, to the stack, and jump the
* effective address.
*/
DEFINE_INST(jsr)
{
mos6502_push_stack(cpu, cpu->PC);
cpu->PC = cpu->last_addr;
}
/*
* The NOP instruction is short for no-operation. It does nothing except
* waste cycles (which happens elsewhere).
*/
DEFINE_INST(nop)
{
// do nothing
}
/*
* Here we return from an interrupt, which effectively resets the PC
* register to the last value on the stack.
*/
DEFINE_INST(rti)
{
cpu->P = mos6502_pop_stack(cpu);
cpu->PC = mos6502_pop_stack(cpu);
}
/*
* The RTS instruction (return from subroutine) works the same as the
* RTI instruction, which may or may not be a misconception on my part.
*/
DEFINE_INST(rts)
{
cpu->PC = mos6502_pop_stack(cpu);
}