forked from Pwedge/wpclp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wpc_lamp_patcher.c
563 lines (509 loc) · 17.6 KB
/
wpc_lamp_patcher.c
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/******************************************************************************
* ** WPC / WPC95 Lamp Matrix Driver Rom patcher **
* Copyright (C) 2010, John Honeycutt
* http://www.emmytech.com/arcade
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*
*****************************************************************************/
/******************************************************************************
* Version 1.0 06/07/2010: Initial Release
*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <ctype.h>
#include <string.h>
#include <stdint.h>
#include <assert.h>
#include "wpc_lamp_patcher.h"
#define VERSION "1.4"
#define DBG(fmt, args...) if (debug) { printf(fmt, ## args); }
#define STRTOHEX(x) strtoul((x), NULL, 16)
#define STRTODEC(x) strtol((x), NULL, 10)
#define MAX_INPUT_FILE_SZ (1024 * 1024) /* 1MB */
#define STRBUF_SZ (256)
int debug = 0;
int force = 0;
int skip_patch = 0;
int extra_delay = 0;
int auto_version = 0;
char strbuf_g[STRBUF_SZ];
/*******************************************************************************
*
******************************************************************************/
void prog_info(void)
{
printf("==============================================================\n");
printf("*** WPC / WPC95 Lamp Matrix Driver Rom patcher ***\n");
printf(" version %s\n", VERSION);
printf(" (c) John Honeycutt, http://emmytech.com/arcade\n");
printf(" [email protected]\n");
printf("This program will update the lamp matrix driver code for WPC &\n");
printf("WPC95 game roms to eliminate 'ghosting' when using LEDs\n");
printf("==============================================================\n\n");
}
/*******************************************************************************
*
******************************************************************************/
void help_exit(char *name)
{
prog_info();
printf("\nsyntax: %s [-f] [-e] [-n] [-a] <input rom filename> <patched rom filename>\n", name);
printf(" '-f' Optional 'force' parameter to continue even if original rom checksum\n");
printf(" is wrong.\n");
printf(" '-n' Optional 'no-patch' parameter to skip the patching phase.\n");
printf(" Can be used to just update rom version and checksum without patching\n");
printf(" the driver.\n");
printf(" '-e' Optional 'extra delay' parameter.\n");
printf(" Without this option a 30uS delay will occur between clearing the\n");
printf(" matrix row/column and writing for the next period.\n");
printf(" (30uS is the delay inserted by the newer williams driver code.)\n");
printf(" Specifying this option causes an extra 26 to 33uS delay to be inserted \n");
printf(" instead of 30uS. The extra delay might help with problem boards.\n");
printf(" '-a' Optional 'automatically set version number' parameter.\n");
printf(" Can be used to automatically set the version number without prompting.\n");
printf(" For ALPHA-NUMERIC version numbers, the ALPHA will be \'%c\'.\n", AUTO_VER_FORMAT_0);
printf(" For MAJOR.MINOR version numbers, the MAJOR will be \'%x\'.\n", AUTO_VER_FORMAT_1);
exit(0);
}
/*******************************************************************************
*
******************************************************************************/
char *get_text(char* prompt, char *text, size_t size)
{
size_t i = 0;
fputs(prompt, stdout);
fflush(stdout);
for ( ;; ) {
int ch = fgetc(stdin);
if ( ch == '\n' || ch == EOF ) {
break;
}
if ( i < size - 1 ) {
text[i++] = ch;
}
}
text[i] = '\0';
return text;
}
/*******************************************************************************
*
******************************************************************************/
int load_file(FILE *fp, unsigned char *bufp, int max_sz)
{
unsigned char *bp = bufp;
int cnt;
while ( (cnt = fread(bp, 1, ONE_KB, fp)) > 0 ) {
bp+=cnt;
}
if ( ferror(fp) ) {
printf("%s: Error while reading input file: cnt=%d (%s)\n", __FUNCTION__, cnt, strerror(errno));
return -1;
}
if ( feof(fp) ) {
DBG("%s: EOF\n", __FUNCTION__);
}
else {
DBG("%s: EOF not set\n", __FUNCTION__);
}
cnt = bp-bufp;
DBG("%s: Read %d (0x%x) bytes\n", __FUNCTION__, cnt, cnt);
return cnt;
}
/*******************************************************************************
*
******************************************************************************/
int write_file(FILE *fp, unsigned char *bufp, int size)
{
int cnt = 0;
unsigned char *bp = bufp;
int wrt_size, wrt_cnt;
while ( cnt < size ) {
if ( (size - cnt) < ONE_KB ) {
wrt_size = size - cnt;
}
else {
wrt_size = ONE_KB;
}
wrt_cnt = fwrite(bp, 1, wrt_size, fp);
if ( wrt_cnt == -1 ) {
printf("\nwrite error: (%s)\n", strerror(errno));
return -1;
}
bp += wrt_cnt;
cnt += wrt_cnt;
}
return cnt;
}
/*******************************************************************************
*
******************************************************************************/
int get_new_rom_version(wpc_rom_t *rom, uint8_t *major, uint8_t *minor)
{
char *userline;
char *ch;
unsigned long val;
printf("\nThe original ROM Version is: REV. ");
if ( rom->ver.version_format == GAME_VER_FORMAT_1) {
printf("%x.%x\n", rom->ver.game_major, rom->ver.game_minor);
}
else {
if ( rom->ver.game_ver_Lx_format == 1 ) {
printf("%c(x)-%x\n", rom->ver.game_ver_char, rom->ver.game_major);
}
else {
printf("%c-%x\n", rom->ver.game_ver_char, rom->ver.game_major);
}
}
if (auto_version == 1) {
printf("Automatically setting ROM Version to: REV. ");
if (rom->ver.version_format == GAME_VER_FORMAT_0) {
*major = AUTO_VER_FORMAT_0;
*minor = rom->ver.game_major;
if (rom->ver.game_ver_Lx_format == 1) {
printf("%c(x)-%x\n", *major, *minor);
} else {
printf("%c-%x\n", *major, *minor);
}
} else if (rom->ver.version_format == GAME_VER_FORMAT_1) {
*major = AUTO_VER_FORMAT_1;
*minor = rom->ver.game_minor;
printf("%x.%x\n", *major, *minor);
} else {
assert(0);
}
return 0;
}
printf("A new version should be assigned to distinguish the new\n");
printf("rom from the original rom.\n");
printf("The format entered should be: ");
if ( rom->ver.version_format == GAME_VER_FORMAT_1) {
printf("<number1>.<number2>\n");
printf("<number1> can be 1 to 99 <number 2> can be 0 to 99\n");
}
else {
if ( rom->ver.game_ver_char == 'P' ) {
// Prototype rom
//
printf("P-<number>\n");
printf("<number> can be 1 to 2f\n");
printf(" example: VERSION>P-12\n");
}
else {
printf("<letter>-<number>\n");
printf(" <letter> can be A to Z except P\n");
printf(" <number> can be 1 to 99\n");
printf(" example: VERSION>M-25\n");
}
}
userline = get_text("\nEnter VERSION>", strbuf_g, STRBUF_SZ);
DBG("Got version: '%s'\n", userline);
if ( rom->ver.version_format == GAME_VER_FORMAT_1) {
if ( (ch = strchr(userline, '.')) == NULL ) {
return -1;
}
if ( (ch - userline) < 1 ) {
return -1;
}
if ( strlen(userline) < (ch - userline + 2)) {
return -1;
}
*ch = '\0';
val = strtol(userline, NULL, 16);
if ( (val < 1) || (val > 0x99) ) {
return -1;
}
*major = val;
val = strtol(ch+1, NULL, 16);
if ( (val < 0) || (val > 0x99) ) {
return -1;
}
*minor = val;
DBG(" new ver: maj=%x min=%x\n", *major, *minor);
}
else {
if ( (ch = strchr(userline, '-')) == NULL ) {
return -1;
}
if ( (ch - userline) < 1 ) {
return -1;
}
if ( strlen(userline) < (ch - userline + 2)) {
return -1;
}
if ( rom->ver.game_ver_char == 'P' ) {
// Prototype rom
//
*major = 'P';
val = strtol(ch+1, NULL, 16);
if ( (val < 1) || (val > 0x2f) ) {
return -1;
}
*minor = val;
}
else {
if ( !(isalpha(userline[0])) || (toupper(userline[0]) == 'P') ) {
return -1;
}
*major = toupper(userline[0]);
val = strtol(ch+1, NULL, 16);
if ( (val < 1) || (val > 0x99) ) {
return -1;
}
*minor = val;
}
DBG(" new ver: letter=%c num=%x(%d)\n", *major, *minor, *minor);
}
return 0;
}
/*******************************************************************************
* Function: main
*
* Description:
*
******************************************************************************/
int main (int argc, char **argv)
{
char *infile = NULL;
char *outfile = NULL;
int opt_error = 0;
unsigned char *fbuf;
FILE *fdi, *fdo;
int fsize, l, m, n, rc;
uint8_t vid1, vid2;
char ch;
wpc_rom_t rom;
int update_not_needed;
// Parse the command line
//
for( n = 1; n < argc; n++ ) {
switch( (int)argv[n][0] ) { // Check for option character.
case '-':
l = strlen( argv[n] );
for( m = 1; m < l; ++m ) { // Scan through options.
ch = (int)argv[n][m];
switch( ch )
{
case 'd':
debug = 1;
break;
case 'f':
force = 1;
break;
case 'e':
extra_delay = 1;
break;
case 'n':
skip_patch = 1;
break;
case 'a':
auto_version = 1;
break;
default:
printf( "Error: unknown option '-%c'\n", ch );
opt_error = 1;
break;
}
}
break;
default:
// Text
if ( infile == NULL ) {
infile = argv[n];
}
else if (outfile == NULL ) {
outfile = argv[n];
}
else {
// Already got all text options
printf( "Error: unexpected string option '%s'\n", argv[n]);
opt_error = 1;
}
break;
}
}
if ( opt_error || (outfile == NULL) ) {
help_exit(argv[0]);
}
prog_info();
if ( strcmp(infile, outfile) == 0 ) {
printf("Error: Input and Output files must have different names\n");
exit(0);
}
DBG("Input file: '%s'\n", infile);
fdi = fopen(infile, "rb");
if (fdi == NULL) {
printf("Error: Failed to open input file: %s (%s)\n", infile, strerror(errno));
exit(1);
}
fbuf = malloc(MAX_INPUT_FILE_SZ);
if ( fbuf == NULL ) {
printf("Error allocating memory: (%s)\n", strerror(errno));
exit(1);
}
// Read the input file
//
fsize = load_file(fdi, fbuf, MAX_INPUT_FILE_SZ);
fclose(fdi);
// Setup the rom struct
//
memset(&rom, 0, sizeof(rom));
rom.image = fbuf;
rom.size = fsize;
DBG("Input ROM Size: %d M-bits (%dK-Bytes)\n\n", rom.size / ROM_SZ_TO_MBIT_DIVISOR, rom.size / ONE_KB);
// Validate input file
//
if ( force ) {
rc = wpc_validate_rom(&rom, WPC_VAL_CSUM_ERR_OK);
}
else {
rc = wpc_validate_rom(&rom, WPC_VAL_FULL_CHECK);
}
if ( rc != 0 ) {
printf("Error: ROM Validation Failed.\n");
wpc_print_rom_info(&rom, infile);
goto err_exit;
}
wpc_print_rom_info(&rom, infile);
if ( rom.cs.csum_valid == 0 ) {
if ( force == 1 ) {
printf("***WARNING***: ROM Checksum Validation Failed.\n");
printf(" 'force' option specified. continuing...\n");
}
else {
goto err_exit;
}
}
// Patch the file
//
if ( skip_patch == 1 ) {
printf("\n***WARNING***: -n option specified.\n");
printf(" Skipping driver patch.\n");
}
else {
printf("\nPatching Lamp Matrix Driver...\n");
rc = wpc_patch_lamp_matrix(&rom, extra_delay, &update_not_needed);
if ( rc != 0 ) {
printf("ERROR: Lamp matrix driver patch failed.\n");
if ( rc == INVALID_OPT_RC ) {
goto err_exit;
}
else {
goto err_exit_with_msg;
}
}
if ( update_not_needed ) {
printf("**** Driver Patch NOT Required ****\n");
printf("This ROM image already contains the updated lamp matrix driver code.\n");
goto err_exit;
}
printf("Done\n");
}
// Get new rom version from the user
//
if ( (get_new_rom_version(&rom, &vid1, &vid2)) != 0 ) {
printf("Invalid ROM Version entered.\n");
exit(1);
}
printf("\n");
// Update the version number and checksum
//
printf("Updating ROM versioning and checksum...\n");
if ( rom.ver.version_format == GAME_VER_FORMAT_1) {
rom.ver.game_major = vid1;
rom.ver.game_minor = vid2;
}
else {
rom.ver.game_ver_char = vid1;
rom.ver.game_major = vid2;
}
rc = wpc_update_rom_version_checksum(&rom);
if ( rc != 0 ) {
printf("Error: ROM version/checksum update failed.\n");
goto err_exit_with_msg;
}
if ( debug ) {
printf("Debug Updated ROM Information:\n");
wpc_print_rom_info(&rom, NULL);
}
// Write the output file
//
fdo = fopen(outfile, "rb");
if (fdo != NULL) {
printf("Error: output file aleady exists: %s\n", outfile);
fclose(fdo);
goto err_exit;
}
fdo = fopen(outfile, "wb");
if (fdo == NULL) {
printf("Error opening output file for writing: %s\n", outfile);
goto err_exit;
}
printf("Writing output file: %s\n", outfile);
rc = write_file(fdo, rom.image, rom.size);
fclose(fdo);
if ( rc != rom.size ) {
printf("Error Writing Output file. returncode=%d\n", rc);
goto err_exit;
}
printf("Updated ROM file write complete.\n");
// Validate the updated ROM
//
printf("Validating Updated ROM file...\n");
fdi = fopen(outfile, "rb");
if (fdi == NULL) {
printf("Error: Failed to open updated file: %s (%s)\n", outfile, strerror(errno));
exit(1);
}
fsize = load_file(fdi, fbuf, MAX_INPUT_FILE_SZ);
fclose(fdi);
memset(&rom, 0, sizeof(rom));
rom.image = fbuf;
rom.size = fsize;
DBG("Updated ROM Size: %d M-bits (%dK-Bytes)\n\n", rom.size / ROM_SZ_TO_MBIT_DIVISOR, rom.size / ONE_KB);
rc = wpc_validate_rom(&rom, WPC_VAL_FULL_CHECK);
if ( rc != 0 ) {
printf("Error: Updated ROM Validation Failed.\n\n");
wpc_print_rom_info(&rom, infile);
goto err_exit;
}
printf("Updated ROM Validation Passed!\n\n");
wpc_print_rom_info(&rom, infile);
printf("Done.\n");
free(fbuf);
exit(0);
err_exit_with_msg:
printf("***********************\n");
printf("The program exited because it had trouble analyzing the rom\n");
printf("file. If you belive your rom is valid and has the ghosting\n");
printf("issue then you can send an email to [email protected]\n");
printf("Please include the version of this program you are running\n");
printf("and the game rom name and version you are trying to patch.\n");
printf("Please attach the rom you are trying to patch to the email.\n");
printf("***********************\n");
err_exit:
free(fbuf);
exit(1);
}