1 /*-------------------------------------------------------------------------
2 lkmem.c - Create a memory summary file with extension .mem
4 Written By - Jesus Calvino-Fraga, jesusc@ieee.org (2002)
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
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.
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 -------------------------------------------------------------------------*/
26 int summary(struct area * areap)
28 #define EQ(A,B) !as_strcmpi((A),(B))
30 #define REPORT_ERROR(A, H) \
32 fprintf(of, "%s%s", (H)?"*** ERROR: ":"", (A)); \
33 fprintf(stderr, "%s%s", (H)?"\n?ASlink-Error-":"",(A)); \
37 #define REPORT_WARNING(A, H) \
39 fprintf(of, "%s%s", (H)?"*** WARNING: ":"", (A)); \
40 fprintf(stderr, "%s%s",(H)?"\n?ASlink-Warning-":"", (A)); \
45 unsigned int Total_Last=0, k;
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[]="---------------------";
64 unsigned int dram[0x100];
66 {0, 0, 0, "REG_BANK_0", 0x0001},
67 {0x0, 0, 0, "REG_BANK_1", 0x0002},
68 {0x0, 0, 0, "REG_BANK_2", 0x0004},
69 {0x0, 0, 0, "REG_BANK_3", 0x0008},
70 {0x0, 0, 0, "BSEG_BYTES", 0x0010},
71 {0, 0, 256, "UNUSED", 0x0000},
72 {0xff, 0, 256, "DATA", 0x0020},
73 {0, 0, 256, "TOTAL:", 0x0000}
76 _Mem IRam= {0xff, 0, 0, "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};
82 if(rflag) /*For the DS390*/
84 XRam.Max=0x1000000; /*24 bits*/
90 if((iram_size<=0)||(iram_size>0x100)) /*Default: 8052 like memory*/
98 else if(iram_size<0x80)
100 Ram[5].Max=iram_size;
101 Ram[6].Max=iram_size;
102 Ram[7].Max=iram_size;
110 IRam.Max=iram_size-0x80;
114 for(j=0; j<(int)iram_size; j++) dram[j]=0;
115 for(; j<0x100; j++) dram[j]=0x8000; /*Memory not available*/
117 /* Open Memory Summary File*/
118 of = afile(linkp->f_idp, "mem", 1);
127 /**/ if (EQ(xp->a_id, "REG_BANK_0"))
129 Ram[0].Size=xp->a_size;
131 else if (EQ(xp->a_id, "REG_BANK_1"))
133 Ram[1].Size=xp->a_size;
135 else if (EQ(xp->a_id, "REG_BANK_2"))
137 Ram[2].Size=xp->a_size;
139 else if (EQ(xp->a_id, "REG_BANK_3"))
141 Ram[3].Size=xp->a_size;
143 else if (EQ(xp->a_id, "BSEG_BYTES"))
145 Ram[4].Size=xp->a_size;
147 else if ( EQ(xp->a_id, "DSEG") || EQ(xp->a_id, "OSEG") )
149 Ram[6].Size+=xp->a_size;
150 if(xp->a_addr<Ram[6].Start) Ram[6].Start=xp->a_addr;
153 else if( EQ(xp->a_id, "CSEG") || EQ(xp->a_id, "GSINIT") ||
154 EQ(xp->a_id, "GSFINAL") || EQ(xp->a_id, "HOME") )
156 Rom.Size+=xp->a_size;
157 if(xp->a_addr<Rom.Start) Rom.Start=xp->a_addr;
160 else if (EQ(xp->a_id, "SSEG"))
162 Stack.Size+=xp->a_size;
163 if(xp->a_addr<Stack.Start) Stack.Start=xp->a_addr;
166 else if (EQ(xp->a_id, "XSEG") || EQ(xp->a_id, "XISEG"))
168 XRam.Size+=xp->a_size;
169 if(xp->a_addr<XRam.Start) XRam.Start=xp->a_addr;
172 else if (EQ(xp->a_id, "ISEG"))
174 IRam.Size+=xp->a_size;
175 if(xp->a_addr<IRam.Start) IRam.Start=xp->a_addr;
181 for(k=Ram[j].Start; (k<(Ram[j].Start+Ram[j].Size))&&(k<0x100); k++)
182 dram[k]|=Ram[j].flag; /*Mark as used*/
185 for(k=IRam.Start; (k<(IRam.Start+IRam.Size))&&(k<0x100); k++)
186 dram[k]|=IRam.flag; /*Mark as used*/
189 /*Compute the amount of unused memory in direct data Ram. This is the
190 gap between the last register bank or bit segment and the data segment.*/
191 for(k=Ram[6].Start-1; (dram[k]==0) && (k>0); k--);
193 Ram[5].Size=Ram[6].Start-Ram[5].Start; /*It may be zero (which is good!)*/
195 /*Compute the data Ram totals*/
198 if(Ram[7].Start>Ram[j].Start) Ram[7].Start=Ram[j].Start;
199 Ram[7].Size+=Ram[j].Size;
201 Total_Last=Ram[6].Size+Ram[6].Start-1;
203 /*Report the Ram totals*/
204 fprintf(of, "Direct Internal RAM:\n");
205 fprintf(of, format, "Name", "Start", "End", "Size", "Max");
209 if((j==0) || (j==7)) fprintf(of, format, line, line, line, line, line);
210 if((j!=5) || (Ram[j].Size>0))
212 sprintf(start, "0x%02lx", Ram[j].Start);
214 end[0]=0;/*Empty string*/
216 sprintf(end, "0x%02lx", j==7?Total_Last:Ram[j].Size+Ram[j].Start-1);
217 sprintf(size, "%5lu", Ram[j].Size);
218 sprintf(max, "%5lu", Ram[j].Max);
219 fprintf(of, format, Ram[j].Name, start, end, size, max);
224 for(k=Ram[6].Start; (k<(Ram[6].Start+Ram[6].Size))&&(k<0x100); k++)
226 if(dram[k]!=Ram[6].flag)
228 sprintf(buff, "Internal memory overlap starting at 0x%02x.\n", k);
229 REPORT_ERROR(buff, 1);
235 if(Ram[4].Size>Ram[4].Max)
237 k=Ram[4].Size-Ram[4].Max;
238 sprintf(buff, "Insufficient bit addressable memory. "
239 "%d byte%s short.\n", k, (k==1)?"":"s");
240 REPORT_ERROR(buff, 1);
245 sprintf(buff, "%ld bytes in data memory wasted. "
246 "SDCC link could use: --data-loc 0x%02lx\n",
247 Ram[5].Size, Ram[6].Start-Ram[5].Size);
248 REPORT_WARNING(buff, 1);
251 if((Ram[6].Start+Ram[6].Size)>Ram[6].Max)
253 k=(Ram[6].Start+Ram[6].Size)-Ram[6].Max;
254 sprintf(buff, "Insufficient space in data memory. "
255 "%d byte%s short.\n", k, (k==1)?"":"s");
256 REPORT_ERROR(buff, 1);
259 /*Report the position of the beginning of the stack*/
260 fprintf(of, "\n%stack starts at: 0x%02lx (sp set to 0x%02lx)",
261 rflag ? "16 bit mode initial s" : "S", Stack.Start, Stack.Start-1);
264 /*Check that the stack pointer is landing in a safe place:*/
265 if( (dram[Stack.Start] & 0x8000) == 0x8000 )
268 sprintf(buff, "Stack set to unavailable memory.\n");
269 REPORT_ERROR(buff, 1);
271 else if(dram[Stack.Start])
274 sprintf(buff, "Stack overlaps area ");
275 REPORT_ERROR(buff, 1);
278 if(dram[Stack.Start]&Ram[j].flag)
280 sprintf(buff, "'%s'\n", Ram[j].Name);
284 if(dram[Stack.Start]&IRam.flag)
286 sprintf(buff, "'%s'\n", IRam.Name);
288 REPORT_ERROR(buff, 0);
292 for(j=Stack.Start, k=0; (j<(int)iram_size)&&(dram[j]==0); j++, k++);
293 fprintf(of, " with %d bytes available\n", k);
296 sprintf(buff, "Only %d byte%s available for stack.\n",
298 REPORT_WARNING(buff, 1);
303 fprintf(of, "\nOther memory:\n");
304 fprintf(of, format, "Name", "Start", "End", "Size", "Max");
305 fprintf(of, format, line, line, line, line, line);
307 /*Report IRam totals:*/
308 sprintf(start, "0x%02lx", IRam.Start);
310 end[0]=0;/*Empty string*/
312 sprintf(end, "0x%02lx", IRam.Size+IRam.Start-1);
313 sprintf(size, "%5lu", IRam.Size);
314 sprintf(max, "%5lu", IRam.Max);
315 fprintf(of, format, IRam.Name, start, end, size, max);
317 /*Report XRam totals:*/
318 sprintf(start, "0x%04lx", XRam.Start);
320 end[0]=0;/*Empty string*/
322 sprintf(end, "0x%04lx", XRam.Size+XRam.Start-1);
323 sprintf(size, "%5lu", XRam.Size);
324 sprintf(max, "%5lu", xram_size<0?XRam.Max:xram_size);
325 fprintf(of, format, XRam.Name, start, end, size, max);
327 /*Report Rom/Flash totals:*/
328 sprintf(start, "0x%04lx", Rom.Start);
330 end[0]=0;/*Empty string*/
332 sprintf(end, "0x%04lx", Rom.Size+Rom.Start-1);
333 sprintf(size, "%5lu", Rom.Size);
334 sprintf(max, "%5lu", code_size<0?Rom.Max:code_size);
335 fprintf(of, format, Rom.Name, start, end, size, max);
337 /*Report any excess:*/
339 if((IRam.Start+IRam.Size)>(IRam.Max+0x80))
341 sprintf(buff, "Insufficient INDIRECT RAM memory.\n");
342 REPORT_ERROR(buff, 1);
345 if( ((XRam.Start+XRam.Size)>XRam.Max) ||
346 (((int)XRam.Size>xram_size)&&(xram_size>=0)) )
348 sprintf(buff, "Insufficient EXTERNAL RAM memory.\n");
349 REPORT_ERROR(buff, 1);
351 if( ((Rom.Start+Rom.Size)>Rom.Max) ||
352 (((int)Rom.Size>code_size)&&(code_size>=0)) )
354 sprintf(buff, "Insufficient ROM/EPROM/FLASH memory.\n");
355 REPORT_ERROR(buff, 1);