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 k=symbol[j].UsageType&0xf;
242 fprintf(DumpFile, "%s, %s, %s, 0x%04x, %s\n",
244 infn[symbol[j].FileNameNumber].PathName,
245 (symbol[j].Procedure>=0)?procedure[symbol[j].Procedure].name:"GLOBAL",
247 k<6?UsageTypeName[k]:"???");
250 fprintf(DumpFile,"\nPROCEDURES:\n");
251 for(j=0; j<numproc; j++)
253 fprintf(DumpFile, "%s, %s, 0x%04x-0x%04x\n",
255 infn[procedure[j].FileNameNumber].PathName,
256 procedure[j].BeginAdd,
257 procedure[j].EndAdd);
260 fprintf(DumpFile,"\nLINE NUMBERS:\n");
261 for(j=0; j<numlinenum; j++)
263 fprintf(DumpFile, "%d:0x%04x, %s -> %s\n",
266 infn[linenum[j].FileNameNumber].PathName,
267 (linenum[j].Procedure>=0)?procedure[linenum[j].Procedure].name:"I don't know");
274 void OutputAOEMF51(void)
276 int i, j, k, recsize;
277 char MHRname[0x100], Mname[0x100];
279 strcpy(aomf51FileName, infn[0].PathName);
281 aomf51out=fopen(aomf51FileName, "wb");
284 printf("Couldn't create file %s\n", aomf51FileName);
288 GetName(infn[0].PathName, MHRname);
291 /*Module header record*/
292 OutputByte(0x02);/*REC TYPE*/
293 OutputWord((strlen(MHRname)+1)+3);/*Record Length*/
294 OutputName(MHRname);/*Module Name*/
295 OutputByte(0xff);/*TRN ID: RL51?*/
299 for(j=0; j<numin; j++)
301 GetName(infn[j].PathName, Mname);
303 /*Scope Definition record: begin module block*/
304 OutputByte(0x10);/*REC TYPE*/
305 OutputWord((strlen(Mname)+1)+2);/*Record Length*/
306 OutputByte(0x00);/*BLK TYP: module block*/
307 OutputName(Mname);/*Module Name*/
310 /*Public symbols defined in this module*/
312 for(k=0; k<numsym; k++)/*Compute the record length*/
313 if ( (symbol[k].FileNameNumber==j) && (symbol[k].Address!=-1) &&
314 (symbol[k].Procedure==-1) &&
315 (symbol[k].Static==-1) ) recsize+=((strlen(symbol[k].name)+1)+5);
317 if(recsize>2) /*If there are any symbols*/
319 OutputByte(0x12); /*REC TYPE*/
320 OutputWord(recsize);/*Record Length*/
321 OutputByte(0x01); /*DEF TYPE: Public symbols*/
322 for(k=0; k<numsym; k++)
324 if ( (symbol[k].FileNameNumber==j) && (symbol[k].Address!=-1) &&
325 (symbol[k].Procedure==-1) &&
326 (symbol[k].Static==-1) )
328 OutputByte(0x00);/*SEG ID*/
329 OutputByte((unsigned char)symbol[k].UsageType);/*SYM INFO*/
330 OutputWord(symbol[k].Address);/*Offset*/
332 OutputName(symbol[k].name);/*Symbol name*/
338 /*Local symbols defined in this module*/
340 for(k=0; k<numsym; k++)/*Compute the record length*/
341 if ( (symbol[k].FileNameNumber==j) && (symbol[k].Address!=-1) &&
342 (symbol[k].Procedure==-1) &&
343 (symbol[k].Static==j) ) recsize+=((strlen(symbol[k].name)+1)+5);
345 if(recsize>2) /*If there are any symbols*/
347 OutputByte(0x12); /*REC TYPE*/
348 OutputWord(recsize);/*Record Length*/
349 OutputByte(0x00); /*DEF TYPE: Local symbols*/
350 for(k=0; k<numsym; k++)
352 if ( (symbol[k].FileNameNumber==j) && (symbol[k].Address!=-1) &&
353 (symbol[k].Procedure==-1) &&
354 (symbol[k].Static==j) )
356 OutputByte(0x00);/*SEG ID*/
357 OutputByte((unsigned char)symbol[k].UsageType);/*SYM INFO*/
358 OutputWord(symbol[k].Address);/*Offset*/
360 OutputName(symbol[k].name);/*Symbol name*/
366 /*Output the procedures of this module*/
368 for(k=0; k<numproc; k++)
370 if(procedure[k].FileNameNumber==j)
372 /*Scope Definition record: begin PROCEDURE block*/
373 OutputByte(0x10);/*REC TYPE*/
374 OutputWord((strlen(procedure[k].name)+1)+2);/*Record Length*/
375 OutputByte(0x02);/*BLK TYP: PROCEDURE block*/
376 OutputName(procedure[k].name);/*Module Name*/
380 OutputByte(0x06);/*REC TYPE*/
381 if(procedure[k].EndAdd==-1) procedure[k].EndAdd=HexSize;
382 recsize=procedure[k].EndAdd-procedure[k].BeginAdd+1+4;
383 OutputWord(recsize);/*Record Length*/
384 OutputByte(0x00);/*SEG ID*/
385 OutputWord(procedure[k].BeginAdd); /*Offset*/
386 for(i=procedure[k].BeginAdd; i<=procedure[k].EndAdd; i++)
387 OutputByte(ihxBuff[i]);
393 for(i=0; i<numsym; i++)/*Get the record length*/
394 if(symbol[i].Procedure==k)
395 recsize+=((strlen(symbol[i].name)+1)+5);
397 if(recsize>2) /*If there are any symbols*/
399 OutputByte(0x12); /*REC TYPE*/
400 OutputWord(recsize);/*Record Length*/
401 OutputByte(0x00); /*DEF TYPE: Local symbols*/
402 for(i=0; i<numsym; i++)
404 if ( (symbol[i].Procedure==k) )
406 OutputByte(0x00);/*SEG ID*/
407 OutputByte((unsigned char)symbol[i].UsageType);/*SYM INFO*/
408 OutputWord(symbol[i].Address);/*Offset*/
410 OutputName(symbol[i].name);/*Symbol name*/
418 for(i=0; i<numlinenum; i++)/*Get the record length*/
419 if(linenum[i].Procedure==k) recsize+=5;
421 if(recsize>2) /*If there are any line numbers*/
423 OutputByte(0x12); /*REC TYPE*/
424 OutputWord(recsize);/*Record Length*/
425 OutputByte(0x03); /*DEF TYPE: Line numbers*/
426 for(i=0; i<numlinenum; i++)
428 if ( (linenum[i].Procedure==k) )
430 OutputByte(0x00);/*SEG ID*/
431 OutputWord(linenum[i].Address);/*Offset*/
432 OutputWord(linenum[i].Number);/*Line Number*/
438 /*Scope Definition record: end PROCEDURE block*/
439 OutputByte(0x10);/*REC TYPE*/
440 OutputWord((strlen(procedure[k].name)+1)+2);/*Record Length*/
441 OutputByte(0x05);/*BLK TYP: PROCEDURE end block*/
442 OutputName(procedure[k].name);/*Module Name*/
447 /*Scope Definition record: end module block*/
448 OutputByte(0x10);/*REC TYPE*/
449 OutputWord((strlen(Mname)+1)+2);/*Record Length*/
450 OutputByte(0x03);/*BLK TYP: module end*/
451 OutputName(Mname);/*Module Name*/
455 /*Content records for everything that is not in the above procedures*/
456 strcpy(Mname, "OTHER_SDCC_STUF");
458 /*Scope Definition record: begin module block*/
459 OutputByte(0x10);/*REC TYPE*/
460 OutputWord((strlen(Mname)+1)+2);/*Record Length*/
461 OutputByte(0x00);/*BLK TYP: module block*/
462 OutputName(Mname);/*Module Name*/
465 for(j=-1; j<numproc; j++)
472 k=procedure[0].BeginAdd;
474 else if(j==(numproc-1))
476 i=procedure[j].EndAdd+1;
481 i=procedure[j].EndAdd+1;
482 k=procedure[j+1].BeginAdd;
485 else /*What, no procedures??? Ok, here it is the whole hex file*/
494 OutputByte(0x06);/*REC TYPE*/
495 OutputWord(k-i+4);/*Record Length*/
496 OutputByte(0x00);/*SEG ID*/
497 OutputWord(i); /*Offset*/
498 for(; i<k; i++) OutputByte(ihxBuff[i]);
503 /*Scope Definition record: end module block*/
504 OutputByte(0x10);/*REC TYPE*/
505 OutputWord((strlen(Mname)+1)+2);/*Record Length*/
506 OutputByte(0x03);/*BLK TYP: module end*/
507 OutputName(Mname);/*Module Name*/
510 /*Module end record*/
511 OutputByte(0x04);/*REC TYPE*/
512 OutputWord((strlen(MHRname)+1)+5);/*Record Length*/
513 OutputName(MHRname);/*Module Name*/
515 OutputByte(0x0f);/*REG MSK: All the register banks?*/
522 void CollectInfoFromCDB(void)
524 int i, j, k, CurrentModule;
527 char SourceName[PATH_MAX];
529 //"S:{G|F<filename>|L<functionName>}$<name>$<level>$<block>(<type info>),<Address Space>,<on Stack?>,<stack offset>"
530 char Sfmt[]="%[^$] %c %[^$] %c %[^$] %c %s";
536 char Bfmt[]="%[^)] %c %c %c %c %d %c %d";
537 char TypeInfo[0x100];
551 /*Build the source filename*/
552 strcpy(SourceName, infn[0].PathName);
553 strcat(SourceName, ".cdb");
554 CDBin=fopen(SourceName, "r");
557 printf("Couldn't open file '%s'\n", SourceName);
561 CurrentModule=0; /*Set the active module as the first one*/
564 fgets(buff, sizeof(buff)-1, CDBin);
566 if(!feof(CDBin)) switch(buff[0])
570 sscanf(&buff[2], "%s", name);
571 for(j=0; j<numin; j++)
572 if(EQ(infn[j].ModuleName, name)) break;
573 if(j<numin) CurrentModule=j;
577 "S:G$actual$0$0({7}ST__00010000:S),E,0,0"
578 "S:Lmain$j$1$1({2}SI:S),E,0,0"
579 "S:G$DS1306_Reset_SPI$0$0({2}DF,SV:S),C,0,0"
580 "S:G$main$0$0({2}DF,SV:S),C,0,0"
590 /*<block>(<type info>),<Address Space>,<on Stack?>,<stack offset>*/
600 case 'G': /*Global symbol*/
602 case 'L': /*Local symbol of a procedure*/
603 for(j=0; j<numproc; j++)
605 if(EQ(&scope[3], procedure[j].name)) break;
607 if(j<numproc) k=j; /*Local symbol*/
609 case 'F': /*Local symbol to a module*/
610 for(j=0; j<numin; j++)
612 if(EQ(&scope[3], infn[j].ModuleName)) break;
618 /*This symbol may have been already defined*/
619 for(j=0; j<numsym; j++)
621 if( EQ(name, symbol[j].name) &&
622 (symbol[j].Procedure==k) &&
623 (symbol[j].Static==i) ) break;
625 if(j==numsym) /*New symbol*/
627 symbol=realloc(symbol, sizeof(_symbol)*(numsym+1));
628 symbol[numsym].FileNameNumber=CurrentModule;
629 strcpy(symbol[numsym].name, name);
630 symbol[numsym].Procedure=k;
631 symbol[numsym].Static=i;
632 symbol[numsym].Address=-1;/*Collected later*/
637 case 'D': /*Code/static segment*/
638 case 'Z': /*Functions and undefined code space*/
639 symbol[numsym].UsageType=0x40;
642 case 'F': /*External ram*/
643 case 'A': /*External stack*/
644 symbol[numsym].UsageType=0x41;
647 case 'E': /*Internal ram (lower 128) bytes*/
648 case 'I': /*SFR space*/
649 case 'R': /*Register Space*/
650 symbol[numsym].UsageType=0x42;
653 case 'B': /*Internal stack*/
654 case 'G': /*Internal ram*/
655 symbol[numsym].UsageType=0x43;
658 case 'H': /*Bit addressable*/
659 case 'J': /*SBIT space*/
660 symbol[numsym].UsageType=0x44;
664 printf("Unknown scope information for: %s, AddressSpace:%c\n", symbol[numsym].name, AddressSpace);
672 F:G$AsciiToHex$0$0({2}DF,SC:U),C,0,0,0,0,0
673 F:G$main$0$0({2}DF,SV:S),C,0,0,0,0,0 */
676 sscanf(buff, "%[^$] %c %[^$]", scope, &c, name);
677 /*The same may have been already defined */
678 for(j=0; j<numproc; j++)
680 if(EQ(name, procedure[j].name)) break;
684 procedure=realloc(procedure, sizeof(_procedure)*(numproc+1));
685 strcpy(procedure[numproc].name, name);
686 procedure[numproc].FileNameNumber=CurrentModule;
687 procedure[numproc].BeginAdd=-1;/*To be collected latter*/
688 procedure[numproc].EndAdd=-1;/*To be collected latter*/
692 /*This function name is also a global symbol*/
693 for(j=0; j<numsym; j++)/*A global symbol may have been already defined*/
695 if( EQ(name, symbol[j].name) && (symbol[j].Procedure==-1) ) break;
699 symbol=realloc(symbol, sizeof(_symbol)*(numsym+1));
700 symbol[numsym].FileNameNumber=CurrentModule;
701 strcpy(symbol[numsym].name, name);
702 symbol[numsym].UsageType=0x00;/*A procedure name symbol*/
703 symbol[numsym].Procedure=-1; /*Global symbol*/
704 symbol[numsym].Address=-1;/*Collected later*/
712 case 'G': /*Example L:G$P0$0$0:80*/
713 sscanf(buff, "%[^$] %c %[^$] %c %[^:] %c %x",
714 scope, &c, name, &c, level, &c, &Address);
716 for(j=0; j<numsym; j++)
718 if(EQ(symbol[j].name, name))
720 if( (symbol[j].Address==-1) && (symbol[j].Procedure==-1) )
722 symbol[j].Address=Address;
725 /*If the symbol is the name of a procedure, the address is also
726 the begining of such procedure*/
727 if((symbol[j].UsageType&0x0f)==0x00)
729 for(k=0; k<numproc; k++)
731 if(EQ(symbol[j].name, procedure[k].name))
733 if(procedure[k].BeginAdd==-1)
734 procedure[k].BeginAdd=Address;
745 case 'F': /*Example L:Fadq$_str_2$0$0:57A*/
746 sscanf(buff, "%[^$] %c %[^$] %c %[^:] %c %x",
747 scope, &c, name, &c, level, &c, &Address);
749 for(j=0; j<numsym; j++)
751 if(EQ(symbol[j].name, name))
753 if( (symbol[j].Address==-1) ) symbol[j].Address=Address;
758 /*It could be also a static function*/
759 for(j=0; j<numproc; j++)
761 if(EQ(procedure[j].name, name))
763 if( (procedure[j].BeginAdd==-1) ) procedure[j].BeginAdd=Address;
770 case 'L': /*Example L:Lmain$j$1$1:29*/
773 L:LDS1306_Write$Value$1$1:34
774 L:LDS1306_Burst_Read$count$1$1:35
775 L:LDS1306_Burst_Read$address$1$1:36
776 L:LDS1306_Burst_Write$count$1$1:37
777 L:LDS1306_Burst_Write$address$1$1:38
779 sscanf(&buff[3], "%[^$] %c %[^$] %c %[^:] %c %x",
780 scope, &c, name, &c, level, &c, &Address);
782 for(k=0; k<numproc; k++)
784 if(EQ(procedure[k].name, scope)) break;
787 if(k<numproc) for(j=0; j<numsym; j++)
789 if( EQ(symbol[j].name, name) && (symbol[j].Procedure==k) )
791 if(symbol[j].Address==-1) symbol[j].Address=Address;
798 case 'C': /*Example L:C$adq.c$38$1$1:3E*/ /*L:C$hwinit.c$29$1$1:7AD*/
799 sscanf(&buff[4], "%[^.] %[^$] %c %d %[^:] %c %x",
800 name, level, &c, &CLine, level, &c, &Address);
802 for(j=0; j<numin; j++)
803 if(EQ(infn[j].ModuleName, name)) break;
806 /*Check if this line is already defined*/
807 for(k=0; k<numlinenum; k++)
809 if( (linenum[k].Number==CLine) &&
810 (linenum[k].FileNameNumber==j) )break;
812 if(k==numlinenum) /*New line number*/
814 linenum=realloc(linenum, sizeof(_linenum)*(numlinenum+1));
815 linenum[numlinenum].Number=CLine;
816 linenum[numlinenum].FileNameNumber=j;
817 linenum[numlinenum].Procedure=-1;/*To be asigned later*/
818 linenum[numlinenum].Address=Address;
824 case 'A': /*Example L:A$adq$424:40*/
825 /*No use for this one*/
828 /*The end of a procedure*/
829 case 'X': /*Example L:XG$AsciiToHex$0$0:88*/
830 sscanf(&buff[3], "%[^$] %c %[^$] %c %[^:] %c %x",
831 scope, &c, name, &c, level, &c, &Address);
833 for(k=0; k<numproc; k++)
835 if(EQ(procedure[k].name, name))
837 if(procedure[k].EndAdd==-1) procedure[k].EndAdd=Address;
850 /*Make sure each procedure has an end*/
851 for(k=0; k<(numproc-1); k++)
853 if (procedure[k].EndAdd==-1) procedure[k].EndAdd=procedure[k+1].BeginAdd-1;
855 /*Asign each line number to a procedure*/
856 for(j=0; j<numlinenum; j++)
858 for(k=0; k<numproc; k++)
860 if ( (linenum[j].Address>=procedure[k].BeginAdd) &&
861 (linenum[j].Address<=procedure[k].EndAdd) &&
862 (linenum[j].FileNameNumber==procedure[k].FileNameNumber) )
864 linenum[j].Procedure=k;
872 int hex2dec (char hex_digit)
875 j=toupper(hex_digit)-'0';
880 unsigned char GetByte(char * buffer)
882 return hex2dec(buffer[0])*0x10+hex2dec(buffer[1]);
885 unsigned short GetWord(char * buffer)
887 return hex2dec(buffer[0])*0x1000+
888 hex2dec(buffer[1])*0x100+
889 hex2dec(buffer[2])*0x10+
893 int ReadHexFile(int * Begin)
898 unsigned char linesize, recordtype, rchksum, value;
899 unsigned short address;
903 /*If the hexfile is already open, close it*/
910 strcpy(ihxFileName, infn[0].PathName);
911 strcat(ihxFileName, ".ihx");
913 if ( (filein=fopen(ihxFileName, "r")) == NULL )
915 printf("Error: Can't open file `%s`.\r\n", ihxFileName);
919 ihxBuff=calloc(MEMSIZE, sizeof(unsigned char));
922 printf("Insufficient memory\n");
927 for(j=0; j<MEMSIZE; j++) ihxBuff[j]=0xff;
931 if(fgets(buffer, sizeof(buffer), filein)==NULL)
933 printf("Error reading file '%s'\n", ihxFileName);
938 linesize = GetByte(&buffer[1]);
939 address = GetWord(&buffer[3]);
940 recordtype = GetByte(&buffer[7]);
941 rchksum = GetByte(&buffer[9]+(linesize*2));
942 chksum=linesize+(address/0x100)+(address%0x100)+recordtype+rchksum;
944 if (recordtype==1) break; /*End of record*/
946 for(j=0; j<linesize; j++)
948 value=GetByte(&buffer[9]+(j*2));
950 ihxBuff[address+j]=value;
952 if(MaxAddress<(address+linesize-1)) MaxAddress=(address+linesize-1);
953 if(address<*Begin) *Begin=address;
955 if((chksum%0x100)!=0)
957 printf("ERROR: Bad checksum in file %s\n", ihxFileName);
968 void CreateAOMF51(void)
970 if((dflag) && (!rflag))
972 CollectInfoFromCDB();
976 HexSize=ReadHexFile(&HexBegin)+1;