/
write.c
1844 lines (1620 loc) · 50.4 KB
/
write.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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "defines.h"
#include "memory.h"
#include "write.h"
#include "files.h"
#include "analyze.h"
extern struct reference *reference_first, *reference_last;
extern struct label *labels_first, *labels_last;
extern struct object_file *obj_first, *obj_last, *obj_tmp;
extern struct section *sec_first, *sec_last, *sec_hd_first, sec_hd_last;
extern struct stack *stacks_first, *stacks_last;
extern struct slot slots[256];
extern unsigned char *rom, *rom_usage;
extern unsigned char *file_header, *file_footer;
extern int romsize, rombanks, banksize, verbose_mode, section_overwrite, symbol_mode;
extern int pc_bank, pc_full, pc_slot, pc_slot_max, snes_rom_mode;
extern int file_header_size, file_footer_size, *bankaddress, *banks;
extern int memory_file_id, memory_file_id_source, memory_line_number, output_mode;
extern int program_start, program_end, cpu_65816, snes_mode, smc_status;
extern int snes_sramsize;
#if 0
static int _sections_sort(const void *a, const void *b) {
if ((*((struct section **)a))->size < (*((struct section **)b))->size)
/* struct section* aa = *((struct section**)a);
struct section* bb = *((struct section**)b);
printf("comparing %s and %s\n",aa->name,bb->name);
return -strcmp(aa->name, bb->name); */
return 1;
return -1;
}
#endif
static int _sections_sortalpha(const void *a, const void *b) {
struct section* aa = *((struct section**)a);
struct section* bb = *((struct section**)b);
//printf("comparing %s and %s\n",aa->name,bb->name);
#if 0
if(!aa->name) fprintf(stderr,"no aname\n");
if(!bb->name) fprintf(stderr,"no bname\n");
if(strcmp(aa->name, bb->name) == 0) return 0;
if(aa->size < bb->size) return 1;
return -1;
#else
int s = -strcmp(aa->name, bb->name);
/* identical names -> sort by file id, lowest first */
if (!s)
return (aa->file_id - bb->file_id);
else
return s;
#endif
}
int smc_create_and_write(FILE *f) {
int i;
if (f == NULL)
return FAILED;
if (output_mode != OUTPUT_ROM)
return FAILED;
i = romsize/(8*1024);
/* low byte of 8KB page count */
fprintf(f, "%c", i & 0xFF);
/* high byte of 8KB page count */
fprintf(f, "%c", (i>>8) & 0xFF);
/* emulation mode select (?) */
i = 0;
if (snes_rom_mode == SNES_ROM_MODE_HIROM)
i |= (1<<5) | (1<<4);
i |= (snes_sramsize ^ 3) << 2;
fprintf(f, "%c", i);
/* bytes 3 to 7 are reserved, should be zero */
fprintf(f, "%c" ,0);
fprintf(f, "%c" ,0);
fprintf(f, "%c" ,0);
fprintf(f, "%c" ,0);
fprintf(f, "%c" ,0);
/* bytes 8, 9 = ID Code = 0xAA, 0xBB */
fprintf(f, "%c", 0xAA);
fprintf(f, "%c", 0xBB);
/* 0x04 = SWC & SMC Game */
fprintf(f, "%c", 0x04);
/* the rest of the header is zeroes */
for (i = 0; i < 512-11; i++)
fprintf(f, "%c", 0);
return SUCCEEDED;
}
void lookforsections(struct section** sa, int sn, int p, int* lookforsize, int* totalsections)
{
#if 1
struct section* s = sa[p];
*totalsections = 1;
//fprintf(stderr,"section %s size %d\n",s->name,s->size);
*lookforsize = s->size;
int pp = p+1;
if(strncmp(s->name, ".text", 5) == 0) return;
while(pp < sn && !strcmp(s->name, sa[pp]->name) && s->status == sa[pp]->status) {
//fprintf(stderr,"same section %s (%d and %d); size %d\n", s->name, p, pp, sa[pp]->size);
*lookforsize += sa[pp]->size;
pp++; (*totalsections)++;
}
//fprintf(stderr,"total size %d in %d sections\n", *lookforsize, *totalsections);
#else
*lookforsize = sa[p]->size;
*totalsections = 1;
#endif
}
int insert_sections(void) {
struct section *s, **sa;
int d, f, i, j, x, t, q, sn, p;
char *ram_slots[256], *c;
/* initialize ram slots */
for (i = 0; i < 256; i++)
ram_slots[i] = NULL;
/* find all touched slots */
s = sec_first;
while (s != NULL) {
if (s->status == SECTION_STATUS_RAM && ram_slots[s->slot] == NULL) {
ram_slots[s->slot] = malloc(slots[s->slot].size);
if (ram_slots[s->slot] == NULL) {
fprintf(stderr, "INSERT_SECTIONS: Out of memory error.\n");
return FAILED;
}
memset(ram_slots[s->slot], 0, slots[s->slot].size);
}
s = s->next;
}
/* count the sections */
i = 0;
s = sec_first;
while (s != NULL) {
/* no references - skip it */
if (s->alive == YES)
i++;
s = s->next;
}
sn = i;
if (sn == 0)
return SUCCEEDED;
sa = malloc(sizeof(struct section *) * sn);
if (sa == NULL) {
fprintf(stderr, "INSERT_SECTIONS: Out of memory error.\n");
return FAILED;
}
/* insert the sections into an array for sorting */
i = 0;
s = sec_first;
while (s != NULL) {
/* no references - skip it */
if (s->alive == YES)
sa[i++] = s;
s = s->next;
}
/* sort the sections by size, biggest first */
qsort(sa, sn, sizeof(struct section *), _sections_sortalpha); //_sections_sort);
#if 0
for(i=0;i<sn;i++)
{
printf("s %s id %d\n",sa[i]->name,sa[i]->file_id);
}
#endif
/* print the sizes (DEBUG) */
/*
for (d = 0; d < i; d++)
fprintf(stderr, "SIZE: %d\n", sa[d]->size);
*/
int lookforsize; /* total size of the section group */
int totalsections; /* number of sections in this group */
/* ram sections */
p = 0;
while (p < sn) {
s = sa[p];
totalsections = 1;
/* search for free space */
if (s->status == SECTION_STATUS_RAM) {
/* find out if there is more than one section with this name and set
lookforsize and totalsections accordingly */
lookforsections(sa, sn, p, &lookforsize, &totalsections);
c = ram_slots[s->slot];
i = slots[s->slot].size;
t = 0;
for (x = 0; x < i; x++, c++) {
if (*c == 0) {
for (q = 0; x < i && q < lookforsize; x++, q++, c++) {
if (*c != 0)
break;
}
if (q == lookforsize) {
t = 1;
break;
}
}
}
if (t == 0) {
fprintf(stderr, "INSERT_SECTIONS: No room for RAM section \"%s\" (%d bytes) in slot %d.\n", s->name, lookforsize, s->slot);
fprintf(stderr, "c-s %d lfs %d tts %d\n", c-ram_slots[s->slot], lookforsize, totalsections);
return FAILED;
}
/* mark as used */
c = c - lookforsize;
for (i = 0; i < lookforsize; i++, c++)
*c = 1;
c -= lookforsize;
/* set the address of each section in this group */
for (i = 0; i < totalsections; i++) {
sa[p + i]->address = c - ram_slots[s->slot];
c += sa[p + i]->size;
}
/* generate labels for start and end of this ramsection (group) */
struct label* l;
l = malloc(sizeof(struct label));
sprintf(l->name, "__startramsection%s", s->name);
l->file_id = s->file_id;
l->file_id_source = s->file_id_source;
l->linenumber = 0;
l->rom_address = sa[p]->address;
l->section = s->id;
l->section_status = s->status;
l->bank = s->bank;
l->base = s->base;
l->slot = s->slot;
l->status = LABEL_STATUS_LABEL;
l->address = sa[p]->address;
add_label(l);
struct label* ll = malloc(sizeof(struct label));
*ll = *l;
sprintf(ll->name, "__endramsection%s", s->name);
ll->rom_address = sa[p+i-1]->address;
ll->address = sa[p+i-1]->address + sa[p+i-1]->size;
add_label(ll);
}
p += totalsections;
}
/* free tmp memory */
for (i = 0; i < 256; i++) {
if (ram_slots[i] != NULL)
free(ram_slots[i]);
}
/* force sections */
p = 0;
while (p < sn) {
s = sa[p++];
if (s->status == SECTION_STATUS_FORCE) {
memory_file_id = s->file_id;
banksize = banks[s->bank];
pc_bank = s->address;
pc_slot = slots[s->slot].address + pc_bank;
pc_full = pc_bank + bankaddress[s->bank];
pc_slot_max = slots[s->slot].address + slots[s->slot].size;
d = pc_full;
i = d + s->size;
s->output_address = d;
section_overwrite = OFF;
if (i > romsize) {
fprintf(stderr, "%s:%s: INSERT_SECTIONS: Section \"%s\" (%d bytes) goes beyond the ROM size.\n", get_file_name(s->file_id),
get_source_file_name(s->file_id, s->file_id_source), s->name, s->size);
return FAILED;
}
if (s->address + s->size > banksize) {
fprintf(stderr, "%s:%s: INSERT_SECTIONS: Section \"%s\" (%d bytes) overflows from ROM bank %d.\n", get_file_name(s->file_id),
get_source_file_name(s->file_id, s->file_id_source), s->name, s->size, s->bank);
return FAILED;
}
for (; d < i; d++) {
if (rom_usage[d] != 0 && rom[d] != s->data[d - pc_full])
break;
}
if (d == i) {
for (i = 0; i < s->size; i++) {
if (mem_insert_pc(s->data[i], s->slot, s->bank) == FAILED)
return FAILED;
}
}
else {
fprintf(stderr, "%s:%s: INSERT_SECTIONS: No room for section \"%s\" (%d bytes).\n", get_file_name(s->file_id),
get_source_file_name(s->file_id, s->file_id_source), s->name, s->size);
return FAILED;
}
}
}
/* absolute sections */
p = 0;
while (p < sn) {
s = sa[p++];
if (s->status == SECTION_STATUS_ABSOLUTE) {
d = s->address;
s->output_address = d;
section_overwrite = ON;
for (i = 0; i < s->size; i++) {
if (mem_insert(d + i, s->data[i]) == FAILED)
return FAILED;
}
}
}
/* free & semifree sections */
p = 0;
while (p < sn) {
s = sa[p++];
if (s->status == SECTION_STATUS_FREE || s->status == SECTION_STATUS_SEMIFREE) {
pc_bank = s->address;
d = bankaddress[s->bank];
/* align the starting address */
f = (pc_bank + d) % s->alignment;
if (f > 0)
pc_bank += s->alignment - f;
i = FAILED;
while (i == FAILED) {
f = pc_bank;
for (x = 0; pc_bank < banks[s->bank] && rom_usage[pc_bank + d] == 0 && x < s->size; pc_bank++, x++);
if (x == s->size) {
i = SUCCEEDED;
break;
}
if (pc_bank == banks[s->bank]) {
fprintf(stderr, "%s:%s: INSERT_SECTIONS: No room for section \"%s\" (%d bytes) in ROM bank %d.\n", get_file_name(s->file_id),
get_source_file_name(s->file_id, s->file_id_source), s->name, s->size, s->bank);
fprintf(stderr,"banks[s->bank] %d, s->address 0x%x\n", banks[s->bank], s->address);
return FAILED;
}
/* find the next starting address */
f = (pc_bank + d) % s->alignment;
if (f > 0)
pc_bank += s->alignment - f;
for (; pc_bank < banks[s->bank] && rom_usage[pc_bank + d] != 0; pc_bank += s->alignment);
}
memory_file_id = s->file_id;
banksize = banks[s->bank];
pc_bank = f;
pc_slot = slots[s->slot].address + pc_bank;
pc_full = pc_bank + bankaddress[s->bank];
pc_slot_max = slots[s->slot].address + slots[s->slot].size;
s->address = pc_bank;
s->output_address = pc_full;
section_overwrite = OFF;
for (i = 0; i < s->size; i++) {
if (mem_insert_pc(s->data[i], s->slot, s->bank) == FAILED)
return FAILED;
}
}
}
/* superfree sections */
p = 0;
while (p < sn) {
totalsections = 1;
s = sa[p];
if (s->status == SECTION_STATUS_SUPERFREE) {
lookforsections(sa, sn, p, &lookforsize, &totalsections);
/* go through all the banks */
i = FAILED;
f = 0;
for (q = 0; i == FAILED && q < rombanks; q++) {
pc_bank = 0;
d = bankaddress[q];
/* align the starting address */
f = (pc_bank + d) % s->alignment;
if (f > 0)
pc_bank += s->alignment - f;
/* if the slotsize and banksize differ -> try the next bank */
if (banks[q] != slots[s->slot].size)
continue;
while (i == FAILED) {
f = pc_bank;
for (x = 0; pc_bank < banks[q] && rom_usage[pc_bank + d] == 0 && x < lookforsize; pc_bank++, x++);
if (x == lookforsize) {
i = SUCCEEDED;
break;
}
if (pc_bank == banks[q])
break;
/* find the next starting address */
f = (pc_bank + d) % s->alignment;
if (f > 0)
pc_bank += s->alignment - f;
for (; pc_bank < banks[s->bank] && rom_usage[pc_bank + d] != 0; pc_bank += s->alignment);
}
}
if (i == SUCCEEDED) {
/* set bank for each section */
for(i = 0; i < totalsections; i++) sa[p+i]->bank = q-1;
memory_file_id = s->file_id;
banksize = banks[s->bank];
pc_bank = f;
pc_slot = pc_bank;
pc_full = pc_bank + bankaddress[s->bank];
pc_slot_max = slots[s->slot].size;
section_overwrite = OFF;
/* write each section to memory and set its address in memory and in output file correctly */
for(j = 0; j < totalsections; j++) {
//fprintf(stderr,"inserting section %d (%s, align %d) bank %d slot %d\n", j, sa[p+j]->name, sa[p+j]->alignment, sa[p+j]->bank, sa[p+j]->slot);
//fprintf(stderr,"pc_bank %d pc_full %d pc_slot %d\n", pc_bank, pc_full,pc_slot);
sa[p+j]->address = pc_bank;
sa[p+j]->output_address = pc_full;
for (i = 0; i < sa[p+j]->size; i++) {
//fprintf(stderr,"inserting section %s byte %d\n", sa[p+j]->name, i);
if (mem_insert_pc(sa[p+j]->data[i], sa[p+j]->slot, sa[p+j]->bank) == FAILED)
return FAILED;
}
}
/* generate labels for section (group) start and end
(not for text sections) */
if(strncmp(s->name,".text", 5)) { //totalsections > 1) {
struct label* l = malloc(sizeof(struct label));
sprintf(l->name, "__startsection%s", s->name);
l->file_id = s->file_id;
l->file_id_source = s->file_id_source;
l->linenumber = 0;
l->rom_address = s->address;
l->section = s->id;
l->section_status = s->status;
l->bank = s->bank;
l->base = s->base;
l->slot = s->slot;
l->status = LABEL_STATUS_LABEL;
l->address = s->address;
add_label(l);
struct label* ll = malloc(sizeof(struct label));
*ll = *l;
sprintf(ll->name, "__endsection%s", s->name);
ll->rom_address = sa[p+j-1]->address;
ll->address = sa[p+j-1]->address + sa[p+j-1]->size;
add_label(ll);
}
}
else {
fprintf(stderr, "%s:%s: INSERT_SECTIONS: No room for section \"%s\" (%d bytes).\n", get_file_name(s->file_id),
get_source_file_name(s->file_id, s->file_id_source), s->name, s->size);
return FAILED;
}
}
p += totalsections;
}
/* overwrite sections */
p = 0;
while (p < sn) {
s = sa[p++];
if (s->status == SECTION_STATUS_OVERWRITE) {
memory_file_id = s->file_id;
banksize = banks[s->bank];
pc_bank = s->address;
pc_slot = slots[s->slot].address + pc_bank;
pc_full = pc_bank + bankaddress[s->bank];
pc_slot_max = slots[s->slot].address + slots[s->slot].size;
s->output_address = pc_full;
section_overwrite = ON;
if (pc_full + s->size > romsize) {
fprintf(stderr, "%s:%s: INSERT_SECTIONS: Section \"%s\" (%d bytes) goes beyond the ROM size.\n", get_file_name(s->file_id),
get_source_file_name(s->file_id, s->file_id_source), s->name, s->size);
return FAILED;
}
if (s->address + s->size > banksize) {
fprintf(stderr, "%s:%s: INSERT_SECTIONS: Section \"%s\" (%d bytes) overflows from ROM bank %d.\n", get_file_name(s->file_id),
get_source_file_name(s->file_id, s->file_id_source), s->name, s->size, s->bank);
return FAILED;
}
for (i = 0; i < s->size; i++) {
if (mem_insert_pc(s->data[i], s->slot, s->bank) == FAILED)
return FAILED;
}
}
}
free(sa);
return SUCCEEDED;
}
/* transform computation stack definitions to ordinary definitions */
int transform_stack_definitions(void) {
struct label *l;
struct stack *s;
l = labels_first;
while (l != NULL) {
if (l->status == LABEL_STATUS_STACK) {
/* DEBUG
printf("--------------------------------------\n");
printf("name: \"%s\"\n", l->name);
printf("sect: \"%d\"\n", l->section);
printf("slot: \"%d\"\n", l->slot);
printf("status: \"%d\"\n", l->status);
printf("file_id: \"%d\"\n", l->file_id);
printf("value: \"%d\"\n", l->address);
*/
s = stacks_first;
/* find the stack associated with the definition */
while (s != NULL) {
if (s->file_id == l->file_id && s->id == l->address)
break;
s = s->next;
}
/* did we find it? */
if (s == NULL) {
fprintf(stderr, "TRANSFORM_STACK_DEFINITIONS: No computation stack associated with computation definition label \"%s\". This is a fatal internal error. Please send the WLA DX author a bug report.\n", l->name);
return FAILED;
}
/* is it ok? */
if (s->computed == 0) {
fprintf(stderr, "TRANSFORM_STACK_DEFINITIONS: The computation of definition \"%s\" hasn't been solved. This is a fatal internal error. Please send the WLA DX author a bug report.\n", l->name);
return FAILED;
}
/* do the transformation */
l->status = LABEL_STATUS_DEFINE;
l->address = s->result;
}
l = l->next;
}
return SUCCEEDED;
}
int fix_labels(void) {
struct section *s = NULL;
struct label *l, *m;
/* fix labels' addresses */
l = labels_first;
while (l != NULL) {
if (l->status == LABEL_STATUS_LABEL || l->status == LABEL_STATUS_SYMBOL || l->status == LABEL_STATUS_BREAKPOINT) {
if (l->section_status == ON) {
s = sec_first;
while (s != NULL) {
if (s->id == l->section) {
l->bank = s->bank;
l->address += s->address;
l->rom_address = l->address + bankaddress[l->bank];
if (s->status != SECTION_STATUS_ABSOLUTE)
l->address += slots[l->slot].address;
break;
}
s = s->next;
}
}
else {
l->rom_address = l->address + bankaddress[l->bank];
l->address += slots[l->slot].address;
}
}
l = l->next;
}
/* check out if a label exists more than once in a different place */
l = labels_first;
while (l != NULL) {
if (is_label_anonymous(l->name) == FAILED && (l->status == LABEL_STATUS_LABEL || l->status == LABEL_STATUS_DEFINE)) {
m = l->next;
while (m != NULL) {
if (strcmp(m->name, l->name) == 0) {
if (l->address != m->address && !(m->name[0] == '*' || is_label_section_local(m->name) == SUCCEEDED || is_label_file_local(m->name) == SUCCEEDED)) {
if (l->status == LABEL_STATUS_DEFINE)
fprintf(stderr, "%s: FIX_LABELS: Definition \"%s\" was defined more than once.\n", get_file_name(l->file_id), l->name);
else {
fprintf(stderr, "%s:%s:%d: FIX_LABELS: Label \"%s\" was defined more than once.\n", get_file_name(l->file_id),
get_source_file_name(l->file_id, l->file_id_source), l->linenumber, l->name);
fprintf(stderr,"mn %s islsl %d islfl %d\n", m->name, is_label_section_local(m->name) == SUCCEEDED, is_label_file_local(m->name) == SUCCEEDED);
}
return FAILED;
}
}
m = m->next;
}
}
l = l->next;
}
return SUCCEEDED;
}
int fix_references(void) {
struct reference *r;
struct section *s = NULL;
struct label *l, lt;
int i, x;
section_overwrite = OFF;
/* insert references */
r = reference_first;
while (r != NULL) {
x = r->address;
/* search for the section of the reference and fix the address */
if (r->section_status == ON) {
s = sec_first;
while (s != NULL) {
if (s->id == r->section) {
r->bank = s->bank;
x += s->address;
r->address += s->address;
break;
}
s = s->next;
}
/* reference is inside a discarded section? */
if (s != NULL && s->alive == NO) {
r = r->next;
continue;
}
if (s == NULL) {
if (write_bank_header_references(r) == FAILED)
return FAILED;
r = r->next;
continue;
}
}
if (!(r->section_status == ON && s->status == SECTION_STATUS_ABSOLUTE)) {
x += bankaddress[r->bank];
r->address += slots[r->slot].address;
}
/* find the destination */
l = labels_first;
/* request for bank number? */
if (r->name[0] == ':') {
if (is_label_anonymous(&r->name[1]) == SUCCEEDED) {
l = get_closest_anonymous_label(&r->name[1], x, r->file_id, l, r->section_status, r->section);
}
else if (strcmp(&r->name[1], "CADDR") == 0 || strcmp(&r->name[1], "caddr") == 0) {
lt.status = LABEL_STATUS_LABEL;
strcpy(lt.name, &r->name[1]);
lt.address = r->address;
lt.bank = r->bank;
lt.section_status = OFF;
l = <
}
else {
struct label *ll = NULL;
while (l != NULL) {
if (strcmp(l->name, &r->name[1]) == 0 && l->status != LABEL_STATUS_SYMBOL && l->status != LABEL_STATUS_BREAKPOINT) {
if (!ll) {
ll = l;
}
else {
//printf("double label %s file_id %d r %s file_id %d\n", l->name, l->file_id, r->name, r->file_id);
if (is_label_file_local(&r->name[1]) == SUCCEEDED) {
if (l->file_id == r->file_id)
ll = l;
}
else {
fprintf(stderr,"%s:%s:%d: DUPLICATE LABEL: \"%s\" in files \"%s\" and \"%s\".\n",
get_file_name(r->file_id), get_source_file_name(r->file_id, r->file_id_source), r->linenumber,
&r->name[1],get_file_name(l->file_id),get_file_name(ll->file_id));
return FAILED;
}
}
}
l = l->next;
}
l = ll;
}
if (l == NULL) {
fprintf(stderr, "%s:%s:%d: FIX_REFERENCES: Bank number request for an unknown label \"%s\".\n",
get_file_name(r->file_id), get_source_file_name(r->file_id, r->file_id_source), r->linenumber, &r->name[1]);
return FAILED;
}
if (cpu_65816 != 0)
i = get_snes_pc_bank(l) >> 16;
else
i = l->bank;
memory_file_id = r->file_id;
memory_file_id_source = r->file_id_source;
memory_line_number = r->linenumber;
/* direct 16bit */
if (r->type == REFERENCE_TYPE_DIRECT_16BIT || r->type == REFERENCE_TYPE_RELATIVE_16BIT) {
mem_insert_ref(x, i & 0xFF);
mem_insert_ref(x + 1, (i >> 8) & 0xFF);
}
/* direct / relative 8bit with a definition */
else if (l->status == LABEL_STATUS_DEFINE) {
fprintf(stderr, "%s:%s:%d: FIX_REFERENCES: Bank number request for a definition \"%s\"?\n",
get_file_name(r->file_id), get_source_file_name(r->file_id, r->file_id_source), r->linenumber, l->name);
return FAILED;
}
/* direct 24bit */
else if (r->type == REFERENCE_TYPE_DIRECT_24BIT) {
mem_insert_ref(x, i & 0xFF);
mem_insert_ref(x + 1, (i >> 8) & 0xFF);
mem_insert_ref(x + 2, (i >> 16) & 0xFF);
}
/* relative/direct 8bit with a label */
else {
mem_insert_ref(x, i & 0xFF);
}
}
/* normal reference */
else {
if (is_label_anonymous(r->name) == SUCCEEDED) {
l = get_closest_anonymous_label(r->name, x, r->file_id, l, r->section_status, r->section);
}
else if (strcmp(r->name, "CADDR") == 0 || strcmp(r->name, "caddr") == 0) {
lt.status = LABEL_STATUS_DEFINE;
strcpy(lt.name, r->name);
lt.address = r->address;
lt.bank = r->bank;
lt.section_status = OFF;
l = <
}
else {
while (l != NULL) {
if (strcmp(l->name, r->name) != 0 || l->status == LABEL_STATUS_SYMBOL || l->status == LABEL_STATUS_BREAKPOINT)
l = l->next;
else {
/* search for the section of the referencee */
if (is_label_section_local(r->name) == SUCCEEDED || is_label_file_local(r->name) == SUCCEEDED) {
if (l->file_id == r->file_id) {
if (l->section_status != r->section_status) {
l = l->next;
continue;
}
if (is_label_section_local(r->name) == SUCCEEDED && l->section_status == ON && l->section != r->section) {
l = l->next;
continue;
}
}
else {
l = l->next;
continue;
}
}
break;
}
}
}
if (l == NULL) {
fprintf(stderr, "%s:%s:%d: FIX_REFERENCES: Reference to an unknown label \"%s\".\n",
get_file_name(r->file_id), get_source_file_name(r->file_id, r->file_id_source), r->linenumber, r->name);
return FAILED;
}
memory_file_id = r->file_id;
memory_file_id_source = r->file_id_source;
memory_line_number = r->linenumber;
/* direct 16bit */
if (r->type == REFERENCE_TYPE_DIRECT_16BIT) {
i = l->address;
mem_insert_ref(x, i & 0xFF);
mem_insert_ref(x + 1, (i >> 8) & 0xFF);
}
/* direct / relative 8bit with a value definition */
else if (l->status == LABEL_STATUS_DEFINE && (r->type == REFERENCE_TYPE_DIRECT_8BIT || r->type == REFERENCE_TYPE_RELATIVE_8BIT)) {
i = ((int)l->address) & 0xFFFF;
if (i > 255 || i < -128) {
fprintf(stderr, "%s:%s:%d: FIX_REFERENCES: Value ($%x) of \"%s\" is too much to be a 8bit value.\n",
get_file_name(r->file_id), get_source_file_name(r->file_id, r->file_id_source), r->linenumber, i, l->name);
return FAILED;
}
mem_insert_ref(x, i & 0xFF);
}
/* direct 24bit */
else if (r->type == REFERENCE_TYPE_DIRECT_24BIT) {
i = l->address;
if (l->status == LABEL_STATUS_LABEL)
i += get_snes_pc_bank(l);
mem_insert_ref(x, i & 0xFF);
mem_insert_ref(x + 1, (i >> 8) & 0xFF);
mem_insert_ref(x + 2, (i >> 16) & 0xFF);
}
/* relative 8bit with a label */
else if (r->type == REFERENCE_TYPE_RELATIVE_8BIT) {
i = (((int)l->address) & 0xFFFF) - r->address - 1;
if (i < -128 || i > 127) {
fprintf(stderr, "%s:%s:%d: FIX_REFERENCES: Too large distance (%d bytes from $%x to $%x \"%s\") for a 8bit reference.\n",
get_file_name(r->file_id), get_source_file_name(r->file_id, r->file_id_source), r->linenumber, i, r->address, (int)l->address, l->name);
return FAILED;
}
mem_insert_ref(x, i & 0xFF);
}
/* relative 16bit with a label */
else if (r->type == REFERENCE_TYPE_RELATIVE_16BIT) {
i = (((int)l->address) & 0xFFFF) - r->address - 2;
if (i < -32768 || i > 65535) {
fprintf(stderr, "%s:%s:%d: FIX_REFERENCES: Too large distance (%d bytes from $%x to $%x \"%s\") for a 16bit reference.\n",
get_file_name(r->file_id), get_source_file_name(r->file_id, r->file_id_source), r->linenumber, i, r->address, (int)l->address, l->name);
return FAILED;
}
mem_insert_ref(x, i & 0xFF);
mem_insert_ref(x + 1, (i >> 8) & 0xFF);
}
else {
i = ((int)l->address) & 0xFFFF;
if (i > 255) {
fprintf(stderr, "%s:%s:%d: FIX_REFERENCES: Value ($%x) of \"%s\" is too much to be a 8bit value.\n",
get_file_name(r->file_id), get_source_file_name(r->file_id, r->file_id_source), r->linenumber, i, l->name);
return FAILED;
}
mem_insert_ref(x, i & 0xFF);
}
}
r = r->next;
}
return SUCCEEDED;
}
int write_symbol_file(char *outname, unsigned char mode) {
struct section *s;
struct label *l;
char name[256], *p;
FILE *f;
int y;
if (outname == NULL)
return FAILED;
strcpy(name, outname);
p = name;
for (y = 0; y < 255 && *p != '.' && *p != 0; y++, p++);
*(p++) = '.';
*(p++) = 's';
*(p++) = 'y';
*(p++) = 'm';
*p = 0;
f = fopen(name, "wb");
if (f == NULL) {
fprintf(stderr, "MAIN: Error opening file \"%s\".\n", name);
return FAILED;
}
fprintf(f, "; this file was created with wlalink by ville helin <vhelin@iki.fi>.\n");
if (mode == SYMBOL_MODE_NOCA5H) {
/* NO$GMB SYMBOL FILE */
fprintf(f, "; no$gmb symbolic information for \"%s\".\n", outname);
l = labels_first;
while (l != NULL) {
if (is_label_anonymous(l->name) == SUCCEEDED || l->status == LABEL_STATUS_SYMBOL || l->status == LABEL_STATUS_BREAKPOINT) {
l = l->next;
continue;
}
/* skip all dropped section labels */
if (l->section_status == ON) {
s = sec_first;
while (l->section != s->id)
s = s->next;
if (s->alive == NO) {
l = l->next;
continue;
}
}
if (l->status == LABEL_STATUS_LABEL) {
if (snes_mode == 0)
fprintf(f, "%.4x:%.4x %s\n", l->bank, (int)l->address, l->name);
else
fprintf(f, "%.4x:%.4x %s\n", get_snes_pc_bank(l)>>16, (int)l->address, l->name);
}
else
fprintf(f, "0000:%.4x %s\n", (int)l->address, l->name);
l = l->next;
}
}
else {
/* WLA SYMBOL FILE */
fprintf(f, "; wla symbolic information for \"%s\".\n", outname);
/* labels */
l = labels_first;
while (l != NULL) {
if (l->status != LABEL_STATUS_LABEL) {
l = l->next;
continue;
}
break;
}
if (l != NULL) {
fprintf(f, "\n[labels]\n");
l = labels_first;
while (l != NULL) {
if (l->status != LABEL_STATUS_LABEL) {
l = l->next;
continue;
}
if (is_label_anonymous(l->name) == SUCCEEDED) {
l = l->next;
continue;
}
/* skip all dropped section labels */
if (l->section_status == ON) {
s = sec_first;
while (l->section != s->id)
s = s->next;
if (s->alive == NO) {
l = l->next;
continue;
}
}
if (snes_mode == 0)
fprintf(f, "%.4x:%.4x %s\n", l->bank, (int)l->address, l->name);
else
fprintf(f, "%.4x:%.4x %s\n", get_snes_pc_bank(l)>>16, (int)l->address, l->name);
l = l->next;
}
}
/* symbols */
l = labels_first;
while (l != NULL) {
if (l->status != LABEL_STATUS_SYMBOL) {
l = l->next;
continue;
}
break;
}
if (l != NULL) {
fprintf(f, "\n[symbols]\n");
l = labels_first;
while (l != NULL) {
if (l->status != LABEL_STATUS_SYMBOL) {
l = l->next;
continue;
}
if (snes_mode == 0)
fprintf(f, "%.4x:%.4x %s\n", l->bank, (int)l->address, l->name);
else