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 recsize=procedure[k].EndAdd-procedure[k].BeginAdd+1+4;
382 OutputWord(recsize);/*Record Length*/
383 OutputByte(0x00);/*SEG ID*/
384 OutputWord(procedure[k].BeginAdd); /*Offset*/
385 for(i=procedure[k].BeginAdd; i<=procedure[k].EndAdd; i++)
386 OutputByte(ihxBuff[i]);
392 for(i=0; i<numsym; i++)/*Get the record length*/
393 if(symbol[i].Procedure==k)
394 recsize+=((strlen(symbol[i].name)+1)+5);
396 if(recsize>2) /*If there are any symbols*/
398 OutputByte(0x12); /*REC TYPE*/
399 OutputWord(recsize);/*Record Length*/
400 OutputByte(0x00); /*DEF TYPE: Local symbols*/
401 for(i=0; i<numsym; i++)
403 if ( (symbol[i].Procedure==k) )
405 OutputByte(0x00);/*SEG ID*/
406 OutputByte((unsigned char)symbol[i].UsageType);/*SYM INFO*/
407 OutputWord(symbol[i].Address);/*Offset*/
409 OutputName(symbol[i].name);/*Symbol name*/
417 for(i=0; i<numlinenum; i++)/*Get the record length*/
418 if(linenum[i].Procedure==k) recsize+=5;
420 if(recsize>2) /*If there are any line numbers*/
422 OutputByte(0x12); /*REC TYPE*/
423 OutputWord(recsize);/*Record Length*/
424 OutputByte(0x03); /*DEF TYPE: Line numbers*/
425 for(i=0; i<numlinenum; i++)
427 if ( (linenum[i].Procedure==k) )
429 OutputByte(0x00);/*SEG ID*/
430 OutputWord(linenum[i].Address);/*Offset*/
431 OutputWord(linenum[i].Number);/*Line Number*/
437 /*Scope Definition record: end PROCEDURE block*/
438 OutputByte(0x10);/*REC TYPE*/
439 OutputWord((strlen(procedure[k].name)+1)+2);/*Record Length*/
440 OutputByte(0x05);/*BLK TYP: PROCEDURE end block*/
441 OutputName(procedure[k].name);/*Module Name*/
446 /*Scope Definition record: end module block*/
447 OutputByte(0x10);/*REC TYPE*/
448 OutputWord((strlen(Mname)+1)+2);/*Record Length*/
449 OutputByte(0x03);/*BLK TYP: module end*/
450 OutputName(Mname);/*Module Name*/
454 /*Content records for everything that is not in the above procedures*/
455 strcpy(Mname, "OTHER_SDCC_STUF");
457 /*Scope Definition record: begin module block*/
458 OutputByte(0x10);/*REC TYPE*/
459 OutputWord((strlen(Mname)+1)+2);/*Record Length*/
460 OutputByte(0x00);/*BLK TYP: module block*/
461 OutputName(Mname);/*Module Name*/
464 for(j=-1; j<numproc; j++)
471 k=procedure[0].BeginAdd;
473 else if(j==(numproc-1))
475 i=procedure[j].EndAdd+1;
480 i=procedure[j].EndAdd+1;
481 k=procedure[j+1].BeginAdd;
484 else /*What, no procedures??? Ok, here it is the whole hex file*/
493 OutputByte(0x06);/*REC TYPE*/
494 OutputWord(k-i+4);/*Record Length*/
495 OutputByte(0x00);/*SEG ID*/
496 OutputWord(i); /*Offset*/
497 for(; i<k; i++) OutputByte(ihxBuff[i]);
502 /*Scope Definition record: end module block*/
503 OutputByte(0x10);/*REC TYPE*/
504 OutputWord((strlen(Mname)+1)+2);/*Record Length*/
505 OutputByte(0x03);/*BLK TYP: module end*/
506 OutputName(Mname);/*Module Name*/
509 /*Module end record*/
510 OutputByte(0x04);/*REC TYPE*/
511 OutputWord((strlen(MHRname)+1)+5);/*Record Length*/
512 OutputName(MHRname);/*Module Name*/
514 OutputByte(0x0f);/*REG MSK: All the register banks?*/
521 void CollectInfoFromCDB(void)
523 int i, j, k, CurrentModule;
526 char SourceName[PATH_MAX];
528 //"S:{G|F<filename>|L<functionName>}$<name>$<level>$<block>(<type info>),<Address Space>,<on Stack?>,<stack offset>"
529 char Sfmt[]="%[^$] %c %[^$] %c %[^$] %c %s";
535 char Bfmt[]="%[^)] %c %c %c %c %d %c %d";
536 char TypeInfo[0x100];
550 /*Build the source filename*/
551 strcpy(SourceName, infn[0].PathName);
552 strcat(SourceName, ".cdb");
553 CDBin=fopen(SourceName, "r");
556 printf("Couldn't open file '%s'\n", SourceName);
560 CurrentModule=0; /*Set the active module as the first one*/
563 fgets(buff, sizeof(buff)-1, CDBin);
565 if(!feof(CDBin)) switch(buff[0])
569 sscanf(&buff[2], "%s", name);
570 for(j=0; j<numin; j++)
571 if(EQ(infn[j].ModuleName, name)) break;
572 if(j<numin) CurrentModule=j;
576 "S:G$actual$0$0({7}ST__00010000:S),E,0,0"
577 "S:Lmain$j$1$1({2}SI:S),E,0,0"
578 "S:G$DS1306_Reset_SPI$0$0({2}DF,SV:S),C,0,0"
579 "S:G$main$0$0({2}DF,SV:S),C,0,0"
589 /*<block>(<type info>),<Address Space>,<on Stack?>,<stack offset>*/
599 case 'G': /*Global symbol*/
601 case 'L': /*Local symbol of a procedure*/
602 for(j=0; j<numproc; j++)
604 if(EQ(&scope[3], procedure[j].name)) break;
606 if(j<numproc) k=j; /*Local symbol*/
608 case 'F': /*Local symbol to a module*/
609 for(j=0; j<numin; j++)
611 if(EQ(&scope[3], infn[j].ModuleName)) break;
617 /*This symbol may have been already defined*/
618 for(j=0; j<numsym; j++)
620 if( EQ(name, symbol[j].name) &&
621 (symbol[j].Procedure==k) &&
622 (symbol[j].Static==i) ) break;
624 if(j==numsym) /*New symbol*/
626 symbol=realloc(symbol, sizeof(_symbol)*(numsym+1));
627 symbol[numsym].FileNameNumber=CurrentModule;
628 strcpy(symbol[numsym].name, name);
629 symbol[numsym].Procedure=k;
630 symbol[numsym].Static=i;
631 symbol[numsym].Address=-1;/*Collected later*/
636 case 'D': /*Code/static segment*/
637 symbol[numsym].UsageType=0x40;
640 case 'F': /*External ram*/
641 case 'A': /*External stack*/
642 symbol[numsym].UsageType=0x41;
645 case 'E': /*Internal ram (lower 128) bytes*/
646 case 'I': /*SFR space*/
647 case 'R': /*Register Space*/
648 symbol[numsym].UsageType=0x42;
651 case 'B': /*Internal stack*/
652 case 'G': /*Internal ram*/
653 symbol[numsym].UsageType=0x43;
656 case 'H': /*Bit addressable*/
657 case 'J': /*SBIT space*/
658 symbol[numsym].UsageType=0x44;
662 printf("Name: %s, AddressSpace:%c\n", symbol[numsym].name, AddressSpace);
670 F:G$AsciiToHex$0$0({2}DF,SC:U),C,0,0,0,0,0
671 F:G$main$0$0({2}DF,SV:S),C,0,0,0,0,0 */
674 sscanf(buff, "%[^$] %c %[^$]", scope, &c, name);
675 /*The same may have been already defined */
676 for(j=0; j<numproc; j++)
678 if(EQ(name, procedure[j].name)) break;
682 procedure=realloc(procedure, sizeof(_procedure)*(numproc+1));
683 strcpy(procedure[numproc].name, name);
684 procedure[numproc].FileNameNumber=CurrentModule;
685 procedure[numproc].BeginAdd=-1;/*To be collected latter*/
686 procedure[numproc].EndAdd=-1;/*To be collected latter*/
690 /*This function name is also a global symbol*/
691 for(j=0; j<numsym; j++)/*A global symbol may have been already defined*/
693 if( EQ(name, symbol[j].name) && (symbol[j].Procedure==-1) ) break;
697 symbol=realloc(symbol, sizeof(_symbol)*(numsym+1));
698 symbol[numsym].FileNameNumber=CurrentModule;
699 strcpy(symbol[numsym].name, name);
700 symbol[numsym].UsageType=0x00;/*A procedure name symbol*/
701 symbol[numsym].Procedure=-1; /*Global symbol*/
702 symbol[numsym].Address=-1;/*Collected later*/
710 case 'G': /*Example L:G$P0$0$0:80*/
711 sscanf(buff, "%[^$] %c %[^$] %c %[^:] %c %x",
712 scope, &c, name, &c, level, &c, &Address);
714 for(j=0; j<numsym; j++)
716 if(EQ(symbol[j].name, name))
718 if( (symbol[j].Address==-1) && (symbol[j].Procedure==-1) )
720 symbol[j].Address=Address;
723 /*If the symbol is the name of a procedure, the address is also
724 the begining of such procedure*/
725 if((symbol[j].UsageType&0x0f)==0x00)
727 for(k=0; k<numproc; k++)
729 if(EQ(symbol[j].name, procedure[k].name))
731 if(procedure[k].BeginAdd==-1)
732 procedure[k].BeginAdd=Address;
743 case 'F': /*Example L:Fadq$_str_2$0$0:57A*/
744 sscanf(buff, "%[^$] %c %[^$] %c %[^:] %c %x",
745 scope, &c, name, &c, level, &c, &Address);
747 for(j=0; j<numsym; j++)
749 if(EQ(symbol[j].name, name))
751 if( (symbol[j].Address==-1) ) symbol[j].Address=Address;
756 /*It could be also a static function*/
757 for(j=0; j<numproc; j++)
759 if(EQ(procedure[j].name, name))
761 if( (procedure[j].BeginAdd==-1) ) procedure[j].BeginAdd=Address;
768 case 'L': /*Example L:Lmain$j$1$1:29*/
771 L:LDS1306_Write$Value$1$1:34
772 L:LDS1306_Burst_Read$count$1$1:35
773 L:LDS1306_Burst_Read$address$1$1:36
774 L:LDS1306_Burst_Write$count$1$1:37
775 L:LDS1306_Burst_Write$address$1$1:38
777 sscanf(&buff[3], "%[^$] %c %[^$] %c %[^:] %c %x",
778 scope, &c, name, &c, level, &c, &Address);
780 for(k=0; k<numproc; k++)
782 if(EQ(procedure[k].name, scope)) break;
785 if(k<numproc) for(j=0; j<numsym; j++)
787 if( EQ(symbol[j].name, name) && (symbol[j].Procedure==k) )
789 if(symbol[j].Address==-1) symbol[j].Address=Address;
796 case 'C': /*Example L:C$adq.c$38$1$1:3E*/ /*L:C$hwinit.c$29$1$1:7AD*/
797 sscanf(&buff[4], "%[^.] %[^$] %c %d %[^:] %c %x",
798 name, level, &c, &CLine, level, &c, &Address);
800 for(j=0; j<numin; j++)
801 if(EQ(infn[j].ModuleName, name)) break;
804 /*Check if this line is already defined*/
805 for(k=0; k<numlinenum; k++)
807 if( (linenum[k].Number==CLine) &&
808 (linenum[k].FileNameNumber==j) )break;
810 if(k==numlinenum) /*New line number*/
812 linenum=realloc(linenum, sizeof(_linenum)*(numlinenum+1));
813 linenum[numlinenum].Number=CLine;
814 linenum[numlinenum].FileNameNumber=j;
815 linenum[numlinenum].Procedure=-1;/*To be asigned later*/
816 linenum[numlinenum].Address=Address;
822 case 'A': /*Example L:A$adq$424:40*/
823 /*No use for this one*/
826 /*The end of a procedure*/
827 case 'X': /*Example L:XG$AsciiToHex$0$0:88*/
828 sscanf(&buff[3], "%[^$] %c %[^$] %c %[^:] %c %x",
829 scope, &c, name, &c, level, &c, &Address);
831 for(k=0; k<numproc; k++)
833 if(EQ(procedure[k].name, name))
835 if(procedure[k].EndAdd==-1) procedure[k].EndAdd=Address;
848 /*Make sure each procedure has an end*/
849 for(k=0; k<(numproc-1); k++)
851 if (procedure[k].EndAdd==-1) procedure[k].EndAdd=procedure[k+1].BeginAdd-1;
853 /*Asign each line number to a procedure*/
854 for(j=0; j<numlinenum; j++)
856 for(k=0; k<numproc; k++)
858 if ( (linenum[j].Address>=procedure[k].BeginAdd) &&
859 (linenum[j].Address<=procedure[k].EndAdd) &&
860 (linenum[j].FileNameNumber==procedure[k].FileNameNumber) )
862 linenum[j].Procedure=k;
870 int hex2dec (char hex_digit)
873 j=toupper(hex_digit)-'0';
878 unsigned char GetByte(char * buffer)
880 return hex2dec(buffer[0])*0x10+hex2dec(buffer[1]);
883 unsigned short GetWord(char * buffer)
885 return hex2dec(buffer[0])*0x1000+
886 hex2dec(buffer[1])*0x100+
887 hex2dec(buffer[2])*0x10+
891 int ReadHexFile(int * Begin)
896 unsigned char linesize, recordtype, rchksum, value;
897 unsigned short address;
901 /*If the hexfile is already open, close it*/
908 strcpy(ihxFileName, infn[0].PathName);
909 strcat(ihxFileName, ".ihx");
911 if ( (filein=fopen(ihxFileName, "r")) == NULL )
913 printf("Error: Can't open file `%s`.\r\n", ihxFileName);
917 ihxBuff=calloc(MEMSIZE, sizeof(unsigned char));
920 printf("Insufficient memory\n");
925 for(j=0; j<MEMSIZE; j++) ihxBuff[j]=0xff;
929 if(fgets(buffer, sizeof(buffer), filein)==NULL)
931 printf("Error reading file '%s'\n", ihxFileName);
936 linesize = GetByte(&buffer[1]);
937 address = GetWord(&buffer[3]);
938 recordtype = GetByte(&buffer[7]);
939 rchksum = GetByte(&buffer[9]+(linesize*2));
940 chksum=linesize+(address/0x100)+(address%0x100)+recordtype+rchksum;
942 if (recordtype==1) break; /*End of record*/
944 for(j=0; j<linesize; j++)
946 value=GetByte(&buffer[9]+(j*2));
948 ihxBuff[address+j]=value;
950 if(MaxAddress<(address+linesize-1)) MaxAddress=(address+linesize-1);
951 if(address<*Begin) *Begin=address;
953 if((chksum%0x100)!=0)
955 printf("ERROR: Bad checksum in file %s\n", ihxFileName);
966 void CreateAOMF51(void)
968 if((dflag) && (!rflag))
970 CollectInfoFromCDB();
974 HexSize=ReadHexFile(&HexBegin)+1;