OpenCores
URL https://opencores.org/ocsvn/open8_urisc/open8_urisc/trunk

Subversion Repositories open8_urisc

[/] [open8_urisc/] [trunk/] [Open8 Tools/] [open8_src/] [open8_link/] [analyze.c] - Blame information for rev 290

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 178 jshamlet
 
2
#include <ctype.h>
3
#include <stdio.h>
4
#include <stdlib.h>
5
#include <string.h>
6
 
7
#include "defines.h"
8
#include "analyze.h"
9
#include "memory.h"
10
#include "listfile.h"
11
 
12
 
13
/* read an integer from t */
14
#define READ_T t[3] + (t[2] << 8) + (t[1] << 16) + (t[0] << 24); t += 4;
15
 
16
/* read a double from t */
17
#define READ_DOU { \
18
  dtmp = (unsigned char *)&dou; \
19
  dtmp[0] = *(t++); \
20
  dtmp[1] = *(t++); \
21
  dtmp[2] = *(t++); \
22
  dtmp[3] = *(t++); \
23
  dtmp[4] = *(t++); \
24
  dtmp[5] = *(t++); \
25
  dtmp[6] = *(t++); \
26
  dtmp[7] = *(t++); \
27
}
28
 
29
 
30
extern struct object_file *obj_first, *obj_last, *obj_tmp;
31
extern struct reference *reference_first, *reference_last;
32
extern struct section *sec_first, *sec_last, *sec_hd_first, *sec_hd_last;
33
extern struct stack *stacks_first, *stacks_last;
34
extern struct label *labels_first, *labels_last;
35
extern struct slot slots[256];
36
extern int rombanks, verbose_mode, section_overwrite, symbol_mode, discard_unreferenced_sections;
37
extern int emptyfill;
38
extern int *banks, *bankaddress;
39
 
40
 
41
 
42
int add_reference(struct reference *r) {
43
 
44
  r->file_id = obj_tmp->id;
45
  r->next = NULL;
46
 
47
  if (reference_first == NULL) {
48
    reference_first = r;
49
    reference_last = r;
50
    r->prev = NULL;
51
  }
52
  else {
53
    r->prev = reference_last;
54
    reference_last->next = r;
55
    reference_last = r;
56
  }
57
 
58
  return SUCCEEDED;
59
}
60
 
61
 
62
int add_stack(struct stack *sta) {
63
 
64
  sta->file_id = obj_tmp->id;
65
  sta->next = NULL;
66
  sta->computed = NO;
67
  sta->under_work = NO;
68
 
69
  if (stacks_first == NULL) {
70
    stacks_first = sta;
71
    stacks_last = sta;
72
    sta->prev = NULL;
73
  }
74
  else {
75
    sta->prev = stacks_last;
76
    stacks_last->next = sta;
77
    stacks_last = sta;
78
  }
79
 
80
  return SUCCEEDED;
81
}
82
 
83
 
84
int add_section(struct section *s) {
85
 
86
  struct section *q;
87
 
88
 
89
  s->file_id = obj_tmp->id;
90
  s->next = NULL;
91
  s->alive = YES;
92
 
93
  if (strcmp(s->name, "BANKHEADER") == 0) {
94
 
95
    q = sec_hd_first;
96
    while (q != NULL) {
97
      if (q->bank == s->bank) {
98
        fprintf(stderr, "%s: ADD_SECTION: BANKHEADER section for bank %d was defined for the second time.\n", obj_tmp->name, s->bank);
99
        return FAILED;
100
      }
101
      q = q->next;
102
    }
103
 
104
    if (sec_hd_first == NULL) {
105
      sec_hd_first = s;
106
      sec_hd_last = s;
107
    }
108
    else {
109
      sec_hd_last->next = s;
110
      sec_hd_last = s;
111
    }
112
  }
113
  else {
114
    if (sec_first == NULL) {
115
      sec_first = s;
116
      sec_last = s;
117
      s->prev = NULL;
118
    }
119
    else {
120
      s->prev = sec_last;
121
      sec_last->next = s;
122
      sec_last = s;
123
    }
124
  }
125
 
126
  return SUCCEEDED;
127
}
128
 
129
 
130
int add_label(struct label *l) {
131
 
132
  l->next = NULL;
133
 
134
  if (labels_first == NULL) {
135
    labels_first = l;
136
    labels_last = l;
137
    l->prev = NULL;
138
  }
139
  else {
140
    l->prev = labels_last;
141
    labels_last->next = l;
142
    labels_last = l;
143
  }
144
 
145
  return SUCCEEDED;
146
}
147
 
148
 
149
int obtain_rombankmap(void) {
150
 
151
  int map_found = OFF, i, x, banksize, a;
152
  struct object_file *o;
153
  unsigned char *t;
154
 
155
 
156
  /* initialize values */
157
  for (i = 0; i < rombanks; i++)
158
    banks[i] = 0;
159
 
160
  o = obj_first;
161
  while (o != NULL) {
162
    if (o->format == WLA_VERSION_OBJ) {
163
      t = o->data + OBJ_ROMBANKMAP;
164
 
165
      /* obtain status */
166
      i = *t;
167
      t++;
168
 
169
      /* general rombanksize? */
170
      if (i == 0) {
171
        /* obtain banksize */
172
        banksize = READ_T
173
 
174
        o->memorymap = t;
175
        map_found = ON;
176
        for (i = 0; i < o->rom_banks; i++) {
177
          if (banks[i] == 0) {
178
            banks[i] = banksize;
179
            bankaddress[i] = i * banksize;
180
          }
181
          else if (banks[i] != banksize) {
182
            fprintf(stderr, "OBTAIN_ROMBANKMAP: ROMBANKMAPs don't match.\n");
183
            return FAILED;
184
          }
185
        }
186
      }
187
      else {
188
        for (a = 0, x = 0; x < o->rom_banks; x++) {
189
          banksize = READ_T
190
          if (banks[x] == 0) {
191
            banks[x] = banksize;
192
            bankaddress[x] = a;
193
          }
194
          else if (banks[x] != banksize) {
195
            fprintf(stderr, "OBTAIN_ROMBANKMAP: ROMBANKMAPs don't match.\n");
196
            return FAILED;
197
          }
198
          a += banksize;
199
        }
200
 
201
        o->memorymap = t;
202
        map_found = ON;
203
      }
204
    }
205
 
206
    o = o->next;
207
  }
208
 
209
  if (map_found == OFF) {
210
    fprintf(stderr, "OBTAIN_ROMBANKMAP: No object files.\n");
211
    return FAILED;
212
  }
213
 
214
  return SUCCEEDED;
215
}
216
 
217
 
218
int obtain_source_file_names(void) {
219
 
220
  struct source_file_name *s = NULL, **p;
221
  struct object_file *o;
222
  unsigned char *t, *m;
223
  int x, z;
224
 
225
 
226
  o = obj_first;
227
  while (o != NULL) {
228
    if (o->format == WLA_VERSION_OBJ)
229
      t = o->source_file_names;
230
    else
231
      t = o->data + LIB_SOURCE_FILE_NAMES;
232
 
233
    x = READ_T
234
 
235
    p = &(o->source_file_names_list);
236
    for (; x > 0; x--) {
237
      s = malloc(sizeof(struct source_file_name));
238
      if (s == NULL) {
239
        fprintf(stderr, "COLLECT_DLR: Out of memory.\n");
240
        return FAILED;
241
      }
242
 
243
      /* compute the length of the name */
244
      for (m = t, z = 0; *m != 0; m++, z++);
245
 
246
      s->name = malloc(z+1);
247
      if (s->name == NULL) {
248
        free(s);
249
        fprintf(stderr, "COLLECT_DLR: Out of memory.\n");
250
        return FAILED;
251
      }
252
 
253
      /* get the name */
254
      for (z = 0; *t != 0; t++, z++)
255
        s->name[z] = *t;
256
      s->name[z] = 0;
257
 
258
      t++;
259
      s->id = *(t++);
260
      s->next = NULL;
261
      *p = s;
262
      p = &(s->next);
263
    }
264
 
265
    o->exported_defines = t;
266
    o = o->next;
267
  }
268
 
269
  return SUCCEEDED;
270
}
271
 
272
 
273
int obtain_memorymap(void) {
274
 
275
  struct object_file *o;
276
  int map_found = OFF, i, x;
277
  unsigned char *t;
278
  struct slot s[256];
279
 
280
 
281
  for (i = 0; i < 256; i++)
282
    slots[i].usage = OFF;
283
 
284
  o = obj_first;
285
  while (o != NULL) {
286
    if (o->format == WLA_VERSION_OBJ) {
287
      t = o->memorymap;
288
 
289
      /* obtain slots */
290
      i = *t;
291
      t++;
292
 
293
      for (x = 0; i > 0; i--, x++) {
294
        slots[x].usage = ON;
295
        slots[x].address =  READ_T
296
        slots[x].size =  READ_T
297
      }
298
 
299
      o->source_file_names = t;
300
      map_found = ON;
301
      break;
302
    }
303
 
304
    o = o->next;
305
  }
306
 
307
  if (map_found == OFF) {
308
    fprintf(stderr, "OBTAIN_MEMORYMAP: No object files.\n");
309
    return FAILED;
310
  }
311
 
312
  /* check if the following memorymaps equal to the previous one */
313
  o = o->next;
314
  while (o != NULL) {
315
    if (o->format == WLA_VERSION_OBJ) {
316
 
317
      for (i = 0; i < 256; i++)
318
        s[i].usage = OFF;
319
      t = o->memorymap;
320
 
321
      /* obtain slots */
322
      i = *t;
323
      t++;
324
 
325
      for (x = 0; i > 0; i--, x++) {
326
        s[x].usage = ON;
327
        s[x].address =  READ_T
328
        s[x].size =  READ_T
329
      }
330
 
331
      o->source_file_names = t;
332
 
333
      for (x = 0, i = 0; i < 256; i++) {
334
        if (s[i].usage == ON) {
335
          if (slots[i].usage == OFF) {
336
            x = 1;
337
            break;
338
          }
339
          if (slots[i].address == s[i].address && slots[i].size == s[i].size)
340
            continue;
341
          x = 1;
342
          break;
343
        }
344
        else {
345
          if (slots[i].usage == ON) {
346
            x = 1;
347
            break;
348
          }
349
        }
350
      }
351
 
352
      if (x == 1) {
353
        fprintf(stderr, "OBTAIN_MEMORYMAP: The object files are compiled for different memory architectures.\n");
354
        return FAILED;
355
      }
356
    }
357
 
358
    o = o->next;
359
  }
360
 
361
  return SUCCEEDED;
362
}
363
 
364
 
365
int collect_dlr(void) {
366
 
367
  struct reference *r;
368
  struct stack *s;
369
  struct label *l;
370
  int section, x, i, n, q;
371
  unsigned char *t, *dtmp;
372
  double dou;
373
 
374
 
375
  section = 0;
376
  obj_tmp = obj_first;
377
  while (obj_tmp != NULL) {
378
 
379
    /* OBJECT FILE */
380
    if (obj_tmp->format == WLA_VERSION_OBJ) {
381
      t = obj_tmp->exported_defines;
382
      i = READ_T
383
 
384
      /* load defines */
385
      for (; i > 0; i--) {
386
        l = malloc(sizeof(struct label));
387
        if (l == NULL) {
388
          fprintf(stderr, "COLLECT_DLR: Out of memory.\n");
389
          return FAILED;
390
        }
391
 
392
        /* copy the name */
393
        for (x = 0; !(*t == 0 || *t == 1); t++, x++)
394
          l->name[x] = *t;
395
        l->name[x] = 0;
396
        if (*t == 0)
397
          l->status = LABEL_STATUS_DEFINE;
398
        else if (*t == 1)
399
          l->status = LABEL_STATUS_STACK;
400
        else {
401
          fprintf(stderr, "COLLECT_DLR: Unknown definition type \"%d\".\n", *t);
402
          return FAILED;
403
        }
404
        t++;
405
 
406
        READ_DOU
407
        l->address = dou;
408
        l->base = 0;
409
        l->file_id = obj_tmp->id;
410
        l->section_status = OFF;
411
 
412
        add_label(l);
413
      }
414
 
415
      /* load labels */
416
      i = READ_T
417
 
418
      for (; i > 0; i--) {
419
        l = malloc(sizeof(struct label));
420
        if (l == NULL) {
421
          fprintf(stderr, "COLLECT_DLR: Out of memory.\n");
422
          return FAILED;
423
        }
424
        for (x = 0; !(*t == 0 || *t == 1 || *t == 2); t++, x++)
425
          l->name[x] = *t;
426
        l->name[x] = 0;
427
 
428
        if (*t == 0)
429
          l->status = LABEL_STATUS_LABEL;
430
        else if (*t == 1)
431
          l->status = LABEL_STATUS_SYMBOL;
432
        else if (*t == 2)
433
          l->status = LABEL_STATUS_BREAKPOINT;
434
        else {
435
          fprintf(stderr, "COLLECT_DLR: Unknown label type \"%d\".\n", *t);
436
          return FAILED;
437
        }
438
 
439
        t++;
440
        l->slot = *(t++);
441
        l->file_id_source = *(t++);
442
        x = *(t++);
443
        if (x == 0)
444
          l->section_status = OFF;
445
        else {
446
          l->section_status = ON;
447
          l->section = x + section;
448
        }
449
        l->address = READ_T
450
        l->linenumber = READ_T
451
        l->base = (((int)l->address) >> 16) & 0xFF;
452
        l->address = ((int)l->address) & 0xFFFF;
453
        l->bank = READ_T
454
        l->file_id = obj_tmp->id;
455
 
456
        add_label(l);
457
      }
458
 
459
      i = READ_T
460
 
461
      /* load references */
462
      for (; i > 0; i--) {
463
        r = malloc(sizeof(struct reference));
464
        if (r == NULL) {
465
          fprintf(stderr, "COLLECT_DLR: Out of memory.\n");
466
          return FAILED;
467
        }
468
        for (x = 0; *t != 0; t++, x++)
469
          r->name[x] = *t;
470
        r->name[x] = 0;
471
        t++;
472
        r->type = *(t++);
473
        r->file_id_source = *(t++);
474
        r->slot = *(t++);
475
        x = *(t++);
476
        if (x == 0)
477
          r->section_status = OFF;
478
        else {
479
          r->section_status = ON;
480
          r->section = x + section;
481
        }
482
        r->linenumber = READ_T
483
        r->address = READ_T
484
        r->bank = READ_T
485
 
486
        add_reference(r);
487
      }
488
 
489
      i = READ_T
490
 
491
      /* load pending calculations */
492
      for (; i > 0; i--) {
493
        s = malloc(sizeof(struct stack));
494
        if (s == NULL) {
495
          fprintf(stderr, "COLLECT_DLR: Out of memory.\n");
496
          return FAILED;
497
        }
498
 
499
        s->id = READ_T
500
        s->type = *(t++);
501
        s->section = section + *(t++);
502
        s->file_id_source = *(t++);
503
        x = *(t++);
504
        s->position = *(t++);
505
        s->slot = *(t++);
506
 
507
        if (s->section == section)
508
          s->section_status = OFF;
509
        else
510
          s->section_status = ON;
511
 
512
        s->address = READ_T
513
        s->linenumber = READ_T
514
        s->bank = READ_T
515
        s->stacksize = x;
516
 
517
        s->stack = malloc(sizeof(struct stackitem) * x);
518
        if (s->stack == NULL) {
519
          fprintf(stderr, "COLLECT_DLR: Out of memory.\n");
520
          free(s);
521
          return FAILED;
522
        }
523
 
524
        add_stack(s);
525
 
526
        for (n = 0; n != x; n++) {
527
          s->stack[n].type = *(t++);
528
          s->stack[n].sign = *(t++);
529
          if (s->stack[n].type == STACK_ITEM_TYPE_STRING) {
530
            for (q = 0; *t != 0; t++, q++)
531
              s->stack[n].string[q] = *t;
532
            s->stack[n].string[q] = 0;
533
            t++;
534
          }
535
          else {
536
            READ_DOU
537
            s->stack[n].value = dou;
538
          }
539
        }
540
      }
541
 
542
      /* save pointer to data block area */
543
      obj_tmp->data_blocks = t;
544
    }
545
    /* LIBRARY FILE */
546
    else if (obj_tmp->format == WLA_VERSION_LIB) {
547
      t = obj_tmp->exported_defines;
548
      i = READ_T
549
 
550
      /* load definitions */
551
      for (; i > 0; i--) {
552
        l = malloc(sizeof(struct label));
553
        if (l == NULL) {
554
          fprintf(stderr, "COLLECT_DLR: Out of memory.\n");
555
          return FAILED;
556
        }
557
 
558
        /* copy the name */
559
        for (x = 0; !(*t == 0 || *t == 1); t++, x++)
560
          l->name[x] = *t;
561
        l->name[x] = 0;
562
        if (*t == 0)
563
          l->status = LABEL_STATUS_DEFINE;
564
        else if (*t == 1)
565
          l->status = LABEL_STATUS_STACK;
566
        else {
567
          fprintf(stderr, "COLLECT_DLR: Unknown definition type \"%d\".\n", *t);
568
          return FAILED;
569
        }
570
        t++;
571
 
572
        READ_DOU
573
        l->address = dou;
574
        l->bank = obj_tmp->bank;
575
        l->slot = obj_tmp->slot;
576
        l->base = 0;
577
        l->file_id = obj_tmp->id;
578
        l->section_status = OFF;
579
 
580
        add_label(l);
581
      }
582
 
583
      i = READ_T
584
 
585
      /* load labels and symbols */
586
      for (; i > 0; i--) {
587
        l = malloc(sizeof(struct label));
588
        if (l == NULL) {
589
          fprintf(stderr, "COLLECT_DLR: Out of memory.\n");
590
          return FAILED;
591
        }
592
        for (x = 0; !(*t == 0 || *t == 1 || *t == 2); t++, x++)
593
          l->name[x] = *t;
594
        l->name[x] = 0;
595
 
596
        if (*t == 0)
597
          l->status = LABEL_STATUS_LABEL;
598
        else if (*t == 1)
599
          l->status = LABEL_STATUS_SYMBOL;
600
        else if (*t == 2)
601
          l->status = LABEL_STATUS_BREAKPOINT;
602
        else {
603
          fprintf(stderr, "COLLECT_DLR: Unknown label type \"%d\".\n", *t);
604
          return FAILED;
605
        }
606
 
607
        t++;
608
        x = *(t++);
609
        l->section = x + section;
610
        l->file_id_source = *(t++);
611
        l->linenumber = READ_T
612
        l->section_status = ON;
613
        l->address = READ_T
614
        l->base = (((int)l->address) >> 16) & 0xFF;
615
        l->address = ((int)l->address) & 0xFFFF;
616
        l->bank = obj_tmp->bank;
617
        l->slot = obj_tmp->slot;
618
        l->file_id = obj_tmp->id;
619
 
620
        add_label(l);
621
      }
622
 
623
      i = READ_T
624
 
625
      /* load references */
626
      for (; i > 0; i--) {
627
        r = malloc(sizeof(struct reference));
628
        if (r == NULL) {
629
          fprintf(stderr, "COLLECT_DLR: Out of memory.\n");
630
          return FAILED;
631
        }
632
        for (x = 0; *t != 0; t++, x++)
633
          r->name[x] = *t;
634
        r->name[x] = 0;
635
        t++;
636
        r->type = *(t++);
637
        x = *(t++);
638
        r->section = x + section;
639
        r->file_id_source = *(t++);
640
        r->linenumber = READ_T
641
        r->section_status = ON;
642
        r->address = READ_T
643
 
644
        r->bank = obj_tmp->bank;
645
        r->slot = obj_tmp->slot;
646
 
647
        add_reference(r);
648
      }
649
 
650
      i = READ_T
651
 
652
      /* load pending calculations */
653
      for (; i > 0; i--) {
654
        s = malloc(sizeof(struct stack));
655
        if (s == NULL) {
656
          fprintf(stderr, "COLLECT_DLR: Out of memory.\n");
657
          return FAILED;
658
        }
659
 
660
        s->id = READ_T
661
        s->type = *(t++);
662
        s->section_status = ON;
663
        s->section = section + *(t++);
664
        s->file_id_source = *(t++);
665
        x = *(t++);
666
        s->position = *(t++);
667
        s->address = READ_T
668
        s->linenumber = READ_T
669
        s->stacksize = x;
670
        s->bank = obj_tmp->bank;
671
        s->slot = obj_tmp->slot;
672
 
673
        s->stack = malloc(sizeof(struct stackitem) * x);
674
        if (s->stack == NULL) {
675
          fprintf(stderr, "COLLECT_DLR: Out of memory.\n");
676
          free(s);
677
          return FAILED;
678
        }
679
 
680
        add_stack(s);
681
 
682
        for (n = 0; n != x; n++) {
683
          s->stack[n].type = *(t++);
684
          s->stack[n].sign = *(t++);
685
          if (s->stack[n].type == STACK_ITEM_TYPE_STRING) {
686
            for (q = 0; *t != 0; t++, q++)
687
              s->stack[n].string[q] = *t;
688
            s->stack[n].string[q] = 0;
689
            t++;
690
          }
691
          else {
692
            READ_DOU
693
            s->stack[n].value = dou;
694
          }
695
        }
696
      }
697
 
698
      /* save pointer to data block area */
699
      obj_tmp->data_blocks = t;
700
    }
701
 
702
    obj_tmp = obj_tmp->next;
703
    section += 0x1000;
704
  }
705
 
706
  return SUCCEEDED;
707
}
708
 
709
 
710
int parse_data_blocks(void) {
711
 
712
  struct section *s;
713
  int section, i, y, x;
714
  unsigned char *t, *p;
715
 
716
 
717
  obj_tmp = obj_first;
718
  section = 0;
719
  while (obj_tmp != NULL) {
720
 
721
    /* OBJECT FILE */
722
    if (obj_tmp->format == WLA_VERSION_OBJ) {
723
      t = obj_tmp->data_blocks;
724
      p = obj_tmp->data + obj_tmp->size;
725
      for (y = 1; t < p; ) {
726
        x = *(t++);
727
 
728
        if (x == DATA_TYPE_BLOCK) {
729
          /* address */
730
          i = READ_T
731
          /* amount of bytes */
732
          x = READ_T
733
 
734
          for (; x > 0; x--, i++)
735
            if (mem_insert(i, *(t++)) == FAILED)
736
              return FAILED;
737
        }
738
        else if (x == DATA_TYPE_SECTION) {
739
          s = malloc(sizeof(struct section));
740
          if (s == NULL) {
741
            fprintf(stderr, "PARSE_DATA_BLOCKS: Out of memory.\n");
742
            return FAILED;
743
          }
744
          y++;
745
 
746
          /* name */
747
          i = 0;
748
          while (*t != 0 && *t != 1 && *t != 2 && *t != 3 && *t != 4 && *t != 5 && *t != 6 && *t != 7)
749
            s->name[i++] = *(t++);
750
          s->name[i] = 0;
751
          s->status = *(t++);
752
          s->id = section + ((int)*(t++));
753
          s->slot = *(t++);
754
          s->file_id_source = *(t++);
755
          s->address = READ_T
756
          s->bank = READ_T
757
          s->size = READ_T
758
          s->alignment = READ_T
759
          s->data = t;
760
          s->library_status = OFF;
761
          t += s->size;
762
 
763
          /* listfile block */
764
          if (listfile_block_read(&t, s) == FAILED)
765
            return FAILED;
766
 
767
          if (add_section(s) == FAILED)
768
            return FAILED;
769
        }
770
      }
771
      obj_tmp = obj_tmp->next;
772
      section += 0x1000;
773
      continue;
774
    }
775
    /* LIBRARY FILE */
776
    else if (obj_tmp->format == WLA_VERSION_LIB) {
777
      t = obj_tmp->data_blocks;
778
      p = obj_tmp->data + obj_tmp->size;
779
      for (y = 1; t < p; ) {
780
 
781
        s = malloc(sizeof(struct section));
782
        if (s == NULL) {
783
          fprintf(stderr, "PARSE_DATA_BLOCKS: Out of memory.\n");
784
          return FAILED;
785
        }
786
        y++;
787
 
788
        /* name */
789
        i = 0;
790
        while (*t != 0 && *t != 7)
791
          s->name[i++] = *(t++);
792
        s->name[i] = 0;
793
        s->status = *(t++);
794
        s->id = section + ((int)*(t++));
795
        s->file_id_source = *(t++);
796
        s->size = READ_T
797
        s->alignment = READ_T
798
        s->data = t;
799
        s->address = 0;
800
        s->bank = obj_tmp->bank;
801
        s->slot = obj_tmp->slot;
802
        s->base = obj_tmp->base;
803
        s->library_status = ON;
804
        s->base_defined = obj_tmp->base_defined;
805
        t += s->size;
806
 
807
        /* listfile block */
808
        if (listfile_block_read(&t, s) == FAILED)
809
          return FAILED;
810
 
811
        add_section(s);
812
      }
813
      obj_tmp = obj_tmp->next;
814
      section += 0x1000;
815
      continue;
816
    }
817
  }
818
 
819
  return SUCCEEDED;
820
}
821
 
822
 
823
int obtain_rombanks(void) {
824
 
825
  unsigned char *t;
826
  int rb = 0, k, s;
827
 
828
 
829
  /* obtain the biggest rom size */
830
  s = 0;
831
  obj_tmp = obj_first;
832
  while (obj_tmp != NULL) {
833
    if (obj_tmp->format == WLA_VERSION_OBJ) {
834
 
835
      t = obj_tmp->data + OBJ_ROMBANKS;
836
      k = t[3] + (t[2] << 8) + (t[1] << 16) + (t[0] << 24);
837
 
838
      obj_tmp->rom_banks = k;
839
 
840
      if (k != rb)
841
        s++;
842
      if (k > rb)
843
        rb = k;
844
    }
845
    obj_tmp = obj_tmp->next;
846
  }
847
 
848
  /* emptyfill has been obtained in the header checks */
849
  rombanks = rb;
850
 
851
  if (s > 1)
852
    fprintf(stderr, "OBTAIN_ROMBANKS: Using the biggest selected amount of ROM banks (%d).\n", rombanks);
853
 
854
  return SUCCEEDED;
855
}
856
 
857
 
858
int clean_up_dlr(void) {
859
 
860
  struct reference *r, *re;
861
  struct stack *s, *st;
862
  struct label *l, *la;
863
  struct section *se, *sec, *sect;
864
 
865
 
866
  se = sec_first;
867
  while (se != NULL) {
868
    /* remove duplicates of unique sections and all the related data */
869
    if (strlen(se->name) >= 3 && se->name[0] == '!' && se->name[1] == '_' && se->name[2] == '_') {
870
      sec = se->next;
871
      while (sec != NULL) {
872
        if (strcmp(se->name, sec->name) == 0) {
873
          sect = sec;
874
 
875
          sect->prev->next = sect->next;
876
          if (sect->next != NULL)
877
            sect->next->prev = sect->prev;
878
          else
879
            sec_last = sect;
880
 
881
          /* free references */
882
          r = reference_first;
883
          while (r != NULL) {
884
            if (r->section_status == ON && r->section == sect->id) {
885
              re = r;
886
              if (re->prev == NULL)
887
                reference_first = re->next;
888
              else
889
                re->prev->next = re->next;
890
              if (re->next == NULL)
891
                reference_last = re->prev;
892
              else
893
                re->next->prev = re->prev;
894
 
895
              r = r->next;
896
              free(re);
897
            }
898
            else
899
              r = r->next;
900
          }
901
 
902
          /* free stacks */
903
          s = stacks_first;
904
          while (s != NULL) {
905
            if (s->section_status == ON && s->section == sect->id) {
906
              st = s;
907
              if (st->stack != NULL) {
908
                free(st->stack);
909
                st->stack = NULL;
910
              }
911
              if (st->prev == NULL)
912
                stacks_first = st->next;
913
              else
914
                st->prev->next = st->next;
915
              if (st->next == NULL)
916
                stacks_last = st->prev;
917
              else
918
                st->next->prev = st->prev;
919
 
920
              s = s->next;
921
              free(st);
922
            }
923
            else
924
              s = s->next;
925
          }
926
 
927
          /* free labels */
928
          l = labels_first;
929
          while (l != NULL) {
930
            if (l->section_status == ON && l->section == sect->id) {
931
              la = l;
932
              if (la->prev == NULL)
933
                labels_first = la->next;
934
              else
935
                la->prev->next = la->next;
936
              if (la->next == NULL)
937
                labels_last = la->prev;
938
              else
939
                la->next->prev = la->prev;
940
 
941
              l = l->next;
942
              free(la);
943
            }
944
            else
945
              l = l->next;
946
          }
947
 
948
          sec = sec->next;
949
          free(sect);
950
        }
951
        else
952
          sec = sec->next;
953
      }
954
    }
955
    se = se->next;
956
  }
957
 
958
  return SUCCEEDED;
959
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.