-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCPU.h
113 lines (89 loc) · 3.14 KB
/
CPU.h
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
#pragma once
//stuff for the disassembler
#include <vector>
#include <string>
#include <map>
//needed for the uintX_t types
#include <cstdint>
class Bus;
class CPU
{
public:
CPU();
~CPU();
//Registers and flags
enum FLAGS2A03
{
C = (1 << 0), // Carry
Z = (1 << 1), // Zero
I = (1 << 2), // Interrupts
D = (1 << 3), // Decimal Mode (NES doesn't support this!)
B = (1 << 4), // Break - Set by IRQ (0), NMI (0), PHP(1), and BRK (1)
U = (1 << 5), // Unused, ALWAYS 1
V = (1 << 6), // Overflow
N = (1 << 7), // Negative
};
uint8_t a = 0x00; // Accumulator
uint8_t x = 0x00; // X Register
uint8_t y = 0x00; // Y Register
uint8_t sp = 0x00; // Stack Pointer
uint16_t pc = 0x000; // Program Counter
uint8_t status = 0x00; // status register... Flags?
private:
Bus *bus = nullptr;
uint8_t cpuRead(uint16_t addr);
void cpuWrite(uint16_t addr, uint8_t data);
private:
//Helper functions to access the status register
uint8_t GetFlag(FLAGS2A03 f);
void SetFlag(FLAGS2A03 f, bool v);
struct INSTRUCTION
{
std::string name;
uint8_t(CPU::* operate)(void) = nullptr;
uint8_t(CPU::* addrmode)(void) = nullptr;
uint8_t cycles = 0;
};
std::vector<INSTRUCTION> lookup; //lookup table for opcodes, probably similar to Dictionary usage in C#.
//helpers
uint8_t fetch();
uint8_t fetched = 0x00;
uint16_t temp = 0x0000; //stores a value for weird stuff that doesn't store the result of the operation it performs
uint16_t addr_abs = 0x0000; //absolute mode address
uint16_t addr_rel = 0x00; //relative address offset
uint8_t opcode = 0x00; //current opcode
uint8_t cycles = 0; //remaining cycles of the current opcode
public:
void ConnectBus(Bus *n) { bus = n; }
// Addressing Modes
uint8_t IMP(); uint8_t IMM();
uint8_t ZP0(); uint8_t ZPX();
uint8_t ZPY(); uint8_t REL();
uint8_t ABS(); uint8_t ABX();
uint8_t ABY(); uint8_t IND();
uint8_t IZX(); uint8_t IZY();
//opcodes
uint8_t ADC(); uint8_t AND(); uint8_t ASL(); uint8_t BCC();
uint8_t BCS(); uint8_t BEQ(); uint8_t BIT(); uint8_t BMI();
uint8_t BNE(); uint8_t BPL(); uint8_t BRK(); uint8_t BVC();
uint8_t BVS(); uint8_t CLC(); uint8_t CLD(); uint8_t CLI();
uint8_t CLV(); uint8_t CMP(); uint8_t CPX(); uint8_t CPY();
uint8_t DEC(); uint8_t DEX(); uint8_t DEY(); uint8_t EOR();
uint8_t INC(); uint8_t INX(); uint8_t INY(); uint8_t JMP();
uint8_t JSR(); uint8_t LDA(); uint8_t LDX(); uint8_t LDY();
uint8_t LSR(); uint8_t NOP(); uint8_t ORA(); uint8_t PHA();
uint8_t PHP(); uint8_t PLA(); uint8_t PLP(); uint8_t ROL();
uint8_t ROR(); uint8_t RTI(); uint8_t RTS(); uint8_t SBC();
uint8_t SEC(); uint8_t SED(); uint8_t SEI(); uint8_t STA();
uint8_t STX(); uint8_t STY(); uint8_t TAX(); uint8_t TAY();
uint8_t TSX(); uint8_t TXA(); uint8_t TXS(); uint8_t TYA();
uint8_t XXX(); //unknown Opcode
void clock();
void reset();
void irq();
void nmi();
uint32_t clock_count = 0; //cycles since emu start
public: //disassembler stuff
bool complete();
std::map<uint16_t, std::string> disassemble(uint16_t nStart, uint16_t nStop);
};