-
Notifications
You must be signed in to change notification settings - Fork 379
/
Copy pathhiscore.cpp
538 lines (440 loc) · 14.3 KB
/
hiscore.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
#include "burnint.h"
// A hiscore.dat support module for FBA - written by Treble Winner, Feb 2009
// Updates & fixes by dink and iq_132
#define MAX_CONFIG_LINE_SIZE 48
#define HISCORE_MAX_RANGES 20
UINT32 nHiscoreNumRanges;
#define APPLIED_STATE_NONE 0
#define APPLIED_STATE_ATTEMPTED 1
#define APPLIED_STATE_CONFIRMED 2
struct _HiscoreMemRange
{
UINT32 Loaded, nCpu, Address, NumBytes, StartValue, EndValue, ApplyNextFrame, Applied;
UINT8 *Data;
};
_HiscoreMemRange HiscoreMemRange[HISCORE_MAX_RANGES];
INT32 EnableHiscores;
static INT32 HiscoresInUse;
static INT32 WriteCheck1;
static INT32 LetsTryToApply = 0;
struct cheat_core {
cpu_core_config *cpuconfig;
INT32 nCPU; // which cpu
};
static cheat_core *cheat_ptr;
static cpu_core_config *cheat_subptr;
extern cheat_core *GetCpuCheatRegister(INT32 nCPU);
static inline void cpu_open(INT32 nCpu)
{
cheat_ptr = GetCpuCheatRegister(nCpu);
cheat_subptr = cheat_ptr->cpuconfig;
cheat_subptr->open(cheat_ptr->nCPU);
}
static UINT32 hexstr2num (const char **pString)
{
const char *string = *pString;
UINT32 result = 0;
if (string)
{
for(;;)
{
char c = *string++;
INT32 digit;
if (c>='0' && c<='9')
{
digit = c-'0';
}
else if (c>='a' && c<='f')
{
digit = 10+c-'a';
}
else if (c>='A' && c<='F')
{
digit = 10+c-'A';
}
else
{
if (!c) string = NULL;
break;
}
result = result*16 + digit;
}
*pString = string;
}
return result;
}
static INT32 is_mem_range (const char *pBuf)
{
char c;
for(;;)
{
c = *pBuf++;
if (c == 0) return 0; /* premature EOL */
if (c == ':') break;
}
c = *pBuf; /* character following first ':' */
return (c>='0' && c<='9') ||
(c>='a' && c<='f') ||
(c>='A' && c<='F');
}
static INT32 is_mem_range_new (const char *pBuf)
{
char c;
for(;;)
{
c = *pBuf++;
if (c == 0) return 0; /* premature EOL */
if (c == ':') break;
}
c = *pBuf; /* character following first ':' */
// cpu id - ignore for now
for(;;)
{
c = *pBuf++;
if (c == 0) return 0; /* premature EOL */
if (c == ',') break;
}
c = *pBuf; /* character following first ',' */
// address space - ignore for now
for(;;)
{
c = *pBuf++;
if (c == 0) return 0; /* premature EOL */
if (c == ',') break;
}
c = *pBuf; /* character following second ',' */
return (c>='0' && c<='9') ||
(c>='a' && c<='f') ||
(c>='A' && c<='F');
}
static INT32 cpustr2num(char *pCpu)
{ // all known versions of the first cpu as of May 15, 2017
return (strstr(pCpu, "maincpu") ||
strstr(pCpu, "cpu1") ||
strstr(pCpu, "master") ||
strstr(pCpu, "fgcpu") ||
strstr(pCpu, "cpua") ||
strstr(pCpu, "master_cpu")) ? 0 : 1;
}
static INT32 matching_game_name (const char *pBuf, const char *name)
{
while (*name)
{
if (*name++ != *pBuf++) return 0;
}
return (*pBuf == ':');
}
static INT32 CheckHiscoreAllowed()
{
INT32 Allowed = 1;
if (!EnableHiscores) Allowed = 0;
if (!(BurnDrvGetFlags() & BDF_HISCORE_SUPPORTED)) Allowed = 0;
return Allowed;
}
void HiscoreInit()
{
Debug_HiscoreInitted = 1;
if (!CheckHiscoreAllowed()) return;
HiscoresInUse = 0;
TCHAR szDatFilename[MAX_PATH];
_stprintf(szDatFilename, _T("%shiscore.dat"), szAppHiscorePath);
FILE *fp = _tfopen(szDatFilename, _T("r"));
if (fp) {
char buffer[MAX_CONFIG_LINE_SIZE];
enum { FIND_NAME, FIND_DATA, FETCH_DATA } mode;
mode = FIND_NAME;
while (fgets(buffer, MAX_CONFIG_LINE_SIZE, fp)) {
if (mode == FIND_NAME) {
if (matching_game_name(buffer, BurnDrvGetTextA(DRV_NAME))) {
mode = FIND_DATA;
}
} else {
if (buffer[0] == '@' && buffer[1] == ':') {
if (is_mem_range_new(buffer)) {
if (nHiscoreNumRanges < HISCORE_MAX_RANGES) {
const char *pBuf = buffer;
char cCpu[80];
char *pCpu = &cCpu[0];
// increment pBuf to the address
char c;
for (;;) {
c = *pBuf++;
if (c == ':') break;
}
c = *pBuf; /* character following first ':' */
// cpu id -> cCpu
for(;;)
{
c = *pBuf++;
if (c == ',') { *pCpu++ = '\0'; break; }
else *pCpu++ = c;
}
c = *pBuf; /* character following first ',' */
// address space - ignore for now
for(;;)
{
c = *pBuf++;
if (c == ',') break;
}
c = *pBuf; /* character following second ',' */
// now set the high score data
HiscoreMemRange[nHiscoreNumRanges].Loaded = 0;
HiscoreMemRange[nHiscoreNumRanges].nCpu = cpustr2num(&cCpu[0]);
HiscoreMemRange[nHiscoreNumRanges].Address = hexstr2num(&pBuf);
HiscoreMemRange[nHiscoreNumRanges].NumBytes = hexstr2num(&pBuf);
HiscoreMemRange[nHiscoreNumRanges].StartValue = hexstr2num(&pBuf);
HiscoreMemRange[nHiscoreNumRanges].EndValue = hexstr2num(&pBuf);
HiscoreMemRange[nHiscoreNumRanges].ApplyNextFrame = 0;
HiscoreMemRange[nHiscoreNumRanges].Applied = 0;
HiscoreMemRange[nHiscoreNumRanges].Data = (UINT8*)BurnMalloc(HiscoreMemRange[nHiscoreNumRanges].NumBytes);
memset(HiscoreMemRange[nHiscoreNumRanges].Data, 0, HiscoreMemRange[nHiscoreNumRanges].NumBytes);
#if 1 && defined FBNEO_DEBUG
bprintf(PRINT_IMPORTANT, _T("Hi Score Memory Range %i Loaded (New Format) - CPU %i (%S), Address %x, Bytes %02x, Start Val %x, End Val %x\n"), nHiscoreNumRanges, HiscoreMemRange[nHiscoreNumRanges].nCpu, cCpu, HiscoreMemRange[nHiscoreNumRanges].Address, HiscoreMemRange[nHiscoreNumRanges].NumBytes, HiscoreMemRange[nHiscoreNumRanges].StartValue, HiscoreMemRange[nHiscoreNumRanges].EndValue);
#endif
nHiscoreNumRanges++;
mode = FETCH_DATA;
} else {
break;
}
} else {
if (mode == FETCH_DATA) break;
}
} else {
if (is_mem_range(buffer)) {
if (nHiscoreNumRanges < HISCORE_MAX_RANGES) {
const char *pBuf = buffer;
HiscoreMemRange[nHiscoreNumRanges].Loaded = 0;
HiscoreMemRange[nHiscoreNumRanges].nCpu = hexstr2num(&pBuf);
HiscoreMemRange[nHiscoreNumRanges].Address = hexstr2num(&pBuf);
HiscoreMemRange[nHiscoreNumRanges].NumBytes = hexstr2num(&pBuf);
HiscoreMemRange[nHiscoreNumRanges].StartValue = hexstr2num(&pBuf);
HiscoreMemRange[nHiscoreNumRanges].EndValue = hexstr2num(&pBuf);
HiscoreMemRange[nHiscoreNumRanges].ApplyNextFrame = 0;
HiscoreMemRange[nHiscoreNumRanges].Applied = 0;
HiscoreMemRange[nHiscoreNumRanges].Data = (UINT8*)BurnMalloc(HiscoreMemRange[nHiscoreNumRanges].NumBytes);
memset(HiscoreMemRange[nHiscoreNumRanges].Data, 0, HiscoreMemRange[nHiscoreNumRanges].NumBytes);
#if 1 && defined FBNEO_DEBUG
bprintf(PRINT_IMPORTANT, _T("Hi Score Memory Range %i Loaded - CPU %i, Address %x, Bytes %02x, Start Val %x, End Val %x\n"), nHiscoreNumRanges, HiscoreMemRange[nHiscoreNumRanges].nCpu, HiscoreMemRange[nHiscoreNumRanges].Address, HiscoreMemRange[nHiscoreNumRanges].NumBytes, HiscoreMemRange[nHiscoreNumRanges].StartValue, HiscoreMemRange[nHiscoreNumRanges].EndValue);
#endif
nHiscoreNumRanges++;
mode = FETCH_DATA;
} else {
break;
}
} else {
if (mode == FETCH_DATA) break;
}
}
}
}
fclose(fp);
}
if (nHiscoreNumRanges) HiscoresInUse = 1;
TCHAR szFilename[MAX_PATH];
#ifndef __LIBRETRO__
_stprintf(szFilename, _T("%s%s.hi"), szAppHiscorePath, BurnDrvGetText(DRV_NAME));
#else
_stprintf(szFilename, _T("%s%s.hi"), szAppEEPROMPath, BurnDrvGetText(DRV_NAME));
#endif
fp = _tfopen(szFilename, _T("rb"));
INT32 Offset = 0;
if (fp) {
UINT32 nSize = 0;
while (!feof(fp)) {
fgetc(fp);
nSize++;
}
UINT8 *Buffer = (UINT8*)BurnMalloc(nSize);
fseek(fp, 0, SEEK_SET);
fread((char *)Buffer, 1, nSize, fp);
for (UINT32 i = 0; i < nHiscoreNumRanges; i++) {
for (UINT32 j = 0; j < HiscoreMemRange[i].NumBytes; j++) {
HiscoreMemRange[i].Data[j] = Buffer[j + Offset];
}
Offset += HiscoreMemRange[i].NumBytes;
HiscoreMemRange[i].Loaded = 1;
#if 1 && defined FBNEO_DEBUG
bprintf(PRINT_IMPORTANT, _T("Hi Score Memory Range %i Loaded from file\n"), i);
#endif
}
BurnFree(Buffer);
fclose(fp);
}
WriteCheck1 = 0;
}
void HiscoreReset()
{
#if defined FBNEO_DEBUG
if (!Debug_HiscoreInitted) bprintf(PRINT_ERROR, _T("HiscoreReset called without init\n"));
#endif
if (!CheckHiscoreAllowed() || !HiscoresInUse) return;
WriteCheck1 = 0;
LetsTryToApply = 0;
for (UINT32 i = 0; i < nHiscoreNumRanges; i++) {
HiscoreMemRange[i].ApplyNextFrame = 0;
HiscoreMemRange[i].Applied = APPLIED_STATE_NONE;
/*if (HiscoreMemRange[i].Loaded)*/ {
cpu_open(HiscoreMemRange[i].nCpu);
cheat_subptr->write(HiscoreMemRange[i].Address, (UINT8)~HiscoreMemRange[i].StartValue);
if (HiscoreMemRange[i].NumBytes > 1) cheat_subptr->write(HiscoreMemRange[i].Address + HiscoreMemRange[i].NumBytes - 1, (UINT8)~HiscoreMemRange[i].EndValue);
cheat_subptr->close();
#if 1 && defined FBNEO_DEBUG
bprintf(PRINT_IMPORTANT, _T("Hi Score Memory Range %i Initted\n"), i);
#endif
}
}
}
INT32 HiscoreOkToWrite()
{ // Check if it is ok to write the hiscore data aka. did we apply it at least?
INT32 Ok = 1;
for (UINT32 i = 0; i < nHiscoreNumRanges; i++) {
if (!(HiscoreMemRange[i].Loaded && HiscoreMemRange[i].Applied == APPLIED_STATE_CONFIRMED)) {
Ok = 0;
}
}
#if 1 && defined FBNEO_DEBUG
bprintf(0, _T("Hiscore Write-Check #1 - Applied data: %X\n"), Ok);
#endif
if (Ok)
return 1; // Passed check #1 - already applied hiscore?
// Check #2 - didn't apply high score, but verified the memory locations
#if 1 && defined FBNEO_DEBUG
bprintf(0, _T("Hiscore Write-Check #2 - Memory verified: %X\n"), WriteCheck1);
#endif
return WriteCheck1;
}
INT32 HiscoreOkToApply(INT32 i)
{
if (!(HiscoreMemRange[i].Loaded && HiscoreMemRange[i].Applied == APPLIED_STATE_NONE && HiscoreMemRange[i].ApplyNextFrame)) {
return 0;
}
return 1;
}
INT32 HiscoreOkToApplyAll()
{ // All of the memory locations in the game's entry must be verfied, then applied when they _all_ match up
INT32 Ok = 1;
for (UINT32 i = 0; i < nHiscoreNumRanges; i++) {
if (!HiscoreOkToApply(i)) {
Ok = 0;
}
}
return Ok;
}
void HiscoreApply()
{
#if defined FBNEO_DEBUG
if (!Debug_HiscoreInitted) bprintf(PRINT_ERROR, _T("HiscoreApply called without init\n"));
#endif
if (!CheckHiscoreAllowed() || !HiscoresInUse) return;
UINT8 WriteCheckOk = 0;
for (UINT32 i = 0; i < nHiscoreNumRanges; i++) {
if (HiscoreMemRange[i].Loaded && HiscoreMemRange[i].Applied == APPLIED_STATE_ATTEMPTED) {
INT32 Confirmed = 1;
cpu_open(HiscoreMemRange[i].nCpu);
for (UINT32 j = 0; j < HiscoreMemRange[i].NumBytes; j++) {
if (cheat_subptr->read(HiscoreMemRange[i].Address + j) != HiscoreMemRange[i].Data[j]) {
Confirmed = 0;
}
}
cheat_subptr->close();
if (Confirmed == 1) {
HiscoreMemRange[i].Applied = APPLIED_STATE_CONFIRMED;
#if 1 && defined FBNEO_DEBUG
bprintf(PRINT_IMPORTANT, _T("Applied Hi Score Memory Range %i on frame number %i\n"), i, GetCurrentFrame());
#endif
} else {
HiscoreMemRange[i].Applied = APPLIED_STATE_NONE;
HiscoreMemRange[i].ApplyNextFrame = 1; // Can't apply yet (machine still booting?) - let's try again!
#if 1 && defined FBNEO_DEBUG
bprintf(PRINT_IMPORTANT, _T("Failed attempt to apply Hi Score Memory Range %i on frame number %i\n"), i, GetCurrentFrame());
#endif
}
}
if (HiscoreMemRange[i].Loaded && HiscoreMemRange[i].Applied == APPLIED_STATE_NONE) {
cpu_open(HiscoreMemRange[i].nCpu);
if (cheat_subptr->read(HiscoreMemRange[i].Address) == HiscoreMemRange[i].StartValue && cheat_subptr->read(HiscoreMemRange[i].Address + HiscoreMemRange[i].NumBytes - 1) == HiscoreMemRange[i].EndValue) {
HiscoreMemRange[i].ApplyNextFrame = 1;
}
cheat_subptr->close();
}
if (!HiscoreMemRange[i].Loaded && !WriteCheck1) {
cpu_open(HiscoreMemRange[i].nCpu);
if (cheat_subptr->read(HiscoreMemRange[i].Address) == HiscoreMemRange[i].StartValue && cheat_subptr->read(HiscoreMemRange[i].Address + HiscoreMemRange[i].NumBytes - 1) == HiscoreMemRange[i].EndValue) {
WriteCheckOk++;
}
cheat_subptr->close();
}
}
if (WriteCheckOk == nHiscoreNumRanges) {
#if 1 && defined FBNEO_DEBUG
bprintf(0, _T("Memory Verified - OK to write Hiscore data!\n"));
#endif
WriteCheck1 = 1; // It's OK to write hi-score data for the first time.
}
if (HiscoreOkToApplyAll()) {
// Game has booted - now we can attempt to apply the HS data
LetsTryToApply = 1;
}
for (UINT32 i = 0; i < nHiscoreNumRanges; i++) {
if (LetsTryToApply && HiscoreOkToApply(i)) {
cpu_open(HiscoreMemRange[i].nCpu);
for (UINT32 j = 0; j < HiscoreMemRange[i].NumBytes; j++) {
cheat_subptr->write(HiscoreMemRange[i].Address + j, HiscoreMemRange[i].Data[j]);
}
cheat_subptr->close();
HiscoreMemRange[i].Applied = APPLIED_STATE_ATTEMPTED;
HiscoreMemRange[i].ApplyNextFrame = 0;
}
}
}
void HiscoreExit()
{
#if defined FBNEO_DEBUG
if (!Debug_HiscoreInitted) bprintf(PRINT_ERROR, _T("HiscoreExit called without init\n"));
#endif
if (!CheckHiscoreAllowed() || !HiscoresInUse) {
Debug_HiscoreInitted = 0;
return;
}
if (HiscoreOkToWrite()) {
TCHAR szFilename[MAX_PATH];
#ifndef __LIBRETRO__
_stprintf(szFilename, _T("%s%s.hi"), szAppHiscorePath, BurnDrvGetText(DRV_NAME));
#else
_stprintf(szFilename, _T("%s%s.hi"), szAppEEPROMPath, BurnDrvGetText(DRV_NAME));
#endif
FILE *fp = _tfopen(szFilename, _T("wb"));
if (fp) {
for (UINT32 i = 0; i < nHiscoreNumRanges; i++) {
UINT8 *Buffer = (UINT8*)BurnMalloc(HiscoreMemRange[i].NumBytes + 10);
memset(Buffer, 0, HiscoreMemRange[i].NumBytes + 10);
cpu_open(HiscoreMemRange[i].nCpu);
for (UINT32 j = 0; j < HiscoreMemRange[i].NumBytes; j++) {
Buffer[j] = cheat_subptr->read(HiscoreMemRange[i].Address + j);
}
cheat_subptr->close();
fwrite(Buffer, 1, HiscoreMemRange[i].NumBytes, fp);
BurnFree(Buffer);
}
fclose(fp);
}
} else {
#if 1 && defined FBNEO_DEBUG
bprintf(0, _T("HiscoreExit(): -NOT- ok to write Hiscore data!\n"));
#endif
}
nHiscoreNumRanges = 0;
WriteCheck1 = 0;
for (UINT32 i = 0; i < HISCORE_MAX_RANGES; i++) {
HiscoreMemRange[i].Loaded = 0;
HiscoreMemRange[i].nCpu = 0;
HiscoreMemRange[i].Address = 0;
HiscoreMemRange[i].NumBytes = 0;
HiscoreMemRange[i].StartValue = 0;
HiscoreMemRange[i].EndValue = 0;
HiscoreMemRange[i].ApplyNextFrame = 0;
HiscoreMemRange[i].Applied = 0;
BurnFree(HiscoreMemRange[i].Data);
}
Debug_HiscoreInitted = 0;
}