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++)
468 k=procedure[0].BeginAdd;
470 else if(j==(numproc-1))
472 i=procedure[j].EndAdd+1;
477 i=procedure[j].EndAdd+1;
478 k=procedure[j+1].BeginAdd;
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]);
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*/
500 /*Module end record*/
501 OutputByte(0x04);/*REC TYPE*/
502 OutputWord((strlen(MHRname)+1)+5);/*Record Length*/
503 OutputName(MHRname);/*Module Name*/
505 OutputByte(0x0f);/*REG MSK: All the register banks?*/
512 void CollectInfoFromCDB(void)
514 int i, j, k, CurrentModule;
517 char SourceName[PATH_MAX];
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";
526 char Bfmt[]="%[^)] %c %c %c %c %d %c %d";
527 char TypeInfo[0x100];
541 /*Build the source filename*/
542 strcpy(SourceName, infn[0].PathName);
543 strcat(SourceName, ".cdb");
544 CDBin=fopen(SourceName, "r");
547 printf("Couldn't open file '%s'\n", SourceName);
551 CurrentModule=0; /*Set the active module as the first one*/
554 fgets(buff, sizeof(buff)-1, CDBin);
556 if(!feof(CDBin)) switch(buff[0])
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;
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"
578 /*<block>(<type info>),<Address Space>,<on Stack?>,<stack offset>*/
588 case 'G': /*Global symbol*/
590 case 'L': /*Local symbol of a procedure*/
591 for(j=0; j<numproc; j++)
593 if(EQ(&scope[3], procedure[j].name)) break;
595 if(j<numproc) k=j; /*Local symbol*/
597 case 'F': /*Local symbol to a module*/
598 for(j=0; j<numin; j++)
600 if(EQ(&scope[3], infn[j].ModuleName)) break;
606 /*This symbol may have been already defined*/
607 for(j=0; j<numsym; j++)
609 if( EQ(name, symbol[j].name) &&
610 (symbol[j].Procedure==k) &&
611 (symbol[j].Static==i) ) break;
613 if(j==numsym) /*New symbol*/
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*/
625 case 'D': /*Code/static segment*/
626 symbol[numsym].UsageType=0x40;
629 case 'F': /*External ram*/
630 case 'A': /*External stack*/
631 symbol[numsym].UsageType=0x41;
634 case 'E': /*Internal ram (lower 128) bytes*/
635 case 'I': /*SFR space*/
636 symbol[numsym].UsageType=0x42;
639 case 'B': /*Internal stack*/
640 case 'G': /*Internal ram*/
641 symbol[numsym].UsageType=0x43;
644 case 'H': /*Bit addressable*/
645 case 'J': /*SBIT space*/
646 symbol[numsym].UsageType=0x44;
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 */
658 sscanf(buff, "%[^$] %c %[^$]", scope, &c, name);
659 /*The same may have been already defined */
660 for(j=0; j<numproc; j++)
662 if(EQ(name, procedure[j].name)) break;
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*/
674 /*This function name is also a global symbol*/
675 for(j=0; j<numsym; j++)/*A global symbol may have been already defined*/
677 if( EQ(name, symbol[j].name) && (symbol[j].Procedure==-1) ) break;
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*/
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);
699 for(j=0; j<numsym; j++)
701 if(EQ(symbol[j].name, name))
703 if( (symbol[j].Address==-1) && (symbol[j].Procedure==-1) )
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)
710 for(k=0; k<numproc; k++)
712 if(EQ(symbol[j].name, procedure[k].name))
714 if(procedure[k].BeginAdd==-1)
715 procedure[k].BeginAdd=Address;
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);
730 for(j=0; j<numsym; j++)
732 if(EQ(symbol[j].name, name))
734 if( (symbol[j].Address==-1) ) symbol[j].Address=Address;
739 /*It could be also a static function*/
740 for(j=0; j<numproc; j++)
742 if(EQ(procedure[j].name, name))
744 if( (procedure[j].BeginAdd==-1) ) procedure[j].BeginAdd=Address;
751 case 'L': /*Example L:Lmain$j$1$1:29*/
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
760 sscanf(&buff[3], "%[^$] %c %[^$] %c %[^:] %c %x",
761 scope, &c, name, &c, level, &c, &Address);
763 for(k=0; k<numproc; k++)
765 if(EQ(procedure[k].name, scope)) break;
768 if(k<numproc) for(j=0; j<numsym; j++)
770 if( EQ(symbol[j].name, name) && (symbol[j].Procedure==k) )
772 if(symbol[j].Address==-1) symbol[j].Address=Address;
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);
783 for(j=0; j<numin; j++)
784 if(EQ(infn[j].ModuleName, name)) break;
787 /*Check if this line is already defined*/
788 for(k=0; k<numlinenum; k++)
790 if( (linenum[k].Number==CLine) &&
791 (linenum[k].FileNameNumber==j) )break;
793 if(k==numlinenum) /*New line number*/
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;
805 case 'A': /*Example L:A$adq$424:40*/
806 /*No use for this one*/
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);
814 for(k=0; k<numproc; k++)
816 if(EQ(procedure[k].name, name))
818 if(procedure[k].EndAdd==-1) procedure[k].EndAdd=Address;
831 /*Make sure each procedure has an end*/
832 for(k=0; k<(numproc-1); k++)
834 if (procedure[k].EndAdd==-1) procedure[k].EndAdd=procedure[k+1].BeginAdd-1;
836 /*Asign each line number to a procedure*/
837 for(j=0; j<numlinenum; j++)
839 for(k=0; k<numproc; k++)
841 if ( (linenum[j].Address>=procedure[k].BeginAdd) &&
842 (linenum[j].Address<=procedure[k].EndAdd) &&
843 (linenum[j].FileNameNumber==procedure[k].FileNameNumber) )
845 linenum[j].Procedure=k;
853 int hex2dec (char hex_digit)
856 j=toupper(hex_digit)-'0';
861 unsigned char GetByte(char * buffer)
863 return hex2dec(buffer[0])*0x10+hex2dec(buffer[1]);
866 unsigned short GetWord(char * buffer)
868 return hex2dec(buffer[0])*0x1000+
869 hex2dec(buffer[1])*0x100+
870 hex2dec(buffer[2])*0x10+
874 int ReadHexFile(int * Begin)
879 unsigned char linesize, recordtype, rchksum, value;
880 unsigned short address;
884 /*If the hexfile is already open, close it*/
891 strcpy(ihxFileName, infn[0].PathName);
892 strcat(ihxFileName, ".ihx");
894 if ( (filein=fopen(ihxFileName, "r")) == NULL )
896 printf("Error: Can't open file `%s`.\r\n", ihxFileName);
900 ihxBuff=calloc(MEMSIZE, sizeof(unsigned char));
903 printf("Insufficient memory\n");
908 for(j=0; j<MEMSIZE; j++) ihxBuff[j]=0xff;
912 if(fgets(buffer, sizeof(buffer), filein)==NULL)
914 printf("Error reading file '%s'\n", ihxFileName);
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;
925 if (recordtype==1) break; /*End of record*/
927 for(j=0; j<linesize; j++)
929 value=GetByte(&buffer[9]+(j*2));
931 ihxBuff[address+j]=value;
933 if(MaxAddress<(address+linesize-1)) MaxAddress=(address+linesize-1);
934 if(address<*Begin) *Begin=address;
936 if((chksum%0x100)!=0)
938 printf("ERROR: Bad checksum in file %s\n", ihxFileName);
949 void CreateAOMF51(void)
951 if((dflag) && (!rflag))
953 CollectInfoFromCDB();
957 HexSize=ReadHexFile(&HexBegin)+1;