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[]=
109 {"", 5} /*A typeless number?*/
112 char * UsageTypeName[]={"CODE", "XDATA", "DATA", "IDATA", "BIT", "NUMBER"};
114 unsigned char * ihxBuff=NULL;
117 int HexSize, HexBegin=0x10000;
120 void GetName(char * filepath, char * name)
123 for(j=strlen(filepath); j>0; j--)
124 if( (filepath[j-1]=='/')||(filepath[j-1]=='\\') ) break;
125 for(k=0; (filepath[j]!=0)&&(filepath[j]!='.'); j++, k++)
130 void SaveLinkedFilePath(char * filepath)
134 if((dflag) && (!rflag))
136 infn=realloc(infn, sizeof(_infn)*(numin+1));
138 strcpy(infn[numin].PathName, filepath);
139 j=strlen(infn[numin].PathName);
141 /*If there is an extension remove it*/
144 if(EQ(&infn[numin].PathName[j-4], ".rel"))
146 infn[numin].PathName[j-4]=0;
150 /*Get the module name=filename, no drive, no dir, no ext*/
151 GetName(infn[numin].PathName, infn[numin].ModuleName);
152 //printf("%s, %s\n", infn[numin].PathName, infn[numin].ModuleName);
154 /*Check if this filename is already in*/
155 for(j=0; j<numin; j++)
157 if(EQ(infn[numin].PathName, infn[j].PathName)) break;
159 if(j==numin) numin++;
200 void OutputByte(unsigned char value)
203 fwrite( &value, 1, 1, aomf51out );
206 void OutputWord(int value)
208 OutputByte((unsigned char)(value%0x100));
209 OutputByte((unsigned char)(value/0x100));
212 void OutputName(char * name)
215 OutputByte((unsigned char)strlen(name));
216 for(k=0; name[k]!=0; k++)
217 OutputByte((unsigned char)toupper(name[k]));
220 void OutputChkSum(void)
222 OutputByte((unsigned char)(0x100-(GlobalChkSum%0x100)));
227 void DumpForDebug (void)
229 char DumpFileName[PATH_MAX];
233 strcpy(DumpFileName, infn[0].PathName);
234 strcat(DumpFileName, ".d51");
236 DumpFile=fopen(DumpFileName, "wb");
239 printf("Couldn't create file %s\n", DumpFileName);
243 fprintf(DumpFile,"SYMBOLS:\n");
245 for(j=0; j<numsym; j++)
247 k=symbol[j].UsageType&0xf;
248 fprintf(DumpFile, "%s, %s, %s, 0x%04x, %s\n",
250 infn[symbol[j].FileNameNumber].PathName,
251 (symbol[j].Procedure>=0)?procedure[symbol[j].Procedure].name:"GLOBAL",
253 k<6?UsageTypeName[k]:"???");
256 fprintf(DumpFile,"\nPROCEDURES:\n");
257 for(j=0; j<numproc; j++)
259 fprintf(DumpFile, "%s, %s, 0x%04x-0x%04x\n",
261 infn[procedure[j].FileNameNumber].PathName,
262 procedure[j].BeginAdd,
263 procedure[j].EndAdd);
266 fprintf(DumpFile,"\nLINE NUMBERS:\n");
267 for(j=0; j<numlinenum; j++)
269 fprintf(DumpFile, "%d:0x%04x, %s -> %s\n",
272 infn[linenum[j].FileNameNumber].PathName,
273 (linenum[j].Procedure>=0)?procedure[linenum[j].Procedure].name:"I don't know");
280 void OutputAOEMF51(void)
282 int i, j, k, recsize;
283 char MHRname[0x100], Mname[0x100];
285 strcpy(aomf51FileName, infn[0].PathName);
287 aomf51out=fopen(aomf51FileName, "wb");
290 printf("Couldn't create file %s\n", aomf51FileName);
294 GetName(infn[0].PathName, MHRname);
297 /*Module header record*/
298 OutputByte(0x02);/*REC TYPE*/
299 OutputWord((strlen(MHRname)+1)+3);/*Record Length*/
300 OutputName(MHRname);/*Module Name*/
301 OutputByte(0xff);/*TRN ID: RL51?*/
305 for(j=0; j<numin; j++)
307 GetName(infn[j].PathName, Mname);
309 /*Scope Definition record: begin module block*/
310 OutputByte(0x10);/*REC TYPE*/
311 OutputWord((strlen(Mname)+1)+2);/*Record Length*/
312 OutputByte(0x00);/*BLK TYP: module block*/
313 OutputName(Mname);/*Module Name*/
316 /*Public symbols defined in this module*/
318 for(k=0; k<numsym; k++)/*Compute the record length*/
319 if ( (symbol[k].FileNameNumber==j) && (symbol[k].Address!=-1) &&
320 (symbol[k].Procedure==-1) &&
321 (symbol[k].Static==-1) ) recsize+=((strlen(symbol[k].name)+1)+5);
323 if(recsize>2) /*If there are any symbols*/
325 OutputByte(0x12); /*REC TYPE*/
326 OutputWord(recsize);/*Record Length*/
327 OutputByte(0x01); /*DEF TYPE: Public symbols*/
328 for(k=0; k<numsym; k++)
330 if ( (symbol[k].FileNameNumber==j) && (symbol[k].Address!=-1) &&
331 (symbol[k].Procedure==-1) &&
332 (symbol[k].Static==-1) )
334 OutputByte(0x00);/*SEG ID*/
335 OutputByte((unsigned char)symbol[k].UsageType);/*SYM INFO*/
336 OutputWord(symbol[k].Address);/*Offset*/
338 OutputName(symbol[k].name);/*Symbol name*/
344 /*Local symbols defined in this module*/
346 for(k=0; k<numsym; k++)/*Compute the record length*/
347 if ( (symbol[k].FileNameNumber==j) && (symbol[k].Address!=-1) &&
348 (symbol[k].Procedure==-1) &&
349 (symbol[k].Static==j) ) recsize+=((strlen(symbol[k].name)+1)+5);
351 if(recsize>2) /*If there are any symbols*/
353 OutputByte(0x12); /*REC TYPE*/
354 OutputWord(recsize);/*Record Length*/
355 OutputByte(0x00); /*DEF TYPE: Local symbols*/
356 for(k=0; k<numsym; k++)
358 if ( (symbol[k].FileNameNumber==j) && (symbol[k].Address!=-1) &&
359 (symbol[k].Procedure==-1) &&
360 (symbol[k].Static==j) )
362 OutputByte(0x00);/*SEG ID*/
363 OutputByte((unsigned char)symbol[k].UsageType);/*SYM INFO*/
364 OutputWord(symbol[k].Address);/*Offset*/
366 OutputName(symbol[k].name);/*Symbol name*/
372 /*Output the procedures of this module*/
374 for(k=0; k<numproc; k++)
376 if(procedure[k].FileNameNumber==j)
378 /*Scope Definition record: begin PROCEDURE block*/
379 OutputByte(0x10);/*REC TYPE*/
380 OutputWord((strlen(procedure[k].name)+1)+2);/*Record Length*/
381 OutputByte(0x02);/*BLK TYP: PROCEDURE block*/
382 OutputName(procedure[k].name);/*Module Name*/
386 OutputByte(0x06);/*REC TYPE*/
387 if(procedure[k].EndAdd==-1) procedure[k].EndAdd=HexSize;
388 recsize=procedure[k].EndAdd-procedure[k].BeginAdd+1+4;
389 OutputWord(recsize);/*Record Length*/
390 OutputByte(0x00);/*SEG ID*/
391 OutputWord(procedure[k].BeginAdd); /*Offset*/
392 for(i=procedure[k].BeginAdd; i<=procedure[k].EndAdd; i++)
393 OutputByte(ihxBuff[i]);
399 for(i=0; i<numsym; i++)/*Get the record length*/
400 if(symbol[i].Procedure==k)
401 recsize+=((strlen(symbol[i].name)+1)+5);
403 if(recsize>2) /*If there are any symbols*/
405 OutputByte(0x12); /*REC TYPE*/
406 OutputWord(recsize);/*Record Length*/
407 OutputByte(0x00); /*DEF TYPE: Local symbols*/
408 for(i=0; i<numsym; i++)
410 if ( (symbol[i].Procedure==k) )
412 OutputByte(0x00);/*SEG ID*/
413 OutputByte((unsigned char)symbol[i].UsageType);/*SYM INFO*/
414 OutputWord(symbol[i].Address);/*Offset*/
416 OutputName(symbol[i].name);/*Symbol name*/
424 for(i=0; i<numlinenum; i++)/*Get the record length*/
425 if(linenum[i].Procedure==k) recsize+=5;
427 if(recsize>2) /*If there are any line numbers*/
429 OutputByte(0x12); /*REC TYPE*/
430 OutputWord(recsize);/*Record Length*/
431 OutputByte(0x03); /*DEF TYPE: Line numbers*/
432 for(i=0; i<numlinenum; i++)
434 if ( (linenum[i].Procedure==k) )
436 OutputByte(0x00);/*SEG ID*/
437 OutputWord(linenum[i].Address);/*Offset*/
438 OutputWord(linenum[i].Number);/*Line Number*/
444 /*Scope Definition record: end PROCEDURE block*/
445 OutputByte(0x10);/*REC TYPE*/
446 OutputWord((strlen(procedure[k].name)+1)+2);/*Record Length*/
447 OutputByte(0x05);/*BLK TYP: PROCEDURE end block*/
448 OutputName(procedure[k].name);/*Module Name*/
453 /*Scope Definition record: end module block*/
454 OutputByte(0x10);/*REC TYPE*/
455 OutputWord((strlen(Mname)+1)+2);/*Record Length*/
456 OutputByte(0x03);/*BLK TYP: module end*/
457 OutputName(Mname);/*Module Name*/
461 /*Content records for everything that is not in the above procedures*/
462 strcpy(Mname, "OTHER_SDCC_STUF");
464 /*Scope Definition record: begin module block*/
465 OutputByte(0x10);/*REC TYPE*/
466 OutputWord((strlen(Mname)+1)+2);/*Record Length*/
467 OutputByte(0x00);/*BLK TYP: module block*/
468 OutputName(Mname);/*Module Name*/
471 for(j=-1; j<numproc; j++)
478 k=procedure[0].BeginAdd;
480 else if(j==(numproc-1))
482 i=procedure[j].EndAdd+1;
487 i=procedure[j].EndAdd+1;
488 k=procedure[j+1].BeginAdd;
491 else /*What, no procedures??? Ok, here it is the whole hex file*/
500 OutputByte(0x06);/*REC TYPE*/
501 OutputWord(k-i+4);/*Record Length*/
502 OutputByte(0x00);/*SEG ID*/
503 OutputWord(i); /*Offset*/
504 for(; i<k; i++) OutputByte(ihxBuff[i]);
509 /*Scope Definition record: end module block*/
510 OutputByte(0x10);/*REC TYPE*/
511 OutputWord((strlen(Mname)+1)+2);/*Record Length*/
512 OutputByte(0x03);/*BLK TYP: module end*/
513 OutputName(Mname);/*Module Name*/
516 /*Module end record*/
517 OutputByte(0x04);/*REC TYPE*/
518 OutputWord((strlen(MHRname)+1)+5);/*Record Length*/
519 OutputName(MHRname);/*Module Name*/
521 OutputByte(0x0f);/*REG MSK: All the register banks?*/
528 void CollectInfoFromCDB(void)
530 int i, j, k, CurrentModule;
533 char SourceName[PATH_MAX];
535 //"S:{G|F<filename>|L<functionName>}$<name>$<level>$<block>(<type info>),<Address Space>,<on Stack?>,<stack offset>"
536 char Sfmt[]="%[^$] %c %[^$] %c %[^$] %c %s";
542 char Bfmt[]="%[^)] %c %c %c %c %d %c %d";
543 char TypeInfo[0x100];
557 /*Build the source filename*/
558 strcpy(SourceName, infn[0].PathName);
559 strcat(SourceName, ".cdb");
560 CDBin=fopen(SourceName, "r");
563 printf("Couldn't open file '%s'\n", SourceName);
567 CurrentModule=0; /*Set the active module as the first one*/
570 fgets(buff, sizeof(buff)-1, CDBin);
572 if(!feof(CDBin)) switch(buff[0])
576 sscanf(&buff[2], "%s", name);
577 for(j=0; j<numin; j++)
578 if(EQ(infn[j].ModuleName, name)) break;
579 if(j<numin) CurrentModule=j;
583 "S:G$actual$0$0({7}ST__00010000:S),E,0,0"
584 "S:Lmain$j$1$1({2}SI:S),E,0,0"
585 "S:G$DS1306_Reset_SPI$0$0({2}DF,SV:S),C,0,0"
586 "S:G$main$0$0({2}DF,SV:S),C,0,0"
596 /*<block>(<type info>),<Address Space>,<on Stack?>,<stack offset>*/
606 case 'G': /*Global symbol*/
608 case 'L': /*Local symbol of a procedure*/
609 for(j=0; j<numproc; j++)
611 if(EQ(&scope[3], procedure[j].name)) break;
613 if(j<numproc) k=j; /*Local symbol*/
615 case 'F': /*Local symbol to a module*/
616 for(j=0; j<numin; j++)
618 if(EQ(&scope[3], infn[j].ModuleName)) break;
624 /*This symbol may have been already defined*/
625 for(j=0; j<numsym; j++)
627 if( EQ(name, symbol[j].name) &&
628 (symbol[j].Procedure==k) &&
629 (symbol[j].Static==i) ) break;
631 if(j==numsym) /*New symbol*/
633 symbol=realloc(symbol, sizeof(_symbol)*(numsym+1));
634 symbol[numsym].FileNameNumber=CurrentModule;
635 strcpy(symbol[numsym].name, name);
636 symbol[numsym].Procedure=k;
637 symbol[numsym].Static=i;
638 symbol[numsym].Address=-1;/*Collected later*/
643 case 'D': /*Code/static segment*/
644 case 'Z': /*Functions and undefined code space*/
645 symbol[numsym].UsageType=0x40;
648 case 'F': /*External ram*/
649 case 'A': /*External stack*/
650 symbol[numsym].UsageType=0x41;
653 case 'E': /*Internal ram (lower 128) bytes*/
654 case 'I': /*SFR space*/
655 case 'R': /*Register Space*/
656 symbol[numsym].UsageType=0x42;
659 case 'B': /*Internal stack*/
660 case 'G': /*Internal ram*/
661 symbol[numsym].UsageType=0x43;
664 case 'H': /*Bit addressable*/
665 case 'J': /*SBIT space*/
666 symbol[numsym].UsageType=0x44;
670 printf("Unknown scope information for: %s, AddressSpace:%c\n", symbol[numsym].name, AddressSpace);
678 F:G$AsciiToHex$0$0({2}DF,SC:U),C,0,0,0,0,0
679 F:G$main$0$0({2}DF,SV:S),C,0,0,0,0,0 */
682 sscanf(buff, "%[^$] %c %[^$]", scope, &c, name);
683 /*The same may have been already defined */
684 for(j=0; j<numproc; j++)
686 if(EQ(name, procedure[j].name)) break;
690 procedure=realloc(procedure, sizeof(_procedure)*(numproc+1));
691 strcpy(procedure[numproc].name, name);
692 procedure[numproc].FileNameNumber=CurrentModule;
693 procedure[numproc].BeginAdd=-1;/*To be collected latter*/
694 procedure[numproc].EndAdd=-1;/*To be collected latter*/
698 /*This function name is also a global symbol*/
699 for(j=0; j<numsym; j++)/*A global symbol may have been already defined*/
701 if( EQ(name, symbol[j].name) && (symbol[j].Procedure==-1) ) break;
705 symbol=realloc(symbol, sizeof(_symbol)*(numsym+1));
706 symbol[numsym].FileNameNumber=CurrentModule;
707 strcpy(symbol[numsym].name, name);
708 symbol[numsym].UsageType=0x00;/*A procedure name symbol*/
709 symbol[numsym].Procedure=-1; /*Global symbol*/
710 symbol[numsym].Address=-1;/*Collected later*/
711 symbol[numsym].Static=-1; // o_gloom
719 case 'G': /*Example L:G$P0$0$0:80*/
720 sscanf(buff, "%[^$] %c %[^$] %c %[^:] %c %x",
721 scope, &c, name, &c, level, &c, &Address);
723 for(j=0; j<numsym; j++)
725 if(EQ(symbol[j].name, name))
727 if( (symbol[j].Address==-1) && (symbol[j].Procedure==-1) )
729 symbol[j].Address=Address;
732 /*If the symbol is the name of a procedure, the address is also
733 the begining of such procedure*/
734 if((symbol[j].UsageType&0x0f)==0x00)
736 for(k=0; k<numproc; k++)
738 if(EQ(symbol[j].name, procedure[k].name))
740 if(procedure[k].BeginAdd==-1)
741 procedure[k].BeginAdd=Address;
752 case 'F': /*Example L:Fadq$_str_2$0$0:57A*/
753 sscanf(buff, "%[^$] %c %[^$] %c %[^:] %c %x",
754 scope, &c, name, &c, level, &c, &Address);
756 for(j=0; j<numsym; j++)
758 if(EQ(symbol[j].name, name))
760 if( (symbol[j].Address==-1) ) symbol[j].Address=Address;
765 /*It could be also a static function*/
766 for(j=0; j<numproc; j++)
768 if(EQ(procedure[j].name, name))
770 if( (procedure[j].BeginAdd==-1) ) procedure[j].BeginAdd=Address;
777 case 'L': /*Example L:Lmain$j$1$1:29*/
780 L:LDS1306_Write$Value$1$1:34
781 L:LDS1306_Burst_Read$count$1$1:35
782 L:LDS1306_Burst_Read$address$1$1:36
783 L:LDS1306_Burst_Write$count$1$1:37
784 L:LDS1306_Burst_Write$address$1$1:38
786 sscanf(&buff[3], "%[^$] %c %[^$] %c %[^:] %c %x",
787 scope, &c, name, &c, level, &c, &Address);
789 for(k=0; k<numproc; k++)
791 if(EQ(procedure[k].name, scope)) break;
794 if(k<numproc) for(j=0; j<numsym; j++)
796 if( EQ(symbol[j].name, name) && (symbol[j].Procedure==k) )
798 if(symbol[j].Address==-1) symbol[j].Address=Address;
805 case 'C': /*Example L:C$adq.c$38$1$1:3E*/ /*L:C$hwinit.c$29$1$1:7AD*/
806 sscanf(&buff[4], "%[^.] %[^$] %c %d %[^:] %c %x",
807 name, level, &c, &CLine, level, &c, &Address);
809 for(j=0; j<numin; j++)
810 if(EQ(infn[j].ModuleName, name)) break;
813 /*Check if this line is already defined*/
814 for(k=0; k<numlinenum; k++)
816 if( (linenum[k].Number==CLine) &&
817 (linenum[k].FileNameNumber==j) )break;
819 if(k==numlinenum) /*New line number*/
821 linenum=realloc(linenum, sizeof(_linenum)*(numlinenum+1));
822 linenum[numlinenum].Number=CLine;
823 linenum[numlinenum].FileNameNumber=j;
824 linenum[numlinenum].Procedure=-1;/*To be asigned later*/
825 linenum[numlinenum].Address=Address;
831 case 'A': /*Example L:A$adq$424:40*/
832 /*No use for this one*/
835 /*The end of a procedure*/
836 case 'X': /*Example L:XG$AsciiToHex$0$0:88*/
837 sscanf(&buff[3], "%[^$] %c %[^$] %c %[^:] %c %x",
838 scope, &c, name, &c, level, &c, &Address);
840 for(k=0; k<numproc; k++)
842 if(EQ(procedure[k].name, name))
844 if(procedure[k].EndAdd==-1) procedure[k].EndAdd=Address;
857 /*Make sure each procedure has an end*/
858 for(k=0; k<(numproc-1); k++)
860 if (procedure[k].EndAdd==-1) procedure[k].EndAdd=procedure[k+1].BeginAdd-1;
862 /*Asign each line number to a procedure*/
863 for(j=0; j<numlinenum; j++)
865 for(k=0; k<numproc; k++)
867 if ( (linenum[j].Address>=procedure[k].BeginAdd) &&
868 (linenum[j].Address<=procedure[k].EndAdd) &&
869 (linenum[j].FileNameNumber==procedure[k].FileNameNumber) )
871 linenum[j].Procedure=k;
879 int hex2dec (char hex_digit)
882 j=toupper(hex_digit)-'0';
887 unsigned char GetByte(char * buffer)
889 return hex2dec(buffer[0])*0x10+hex2dec(buffer[1]);
892 unsigned short GetWord(char * buffer)
894 return hex2dec(buffer[0])*0x1000+
895 hex2dec(buffer[1])*0x100+
896 hex2dec(buffer[2])*0x10+
900 int ReadHexFile(int * Begin)
905 unsigned char linesize, recordtype, rchksum, value;
906 unsigned short address;
910 /*If the hexfile is already open, close it*/
917 strcpy(ihxFileName, infn[0].PathName);
918 strcat(ihxFileName, ".ihx");
920 if ( (filein=fopen(ihxFileName, "r")) == NULL )
922 printf("Error: Can't open file `%s`.\r\n", ihxFileName);
926 ihxBuff=calloc(MEMSIZE, sizeof(unsigned char));
929 printf("Insufficient memory\n");
934 for(j=0; j<MEMSIZE; j++) ihxBuff[j]=0xff;
938 if(fgets(buffer, sizeof(buffer), filein)==NULL)
940 printf("Error reading file '%s'\n", ihxFileName);
945 linesize = GetByte(&buffer[1]);
946 address = GetWord(&buffer[3]);
947 recordtype = GetByte(&buffer[7]);
948 rchksum = GetByte(&buffer[9]+(linesize*2));
949 chksum=linesize+(address/0x100)+(address%0x100)+recordtype+rchksum;
951 if (recordtype==1) break; /*End of record*/
953 for(j=0; j<linesize; j++)
955 value=GetByte(&buffer[9]+(j*2));
957 ihxBuff[address+j]=value;
959 if(MaxAddress<(address+linesize-1)) MaxAddress=(address+linesize-1);
960 if(address<*Begin) *Begin=address;
962 if((chksum%0x100)!=0)
964 printf("ERROR: Bad checksum in file %s\n", ihxFileName);
975 void CreateAOMF51(void)
977 if((dflag) && (!rflag))
979 CollectInfoFromCDB();
983 HexSize=ReadHexFile(&HexBegin)+1;