#include #include #define BIT_HEAD_LENGTH 0x0009 #define ONE_HEAD_LENGTH 0x0001 /* LITTLE-ENDIAN of 0xaa995566 */ #define DATA_SYNC 0x665599aa typedef unsigned long kword; typedef unsigned short hword; typedef unsigned char kbyte; typedef struct _bitsdata { kword sizeup; hword length; kbyte *pdata; } bitsdata; hword swapbyte(hword test) { hword temp = test & 0xff; test >>= 8; temp <<= 8; temp |= test; return temp; } kword swapword(kword test) { kword temp = test & 0xff; test >>= 8; temp <<= 8; temp |= test & 0xff; test >>= 8; temp <<= 8; temp |= test & 0xff; test >>= 8; temp <<= 8; temp |= test & 0xff; return temp; } int getalpha(FILE* pfile, char test) { char tdata; void *ptest = (void*) &tdata; fread(ptest,1,sizeof(char),pfile); if(tdata==test) return 1; else return 0; } int getlength(FILE* pfile, hword* pdata) { int retval; void *ptest = (void*) pdata; retval = fread(ptest,1,sizeof(hword),pfile); *pdata = swapbyte(*pdata); return retval; } int getsizeup(FILE* pfile, kword* pdata) { int retval; void *ptest = (void*) pdata; retval = fread(ptest,1,sizeof(kword),pfile); *pdata = swapword(*pdata); return retval; } int createdata(FILE* pfile, bitsdata* pbits) { kbyte* ptemp = 0x0; kword sizeup = 0x0; hword length = 0x0; void* ptest; int result; /* get data length */ if(pbits->sizeup) { getsizeup(pfile,&sizeup); length = sizeup; } else { getlength(pfile,&length); sizeup = length; } /* create storage */ ptemp = malloc(sizeup); if(!ptemp) return -1; /* setup structure */ pbits->pdata = ptemp; pbits->length = length; pbits->sizeup = sizeup; /* read in data, return read count */ ptest = (void*) pbits->pdata; result = fread(ptest,1,sizeup,pfile); if(result!=(int)sizeup) { printf("ERROR? Result: %d [%08x], Sizeup: %d [%08x]\n", result,result,sizeup,sizeup); } return result; } int deletedata(bitsdata* pbits) { if(pbits->pdata) free(pbits->pdata); pbits->pdata = 0x0; pbits->length = 0; pbits->sizeup = 0; return 0; } int main(int argc, char *argv[]) { FILE *pfile; int count; bitsdata xdata = { 0,0,0x0 }; kword* datasync; kbyte headmask = 0x0f; char protocol = 'a'; if(argc<2) { printf("%s .bit\n",argv[0]); return -1; } pfile = fopen(argv[1],"rb"); if(!pfile) { printf("Cannot open Xilinx bitstream file %s!\n",argv[1]); return -2; } /* get file header */ if(createdata(pfile,&xdata)<0) { printf("error creating file header!\n"); fclose(pfile); return -1; } /* check file header length */ if(xdata.length!=BIT_HEAD_LENGTH) { printf("invalid %04x format? [%04x]\n",BIT_HEAD_LENGTH,xdata.length); deletedata(&xdata); fclose(pfile); return -1; } /* check file header */ for(count=0;count not in pattern? */ getlength(pfile,&xdata.length); if(xdata.length!=ONE_HEAD_LENGTH) { printf("invalid %04x format? [%04x]\n", ONE_HEAD_LENGTH,xdata.length); fclose(pfile); return -1; } /* check protocol char */ if(!getalpha(pfile,protocol)) { printf("protocol char [%c] not found!\n", protocol); fclose(pfile); return -1; } /* check design name */ if(createdata(pfile,&xdata)<0) { printf("error getting design name!\n"); fclose(pfile); return -1; } /* validate data */ if(xdata.pdata[xdata.length-1]!=0x00) { printf("invalid data terminator? [%02x]\n", xdata.pdata[xdata.length-1]); deletedata(&xdata); fclose(pfile); return -1; } /* print design name */ printf("Design name: %s\n",(char*)xdata.pdata); deletedata(&xdata); /* check next protocol char */ protocol++; if(!getalpha(pfile,protocol)) { printf("protocol char [%c] not found!\n", protocol); fclose(pfile); return -1; } /* check part name */ if(createdata(pfile,&xdata)<0) { printf("error getting part name!\n"); fclose(pfile); return -1; } /* validate data */ if(xdata.pdata[xdata.length-1]!=0x00) { printf("invalid data terminator? [%02x]\n", xdata.pdata[xdata.length-1]); deletedata(&xdata); fclose(pfile); return -1; } /* print part name */ printf("Part name: %s\n",(char*)xdata.pdata); deletedata(&xdata); /* check next protocol char */ protocol++; if(!getalpha(pfile,protocol)) { printf("protocol char [%c] not found!\n", protocol); fclose(pfile); return -1; } /* check string date */ if(createdata(pfile,&xdata)<0) { printf("error getting string date!\n"); fclose(pfile); return -1; } /* validate data */ if(xdata.pdata[xdata.length-1]!=0x00) { printf("invalid data terminator? [%02x]\n", xdata.pdata[xdata.length-1]); deletedata(&xdata); fclose(pfile); return -1; } /* print string date */ printf("Date created: %s\n",(char*)xdata.pdata); deletedata(&xdata); /* check next protocol char */ protocol++; if(!getalpha(pfile,protocol)) { printf("protocol char [%c] not found!\n", protocol); fclose(pfile); return -1; } /* check string time */ if(createdata(pfile,&xdata)<0) { printf("error getting string time!\n"); fclose(pfile); return -1; } /* validate data */ if(xdata.pdata[xdata.length-1]!=0x00) { printf("invalid data terminator? [%02x]\n", xdata.pdata[xdata.length-1]); deletedata(&xdata); fclose(pfile); return -1; } /* print string time */ printf("Time created: %s\n",(char*)xdata.pdata); deletedata(&xdata); /* check next protocol char */ protocol++; if(!getalpha(pfile,protocol)) { printf("protocol char [%c] not found!\n", protocol); fclose(pfile); return -1; } /* check raw bits */ xdata.sizeup = 1; if(createdata(pfile,&xdata)<0) { printf("error getting raw bits!\n"); fclose(pfile); return -1; } /* validate raw bit stream */ datasync = (kword*) xdata.pdata; printf("Dummy word => [%08x]\n", *datasync); datasync++; printf("DSYNC word => [%08x]\n", *datasync); if(*datasync!=DATA_SYNC) printf("invalid raw data sync? [%08x]\n",DATA_SYNC); /* print raw bits */ printf("Raw bitsize: %d bytes\n",xdata.sizeup); deletedata(&xdata); fclose(pfile); return 0; }