-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlynxdir.cpp
More file actions
executable file
·517 lines (468 loc) · 16.1 KB
/
Copy pathlynxdir.cpp
File metadata and controls
executable file
·517 lines (468 loc) · 16.1 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
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
/*
Completly rewritten by BS
*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdarg.h>
#include <string.h>
#define VER "2.0.0"
#define stricmp(a,b) strcasecmp(a,b)
#define strnicmp(a,b,c) strncasecmp(a,b,c)
#include "lynxrom.h"
// #define DEBUG 1
#define MODE_BLL false
#define MODE_EPICS true
int verbose;
lynxrom ROM;
char* ROMname;
bool ExtractName(char* fname)
{
int l;
char* c, *d;
l = strlen(fname);
ROMname = new char[l + 5];
strcpy(ROMname, fname);
c = strrchr(ROMname, '.');
d = strrchr(ROMname, '/');
if (c && c > d) *c = 0;
return true;
}
bool ParseMAK(char* fname)
{
char buffer[1000];
FILE* fh;
static bool align = false, mode = MODE_BLL, title = true, skip_bank = false, cont_bank=false;
static int offset = 0;
static int fileadr = 0;
static int addfileoffset = 0;
fh = fopen(fname, "rt");
if (fh == 0) return (false);
while (!feof(fh)) {
char* c;
*buffer = 0;
fgets(buffer, 999, fh);
buffer[999] = 0;
if (buffer[0] == 0 || buffer[0] == ';') continue;
c = buffer;
while (*c) c++;
while (c > buffer) {
if (*c == 0 || *c == 10 || *c == 13 || *c == ' ' || *c == '\t') {
*c = 0;
c--;
continue;
}
break;
}
c = buffer;
while (*c == ' ' || *c == '\t') c++;
if (*c == '#') { // Options
static int newfileoffset;
newfileoffset = 0;
/// ROM file option like:
/// BLOCKSIZE nr
/// DIRSTART nr
/// TROYAN (put troyan horse as file 16) BLL 1024k
/// next FILE options
/// ALIGN
/// TITLE
/// BLL
/// EPYX
/// COPY nr
if (strnicmp(c + 1, "BLOCKSIZE", 9) == 0) {
ROM.SetBlockSize(atoi(c + 10));
} else if (strnicmp(c + 1, "BLOCKLIMIT", 10) == 0) {
ROM.SetBlockLimit(atoi(c + 11));
} else if (strnicmp(c + 1, "DIRSTART", 8) == 0) {
ROM.SetDirStart(atoi(c + 9));
} else if (strnicmp(c + 1, "DIROFFSET", 9) == 0) {
offset = atoi(c + 10);
} else if (strnicmp(c + 1, "NOLOADER", 8) == 0) {
if(verbose) printf("-> Dont write any loader, fill with 0s\n");
ROM.SetLoader(L_NOLOADER);
} else if (strnicmp(c + 1, "NEWMINI_F000", 12) == 0) {
if(verbose) printf("-> Set new Minimal header (+ loader) @ $F000\n");
mode = MODE_EPICS;
ROM.SetMiniHeader(L_MINI_F000);
} else if (strnicmp(c + 1, "NEWMINI_FB68", 12) == 0) {
if(verbose) printf("-> Set new Minimal header (+ loader) @ $FB68\n");
mode = MODE_EPICS;
ROM.SetMiniHeader(L_MINI_FB68);
} else if (strnicmp(c + 1, "HACKAUTO", 8) == 0) {
if(verbose) printf("-> Set HACK header (auto determine)\n");
mode = MODE_EPICS;
ROM.SetHackHeader(L_HACKAUTO, 0);
} else if (strnicmp(c + 1, "HACK512", 7) == 0) {
if(verbose) printf("-> Set HACK512 header\n");
mode = MODE_EPICS;
ROM.SetHackHeader(L_HACK512, 512);
} else if (strnicmp(c + 1, "HACK1024", 8) == 0) {
if(verbose) printf("-> Set HACK1024 header\n");
mode = MODE_EPICS;
ROM.SetHackHeader(L_HACK1024, 1024);
} else if (strnicmp(c + 1, "HACK2048", 8) == 0) {
if(verbose) printf("-> Set HACK2048 header\n");
mode = MODE_EPICS;
ROM.SetHackHeader(L_HACK2048, 2048);
} else if (strnicmp(c + 1, "INTERNAL", 8) == 0) {
if(verbose) if(verbose) printf("-> Use internal bll newloader\n");
mode = MODE_BLL;
ROM.SetInternalLoader();
} else if (strnicmp(c + 1, "AUDIN", 5) == 0) {
if(verbose) printf("-> Set AUDIN cart\n");
ROM.SetAudIn(true);
} else if (strnicmp(c + 1, "BANK2", 5) == 0) {
if(verbose) printf("-> Set BANK2 cart\n");
ROM.SetBank2(true);
} else if (strnicmp(c + 1, "CONT", 4) == 0) {
if(verbose) printf("-> Set Continous Bank\n");
ROM.SetContinueBank(true);
} else if (strnicmp(c + 1, "NOLYX", 5) == 0) {
if(verbose) printf("-> Dont write LYX\n");
ROM.set_write_lyx(false);
} else if (strnicmp(c + 1, "NOLNX", 5) == 0) {
if(verbose) printf("-> Dont write LNX\n");
ROM.set_write_lnx(false);
} else if (strnicmp(c + 1, "LNXROT", 6) == 0) {
int rotation;
rotation = atoi(c + 7);
if(verbose) {
printf("-> Flag Rotate in LNX to ");
switch (rotation) {
case 0: printf("NORM\n"); break;
case 1: printf("LEFT\n"); break;
case 2: printf("RIGHT\n"); break;
default: printf("undefined\n"); break;
}
}
ROM.set_lnxrot(rotation);
} else if (strnicmp(c + 1, "LNXNAME", 7) == 0) {
if(verbose) printf("-> Set Name of Game in LNX %s\n", c + 9);
ROM.set_lnxname(c + 9);
} else if (strnicmp(c + 1, "LNXMANU", 7) == 0) {
if(verbose) printf("-> Set Manufacturer in LNX %s\n", c + 9);
ROM.set_lnxmanu(c + 9);
} else if (strnicmp(c + 1, "TROYAN", 6) == 0) {
if(verbose) printf("-> Set troyan entry (FILE 16)\n");
ROM.SetTroyan();
} else if (strnicmp(c + 1, "TITLEADR", 8) == 0) {
if(verbose) printf("-> Set adress for titlepic\n");
ROM.SetTitleAdr(atoi(c + 9));
} else if (strnicmp(c + 1, "PUTTITLE", 8) == 0) {
if(verbose) printf("-> Use internal titlepic\n");
ROM.AddFile(0, true, align, mode, offset, skip_bank, 0, fileadr);
align = false;
title = false;
skip_bank = false;
offset = 0;
} else if (strnicmp(c + 1, "FILEADR", 7) == 0) {
fileadr = atoi(c + 8);
if(verbose) printf("(not fully supported option!!!) -> Set Default Load Adr for data files to %d\n", fileadr);
} else if (strnicmp(c + 1, "ALIGN", 5) == 0) {
if(verbose) printf("-> Align next\n");
align = true;
} else if (strnicmp(c + 1, "SKIP_BANK", 9) == 0) {
if(verbose) printf("-> Skip Bank next\n");
skip_bank = true;
} else if (strnicmp(c + 1, "BLL", 3) == 0) {
if(verbose) printf("-> Set Dir Mode Bll\n");
mode = MODE_BLL;
} else if (strnicmp(c + 1, "EPYX", 4) == 0) {
if(verbose) printf("-> Set Dir Mode Epyx\n");
mode = MODE_EPICS;
} else if (strnicmp(c + 1, "COPY", 4) == 0) {
int nr=-2;
if(sscanf(c+5,"%d",&nr)!=1) nr=-2;// reference last one
ROM.AddCopy( nr, mode, offset); // fileadr
align = false;
title = false;
offset = 0;
} else if (strnicmp(c + 1, "EMPTY", 5) == 0 || strnicmp(c + 1, "NONE", 4) == 0) {
ROM.AddCopy(-1, mode, offset); // fileadr we fake a copy of
align = false;
title = false;
offset = 0;
} else if (strnicmp(c + 1, "SKIP", 4) == 0) {
newfileoffset = atoi(c + 5);
align = false;
title = false;
offset = 0;
} else if (strnicmp(c + 1, "EE_8BIT", 7) == 0) {
ROM.set_eeprom_8bit();
} else if (strnicmp(c + 1, "EE_16BIT", 8) == 0) {
ROM.set_eeprom_16bit();
} else if (strnicmp(c + 1, "EE_93C46", 8) == 0) {
ROM.set_eeprom_type(EEPROM_93C46);
} else if (strnicmp(c + 1, "EE_93C56", 8) == 0) {
ROM.set_eeprom_type(EEPROM_93C56);
} else if (strnicmp(c + 1, "EE_93C66", 8) == 0) {
ROM.set_eeprom_type(EEPROM_93C66);
} else if (strnicmp(c + 1, "EE_93C76", 8) == 0) {
ROM.set_eeprom_type(EEPROM_93C76);
} else if (strnicmp(c + 1, "EE_93C86", 8) == 0) {
ROM.set_eeprom_type(EEPROM_93C86);
} else if (strnicmp(c + 1, "INCLUDE", 7) == 0) {
if (!ParseMAK(c+9)) {
printf("\nProblems loading file. %s\n", c+9);
exit(1);
}
printf("processed %s\n", c+9);
}else{
printf("== ERROR ===\nUnknown line \"%s\"\n",c);
exit(1000);
}
addfileoffset = newfileoffset;
} else if(*c==0 || *c==';' || *c==10 || *c==13){
// Skip empty / comment line
} else {
ROM.AddFile(c, title, align, mode, offset, skip_bank, addfileoffset, fileadr);
align = false;
title = false;
skip_bank = false;
offset = 0;
addfileoffset = 0;
}
}
fclose(fh);
return (true);
}
char usage[] = {
"LynxDir Generator Version " VER "\n"
" a replacement for the lynxer \n"
"(c) 2010-2017 Bjoern Spruck\n"
" based on the lynxer by Bastian Schick\n"
" It can create ROMs with \n"
" * BLL type 1024 bytes/block, using Troyan Horse\n"
" * EPXY Loader 512/1024/2048 w/o checksum\n"
" * EPXY Loader with BLL type file system\n"
" * CC65/Karri MiniLoader @ $F000 and $FB68\n"
" * or without loader for later encryption\n"
" ... and a few other things\n"
"\nUsage :\n"
"lynxdir [-hvsif01r] batchfile.mak\n"
"lynxdir mainexe.o\n"
"-h this help\n"
"-v verbose\n"
"-f[01r] fill ROM with 0,1,random\n"
"-s dont skip exec header (adds extra 10 bytes per exec)\n"
"-i remove imp (overwrite with random)\n"
"-x dont write LNX\n"
"-y dont write LYX\n"
"all other options should be set in MAK file, see examples!\n"
};
bool add_lnx_header(const char* fn2, int len)
{
struct LNX_STRUCT* ll;
char* m = new char[len];
FILE* fh;
fh = fopen(fn2, "rb");
if (!fh) {
printf("\nCannot read %s.\n", fn2);
exit(1);
}
fread(m, 1, len, fh);
fclose(fh);
char* fn;
fn = new char[strlen(fn2) + 5];
strcpy(fn, fn2);
char* bn;
bn = strrchr(fn, '.');
if (bn) strcpy(bn, ".lnx");
else strcat(bn, ".lnx");
if(verbose) printf("Writing to %s\n", fn);
fh = fopen(fn, "wb+");
if (fh == 0) return (false);
ll = new struct LNX_STRUCT;
memset(ll, 0, sizeof(struct LNX_STRUCT));
strncpy((char*)ll->magic, "LYNX",4);
ll->page_size_bank0 = len >> 8;
if(verbose) printf("using Blocksize of %d bytes\n", ll->page_size_bank0);
// ll->page_size_bank1=0;
ll->version = 1;
bn = strrchr(fn, '/');
if (bn == 0) bn = fn;
bn = strrchr(fn, '\\');
if (bn == 0) bn = fn;
strncpy((char*)ll->cartname, bn, 32);
((char*)ll->cartname)[31] = 0;
strncpy((char*)ll->manufname, "lynxdir (c) B.S.", 16);
((char*)ll->manufname)[15] = 0;
// ll->rotation=0;
if (fwrite(ll, 1, sizeof(struct LNX_STRUCT),
fh) != sizeof(struct LNX_STRUCT)) printf("Error: Couldn't write LNX header for %s !\n", fn);
if (fwrite(m, 1, len, fh) != len) printf("Error: Couldn't write data for %s !\n", fn);
fclose(fh);
delete []m;
delete ll;
return (true);
}
/*************************************************************
*** Main ***
*************************************************************/
int main(int argc, char* argv[])
{
printf("LynxDir Generator Version " VER "\n");
if (argc == 1) {
printf("%s:\n%s", argv[0], usage);
exit(-1);
}
// Handle Parameters
ROM.init();
ROM.init_rom(0, 256, false, false);
int argc_filename;
verbose = 0;
argc_filename = 1;
for (int ii = 1; ii < argc; ii++) {
if (argv[ii][0] != '-') break;
for (int jj = 1; jj < 10; jj++) {
if (argv[ii][jj] == 0) break;
switch (argv[ii][jj]) {
case 'h': {
printf("%s:\n%s", argv[0], usage);
exit(-1);
}
case 'v': {
verbose++;
ROM.set_verbose(verbose);
break;
}
case 'f': {
ROM.set_filler(true);
if(verbose) printf("Fill LYX!\n");
break;
}
case '0': {
ROM.set_filler_zero();
if(verbose) printf("Fill with 0x00!\n");
break;
}
case '1': {
ROM.set_filler_one();
if(verbose) printf("Fill with 0xFF!\n");
break;
}
case 'r': {
ROM.set_fillrand(true);
if(verbose) printf("Fill with random!\n");
break;
}
case 's': {
ROM.set_skipheader(false);
if(verbose) printf("Dont Skip Header!\n");
break;
}
case 'i': {
ROM.set_delimp(true);
if(verbose) printf("Remove IMP!\n");
break;
}
case 'x': {
ROM.set_write_lnx(false);
if(verbose) printf("Dont write LNX!\n");
break;
}
case 'y': {
ROM.set_write_lyx(false);
if(verbose) printf("Dont write LYX!\n");
break;
}
default: {
printf("\nwrong parameter %s\n", argv[ii]);
exit(1);
break;
}
}
}
argc_filename++;
}
if (argc <= argc_filename) {
printf("\nfilename missing\n");
exit(1);
}
// Check if File is homebrew.o, rom image or MAK
{
unsigned int len;
len = ROM.FileLength(argv[argc_filename]);
FILE* fh;
fh = fopen(argv[argc_filename], "rb");
if (!fh) {
printf("\nCannot read %s.\n", argv[argc_filename]);
exit(1);
}
ExtractName(argv[argc_filename]);
unsigned char header[4];
fread(header, 1, 4, fh);
fclose(fh);
if ((len == 128 * 1024 || len == 256 * 1024 || len == 512 * 1024)) {
printf("\n%s is a ROM image. The only thing I could do with that is remove the checksumming and add a LNX header...\n",
argv[argc_filename]);
printf("BUT!!! removing checksumming is not working automatically for all ROMS, therefore, only a LNX header will be added!\n"\
"You might consider using make_lnx for this task next time!\n");
add_lnx_header(argv[argc_filename], len);
exit(0);
} else if (strcasestr(argv[argc_filename], ".lyx")) {
printf("\n%s is a ROM image. The only thing I could do with that is remove the checksumming and add a LNX header...\n",
argv[argc_filename]);
printf("BUT!!! removing checksumming is not working automatically for all ROMS\n" \
"BUT!!! the lyx file has not a vaild size and not info on teh blocksize, therefore nothing can be done!\n"\
"You might consider using make_lnx for this task, but you have to know the blocksize!\n"\
"=> I will quit here!\n");
exit(100);
} else if ((len == 128 * 1024 + sizeof(struct LNX_STRUCT) || len == 256 * 1024 + sizeof(struct LNX_STRUCT)
|| len == 512 * 1024 + sizeof(struct LNX_STRUCT)) ||
(header[0] == 'L' && header[1] == 'Y' && header[2] == 'N' && header[3] == 'X') || strcasestr(argv[argc_filename], ".lyx")) {
printf("\n%s is a LNX image. The only thing I could do with that is removing the checksumming...\n", argv[argc_filename]);
printf("BUT!!! this might not work for every ROM, therefore...\n=> I will quit here!\n");
exit(100);
} else if ((header[0] | header[1]) == 0x88) {
// HOMEBREW.O
printf("\n%s is BLL homebrew.\nusing \"simple\" mode!\n", argv[argc_filename]);
// Shorter and without title picture ... karris cc65 loader to $f000 or $fb68
ROM.SetBlockSize(512);
ROM.SetDirStart(203);
ROM.SetMiniHeader(2);// better use the higher one to allow very large binaries
ROM.AddFile(argv[argc_filename], false, false, true, 0, 0);
// Hacked epyxloader
// ROM.SetBlockSize(512);
// ROM.SetDirStart(410);
// ROM.SetHackHeader(512);
// ROM.AddFile(0,true,false,true,0,0);
// ROM.AddFile(argv[argc_filename],false,false,true,0,0);
} else if ((header[0] | header[1]) == 0x89) {
// HOMEBREW.O but Packed :-(
printf("\n%s is a turbopacked file, not supported for \"simple\" mode.\n", argv[argc_filename]);
exit(100);
} else {
// Seems to be MAK
if (verbose) printf("\nRunning in script mode. (%s)\n", argv[argc_filename]);
{
char* c, nn[65];
c = strrchr(argv[argc_filename], '/');
if (c == 0) c = argv[argc_filename];
strncpy(nn, c, 64);
nn[64] = 0;
c = strrchr(nn, '.');
if (c) *c = 0;
ROM.set_lnxname(nn);
}
if (!ParseMAK(argv[argc_filename])) {
printf("\nProblems loading file. %s\n", argv[argc_filename]);
exit(1);
}
if (verbose) printf("\nMak file parsed. (%s)\n", argv[argc_filename]);
}
}
if (verbose) printf("\nBuilding ROM...\n");
ROM.built();
char* n;
n = new char[strlen(ROMname) + 5];
strcpy(n, ROMname);
strcat(n, ".lyx");
ROM.savelyx(n);
strcpy(n, ROMname);
strcat(n, ".lnx");
ROM.savelnx(n);
delete []n;
return (0);
}