replace non-ANSI strcmpi by as_strcmpi
[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(rflag) /*For the DS390*/
83         {
84                 XRam.Max=0x1000000; /*24 bits*/
85                 XRam.Start=0xffffff;
86                 Rom.Max=0x1000000;
87                 Rom.Start=0xffffff;
88         }
89
90         if((iram_size<=0)||(iram_size>0x100)) /*Default: 8052 like memory*/
91         {
92                 Ram[5].Max=0x80;
93                 Ram[6].Max=0x80;
94                 Ram[7].Max=0x80;
95                 IRam.Max=0x80;
96                 iram_size=0x100;
97         }
98         else if(iram_size<0x80)
99         {
100                 Ram[5].Max=iram_size;
101                 Ram[6].Max=iram_size;
102                 Ram[7].Max=iram_size;
103                 IRam.Max=0;
104         }
105         else
106         {
107                 Ram[5].Max=0x80;
108                 Ram[6].Max=0x80;
109                 Ram[7].Max=0x80;
110                 IRam.Max=iram_size-0x80;
111         }
112
113         for(j=0; j<(int)iram_size; j++) dram[j]=0;
114         for(; j<0x100; j++) dram[j]=0x8000; /*Memory not available*/
115
116         /* Open Memory Summary File*/
117         of = afile(linkp->f_idp, "mem", 1);
118         if (of == NULL)
119         {
120                 lkexit(1);
121         }
122
123         xp=areap;
124         while (xp)
125         {
126                 /**/ if (EQ(xp->a_id, "REG_BANK_0"))
127                 {
128                         Ram[0].Size=xp->a_size;
129                 }
130                 else if (EQ(xp->a_id, "REG_BANK_1"))
131                 {
132                         Ram[1].Size=xp->a_size;
133                 }
134                 else if (EQ(xp->a_id, "REG_BANK_2"))
135                 {
136                         Ram[2].Size=xp->a_size;
137                 }
138                 else if (EQ(xp->a_id, "REG_BANK_3"))
139                 {
140                         Ram[3].Size=xp->a_size;
141                 }
142                 else if (EQ(xp->a_id, "BSEG_BYTES"))
143                 {
144                         Ram[4].Size=xp->a_size;
145                 }
146                 else if ( EQ(xp->a_id, "DSEG") || EQ(xp->a_id, "OSEG") )
147                 {
148                         Ram[6].Size+=xp->a_size;
149                         if(xp->a_addr<Ram[6].Start) Ram[6].Start=xp->a_addr;
150                 }
151
152                 else if( EQ(xp->a_id, "CSEG") || EQ(xp->a_id, "GSINIT") ||
153                                  EQ(xp->a_id, "GSFINAL") || EQ(xp->a_id, "HOME") )
154                 {
155                         Rom.Size+=xp->a_size;
156                         if(xp->a_addr<Rom.Start) Rom.Start=xp->a_addr;
157                 }
158                 
159                 else if (EQ(xp->a_id, "SSEG"))
160                 {
161                         Stack.Size+=xp->a_size;
162                         if(xp->a_addr<Stack.Start) Stack.Start=xp->a_addr;
163                 }
164
165                 else if (EQ(xp->a_id, "XSEG") || EQ(xp->a_id, "XISEG")) 
166                 {
167                         XRam.Size+=xp->a_size;
168                         if(xp->a_addr<XRam.Start) XRam.Start=xp->a_addr;
169                 }
170
171                 else if (EQ(xp->a_id, "ISEG"))
172                 {
173                         IRam.Size+=xp->a_size;
174                         if(xp->a_addr<IRam.Start) IRam.Start=xp->a_addr;
175                 }
176                 xp=xp->a_ap;
177         }
178
179         for(j=0; j<7; j++)
180                 for(k=Ram[j].Start; (k<(Ram[j].Start+Ram[j].Size))&&(k<0x100); k++)
181                         dram[k]|=Ram[j].flag; /*Mark as used*/
182         
183         for(k=IRam.Start; (k<(IRam.Start+IRam.Size))&&(k<0x100); k++)
184                 dram[k]|=IRam.flag; /*Mark as used*/
185
186         /*Compute the amount of unused memory in direct data Ram.  This is the
187         gap between the last register bank or bit segment and the data segment.*/
188         for(k=Ram[6].Start-1; (dram[k]==0) && (k>0); k--);
189         Ram[5].Start=k+1;
190         Ram[5].Size=Ram[6].Start-Ram[5].Start; /*It may be zero (which is good!)*/
191
192         /*Compute the data Ram totals*/
193         for(j=0; j<7; j++)
194         {
195                 if(Ram[7].Start>Ram[j].Start) Ram[7].Start=Ram[j].Start;
196                 Ram[7].Size+=Ram[j].Size;
197         }
198         Total_Last=Ram[6].Size+Ram[6].Start-1;
199
200         /*Report the Ram totals*/
201         fprintf(of, "Direct Internal RAM:\n");
202         fprintf(of, format, "Name", "Start", "End", "Size", "Max");
203
204         for(j=0; j<8; j++)
205         {
206                 if((j==0) || (j==7)) fprintf(of, format, line, line, line, line, line);
207                 if((j!=5) || (Ram[j].Size>0))
208                 {
209                         sprintf(start, "0x%02lx", Ram[j].Start);
210                         if(Ram[j].Size==0)
211                                 end[0]=0;/*Empty string*/
212                         else
213                                 sprintf(end,  "0x%02lx", j==7?Total_Last:Ram[j].Size+Ram[j].Start-1);
214                         sprintf(size, "%5lu", Ram[j].Size);
215                         sprintf(max, "%5lu", Ram[j].Max);
216                         fprintf(of, format, Ram[j].Name, start, end, size, max);
217                 }
218         }
219
220         for(k=Ram[6].Start; (k<(Ram[6].Start+Ram[6].Size))&&(k<0x100); k++)
221         {
222                 if(dram[k]!=Ram[6].flag)
223                 {
224                         sprintf(buff, "Internal memory overlap starting at 0x%02x.\n", k);
225                         REPORT_ERROR(buff, 1);
226                         break;
227                 }
228         }
229
230         if(Ram[4].Size>Ram[4].Max)
231         {
232                 k=Ram[4].Size-Ram[4].Max;
233                 sprintf(buff, "Insufficient bit addressable memory.  "
234                                         "%d byte%s short.\n", k, (k==1)?"":"s");
235                 REPORT_ERROR(buff, 1);
236         }
237
238         if(Ram[5].Size!=0)
239         {
240                 sprintf(buff, "%ld bytes in DRAM wasted.  "
241                             "SDCC link could use: --data-loc 0x%02lx\n",
242                                         Ram[5].Size, Ram[6].Start-Ram[5].Size);
243                 REPORT_WARNING(buff, 1);
244         }
245
246         if((Ram[6].Start+Ram[6].Size)>Ram[6].Max)
247         {
248                 k=(Ram[6].Start+Ram[6].Size)-Ram[6].Max;
249                 sprintf(buff, "Insufficient DRAM memory.  "
250                                         "%d byte%s short.\n", k, (k==1)?"":"s");
251                 REPORT_ERROR(buff, 1);
252         }
253
254         /*Report the position of the begining of the stack*/
255         fprintf(of, "\n%stack starts at: 0x%02lx (sp set to 0x%02lx)",
256                 rflag ? "16 bit mode initial s" : "S", Stack.Start, Stack.Start-1);
257
258         /*Check that the stack pointer is landing in a safe place:*/
259         if( (dram[Stack.Start] & 0x8000) == 0x8000 )
260         {
261                 fprintf(of, ".\n");
262                 sprintf(buff, "Stack set to unavailable memory.\n");
263                 REPORT_ERROR(buff, 1);
264         }
265         else if(dram[Stack.Start])
266         {
267                 fprintf(of, ".\n");
268                 sprintf(buff, "Stack overlaps area ");
269                 REPORT_ERROR(buff, 1);
270                 for(j=0; j<7; j++)
271                 {
272                         if(dram[Stack.Start]&Ram[j].flag)
273                         {
274                                 sprintf(buff, "'%s'\n", Ram[j].Name);
275                                 break;
276                         }
277                 }
278                 if(dram[Stack.Start]&IRam.flag)
279                 {
280                         sprintf(buff, "'%s'\n", IRam.Name);
281                 }
282                 REPORT_ERROR(buff, 0);
283         }
284         else
285         {
286                 for(j=Stack.Start, k=0; (j<(int)iram_size)&&(dram[j]==0); j++, k++);
287                 fprintf(of, " with %d bytes available\n", k);
288                 if (k<MIN_STACK)
289                 {
290                         sprintf(buff, "Only %d byte%s available for stack.\n",
291                                 k, (k==1)?"":"s");
292                         REPORT_WARNING(buff, 1);
293                 }
294         }
295
296         fprintf(of, "\nOther memory:\n");
297         fprintf(of, format, "Name", "Start", "End", "Size", "Max");
298         fprintf(of, format, line, line, line, line, line);
299
300         /*Report IRam totals:*/
301         sprintf(start, "0x%02lx", IRam.Start);
302         if(IRam.Size==0)
303                 end[0]=0;/*Empty string*/
304         else
305                 sprintf(end,  "0x%02lx", IRam.Size+IRam.Start-1);
306         sprintf(size, "%5lu", IRam.Size);
307         sprintf(max, "%5lu", IRam.Max);
308         fprintf(of, format, IRam.Name, start, end, size, max);
309
310         /*Report XRam totals:*/
311         sprintf(start, "0x%04lx", XRam.Start);
312         if(XRam.Size==0)
313                 end[0]=0;/*Empty string*/
314         else
315                 sprintf(end,  "0x%04lx", XRam.Size+XRam.Start-1);
316         sprintf(size, "%5lu", XRam.Size);
317         sprintf(max, "%5lu", XRam.Max);
318         fprintf(of, format, XRam.Name, start, end, size, max);
319
320         /*Report Rom/Flash totals:*/
321         sprintf(start, "0x%04lx", Rom.Start);
322         if(Rom.Size==0)
323                 end[0]=0;/*Empty string*/
324         else
325                 sprintf(end,  "0x%04lx", Rom.Size+Rom.Start-1);
326         sprintf(size, "%5lu", Rom.Size);
327         sprintf(max, "%5lu", Rom.Max);
328         fprintf(of, format, Rom.Name, start, end, size, max);
329
330         /*Report any excess:*/
331         if((IRam.Start+IRam.Size)>(IRam.Max+0x80))
332         {
333                 sprintf(buff, "Insufficient INDIRECT RAM memory.\n");
334                 REPORT_ERROR(buff, 1);
335         }
336         if((XRam.Start+XRam.Size)>XRam.Max)
337         {
338                 sprintf(buff, "Insufficient EXTERNAL RAM memory.\n");
339                 REPORT_ERROR(buff, 1);
340         }
341         if((Rom.Start+Rom.Size)>Rom.Max)
342         {
343                 sprintf(buff, "Insufficient ROM/EPROM/FLASH memory.\n");
344                 REPORT_ERROR(buff, 1);
345         }
346
347         fclose(of);
348         return toreturn;                
349 }