a module can start with a digit
[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;
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                 fprintf(DumpFile, "%s, %s, %s, 0x%04x, %s\n",
242                         symbol[j].name,
243                         infn[symbol[j].FileNameNumber].PathName,
244                         (symbol[j].Procedure>=0)?procedure[symbol[j].Procedure].name:"GLOBAL",
245                         symbol[j].Address,
246                         UsageTypeName[symbol[j].UsageType&0xf]);
247         }
248
249         fprintf(DumpFile,"\nPROCEDURES:\n");
250         for(j=0; j<numproc; j++)
251         {
252                 fprintf(DumpFile, "%s, %s, 0x%04x-0x%04x\n",
253                         procedure[j].name,
254                         infn[procedure[j].FileNameNumber].PathName,
255                         procedure[j].BeginAdd,
256                         procedure[j].EndAdd);
257         }
258
259         fprintf(DumpFile,"\nLINE NUMBERS:\n");
260         for(j=0; j<numlinenum; j++)
261         {
262                 fprintf(DumpFile, "%d:0x%04x, %s -> %s\n",
263                         linenum[j].Number,
264                         linenum[j].Address,
265                         infn[linenum[j].FileNameNumber].PathName,
266                         (linenum[j].Procedure>=0)?procedure[linenum[j].Procedure].name:"I don't know");
267         }
268
269         fclose(DumpFile);
270 }
271 #endif
272
273 void OutputAOEMF51(void)
274 {
275         int i, j, k, recsize;
276         char MHRname[0x100], Mname[0x100];
277
278         strcpy(aomf51FileName, infn[0].PathName);
279
280         aomf51out=fopen(aomf51FileName, "wb");
281         if(aomf51out==NULL)
282         {
283                 printf("Couldn't create file %s\n", aomf51FileName);
284                 return;
285         }
286
287         GetName(infn[0].PathName, MHRname);
288         GlobalChkSum=0;
289         
290         /*Module header record*/
291         OutputByte(0x02);/*REC TYPE*/
292         OutputWord((strlen(MHRname)+1)+3);/*Record Length*/
293         OutputName(MHRname);/*Module Name*/
294         OutputByte(0xff);/*TRN ID: RL51?*/
295         OutputByte(0x00);
296         OutputChkSum();
297
298         for(j=0; j<numin; j++)
299         {
300                 GetName(infn[j].PathName, Mname);
301
302                 /*Scope Definition record: begin module block*/
303                 OutputByte(0x10);/*REC TYPE*/
304                 OutputWord((strlen(Mname)+1)+2);/*Record Length*/
305                 OutputByte(0x00);/*BLK TYP: module block*/
306                 OutputName(Mname);/*Module Name*/
307                 OutputChkSum();
308
309                 /*Public symbols defined in this module*/
310                 recsize=2;
311                 for(k=0; k<numsym; k++)/*Compute the record length*/
312                         if ( (symbol[k].FileNameNumber==j) && (symbol[k].Address!=-1) &&
313                                  (symbol[k].Procedure==-1) &&
314                                  (symbol[k].Static==-1) ) recsize+=((strlen(symbol[k].name)+1)+5);
315
316                 if(recsize>2) /*If there are any symbols*/
317                 {
318                         OutputByte(0x12);       /*REC TYPE*/
319                         OutputWord(recsize);/*Record Length*/
320                         OutputByte(0x01);       /*DEF TYPE: Public symbols*/
321                         for(k=0; k<numsym; k++)
322                         {
323                                 if ( (symbol[k].FileNameNumber==j) && (symbol[k].Address!=-1) &&
324                                          (symbol[k].Procedure==-1) &&
325                                          (symbol[k].Static==-1) )
326                                 {
327                                         OutputByte(0x00);/*SEG ID*/
328                                         OutputByte((unsigned char)symbol[k].UsageType);/*SYM INFO*/
329                                         OutputWord(symbol[k].Address);/*Offset*/
330                                         OutputByte(0x00);
331                                         OutputName(symbol[k].name);/*Symbol name*/
332                                 }
333                         }
334                         OutputChkSum();
335                 }
336
337                 /*Local symbols defined in this module*/
338                 recsize=2;
339                 for(k=0; k<numsym; k++)/*Compute the record length*/
340                         if ( (symbol[k].FileNameNumber==j) && (symbol[k].Address!=-1) &&
341                                  (symbol[k].Procedure==-1) &&
342                                  (symbol[k].Static==j) ) recsize+=((strlen(symbol[k].name)+1)+5);
343
344                 if(recsize>2) /*If there are any symbols*/
345                 {
346                         OutputByte(0x12);       /*REC TYPE*/
347                         OutputWord(recsize);/*Record Length*/
348                         OutputByte(0x00);       /*DEF TYPE: Local symbols*/
349                         for(k=0; k<numsym; k++)
350                         {
351                                 if ( (symbol[k].FileNameNumber==j) && (symbol[k].Address!=-1) &&
352                                          (symbol[k].Procedure==-1) &&
353                                          (symbol[k].Static==j) )
354                                 {
355                                         OutputByte(0x00);/*SEG ID*/
356                                         OutputByte((unsigned char)symbol[k].UsageType);/*SYM INFO*/
357                                         OutputWord(symbol[k].Address);/*Offset*/
358                                         OutputByte(0x00);
359                                         OutputName(symbol[k].name);/*Symbol name*/
360                                 }
361                         }
362                         OutputChkSum();
363                 }
364
365                 /*Output the procedures of this module*/
366
367                 for(k=0; k<numproc; k++)
368                 {
369                         if(procedure[k].FileNameNumber==j)
370                         {
371                                 /*Scope Definition record: begin PROCEDURE block*/
372                                 OutputByte(0x10);/*REC TYPE*/
373                                 OutputWord((strlen(procedure[k].name)+1)+2);/*Record Length*/
374                                 OutputByte(0x02);/*BLK TYP: PROCEDURE block*/
375                                 OutputName(procedure[k].name);/*Module Name*/
376                                 OutputChkSum();
377
378                                 /*Content Record*/
379                                 OutputByte(0x06);/*REC TYPE*/
380                                 recsize=procedure[k].EndAdd-procedure[k].BeginAdd+1+4;
381                                 OutputWord(recsize);/*Record Length*/
382                                 OutputByte(0x00);/*SEG ID*/
383                                 OutputWord(procedure[k].BeginAdd); /*Offset*/
384                                 for(i=procedure[k].BeginAdd; i<=procedure[k].EndAdd; i++)
385                                         OutputByte(ihxBuff[i]);
386                                 OutputChkSum();
387
388                                 /*Local Symbols*/
389                                 
390                                 recsize=2;
391                                 for(i=0; i<numsym; i++)/*Get the record length*/
392                                         if(symbol[i].Procedure==k)
393                                                 recsize+=((strlen(symbol[i].name)+1)+5);
394
395                                 if(recsize>2) /*If there are any symbols*/
396                                 {
397                                         OutputByte(0x12);       /*REC TYPE*/
398                                         OutputWord(recsize);/*Record Length*/
399                                         OutputByte(0x00);       /*DEF TYPE: Local symbols*/
400                                         for(i=0; i<numsym; i++)
401                                         {
402                                                 if ( (symbol[i].Procedure==k) )
403                                                 {
404                                                         OutputByte(0x00);/*SEG ID*/
405                                                         OutputByte((unsigned char)symbol[i].UsageType);/*SYM INFO*/
406                                                         OutputWord(symbol[i].Address);/*Offset*/
407                                                         OutputByte(0x00);
408                                                         OutputName(symbol[i].name);/*Symbol name*/
409                                                 }
410                                         }
411                                         OutputChkSum();
412                                 }
413
414                                 /*Line Numbers*/
415                                 recsize=2;
416                                 for(i=0; i<numlinenum; i++)/*Get the record length*/
417                                         if(linenum[i].Procedure==k) recsize+=5;
418                                 
419                                 if(recsize>2) /*If there are any line numbers*/
420                                 {
421                                         OutputByte(0x12);       /*REC TYPE*/
422                                         OutputWord(recsize);/*Record Length*/
423                                         OutputByte(0x03);       /*DEF TYPE: Line numbers*/
424                                         for(i=0; i<numlinenum; i++)
425                                         {
426                                                 if ( (linenum[i].Procedure==k) )
427                                                 {
428                                                         OutputByte(0x00);/*SEG ID*/
429                                                         OutputWord(linenum[i].Address);/*Offset*/
430                                                         OutputWord(linenum[i].Number);/*Line Number*/
431                                                 }
432                                         }
433                                         OutputChkSum();
434                                 }
435                         
436                                 /*Scope Definition record: end PROCEDURE block*/
437                                 OutputByte(0x10);/*REC TYPE*/
438                                 OutputWord((strlen(procedure[k].name)+1)+2);/*Record Length*/
439                                 OutputByte(0x05);/*BLK TYP: PROCEDURE end block*/
440                                 OutputName(procedure[k].name);/*Module Name*/
441                                 OutputChkSum();
442                         }
443                 }
444
445                 /*Scope Definition record: end module block*/
446                 OutputByte(0x10);/*REC TYPE*/
447                 OutputWord((strlen(Mname)+1)+2);/*Record Length*/
448                 OutputByte(0x03);/*BLK TYP: module end*/
449                 OutputName(Mname);/*Module Name*/
450                 OutputChkSum();
451         }
452
453         /*Content records for everything that is not in the above procedures*/
454         strcpy(Mname, "OTHER_SDCC_STUF");
455
456         /*Scope Definition record: begin module block*/
457         OutputByte(0x10);/*REC TYPE*/
458         OutputWord((strlen(Mname)+1)+2);/*Record Length*/
459         OutputByte(0x00);/*BLK TYP: module block*/
460         OutputName(Mname);/*Module Name*/
461         OutputChkSum();
462
463         for(j=-1; j<numproc; j++)
464         {
465                 if(j==-1)
466                 {
467                         i=HexBegin;
468                         k=procedure[0].BeginAdd;
469                 }
470                 else if(j==(numproc-1))
471                 {
472                         i=procedure[j].EndAdd+1;
473                         k=HexSize;
474                 }
475                 else
476                 {
477                         i=procedure[j].EndAdd+1;
478                         k=procedure[j+1].BeginAdd;
479                 }
480
481                 if(i<k)
482                 {
483                         /*Content Record*/
484                         OutputByte(0x06);/*REC TYPE*/
485                         OutputWord(k-i+4);/*Record Length*/
486                         OutputByte(0x00);/*SEG ID*/
487                         OutputWord(i); /*Offset*/
488                         for(; i<k; i++) OutputByte(ihxBuff[i]);
489                         OutputChkSum();
490                 }
491         }
492         
493         /*Scope Definition record: end module block*/
494         OutputByte(0x10);/*REC TYPE*/
495         OutputWord((strlen(Mname)+1)+2);/*Record Length*/
496         OutputByte(0x03);/*BLK TYP: module end*/
497         OutputName(Mname);/*Module Name*/
498         OutputChkSum();
499
500         /*Module end record*/
501         OutputByte(0x04);/*REC TYPE*/
502         OutputWord((strlen(MHRname)+1)+5);/*Record Length*/
503         OutputName(MHRname);/*Module Name*/
504         OutputWord(0x00);
505         OutputByte(0x0f);/*REG MSK: All the register banks?*/
506         OutputByte(0x00);
507         OutputChkSum();
508
509         fclose(aomf51out);
510 }
511
512 void CollectInfoFromCDB(void)
513 {
514         int i, j, k, CurrentModule;
515         FILE * CDBin;
516         char buff[0x1000];
517         char SourceName[PATH_MAX];
518
519         //"S:{G|F<filename>|L<functionName>}$<name>$<level>$<block>(<type info>),<Address Space>,<on Stack?>,<stack offset>"
520         char Sfmt[]="%[^$] %c %[^$] %c %[^$] %c %s";
521         char c;
522         char scope[0x100];
523         char name[0x100];
524         char level[0x100];
525         char block[0x100];
526         char Bfmt[]="%[^)] %c %c %c %c %d %c %d";
527         char TypeInfo[0x100];
528         char AddressSpace;
529         int OnStack;
530         int StackOffset;
531         int Address, CLine;
532         
533         if(numin==0) return;
534
535         if (dfp != NULL)
536         {
537                 fclose(dfp);
538                 dfp=NULL;
539         }
540
541         /*Build the source filename*/
542         strcpy(SourceName, infn[0].PathName);
543         strcat(SourceName, ".cdb");
544         CDBin=fopen(SourceName, "r");
545         if(CDBin==NULL)
546         {
547                 printf("Couldn't open file '%s'\n", SourceName);
548                 lkexit(1);
549         }
550
551         CurrentModule=0; /*Set the active module as the first one*/
552         while(!feof(CDBin))
553         {
554                 fgets(buff, sizeof(buff)-1, CDBin);
555
556                 if(!feof(CDBin)) switch(buff[0])
557                 {
558                         /*Example: "M:adq"*/
559                         case 'M':
560                                 sscanf(&buff[2], "%s", name);
561                                 for(j=0; j<numin; j++)
562                                         if(EQ(infn[j].ModuleName, name)) break;
563                                 if(j<numin) CurrentModule=j;
564                         break;
565
566                         /* Example:
567                         "S:G$actual$0$0({7}ST__00010000:S),E,0,0"
568                         "S:Lmain$j$1$1({2}SI:S),E,0,0"
569                         */
570
571                         case 'S':
572                                 sscanf(buff, Sfmt,
573                                         scope, &c,
574                                         name, &c,
575                                         level, &c,
576                                         block);
577                                 
578                                 /*<block>(<type info>),<Address Space>,<on Stack?>,<stack offset>*/
579                                 sscanf(block, Bfmt,
580                                            TypeInfo, &c, &c,
581                                            &AddressSpace, &c,
582                                            &OnStack, &c, 
583                                            &StackOffset);
584                                 
585                                 i=-1; k=-1;
586                                 switch(scope[2])
587                                 {
588                                         case 'G': /*Global symbol*/
589                                         break;
590                                         case 'L': /*Local symbol of a procedure*/
591                                                 for(j=0; j<numproc; j++)
592                                                 {
593                                                         if(EQ(&scope[3], procedure[j].name)) break;
594                                                 }
595                                                 if(j<numproc) k=j; /*Local symbol*/
596                                         break;
597                                         case 'F': /*Local symbol to a module*/
598                                                 for(j=0; j<numin; j++)
599                                                 {
600                                                         if(EQ(&scope[3], infn[j].ModuleName)) break;
601                                                 }
602                                                 if(j<numin) i=j;
603                                         break;
604                                 }
605
606                                 /*This symbol may have been already defined*/
607                                 for(j=0; j<numsym; j++)
608                                 {
609                                         if( EQ(name, symbol[j].name) && 
610                                                 (symbol[j].Procedure==k) &&
611                                                 (symbol[j].Static==i) ) break;
612                                 }
613                                 if(j==numsym) /*New symbol*/
614                                 {
615                                         symbol=realloc(symbol, sizeof(_symbol)*(numsym+1));
616                                         symbol[numsym].FileNameNumber=CurrentModule;
617                                         strcpy(symbol[numsym].name, name);
618                                         symbol[numsym].Procedure=k;
619                                         symbol[numsym].Static=i;
620                                         symbol[numsym].Address=-1;/*Collected later*/
621
622                                         switch(AddressSpace)
623                                         {
624                                                 case 'C': /*Code*/ 
625                                                 case 'D': /*Code/static segment*/ 
626                                                         symbol[numsym].UsageType=0x40;
627                                                 break;
628
629                                                 case 'F': /*External ram*/ 
630                                                 case 'A': /*External stack*/
631                                                         symbol[numsym].UsageType=0x41;
632                                                 break;
633
634                                                 case 'E': /*Internal ram (lower 128) bytes*/ 
635                                                 case 'I': /*SFR space*/ 
636                                                         symbol[numsym].UsageType=0x42;
637                                                 break;
638
639                                                 case 'B': /*Internal stack*/ 
640                                                 case 'G': /*Internal ram*/ 
641                                                         symbol[numsym].UsageType=0x43;
642                                                 break;
643
644                                                 case 'H': /*Bit addressable*/ 
645                                                 case 'J': /*SBIT space*/ 
646                                                         symbol[numsym].UsageType=0x44;
647                                                 break;
648                                         }
649                                         numsym++;
650                                 }
651                         break;
652
653                         /*Examples:
654                         F:G$AsciiToHex$0$0({2}DF,SC:U),C,0,0,0,0,0
655                         F:G$main$0$0({2}DF,SV:S),C,0,0,0,0,0   */
656
657                         case 'F':
658                                 sscanf(buff, "%[^$] %c %[^$]", scope, &c, name);
659                                 /*The same may have been already defined */
660                                 for(j=0; j<numproc; j++)
661                                 {
662                                         if(EQ(name, procedure[j].name)) break;
663                                 }
664                                 if(j==numproc)
665                                 {
666                                         procedure=realloc(procedure, sizeof(_procedure)*(numproc+1));
667                                         strcpy(procedure[numproc].name, name);
668                                         procedure[numproc].FileNameNumber=CurrentModule;
669                                         procedure[numproc].BeginAdd=-1;/*To be collected latter*/
670                                         procedure[numproc].EndAdd=-1;/*To be collected latter*/
671                                         numproc++;
672                                 }
673
674                                 /*This function name is also a global symbol*/
675                                 for(j=0; j<numsym; j++)/*A global symbol may have been already defined*/
676                                 {
677                                         if( EQ(name, symbol[j].name) && (symbol[j].Procedure==-1) ) break;
678                                 }
679                                 if(j==numsym)
680                                 {
681                                         symbol=realloc(symbol, sizeof(_symbol)*(numsym+1));
682                                         symbol[numsym].FileNameNumber=CurrentModule;
683                                         strcpy(symbol[numsym].name, name);
684                                         symbol[numsym].UsageType=0x00;/*A procedure name symbol*/
685                                         symbol[numsym].Procedure=-1; /*Global symbol*/
686                                         symbol[numsym].Address=-1;/*Collected later*/
687                                         numsym++;
688                                 }
689
690                         break;
691
692                         case 'L':
693                                 switch(buff[2])
694                                 {
695                                         case 'G': /*Example L:G$P0$0$0:80*/
696                                                 sscanf(buff, "%[^$] %c %[^$] %c %[^:] %c %x",
697                                                         scope, &c, name, &c, level, &c, &Address);
698                                                 
699                                                 for(j=0; j<numsym; j++)
700                                                 {
701                                                         if(EQ(symbol[j].name, name))
702                                                         {
703                                                                 if( (symbol[j].Address==-1) && (symbol[j].Procedure==-1) )
704                                                                 {
705                                                                         symbol[j].Address=Address;
706                                                                         /*If the symbol is the name of a procedure, the address is also
707                                                                         the begining of such procedure*/
708                                                                         if(symbol[j].UsageType==0x00)
709                                                                         {
710                                                                                 for(k=0; k<numproc; k++)
711                                                                                 {
712                                                                                         if(EQ(symbol[j].name, procedure[k].name))
713                                                                                         {
714                                                                                                 if(procedure[k].BeginAdd==-1)
715                                                                                                         procedure[k].BeginAdd=Address;
716                                                                                                 break;
717                                                                                         }
718                                                                                 }
719                                                                         }
720                                                                 }
721                                                                 break;
722                                                         }
723                                                 }
724                                         break;
725                                         
726                                         case 'F': /*Example L:Fadq$_str_2$0$0:57A*/
727                                                 sscanf(buff, "%[^$] %c %[^$] %c %[^:] %c %x",
728                                                         scope, &c, name, &c, level, &c, &Address);
729                                                 
730                                                 for(j=0; j<numsym; j++)
731                                                 {
732                                                         if(EQ(symbol[j].name, name))
733                                                         {
734                                                                 if( (symbol[j].Address==-1) ) symbol[j].Address=Address;
735                                                                 break;
736                                                         }
737                                                 }
738                                                 
739                                                 /*It could be also a static function*/
740                                                 for(j=0; j<numproc; j++)
741                                                 {
742                                                         if(EQ(procedure[j].name, name))
743                                                         {
744                                                                 if( (procedure[j].BeginAdd==-1) ) procedure[j].BeginAdd=Address;
745                                                                 break;
746                                                         }
747                                                 }
748
749                                         break;
750                                         
751                                         case 'L': /*Example L:Lmain$j$1$1:29*/
752
753                                                 /*
754                                                 L:LDS1306_Write$Value$1$1:34
755                                                 L:LDS1306_Burst_Read$count$1$1:35
756                                                 L:LDS1306_Burst_Read$address$1$1:36
757                                                 L:LDS1306_Burst_Write$count$1$1:37
758                                                 L:LDS1306_Burst_Write$address$1$1:38
759                                                 */
760                                                 sscanf(&buff[3], "%[^$] %c %[^$] %c %[^:] %c %x",
761                                                         scope, &c, name, &c, level, &c, &Address);
762                                                 
763                                                 for(k=0; k<numproc; k++)
764                                                 {
765                                                         if(EQ(procedure[k].name, scope)) break;
766                                                 }
767                                                 
768                                                 if(k<numproc) for(j=0; j<numsym; j++)
769                                                 {
770                                                         if( EQ(symbol[j].name, name) && (symbol[j].Procedure==k) )
771                                                         {
772                                                                 if(symbol[j].Address==-1) symbol[j].Address=Address;
773                                                                 break;
774                                                         }
775                                                 }
776                                         break;
777                                         
778                                         /*Line Numbers*/
779                                         case 'C': /*Example L:C$adq.c$38$1$1:3E*/  /*L:C$hwinit.c$29$1$1:7AD*/
780                                                 sscanf(&buff[4], "%[^.] %[^$] %c %d %[^:] %c %x",
781                                                         name, level, &c, &CLine, level, &c, &Address);
782
783                                                 for(j=0; j<numin; j++)
784                                                         if(EQ(infn[j].ModuleName, name)) break;
785                                                 if(j<numin)
786                                                 {
787                                                         /*Check if this line is already defined*/
788                                                         for(k=0; k<numlinenum; k++)
789                                                         {
790                                                                 if( (linenum[k].Number==CLine) &&
791                                                                         (linenum[k].FileNameNumber==j) )break;
792                                                         }
793                                                         if(k==numlinenum) /*New line number*/
794                                                         {
795                                                                 linenum=realloc(linenum, sizeof(_linenum)*(numlinenum+1));
796                                                                 linenum[numlinenum].Number=CLine;
797                                                                 linenum[numlinenum].FileNameNumber=j;
798                                                                 linenum[numlinenum].Procedure=-1;/*To be asigned later*/
799                                                                 linenum[numlinenum].Address=Address;
800                                                                 numlinenum++;
801                                                         }
802                                                 }
803                                         break;
804                                         
805                                         case 'A': /*Example L:A$adq$424:40*/
806                                                 /*No use for this one*/
807                                         break;
808                                         
809                                         /*The end of a procedure*/
810                                         case 'X': /*Example L:XG$AsciiToHex$0$0:88*/
811                                                 sscanf(&buff[3], "%[^$] %c %[^$] %c %[^:] %c %x",
812                                                         scope, &c, name, &c, level, &c, &Address);
813
814                                                 for(k=0; k<numproc; k++)
815                                                 {
816                                                         if(EQ(procedure[k].name, name))
817                                                         {
818                                                                 if(procedure[k].EndAdd==-1) procedure[k].EndAdd=Address;
819                                                                 break;
820                                                         }
821                                                 }
822                                         break;
823                                 }
824                         break;
825
826                         default:
827                         break;
828                 }
829         }
830
831         /*Make sure each procedure has an end*/
832         for(k=0; k<(numproc-1); k++)
833         {
834                 if (procedure[k].EndAdd==-1) procedure[k].EndAdd=procedure[k+1].BeginAdd-1;
835         }
836         /*Asign each line number to a procedure*/
837         for(j=0; j<numlinenum; j++)
838         {
839                 for(k=0; k<numproc; k++)
840                 {
841                         if ( (linenum[j].Address>=procedure[k].BeginAdd) &&
842                                  (linenum[j].Address<=procedure[k].EndAdd) &&
843                                  (linenum[j].FileNameNumber==procedure[k].FileNameNumber) )
844                         {
845                                 linenum[j].Procedure=k;
846                         }
847                 }
848         }
849
850         fclose(CDBin);
851 }
852
853 int hex2dec (char hex_digit)
854 {
855    int j;
856    j=toupper(hex_digit)-'0';
857    if (j>9) j -= 7;
858    return j;
859 }
860
861 unsigned char GetByte(char * buffer)
862 {
863         return hex2dec(buffer[0])*0x10+hex2dec(buffer[1]);
864 }
865
866 unsigned short GetWord(char * buffer)
867 {
868         return  hex2dec(buffer[0])*0x1000+
869                         hex2dec(buffer[1])*0x100+
870                         hex2dec(buffer[2])*0x10+
871                         hex2dec(buffer[3]);
872 }
873
874 int ReadHexFile(int * Begin)
875 {
876         char buffer[1024];
877         FILE * filein;
878         int j;
879         unsigned char linesize, recordtype, rchksum, value;
880         unsigned short address;
881         int MaxAddress=0;
882         int chksum;
883
884         /*If the hexfile is already open, close it*/
885         if(ofp!=NULL)
886         {
887                 fclose(ofp);
888                 ofp=NULL;
889         }
890         
891         strcpy(ihxFileName, infn[0].PathName);
892         strcat(ihxFileName, ".ihx");
893
894         if ( (filein=fopen(ihxFileName, "r")) == NULL )
895         {
896            printf("Error: Can't open file `%s`.\r\n", ihxFileName);
897            return 0;
898         }
899    
900         ihxBuff=calloc(MEMSIZE, sizeof(unsigned char));
901         if(ihxBuff==NULL)
902         {
903                 printf("Insufficient memory\n");
904                 fclose(filein);
905                 return -1;
906         }
907
908         for(j=0; j<MEMSIZE; j++) ihxBuff[j]=0xff;
909
910     while(1)
911     {
912                 if(fgets(buffer, sizeof(buffer), filein)==NULL)
913                 {
914                         printf("Error reading file '%s'\n", ihxFileName);
915                         break;
916                 }
917         if(buffer[0]==':')
918         {
919                         linesize = GetByte(&buffer[1]);
920                         address = GetWord(&buffer[3]);
921                         recordtype = GetByte(&buffer[7]);
922                         rchksum = GetByte(&buffer[9]+(linesize*2));
923                         chksum=linesize+(address/0x100)+(address%0x100)+recordtype+rchksum;
924
925                         if (recordtype==1) break; /*End of record*/
926
927                         for(j=0; j<linesize; j++)
928                         {
929                                 value=GetByte(&buffer[9]+(j*2));
930                                 chksum+=value;
931                                 ihxBuff[address+j]=value;
932                         }
933                         if(MaxAddress<(address+linesize-1)) MaxAddress=(address+linesize-1);
934                         if(address<*Begin) *Begin=address;
935
936                         if((chksum%0x100)!=0)
937                         {
938                                 printf("ERROR: Bad checksum in file %s\n", ihxFileName);
939                                 fclose(filein);
940                                 return -1;
941                         }
942                 }
943     }
944     fclose(filein);
945         
946     return MaxAddress;
947 }
948
949 void CreateAOMF51(void)
950 {
951         if((dflag) && (!rflag))
952         {
953                 CollectInfoFromCDB();
954                 #ifdef DODUMP
955                 DumpForDebug();
956                 #endif
957                 HexSize=ReadHexFile(&HexBegin)+1;
958                 OutputAOEMF51();
959                 FreeAll();
960         }
961 }