forked from vhelin/wla-dx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpass_2.c
515 lines (427 loc) · 16.1 KB
/
pass_2.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
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "defines.h"
#include "pass_1.h"
#include "pass_2.h"
#include "pass_4.h"
#include "hashmap.h"
#include "printf.h"
#ifdef GB
extern int licenseecodeold;
extern int nintendologo_defined;
extern int name_defined, licenseecodeold_defined, licenseecodenew_defined;
extern int countrycode, countrycode_defined;
extern int version, version_defined;
extern char name[32], licenseecodenew_c1, licenseecodenew_c2;
unsigned char nintendo_logo_dat[] = {
0xCE, 0xED, 0x66, 0x66, 0xCC, 0x0D, 0x00, 0x0B,
0x03, 0x73, 0x00, 0x83, 0x00, 0x0C, 0x00, 0x0D,
0x00, 0x08, 0x11, 0x1F, 0x88, 0x89, 0x00, 0x0E,
0xDC, 0xCC, 0x6E, 0xE6, 0xDD, 0xDD, 0xD9, 0x99,
0xBB, 0xBB, 0x67, 0x63, 0x6E, 0x0E, 0xEC, 0xCC,
0xDD, 0xDC, 0x99, 0x9F, 0xBB, 0xB9, 0x33, 0x3E
};
#endif
#ifdef Z80
extern char sdsctag_name_str[MAX_NAME_LENGTH + 1], sdsctag_notes_str[MAX_NAME_LENGTH + 1], sdsctag_author_str[MAX_NAME_LENGTH + 1];
extern int sdsctag_name_type, sdsctag_notes_type, sdsctag_author_type, sdsc_ma, sdsc_mi;
extern int sdsctag_name_value, sdsctag_notes_value, sdsctag_author_value;
extern int computesmschecksum_defined, sdsctag_defined, smstag_defined;
extern int smsheader_defined, smsversion, smsversion_defined, smsregioncode, smsregioncode_defined;
extern int smsproductcode_defined, smsproductcode1, smsproductcode2, smsproductcode3, smsreservedspace1, smsreservedspace2;
extern int smsromsize, smsromsize_defined;
#endif
#ifdef W65816
extern char name[32];
extern char snesid[4];
extern int snesid_defined, snesromsize;
extern int sramsize_defined, sramsize, country_defined, country;
extern int cartridgetype, cartridgetype_defined, licenseecode_defined, licenseecode;
extern int version_defined, version;
extern int hirom_defined, lorom_defined, slowrom_defined, fastrom_defined, snes_mode, name_defined;
extern int computesneschecksum_defined, exhirom_defined, exlorom_defined;
#endif
extern FILE *file_out_ptr;
extern int ind, inz, rambanks, rambanks_defined, ifdef;
extern int rombanks_defined, rombanks, cartridgetype, cartridgetype_defined;
extern int output_format, romgbc, romsgb, romdmg, max_address;
extern int romtype, verbose_mode, section_status, background_defined, memorymap_defined;
extern int emptyfill_defined, emptyfill, rombankmap_defined, section_id;
extern unsigned char *rom_banks, *rom_banks_usage_table;
extern char mem_insert_action[MAX_NAME_LENGTH*3 + 1024];
char include_directives_name[] = "INCLUDE_DIRECTIVES:";
extern struct section_def *sections_first, *sections_last, *sec_tmp, *sec_next;
#ifdef W65816
void write_snes_cartridge_information(int start);
#endif
int pass_2(void) {
if (ifdef > 0) {
fprintf(stderr, "%x x .ENDIFs are missing.\n", ifdef);
return FAILED;
}
if (verbose_mode == ON)
printf("Directive checks...\n");
if (section_status == ON) {
fprintf(stderr, "%s The section \"%s\" was not closed.\n", include_directives_name, sections_last->name);
return FAILED;
}
if (output_format != OUTPUT_LIBRARY && rombanks_defined == 0 && rombankmap_defined == 0) {
fprintf(stderr, "%s ROMBANKS/ROMBANKMAP wasn't defined.\n", include_directives_name);
return FAILED;
}
#ifdef Z80
/* SMSTAG */
if (smstag_defined != 0) {
/* OBSOLETE: MOVED TO wlalink/compute.c/compute_sms_checksum()
mem_insert_absolute(0x7FF0, 0x54);
mem_insert_absolute(0x7FF1, 0x4D);
mem_insert_absolute(0x7FF2, 0x52);
mem_insert_absolute(0x7FF3, 0x20);
mem_insert_absolute(0x7FF4, 0x53);
mem_insert_absolute(0x7FF5, 0x45);
mem_insert_absolute(0x7FF6, 0x47);
mem_insert_absolute(0x7FF7, 0x41);
*/
}
/* SMSHEADER */
if (smsheader_defined != 0) {
int tag_address = 0x7FF0;
int rs = 0, rc = 4;
if (smsromsize_defined != 0)
rs = smsromsize;
else {
/* try to calculate the correct romsize value */
if (max_address < 16*1024)
rs = 0xA; /* 8KB */
else if (max_address < 32*1024)
rs = 0xB; /* 16KB */
else
rs = 0xC; /* 32KB+ */
}
if (max_address < 0x4000) {
/* let's assume it's a 8KB ROM */
tag_address = 0x1FF0;
}
else if (max_address < 0x8000) {
/* let's assume it's a 16KB ROM */
tag_address = 0x3FF0;
}
if (smsregioncode_defined != 0)
rc = smsregioncode;
/* create a what-we-are-doing message for mem_insert*() warnings/errors */
snprintf(mem_insert_action, sizeof(mem_insert_action), "Writing SMS ROM header bytes");
mem_insert_absolute(tag_address + 0x8, smsreservedspace1);
mem_insert_absolute(tag_address + 0x9, smsreservedspace2);
mem_insert_absolute(tag_address + 0xC, smsproductcode1);
mem_insert_absolute(tag_address + 0xD, smsproductcode2);
mem_insert_absolute(tag_address + 0xE, (smsproductcode3 << 4) | smsversion);
mem_insert_absolute(tag_address + 0xF, (rc << 4) | rs);
}
/* SDSCTAG */
if (sdsctag_defined != 0) {
struct tm *ti;
time_t t;
int da, mo, ye_l, ye_h, ye, x, q;
/* insert string pointers */
if (create_a_new_section_structure() == FAILED)
return FAILED;
strcpy(sec_tmp->name, "!__WLA_SDSCTAG_STRINGS");
sec_tmp->status = SECTION_STATUS_ABSOLUTE;
fprintf(file_out_ptr, "A%d %d ", sec_tmp->id, 0x7FEA);
/* insert the system line (0) */
fprintf(file_out_ptr, "k0 ");
/* the data */
if (sdsctag_author_type == TYPE_VALUE)
fprintf(file_out_ptr, "y%d ", sdsctag_author_value);
else if (sdsctag_author_type == TYPE_LABEL)
fprintf(file_out_ptr, "r%s ", sdsctag_author_str);
else if (sdsctag_author_type == TYPE_STACK_CALCULATION)
fprintf(file_out_ptr, "C%d ", sdsctag_author_value);
else
fprintf(file_out_ptr, "r *WLA_SDSC_PRG_AUTHOR_PTR ");
if (sdsctag_name_type == TYPE_VALUE)
fprintf(file_out_ptr, "y%d ", sdsctag_name_value);
else if (sdsctag_name_type == TYPE_LABEL)
fprintf(file_out_ptr, "r%s ", sdsctag_name_str);
else if (sdsctag_name_type == TYPE_STACK_CALCULATION)
fprintf(file_out_ptr, "C%d ", sdsctag_name_value);
else
fprintf(file_out_ptr, "r *WLA_SDSC_PRG_NAME_PTR ");
if (sdsctag_notes_type == TYPE_VALUE)
fprintf(file_out_ptr, "y%d ", sdsctag_notes_value);
else if (sdsctag_notes_type == TYPE_LABEL)
fprintf(file_out_ptr, "r%s ", sdsctag_notes_str);
else if (sdsctag_notes_type == TYPE_STACK_CALCULATION)
fprintf(file_out_ptr, "C%d ", sdsctag_notes_value);
else
fprintf(file_out_ptr, "r *WLA_SDSC_PRG_NOTES_PTR ");
fprintf(file_out_ptr, "s ");
/* create string sections */
if (sdsctag_author_type == TYPE_STRING) {
if (create_a_new_section_structure() == FAILED)
return FAILED;
strcpy(sec_tmp->name, "!__WLA_SDSCTAG_STRING_AUTHOR");
sec_tmp->status = SECTION_STATUS_SEMIFREE;
fprintf(file_out_ptr, "B0 0 O1 S%d L *WLA_SDSC_PRG_AUTHOR_PTR ", sec_tmp->id);
for (q = 0; q < (int)strlen(sdsctag_author_str); q++)
fprintf(file_out_ptr, "d%d ", sdsctag_author_str[q]);
fprintf(file_out_ptr, "d0 s ");
}
if (sdsctag_name_type == TYPE_STRING) {
if (create_a_new_section_structure() == FAILED)
return FAILED;
strcpy(sec_tmp->name, "!__WLA_SDSCTAG_STRING_NAME");
sec_tmp->status = SECTION_STATUS_SEMIFREE;
fprintf(file_out_ptr, "B0 0 O1 S%d L *WLA_SDSC_PRG_NAME_PTR ", sec_tmp->id);
for (q = 0; q < (int)strlen(sdsctag_name_str); q++)
fprintf(file_out_ptr, "d%d ", sdsctag_name_str[q]);
fprintf(file_out_ptr, "d0 s ");
}
if (sdsctag_notes_type == TYPE_STRING) {
if (create_a_new_section_structure() == FAILED)
return FAILED;
strcpy(sec_tmp->name, "!__WLA_SDSCTAG_STRING_NOTES");
sec_tmp->status = SECTION_STATUS_SEMIFREE;
fprintf(file_out_ptr, "B0 0 O1 S%d L *WLA_SDSC_PRG_NOTES_PTR ", sec_tmp->id);
for (q = 0; q < (int)strlen(sdsctag_notes_str); q++)
fprintf(file_out_ptr, "d%d ", sdsctag_notes_str[q]);
fprintf(file_out_ptr, "d0 s ");
}
/* create the time and date data */
time(&t);
ti = localtime(&t);
da = ti->tm_mday;
mo = ti->tm_mon + 1;
ye = ti->tm_year + 1900;
/* BCD the values */
q = (sdsc_ma / 10);
sdsc_ma = (q << 4) | (sdsc_ma - (q * 10));
q = (sdsc_mi / 10);
sdsc_mi = (q << 4) | (sdsc_mi - (q * 10));
q = (da / 10);
da = (q << 4) | (da - (q * 10));
q = (mo / 10);
mo = (q << 4) | (mo - (q * 10));
q = (ye / 1000);
ye -= (q * 1000);
x = (ye / 100);
ye -= (x * 100);
ye_h = (q << 4) | x;
q = (ye / 10);
ye -= (q * 10);
ye_l = (q << 4) | ye;
if (create_a_new_section_structure() == FAILED)
return FAILED;
strcpy(sec_tmp->name, "!__WLA_SDSCTAG_TIMEDATE");
sec_tmp->status = SECTION_STATUS_ABSOLUTE;
fprintf(file_out_ptr, "A%d %d ", sec_tmp->id, 0x7FE0);
/* insert the system line (0) */
fprintf(file_out_ptr, "k0 ");
/* SDSC header data */
/* SDSC */
fprintf(file_out_ptr, "d%d ", 0x53);
fprintf(file_out_ptr, "d%d ", 0x44);
fprintf(file_out_ptr, "d%d ", 0x53);
fprintf(file_out_ptr, "d%d ", 0x43);
/* version */
fprintf(file_out_ptr, "d%d ", sdsc_ma);
fprintf(file_out_ptr, "d%d ", sdsc_mi);
/* date */
fprintf(file_out_ptr, "d%d ", da);
fprintf(file_out_ptr, "d%d ", mo);
fprintf(file_out_ptr, "d%d ", ye_l);
fprintf(file_out_ptr, "d%d ", ye_h);
fprintf(file_out_ptr, "s ");
/*
mem_insert_absolute(0x7FE0, 0x53);
mem_insert_absolute(0x7FE1, 0x44);
mem_insert_absolute(0x7FE2, 0x53);
mem_insert_absolute(0x7FE3, 0x43);
mem_insert_absolute(0x7FE4, sdsc_ma);
mem_insert_absolute(0x7FE5, sdsc_mi);
mem_insert_absolute(0x7FE6, da);
mem_insert_absolute(0x7FE7, mo);
mem_insert_absolute(0x7FE8, ye_l);
mem_insert_absolute(0x7FE9, ye_h);
*/
}
#endif
#ifdef W65816
if (output_format != OUTPUT_LIBRARY) {
/* snes cartridge information */
if (snes_mode != 0) {
if (hirom_defined != 0)
write_snes_cartridge_information(0xFFD5);
else if (exhirom_defined != 0) {
write_snes_cartridge_information(0x40FFD5);
/* mirror the info */
write_snes_cartridge_information(0xFFD5);
}
else
write_snes_cartridge_information(0x7FD5);
}
}
#endif
#ifdef GB
/* insert the descriptive data (not in library files) */
if (output_format == OUTPUT_OBJECT) {
if (nintendologo_defined != 0) {
int nl1 = 0x104;
unsigned int nl2 = 0;
/* create a what-we-are-doing message for mem_insert*() warnings/errors */
snprintf(mem_insert_action, sizeof(mem_insert_action), "Writing GB ROM Nintendo logo bytes");
while (nl2 < sizeof(nintendo_logo_dat)) {
mem_insert_absolute(nl1, nintendo_logo_dat[nl2]);
nl1++;
nl2++;
}
}
if (romgbc == 1) {
/* create a what-we-are-doing message for mem_insert*() warnings/errors */
snprintf(mem_insert_action, sizeof(mem_insert_action), "Writing GB ROM type: GB Color compatible");
mem_insert_absolute(323, 0x80);
}
else if (romgbc == 2) {
/* create a what-we-are-doing message for mem_insert*() warnings/errors */
snprintf(mem_insert_action, sizeof(mem_insert_action), "Writing GB ROM type: GB Color only");
mem_insert_absolute(323, 0xc0);
}
if (romdmg != 0) {
/* create a what-we-are-doing message for mem_insert*() warnings/errors */
snprintf(mem_insert_action, sizeof(mem_insert_action), "Writing GB ROM type: GB original");
mem_insert_absolute(326, 0);
}
if (romsgb != 0) {
/* create a what-we-are-doing message for mem_insert*() warnings/errors */
snprintf(mem_insert_action, sizeof(mem_insert_action), "Writing GB ROM type: Super GB");
mem_insert_absolute(326, 3);
}
if (rambanks_defined != 0) {
/* create a what-we-are-doing message for mem_insert*() warnings/errors */
snprintf(mem_insert_action, sizeof(mem_insert_action), "Writing GB RAM banks");
mem_insert_absolute(329, rambanks);
}
if (rombanks_defined != 0) {
/* create a what-we-are-doing message for mem_insert*() warnings/errors */
snprintf(mem_insert_action, sizeof(mem_insert_action), "Writing GB ROM banks");
mem_insert_absolute(328, romtype);
}
if (cartridgetype_defined != 0) {
/* create a what-we-are-doing message for mem_insert*() warnings/errors */
snprintf(mem_insert_action, sizeof(mem_insert_action), "Writing GB cartridge type");
mem_insert_absolute(327, cartridgetype);
}
if (licenseecodeold_defined != 0) {
/* create a what-we-are-doing message for mem_insert*() warnings/errors */
snprintf(mem_insert_action, sizeof(mem_insert_action), "Writing GB ROM licensee code (old)");
mem_insert_absolute(331, licenseecodeold);
mem_insert_absolute(324, 0);
mem_insert_absolute(325, 0);
}
if (licenseecodenew_defined != 0) {
/* create a what-we-are-doing message for mem_insert*() warnings/errors */
snprintf(mem_insert_action, sizeof(mem_insert_action), "Writing GB ROM licensee code (new)");
mem_insert_absolute(331, 51);
mem_insert_absolute(324, licenseecodenew_c1);
mem_insert_absolute(325, licenseecodenew_c2);
}
if (countrycode_defined != 0) {
/* create a what-we-are-doing message for mem_insert*() warnings/errors */
snprintf(mem_insert_action, sizeof(mem_insert_action), "Writing GB ROM country code");
mem_insert_absolute(330, countrycode);
}
if (name_defined != 0) {
if (romgbc != 0)
inz = 15;
else
inz = 16;
/* create a what-we-are-doing message for mem_insert*() warnings/errors */
snprintf(mem_insert_action, sizeof(mem_insert_action), "Writing GB ROM name");
for (ind = 0; ind < inz; ind++)
mem_insert_absolute(308 + ind, name[ind]);
}
if (version_defined != 0) {
/* create a what-we-are-doing message for mem_insert*() warnings/errors */
snprintf(mem_insert_action, sizeof(mem_insert_action), "Writing GB ROM version");
mem_insert_absolute(332, version);
}
}
#endif
fclose(file_out_ptr);
file_out_ptr = NULL;
/* clear the mem_insert*() warnings/errors buffer */
mem_insert_action[0] = 0;
return SUCCEEDED;
}
#ifdef W65816
void write_snes_cartridge_information(int start) {
inz = 32;
if (hirom_defined != 0)
inz |= 1;
else if (exhirom_defined != 0)
inz |= (1 << 2) | 1;
else if (exlorom_defined != 0)
inz |= (1 << 1);
if (fastrom_defined != 0)
inz += 16;
/* create a what-we-are-doing message for mem_insert*() warnings/errors */
snprintf(mem_insert_action, sizeof(mem_insert_action), "Writing SNES ROM information");
mem_insert_absolute(start, inz);
if (cartridgetype_defined != 0)
mem_insert_absolute(start + 1, cartridgetype);
if (snesromsize != 0)
mem_insert_absolute(start + 2, snesromsize);
if (sramsize_defined != 0)
mem_insert_absolute(start + 3, sramsize);
if (country_defined != 0)
mem_insert_absolute(start + 4, country);
if (licenseecode_defined != 0)
mem_insert_absolute(start + 5, licenseecode);
if (version_defined != 0)
mem_insert_absolute(start + 6, version);
/* snes cartridge ID */
if (snesid_defined != 0) {
for (ind = 0; ind < 4; ind++)
mem_insert_absolute(start - (0xD5 - 0xB2) + ind, snesid[ind]);
}
/* snes cartridge name */
if (name_defined != 0) {
for (ind = 0; ind < 21; ind++)
mem_insert_absolute(start - (0xD5 - 0xC0) + ind, name[ind]);
}
}
#endif
int create_a_new_section_structure(void) {
sec_tmp = calloc(sizeof(struct section_def), 1);
if (sec_tmp == NULL) {
print_error("Out of memory while allocating room for a new SECTION.\n", ERROR_DIR);
return FAILED;
}
sec_tmp->listfile_items = 0;
sec_tmp->listfile_ints = NULL;
sec_tmp->listfile_cmds = NULL;
sec_tmp->maxsize_status = OFF;
sec_tmp->data = NULL;
sec_tmp->next = NULL;
sec_tmp->id = section_id;
sec_tmp->alive = ON;
sec_tmp->advance_org = NO;
sec_tmp->nspace = NULL;
sec_tmp->label_map = hashmap_new();
section_id++;
sec_tmp->filename_id = 0;
sec_tmp->alignment = 1;
sec_tmp->offset = 0;
if (sections_first == NULL) {
sections_first = sec_tmp;
sections_last = sec_tmp;
}
else {
sections_last->next = sec_tmp;
sections_last = sec_tmp;
}
return SUCCEEDED;
}