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 -------------------------------------------------------------------------*/
27 #define EQ(A,B) !strcmp((A),(B))
28 #define MEMSIZE 0x10000
33 char PathName[PATH_MAX];
34 char ModuleName[PATH_MAX];
40 char ihxFileName[PATH_MAX];
41 char aomf51FileName[PATH_MAX];
47 int Procedure;//If the symbol belongs to a function
48 int Static; //If the symbol is only public on its file
54 _symbol * symbol=NULL;
65 _procedure * procedure=NULL;
76 _linenum * linenum=NULL;
85 _UsageType UsageType[]=
103 {"", 5} /*A typeless number?*/
106 char * UsageTypeName[]={"CODE", "XDATA", "DATA", "IDATA", "BIT", "NUMBER"};
108 unsigned char * ihxBuff=NULL;
111 int HexSize, HexBegin=0x10000;
114 void GetName(char * filepath, char * name)
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++)
124 void SaveLinkedFilePath(char * filepath)
128 if((dflag) && (!rflag))
130 infn=realloc(infn, sizeof(_infn)*(numin+1));
132 strcpy(infn[numin].PathName, filepath);
133 j=strlen(infn[numin].PathName);
135 /*If there is an extension remove it*/
138 if(EQ(&infn[numin].PathName[j-4], ".rel"))
140 infn[numin].PathName[j-4]=0;
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);
148 /*Check if this filename is already in*/
149 for(j=0; j<numin; j++)
151 if(EQ(infn[numin].PathName, infn[j].PathName)) break;
153 if(j==numin) numin++;
194 void OutputByte(unsigned char value)
197 fwrite( &value, 1, 1, aomf51out );
200 void OutputWord(int value)
202 OutputByte((unsigned char)(value%0x100));
203 OutputByte((unsigned char)(value/0x100));
206 void OutputName(char * name)
209 OutputByte((unsigned char)strlen(name));
210 for(k=0; name[k]!=0; k++)
211 OutputByte((unsigned char)toupper(name[k]));
214 void OutputChkSum(void)
216 OutputByte((unsigned char)(0x100-(GlobalChkSum%0x100)));
221 void DumpForDebug (void)
223 char DumpFileName[PATH_MAX];
227 strcpy(DumpFileName, infn[0].PathName);
228 strcat(DumpFileName, ".d51");
230 DumpFile=fopen(DumpFileName, "wb");
233 printf("Couldn't create file %s\n", DumpFileName);
237 fprintf(DumpFile,"SYMBOLS:\n");
239 for(j=0; j<numsym; j++)
241 fprintf(DumpFile, "%s, %s, %s, 0x%04x, %s\n",
243 infn[symbol[j].FileNameNumber].PathName,
244 (symbol[j].Procedure>=0)?procedure[symbol[j].Procedure].name:"GLOBAL",
246 UsageTypeName[symbol[j].UsageType&0xf]);
249 fprintf(DumpFile,"\nPROCEDURES:\n");
250 for(j=0; j<numproc; j++)
252 fprintf(DumpFile, "%s, %s, 0x%04x-0x%04x\n",
254 infn[procedure[j].FileNameNumber].PathName,
255 procedure[j].BeginAdd,
256 procedure[j].EndAdd);
259 fprintf(DumpFile,"\nLINE NUMBERS:\n");
260 for(j=0; j<numlinenum; j++)
262 fprintf(DumpFile, "%d:0x%04x, %s -> %s\n",
265 infn[linenum[j].FileNameNumber].PathName,
266 (linenum[j].Procedure>=0)?procedure[linenum[j].Procedure].name:"I don't know");
273 void OutputAOEMF51(void)
275 int i, j, k, recsize;
276 char MHRname[0x100], Mname[0x100];
278 strcpy(aomf51FileName, infn[0].PathName);
280 aomf51out=fopen(aomf51FileName, "wb");
283 printf("Couldn't create file %s\n", aomf51FileName);
287 GetName(infn[0].PathName, MHRname);
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?*/
298 for(j=0; j<numin; j++)
300 GetName(infn[j].PathName, Mname);
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*/
309 /*Public symbols defined in this module*/
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);
316 if(recsize>2) /*If there are any symbols*/
318 OutputByte(0x12); /*REC TYPE*/
319 OutputWord(recsize);/*Record Length*/
320 OutputByte(0x01); /*DEF TYPE: Public symbols*/
321 for(k=0; k<numsym; k++)
323 if ( (symbol[k].FileNameNumber==j) && (symbol[k].Address!=-1) &&
324 (symbol[k].Procedure==-1) &&
325 (symbol[k].Static==-1) )
327 OutputByte(0x00);/*SEG ID*/
328 OutputByte((unsigned char)symbol[k].UsageType);/*SYM INFO*/
329 OutputWord(symbol[k].Address);/*Offset*/
331 OutputName(symbol[k].name);/*Symbol name*/
337 /*Local symbols defined in this module*/
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);
344 if(recsize>2) /*If there are any symbols*/
346 OutputByte(0x12); /*REC TYPE*/
347 OutputWord(recsize);/*Record Length*/
348 OutputByte(0x00); /*DEF TYPE: Local symbols*/
349 for(k=0; k<numsym; k++)
351 if ( (symbol[k].FileNameNumber==j) && (symbol[k].Address!=-1) &&
352 (symbol[k].Procedure==-1) &&
353 (symbol[k].Static==j) )
355 OutputByte(0x00);/*SEG ID*/
356 OutputByte((unsigned char)symbol[k].UsageType);/*SYM INFO*/
357 OutputWord(symbol[k].Address);/*Offset*/
359 OutputName(symbol[k].name);/*Symbol name*/
365 /*Output the procedures of this module*/
367 for(k=0; k<numproc; k++)
369 if(procedure[k].FileNameNumber==j)
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*/
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]);
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);
395 if(recsize>2) /*If there are any symbols*/
397 OutputByte(0x12); /*REC TYPE*/
398 OutputWord(recsize);/*Record Length*/
399 OutputByte(0x00); /*DEF TYPE: Local symbols*/
400 for(i=0; i<numsym; i++)
402 if ( (symbol[i].Procedure==k) )
404 OutputByte(0x00);/*SEG ID*/
405 OutputByte((unsigned char)symbol[i].UsageType);/*SYM INFO*/
406 OutputWord(symbol[i].Address);/*Offset*/
408 OutputName(symbol[i].name);/*Symbol name*/
416 for(i=0; i<numlinenum; i++)/*Get the record length*/
417 if(linenum[i].Procedure==k) recsize+=5;
419 if(recsize>2) /*If there are any line numbers*/
421 OutputByte(0x12); /*REC TYPE*/
422 OutputWord(recsize);/*Record Length*/
423 OutputByte(0x03); /*DEF TYPE: Line numbers*/
424 for(i=0; i<numlinenum; i++)
426 if ( (linenum[i].Procedure==k) )
428 OutputByte(0x00);/*SEG ID*/
429 OutputWord(linenum[i].Address);/*Offset*/
430 OutputWord(linenum[i].Number);/*Line Number*/
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*/
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*/
453 /*Content records for everything that is not in the above procedures*/
454 strcpy(Mname, "OTHER_SDCC_STUF");
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*/
463 for(j=-1; j<numproc; j++)
470 k=procedure[0].BeginAdd;
472 else if(j==(numproc-1))
474 i=procedure[j].EndAdd+1;
479 i=procedure[j].EndAdd+1;
480 k=procedure[j+1].BeginAdd;
483 else /*What, no procedures??? Ok, here it is the whole hex file*/
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]);
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*/
508 /*Module end record*/
509 OutputByte(0x04);/*REC TYPE*/
510 OutputWord((strlen(MHRname)+1)+5);/*Record Length*/
511 OutputName(MHRname);/*Module Name*/
513 OutputByte(0x0f);/*REG MSK: All the register banks?*/
520 void CollectInfoFromCDB(void)
522 int i, j, k, CurrentModule;
525 char SourceName[PATH_MAX];
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";
534 char Bfmt[]="%[^)] %c %c %c %c %d %c %d";
535 char TypeInfo[0x100];
549 /*Build the source filename*/
550 strcpy(SourceName, infn[0].PathName);
551 strcat(SourceName, ".cdb");
552 CDBin=fopen(SourceName, "r");
555 printf("Couldn't open file '%s'\n", SourceName);
559 CurrentModule=0; /*Set the active module as the first one*/
562 fgets(buff, sizeof(buff)-1, CDBin);
564 if(!feof(CDBin)) switch(buff[0])
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;
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"
586 /*<block>(<type info>),<Address Space>,<on Stack?>,<stack offset>*/
596 case 'G': /*Global symbol*/
598 case 'L': /*Local symbol of a procedure*/
599 for(j=0; j<numproc; j++)
601 if(EQ(&scope[3], procedure[j].name)) break;
603 if(j<numproc) k=j; /*Local symbol*/
605 case 'F': /*Local symbol to a module*/
606 for(j=0; j<numin; j++)
608 if(EQ(&scope[3], infn[j].ModuleName)) break;
614 /*This symbol may have been already defined*/
615 for(j=0; j<numsym; j++)
617 if( EQ(name, symbol[j].name) &&
618 (symbol[j].Procedure==k) &&
619 (symbol[j].Static==i) ) break;
621 if(j==numsym) /*New symbol*/
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*/
633 case 'D': /*Code/static segment*/
634 symbol[numsym].UsageType=0x40;
637 case 'F': /*External ram*/
638 case 'A': /*External stack*/
639 symbol[numsym].UsageType=0x41;
642 case 'E': /*Internal ram (lower 128) bytes*/
643 case 'I': /*SFR space*/
644 symbol[numsym].UsageType=0x42;
647 case 'B': /*Internal stack*/
648 case 'G': /*Internal ram*/
649 symbol[numsym].UsageType=0x43;
652 case 'H': /*Bit addressable*/
653 case 'J': /*SBIT space*/
654 symbol[numsym].UsageType=0x44;
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 */
666 sscanf(buff, "%[^$] %c %[^$]", scope, &c, name);
667 /*The same may have been already defined */
668 for(j=0; j<numproc; j++)
670 if(EQ(name, procedure[j].name)) break;
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*/
682 /*This function name is also a global symbol*/
683 for(j=0; j<numsym; j++)/*A global symbol may have been already defined*/
685 if( EQ(name, symbol[j].name) && (symbol[j].Procedure==-1) ) break;
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*/
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);
707 for(j=0; j<numsym; j++)
709 if(EQ(symbol[j].name, name))
711 if( (symbol[j].Address==-1) && (symbol[j].Procedure==-1) )
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)
718 for(k=0; k<numproc; k++)
720 if(EQ(symbol[j].name, procedure[k].name))
722 if(procedure[k].BeginAdd==-1)
723 procedure[k].BeginAdd=Address;
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);
738 for(j=0; j<numsym; j++)
740 if(EQ(symbol[j].name, name))
742 if( (symbol[j].Address==-1) ) symbol[j].Address=Address;
747 /*It could be also a static function*/
748 for(j=0; j<numproc; j++)
750 if(EQ(procedure[j].name, name))
752 if( (procedure[j].BeginAdd==-1) ) procedure[j].BeginAdd=Address;
759 case 'L': /*Example L:Lmain$j$1$1:29*/
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
768 sscanf(&buff[3], "%[^$] %c %[^$] %c %[^:] %c %x",
769 scope, &c, name, &c, level, &c, &Address);
771 for(k=0; k<numproc; k++)
773 if(EQ(procedure[k].name, scope)) break;
776 if(k<numproc) for(j=0; j<numsym; j++)
778 if( EQ(symbol[j].name, name) && (symbol[j].Procedure==k) )
780 if(symbol[j].Address==-1) symbol[j].Address=Address;
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);
791 for(j=0; j<numin; j++)
792 if(EQ(infn[j].ModuleName, name)) break;
795 /*Check if this line is already defined*/
796 for(k=0; k<numlinenum; k++)
798 if( (linenum[k].Number==CLine) &&
799 (linenum[k].FileNameNumber==j) )break;
801 if(k==numlinenum) /*New line number*/
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;
813 case 'A': /*Example L:A$adq$424:40*/
814 /*No use for this one*/
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);
822 for(k=0; k<numproc; k++)
824 if(EQ(procedure[k].name, name))
826 if(procedure[k].EndAdd==-1) procedure[k].EndAdd=Address;
839 /*Make sure each procedure has an end*/
840 for(k=0; k<(numproc-1); k++)
842 if (procedure[k].EndAdd==-1) procedure[k].EndAdd=procedure[k+1].BeginAdd-1;
844 /*Asign each line number to a procedure*/
845 for(j=0; j<numlinenum; j++)
847 for(k=0; k<numproc; k++)
849 if ( (linenum[j].Address>=procedure[k].BeginAdd) &&
850 (linenum[j].Address<=procedure[k].EndAdd) &&
851 (linenum[j].FileNameNumber==procedure[k].FileNameNumber) )
853 linenum[j].Procedure=k;
861 int hex2dec (char hex_digit)
864 j=toupper(hex_digit)-'0';
869 unsigned char GetByte(char * buffer)
871 return hex2dec(buffer[0])*0x10+hex2dec(buffer[1]);
874 unsigned short GetWord(char * buffer)
876 return hex2dec(buffer[0])*0x1000+
877 hex2dec(buffer[1])*0x100+
878 hex2dec(buffer[2])*0x10+
882 int ReadHexFile(int * Begin)
887 unsigned char linesize, recordtype, rchksum, value;
888 unsigned short address;
892 /*If the hexfile is already open, close it*/
899 strcpy(ihxFileName, infn[0].PathName);
900 strcat(ihxFileName, ".ihx");
902 if ( (filein=fopen(ihxFileName, "r")) == NULL )
904 printf("Error: Can't open file `%s`.\r\n", ihxFileName);
908 ihxBuff=calloc(MEMSIZE, sizeof(unsigned char));
911 printf("Insufficient memory\n");
916 for(j=0; j<MEMSIZE; j++) ihxBuff[j]=0xff;
920 if(fgets(buffer, sizeof(buffer), filein)==NULL)
922 printf("Error reading file '%s'\n", ihxFileName);
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;
933 if (recordtype==1) break; /*End of record*/
935 for(j=0; j<linesize; j++)
937 value=GetByte(&buffer[9]+(j*2));
939 ihxBuff[address+j]=value;
941 if(MaxAddress<(address+linesize-1)) MaxAddress=(address+linesize-1);
942 if(address<*Begin) *Begin=address;
944 if((chksum%0x100)!=0)
946 printf("ERROR: Bad checksum in file %s\n", ihxFileName);
957 void CreateAOMF51(void)
959 if((dflag) && (!rflag))
961 CollectInfoFromCDB();
965 HexSize=ReadHexFile(&HexBegin)+1;