58f96c1d71dd81c418f7ea992b4099d7ceac39fe
[fw/sdcc] / as / link / mcs51 / lkarea.c
1 /* lkarea.c */
2
3 /*
4  * (C) Copyright 1989-1995
5  * All Rights Reserved
6  *
7  * Alan R. Baldwin
8  * 721 Berkeley St.
9  * Kent, Ohio  44240
10  *
11  *  3-Nov-97 JLH:
12  *           - change lkparea to use a_type == 0 as "virgin area" flag
13  * 02-Apr-98 JLH: add code to link 8051 data spaces
14  */
15
16 #include <stdio.h>
17 #include <string.h>
18 #include "aslink.h"
19
20 /*)Module   lkarea.c
21  *
22  *  The module lkarea.c contains the functions which
23  *  create and link together all area definitions read
24  *  from the .rel file(s).
25  *
26  *  lkarea.c contains the following functions:
27  *      VOID    lnkarea()
28  *      VOID    lnksect()
29  *      VOID    lkparea()
30  *      VOID    newarea()
31  *
32  *  lkarea.c contains no global variables.
33  */
34
35 /*)Function VOID    newarea()
36  *
37  *  The function newarea() creates and/or modifies area
38  *  and areax structures for each A directive read from
39  *  the .rel file(s).  The function lkparea() is called
40  *  to find the area structure associated with this name.
41  *  If the area does not yet exist then a new area
42  *  structure is created and linked to any existing
43  *  linked area structures. The area flags are copied
44  *  into the area flag variable.  For each occurence of
45  *  an A directive an areax structure is created and
46  *  linked to the areax structures associated with this
47  *  area.  The size of this area section is placed into
48  *  the areax structure.  The flag value for all subsequent
49  *  area definitions for the same area are compared and
50  *  flagged as an error if they are not identical.
51  *  The areax structure created for every occurence of
52  *  an A directive is loaded with a pointer to the base
53  *  area structure and a pointer to the associated
54  *  head structure.  And finally, a pointer to this
55  *  areax structure is loaded into the list of areax
56  *  structures in the head structure.  Refer to lkdata.c
57  *  for details of the structures and their linkage.
58  *
59  *  local variables:
60  *      areax **halp        pointer to an array of pointers
61  *      int     i           counter, loop variable, value
62  *      char    id[]        id string
63  *      int     narea       number of areas in this head structure
64  *      areax * taxp        pointer to an areax structure
65  *                          to areax structures
66  *
67  *  global variables:
68  *      area    *ap         Pointer to the current
69  *                          area structure
70  *      areax   *axp        Pointer to the current
71  *                          areax structure
72  *      head    *hp         Pointer to the current
73  *                          head structure
74  *      int     lkerr       error flag
75  *
76  *  functions called:
77  *      Addr_T  eval()      lkeval.c
78  *      VOID    exit()      c_library
79  *      int     fprintf()   c_library
80  *      VOID    getid()     lklex.c
81  *      VOID    lkparea()   lkarea.c
82  *      VOID    skip()      lklex.c
83  *
84  *  side effects:
85  *      The area and areax structures are created and
86  *      linked with the appropriate head structures.
87  *      Failure to allocate area or areax structure
88  *      space will terminate the linker.  Other internal
89  *      errors most likely caused by corrupted .rel
90  *      files will also terminate the linker.
91  */
92
93 /*
94  * Create an area entry.
95  *
96  * A xxxxxx size nnnn flags mm
97  *   |           |          |
98  *   |           |          `--  ap->a_flag
99  *   |           `------------- axp->a_size
100  *   `-------------------------  ap->a_id
101  *
102  */
103 VOID
104 newarea()
105 {
106     register int i, narea;
107     struct areax *taxp;
108     struct areax **halp;
109     char id[NCPS];
110
111     /*
112      * Create Area entry
113      */
114     getid(id, -1);
115     lkparea(id);
116     /*
117      * Evaluate area size
118      */
119     skip(-1);
120     axp->a_size = eval();
121     /*
122      * Evaluate flags
123      */
124     skip(-1);
125     i = 0;
126     taxp = ap->a_axp;
127     while (taxp->a_axp) {
128         ++i;
129         taxp = taxp->a_axp;
130     }
131     if (i == 0) {
132         ap->a_flag = eval();
133     } else {
134         i = eval();
135 /*      if (i && (ap->a_flag != i)) { */
136 /*          fprintf(stderr, "Conflicting flags in area %8s\n", id); */
137 /*          lkerr++; */
138 /*      } */
139     }
140     /*
141      * Evaluate area address
142      */
143     skip(-1);
144     axp->a_addr = eval();
145     /*
146      * Place pointer in header area list
147      */
148     if (headp == NULL) {
149         fprintf(stderr, "No header defined\n");
150         lkexit(1);
151     }
152     narea = hp->h_narea;
153     halp = hp->a_list;
154     for (i=0; i < narea ;++i) {
155         if (halp[i] == NULL) {
156             halp[i] = taxp;
157             return;
158         }
159     }
160     fprintf(stderr, "Header area list overflow\n");
161     lkexit(1);
162 }
163
164 /*)Function VOID    lkparea(id)
165  *
166  *      char *  id      pointer to the area name string
167  *
168  *  The function lkparea() searches the linked area structures
169  *  for a name match.  If the name is not found then an area
170  *  structure is created.  An areax structure is created and
171  *  appended to the areax structures linked to the area structure.
172  *  The associated base area and head structure pointers are
173  *  loaded into the areax structure.
174  *
175  *  local variables:
176  *      area *  tap         pointer to an area structure
177  *      areax * taxp        pointer to an areax structure
178  *
179  *  global variables:
180  *      area    *ap         Pointer to the current
181  *                          area structure
182  *      area    *areap      The pointer to the first
183  *                          area structure of a linked list
184  *      areax   *axp        Pointer to the current
185  *                          areax structure
186  *
187  *  functions called:
188  *      VOID *  new()       lksym()
189  *      char *  strcpy()    c_library
190  *      int     symeq()     lksym.c
191  *
192  *  side effects:
193  *      Area and/or areax structures are created.
194  *      Failure to allocate space for created structures
195  *      will terminate the linker.
196  */
197
198 VOID
199 lkparea(char *id)
200 {
201     register struct area *tap;
202     register struct areax *taxp;
203
204     ap = areap;
205     axp = (struct areax *) new (sizeof(struct areax));
206     axp->a_addr = -1; /* default: no address yet */
207     while (ap) {
208         if (symeq(id, ap->a_id)) {
209             taxp = ap->a_axp;
210             while (taxp->a_axp)
211                 taxp = taxp->a_axp;
212             taxp->a_axp = axp;
213             axp->a_bap = ap;
214             axp->a_bhp = hp;
215             return;
216         }
217         ap = ap->a_ap;
218     }
219     ap = (struct area *) new (sizeof(struct area));
220     if (areap == NULL) {
221         areap = ap;
222     } else {
223         tap = areap;
224         while (tap->a_ap)
225             tap = tap->a_ap;
226         tap->a_ap = ap;
227     }
228     ap->a_axp = axp;
229     axp->a_bap = ap;
230     axp->a_bhp = hp;
231     strncpy(ap->a_id, id, NCPS);
232     ap->a_addr = 0;
233 }
234
235 /*)Function VOID    lnkarea()
236  *
237  *  The function lnkarea() resolves all area addresses.
238  *  The function evaluates each area structure (and all
239  *  the associated areax structures) in sequence.  The
240  *  linking process supports four (4) possible area types:
241  *
242  *  ABS/OVR -   All sections (each individual areax
243  *          section) starts at the identical base
244  *          area address overlaying all other
245  *          areax sections for this area.  The
246  *          size of the area is largest of the area
247  *          sections.
248  *
249  *  ABS/CON -   All sections (each individual areax
250  *          section) are concatenated with the
251  *          first section starting at the base
252  *          area address.  The size of the area
253  *          is the sum of the section sizes.
254  *
255  *  NOTE:   Multiple absolute (ABS) areas are
256  *          never concatenated with each other,
257  *          thus absolute area A and absolute area
258  *          B will overlay each other if they begin
259  *          at the same location (the default is
260  *          always address 0 for absolute areas).
261  *
262  *  REL/OVR -   All sections (each individual areax
263  *          section) starts at the identical base
264  *          area address overlaying all other
265  *          areax sections for this area.  The
266  *          size of the area is largest of the area
267  *          sections.
268  *
269  *  REL/CON -   All sections (each individual areax
270  *          section) are concatenated with the
271  *          first section starting at the base
272  *          area address.  The size of the area
273  *          is the sum of the section sizes.
274  *
275  *  NOTE:   Relocatable (REL) areas are always concatenated
276  *          with each other, thus relocatable area B
277  *          (defined after area A) will follow
278  *          relocatable area A independent of the
279  *          starting address of area A.  Within a
280  *          specific area each areax section may be
281  *          overlayed or concatenated with other
282  *          areax sections.
283  *
284  *
285  *  If a base address for an area is specified then the
286  *  area will start at that address.  Any relocatable
287  *  areas defined subsequently will be concatenated to the
288  *  previous relocatable area if it does not have a base
289  *  address specified.
290  *
291  *  The names s_<areaname> and l_<areaname> are created to
292  *  define the starting address and length of each area.
293  *
294  *  local variables:
295  *      Addr_T  rloc        ;current relocation address
296  *      char    temp[]      ;temporary string
297  *      struct symbol   *sp ;symbol structure
298  *
299  *  global variables:
300  *      area    *ap         Pointer to the current
301  *                          area structure
302  *      area    *areap      The pointer to the first
303  *                          area structure of a linked list
304  *
305  *  functions called:
306  *      int     fprintf()   c_library
307  *      VOID    lnksect()   lkarea.c
308  *      symbol *lkpsym()    lksym.c
309  *      char *  strncpy()   c_library
310  *      int     symeq()     lksym.c
311  *
312  *  side effects:
313  *      All area and areax addresses and sizes are
314  *      determined and saved in their respective
315  *      structures.
316  */
317
318 VOID lnksect(register struct area *tap);
319 /*
320  * Resolve all area addresses.
321  */
322 VOID
323 lnkarea()
324 {
325     Addr_T rloc[4];
326     int  locIndex;
327     char temp[NCPS];
328     struct sym *sp;
329     /*JCF: used to save the REG_BANK_[0-3] and SBIT_BYTES area pointers*/
330     struct area *ta[5];
331     int j;
332
333     rloc[0] = rloc[1] = rloc[2] = rloc[3] = 0;
334     ap = areap;
335     while (ap) {
336         if (ap->a_flag&A_ABS) {
337             /*
338              * Absolute sections
339              */
340             lnksect(ap);
341         } else {
342             /* Determine memory space */
343             locIndex = 0;
344             if (ap->a_flag & A_CODE) {
345                 locIndex = 1;
346             }
347             if (ap->a_flag & A_XDATA) {
348                 locIndex = 2;
349             }
350             if (ap->a_flag & A_BIT) {
351                 locIndex = 3;
352             }
353             /*
354              * Relocatable sections
355              */
356             if (ap->a_type == 0) {  /* JLH */
357                 ap->a_addr = rloc[ locIndex ];
358                 ap->a_type = 1;
359             }
360             lnksect(ap);
361             rloc[ locIndex ] = ap->a_addr + ap->a_size;
362         }
363
364         /*
365          * Create symbols called:
366          *  s_<areaname>    the start address of the area
367          *  l_<areaname>    the length of the area
368          */
369
370         if (! symeq(ap->a_id, _abs_)) {
371             strncpy(temp+2,ap->a_id,NCPS-2);
372             *(temp+1) = '_';
373
374             *temp = 's';
375             sp = lkpsym(temp, 1);
376             sp->s_addr = ap->a_addr ;
377             /* sp->s_axp = ap->a_axp;  JLH: was NULL; */
378             sp->s_type |= S_DEF;
379
380             *temp = 'l';
381             sp = lkpsym(temp, 1);
382             sp->s_addr = ap->a_size;
383             sp->s_axp = NULL;
384             sp->s_type |= S_DEF;
385
386         }
387
388         /*JCF: Since area BSEG is defined just before BSEG_BYTES, use the bit size of BSEG
389         to compute the byte size of BSEG_BYTES: */
390         if (!strcmp(ap->a_id, "BSEG")) {
391             ap->a_ap->a_axp->a_size += ((ap->a_addr + ap->a_size + 7)/8); /*Bits to bytes*/
392         }
393         else if (!strcmp(ap->a_id, "REG_BANK_0")) ta[0]=ap;
394         else if (!strcmp(ap->a_id, "REG_BANK_1")) ta[1]=ap;
395         else if (!strcmp(ap->a_id, "REG_BANK_2")) ta[2]=ap;
396         else if (!strcmp(ap->a_id, "REG_BANK_3")) ta[3]=ap;
397         else if (!strcmp(ap->a_id, "BSEG_BYTES"))
398         {
399             ta[4]=ap;
400             for(j=4; j>1; j--)
401             {
402                 /*If upper register banks are not used roll back the relocation counter*/
403                 if ( (ta[j]->a_size==0) && (ta[j-1]->a_size==0) )
404                 {
405                     rloc[0]-=8;
406                 }
407                 else break;
408             }
409         }
410         ap = ap->a_ap;
411     }
412 }
413
414 /*)Function VOID    lnksect()
415  *
416  *      area *  tap         pointer to an area structure
417  *
418  *  The function lnksect() is the function called by
419  *  lnkarea() to resolve the areax addresses.  Refer
420  *  to the function lnkarea() for more detail. Pageing
421  *  boundary and length errors will be reported by this
422  *  function.
423  *
424  *  local variables:
425  *      Addr_T  size        size of area
426  *      Addr_T  addr        address of area
427  *      areax * taxp        pointer to an areax structure
428  *
429  *  global variables:
430  *      int     lkerr       error flag
431  *
432  *  functions called:
433  *      none
434  *
435  *  side effects:
436  *      All area and areax addresses and sizes area determined
437  *      and linked into the structures.
438  */
439
440 VOID
441 lnksect(register struct area *tap)
442 {
443     register Addr_T size, addr;
444     register struct areax *taxp;
445
446     size = 0;
447     addr = tap->a_addr;
448 #if 0
449     if ((tap->a_flag&A_PAG) && (addr & 0xFF)) {
450         fprintf(stderr,
451         "\n?ASlink-Warning-Paged Area %8s Boundary Error\n", tap->a_id);
452         lkerr++;
453     }
454 #endif
455     taxp = tap->a_axp;
456     if (tap->a_flag&A_OVR) {
457         /*
458          * Overlayed sections
459          */
460         while (taxp) {
461             taxp->a_addr = addr;
462             if (taxp->a_size > size)
463                 size = taxp->a_size;
464             taxp = taxp->a_axp;
465         }
466     } else {
467         /*
468          * Concatenated sections
469          */
470         while (taxp) {
471             taxp->a_addr = addr;
472             addr += taxp->a_size;
473             size += taxp->a_size;
474             taxp = taxp->a_axp;
475         }
476     }
477     tap->a_size = size;
478     if ((tap->a_flag&A_PAG) && (size > 256)) {
479         fprintf(stderr,
480         "\n?ASlink-Warning-Paged Area %8s Length Error\n", tap->a_id);
481         lkerr++;
482     }
483     if ((tap->a_flag&A_PAG) && (tap->a_size) &&
484         ((tap->a_addr & 0xFFFFFF00) != ((addr-1) & 0xFFFFFF00)))
485     {
486         fprintf(stderr,
487         "\n?ASlink-Warning-Paged Area %8s Boundary Error\n", tap->a_id);
488         lkerr++;
489     }
490 }
491
492 Addr_T lnksect2 (struct area *tap, int locIndex);
493 char idatamap[256];
494 unsigned long codemap[524288];
495 unsigned long xdatamap[131072];
496 struct area *dseg_ap = NULL;
497 Addr_T dram_start = 0;
498 Addr_T iram_start = 0;
499
500 /*Modified version of the functions for packing variables in internal data memory*/
501 VOID lnkarea2 (void)
502 {
503     Addr_T rloc[4]={0, 0, 0, 0};
504     Addr_T gs_size = 0;
505     int  locIndex;
506     char temp[NCPS];
507     struct sym *sp;
508     int j;
509     struct area *bseg_ap = NULL;
510     struct area *abs_ap = NULL;
511     struct area *gs0_ap = NULL;
512     struct sym *sp_dseg_s=NULL, *sp_dseg_l=NULL;
513
514     for(j=0; j<256; j++) idatamap[j]=' ';
515     memset(codemap, 0, sizeof(codemap));
516     memset(xdatamap, 0, sizeof(xdatamap));
517
518     /* first sort all absolute areas to the front */
519     ap = areap;
520     /* no need to check first area, it's in front anyway */
521     while (ap && ap->a_ap)
522     {
523         if (ap->a_ap->a_flag & A_ABS)
524         {/* next area is absolute, move it to front,
525             reversed sequence is no problem for absolutes */
526             abs_ap = ap->a_ap;
527             ap->a_ap = abs_ap->a_ap;
528             abs_ap->a_ap = areap;
529             areap = abs_ap;
530         }
531         else
532         {
533             ap = ap->a_ap;
534         }
535     }
536
537     /* next accumulate all GSINITx/GSFINAL area sizes
538        into GSINIT so they stay together */
539     ap = areap;
540     abs_ap = areap;
541     while (ap)
542     {
543         if (ap->a_flag & A_ABS)
544         {
545             abs_ap = ap; /* Remember the last abs area */
546         }
547         if (!strncmp(ap->a_id, "GS", 2))
548         {/* GSxxxxx area */
549             if (ap->a_size == 0)
550             {
551                 axp = ap->a_axp;
552                 while (axp)
553                 {
554                     ap->a_size += axp->a_size;
555                     axp = axp->a_axp;
556                 }
557             }
558             gs_size += ap->a_size;
559             if (!strcmp(ap->a_id, "GSINIT0"))
560             {/* GSINIT0 area */
561                 gs0_ap = ap;
562             }
563         }
564         /*Since area BSEG is defined just before BSEG_BYTES, use the bit size of BSEG
565         to compute the byte size of BSEG_BYTES: */
566         else if (!strcmp(ap->a_id, "BSEG"))
567         {
568             bseg_ap = ap->a_ap;            //BSEG_BYTES
569             for (axp=ap->a_axp; axp; axp=axp->a_axp)
570                 ap->a_size += axp->a_size;
571             bseg_ap->a_axp->a_size = ((ap->a_addr + ap->a_size + 7)/8); /*Bits to bytes*/
572             ap->a_ap = bseg_ap->a_ap;      //removed BSEG_BYTES from list
573             bseg_ap->a_ap = abs_ap->a_ap;
574             abs_ap->a_ap = bseg_ap;        //inserted BSEG_BYTES after abs
575             bseg_ap = ap;                  //BSEG
576         }
577         else if (!strcmp(ap->a_id, "DSEG"))
578         {
579             dseg_ap = ap; /*Need it later*/
580             dram_start = ap->a_addr;
581         }
582         else if (!strcmp(ap->a_id, "ISEG"))
583         {
584             iram_start = ap->a_addr;
585         }
586         ap = ap->a_ap;
587     }
588     if (gs0_ap)
589         gs0_ap->a_size = gs_size;
590
591     ap = areap;
592     while (ap)
593     {
594         /* Determine memory space */
595              if (ap->a_flag & A_CODE)  locIndex = 1;
596         else if (ap->a_flag & A_XDATA) locIndex = 2;
597         else if (ap->a_flag & A_BIT)   locIndex = 3;
598         else locIndex = 0;
599
600         if (ap->a_flag & A_ABS) /* Absolute sections */
601         {
602             lnksect2(ap, locIndex);
603         }
604         else /* Relocatable sections */
605         {
606             if (ap->a_type == 0)
607             {
608                 ap->a_addr = rloc[locIndex];
609                 ap->a_type = 1;
610             }
611
612             rloc[locIndex] = lnksect2(ap, locIndex);
613         }
614
615         if (!strcmp(ap->a_id, "BSEG_BYTES"))
616         {
617             bseg_ap->a_addr = (ap->a_axp->a_addr - 0x20) * 8; /*Bytes to bits*/
618         }
619         /*
620          * Create symbols called:
621          *  s_<areaname>    the start address of the area
622          *  l_<areaname>    the length of the area
623          */
624
625         if (! symeq(ap->a_id, _abs_))
626         {
627             strncpy(temp+2,ap->a_id,NCPS-2);
628             *(temp+1) = '_';
629
630             *temp = 's';
631             sp = lkpsym(temp, 1);
632             sp->s_addr = ap->a_addr;
633             sp->s_type |= S_DEF;
634             if (!strcmp(ap->a_id, "DSEG")) sp_dseg_s=sp;
635
636             *temp = 'l';
637             sp = lkpsym(temp, 1);
638             sp->s_addr = ap->a_size;
639             sp->s_axp = NULL;
640             sp->s_type |= S_DEF;
641             if (!strcmp(ap->a_id, "DSEG")) sp_dseg_l=sp;
642         }
643
644         ap = ap->a_ap;
645     }
646
647     /*Compute the size of DSEG*/
648     if(dseg_ap!=NULL)
649     {
650         dseg_ap->a_addr=0;
651         dseg_ap->a_size=0;
652         for(j=0; j<0x80; j++) if(idatamap[j]!=' ') dseg_ap->a_size++;
653     }
654     if(sp_dseg_s!=NULL) sp_dseg_s->s_addr=0;
655     if(sp_dseg_l!=NULL) sp_dseg_l->s_addr=dseg_ap->a_size;
656 }
657
658 static
659 Addr_T find_empty_space(Addr_T start, Addr_T size, unsigned long *map)
660 {
661     int i, j, k;
662     unsigned long mask, b;
663
664     while (1)
665     {
666         Addr_T a = start;
667         i = start >> 5;
668         j = (start + size) >> 5;
669         mask = -(1 << (start & 0x1F));
670
671         while (i < j)
672         {
673             if (map[i] & mask)
674             {
675                 k = 32;
676                 for (b=0x80000000; b!=0; b>>=1, k--)
677                 {
678                     if (map[i] & b)
679                       break;
680                 }
681                 start = a + k;
682                 break;
683             }
684             i++;
685             mask = 0xFFFFFFFF;
686             a += 32;
687         }
688         if (start > a)
689           continue;
690
691         mask &= (1 << ((start + size) & 0x1F)) - 1;
692         if (map[i] & mask)
693         {
694             k = 32;
695             for (b=0x80000000; b!=0; b>>=1, k--)
696             {
697                 if (map[i] & b)
698                     break;
699             }
700             start = (a & ~0x1F) + k;
701         }
702         if (start <= a)
703           break;
704     }
705     return start;
706 }
707
708 static
709 Addr_T allocate_space(Addr_T start, Addr_T size, char* id, unsigned long *map)
710 {
711     int i, j;
712     unsigned long mask;
713     Addr_T a = start;
714     i = start >> 5;
715     j = (start + size) >> 5;
716     mask = -(1 << (start & 0x1F));
717
718     while (i < j)
719     {
720         if (map[i] & mask)
721         {
722             fprintf(stderr, "memory overlap near 0x%X for %s\n", a, id);
723         }
724         map[i++] |= mask;
725         mask = 0xFFFFFFFF;
726         a += 32;
727     }
728     mask &= (1 << ((start + size) & 0x1F)) - 1;
729     if (map[i] & mask)
730     {
731         fprintf(stderr, "memory overlap near 0x%X for %s\n", a, id);
732     }
733     map[i] |= mask;
734     return start;
735 }
736
737 Addr_T lnksect2 (struct area *tap, int locIndex)
738 {
739     register Addr_T size, addr;
740     register struct areax *taxp;
741     int j, k, ramlimit, ramstart;
742     char fchar=' ', dchar='a';
743     char ErrMsg[]="?ASlink-Error-Could not get %d consecutive byte%s"
744                   " in internal RAM for area %s.\n";
745
746     tap->a_unaloc=0;
747
748     /*Notice that only ISEG and SSEG can be in the indirectly addressable internal RAM*/
749     if( (!strcmp(tap->a_id, "ISEG")) || (!strcmp(tap->a_id, "SSEG")) )
750     {
751         ramstart = iram_start;
752
753         if ((iram_size <= 0) || (ramstart + iram_size > 0x100))
754             ramlimit = 0x100;
755         else
756             ramlimit = ramstart + iram_size;
757     }
758     else
759     {
760         ramstart = dram_start;
761
762         if ((iram_size <= 0) || (ramstart + iram_size > 0x80))
763             ramlimit = 0x80;
764         else
765             ramlimit = ramstart + iram_size;
766     }
767
768     size = 0;
769     addr = tap->a_addr;
770 #if 0
771     if ((tap->a_flag&A_PAG) && (addr & 0xFF))
772     {
773         fprintf(stderr,
774           "\n?ASlink-Warning-Paged Area %8s Boundary Error\n", tap->a_id);
775         lkerr++;
776     }
777 #endif
778     taxp = tap->a_axp;
779
780     /*Use a letter to identify each area in the internal RAM layout map*/
781     if (locIndex==0)
782     {
783         /**/ if(!strcmp(tap->a_id, "DSEG"))
784             fchar='D'; /*It will be converted to letters 'a' to 'z' later for each areax*/
785         else if(!strcmp(tap->a_id, "ISEG"))
786             fchar='I';
787         else if(!strcmp(tap->a_id, "SSEG"))
788             fchar='S';
789         else if(!strcmp(tap->a_id, "OSEG"))
790             fchar='Q';
791         else if(!strcmp(tap->a_id, "REG_BANK_0"))
792             fchar='0';
793         else if(!strcmp(tap->a_id, "REG_BANK_1"))
794             fchar='1';
795         else if(!strcmp(tap->a_id, "REG_BANK_2"))
796             fchar='2';
797         else if(!strcmp(tap->a_id, "REG_BANK_3"))
798             fchar='3';
799         else if(!strcmp(tap->a_id, "BSEG_BYTES"))
800             fchar='B';
801         else if(!strcmp(tap->a_id, "BIT_BANK"))
802             fchar='T';
803         else
804             fchar=' ';/*???*/
805     }
806     else if (locIndex == 1)
807     {
808         /**/ if(!strcmp(tap->a_id, "GSINIT"))
809             fchar='G';
810     }
811     else if (locIndex == 2)
812     {
813         /**/ if(!strcmp(tap->a_id, "XSTK"))
814             fchar='K';
815     }
816
817     if (tap->a_flag&A_OVR) /* Overlayed sections */
818     {
819         while (taxp)
820         {
821             if(taxp->a_size == 0)
822             {
823                 taxp = taxp->a_axp;
824                 continue;
825             }
826
827             if ( (fchar=='0')||(fchar=='1')||(fchar=='2')||(fchar=='3') ) /*Reg banks*/
828             {
829                 addr=(fchar-'0')*8;
830                 taxp->a_addr=addr;
831                 size=taxp->a_size;
832                 for(j=addr; (j<(int)(addr+size)) && (j<ramlimit); j++)
833                     idatamap[j]=fchar;
834             }
835             else if( (fchar=='S') || (fchar=='Q') ) /*Overlay and stack in internal RAM*/
836             {
837                 /*Find the size of the space currently used for this areax overlay*/
838                 for(j=ramstart, size=0; j<ramlimit; j++)
839                     if(idatamap[j]==fchar) size++;
840
841                 if( (fchar=='S') && (stacksize==0) )
842                 {
843                    /*Search for the largest space available and use it for stack*/
844                     for(j=ramstart, k=0, taxp->a_size=0; j<ramlimit; j++)
845                     {
846                         if(idatamap[j]==' ')
847                         {
848                             if((++k)>(int)taxp->a_size)
849                                 taxp->a_size=k;
850                         }
851                         else
852                         {
853                             k=0;
854                         }
855                     }
856                     stacksize=taxp->a_size;
857                 }
858
859                 /*If more space required, release the previously allocated areax in
860                 internal RAM and search for a bigger one*/
861                 if((int)taxp->a_size>size)
862                 {
863                     size=(int)taxp->a_size;
864
865                     for(j=ramstart; j<ramlimit; j++)
866                         if(idatamap[j]==fchar) idatamap[j]=' ';
867
868                     /*Search for a space large enough in data memory for this overlay areax*/
869                     for(j=ramstart, k=0; j<ramlimit; j++)
870                     {
871                         if(idatamap[j]==' ')
872                             k++;
873                         else
874                             k=0;
875                     if(k==(int)taxp->a_size)
876                             break;
877                     }
878
879                     /*Mark the memory used for overlay*/
880                     if(k==(int)taxp->a_size)
881                     {
882                         addr = j-k+1;
883                         for(j=addr; (j<(int)(addr+size)); j++)
884                             idatamap[j]=fchar;
885                     }
886                     else /*Couldn't find a chunk big enough: report the problem.*/
887                     {
888                         tap->a_unaloc=taxp->a_size;
889                         fprintf(stderr, ErrMsg, taxp->a_size, taxp->a_size>1?"s":"", tap->a_id);
890                         lkerr++;
891                     }
892                 }
893             }
894             else if (fchar=='T') /*Bit addressable bytes in internal RAM*/
895             {
896                 /*Find the size of the space currently used for this areax overlay*/
897 //              for(j=0x20, size=0; j<0x30; j++)
898 //                  if(idatamap[j]==fchar) size++;
899
900                 /*If more space required, release the previously allocated areax in
901                 internal RAM and search for a bigger one*/
902                 if((int)taxp->a_size>size)
903                 {
904                     size=(int)taxp->a_size;
905
906                     for(j=0x20; j<0x30; j++)
907                         if(idatamap[j]==fchar) idatamap[j]=' ';
908
909                     /*Search for a space large enough in data memory for this overlay areax*/
910                     for(j=0x20, k=0; j<0x30; j++)
911                     {
912                         if(idatamap[j]==' ')
913                             k++;
914                         else
915                             k=0;
916                         if(k==(int)taxp->a_size)
917                             break;
918                     }
919
920                     /*Mark the memory used for overlay*/
921                     if(k==(int)size)
922                     {
923                         addr = j-k+1;
924                         for(j=addr; (j<(int)(addr+size)); j++)
925                             idatamap[j]=fchar;
926                     }
927                     else /*Couldn't find a chunk big enough: report the problem.*/
928                     {
929                         tap->a_unaloc=taxp->a_size;
930                         fprintf(stderr, ErrMsg, taxp->a_size, taxp->a_size>1?"s":"", tap->a_id);
931                         lkerr++;
932                     }
933                 }
934             }
935             else /*Overlay areas not in internal ram*/
936             {
937                 taxp->a_addr = addr;
938                 if (taxp->a_size > size) size = taxp->a_size;
939             }
940             taxp = taxp->a_axp;
941         }
942         /*Now set all overlayed areax to the same start address*/
943         taxp = tap->a_axp;
944         while (taxp)
945         {
946             taxp->a_addr = addr;
947             taxp = taxp->a_axp;
948         }
949     }
950     else if (tap->a_flag & A_ABS) /* Absolute sections */
951     {
952         while (taxp)
953         {
954             if (locIndex == 0)
955             {
956                 for (j=taxp->a_addr; (j<(int)(taxp->a_addr+taxp->a_size)) && (j<256); j++)
957                 {
958                     if (idatamap[j] == ' ')
959                         idatamap[j] = 'A';
960                     else
961                         fprintf(stderr, "memory overlap at 0x%X for %s\n", j, tap->a_id);
962                 }
963             }
964             else if (locIndex == 1)
965             {
966                 allocate_space(taxp->a_addr, taxp->a_size, tap->a_id, codemap);
967             }
968             else if (locIndex == 2)
969             {
970                 allocate_space(taxp->a_addr, taxp->a_size, tap->a_id, xdatamap);
971             }
972             taxp->a_addr = 0; /* reset to zero so relative addresses become absolute */
973             size += taxp->a_size;
974             taxp = taxp->a_axp;
975         }
976     }
977     else /* Concatenated sections */
978     {
979         if ((locIndex == 1) && tap->a_size)
980         {
981             addr = find_empty_space(addr, tap->a_size, codemap);
982         }
983         if ((locIndex == 2) && tap->a_size)
984         {
985             addr = find_empty_space(addr, tap->a_size, xdatamap);
986         }
987         while (taxp)
988         {
989             if( (fchar=='D') || (fchar=='I') )
990             {
991                 if(taxp->a_size)
992                 {
993                     /*Search for a space large enough in internal RAM for this areax*/
994                     for(j=ramstart, k=0; j<ramlimit; j++)
995                     {
996                         if(idatamap[j]==' ')
997                             k++;
998                         else
999                             k=0;
1000                         if(k==(int)taxp->a_size)
1001                             break;
1002                     }
1003
1004                     if(k==(int)taxp->a_size)
1005                     {
1006                         taxp->a_addr = j-k+1;
1007
1008                         size += taxp->a_size;
1009
1010                         for(j=taxp->a_addr; (j<(int)(taxp->a_addr+taxp->a_size)) && (j<ramlimit); j++)
1011                             idatamap[j]=(fchar=='D')?dchar:fchar;
1012                         if((taxp->a_size>0)&&(fchar=='D'))dchar++;
1013                         if((dchar<'a')||(dchar>'z')) dchar='D'; /*Ran out of letters?*/
1014                     }
1015                     else /*We are in trouble, there is not enough memory for an areax chunk*/
1016                     {
1017                         taxp->a_addr = addr;
1018                         addr += taxp->a_size;
1019                         size += taxp->a_size;
1020                         tap->a_unaloc+=taxp->a_size;
1021                         fprintf(stderr, ErrMsg, taxp->a_size, taxp->a_size>1?"s":"", tap->a_id);
1022                         lkerr++;
1023                     }
1024                }
1025                taxp = taxp->a_axp;
1026             }
1027             else if(fchar=='B')
1028             {
1029                 if(taxp->a_size!=0)
1030                 {
1031                     /*Search for a space large enough in data memory for this areax*/
1032                     for(j=0x20, k=0; j<0x30; j++)
1033                     {
1034                         if(idatamap[j]==' ')
1035                             k++;
1036                         else
1037                             k=0;
1038                         if(k==(int)taxp->a_size) break;
1039                     }
1040
1041                     /*Mark the memory used*/
1042                     if(k==(int)taxp->a_size)
1043                     {
1044                         taxp->a_addr = j-k+1;
1045                         for(j=taxp->a_addr; (j<(int)(taxp->a_addr+taxp->a_size)) && (j<0x30); j++)
1046                         idatamap[j]=fchar;
1047                     }
1048                     else /*Couldn't find a chunk big enough: report the problem.*/
1049                     {
1050                         tap->a_unaloc=taxp->a_size;
1051                         fprintf(stderr, ErrMsg, taxp->a_size, taxp->a_size>1?"s":"", tap->a_id);
1052                         lkerr++;
1053                     }
1054                 }
1055                 size += taxp->a_size;
1056                 taxp = taxp->a_axp;
1057             }
1058             else /*For concatenated BIT, CODE, and XRAM areax's*/
1059             {
1060                 if((fchar=='K') && (taxp->a_size == 1))
1061                 {
1062                     taxp->a_size = 256-(addr & 0xFF);
1063                 }
1064                 //find next unused address now
1065                 if ((locIndex == 1) && taxp->a_size)
1066                 {
1067                     addr = find_empty_space(addr, taxp->a_size, codemap);
1068                     allocate_space(addr, taxp->a_size, tap->a_id, codemap);
1069                 }
1070                 if ((locIndex == 2) && taxp->a_size)
1071                 {
1072                     addr = find_empty_space(addr, taxp->a_size, xdatamap);
1073                     allocate_space(addr, taxp->a_size, tap->a_id, xdatamap);
1074                 }
1075                 taxp->a_addr = addr;
1076                 addr += taxp->a_size;
1077                 size += taxp->a_size;
1078                 taxp = taxp->a_axp;
1079             }
1080         }
1081     }
1082     tap->a_size = size;
1083
1084     if ((tap->a_flag&A_PAG) && (size > 256))
1085     {
1086         fprintf(stderr,
1087         "\n?ASlink-Warning-Paged Area %8s Length Error\n", tap->a_id);
1088         lkerr++;
1089     }
1090     if ((tap->a_flag&A_PAG) && (tap->a_size) &&
1091         ((tap->a_addr & 0xFFFFFF00) != ((addr-1) & 0xFFFFFF00)))
1092     {
1093         fprintf(stderr,
1094         "\n?ASlink-Warning-Paged Area %8s Boundary Error\n", tap->a_id);
1095         lkerr++;
1096     }
1097     return addr;
1098 }