9ff0c161f9bf91956ce938565e31598dd2e114a7
[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
26 int summary(struct area * areap) 
27 {
28         #define EQ(A,B) !strcmpi((A),(B))
29         #define MIN_STACK 16
30         #define REPORT_ERROR(A, H) \
31         {\
32                 fprintf(of, "%s%s", (H)?"*** ERROR: ":"", (A)); \
33                 fprintf(stderr, "%s%s", (H)?"\n?ASlink-Error-":"",(A)); \
34                 toreturn=1; \
35         }
36
37         #define REPORT_WARNING(A, H) \
38         { \
39                 fprintf(of, "%s%s", (H)?"*** WARNING: ":"", (A)); \
40                 fprintf(stderr, "%s%s",(H)?"\n?ASlink-Warning-":"", (A)); \
41         }
42
43         char buff[128];
44         int j, toreturn=0;
45         unsigned int Total_Last=0, k; 
46
47         struct area * xp;
48         FILE * of;
49         
50         /*Artifacts used for printing*/
51         char start[15], end[15], size[15], max[15];
52         char format[]="   %-16.16s %-8.8s %-8.8s %-8.8s %-8.8s\n";
53         char line[]="---------------------";
54
55         typedef struct
56         {
57                 unsigned long Start;
58                 unsigned long Size;
59                 unsigned long Max;
60                 char Name[NCPS];
61                 unsigned long flag;
62         } _Mem;
63
64         unsigned int dram[0x100];
65         _Mem Ram[]={
66                 {0,             8,      8,       "REG_BANK_0", 0x0001},
67                 {0x8,   8,      8,       "REG_BANK_1", 0x0002},
68                 {0x10,  8,      8,       "REG_BANK_2", 0x0004},
69                 {0x18,  8,      8,       "REG_BANK_3", 0x0008},
70                 {0x20,  0,      16,      "BSEG_BYTES", 0x0010},
71                 {0,             0,      128, "UNUSED",     0x0000},
72                 {0x7f,  0,      128, "DATA",       0x0020},
73                 {0,             0,      128, "TOTAL:",     0x0000}
74         };
75         
76         _Mem IRam= {0xff,   0,   128, "INDIRECT RAM",           0x0080};
77         _Mem Stack={0xff,   0,     1, "STACK",                          0x0000};
78         _Mem XRam= {0xffff, 0, 65536, "EXTERNAL RAM",           0x0100};
79         _Mem Rom=  {0xffff, 0, 65536, "ROM/EPROM/FLASH",        0x0200};
80         
81         if(rflag) /*For the DS390*/
82         {
83                 XRam.Max=0x1000000; /*24 bits*/
84                 XRam.Start=0xffffff;
85                 Rom.Max=0x1000000;
86                 Rom.Start=0xffffff;
87         }
88
89         if((iram_size<=0)||(iram_size>0x100)) /*Default: 8052 like memory*/
90         {
91                 Ram[5].Max=0x80;
92                 Ram[6].Max=0x80;
93                 Ram[7].Max=0x80;
94                 IRam.Max=0x80;
95                 iram_size=0x100;
96         }
97         else if(iram_size<0x80)
98         {
99                 Ram[5].Max=iram_size;
100                 Ram[6].Max=iram_size;
101                 Ram[7].Max=iram_size;
102                 IRam.Max=0;
103         }
104         else
105         {
106                 Ram[5].Max=0x80;
107                 Ram[6].Max=0x80;
108                 Ram[7].Max=0x80;
109                 IRam.Max=iram_size-0x80;
110         }
111
112         for(j=0; j<(int)iram_size; j++) dram[j]=0;
113         for(; j<0x100; j++) dram[j]=0x8000; /*Memory not available*/
114
115         /* Open Memory Summary File*/
116         of = afile(linkp->f_idp, "mem", 1);
117         if (of == NULL)
118         {
119                 lkexit(1);
120         }
121
122         xp=areap;
123         while (xp)
124         {
125                 /**/ if (EQ(xp->a_id, "REG_BANK_0"))
126                 {
127                         Ram[0].Size=xp->a_size;
128                 }
129                 else if (EQ(xp->a_id, "REG_BANK_1"))
130                 {
131                         Ram[1].Size=xp->a_size;
132                 }
133                 else if (EQ(xp->a_id, "REG_BANK_2"))
134                 {
135                         Ram[2].Size=xp->a_size;
136                 }
137                 else if (EQ(xp->a_id, "REG_BANK_3"))
138                 {
139                         Ram[3].Size=xp->a_size;
140                 }
141                 else if (EQ(xp->a_id, "BSEG_BYTES"))
142                 {
143                         Ram[4].Size=xp->a_size;
144                 }
145                 else if ( EQ(xp->a_id, "DSEG") || EQ(xp->a_id, "OSEG") )
146                 {
147                         Ram[6].Size+=xp->a_size;
148                         if(xp->a_addr<Ram[6].Start) Ram[6].Start=xp->a_addr;
149                 }
150
151                 else if( EQ(xp->a_id, "CSEG") || EQ(xp->a_id, "GSINIT") ||
152                                  EQ(xp->a_id, "GSFINAL") || EQ(xp->a_id, "HOME") )
153                 {
154                         Rom.Size+=xp->a_size;
155                         if(xp->a_addr<Rom.Start) Rom.Start=xp->a_addr;
156                 }
157                 
158                 else if (EQ(xp->a_id, "SSEG"))
159                 {
160                         Stack.Size+=xp->a_size;
161                         if(xp->a_addr<Stack.Start) Stack.Start=xp->a_addr;
162                 }
163
164                 else if (EQ(xp->a_id, "XSEG") || EQ(xp->a_id, "XISEG")) 
165                 {
166                         XRam.Size+=xp->a_size;
167                         if(xp->a_addr<XRam.Start) XRam.Start=xp->a_addr;
168                 }
169
170                 else if (EQ(xp->a_id, "ISEG"))
171                 {
172                         IRam.Size+=xp->a_size;
173                         if(xp->a_addr<IRam.Start) IRam.Start=xp->a_addr;
174                 }
175                 xp=xp->a_ap;
176         }
177
178         for(j=0; j<7; j++)
179                 for(k=Ram[j].Start; (k<(Ram[j].Start+Ram[j].Size))&&(k<0x100); k++)
180                         dram[k]|=Ram[j].flag; /*Mark as used*/
181         
182         for(k=IRam.Start; (k<(IRam.Start+IRam.Size))&&(k<0x100); k++)
183                 dram[k]|=IRam.flag; /*Mark as used*/
184
185         /*Compute the amount of unused memory in direct data Ram.  This is the
186         gap between the last register bank or bit segment and the data segment.*/
187         for(k=Ram[6].Start-1; (dram[k]==0) && (k>0); k--);
188         Ram[5].Start=k+1;
189         Ram[5].Size=Ram[6].Start-Ram[5].Start; /*It may be zero (which is good!)*/
190
191         /*Compute the data Ram totals*/
192         for(j=0; j<7; j++)
193         {
194                 if(Ram[7].Start>Ram[j].Start) Ram[7].Start=Ram[j].Start;
195                 Ram[7].Size+=Ram[j].Size;
196         }
197         Total_Last=Ram[6].Size+Ram[6].Start-1;
198
199         /*Report the Ram totals*/
200         fprintf(of, "Direct Internal RAM:\n");
201         fprintf(of, format, "Name", "Start", "End", "Size", "Max");
202
203         for(j=0; j<8; j++)
204         {
205                 if((j==0) || (j==7)) fprintf(of, format, line, line, line, line, line);
206                 if((j!=5) || (Ram[j].Size>0))
207                 {
208                         sprintf(start, "0x%02lx", Ram[j].Start);
209                         if(Ram[j].Size==0)
210                                 end[0]=0;/*Empty string*/
211                         else
212                                 sprintf(end,  "0x%02lx", j==7?Total_Last:Ram[j].Size+Ram[j].Start-1);
213                         sprintf(size, "%5lu", Ram[j].Size);
214                         sprintf(max, "%5lu", Ram[j].Max);
215                         fprintf(of, format, Ram[j].Name, start, end, size, max);
216                 }
217         }
218
219         for(k=Ram[6].Start; (k<(Ram[6].Start+Ram[6].Size))&&(k<0x100); k++)
220         {
221                 if(dram[k]!=Ram[6].flag)
222                 {
223                         sprintf(buff, "Internal memory overlap starting at 0x%02x.\n", k);
224                         REPORT_ERROR(buff, 1);
225                         break;
226                 }
227         }
228
229         if(Ram[4].Size>Ram[4].Max)
230         {
231                 k=Ram[4].Size-Ram[4].Max;
232                 sprintf(buff, "Insufficient bit addressable memory.  "
233                                         "%d byte%s short.\n", k, (k==1)?"":"s");
234                 REPORT_ERROR(buff, 1);
235         }
236
237         if(Ram[5].Size!=0)
238         {
239                 sprintf(buff, "%ld bytes in DRAM wasted.  "
240                             "SDCC link could use: --data-loc 0x%02lx\n",
241                                         Ram[5].Size, Ram[6].Start-Ram[5].Size);
242                 REPORT_WARNING(buff, 1);
243         }
244
245         if((Ram[6].Start+Ram[6].Size)>Ram[6].Max)
246         {
247                 k=(Ram[6].Start+Ram[6].Size)-Ram[6].Max;
248                 sprintf(buff, "Insufficient DRAM memory.  "
249                                         "%d byte%s short.\n", k, (k==1)?"":"s");
250                 REPORT_ERROR(buff, 1);
251         }
252
253         /*Report the position of the begining of the stack*/
254         /* TODO find flag for DS390 */
255         fprintf(of, "\n%stack starts at: 0x%02lx (sp set to 0x%02lx)",
256                 0 ? "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 }