1
0
mirror of https://github.com/pevans/erc-c.git synced 2025-03-10 22:30:23 +00:00
erc-c/src/mos6502.exec.c

70 lines
1.5 KiB
C
Raw Normal View History

2017-12-02 13:05:53 -06:00
/*
* mos6502.exec.c
2017-12-08 22:12:31 -06:00
*
* These instructions concern program execution; things like JMP, JSR,
* BRK, and so forth.
2017-12-02 13:05:53 -06:00
*/
#include "mos6502.h"
#include "mos6502.enums.h"
2017-12-06 18:01:13 -06:00
/*
* 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.
*/
2017-12-02 13:05:53 -06:00
DEFINE_INST(brk)
{
mos6502_push_stack(cpu, cpu->PC);
mos6502_push_stack(cpu, cpu->P);
2018-01-05 14:18:39 -06:00
cpu->P |= MOS_INTERRUPT;
2017-12-02 13:05:53 -06:00
cpu->PC += 2;
}
2017-12-06 18:01:13 -06:00
/*
* A jump is straight forward; whatever the effective address is, that
* is now the new value of the PC register.
*/
2017-12-02 13:05:53 -06:00
DEFINE_INST(jmp)
{
cpu->PC = cpu->last_addr;
}
2017-12-06 18:01:13 -06:00
/*
* 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.
*/
2017-12-02 13:05:53 -06:00
DEFINE_INST(jsr)
{
mos6502_push_stack(cpu, cpu->PC + 2);
cpu->PC = cpu->last_addr;
}
2017-12-06 18:01:13 -06:00
/*
* The NOP instruction is short for no-operation. It does nothing except
* waste cycles (which happens elsewhere).
*/
2017-12-02 13:05:53 -06:00
DEFINE_INST(nop)
{
// do nothing
}
2017-12-06 18:01:13 -06:00
/*
* Here we return from an interrupt, which effectively resets the PC
* register to the last value on the stack.
*/
2017-12-02 13:05:53 -06:00
DEFINE_INST(rti)
{
cpu->P = mos6502_pop_stack(cpu);
2017-12-02 13:05:53 -06:00
cpu->PC = mos6502_pop_stack(cpu);
}
2017-12-06 18:01:13 -06:00
/*
* The RTS instruction (return from subroutine) works the same as the
* RTI instruction, which may or may not be a misconception on my part.
*/
2017-12-02 13:05:53 -06:00
DEFINE_INST(rts)
{
cpu->PC = mos6502_pop_stack(cpu);
}