#include #include //for htonl- windows machines Winsock2.h works (in Ws2_32.lib). For a unix machine you can use arpa/inet.h #include //Note to users: /* Modelsim reads in binary files network endian, no matter what machine it is installed on. So if you are using an x86 (I'm looking at you windows users) you'll need to do network endian conversions when you set up a file to be read by modelsim. Modelsim doesn't really have a way to WRITE in binary, so you'll have to do the conversion yourself. fwrite should write in binary, but it doesn't with modelsim. It does the same thing as fdisplay, which is ascii. */ int _tmain(int argc, _TCHAR* argv[]) { int i = 0; int temp = 0; int counter = 0; int iter; int const MAX_LINE = 15; char wordbuf[MAX_LINE]; FILE *f ; FILE *fout; f = NULL; fout= NULL; int create_increment = 0; int convert_to_addressed_ascii = 0; int convert_to_readmem = 0; int readmem_size = 32; int convert_to_binary = 1; // sometimes convenient to view the file in a non-hex viewer with the addresses written in ascii // convenient for doing windiff against two separate hex files. I don't know how to diff two hex files in a readable way should // not EVERY output address be written to. IE., the files may be exactly the same except that they are written to different address // offsets. if(convert_to_addressed_ascii){ f = NULL; fout= NULL; f = fopen("input_base0.bin","rb"); fout = fopen("output_waddress.txt","wb"); iter = 0; if (f != NULL && fout != NULL){ do{ i = fread(&temp,sizeof(temp),1,f); if (i > 0){ fprintf(fout,"%06x\t%x\n",iter,temp); iter = iter + 4; } }while (i > 0); fclose(f); fclose(fout); } } // The output of modelsim is ALWAYS text. it can be easier to compare binary files (such as images) as binary, not text if(convert_to_binary){ f = NULL; fout= NULL; f = fopen("output_fdisplay.txt","rb"); fout = fopen("output_fdisplay.bin","wb"); iter = 0; if (f != NULL && fout != NULL){ while (fgets(wordbuf, MAX_LINE, f) != NULL){ i = sscanf(wordbuf, "%x", &temp); temp = htonl(temp); //to easily compare against the file formatting that was fed INTO modelsim fwrite(&temp,sizeof(temp),1,fout); } fclose(f); fclose(fout); } } //readmem has particular input constraints, text based. //readmemb takes in binary, but sometimes you'd rather use readmemh //this example assumes readmemh is filling a 32 bit word buffer or an 8 bit word buffer. //note that the values read out of the input.bin are inherently 32 bit words. //verilog example input buffers //reg [31 : 0] membuf32 [ 0 : 4095 ]; // 4K input file //reg [7 : 0] membuf8 [ 0 : 4095 ]; // 1K input file if(convert_to_readmem){ f = NULL; fout= NULL; f = fopen("input_base0.bin","rb"); if (readmem_size == 32){ fout = fopen("input32.mem","wb"); } else if (readmem_size == 8){ fout = fopen("input8.mem","wb"); } iter = 0; if (f != NULL && fout != NULL){ do{ i = fread(&temp,sizeof(temp),1,f); if(i > 0){ if(readmem_size == 8){ fprintf(fout,"%02x\n",(temp & 0xFF)); fprintf(fout,"%02x\n",((temp & 0xFF00)>>8)); fprintf(fout,"%02x\n",((temp & 0xFF0000)>>16)); fprintf(fout,"%02x\n",((temp & 0xFF000000)>>24)); } else if (readmem_size == 32){ fprintf(fout,"%08x\n",htonl(temp)); } } }while (i > 0); fclose(fout); fclose(f); } } if(create_increment){ f = NULL; f = fopen("input_base0.bin","wb"); if (f != NULL){ for (iter = 0; iter < 1024; iter++){ temp = htonl(iter); fwrite(&temp,sizeof(temp),1,f); } fprintf(f,"\n"); fclose(f); } } return 0; }