1 /*-------------------------------------------------------------------------
2 lkaomf51.c - Create an absolute object memory format 51 file
4 Written By - Jesus Calvino-Fraga, jesusc@ieee.org (2002)
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 -------------------------------------------------------------------------*/
26 #define EQ(A,B) !strcmp((A),(B))
27 #define MEMSIZE 0x10000
32 char PathName[PATH_MAX];
33 char ModuleName[PATH_MAX];
39 char ihxFileName[PATH_MAX];
40 char aomf51FileName[PATH_MAX];
46 int Procedure;//If the symbol belongs to a function
47 int Static; //If the symbol is only public on its file
53 _symbol * symbol=NULL;
64 _procedure * procedure=NULL;
75 _linenum * linenum=NULL;
84 _UsageType UsageType[]=
102 {"", 5} /*A typeless number?*/
105 char * UsageTypeName[]={"CODE", "XDATA", "DATA", "IDATA", "BIT", "NUMBER"};
107 unsigned char * ihxBuff=NULL;
110 int HexSize, HexBegin=0x10000;
113 void GetName(char * filepath, char * name)
116 for(j=strlen(filepath); j>0; j--)
117 if( (filepath[j-1]=='/')||(filepath[j-1]=='\\') ) break;
118 for(k=0; (filepath[j]!=0)&&(filepath[j]!='.'); j++, k++)
123 void SaveLinkedFilePath(char * filepath)
127 if((dflag) && (!rflag))
129 infn=realloc(infn, sizeof(_infn)*(numin+1));
131 strcpy(infn[numin].PathName, filepath);
132 j=strlen(infn[numin].PathName);
134 /*If there is an extension remove it*/
137 if(EQ(&infn[numin].PathName[j-4], ".rel"))
139 infn[numin].PathName[j-4]=0;
143 /*Get the module name=filename, no drive, no dir, no ext*/
144 GetName(infn[numin].PathName, infn[numin].ModuleName);
145 //printf("%s, %s\n", infn[numin].PathName, infn[numin].ModuleName);
147 /*Check if this filename is already in*/
148 for(j=0; j<numin; j++)
150 if(EQ(infn[numin].PathName, infn[j].PathName)) break;
152 if(j==numin) numin++;
193 void OutputByte(unsigned char value)
196 fwrite( &value, 1, 1, aomf51out );
199 void OutputWord(int value)
201 OutputByte((unsigned char)(value%0x100));
202 OutputByte((unsigned char)(value/0x100));
205 void OutputName(char * name)
208 OutputByte((unsigned char)strlen(name));
209 for(k=0; name[k]!=0; k++)
210 OutputByte((unsigned char)toupper(name[k]));
213 void OutputChkSum(void)
215 OutputByte((unsigned char)(0x100-(GlobalChkSum%0x100)));
220 void DumpForDebug (void)
222 char DumpFileName[PATH_MAX];
226 strcpy(DumpFileName, infn[0].PathName);
227 strcat(DumpFileName, ".d51");
229 DumpFile=fopen(DumpFileName, "wb");
232 printf("Couldn't create file %s\n", DumpFileName);
236 fprintf(DumpFile,"SYMBOLS:\n");
238 for(j=0; j<numsym; j++)
240 fprintf(DumpFile, "%s, %s, %s, 0x%04x, %s\n",
242 infn[symbol[j].FileNameNumber].PathName,
243 (symbol[j].Procedure>=0)?procedure[symbol[j].Procedure].name:"GLOBAL",
245 UsageTypeName[symbol[j].UsageType&0xf]);
248 fprintf(DumpFile,"\nPROCEDURES:\n");
249 for(j=0; j<numproc; j++)
251 fprintf(DumpFile, "%s, %s, 0x%04x-0x%04x\n",
253 infn[procedure[j].FileNameNumber].PathName,
254 procedure[j].BeginAdd,
255 procedure[j].EndAdd);
258 fprintf(DumpFile,"\nLINE NUMBERS:\n");
259 for(j=0; j<numlinenum; j++)
261 fprintf(DumpFile, "%d:0x%04x, %s -> %s\n",
264 infn[linenum[j].FileNameNumber].PathName,
265 (linenum[j].Procedure>=0)?procedure[linenum[j].Procedure].name:"I don't know");
272 void OutputAOEMF51(void)
274 int i, j, k, recsize;
275 char MHRname[0x100], Mname[0x100];
277 strcpy(aomf51FileName, infn[0].PathName);
279 aomf51out=fopen(aomf51FileName, "wb");
282 printf("Couldn't create file %s\n", aomf51FileName);
286 GetName(infn[0].PathName, MHRname);
289 /*Module header record*/
290 OutputByte(0x02);/*REC TYPE*/
291 OutputWord((strlen(MHRname)+1)+3);/*Record Length*/
292 OutputName(MHRname);/*Module Name*/
293 OutputByte(0xff);/*TRN ID: RL51?*/
297 for(j=0; j<numin; j++)
299 GetName(infn[j].PathName, Mname);
301 /*Scope Definition record: begin module block*/
302 OutputByte(0x10);/*REC TYPE*/
303 OutputWord((strlen(Mname)+1)+2);/*Record Length*/
304 OutputByte(0x00);/*BLK TYP: module block*/
305 OutputName(Mname);/*Module Name*/
308 /*Public symbols defined in this module*/
310 for(k=0; k<numsym; k++)/*Compute the record length*/
311 if ( (symbol[k].FileNameNumber==j) && (symbol[k].Address!=-1) &&
312 (symbol[k].Procedure==-1) &&
313 (symbol[k].Static==-1) ) recsize+=((strlen(symbol[k].name)+1)+5);
315 if(recsize>2) /*If there are any symbols*/
317 OutputByte(0x12); /*REC TYPE*/
318 OutputWord(recsize);/*Record Length*/
319 OutputByte(0x01); /*DEF TYPE: Public symbols*/
320 for(k=0; k<numsym; k++)
322 if ( (symbol[k].FileNameNumber==j) && (symbol[k].Address!=-1) &&
323 (symbol[k].Procedure==-1) &&
324 (symbol[k].Static==-1) )
326 OutputByte(0x00);/*SEG ID*/
327 OutputByte((unsigned char)symbol[k].UsageType);/*SYM INFO*/
328 OutputWord(symbol[k].Address);/*Offset*/
330 OutputName(symbol[k].name);/*Symbol name*/
336 /*Local symbols defined in this module*/
338 for(k=0; k<numsym; k++)/*Compute the record length*/
339 if ( (symbol[k].FileNameNumber==j) && (symbol[k].Address!=-1) &&
340 (symbol[k].Procedure==-1) &&
341 (symbol[k].Static==j) ) recsize+=((strlen(symbol[k].name)+1)+5);
343 if(recsize>2) /*If there are any symbols*/
345 OutputByte(0x12); /*REC TYPE*/
346 OutputWord(recsize);/*Record Length*/
347 OutputByte(0x00); /*DEF TYPE: Local symbols*/
348 for(k=0; k<numsym; k++)
350 if ( (symbol[k].FileNameNumber==j) && (symbol[k].Address!=-1) &&
351 (symbol[k].Procedure==-1) &&
352 (symbol[k].Static==j) )
354 OutputByte(0x00);/*SEG ID*/
355 OutputByte((unsigned char)symbol[k].UsageType);/*SYM INFO*/
356 OutputWord(symbol[k].Address);/*Offset*/
358 OutputName(symbol[k].name);/*Symbol name*/
364 /*Output the procedures of this module*/
366 for(k=0; k<numproc; k++)
368 if(procedure[k].FileNameNumber==j)
370 /*Scope Definition record: begin PROCEDURE block*/
371 OutputByte(0x10);/*REC TYPE*/
372 OutputWord((strlen(procedure[k].name)+1)+2);/*Record Length*/
373 OutputByte(0x02);/*BLK TYP: PROCEDURE block*/
374 OutputName(procedure[k].name);/*Module Name*/
378 OutputByte(0x06);/*REC TYPE*/
379 recsize=procedure[k].EndAdd-procedure[k].BeginAdd+1+4;
380 OutputWord(recsize);/*Record Length*/
381 OutputByte(0x00);/*SEG ID*/
382 OutputWord(procedure[k].BeginAdd); /*Offset*/
383 for(i=procedure[k].BeginAdd; i<=procedure[k].EndAdd; i++)
384 OutputByte(ihxBuff[i]);
390 for(i=0; i<numsym; i++)/*Get the record length*/
391 if(symbol[i].Procedure==k)
392 recsize+=((strlen(symbol[i].name)+1)+5);
394 if(recsize>2) /*If there are any symbols*/
396 OutputByte(0x12); /*REC TYPE*/
397 OutputWord(recsize);/*Record Length*/
398 OutputByte(0x00); /*DEF TYPE: Local symbols*/
399 for(i=0; i<numsym; i++)
401 if ( (symbol[i].Procedure==k) )
403 OutputByte(0x00);/*SEG ID*/
404 OutputByte((unsigned char)symbol[i].UsageType);/*SYM INFO*/
405 OutputWord(symbol[i].Address);/*Offset*/
407 OutputName(symbol[i].name);/*Symbol name*/
415 for(i=0; i<numlinenum; i++)/*Get the record length*/
416 if(linenum[i].Procedure==k) recsize+=5;
418 if(recsize>2) /*If there are any line numbers*/
420 OutputByte(0x12); /*REC TYPE*/
421 OutputWord(recsize);/*Record Length*/
422 OutputByte(0x03); /*DEF TYPE: Line numbers*/
423 for(i=0; i<numlinenum; i++)
425 if ( (linenum[i].Procedure==k) )
427 OutputByte(0x00);/*SEG ID*/
428 OutputWord(linenum[i].Address);/*Offset*/
429 OutputWord(linenum[i].Number);/*Line Number*/
435 /*Scope Definition record: end PROCEDURE block*/
436 OutputByte(0x10);/*REC TYPE*/
437 OutputWord((strlen(procedure[k].name)+1)+2);/*Record Length*/
438 OutputByte(0x05);/*BLK TYP: PROCEDURE end block*/
439 OutputName(procedure[k].name);/*Module Name*/
444 /*Scope Definition record: end module block*/
445 OutputByte(0x10);/*REC TYPE*/
446 OutputWord((strlen(Mname)+1)+2);/*Record Length*/
447 OutputByte(0x03);/*BLK TYP: module end*/
448 OutputName(Mname);/*Module Name*/
452 /*Content records for everything that is not in the above procedures*/
453 strcpy(Mname, "OTHER_SDCC_STUF");
455 /*Scope Definition record: begin module block*/
456 OutputByte(0x10);/*REC TYPE*/
457 OutputWord((strlen(Mname)+1)+2);/*Record Length*/
458 OutputByte(0x00);/*BLK TYP: module block*/
459 OutputName(Mname);/*Module Name*/
462 for(j=-1; j<numproc; j++)
467 k=procedure[0].BeginAdd;
469 else if(j==(numproc-1))
471 i=procedure[j].EndAdd+1;
476 i=procedure[j].EndAdd+1;
477 k=procedure[j+1].BeginAdd;
483 OutputByte(0x06);/*REC TYPE*/
484 OutputWord(k-i+4);/*Record Length*/
485 OutputByte(0x00);/*SEG ID*/
486 OutputWord(i); /*Offset*/
487 for(; i<k; i++) OutputByte(ihxBuff[i]);
492 /*Scope Definition record: end module block*/
493 OutputByte(0x10);/*REC TYPE*/
494 OutputWord((strlen(Mname)+1)+2);/*Record Length*/
495 OutputByte(0x03);/*BLK TYP: module end*/
496 OutputName(Mname);/*Module Name*/
499 /*Module end record*/
500 OutputByte(0x04);/*REC TYPE*/
501 OutputWord((strlen(MHRname)+1)+5);/*Record Length*/
502 OutputName(MHRname);/*Module Name*/
504 OutputByte(0x0f);/*REG MSK: All the register banks?*/
511 void CollectInfoFromCDB(void)
513 int i, j, k, CurrentModule;
516 char SourceName[PATH_MAX];
518 //"S:{G|F<filename>|L<functionName>}$<name>$<level>$<block>(<type info>),<Address Space>,<on Stack?>,<stack offset>"
519 char Sfmt[]="%[^$] %c %[^$] %c %[^$] %c %s";
525 char Bfmt[]="%[^)] %c %c %c %c %d %c %d";
526 char TypeInfo[0x100];
540 /*Build the source filename*/
541 strcpy(SourceName, infn[0].PathName);
542 strcat(SourceName, ".cdb");
543 CDBin=fopen(SourceName, "r");
546 printf("Couldn't open file '%s'\n", SourceName);
550 CurrentModule=0; /*Set the active module as the first one*/
553 fgets(buff, sizeof(buff)-1, CDBin);
555 if(!feof(CDBin)) switch(buff[0])
559 sscanf(&buff[2], "%s", name);
560 for(j=0; j<numin; j++)
561 if(EQ(infn[j].ModuleName, name)) break;
562 if(j<numin) CurrentModule=j;
566 "S:G$actual$0$0({7}ST__00010000:S),E,0,0"
567 "S:Lmain$j$1$1({2}SI:S),E,0,0"
577 /*<block>(<type info>),<Address Space>,<on Stack?>,<stack offset>*/
587 case 'G': /*Global symbol*/
589 case 'L': /*Local symbol of a procedure*/
590 for(j=0; j<numproc; j++)
592 if(EQ(&scope[3], procedure[j].name)) break;
594 if(j<numproc) k=j; /*Local symbol*/
596 case 'F': /*Local symbol to a module*/
597 for(j=0; j<numin; j++)
599 if(EQ(&scope[3], infn[j].ModuleName)) break;
605 /*This symbol may have been already defined*/
606 for(j=0; j<numsym; j++)
608 if( EQ(name, symbol[j].name) &&
609 (symbol[j].Procedure==k) &&
610 (symbol[j].Static==i) ) break;
612 if(j==numsym) /*New symbol*/
614 symbol=realloc(symbol, sizeof(_symbol)*(numsym+1));
615 symbol[numsym].FileNameNumber=CurrentModule;
616 strcpy(symbol[numsym].name, name);
617 symbol[numsym].Procedure=k;
618 symbol[numsym].Static=i;
619 symbol[numsym].Address=-1;/*Collected later*/
624 case 'D': /*Code/static segment*/
625 symbol[numsym].UsageType=0x40;
628 case 'F': /*External ram*/
629 case 'A': /*External stack*/
630 symbol[numsym].UsageType=0x41;
633 case 'E': /*Internal ram (lower 128) bytes*/
634 case 'I': /*SFR space*/
635 symbol[numsym].UsageType=0x42;
638 case 'B': /*Internal stack*/
639 case 'G': /*Internal ram*/
640 symbol[numsym].UsageType=0x43;
643 case 'H': /*Bit addressable*/
644 case 'J': /*SBIT space*/
645 symbol[numsym].UsageType=0x44;
653 F:G$AsciiToHex$0$0({2}DF,SC:U),C,0,0,0,0,0
654 F:G$main$0$0({2}DF,SV:S),C,0,0,0,0,0 */
657 sscanf(buff, "%[^$] %c %[^$]", scope, &c, name);
658 /*The same may have been already defined */
659 for(j=0; j<numproc; j++)
661 if(EQ(name, procedure[j].name)) break;
665 procedure=realloc(procedure, sizeof(_procedure)*(numproc+1));
666 strcpy(procedure[numproc].name, name);
667 procedure[numproc].FileNameNumber=CurrentModule;
668 procedure[numproc].BeginAdd=-1;/*To be collected latter*/
669 procedure[numproc].EndAdd=-1;/*To be collected latter*/
673 /*This function name is also a global symbol*/
674 for(j=0; j<numsym; j++)/*A global symbol may have been already defined*/
676 if( EQ(name, symbol[j].name) && (symbol[j].Procedure==-1) ) break;
680 symbol=realloc(symbol, sizeof(_symbol)*(numsym+1));
681 symbol[numsym].FileNameNumber=CurrentModule;
682 strcpy(symbol[numsym].name, name);
683 symbol[numsym].UsageType=0x00;/*A procedure name symbol*/
684 symbol[numsym].Procedure=-1; /*Global symbol*/
685 symbol[numsym].Address=-1;/*Collected later*/
694 case 'G': /*Example L:G$P0$0$0:80*/
695 sscanf(buff, "%[^$] %c %[^$] %c %[^:] %c %x",
696 scope, &c, name, &c, level, &c, &Address);
698 for(j=0; j<numsym; j++)
700 if(EQ(symbol[j].name, name))
702 if( (symbol[j].Address==-1) && (symbol[j].Procedure==-1) )
704 symbol[j].Address=Address;
705 /*If the symbol is the name of a procedure, the address is also
706 the begining of such procedure*/
707 if(symbol[j].UsageType==0x00)
709 for(k=0; k<numproc; k++)
711 if(EQ(symbol[j].name, procedure[k].name))
713 if(procedure[k].BeginAdd==-1)
714 procedure[k].BeginAdd=Address;
725 case 'F': /*Example L:Fadq$_str_2$0$0:57A*/
726 sscanf(buff, "%[^$] %c %[^$] %c %[^:] %c %x",
727 scope, &c, name, &c, level, &c, &Address);
729 for(j=0; j<numsym; j++)
731 if(EQ(symbol[j].name, name))
733 if( (symbol[j].Address==-1) ) symbol[j].Address=Address;
738 /*It could be also a static function*/
739 for(j=0; j<numproc; j++)
741 if(EQ(procedure[j].name, name))
743 if( (procedure[j].BeginAdd==-1) ) procedure[j].BeginAdd=Address;
750 case 'L': /*Example L:Lmain$j$1$1:29*/
753 L:LDS1306_Write$Value$1$1:34
754 L:LDS1306_Burst_Read$count$1$1:35
755 L:LDS1306_Burst_Read$address$1$1:36
756 L:LDS1306_Burst_Write$count$1$1:37
757 L:LDS1306_Burst_Write$address$1$1:38
759 sscanf(&buff[3], "%[^$] %c %[^$] %c %[^:] %c %x",
760 scope, &c, name, &c, level, &c, &Address);
762 for(k=0; k<numproc; k++)
764 if(EQ(procedure[k].name, scope)) break;
767 if(k<numproc) for(j=0; j<numsym; j++)
769 if( EQ(symbol[j].name, name) && (symbol[j].Procedure==k) )
771 if(symbol[j].Address==-1) symbol[j].Address=Address;
778 case 'C': /*Example L:C$adq.c$38$1$1:3E*/ /*L:C$hwinit.c$29$1$1:7AD*/
779 sscanf(&buff[4], "%[^.] %[^$] %c %d %[^:] %c %x",
780 name, level, &c, &CLine, level, &c, &Address);
782 for(j=0; j<numin; j++)
783 if(EQ(infn[j].ModuleName, name)) break;
786 /*Check if this line is already defined*/
787 for(k=0; k<numlinenum; k++)
789 if( (linenum[k].Number==CLine) &&
790 (linenum[k].FileNameNumber==j) )break;
792 if(k==numlinenum) /*New line number*/
794 linenum=realloc(linenum, sizeof(_linenum)*(numlinenum+1));
795 linenum[numlinenum].Number=CLine;
796 linenum[numlinenum].FileNameNumber=j;
797 linenum[numlinenum].Procedure=-1;/*To be asigned later*/
798 linenum[numlinenum].Address=Address;
804 case 'A': /*Example L:A$adq$424:40*/
805 /*No use for this one*/
808 /*The end of a procedure*/
809 case 'X': /*Example L:XG$AsciiToHex$0$0:88*/
810 sscanf(&buff[3], "%[^$] %c %[^$] %c %[^:] %c %x",
811 scope, &c, name, &c, level, &c, &Address);
813 for(k=0; k<numproc; k++)
815 if(EQ(procedure[k].name, name))
817 if(procedure[k].EndAdd==-1) procedure[k].EndAdd=Address;
830 /*Make sure each procedure has an end*/
831 for(k=0; k<(numproc-1); k++)
833 if (procedure[k].EndAdd==-1) procedure[k].EndAdd=procedure[k+1].BeginAdd-1;
835 /*Asign each line number to a procedure*/
836 for(j=0; j<numlinenum; j++)
838 for(k=0; k<numproc; k++)
840 if ( (linenum[j].Address>=procedure[k].BeginAdd) &&
841 (linenum[j].Address<=procedure[k].EndAdd) &&
842 (linenum[j].FileNameNumber==procedure[k].FileNameNumber) )
844 linenum[j].Procedure=k;
852 int hex2dec (char hex_digit)
855 j=toupper(hex_digit)-'0';
860 unsigned char GetByte(char * buffer)
862 return hex2dec(buffer[0])*0x10+hex2dec(buffer[1]);
865 unsigned short GetWord(char * buffer)
867 return hex2dec(buffer[0])*0x1000+
868 hex2dec(buffer[1])*0x100+
869 hex2dec(buffer[2])*0x10+
873 int ReadHexFile(int * Begin)
878 unsigned char linesize, recordtype, rchksum, value;
879 unsigned short address;
883 /*If the hexfile is already open, close it*/
890 strcpy(ihxFileName, infn[0].PathName);
891 strcat(ihxFileName, ".ihx");
893 if ( (filein=fopen(ihxFileName, "r")) == NULL )
895 printf("Error: Can't open file `%s`.\r\n", ihxFileName);
899 ihxBuff=calloc(MEMSIZE, sizeof(unsigned char));
902 printf("Insufficient memory\n");
907 for(j=0; j<MEMSIZE; j++) ihxBuff[j]=0xff;
911 if(fgets(buffer, sizeof(buffer), filein)==NULL)
913 printf("Error reading file '%s'\n", ihxFileName);
918 linesize = GetByte(&buffer[1]);
919 address = GetWord(&buffer[3]);
920 recordtype = GetByte(&buffer[7]);
921 rchksum = GetByte(&buffer[9]+(linesize*2));
922 chksum=linesize+(address/0x100)+(address%0x100)+recordtype+rchksum;
924 if (recordtype==1) break; /*End of record*/
926 for(j=0; j<linesize; j++)
928 value=GetByte(&buffer[9]+(j*2));
930 ihxBuff[address+j]=value;
932 if(MaxAddress<(address+linesize-1)) MaxAddress=(address+linesize-1);
933 if(address<*Begin) *Begin=address;
935 if((chksum%0x100)!=0)
937 printf("ERROR: Bad checksum in file %s\n", ihxFileName);
948 void CreateAOMF51(void)
950 if((dflag) && (!rflag))
952 CollectInfoFromCDB();
956 HexSize=ReadHexFile(&HexBegin)+1;