added interrupts
This commit is contained in:
parent
56c9dfbe6e
commit
034645154d
|
@ -37,6 +37,9 @@ struct CPU* createCPU(struct Bus* parent)
|
||||||
cpu->remainingCycles = 7;
|
cpu->remainingCycles = 7;
|
||||||
cpu->totalCycles = 0;
|
cpu->totalCycles = 0;
|
||||||
|
|
||||||
|
cpu->irq = 0;
|
||||||
|
cpu->nmi = 0;
|
||||||
|
|
||||||
cpu->bus = parent;
|
cpu->bus = parent;
|
||||||
return cpu;
|
return cpu;
|
||||||
}
|
}
|
||||||
|
@ -54,6 +57,40 @@ int tickCPU(struct CPU* cpu)
|
||||||
cpu->remainingCycles--;
|
cpu->remainingCycles--;
|
||||||
cpu->totalCycles += 1;
|
cpu->totalCycles += 1;
|
||||||
|
|
||||||
|
if (cpu->remainingCycles == 1)
|
||||||
|
{
|
||||||
|
// Handle interrupts
|
||||||
|
if (cpu->nmi)
|
||||||
|
{
|
||||||
|
Push(cpu->bus, cpu->pc.hi);
|
||||||
|
Push(cpu->bus, cpu->pc.lo);
|
||||||
|
Push(cpu->bus, cpu->status.raw & 0b11101111);
|
||||||
|
|
||||||
|
cpu->nmi = 0;
|
||||||
|
|
||||||
|
cpu->pc.lo = readBus(cpu->bus, 0xFFFA);
|
||||||
|
cpu->pc.hi = readBus(cpu->bus, 0xFFFB);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!cpu->status.id)
|
||||||
|
{
|
||||||
|
if (cpu->irq)
|
||||||
|
{
|
||||||
|
Push(cpu->bus, cpu->pc.hi);
|
||||||
|
Push(cpu->bus, cpu->pc.lo);
|
||||||
|
Push(cpu->bus, cpu->status.raw & 0b11101111);
|
||||||
|
|
||||||
|
cpu->status.id = 1;
|
||||||
|
cpu->pc.lo = readBus(cpu->bus, 0xFFFE);
|
||||||
|
cpu->pc.hi = readBus(cpu->bus, 0xFFFF);
|
||||||
|
|
||||||
|
cpu->irq = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (cpu->remainingCycles == 0)
|
if (cpu->remainingCycles == 0)
|
||||||
{
|
{
|
||||||
fetch(cpu);
|
fetch(cpu);
|
||||||
|
@ -189,7 +226,7 @@ void fetch(struct CPU* cpu)
|
||||||
|
|
||||||
void execute(struct CPU* cpu)
|
void execute(struct CPU* cpu)
|
||||||
{
|
{
|
||||||
LOG_BUS(cpu->bus);
|
// LOG_BUS(cpu->bus);
|
||||||
|
|
||||||
switch (cpu->currentOpcode->op)
|
switch (cpu->currentOpcode->op)
|
||||||
{
|
{
|
||||||
|
@ -795,3 +832,13 @@ void execute(struct CPU* cpu)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void IRQ(struct CPU* cpu)
|
||||||
|
{
|
||||||
|
cpu->irq = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void NMI(struct CPU* cpu)
|
||||||
|
{
|
||||||
|
cpu->nmi = 1;
|
||||||
|
}
|
||||||
|
|
|
@ -80,6 +80,9 @@ struct CPU
|
||||||
Word fetchedAddress;
|
Word fetchedAddress;
|
||||||
char fetchedRelAddress;
|
char fetchedRelAddress;
|
||||||
|
|
||||||
|
Byte irq;
|
||||||
|
Byte nmi;
|
||||||
|
|
||||||
const struct Opcode* currentOpcode;
|
const struct Opcode* currentOpcode;
|
||||||
|
|
||||||
struct Bus* bus;
|
struct Bus* bus;
|
||||||
|
@ -94,4 +97,7 @@ void tickInstr(struct CPU* cpu);
|
||||||
void fetch(struct CPU* cpu);
|
void fetch(struct CPU* cpu);
|
||||||
void execute(struct CPU* cpu);
|
void execute(struct CPU* cpu);
|
||||||
|
|
||||||
|
void IRQ(struct CPU* cpu);
|
||||||
|
void NMI(struct CPU* cpu);
|
||||||
|
|
||||||
#endif // _CPU_H_
|
#endif // _CPU_H_
|
|
@ -4,6 +4,7 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <memory.h>
|
#include <memory.h>
|
||||||
#include "bus.h"
|
#include "bus.h"
|
||||||
|
#include "cpu.h"
|
||||||
#include "cartridge.h"
|
#include "cartridge.h"
|
||||||
|
|
||||||
struct PPU* createPPU(struct Bus* parent)
|
struct PPU* createPPU(struct Bus* parent)
|
||||||
|
@ -222,6 +223,11 @@ void ppuWrite(struct PPU* ppu, Word addr, Byte val)
|
||||||
|
|
||||||
int tickPPU(struct PPU* ppu)
|
int tickPPU(struct PPU* ppu)
|
||||||
{
|
{
|
||||||
|
// Do stuff
|
||||||
|
if (ppu->x == 0 && ppu->y == 241)
|
||||||
|
NMI(ppu->bus->cpu);
|
||||||
|
|
||||||
|
// Increment counters
|
||||||
ppu->x++;
|
ppu->x++;
|
||||||
|
|
||||||
if (ppu->x == 341)
|
if (ppu->x == 341)
|
||||||
|
|
Loading…
Reference in a new issue