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 case 'Z': /*Functions and undefined code space*/
638 symbol[numsym].UsageType=0x40;
641 case 'F': /*External ram*/
642 case 'A': /*External stack*/
643 symbol[numsym].UsageType=0x41;
646 case 'E': /*Internal ram (lower 128) bytes*/
647 case 'I': /*SFR space*/
648 case 'R': /*Register Space*/
649 symbol[numsym].UsageType=0x42;
652 case 'B': /*Internal stack*/
653 case 'G': /*Internal ram*/
654 symbol[numsym].UsageType=0x43;
657 case 'H': /*Bit addressable*/
658 case 'J': /*SBIT space*/
659 symbol[numsym].UsageType=0x44;
663 printf("Unknown scope information for: %s, AddressSpace:%c\n", symbol[numsym].name, AddressSpace);
671 F:G$AsciiToHex$0$0({2}DF,SC:U),C,0,0,0,0,0
672 F:G$main$0$0({2}DF,SV:S),C,0,0,0,0,0 */
675 sscanf(buff, "%[^$] %c %[^$]", scope, &c, name);
676 /*The same may have been already defined */
677 for(j=0; j<numproc; j++)
679 if(EQ(name, procedure[j].name)) break;
683 procedure=realloc(procedure, sizeof(_procedure)*(numproc+1));
684 strcpy(procedure[numproc].name, name);
685 procedure[numproc].FileNameNumber=CurrentModule;
686 procedure[numproc].BeginAdd=-1;/*To be collected latter*/
687 procedure[numproc].EndAdd=-1;/*To be collected latter*/
691 /*This function name is also a global symbol*/
692 for(j=0; j<numsym; j++)/*A global symbol may have been already defined*/
694 if( EQ(name, symbol[j].name) && (symbol[j].Procedure==-1) ) break;
698 symbol=realloc(symbol, sizeof(_symbol)*(numsym+1));
699 symbol[numsym].FileNameNumber=CurrentModule;
700 strcpy(symbol[numsym].name, name);
701 symbol[numsym].UsageType=0x00;/*A procedure name symbol*/
702 symbol[numsym].Procedure=-1; /*Global symbol*/
703 symbol[numsym].Address=-1;/*Collected later*/
711 case 'G': /*Example L:G$P0$0$0:80*/
712 sscanf(buff, "%[^$] %c %[^$] %c %[^:] %c %x",
713 scope, &c, name, &c, level, &c, &Address);
715 for(j=0; j<numsym; j++)
717 if(EQ(symbol[j].name, name))
719 if( (symbol[j].Address==-1) && (symbol[j].Procedure==-1) )
721 symbol[j].Address=Address;
724 /*If the symbol is the name of a procedure, the address is also
725 the begining of such procedure*/
726 if((symbol[j].UsageType&0x0f)==0x00)
728 for(k=0; k<numproc; k++)
730 if(EQ(symbol[j].name, procedure[k].name))
732 if(procedure[k].BeginAdd==-1)
733 procedure[k].BeginAdd=Address;
744 case 'F': /*Example L:Fadq$_str_2$0$0:57A*/
745 sscanf(buff, "%[^$] %c %[^$] %c %[^:] %c %x",
746 scope, &c, name, &c, level, &c, &Address);
748 for(j=0; j<numsym; j++)
750 if(EQ(symbol[j].name, name))
752 if( (symbol[j].Address==-1) ) symbol[j].Address=Address;
757 /*It could be also a static function*/
758 for(j=0; j<numproc; j++)
760 if(EQ(procedure[j].name, name))
762 if( (procedure[j].BeginAdd==-1) ) procedure[j].BeginAdd=Address;
769 case 'L': /*Example L:Lmain$j$1$1:29*/
772 L:LDS1306_Write$Value$1$1:34
773 L:LDS1306_Burst_Read$count$1$1:35
774 L:LDS1306_Burst_Read$address$1$1:36
775 L:LDS1306_Burst_Write$count$1$1:37
776 L:LDS1306_Burst_Write$address$1$1:38
778 sscanf(&buff[3], "%[^$] %c %[^$] %c %[^:] %c %x",
779 scope, &c, name, &c, level, &c, &Address);
781 for(k=0; k<numproc; k++)
783 if(EQ(procedure[k].name, scope)) break;
786 if(k<numproc) for(j=0; j<numsym; j++)
788 if( EQ(symbol[j].name, name) && (symbol[j].Procedure==k) )
790 if(symbol[j].Address==-1) symbol[j].Address=Address;
797 case 'C': /*Example L:C$adq.c$38$1$1:3E*/ /*L:C$hwinit.c$29$1$1:7AD*/
798 sscanf(&buff[4], "%[^.] %[^$] %c %d %[^:] %c %x",
799 name, level, &c, &CLine, level, &c, &Address);
801 for(j=0; j<numin; j++)
802 if(EQ(infn[j].ModuleName, name)) break;
805 /*Check if this line is already defined*/
806 for(k=0; k<numlinenum; k++)
808 if( (linenum[k].Number==CLine) &&
809 (linenum[k].FileNameNumber==j) )break;
811 if(k==numlinenum) /*New line number*/
813 linenum=realloc(linenum, sizeof(_linenum)*(numlinenum+1));
814 linenum[numlinenum].Number=CLine;
815 linenum[numlinenum].FileNameNumber=j;
816 linenum[numlinenum].Procedure=-1;/*To be asigned later*/
817 linenum[numlinenum].Address=Address;
823 case 'A': /*Example L:A$adq$424:40*/
824 /*No use for this one*/
827 /*The end of a procedure*/
828 case 'X': /*Example L:XG$AsciiToHex$0$0:88*/
829 sscanf(&buff[3], "%[^$] %c %[^$] %c %[^:] %c %x",
830 scope, &c, name, &c, level, &c, &Address);
832 for(k=0; k<numproc; k++)
834 if(EQ(procedure[k].name, name))
836 if(procedure[k].EndAdd==-1) procedure[k].EndAdd=Address;
849 /*Make sure each procedure has an end*/
850 for(k=0; k<(numproc-1); k++)
852 if (procedure[k].EndAdd==-1) procedure[k].EndAdd=procedure[k+1].BeginAdd-1;
854 /*Asign each line number to a procedure*/
855 for(j=0; j<numlinenum; j++)
857 for(k=0; k<numproc; k++)
859 if ( (linenum[j].Address>=procedure[k].BeginAdd) &&
860 (linenum[j].Address<=procedure[k].EndAdd) &&
861 (linenum[j].FileNameNumber==procedure[k].FileNameNumber) )
863 linenum[j].Procedure=k;
871 int hex2dec (char hex_digit)
874 j=toupper(hex_digit)-'0';
879 unsigned char GetByte(char * buffer)
881 return hex2dec(buffer[0])*0x10+hex2dec(buffer[1]);
884 unsigned short GetWord(char * buffer)
886 return hex2dec(buffer[0])*0x1000+
887 hex2dec(buffer[1])*0x100+
888 hex2dec(buffer[2])*0x10+
892 int ReadHexFile(int * Begin)
897 unsigned char linesize, recordtype, rchksum, value;
898 unsigned short address;
902 /*If the hexfile is already open, close it*/
909 strcpy(ihxFileName, infn[0].PathName);
910 strcat(ihxFileName, ".ihx");
912 if ( (filein=fopen(ihxFileName, "r")) == NULL )
914 printf("Error: Can't open file `%s`.\r\n", ihxFileName);
918 ihxBuff=calloc(MEMSIZE, sizeof(unsigned char));
921 printf("Insufficient memory\n");
926 for(j=0; j<MEMSIZE; j++) ihxBuff[j]=0xff;
930 if(fgets(buffer, sizeof(buffer), filein)==NULL)
932 printf("Error reading file '%s'\n", ihxFileName);
937 linesize = GetByte(&buffer[1]);
938 address = GetWord(&buffer[3]);
939 recordtype = GetByte(&buffer[7]);
940 rchksum = GetByte(&buffer[9]+(linesize*2));
941 chksum=linesize+(address/0x100)+(address%0x100)+recordtype+rchksum;
943 if (recordtype==1) break; /*End of record*/
945 for(j=0; j<linesize; j++)
947 value=GetByte(&buffer[9]+(j*2));
949 ihxBuff[address+j]=value;
951 if(MaxAddress<(address+linesize-1)) MaxAddress=(address+linesize-1);
952 if(address<*Begin) *Begin=address;
954 if((chksum%0x100)!=0)
956 printf("ERROR: Bad checksum in file %s\n", ihxFileName);
967 void CreateAOMF51(void)
969 if((dflag) && (!rflag))
971 CollectInfoFromCDB();
975 HexSize=ReadHexFile(&HexBegin)+1;