-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
144 lines (117 loc) · 3.26 KB
/
Copy pathmain.cpp
File metadata and controls
144 lines (117 loc) · 3.26 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <iostream>
#include <bitset>
#include <string>
#include <fstream>
#include <helper.hpp>
#include <R6502Mem.hpp>
#include <R6502Bus.hpp>
#include <R6502.hpp>
void loadHexProgram(std::string fileName, R6502Mem *memory, uint32_t writeOffset)
{
if (memory == nullptr)
{
ERROR(loadHexProgram, "FATAL: memory cannot be null");
exit(1);
}
if (writeOffset > memory->size)
{
ERROR(loadHexProgram, "Write offset cannot be larger than memory size: " << memory->size << " offset: " << writeOffset);
exit(1);
}
std::ifstream in(fileName);
if (!in)
{
ERROR(loadHexProgram, "Failed to load file: " << fileName);
exit(1);
}
while (!in.eof())
{
std::string byte;
in >> byte;
memory->memory[writeOffset++] = std::stoi(byte, 0, 16);
}
in.close();
}
void loadBinProgram(std::string fileName, R6502Mem *memory, uint32_t writeOffset)
{
if (memory == nullptr)
{
ERROR(loadBinProgram, "FATAL: memory cannot be null");
exit(1);
}
if (writeOffset > memory->size)
{
ERROR(loadBinProgram, "Write offset cannot be larger than memory size: " << memory->size << " offset: " << writeOffset);
exit(1);
}
std::ifstream in(fileName, std::ios::binary);
if (!in)
{
ERROR(loadBinProgram, "Failed to load file: " << fileName);
exit(1);
}
while (!in.eof())
{
in >> memory->memory[writeOffset];
writeOffset++;
}
in.close();
}
// debug Bus memory to run simple programs
class R6502MemBus : public R6502Bus
{
public:
R6502Mem memory{0xFFFF, 0};
uint8_t read(uint32_t address)
{
return memory.read(address);
}
void write(uint32_t address, uint8_t data)
{
memory.write(address, data);
}
};
int main()
{
R6502 cpu{std::make_unique<R6502MemBus>()};
R6502MemBus *bus = static_cast<R6502MemBus *>(cpu.getBus());
R6502Mem *memory = &(bus->memory);
loadHexProgram("./programs/interrupt.hex", memory, 0x600);
bus->write(0xFFFA, 0x11);
bus->write(0xFFFB, 0x06);
bus->write(0xFFFE, 0x0A);
bus->write(0xFFFF, 0x06);
cpu.IP = 0x600;
LOG(ProgramMemory, "Before start");
memory->renderMemory();
std::cout << "Auto run?(y/n): ";
char ch1 = 0;
std::cin >> ch1;
bool autoRun = ch1 == 'y';
char ch = 0;
do
{
std::cout << std::dec << "\e[H\e[2JInstruction: " << R6502::Instructions[cpu.instr].op << " IP: " << cpu.IP << '\n';
cpu.clock();
std::cout << "CPU STATE:: X: " << static_cast<int>(cpu.reg_X) << " Y: " << static_cast<int>(cpu.reg_Y) << " Acc: " << static_cast<int>(cpu.reg_Acc) << " Status: " << std::bitset<8>(cpu.reg_Status) << '\n';
memory->renderMemory(512);
std::cout << "\n> ";
if (autoRun)
{
ch = 's';
}
else
{
std::cin >> ch;
}
if (ch == 'i')
{
cpu.IRQ(); // raise an interrupt
}
else if (ch == 'n')
{
cpu.NMI();
}
} while (ch != 'q');
return 0;
}