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) !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, 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}
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};
81 if(rflag) /*For the DS390*/
83 XRam.Max=0x1000000; /*24 bits*/
89 if((iram_size<=0)||(iram_size>0x100)) /*Default: 8052 like memory*/
97 else if(iram_size<0x80)
100 Ram[6].Max=iram_size;
101 Ram[7].Max=iram_size;
109 IRam.Max=iram_size-0x80;
112 for(j=0; j<(int)iram_size; j++) dram[j]=0;
113 for(; j<0x100; j++) dram[j]=0x8000; /*Memory not available*/
115 /* Open Memory Summary File*/
116 of = afile(linkp->f_idp, "mem", 1);
125 /**/ if (EQ(xp->a_id, "REG_BANK_0"))
127 Ram[0].Size=xp->a_size;
129 else if (EQ(xp->a_id, "REG_BANK_1"))
131 Ram[1].Size=xp->a_size;
133 else if (EQ(xp->a_id, "REG_BANK_2"))
135 Ram[2].Size=xp->a_size;
137 else if (EQ(xp->a_id, "REG_BANK_3"))
139 Ram[3].Size=xp->a_size;
141 else if (EQ(xp->a_id, "BSEG_BYTES"))
143 Ram[4].Size=xp->a_size;
145 else if ( EQ(xp->a_id, "DSEG") || EQ(xp->a_id, "OSEG") )
147 Ram[6].Size+=xp->a_size;
148 if(xp->a_addr<Ram[6].Start) Ram[6].Start=xp->a_addr;
151 else if( EQ(xp->a_id, "CSEG") || EQ(xp->a_id, "GSINIT") ||
152 EQ(xp->a_id, "GSFINAL") || EQ(xp->a_id, "HOME") )
154 Rom.Size+=xp->a_size;
155 if(xp->a_addr<Rom.Start) Rom.Start=xp->a_addr;
158 else if (EQ(xp->a_id, "SSEG"))
160 Stack.Size+=xp->a_size;
161 if(xp->a_addr<Stack.Start) Stack.Start=xp->a_addr;
164 else if (EQ(xp->a_id, "XSEG") || EQ(xp->a_id, "XISEG"))
166 XRam.Size+=xp->a_size;
167 if(xp->a_addr<XRam.Start) XRam.Start=xp->a_addr;
170 else if (EQ(xp->a_id, "ISEG"))
172 IRam.Size+=xp->a_size;
173 if(xp->a_addr<IRam.Start) IRam.Start=xp->a_addr;
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*/
182 for(k=IRam.Start; (k<(IRam.Start+IRam.Size))&&(k<0x100); k++)
183 dram[k]|=IRam.flag; /*Mark as used*/
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--);
189 Ram[5].Size=Ram[6].Start-Ram[5].Start; /*It may be zero (which is good!)*/
191 /*Compute the data Ram totals*/
194 if(Ram[7].Start>Ram[j].Start) Ram[7].Start=Ram[j].Start;
195 Ram[7].Size+=Ram[j].Size;
197 Total_Last=Ram[6].Size+Ram[6].Start-1;
199 /*Report the Ram totals*/
200 fprintf(of, "Direct Internal RAM:\n");
201 fprintf(of, format, "Name", "Start", "End", "Size", "Max");
205 if((j==0) || (j==7)) fprintf(of, format, line, line, line, line, line);
206 if((j!=5) || (Ram[j].Size>0))
208 sprintf(start, "0x%02lx", Ram[j].Start);
210 end[0]=0;/*Empty string*/
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);
219 for(k=Ram[6].Start; (k<(Ram[6].Start+Ram[6].Size))&&(k<0x100); k++)
221 if(dram[k]!=Ram[6].flag)
223 sprintf(buff, "Internal memory overlap starting at 0x%02x.\n", k);
224 REPORT_ERROR(buff, 1);
229 if(Ram[4].Size>Ram[4].Max)
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);
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);
245 if((Ram[6].Start+Ram[6].Size)>Ram[6].Max)
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);
253 /*Report the position of the begining of the stack*/
254 fprintf(of, "\nStack starts at: 0x%02lx", Stack.Start);
256 /*Check that the stack pointer is landing in a safe place:*/
257 if( (dram[Stack.Start] & 0x8000) == 0x8000 )
260 sprintf(buff, "Stack set to unavailable memory.\n");
261 REPORT_ERROR(buff, 1);
263 else if(dram[Stack.Start])
266 sprintf(buff, "Stack overlaps area ");
267 REPORT_ERROR(buff, 1);
270 if(dram[Stack.Start]&Ram[j].flag)
272 sprintf(buff, "'%s'\n", Ram[j].Name);
276 if(dram[Stack.Start]&IRam.flag)
278 sprintf(buff, "'%s'\n", IRam.Name);
280 REPORT_ERROR(buff, 0);
284 for(j=Stack.Start, k=0; (j<(int)iram_size)&&(dram[j]==0); j++, k++);
285 fprintf(of, " with %d bytes available\n", k);
288 sprintf(buff, "Only %d byte%s available for stack.\n",
290 REPORT_WARNING(buff, 1);
294 fprintf(of, "\nOther memory:\n");
295 fprintf(of, format, "Name", "Start", "End", "Size", "Max");
296 fprintf(of, format, line, line, line, line, line);
298 /*Report IRam totals:*/
299 sprintf(start, "0x%02lx", IRam.Start);
301 end[0]=0;/*Empty string*/
303 sprintf(end, "0x%02lx", IRam.Size+IRam.Start-1);
304 sprintf(size, "%5lu", IRam.Size);
305 sprintf(max, "%5lu", IRam.Max);
306 fprintf(of, format, IRam.Name, start, end, size, max);
308 /*Report XRam totals:*/
309 sprintf(start, "0x%04lx", XRam.Start);
311 end[0]=0;/*Empty string*/
313 sprintf(end, "0x%04lx", XRam.Size+XRam.Start-1);
314 sprintf(size, "%5lu", XRam.Size);
315 sprintf(max, "%5lu", XRam.Max);
316 fprintf(of, format, XRam.Name, start, end, size, max);
318 /*Report Rom/Flash totals:*/
319 sprintf(start, "0x%04lx", Rom.Start);
321 end[0]=0;/*Empty string*/
323 sprintf(end, "0x%04lx", Rom.Size+Rom.Start-1);
324 sprintf(size, "%5lu", Rom.Size);
325 sprintf(max, "%5lu", Rom.Max);
326 fprintf(of, format, Rom.Name, start, end, size, max);
328 /*Report any excess:*/
329 if((IRam.Start+IRam.Size)>(IRam.Max+0x80))
331 sprintf(buff, "Insufficient INDIRECT RAM memory.\n");
332 REPORT_ERROR(buff, 1);
334 if((XRam.Start+XRam.Size)>XRam.Max)
336 sprintf(buff, "Insufficient EXTERNAL RAM memory.\n");
337 REPORT_ERROR(buff, 1);
339 if((Rom.Start+Rom.Size)>Rom.Max)
341 sprintf(buff, "Insufficient ROM/EPROM/FLASH memory.\n");
342 REPORT_ERROR(buff, 1);