forked from devinacker/bsnes-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracer.cpp
265 lines (226 loc) · 7.67 KB
/
tracer.cpp
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#include "tracer.moc"
Tracer *tracer;
#include "w32_socket.cpp"
// TODO: demo only: make these checkboxes in the UI or config options
// tracer output info format
// false: binary format (small/faster),
// true: text format (easier to parse / but slower and HUGE))
const bool traceOutputFormatIsText = false;
// where trace output will be sent
// true: listen on a socket port and stream data to a client
// false: output via a logfile on disk
const bool traceOutputMediumIsSocket = true;
#define DEFAULT_TRACE_SERVER_LISTEN_PORT "27015"
void Tracer::outputTrace(const char* buf, int len) {
outputTraceToFile(buf, len);
outputTraceToSocket(buf, len);
}
void Tracer::outputTraceToFile(const char *buf, int len) {
if (!tracefile.open())
return;
// perf: without trask masking, this is SUPER SLOW, grinds the emulation to 2FPS when enabled.
// TODO: there's probably some easy way to improve performance here, like buffering and async IO calls.
// this chews on gigs of data quickly, so whatever you do, be mindful of performance.
//
tracefile.write(reinterpret_cast<const uint8_t *>(buf), len);
if (traceOutputFormatIsText)
tracefile.print("\n");
}
void Tracer::outputTraceToSocket(const char *buf, int len) {
if (!traceServer.IsInitialized())
return;
traceServer.Push((const uint8_t*)buf, len);
}
void Tracer::outputCpuTrace() {
char buf[256]; int len; // TODO: bounds check buf/len, make sure we don't overflow
if (traceOutputFormatIsText) {
SNES::cpu.disassemble_opcode(buf, SNES::cpu.regs.pc, config().debugger.showHClocks); // text
len = strlen(buf) + 1; // null terminator
} else {
SNES::cpu.disassemble_opcode_bin(buf, SNES::cpu.regs.pc, len); // binary
}
outputTrace(buf, len);
}
void Tracer::outputSmpTrace() {
char buf[256]; int len;
if (traceOutputFormatIsText) {
SNES::smp.disassemble_opcode(buf, SNES::cpu.regs.pc);
len = strlen(buf) + 1; // byte size = string + null term
} else {
// TODO: implement // SNES::smp.disassemble_opcode_bin(buf, SNES::cpu.regs.pc, len); // binary
return; // TODO: not supported just yet
}
outputTrace(buf, len);
}
void Tracer::outputSa1Trace() {
char buf[256]; int len;
if (traceOutputFormatIsText) {
SNES::sa1.disassemble_opcode(buf, SNES::cpu.regs.pc, config().debugger.showHClocks);
len = strlen(buf) + 1; // byte size = string + null term
} else {
// TODO: implement // SNES::sa1.disassemble_opcode_bin(buf, SNES::cpu.regs.pc, config().debugger.showHClocks, len); // binary
return; // TODO: not supported just yet
}
outputTrace(buf, len);
}
void Tracer::outputSfxTrace() {
char buf[256]; int len;
if (traceOutputFormatIsText) {
SNES::superfx.disassemble_opcode(buf, SNES::cpu.regs.pc);
len = strlen(buf) + 1; // byte size = string + null term
} else {
// TODO: implement // SNES::superfx.disassemble_opcode_bin(buf, SNES::cpu.regs.pc, len); // binary
return; // TODO: not supported just yet
}
outputTrace(buf, len);
}
void Tracer::outputSgbTrace() {
char buf[256]; int len;
if (traceOutputFormatIsText) {
SNES::supergameboy.disassemble_opcode(buf, SNES::cpu.regs.pc);
len = strlen(buf) + 1; // byte size = string + null term
} else {
// TODO: implement // SNES::supergameboy.disassemble_opcode_bin(buf, SNES::cpu.regs.pc, len); // binary
return; // TODO: not supported just yet
}
outputTrace(buf, len);
}
void Tracer::stepCpu() {
if(traceCpu) {
unsigned addr = SNES::cpu.regs.pc;
if(!traceMask || !(traceMaskCPU[addr >> 3] & (0x80 >> (addr & 7)))) {
outputCpuTrace();
}
traceMaskCPU[addr >> 3] |= 0x80 >> (addr & 7);
}
}
void Tracer::stepSmp() {
if(traceSmp) {
unsigned addr = SNES::smp.regs.pc;
if(!traceMask || !(traceMaskSMP[addr >> 3] & (0x80 >> (addr & 7)))) {
outputSmpTrace();
}
traceMaskSMP[addr >> 3] |= 0x80 >> (addr & 7);
}
}
void Tracer::stepSa1() {
if(traceSa1) {
unsigned addr = SNES::sa1.regs.pc;
if(!traceMask || !(traceMaskSA1[addr >> 3] & (0x80 >> (addr & 7)))) {
outputSa1Trace();
}
traceMaskSA1[addr >> 3] |= 0x80 >> (addr & 7);
}
}
void Tracer::stepSfx() {
if(traceSfx) {
unsigned addr = SNES::superfx.opcode_pc;
if(!traceMask || !(traceMaskSFX[addr >> 3] & (0x80 >> (addr & 7)))) {
outputSfxTrace();
}
traceMaskSFX[addr >> 3] |= 0x80 >> (addr & 7);
}
}
void Tracer::stepSgb() {
if(traceSgb) {
unsigned addr = SNES::supergameboy.opcode_pc;
if(!traceMask || !(traceMaskSGB[addr >> 3] & (0x80 >> (addr & 7)))) {
outputSgbTrace();
}
traceMaskSGB[addr >> 3] |= 0x80 >> (addr & 7);
}
}
void Tracer::resetTraceState() {
tracefile.close();
traceServer.Shutdown();
setTraceState(traceCpu || traceSmp || traceSa1 || traceSfx || traceSgb);
// reset trace masks
if (traceMask)
setTraceMaskState(true);
}
void Tracer::ensureTraceOutputReady() {
if (!traceOutputMediumIsSocket && !tracefile.open()) {
string name = filepath(nall::basename(cartridge.fileName), config().path.data);
name << "-trace.log";
tracefile.open(name, file::mode::write);
}
if (traceOutputMediumIsSocket && !traceServer.IsInitialized()) {
// demo: this blocks the entire UI while waiting for a client to connect (not great, better ways to handle)
traceServer.Init(DEFAULT_TRACE_SERVER_LISTEN_PORT);
}
}
void Tracer::ensureTraceOutputShutdown() {
if (tracefile.open()) {
tracefile.close();
}
if (traceServer.IsInitialized()) {
traceServer.Shutdown();
}
}
void Tracer::setTraceState(bool state) {
if(state && SNES::cartridge.loaded()) {
ensureTraceOutputReady();
} else if(!traceCpu && !traceSmp && !traceSa1 && !traceSfx && !traceSgb) {
ensureTraceOutputShutdown();
}
}
void Tracer::setCpuTraceState(int state) {
traceCpu = (state == Qt::Checked);
setTraceState(traceCpu);
}
void Tracer::setSmpTraceState(int state) {
traceSmp = (state == Qt::Checked);
setTraceState(traceSmp);
}
void Tracer::setSa1TraceState(int state) {
traceSa1 = (state == Qt::Checked);
setTraceState(traceSa1);
}
void Tracer::setSfxTraceState(int state) {
traceSfx = (state == Qt::Checked);
setTraceState(traceSfx);
}
void Tracer::setSgbTraceState(int state) {
traceSgb = (state == Qt::Checked);
setTraceState(traceSgb);
}
void Tracer::setTraceMaskState(bool state) {
traceMask = state;
if(traceMask) {
//flush all bitmasks once enabled
memset(traceMaskCPU, 0x00, (1 << 24) >> 3);
memset(traceMaskSMP, 0x00, (1 << 16) >> 3);
memset(traceMaskSA1, 0x00, (1 << 24) >> 3);
memset(traceMaskSFX, 0x00, (1 << 23) >> 3);
memset(traceMaskSGB, 0x00, (1 << 16) >> 3);
}
}
Tracer::Tracer() {
traceCpu = false;
traceSmp = false;
traceSa1 = false;
traceSfx = false;
traceSgb = false;
traceMask = false;
traceMaskCPU = new uint8_t[(1 << 24) >> 3]();
traceMaskSMP = new uint8_t[(1 << 16) >> 3]();
traceMaskSA1 = new uint8_t[(1 << 24) >> 3]();
traceMaskSFX = new uint8_t[(1 << 23) >> 3]();
traceMaskSGB = new uint8_t[(1 << 16) >> 3]();
SNES::cpu.step_event = { &Tracer::stepCpu, this };
SNES::smp.step_event = { &Tracer::stepSmp, this };
SNES::sa1.step_event = { &Tracer::stepSa1, this };
SNES::superfx.step_event = { &Tracer::stepSfx, this };
SNES::supergameboy.step_event = { &Tracer::stepSgb, this };
}
Tracer::~Tracer() {
delete[] traceMaskCPU;
delete[] traceMaskSMP;
delete[] traceMaskSA1;
delete[] traceMaskSFX;
delete[] traceMaskSGB;
if(tracefile.open()) tracefile.close();
}
void Tracer::flushTraceOutput() {
traceServer.FlushWorkingBuffer();
}