Added options --stack-size and --pack-iram
[fw/sdcc] / as / mcs51 / lkmem.c
1 /*-------------------------------------------------------------------------
2   lkmem.c - Create a memory summary file with extension .mem
3
4    Written By -  Jesus Calvino-Fraga, jesusc@ieee.org (2002)
5
6    This program is free software; you can redistribute it and/or modify it
7    under the terms of the GNU General Public License as published by the
8    Free Software Foundation; either version 2, or (at your option) any
9    later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 -------------------------------------------------------------------------*/
20
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include "aslink.h"
25 #include "strcmpi.h"
26
27 int summary(struct area * areap) 
28 {
29         #define EQ(A,B) !as_strcmpi((A),(B))
30         #define MIN_STACK 16
31         #define REPORT_ERROR(A, H) \
32         {\
33                 fprintf(of, "%s%s", (H)?"*** ERROR: ":"", (A)); \
34                 fprintf(stderr, "%s%s", (H)?"\n?ASlink-Error-":"",(A)); \
35                 toreturn=1; \
36         }
37
38         #define REPORT_WARNING(A, H) \
39         { \
40                 fprintf(of, "%s%s", (H)?"*** WARNING: ":"", (A)); \
41                 fprintf(stderr, "%s%s",(H)?"\n?ASlink-Warning-":"", (A)); \
42         }
43
44         char buff[128];
45         int j, toreturn=0;
46         unsigned int Total_Last=0, k; 
47
48         struct area * xp;
49         FILE * of;
50         
51         /*Artifacts used for printing*/
52         char start[15], end[15], size[15], max[15];
53         char format[]="   %-16.16s %-8.8s %-8.8s %-8.8s %-8.8s\n";
54         char line[]="---------------------";
55
56         typedef struct
57         {
58                 unsigned long Start;
59                 unsigned long Size;
60                 unsigned long Max;
61                 char Name[NCPS];
62                 unsigned long flag;
63         } _Mem;
64
65         unsigned int dram[0x100];
66         _Mem Ram[]={
67                 {0,             8,      8,       "REG_BANK_0", 0x0001},
68                 {0x8,   8,      8,       "REG_BANK_1", 0x0002},
69                 {0x10,  8,      8,       "REG_BANK_2", 0x0004},
70                 {0x18,  8,      8,       "REG_BANK_3", 0x0008},
71                 {0x20,  0,      16,      "BSEG_BYTES", 0x0010},
72                 {0,             0,      128, "UNUSED",     0x0000},
73                 {0x7f,  0,      128, "DATA",       0x0020},
74                 {0,             0,      128, "TOTAL:",     0x0000}
75         };
76
77         _Mem IRam= {0xff,   0,   128, "INDIRECT RAM",           0x0080};
78         _Mem Stack={0xff,   0,     1, "STACK",                          0x0000};
79         _Mem XRam= {0xffff, 0, 65536, "EXTERNAL RAM",           0x0100};
80         _Mem Rom=  {0xffff, 0, 65536, "ROM/EPROM/FLASH",        0x0200};
81
82         if(stacksize==0) stacksize=MIN_STACK;
83
84         if(rflag) /*For the DS390*/
85         {
86                 XRam.Max=0x1000000; /*24 bits*/
87                 XRam.Start=0xffffff;
88                 Rom.Max=0x1000000;
89                 Rom.Start=0xffffff;
90         }
91
92         if((iram_size<=0)||(iram_size>0x100)) /*Default: 8052 like memory*/
93         {
94                 Ram[5].Max=0x80;
95                 Ram[6].Max=0x80;
96                 Ram[7].Max=0x80;
97                 IRam.Max=0x80;
98                 iram_size=0x100;
99         }
100         else if(iram_size<0x80)
101         {
102                 Ram[5].Max=iram_size;
103                 Ram[6].Max=iram_size;
104                 Ram[7].Max=iram_size;
105                 IRam.Max=0;
106         }
107         else
108         {
109                 Ram[5].Max=0x80;
110                 Ram[6].Max=0x80;
111                 Ram[7].Max=0x80;
112                 IRam.Max=iram_size-0x80;
113         }
114
115         for(j=0; j<(int)iram_size; j++) dram[j]=0;
116         for(; j<0x100; j++) dram[j]=0x8000; /*Memory not available*/
117
118         /* Open Memory Summary File*/
119         of = afile(linkp->f_idp, "mem", 1);
120         if (of == NULL)
121         {
122                 lkexit(1);
123         }
124
125         xp=areap;
126         while (xp)
127         {
128                 /**/ if (EQ(xp->a_id, "REG_BANK_0"))
129                 {
130                         Ram[0].Size=xp->a_size;
131                 }
132                 else if (EQ(xp->a_id, "REG_BANK_1"))
133                 {
134                         Ram[1].Size=xp->a_size;
135                 }
136                 else if (EQ(xp->a_id, "REG_BANK_2"))
137                 {
138                         Ram[2].Size=xp->a_size;
139                 }
140                 else if (EQ(xp->a_id, "REG_BANK_3"))
141                 {
142                         Ram[3].Size=xp->a_size;
143                 }
144                 else if (EQ(xp->a_id, "BSEG_BYTES"))
145                 {
146                         Ram[4].Size=xp->a_size;
147                 }
148                 else if ( EQ(xp->a_id, "DSEG") || EQ(xp->a_id, "OSEG") )
149                 {
150                         Ram[6].Size+=xp->a_size;
151                         if(xp->a_addr<Ram[6].Start) Ram[6].Start=xp->a_addr;
152                 }
153
154                 else if( EQ(xp->a_id, "CSEG") || EQ(xp->a_id, "GSINIT") ||
155                                  EQ(xp->a_id, "GSFINAL") || EQ(xp->a_id, "HOME") )
156                 {
157                         Rom.Size+=xp->a_size;
158                         if(xp->a_addr<Rom.Start) Rom.Start=xp->a_addr;
159                 }
160                 
161                 else if (EQ(xp->a_id, "SSEG"))
162                 {
163                         Stack.Size+=xp->a_size;
164                         if(xp->a_addr<Stack.Start) Stack.Start=xp->a_addr;
165                 }
166
167                 else if (EQ(xp->a_id, "XSEG") || EQ(xp->a_id, "XISEG")) 
168                 {
169                         XRam.Size+=xp->a_size;
170                         if(xp->a_addr<XRam.Start) XRam.Start=xp->a_addr;
171                 }
172
173                 else if (EQ(xp->a_id, "ISEG"))
174                 {
175                         IRam.Size+=xp->a_size;
176                         if(xp->a_addr<IRam.Start) IRam.Start=xp->a_addr;
177                 }
178                 xp=xp->a_ap;
179         }
180
181         for(j=0; j<7; j++)
182                 for(k=Ram[j].Start; (k<(Ram[j].Start+Ram[j].Size))&&(k<0x100); k++)
183                         dram[k]|=Ram[j].flag; /*Mark as used*/
184         
185         for(k=IRam.Start; (k<(IRam.Start+IRam.Size))&&(k<0x100); k++)
186                 dram[k]|=IRam.flag; /*Mark as used*/
187
188         /*Compute the amount of unused memory in direct data Ram.  This is the
189         gap between the last register bank or bit segment and the data segment.*/
190         for(k=Ram[6].Start-1; (dram[k]==0) && (k>0); k--);
191         Ram[5].Start=k+1;
192         Ram[5].Size=Ram[6].Start-Ram[5].Start; /*It may be zero (which is good!)*/
193
194         /*Compute the data Ram totals*/
195         for(j=0; j<7; j++)
196         {
197                 if(Ram[7].Start>Ram[j].Start) Ram[7].Start=Ram[j].Start;
198                 Ram[7].Size+=Ram[j].Size;
199         }
200         Total_Last=Ram[6].Size+Ram[6].Start-1;
201
202         /*Report the Ram totals*/
203         fprintf(of, "Direct Internal RAM:\n");
204         fprintf(of, format, "Name", "Start", "End", "Size", "Max");
205
206         for(j=0; j<8; j++)
207         {
208                 if((j==0) || (j==7)) fprintf(of, format, line, line, line, line, line);
209                 if((j!=5) || (Ram[j].Size>0))
210                 {
211                         sprintf(start, "0x%02lx", Ram[j].Start);
212                         if(Ram[j].Size==0)
213                                 end[0]=0;/*Empty string*/
214                         else
215                                 sprintf(end,  "0x%02lx", j==7?Total_Last:Ram[j].Size+Ram[j].Start-1);
216                         sprintf(size, "%5lu", Ram[j].Size);
217                         sprintf(max, "%5lu", Ram[j].Max);
218                         fprintf(of, format, Ram[j].Name, start, end, size, max);
219                 }
220         }
221
222         for(k=Ram[6].Start; (k<(Ram[6].Start+Ram[6].Size))&&(k<0x100); k++)
223         {
224                 if(dram[k]!=Ram[6].flag)
225                 {
226                         sprintf(buff, "Internal memory overlap starting at 0x%02x.\n", k);
227                         REPORT_ERROR(buff, 1);
228                         break;
229                 }
230         }
231
232         if(Ram[4].Size>Ram[4].Max)
233         {
234                 k=Ram[4].Size-Ram[4].Max;
235                 sprintf(buff, "Insufficient bit addressable memory.  "
236                                         "%d byte%s short.\n", k, (k==1)?"":"s");
237                 REPORT_ERROR(buff, 1);
238         }
239
240         if(Ram[5].Size!=0)
241         {
242                 sprintf(buff, "%ld bytes in data memory wasted.  "
243                             "SDCC link could use: --data-loc 0x%02lx\n",
244                                         Ram[5].Size, Ram[6].Start-Ram[5].Size);
245                 REPORT_WARNING(buff, 1);
246         }
247
248         if((Ram[6].Start+Ram[6].Size)>Ram[6].Max)
249         {
250                 k=(Ram[6].Start+Ram[6].Size)-Ram[6].Max;
251                 sprintf(buff, "Insufficient space in data memory.   "
252                                         "%d byte%s short.\n", k, (k==1)?"":"s");
253                 REPORT_ERROR(buff, 1);
254         }
255
256         /*Report the position of the begining of the stack*/
257         fprintf(of, "\n%stack starts at: 0x%02lx (sp set to 0x%02lx)",
258                 rflag ? "16 bit mode initial s" : "S", Stack.Start, Stack.Start-1);
259
260         /*Check that the stack pointer is landing in a safe place:*/
261         if( (dram[Stack.Start] & 0x8000) == 0x8000 )
262         {
263                 fprintf(of, ".\n");
264                 sprintf(buff, "Stack set to unavailable memory.\n");
265                 REPORT_ERROR(buff, 1);
266         }
267         else if(dram[Stack.Start])
268         {
269                 fprintf(of, ".\n");
270                 sprintf(buff, "Stack overlaps area ");
271                 REPORT_ERROR(buff, 1);
272                 for(j=0; j<7; j++)
273                 {
274                         if(dram[Stack.Start]&Ram[j].flag)
275                         {
276                                 sprintf(buff, "'%s'\n", Ram[j].Name);
277                                 break;
278                         }
279                 }
280                 if(dram[Stack.Start]&IRam.flag)
281                 {
282                         sprintf(buff, "'%s'\n", IRam.Name);
283                 }
284                 REPORT_ERROR(buff, 0);
285         }
286         else
287         {
288                 for(j=Stack.Start, k=0; (j<(int)iram_size)&&(dram[j]==0); j++, k++);
289                 fprintf(of, " with %d bytes available\n", k);
290                 if ((int)k<stacksize)
291                 {
292                         sprintf(buff, "Only %d byte%s available for stack.\n",
293                                 k, (k==1)?"":"s");
294                         REPORT_WARNING(buff, 1);
295                 }
296         }
297
298         fprintf(of, "\nOther memory:\n");
299         fprintf(of, format, "Name", "Start", "End", "Size", "Max");
300         fprintf(of, format, line, line, line, line, line);
301
302         /*Report IRam totals:*/
303         sprintf(start, "0x%02lx", IRam.Start);
304         if(IRam.Size==0)
305                 end[0]=0;/*Empty string*/
306         else
307                 sprintf(end,  "0x%02lx", IRam.Size+IRam.Start-1);
308         sprintf(size, "%5lu", IRam.Size);
309         sprintf(max, "%5lu", IRam.Max);
310         fprintf(of, format, IRam.Name, start, end, size, max);
311
312         /*Report XRam totals:*/
313         sprintf(start, "0x%04lx", XRam.Start);
314         if(XRam.Size==0)
315                 end[0]=0;/*Empty string*/
316         else
317                 sprintf(end,  "0x%04lx", XRam.Size+XRam.Start-1);
318         sprintf(size, "%5lu", XRam.Size);
319         sprintf(max, "%5lu", xram_size<0?XRam.Max:xram_size);
320         fprintf(of, format, XRam.Name, start, end, size, max);
321
322         /*Report Rom/Flash totals:*/
323         sprintf(start, "0x%04lx", Rom.Start);
324         if(Rom.Size==0)
325                 end[0]=0;/*Empty string*/
326         else
327                 sprintf(end,  "0x%04lx", Rom.Size+Rom.Start-1);
328         sprintf(size, "%5lu", Rom.Size);
329         sprintf(max, "%5lu", code_size<0?Rom.Max:code_size);
330         fprintf(of, format, Rom.Name, start, end, size, max);
331
332         /*Report any excess:*/
333         if((IRam.Start+IRam.Size)>(IRam.Max+0x80))
334         {
335                 sprintf(buff, "Insufficient INDIRECT RAM memory.\n");
336                 REPORT_ERROR(buff, 1);
337         }
338         if( ((XRam.Start+XRam.Size)>XRam.Max) ||
339                 (((int)XRam.Size>xram_size)&&(xram_size>=0)) )
340         {
341                 sprintf(buff, "Insufficient EXTERNAL RAM memory.\n");
342                 REPORT_ERROR(buff, 1);
343         }
344         if( ((Rom.Start+Rom.Size)>Rom.Max) ||
345                 (((int)Rom.Size>code_size)&&(code_size>=0)) )
346         {
347                 sprintf(buff, "Insufficient ROM/EPROM/FLASH memory.\n");
348                 REPORT_ERROR(buff, 1);
349         }
350
351         fclose(of);
352         return toreturn;                
353 }
354
355 extern char idatamap[]; //0:not used, 1:used
356
357
358 int summary2(struct area * areap) 
359 {
360         #define EQ(A,B) !as_strcmpi((A),(B))
361
362         char buff[128];
363         int j, toreturn=0;
364     long int Stack_Start=0, Stack_size;
365
366         struct area * xp;
367         FILE * of;
368         
369         /*Artifacts used for printing*/
370         char start[15], end[15], size[15], max[15];
371         char format[]="   %-16.16s %-8.8s %-8.8s %-8.8s %-8.8s\n";
372         char line[]="---------------------";
373
374         typedef struct
375         {
376                 unsigned long Start;
377                 unsigned long Size;
378                 unsigned long Max;
379                 char Name[NCPS];
380                 unsigned long flag;
381         } _Mem;
382         
383         _Mem Stack={0xff,   0,     1, "STACK",                          0x0000};
384         _Mem XRam= {0xffff, 0, 65536, "EXTERNAL RAM",           0x0100};
385         _Mem Rom=  {0xffff, 0, 65536, "ROM/EPROM/FLASH",        0x0200};
386         
387         if(rflag) /*For the DS390*/
388         {
389                 XRam.Max=0x1000000; /*24 bits*/
390                 XRam.Start=0xffffff;
391                 Rom.Max=0x1000000;
392                 Rom.Start=0xffffff;
393         }
394
395         /* Open Memory Summary File*/
396         of = afile(linkp->f_idp, "mem", 1);
397         if (of == NULL)
398         {
399                 lkexit(1);
400         }
401
402         xp=areap;
403         while (xp)
404         {
405                 if( EQ(xp->a_id, "CSEG") || EQ(xp->a_id, "GSINIT") ||
406                                  EQ(xp->a_id, "GSFINAL") || EQ(xp->a_id, "HOME") )
407                 {
408                         Rom.Size+=xp->a_size;
409                         if(xp->a_addr<Rom.Start) Rom.Start=xp->a_addr;
410                 }
411                 
412                 else if (EQ(xp->a_id, "SSEG"))
413                 {
414                         Stack.Size+=xp->a_size;
415                         if(xp->a_addr<Stack.Start) Stack.Start=xp->a_addr;
416                 }
417
418                 else if (EQ(xp->a_id, "XSEG") || EQ(xp->a_id, "XISEG")) 
419                 {
420                         XRam.Size+=xp->a_size;
421                         if(xp->a_addr<XRam.Start) XRam.Start=xp->a_addr;
422                 }
423
424                 xp=xp->a_ap;
425         }
426
427         /*Report the Ram totals*/
428         fprintf(of, "Internal RAM layout:\n");
429         fprintf(of, "      0 1 2 3 4 5 6 7 8 9 A B C D E F");
430     for(j=0; j<256; j++)
431         {
432                 if(j%16==0) fprintf(of, "\n0x%02x:|", j);
433                 fprintf(of, "%c|", idatamap[j]);
434         }
435         fprintf(of, "\n0-3:Reg Banks, a-z:Data, B:Bits, Q:Overlay, I:iData, S:Stack\n");
436
437     for(j=0; j<256; j++)
438     {
439         if(idatamap[j]=='S')
440         {
441             Stack_Start=j;
442             break;
443         }
444     }
445
446     for(j=Stack_Start, Stack_size=0; j<256; j++)
447     {
448         if((idatamap[j]=='S')||(idatamap[j]==' ')) Stack_size++;
449         else break;
450     }
451     
452         xp=areap;
453         while (xp)
454     {
455         if(xp->a_unaloc>0)
456         {
457             fprintf(of, "\nERROR: Couldn't get %d byte%s allocated" 
458                         " in internal RAM for area %s.",
459                         xp->a_unaloc, xp->a_unaloc>1?"s":"", xp->a_id);
460             toreturn=1;
461         }
462                 xp=xp->a_ap;
463     }
464
465         /*Report the position of the begining of the stack*/
466     if(Stack_Start!=256)
467             fprintf(of, "\n%stack starts at: 0x%02lx (sp set to 0x%02lx) with %ld bytes available.",
468                     rflag ? "16 bit mode initial s" : "S", Stack_Start, Stack_Start-1, Stack_size);
469     else
470         fprintf(of, "\nI don't have a clue where the stack ended up! Sorry...");
471
472         fprintf(of, "\n\nOther memory:\n");
473         fprintf(of, format, "Name", "Start", "End", "Size", "Max");
474         fprintf(of, format, line, line, line, line, line);
475
476         /*Report XRam totals:*/
477         sprintf(start, "0x%04lx", XRam.Start);
478         if(XRam.Size==0)
479                 end[0]=0;/*Empty string*/
480         else
481                 sprintf(end,  "0x%04lx", XRam.Size+XRam.Start-1);
482         sprintf(size, "%5lu", XRam.Size);
483         sprintf(max, "%5lu", xram_size<0?XRam.Max:xram_size);
484         fprintf(of, format, XRam.Name, start, end, size, max);
485
486         /*Report Rom/Flash totals:*/
487         sprintf(start, "0x%04lx", Rom.Start);
488         if(Rom.Size==0)
489                 end[0]=0;/*Empty string*/
490         else
491                 sprintf(end,  "0x%04lx", Rom.Size+Rom.Start-1);
492         sprintf(size, "%5lu", Rom.Size);
493         sprintf(max, "%5lu", code_size<0?Rom.Max:code_size);
494         fprintf(of, format, Rom.Name, start, end, size, max);
495
496         /*Report any excess:*/
497         if( ((XRam.Start+XRam.Size)>XRam.Max) ||
498                 (((int)XRam.Size>xram_size)&&(xram_size>=0)) )
499         {
500                 sprintf(buff, "Insufficient EXTERNAL RAM memory.\n");
501                 REPORT_ERROR(buff, 1);
502         }
503         if( ((Rom.Start+Rom.Size)>Rom.Max) ||
504                 (((int)Rom.Size>code_size)&&(code_size>=0)) )
505         {
506                 sprintf(buff, "Insufficient ROM/EPROM/FLASH memory.\n");
507                 REPORT_ERROR(buff, 1);
508         }
509
510         fclose(of);
511         return toreturn;                
512 }