/* ** fp880709/010413 convert binary file to sxf, fpc, dmp & hex */ #include #include #include #include #define S1F 1 #define S2F 2 #define S3F 3 #define FPC 4 #define DMP 5 #define HEX 6 /* fpc type defs */ #define ABS 0 #define DAT 1 #define REL 2 #define HDR 3 #define REP 4 FILE *ifp; FILE *ofp; unsigned char buf[33]; unsigned long gethex(char *p, int n) { unsigned long h = 0; int i = 0; char c; while((n==0)||(i>4)+(unsigned char)(adr>>12); fprintf(ofp, ":020000%02X%04X%02X\n", typ, (unsigned int)(adr>>4), (-chk)&255); return(0); case 4 : case 5 : chk=2+typ+(unsigned char)(adr>>16)+(unsigned char)(adr>>24); fprintf(ofp, ":020000%02X%04X%02X\n", typ, (unsigned int)(adr>>16), (-chk)&255); return(0); } return(len); } /* ** output long in FPC to outfile ** dig must be 5 */ void putfpc(unsigned long fpc, unsigned int dig) { if(dig>1) putfpc(fpc/85L, dig-1); dig=fpc%85L; fputc(dig<5 ? dig+'%' : dig+'%'+1, ofp); } /* ** output FPC string */ unsigned char outfpc(unsigned short typ, unsigned char len, unsigned long adr, unsigned char *ptr) { unsigned char chk; /* check byte */ unsigned long hed; /* fpc string header */ unsigned long val; /* 4 bytes in a long */ int i, j; /* counters */ fprintf(ofp, "$"); /* fpc string intro */ hed = (unsigned long)typ; /* some common things */ chk =-(unsigned char)typ-(unsigned char)(typ>>8); switch(typ) { case ABS : /* abs address & data */ case REL : /* rel address & data */ if(len>251) { putfpc(0x0200ffffL, 5); /* error indicator */ break; } chk-= (len+4); chk-= (unsigned char)adr; chk-= (unsigned char)(adr>>8); chk-= (unsigned char)(adr>>16); chk-= (unsigned char)(adr>>24); for(i=0; i>24)&255L); chk+=c; fprintf(ofp, "%02X", c&255); case 2 : c=(unsigned char)((adr>>16)&255L); chk+=c; fprintf(ofp, "%02X", c&255); case 1 : c=(unsigned char)((adr>>8)&255L); chk+=c; fprintf(ofp, "%02X", c&255); c=(unsigned char)(adr&255L); chk+=c; fprintf(ofp, "%02X", c&255); break; case 0 : fprintf(ofp, "0000"); break; } for(i=0; i32) quit(8, ""); strcpy(buf, argv[next]); next++; continue; } if(strcmp("-0", argv[next])==0) { next++; fpce=0; continue; } quit(10, argv[next]); } if((address+fsize)65536L) quit(6, ""); case S2F : if((fsize+address)>16777216L) quit(6, ""); case S3F : if((n=(unsigned char)strlen(buf))!=0) outhex(0, n, 0L, buf); break; case FPC : if((n=(unsigned char)strlen(buf))!=0) { if(fpce==0) quit(12, ""); outfpc(HDR, n, 0L, buf); } break; case DMP : if(buf[0]!='\0') { if(isxdigit(buf[0])) quit(11, ""); fprintf(ofp, "%s\n", buf); } size=16; break; case HEX : if((n=(unsigned char)strlen(buf))!=0) quit(9, buf); ihexlast=address; if((fsize+address)>65536L) outist(4, 2, address, buf); break; default : break; } if(buf[0]!='\0') fprintf(stdout, "header : %s\n", buf); fprintf(stdout, "length : %8lX\n", fsize); fprintf(stdout, "offset : %8lX\n", address); lincnt=0L; sum=0L; while((n=(unsigned char)fread(buf, 1, size, ifp))!=0) { for(i=0; i<(int)n; i++) sum+=(unsigned long)buf[i]; switch(m) { case S1F : case S2F : case S3F : address+=(unsigned long)outhex(m, n, address, buf); break; case FPC : if(fpce==0) address+=(unsigned long)outfpc(ABS, n, address, buf); else { if((lincnt%16)==0L) outfpc(ABS, 0, address, buf); for(i=1; i>16)!=(ihexlast>>16)) { ihexlast=address; outist(4, 2, address, buf); } address+=(unsigned long)outist(0, n, address, buf); break; default : break; } } switch(m) { case S1F : case S2F : case S3F : outhex(m, 0, 0L, buf); break; case FPC : fprintf(ofp, "$%%%%%%%%%%\n"); /* end string */ break; case HEX : outist(1, 0, 0L, buf); break; default : break; } fclose (ifp); fclose (ofp); fprintf(stdout, "chksum : %8lX\n", sum); quit(0, ""); }