4caa8f58571dae1447b3493c238c6f0fda74e7c6
[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(numproc)
466                 {
467                         if(j==-1)
468                         {
469                                 i=HexBegin;
470                                 k=procedure[0].BeginAdd;
471                         }
472                         else if(j==(numproc-1))
473                         {
474                                 i=procedure[j].EndAdd+1;
475                                 k=HexSize;
476                         }
477                         else
478                         {
479                                 i=procedure[j].EndAdd+1;
480                                 k=procedure[j+1].BeginAdd;
481                         }
482                 }
483                 else /*What, no procedures??? Ok, here it is the whole hex file*/
484                 {
485                         i=HexBegin;
486                         k=HexSize;
487                 }
488
489                 if(i<k)
490                 {
491                         /*Content Record*/
492                         OutputByte(0x06);/*REC TYPE*/
493                         OutputWord(k-i+4);/*Record Length*/
494                         OutputByte(0x00);/*SEG ID*/
495                         OutputWord(i); /*Offset*/
496                         for(; i<k; i++) OutputByte(ihxBuff[i]);
497                         OutputChkSum();
498                 }
499         }
500         
501         /*Scope Definition record: end module block*/
502         OutputByte(0x10);/*REC TYPE*/
503         OutputWord((strlen(Mname)+1)+2);/*Record Length*/
504         OutputByte(0x03);/*BLK TYP: module end*/
505         OutputName(Mname);/*Module Name*/
506         OutputChkSum();
507
508         /*Module end record*/
509         OutputByte(0x04);/*REC TYPE*/
510         OutputWord((strlen(MHRname)+1)+5);/*Record Length*/
511         OutputName(MHRname);/*Module Name*/
512         OutputWord(0x00);
513         OutputByte(0x0f);/*REG MSK: All the register banks?*/
514         OutputByte(0x00);
515         OutputChkSum();
516
517         fclose(aomf51out);
518 }
519
520 void CollectInfoFromCDB(void)
521 {
522         int i, j, k, CurrentModule;
523         FILE * CDBin;
524         char buff[0x1000];
525         char SourceName[PATH_MAX];
526
527         //"S:{G|F<filename>|L<functionName>}$<name>$<level>$<block>(<type info>),<Address Space>,<on Stack?>,<stack offset>"
528         char Sfmt[]="%[^$] %c %[^$] %c %[^$] %c %s";
529         char c;
530         char scope[0x100];
531         char name[0x100];
532         char level[0x100];
533         char block[0x100];
534         char Bfmt[]="%[^)] %c %c %c %c %d %c %d";
535         char TypeInfo[0x100];
536         char AddressSpace;
537         int OnStack;
538         int StackOffset;
539         int Address, CLine;
540         
541         if(numin==0) return;
542
543         if (dfp != NULL)
544         {
545                 fclose(dfp);
546                 dfp=NULL;
547         }
548
549         /*Build the source filename*/
550         strcpy(SourceName, infn[0].PathName);
551         strcat(SourceName, ".cdb");
552         CDBin=fopen(SourceName, "r");
553         if(CDBin==NULL)
554         {
555                 printf("Couldn't open file '%s'\n", SourceName);
556                 lkexit(1);
557         }
558
559         CurrentModule=0; /*Set the active module as the first one*/
560         while(!feof(CDBin))
561         {
562                 fgets(buff, sizeof(buff)-1, CDBin);
563
564                 if(!feof(CDBin)) switch(buff[0])
565                 {
566                         /*Example: "M:adq"*/
567                         case 'M':
568                                 sscanf(&buff[2], "%s", name);
569                                 for(j=0; j<numin; j++)
570                                         if(EQ(infn[j].ModuleName, name)) break;
571                                 if(j<numin) CurrentModule=j;
572                         break;
573
574                         /* Example:
575                         "S:G$actual$0$0({7}ST__00010000:S),E,0,0"
576                         "S:Lmain$j$1$1({2}SI:S),E,0,0"
577                         */
578
579                         case 'S':
580                                 sscanf(buff, Sfmt,
581                                         scope, &c,
582                                         name, &c,
583                                         level, &c,
584                                         block);
585                                 
586                                 /*<block>(<type info>),<Address Space>,<on Stack?>,<stack offset>*/
587                                 sscanf(block, Bfmt,
588                                            TypeInfo, &c, &c,
589                                            &AddressSpace, &c,
590                                            &OnStack, &c, 
591                                            &StackOffset);
592                                 
593                                 i=-1; k=-1;
594                                 switch(scope[2])
595                                 {
596                                         case 'G': /*Global symbol*/
597                                         break;
598                                         case 'L': /*Local symbol of a procedure*/
599                                                 for(j=0; j<numproc; j++)
600                                                 {
601                                                         if(EQ(&scope[3], procedure[j].name)) break;
602                                                 }
603                                                 if(j<numproc) k=j; /*Local symbol*/
604                                         break;
605                                         case 'F': /*Local symbol to a module*/
606                                                 for(j=0; j<numin; j++)
607                                                 {
608                                                         if(EQ(&scope[3], infn[j].ModuleName)) break;
609                                                 }
610                                                 if(j<numin) i=j;
611                                         break;
612                                 }
613
614                                 /*This symbol may have been already defined*/
615                                 for(j=0; j<numsym; j++)
616                                 {
617                                         if( EQ(name, symbol[j].name) && 
618                                                 (symbol[j].Procedure==k) &&
619                                                 (symbol[j].Static==i) ) break;
620                                 }
621                                 if(j==numsym) /*New symbol*/
622                                 {
623                                         symbol=realloc(symbol, sizeof(_symbol)*(numsym+1));
624                                         symbol[numsym].FileNameNumber=CurrentModule;
625                                         strcpy(symbol[numsym].name, name);
626                                         symbol[numsym].Procedure=k;
627                                         symbol[numsym].Static=i;
628                                         symbol[numsym].Address=-1;/*Collected later*/
629
630                                         switch(AddressSpace)
631                                         {
632                                                 case 'C': /*Code*/ 
633                                                 case 'D': /*Code/static segment*/ 
634                                                         symbol[numsym].UsageType=0x40;
635                                                 break;
636
637                                                 case 'F': /*External ram*/ 
638                                                 case 'A': /*External stack*/
639                                                         symbol[numsym].UsageType=0x41;
640                                                 break;
641
642                                                 case 'E': /*Internal ram (lower 128) bytes*/ 
643                                                 case 'I': /*SFR space*/ 
644                                                         symbol[numsym].UsageType=0x42;
645                                                 break;
646
647                                                 case 'B': /*Internal stack*/ 
648                                                 case 'G': /*Internal ram*/ 
649                                                         symbol[numsym].UsageType=0x43;
650                                                 break;
651
652                                                 case 'H': /*Bit addressable*/ 
653                                                 case 'J': /*SBIT space*/ 
654                                                         symbol[numsym].UsageType=0x44;
655                                                 break;
656                                         }
657                                         numsym++;
658                                 }
659                         break;
660
661                         /*Examples:
662                         F:G$AsciiToHex$0$0({2}DF,SC:U),C,0,0,0,0,0
663                         F:G$main$0$0({2}DF,SV:S),C,0,0,0,0,0   */
664
665                         case 'F':
666                                 sscanf(buff, "%[^$] %c %[^$]", scope, &c, name);
667                                 /*The same may have been already defined */
668                                 for(j=0; j<numproc; j++)
669                                 {
670                                         if(EQ(name, procedure[j].name)) break;
671                                 }
672                                 if(j==numproc)
673                                 {
674                                         procedure=realloc(procedure, sizeof(_procedure)*(numproc+1));
675                                         strcpy(procedure[numproc].name, name);
676                                         procedure[numproc].FileNameNumber=CurrentModule;
677                                         procedure[numproc].BeginAdd=-1;/*To be collected latter*/
678                                         procedure[numproc].EndAdd=-1;/*To be collected latter*/
679                                         numproc++;
680                                 }
681
682                                 /*This function name is also a global symbol*/
683                                 for(j=0; j<numsym; j++)/*A global symbol may have been already defined*/
684                                 {
685                                         if( EQ(name, symbol[j].name) && (symbol[j].Procedure==-1) ) break;
686                                 }
687                                 if(j==numsym)
688                                 {
689                                         symbol=realloc(symbol, sizeof(_symbol)*(numsym+1));
690                                         symbol[numsym].FileNameNumber=CurrentModule;
691                                         strcpy(symbol[numsym].name, name);
692                                         symbol[numsym].UsageType=0x00;/*A procedure name symbol*/
693                                         symbol[numsym].Procedure=-1; /*Global symbol*/
694                                         symbol[numsym].Address=-1;/*Collected later*/
695                                         numsym++;
696                                 }
697
698                         break;
699
700                         case 'L':
701                                 switch(buff[2])
702                                 {
703                                         case 'G': /*Example L:G$P0$0$0:80*/
704                                                 sscanf(buff, "%[^$] %c %[^$] %c %[^:] %c %x",
705                                                         scope, &c, name, &c, level, &c, &Address);
706                                                 
707                                                 for(j=0; j<numsym; j++)
708                                                 {
709                                                         if(EQ(symbol[j].name, name))
710                                                         {
711                                                                 if( (symbol[j].Address==-1) && (symbol[j].Procedure==-1) )
712                                                                 {
713                                                                         symbol[j].Address=Address;
714                                                                         /*If the symbol is the name of a procedure, the address is also
715                                                                         the begining of such procedure*/
716                                                                         if(symbol[j].UsageType==0x00)
717                                                                         {
718                                                                                 for(k=0; k<numproc; k++)
719                                                                                 {
720                                                                                         if(EQ(symbol[j].name, procedure[k].name))
721                                                                                         {
722                                                                                                 if(procedure[k].BeginAdd==-1)
723                                                                                                         procedure[k].BeginAdd=Address;
724                                                                                                 break;
725                                                                                         }
726                                                                                 }
727                                                                         }
728                                                                 }
729                                                                 break;
730                                                         }
731                                                 }
732                                         break;
733                                         
734                                         case 'F': /*Example L:Fadq$_str_2$0$0:57A*/
735                                                 sscanf(buff, "%[^$] %c %[^$] %c %[^:] %c %x",
736                                                         scope, &c, name, &c, level, &c, &Address);
737                                                 
738                                                 for(j=0; j<numsym; j++)
739                                                 {
740                                                         if(EQ(symbol[j].name, name))
741                                                         {
742                                                                 if( (symbol[j].Address==-1) ) symbol[j].Address=Address;
743                                                                 break;
744                                                         }
745                                                 }
746                                                 
747                                                 /*It could be also a static function*/
748                                                 for(j=0; j<numproc; j++)
749                                                 {
750                                                         if(EQ(procedure[j].name, name))
751                                                         {
752                                                                 if( (procedure[j].BeginAdd==-1) ) procedure[j].BeginAdd=Address;
753                                                                 break;
754                                                         }
755                                                 }
756
757                                         break;
758                                         
759                                         case 'L': /*Example L:Lmain$j$1$1:29*/
760
761                                                 /*
762                                                 L:LDS1306_Write$Value$1$1:34
763                                                 L:LDS1306_Burst_Read$count$1$1:35
764                                                 L:LDS1306_Burst_Read$address$1$1:36
765                                                 L:LDS1306_Burst_Write$count$1$1:37
766                                                 L:LDS1306_Burst_Write$address$1$1:38
767                                                 */
768                                                 sscanf(&buff[3], "%[^$] %c %[^$] %c %[^:] %c %x",
769                                                         scope, &c, name, &c, level, &c, &Address);
770                                                 
771                                                 for(k=0; k<numproc; k++)
772                                                 {
773                                                         if(EQ(procedure[k].name, scope)) break;
774                                                 }
775                                                 
776                                                 if(k<numproc) for(j=0; j<numsym; j++)
777                                                 {
778                                                         if( EQ(symbol[j].name, name) && (symbol[j].Procedure==k) )
779                                                         {
780                                                                 if(symbol[j].Address==-1) symbol[j].Address=Address;
781                                                                 break;
782                                                         }
783                                                 }
784                                         break;
785                                         
786                                         /*Line Numbers*/
787                                         case 'C': /*Example L:C$adq.c$38$1$1:3E*/  /*L:C$hwinit.c$29$1$1:7AD*/
788                                                 sscanf(&buff[4], "%[^.] %[^$] %c %d %[^:] %c %x",
789                                                         name, level, &c, &CLine, level, &c, &Address);
790
791                                                 for(j=0; j<numin; j++)
792                                                         if(EQ(infn[j].ModuleName, name)) break;
793                                                 if(j<numin)
794                                                 {
795                                                         /*Check if this line is already defined*/
796                                                         for(k=0; k<numlinenum; k++)
797                                                         {
798                                                                 if( (linenum[k].Number==CLine) &&
799                                                                         (linenum[k].FileNameNumber==j) )break;
800                                                         }
801                                                         if(k==numlinenum) /*New line number*/
802                                                         {
803                                                                 linenum=realloc(linenum, sizeof(_linenum)*(numlinenum+1));
804                                                                 linenum[numlinenum].Number=CLine;
805                                                                 linenum[numlinenum].FileNameNumber=j;
806                                                                 linenum[numlinenum].Procedure=-1;/*To be asigned later*/
807                                                                 linenum[numlinenum].Address=Address;
808                                                                 numlinenum++;
809                                                         }
810                                                 }
811                                         break;
812                                         
813                                         case 'A': /*Example L:A$adq$424:40*/
814                                                 /*No use for this one*/
815                                         break;
816                                         
817                                         /*The end of a procedure*/
818                                         case 'X': /*Example L:XG$AsciiToHex$0$0:88*/
819                                                 sscanf(&buff[3], "%[^$] %c %[^$] %c %[^:] %c %x",
820                                                         scope, &c, name, &c, level, &c, &Address);
821
822                                                 for(k=0; k<numproc; k++)
823                                                 {
824                                                         if(EQ(procedure[k].name, name))
825                                                         {
826                                                                 if(procedure[k].EndAdd==-1) procedure[k].EndAdd=Address;
827                                                                 break;
828                                                         }
829                                                 }
830                                         break;
831                                 }
832                         break;
833
834                         default:
835                         break;
836                 }
837         }
838
839         /*Make sure each procedure has an end*/
840         for(k=0; k<(numproc-1); k++)
841         {
842                 if (procedure[k].EndAdd==-1) procedure[k].EndAdd=procedure[k+1].BeginAdd-1;
843         }
844         /*Asign each line number to a procedure*/
845         for(j=0; j<numlinenum; j++)
846         {
847                 for(k=0; k<numproc; k++)
848                 {
849                         if ( (linenum[j].Address>=procedure[k].BeginAdd) &&
850                                  (linenum[j].Address<=procedure[k].EndAdd) &&
851                                  (linenum[j].FileNameNumber==procedure[k].FileNameNumber) )
852                         {
853                                 linenum[j].Procedure=k;
854                         }
855                 }
856         }
857
858         fclose(CDBin);
859 }
860
861 int hex2dec (char hex_digit)
862 {
863    int j;
864    j=toupper(hex_digit)-'0';
865    if (j>9) j -= 7;
866    return j;
867 }
868
869 unsigned char GetByte(char * buffer)
870 {
871         return hex2dec(buffer[0])*0x10+hex2dec(buffer[1]);
872 }
873
874 unsigned short GetWord(char * buffer)
875 {
876         return  hex2dec(buffer[0])*0x1000+
877                         hex2dec(buffer[1])*0x100+
878                         hex2dec(buffer[2])*0x10+
879                         hex2dec(buffer[3]);
880 }
881
882 int ReadHexFile(int * Begin)
883 {
884         char buffer[1024];
885         FILE * filein;
886         int j;
887         unsigned char linesize, recordtype, rchksum, value;
888         unsigned short address;
889         int MaxAddress=0;
890         int chksum;
891
892         /*If the hexfile is already open, close it*/
893         if(ofp!=NULL)
894         {
895                 fclose(ofp);
896                 ofp=NULL;
897         }
898         
899         strcpy(ihxFileName, infn[0].PathName);
900         strcat(ihxFileName, ".ihx");
901
902         if ( (filein=fopen(ihxFileName, "r")) == NULL )
903         {
904            printf("Error: Can't open file `%s`.\r\n", ihxFileName);
905            return 0;
906         }
907    
908         ihxBuff=calloc(MEMSIZE, sizeof(unsigned char));
909         if(ihxBuff==NULL)
910         {
911                 printf("Insufficient memory\n");
912                 fclose(filein);
913                 return -1;
914         }
915
916         for(j=0; j<MEMSIZE; j++) ihxBuff[j]=0xff;
917
918     while(1)
919     {
920                 if(fgets(buffer, sizeof(buffer), filein)==NULL)
921                 {
922                         printf("Error reading file '%s'\n", ihxFileName);
923                         break;
924                 }
925         if(buffer[0]==':')
926         {
927                         linesize = GetByte(&buffer[1]);
928                         address = GetWord(&buffer[3]);
929                         recordtype = GetByte(&buffer[7]);
930                         rchksum = GetByte(&buffer[9]+(linesize*2));
931                         chksum=linesize+(address/0x100)+(address%0x100)+recordtype+rchksum;
932
933                         if (recordtype==1) break; /*End of record*/
934
935                         for(j=0; j<linesize; j++)
936                         {
937                                 value=GetByte(&buffer[9]+(j*2));
938                                 chksum+=value;
939                                 ihxBuff[address+j]=value;
940                         }
941                         if(MaxAddress<(address+linesize-1)) MaxAddress=(address+linesize-1);
942                         if(address<*Begin) *Begin=address;
943
944                         if((chksum%0x100)!=0)
945                         {
946                                 printf("ERROR: Bad checksum in file %s\n", ihxFileName);
947                                 fclose(filein);
948                                 return -1;
949                         }
950                 }
951     }
952     fclose(filein);
953         
954     return MaxAddress;
955 }
956
957 void CreateAOMF51(void)
958 {
959         if((dflag) && (!rflag))
960         {
961                 CollectInfoFromCDB();
962                 #ifdef DODUMP
963                 DumpForDebug();
964                 #endif
965                 HexSize=ReadHexFile(&HexBegin)+1;
966                 OutputAOEMF51();
967                 FreeAll();
968         }
969 }