Do not generate dump file .d51
[fw/sdcc] / as / mcs51 / lkaomf51.c
1 /*-------------------------------------------------------------------------
2   lkaomf51.c - Create an absolute object memory format 51 file
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 <ctype.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include "aslink.h"
26
27 #define EQ(A,B) !strcmp((A),(B))
28 #define MEMSIZE 0x10000
29 //#define DODUMP 1
30
31 typedef struct
32 {
33         char PathName[PATH_MAX];
34         char ModuleName[PATH_MAX];
35 } _infn;
36
37 int numin=0;
38 _infn * infn=NULL;
39
40 char ihxFileName[PATH_MAX];
41 char aomf51FileName[PATH_MAX];
42
43 typedef struct
44 {
45    char name[0x100];
46    int FileNameNumber;
47    int Procedure;//If the symbol belongs to a function
48    int Static; //If the symbol is only public on its file
49    int Address;
50    int UsageType;
51 } _symbol;
52
53 int numsym=0;
54 _symbol * symbol=NULL;
55
56 typedef struct
57 {
58    char name[0x100];
59    int FileNameNumber;
60    int BeginAdd;
61    int EndAdd;
62 } _procedure;
63
64 int numproc=0;
65 _procedure * procedure=NULL;
66
67 typedef struct
68 {
69    int Number;
70    int Address;
71    int Procedure;
72    int FileNameNumber;
73 } _linenum;
74
75 int numlinenum=0;
76 _linenum * linenum=NULL;
77
78 typedef struct
79 {
80         char * name;
81         int usage;
82 }
83 _UsageType;
84
85 _UsageType UsageType[]=
86 {
87         {"CSEG",                0},
88         {"GSINIT",              0},
89         {"GSFINAL",             0},
90         {"HOME",                0},
91         {"XINIT",               0},
92         {"XSEG",                1},
93         {"XISEG",               1},
94         {"REG_BANK_0",  2},
95         {"REG_BANK_1",  2},
96         {"REG_BANK_2",  2},
97         {"REG_BANK_3",  2},
98         {"DSEG",                2},
99         {"OSEG",                2},
100         {"SSEG",                2},
101         {"ISEG",                3},
102         {"BSEG",                4},
103         {"",                    5} /*A typeless number?*/
104 };
105
106 char * UsageTypeName[]={"CODE", "XDATA", "DATA", "IDATA", "BIT", "NUMBER"};
107 int AddNumber;
108 unsigned char * ihxBuff=NULL;
109 FILE * aomf51out;
110 int GlobalChkSum=0;
111 int HexSize, HexBegin=0x10000;
112
113
114 void GetName(char * filepath, char * name)
115 {
116         int j, k;
117         for(j=strlen(filepath); j>0; j--)
118                 if( (filepath[j-1]=='/')||(filepath[j-1]=='\\') ) break;
119         for(k=0; (filepath[j]!=0)&&(filepath[j]!='.'); j++, k++)
120                 name[k]=filepath[j];
121         name[k]=0;
122 }
123
124 void SaveLinkedFilePath(char * filepath) 
125 {
126         int j;
127
128         if((dflag) && (!rflag))
129         {
130                 infn=realloc(infn, sizeof(_infn)*(numin+1));
131
132                 strcpy(infn[numin].PathName, filepath);
133                 j=strlen(infn[numin].PathName);
134                 
135                 /*If there is an extension remove it*/
136                 if(j>=4)
137                 {
138                         if(EQ(&infn[numin].PathName[j-4], ".rel"))
139                         {
140                                 infn[numin].PathName[j-4]=0;
141                         }
142                 }
143
144                 /*Get the module name=filename, no drive, no dir, no ext*/
145                 GetName(infn[numin].PathName, infn[numin].ModuleName);
146                 //printf("%s, %s\n", infn[numin].PathName, infn[numin].ModuleName);
147                 
148                 /*Check if this filename is already in*/
149                 for(j=0; j<numin; j++)
150                 {
151                         if(EQ(infn[numin].PathName, infn[j].PathName)) break;
152                 }
153                 if(j==numin) numin++;
154         }
155 }
156
157 void FreeAll(void)
158 {
159         if(infn!=NULL)
160         {
161                 free(infn);
162                 numin=0;
163                 infn=NULL;
164         }
165
166         if(symbol!=NULL)
167         {
168                 free(symbol);
169                 numsym=0;
170                 symbol=NULL;
171         }
172
173         if(procedure!=NULL)
174         {
175                 free(procedure);
176                 numproc=0;
177                 procedure=NULL;
178
179         }
180         if(linenum!=NULL)
181         {
182                 free(linenum);
183                 numlinenum=0;
184                 linenum=NULL;
185         }
186
187         if(ihxBuff!=NULL)
188         {
189                 free(ihxBuff);
190                 ihxBuff=NULL;
191         }
192 }
193
194 void OutputByte(unsigned char value)
195 {
196         GlobalChkSum+=value;
197         fwrite( &value, 1, 1, aomf51out );
198 }
199
200 void OutputWord(int value)
201 {
202         OutputByte((unsigned char)(value%0x100));
203         OutputByte((unsigned char)(value/0x100));
204 }
205
206 void OutputName(char * name)
207 {
208         int k;
209         OutputByte((unsigned char)strlen(name));
210         for(k=0; name[k]!=0; k++)
211                 OutputByte((unsigned char)toupper(name[k]));
212 }
213
214 void OutputChkSum(void)
215 {
216         OutputByte((unsigned char)(0x100-(GlobalChkSum%0x100)));
217         GlobalChkSum=0;
218 }
219
220 #ifdef DODUMP
221 void DumpForDebug (void)
222 {
223         char DumpFileName[PATH_MAX];
224         FILE * DumpFile;
225         int j, k;
226
227         strcpy(DumpFileName, infn[0].PathName);
228         strcat(DumpFileName, ".d51");
229
230         DumpFile=fopen(DumpFileName, "wb");
231         if(DumpFile==NULL)
232         {
233                 printf("Couldn't create file %s\n", DumpFileName);
234                 return;
235         }
236
237         fprintf(DumpFile,"SYMBOLS:\n");
238
239         for(j=0; j<numsym; j++)
240         {
241                 k=symbol[j].UsageType&0xf;
242                 fprintf(DumpFile, "%s, %s, %s, 0x%04x, %s\n",
243                         symbol[j].name,
244                         infn[symbol[j].FileNameNumber].PathName,
245                         (symbol[j].Procedure>=0)?procedure[symbol[j].Procedure].name:"GLOBAL",
246                         symbol[j].Address,
247                         k<6?UsageTypeName[k]:"???");
248         }
249         
250         fprintf(DumpFile,"\nPROCEDURES:\n");
251         for(j=0; j<numproc; j++)
252         {
253                 fprintf(DumpFile, "%s, %s, 0x%04x-0x%04x\n",
254                         procedure[j].name,
255                         infn[procedure[j].FileNameNumber].PathName,
256                         procedure[j].BeginAdd,
257                         procedure[j].EndAdd);
258         }
259
260         fprintf(DumpFile,"\nLINE NUMBERS:\n");
261         for(j=0; j<numlinenum; j++)
262         {
263                 fprintf(DumpFile, "%d:0x%04x, %s -> %s\n",
264                         linenum[j].Number,
265                         linenum[j].Address,
266                         infn[linenum[j].FileNameNumber].PathName,
267                         (linenum[j].Procedure>=0)?procedure[linenum[j].Procedure].name:"I don't know");
268         }
269
270         fclose(DumpFile);
271 }
272 #endif
273
274 void OutputAOEMF51(void)
275 {
276         int i, j, k, recsize;
277         char MHRname[0x100], Mname[0x100];
278
279         strcpy(aomf51FileName, infn[0].PathName);
280
281         aomf51out=fopen(aomf51FileName, "wb");
282         if(aomf51out==NULL)
283         {
284                 printf("Couldn't create file %s\n", aomf51FileName);
285                 return;
286         }
287
288         GetName(infn[0].PathName, MHRname);
289         GlobalChkSum=0;
290         
291         /*Module header record*/
292         OutputByte(0x02);/*REC TYPE*/
293         OutputWord((strlen(MHRname)+1)+3);/*Record Length*/
294         OutputName(MHRname);/*Module Name*/
295         OutputByte(0xff);/*TRN ID: RL51?*/
296         OutputByte(0x00);
297         OutputChkSum();
298
299         for(j=0; j<numin; j++)
300         {
301                 GetName(infn[j].PathName, Mname);
302
303                 /*Scope Definition record: begin module block*/
304                 OutputByte(0x10);/*REC TYPE*/
305                 OutputWord((strlen(Mname)+1)+2);/*Record Length*/
306                 OutputByte(0x00);/*BLK TYP: module block*/
307                 OutputName(Mname);/*Module Name*/
308                 OutputChkSum();
309
310                 /*Public symbols defined in this module*/
311                 recsize=2;
312                 for(k=0; k<numsym; k++)/*Compute the record length*/
313                         if ( (symbol[k].FileNameNumber==j) && (symbol[k].Address!=-1) &&
314                                  (symbol[k].Procedure==-1) &&
315                                  (symbol[k].Static==-1) ) recsize+=((strlen(symbol[k].name)+1)+5);
316
317                 if(recsize>2) /*If there are any symbols*/
318                 {
319                         OutputByte(0x12);       /*REC TYPE*/
320                         OutputWord(recsize);/*Record Length*/
321                         OutputByte(0x01);       /*DEF TYPE: Public symbols*/
322                         for(k=0; k<numsym; k++)
323                         {
324                                 if ( (symbol[k].FileNameNumber==j) && (symbol[k].Address!=-1) &&
325                                          (symbol[k].Procedure==-1) &&
326                                          (symbol[k].Static==-1) )
327                                 {
328                                         OutputByte(0x00);/*SEG ID*/
329                                         OutputByte((unsigned char)symbol[k].UsageType);/*SYM INFO*/
330                                         OutputWord(symbol[k].Address);/*Offset*/
331                                         OutputByte(0x00);
332                                         OutputName(symbol[k].name);/*Symbol name*/
333                                 }
334                         }
335                         OutputChkSum();
336                 }
337
338                 /*Local symbols defined in this module*/
339                 recsize=2;
340                 for(k=0; k<numsym; k++)/*Compute the record length*/
341                         if ( (symbol[k].FileNameNumber==j) && (symbol[k].Address!=-1) &&
342                                  (symbol[k].Procedure==-1) &&
343                                  (symbol[k].Static==j) ) recsize+=((strlen(symbol[k].name)+1)+5);
344
345                 if(recsize>2) /*If there are any symbols*/
346                 {
347                         OutputByte(0x12);       /*REC TYPE*/
348                         OutputWord(recsize);/*Record Length*/
349                         OutputByte(0x00);       /*DEF TYPE: Local symbols*/
350                         for(k=0; k<numsym; k++)
351                         {
352                                 if ( (symbol[k].FileNameNumber==j) && (symbol[k].Address!=-1) &&
353                                          (symbol[k].Procedure==-1) &&
354                                          (symbol[k].Static==j) )
355                                 {
356                                         OutputByte(0x00);/*SEG ID*/
357                                         OutputByte((unsigned char)symbol[k].UsageType);/*SYM INFO*/
358                                         OutputWord(symbol[k].Address);/*Offset*/
359                                         OutputByte(0x00);
360                                         OutputName(symbol[k].name);/*Symbol name*/
361                                 }
362                         }
363                         OutputChkSum();
364                 }
365
366                 /*Output the procedures of this module*/
367
368                 for(k=0; k<numproc; k++)
369                 {
370                         if(procedure[k].FileNameNumber==j)
371                         {
372                                 /*Scope Definition record: begin PROCEDURE block*/
373                                 OutputByte(0x10);/*REC TYPE*/
374                                 OutputWord((strlen(procedure[k].name)+1)+2);/*Record Length*/
375                                 OutputByte(0x02);/*BLK TYP: PROCEDURE block*/
376                                 OutputName(procedure[k].name);/*Module Name*/
377                                 OutputChkSum();
378
379                                 /*Content Record*/
380                                 OutputByte(0x06);/*REC TYPE*/
381                                 recsize=procedure[k].EndAdd-procedure[k].BeginAdd+1+4;
382                                 OutputWord(recsize);/*Record Length*/
383                                 OutputByte(0x00);/*SEG ID*/
384                                 OutputWord(procedure[k].BeginAdd); /*Offset*/
385                                 for(i=procedure[k].BeginAdd; i<=procedure[k].EndAdd; i++)
386                                         OutputByte(ihxBuff[i]);
387                                 OutputChkSum();
388
389                                 /*Local Symbols*/
390                                 
391                                 recsize=2;
392                                 for(i=0; i<numsym; i++)/*Get the record length*/
393                                         if(symbol[i].Procedure==k)
394                                                 recsize+=((strlen(symbol[i].name)+1)+5);
395
396                                 if(recsize>2) /*If there are any symbols*/
397                                 {
398                                         OutputByte(0x12);       /*REC TYPE*/
399                                         OutputWord(recsize);/*Record Length*/
400                                         OutputByte(0x00);       /*DEF TYPE: Local symbols*/
401                                         for(i=0; i<numsym; i++)
402                                         {
403                                                 if ( (symbol[i].Procedure==k) )
404                                                 {
405                                                         OutputByte(0x00);/*SEG ID*/
406                                                         OutputByte((unsigned char)symbol[i].UsageType);/*SYM INFO*/
407                                                         OutputWord(symbol[i].Address);/*Offset*/
408                                                         OutputByte(0x00);
409                                                         OutputName(symbol[i].name);/*Symbol name*/
410                                                 }
411                                         }
412                                         OutputChkSum();
413                                 }
414
415                                 /*Line Numbers*/
416                                 recsize=2;
417                                 for(i=0; i<numlinenum; i++)/*Get the record length*/
418                                         if(linenum[i].Procedure==k) recsize+=5;
419                                 
420                                 if(recsize>2) /*If there are any line numbers*/
421                                 {
422                                         OutputByte(0x12);       /*REC TYPE*/
423                                         OutputWord(recsize);/*Record Length*/
424                                         OutputByte(0x03);       /*DEF TYPE: Line numbers*/
425                                         for(i=0; i<numlinenum; i++)
426                                         {
427                                                 if ( (linenum[i].Procedure==k) )
428                                                 {
429                                                         OutputByte(0x00);/*SEG ID*/
430                                                         OutputWord(linenum[i].Address);/*Offset*/
431                                                         OutputWord(linenum[i].Number);/*Line Number*/
432                                                 }
433                                         }
434                                         OutputChkSum();
435                                 }
436                         
437                                 /*Scope Definition record: end PROCEDURE block*/
438                                 OutputByte(0x10);/*REC TYPE*/
439                                 OutputWord((strlen(procedure[k].name)+1)+2);/*Record Length*/
440                                 OutputByte(0x05);/*BLK TYP: PROCEDURE end block*/
441                                 OutputName(procedure[k].name);/*Module Name*/
442                                 OutputChkSum();
443                         }
444                 }
445
446                 /*Scope Definition record: end module block*/
447                 OutputByte(0x10);/*REC TYPE*/
448                 OutputWord((strlen(Mname)+1)+2);/*Record Length*/
449                 OutputByte(0x03);/*BLK TYP: module end*/
450                 OutputName(Mname);/*Module Name*/
451                 OutputChkSum();
452         }
453
454         /*Content records for everything that is not in the above procedures*/
455         strcpy(Mname, "OTHER_SDCC_STUF");
456
457         /*Scope Definition record: begin module block*/
458         OutputByte(0x10);/*REC TYPE*/
459         OutputWord((strlen(Mname)+1)+2);/*Record Length*/
460         OutputByte(0x00);/*BLK TYP: module block*/
461         OutputName(Mname);/*Module Name*/
462         OutputChkSum();
463
464         for(j=-1; j<numproc; j++)
465         {
466                 if(numproc)
467                 {
468                         if(j==-1)
469                         {
470                                 i=HexBegin;
471                                 k=procedure[0].BeginAdd;
472                         }
473                         else if(j==(numproc-1))
474                         {
475                                 i=procedure[j].EndAdd+1;
476                                 k=HexSize;
477                         }
478                         else
479                         {
480                                 i=procedure[j].EndAdd+1;
481                                 k=procedure[j+1].BeginAdd;
482                         }
483                 }
484                 else /*What, no procedures??? Ok, here it is the whole hex file*/
485                 {
486                         i=HexBegin;
487                         k=HexSize;
488                 }
489
490                 if(i<k)
491                 {
492                         /*Content Record*/
493                         OutputByte(0x06);/*REC TYPE*/
494                         OutputWord(k-i+4);/*Record Length*/
495                         OutputByte(0x00);/*SEG ID*/
496                         OutputWord(i); /*Offset*/
497                         for(; i<k; i++) OutputByte(ihxBuff[i]);
498                         OutputChkSum();
499                 }
500         }
501         
502         /*Scope Definition record: end module block*/
503         OutputByte(0x10);/*REC TYPE*/
504         OutputWord((strlen(Mname)+1)+2);/*Record Length*/
505         OutputByte(0x03);/*BLK TYP: module end*/
506         OutputName(Mname);/*Module Name*/
507         OutputChkSum();
508
509         /*Module end record*/
510         OutputByte(0x04);/*REC TYPE*/
511         OutputWord((strlen(MHRname)+1)+5);/*Record Length*/
512         OutputName(MHRname);/*Module Name*/
513         OutputWord(0x00);
514         OutputByte(0x0f);/*REG MSK: All the register banks?*/
515         OutputByte(0x00);
516         OutputChkSum();
517
518         fclose(aomf51out);
519 }
520
521 void CollectInfoFromCDB(void)
522 {
523         int i, j, k, CurrentModule;
524         FILE * CDBin;
525         char buff[0x1000];
526         char SourceName[PATH_MAX];
527
528         //"S:{G|F<filename>|L<functionName>}$<name>$<level>$<block>(<type info>),<Address Space>,<on Stack?>,<stack offset>"
529         char Sfmt[]="%[^$] %c %[^$] %c %[^$] %c %s";
530         char c;
531         char scope[0x100];
532         char name[0x100];
533         char level[0x100];
534         char block[0x100];
535         char Bfmt[]="%[^)] %c %c %c %c %d %c %d";
536         char TypeInfo[0x100];
537         char AddressSpace;
538         int OnStack;
539         int StackOffset;
540         int Address, CLine;
541         
542         if(numin==0) return;
543
544         if (dfp != NULL)
545         {
546                 fclose(dfp);
547                 dfp=NULL;
548         }
549
550         /*Build the source filename*/
551         strcpy(SourceName, infn[0].PathName);
552         strcat(SourceName, ".cdb");
553         CDBin=fopen(SourceName, "r");
554         if(CDBin==NULL)
555         {
556                 printf("Couldn't open file '%s'\n", SourceName);
557                 lkexit(1);
558         }
559
560         CurrentModule=0; /*Set the active module as the first one*/
561         while(!feof(CDBin))
562         {
563                 fgets(buff, sizeof(buff)-1, CDBin);
564
565                 if(!feof(CDBin)) switch(buff[0])
566                 {
567                         /*Example: "M:adq"*/
568                         case 'M':
569                                 sscanf(&buff[2], "%s", name);
570                                 for(j=0; j<numin; j++)
571                                         if(EQ(infn[j].ModuleName, name)) break;
572                                 if(j<numin) CurrentModule=j;
573                         break;
574
575                         /* Example:
576                         "S:G$actual$0$0({7}ST__00010000:S),E,0,0"
577                         "S:Lmain$j$1$1({2}SI:S),E,0,0"
578                         "S:G$DS1306_Reset_SPI$0$0({2}DF,SV:S),C,0,0"
579                         "S:G$main$0$0({2}DF,SV:S),C,0,0"
580                         */
581
582                         case 'S':
583                                 sscanf(buff, Sfmt,
584                                         scope, &c,
585                                         name, &c,
586                                         level, &c,
587                                         block);
588                                 
589                                 /*<block>(<type info>),<Address Space>,<on Stack?>,<stack offset>*/
590                                 sscanf(block, Bfmt,
591                                            TypeInfo, &c, &c,
592                                            &AddressSpace, &c,
593                                            &OnStack, &c, 
594                                            &StackOffset);
595
596                                 i=-1; k=-1;
597                                 switch(scope[2])
598                                 {
599                                         case 'G': /*Global symbol*/
600                                         break;
601                                         case 'L': /*Local symbol of a procedure*/
602                                                 for(j=0; j<numproc; j++)
603                                                 {
604                                                         if(EQ(&scope[3], procedure[j].name)) break;
605                                                 }
606                                                 if(j<numproc) k=j; /*Local symbol*/
607                                         break;
608                                         case 'F': /*Local symbol to a module*/
609                                                 for(j=0; j<numin; j++)
610                                                 {
611                                                         if(EQ(&scope[3], infn[j].ModuleName)) break;
612                                                 }
613                                                 if(j<numin) i=j;
614                                         break;
615                                 }
616
617                                 /*This symbol may have been already defined*/
618                                 for(j=0; j<numsym; j++)
619                                 {
620                                         if( EQ(name, symbol[j].name) && 
621                                                 (symbol[j].Procedure==k) &&
622                                                 (symbol[j].Static==i) ) break;
623                                 }
624                                 if(j==numsym) /*New symbol*/
625                                 {
626                                         symbol=realloc(symbol, sizeof(_symbol)*(numsym+1));
627                                         symbol[numsym].FileNameNumber=CurrentModule;
628                                         strcpy(symbol[numsym].name, name);
629                                         symbol[numsym].Procedure=k;
630                                         symbol[numsym].Static=i;
631                                         symbol[numsym].Address=-1;/*Collected later*/
632
633                                         switch(AddressSpace)
634                                         {
635                                                 case 'C': /*Code*/ 
636                                                 case 'D': /*Code/static segment*/ 
637                                                         symbol[numsym].UsageType=0x40;
638                                                 break;
639
640                                                 case 'F': /*External ram*/ 
641                                                 case 'A': /*External stack*/
642                                                         symbol[numsym].UsageType=0x41;
643                                                 break;
644
645                                                 case 'E': /*Internal ram (lower 128) bytes*/ 
646                                                 case 'I': /*SFR space*/ 
647                                                 case 'R': /*Register Space*/ 
648                                                         symbol[numsym].UsageType=0x42;
649                                                 break;
650
651                                                 case 'B': /*Internal stack*/ 
652                                                 case 'G': /*Internal ram*/ 
653                                                         symbol[numsym].UsageType=0x43;
654                                                 break;
655
656                                                 case 'H': /*Bit addressable*/ 
657                                                 case 'J': /*SBIT space*/ 
658                                                         symbol[numsym].UsageType=0x44;
659                                                 break;
660                                                 
661                                                 default:
662                                                         printf("Name: %s, AddressSpace:%c\n", symbol[numsym].name, AddressSpace);
663                                                 break;
664                                         }
665                                         numsym++;
666                                 }
667                         break;
668
669                         /*Examples:
670                         F:G$AsciiToHex$0$0({2}DF,SC:U),C,0,0,0,0,0
671                         F:G$main$0$0({2}DF,SV:S),C,0,0,0,0,0   */
672
673                         case 'F':
674                                 sscanf(buff, "%[^$] %c %[^$]", scope, &c, name);
675                                 /*The same may have been already defined */
676                                 for(j=0; j<numproc; j++)
677                                 {
678                                         if(EQ(name, procedure[j].name)) break;
679                                 }
680                                 if(j==numproc)
681                                 {
682                                         procedure=realloc(procedure, sizeof(_procedure)*(numproc+1));
683                                         strcpy(procedure[numproc].name, name);
684                                         procedure[numproc].FileNameNumber=CurrentModule;
685                                         procedure[numproc].BeginAdd=-1;/*To be collected latter*/
686                                         procedure[numproc].EndAdd=-1;/*To be collected latter*/
687                                         numproc++;
688                                 }
689                                 
690                                 /*This function name is also a global symbol*/
691                                 for(j=0; j<numsym; j++)/*A global symbol may have been already defined*/
692                                 {
693                                         if( EQ(name, symbol[j].name) && (symbol[j].Procedure==-1) ) break;
694                                 }
695                                 if(j==numsym)
696                                 {
697                                         symbol=realloc(symbol, sizeof(_symbol)*(numsym+1));
698                                         symbol[numsym].FileNameNumber=CurrentModule;
699                                         strcpy(symbol[numsym].name, name);
700                                         symbol[numsym].UsageType=0x00;/*A procedure name symbol*/
701                                         symbol[numsym].Procedure=-1; /*Global symbol*/
702                                         symbol[numsym].Address=-1;/*Collected later*/
703                                         numsym++;
704                                 }
705                         break;
706
707                         case 'L':
708                                 switch(buff[2])
709                                 {
710                                         case 'G': /*Example L:G$P0$0$0:80*/
711                                                 sscanf(buff, "%[^$] %c %[^$] %c %[^:] %c %x",
712                                                         scope, &c, name, &c, level, &c, &Address);
713
714                                                 for(j=0; j<numsym; j++)
715                                                 {
716                                                         if(EQ(symbol[j].name, name))
717                                                         {
718                                                                 if( (symbol[j].Address==-1) && (symbol[j].Procedure==-1) )
719                                                                 {
720                                                                         symbol[j].Address=Address;
721                                                                 }
722                                                                 
723                                                                 /*If the symbol is the name of a procedure, the address is also
724                                                                 the begining of such procedure*/
725                                                                 if((symbol[j].UsageType&0x0f)==0x00)
726                                                                 {
727                                                                         for(k=0; k<numproc; k++)
728                                                                         {
729                                                                                 if(EQ(symbol[j].name, procedure[k].name))
730                                                                                 {
731                                                                                         if(procedure[k].BeginAdd==-1)
732                                                                                                 procedure[k].BeginAdd=Address;
733                                                                                         break;
734                                                                                 }
735                                                                         }
736                                                                 }
737                                                                 
738                                                                 break;
739                                                         }
740                                                 }
741                                         break;
742                                         
743                                         case 'F': /*Example L:Fadq$_str_2$0$0:57A*/
744                                                 sscanf(buff, "%[^$] %c %[^$] %c %[^:] %c %x",
745                                                         scope, &c, name, &c, level, &c, &Address);
746                                                 
747                                                 for(j=0; j<numsym; j++)
748                                                 {
749                                                         if(EQ(symbol[j].name, name))
750                                                         {
751                                                                 if( (symbol[j].Address==-1) ) symbol[j].Address=Address;
752                                                                 break;
753                                                         }
754                                                 }
755                                                 
756                                                 /*It could be also a static function*/
757                                                 for(j=0; j<numproc; j++)
758                                                 {
759                                                         if(EQ(procedure[j].name, name))
760                                                         {
761                                                                 if( (procedure[j].BeginAdd==-1) ) procedure[j].BeginAdd=Address;
762                                                                 break;
763                                                         }
764                                                 }
765
766                                         break;
767                                         
768                                         case 'L': /*Example L:Lmain$j$1$1:29*/
769
770                                                 /*
771                                                 L:LDS1306_Write$Value$1$1:34
772                                                 L:LDS1306_Burst_Read$count$1$1:35
773                                                 L:LDS1306_Burst_Read$address$1$1:36
774                                                 L:LDS1306_Burst_Write$count$1$1:37
775                                                 L:LDS1306_Burst_Write$address$1$1:38
776                                                 */
777                                                 sscanf(&buff[3], "%[^$] %c %[^$] %c %[^:] %c %x",
778                                                         scope, &c, name, &c, level, &c, &Address);
779                                                 
780                                                 for(k=0; k<numproc; k++)
781                                                 {
782                                                         if(EQ(procedure[k].name, scope)) break;
783                                                 }
784                                                 
785                                                 if(k<numproc) for(j=0; j<numsym; j++)
786                                                 {
787                                                         if( EQ(symbol[j].name, name) && (symbol[j].Procedure==k) )
788                                                         {
789                                                                 if(symbol[j].Address==-1) symbol[j].Address=Address;
790                                                                 break;
791                                                         }
792                                                 }
793                                         break;
794                                         
795                                         /*Line Numbers*/
796                                         case 'C': /*Example L:C$adq.c$38$1$1:3E*/  /*L:C$hwinit.c$29$1$1:7AD*/
797                                                 sscanf(&buff[4], "%[^.] %[^$] %c %d %[^:] %c %x",
798                                                         name, level, &c, &CLine, level, &c, &Address);
799
800                                                 for(j=0; j<numin; j++)
801                                                         if(EQ(infn[j].ModuleName, name)) break;
802                                                 if(j<numin)
803                                                 {
804                                                         /*Check if this line is already defined*/
805                                                         for(k=0; k<numlinenum; k++)
806                                                         {
807                                                                 if( (linenum[k].Number==CLine) &&
808                                                                         (linenum[k].FileNameNumber==j) )break;
809                                                         }
810                                                         if(k==numlinenum) /*New line number*/
811                                                         {
812                                                                 linenum=realloc(linenum, sizeof(_linenum)*(numlinenum+1));
813                                                                 linenum[numlinenum].Number=CLine;
814                                                                 linenum[numlinenum].FileNameNumber=j;
815                                                                 linenum[numlinenum].Procedure=-1;/*To be asigned later*/
816                                                                 linenum[numlinenum].Address=Address;
817                                                                 numlinenum++;
818                                                         }
819                                                 }
820                                         break;
821                                         
822                                         case 'A': /*Example L:A$adq$424:40*/
823                                                 /*No use for this one*/
824                                         break;
825                                         
826                                         /*The end of a procedure*/
827                                         case 'X': /*Example L:XG$AsciiToHex$0$0:88*/
828                                                 sscanf(&buff[3], "%[^$] %c %[^$] %c %[^:] %c %x",
829                                                         scope, &c, name, &c, level, &c, &Address);
830
831                                                 for(k=0; k<numproc; k++)
832                                                 {
833                                                         if(EQ(procedure[k].name, name))
834                                                         {
835                                                                 if(procedure[k].EndAdd==-1) procedure[k].EndAdd=Address;
836                                                                 break;
837                                                         }
838                                                 }
839                                         break;
840                                 }
841                         break;
842
843                         default:
844                         break;
845                 }
846         }
847
848         /*Make sure each procedure has an end*/
849         for(k=0; k<(numproc-1); k++)
850         {
851                 if (procedure[k].EndAdd==-1) procedure[k].EndAdd=procedure[k+1].BeginAdd-1;
852         }
853         /*Asign each line number to a procedure*/
854         for(j=0; j<numlinenum; j++)
855         {
856                 for(k=0; k<numproc; k++)
857                 {
858                         if ( (linenum[j].Address>=procedure[k].BeginAdd) &&
859                                  (linenum[j].Address<=procedure[k].EndAdd) &&
860                                  (linenum[j].FileNameNumber==procedure[k].FileNameNumber) )
861                         {
862                                 linenum[j].Procedure=k;
863                         }
864                 }
865         }
866
867         fclose(CDBin);
868 }
869
870 int hex2dec (char hex_digit)
871 {
872    int j;
873    j=toupper(hex_digit)-'0';
874    if (j>9) j -= 7;
875    return j;
876 }
877
878 unsigned char GetByte(char * buffer)
879 {
880         return hex2dec(buffer[0])*0x10+hex2dec(buffer[1]);
881 }
882
883 unsigned short GetWord(char * buffer)
884 {
885         return  hex2dec(buffer[0])*0x1000+
886                         hex2dec(buffer[1])*0x100+
887                         hex2dec(buffer[2])*0x10+
888                         hex2dec(buffer[3]);
889 }
890
891 int ReadHexFile(int * Begin)
892 {
893         char buffer[1024];
894         FILE * filein;
895         int j;
896         unsigned char linesize, recordtype, rchksum, value;
897         unsigned short address;
898         int MaxAddress=0;
899         int chksum;
900
901         /*If the hexfile is already open, close it*/
902         if(ofp!=NULL)
903         {
904                 fclose(ofp);
905                 ofp=NULL;
906         }
907         
908         strcpy(ihxFileName, infn[0].PathName);
909         strcat(ihxFileName, ".ihx");
910
911         if ( (filein=fopen(ihxFileName, "r")) == NULL )
912         {
913            printf("Error: Can't open file `%s`.\r\n", ihxFileName);
914            return 0;
915         }
916    
917         ihxBuff=calloc(MEMSIZE, sizeof(unsigned char));
918         if(ihxBuff==NULL)
919         {
920                 printf("Insufficient memory\n");
921                 fclose(filein);
922                 return -1;
923         }
924
925         for(j=0; j<MEMSIZE; j++) ihxBuff[j]=0xff;
926
927     while(1)
928     {
929                 if(fgets(buffer, sizeof(buffer), filein)==NULL)
930                 {
931                         printf("Error reading file '%s'\n", ihxFileName);
932                         break;
933                 }
934         if(buffer[0]==':')
935         {
936                         linesize = GetByte(&buffer[1]);
937                         address = GetWord(&buffer[3]);
938                         recordtype = GetByte(&buffer[7]);
939                         rchksum = GetByte(&buffer[9]+(linesize*2));
940                         chksum=linesize+(address/0x100)+(address%0x100)+recordtype+rchksum;
941
942                         if (recordtype==1) break; /*End of record*/
943
944                         for(j=0; j<linesize; j++)
945                         {
946                                 value=GetByte(&buffer[9]+(j*2));
947                                 chksum+=value;
948                                 ihxBuff[address+j]=value;
949                         }
950                         if(MaxAddress<(address+linesize-1)) MaxAddress=(address+linesize-1);
951                         if(address<*Begin) *Begin=address;
952
953                         if((chksum%0x100)!=0)
954                         {
955                                 printf("ERROR: Bad checksum in file %s\n", ihxFileName);
956                                 fclose(filein);
957                                 return -1;
958                         }
959                 }
960     }
961     fclose(filein);
962         
963     return MaxAddress;
964 }
965
966 void CreateAOMF51(void)
967 {
968         if((dflag) && (!rflag))
969         {
970                 CollectInfoFromCDB();
971                 #ifdef DODUMP
972                 DumpForDebug();
973                 #endif
974                 HexSize=ReadHexFile(&HexBegin)+1;
975                 OutputAOEMF51();
976                 FreeAll();
977         }
978 }