-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCPU.h
More file actions
124 lines (97 loc) · 2.69 KB
/
CPU.h
File metadata and controls
124 lines (97 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include "MMU.h"
#include <fstream>
class CPU {
public:
CPU(MMU& mmu) :
nesTestOutput("nestest.log", std::ofstream::out),
mmu(mmu) {
SPDLOG_INFO("CPU created");
}
~CPU() {
SPDLOG_INFO("CPU destroyed");
}
void PowerOn();
void Reset();
void Execute();
void Run();
void Pause();
enum AddrConstants : Addr {
Addr_Stack = 0x0100,
Addr_IRQ = 0xFFFE,
Addr_Reset = 0xFFFC,
Addr_NMI = 0xFFFA,
Addr_BRK = 0xFFFE
};
private:
#include "InstrTable.h"
void ReadResetVector();
void FetchOperands(AddrMode addrMode, uint8_t opcode, uint16_t instrOffset);
void UpdateOperands(AddrMode addrMode, uint8_t opcode);
bool ShouldPrintOperand(uint8_t opcode);
void ExecInstr(uint8_t opcode);
void UpdateCycleCount(AddrMode addrMode, uint8_t opcode);
void Tick(size_t cycles);
void Push(uint8_t value);
void PushAddr(Addr address);
uint8_t Pop();
Addr PopAddr();
// Instruction string representation for NESTest
std::string instrToStr;
std::ofstream nesTestOutput;
void PrintNESTestLine(Addr instrOffset);
bool running = true;
size_t cycles = 0;
enum StatusFlags {
Flag_Carry = 0,
Flag_Zero = 1,
Flag_InterruptDisable = 2,
Flag_Decimal = 3,
Flag_B4 = 4,
Flag_B5 = 5,
Flag_Overflow = 6,
Flag_Negative = 7
};
std::bitset<8> P; // Processor status
// NVss DIZC
// |||| |||+- Carry
// |||| ||+-- Zero
// |||| |+--- Interrupt Disable
// |||| +---- Decimal
// ||++------ No CPU effect, see: the B flag
// |+-------- Overflow
// +--------- Negative
// Memory
MMU& mmu;
// Registers
uint8_t A; // Accumulator
uint8_t X; // X index
uint8_t Y; // Y index
uint8_t S; // Stack pointer
uint16_t PC; // Program counter
const uint8_t NEGATIVE_BIT = 0b1000'0000;
//const uint16_t CARRY_BIT = 0b1'0000'0000;
const uint8_t OVERFLOW_BIT = 0b0100'0000;
uint8_t operand = 0;
Addr operandAddr = 0;
bool pageCrossed = false;
// Optional values immediately following the opcode, depending on addressing mode
uint8_t imm0 = 0;
uint8_t imm1 = 0;
struct PPUEmu {
uint16_t scanline = 0;
uint16_t cycles = 0;
void Tick(size_t newCycles) {
cycles += newCycles;
if (cycles >= 341) {
cycles -= 341;
scanline++;
}
if (scanline == 241) {
SPDLOG_INFO("PPU::Tick() - VBLANK");
}
if (scanline >= 262) {
scanline -= 262;
}
}
} ppuEmu;
};