Subversion Repositories pentevo

Rev

Rev 167 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed | ?url?

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5. int emit_header(FILE * infile, int abits);
  6. int emit_body(FILE * infile, FILE * outfile, int inlen, int abits);
  7. int emit_footer(FILE * infile);
  8.  
  9.  
  10. int main( int argc, char* argv[] )
  11. {
  12.         int error=0;
  13.  
  14.         FILE * infile=NULL;
  15.         FILE * outfile=NULL;
  16.  
  17.         int inlen;
  18.         int abits;
  19.         int temp;
  20.  
  21.         if( argc!=3 )
  22.         {
  23.                 printf("usage: bin2v <infile> <outfile>\n\n");
  24.                 error++;
  25.         }
  26.  
  27.         if( !error )
  28.         {
  29.                 if( !(infile=fopen(argv[1],"rb")) )
  30.                 {
  31.                         printf("cant open infile!\n");
  32.                         error++;
  33.                 }
  34.         }
  35.  
  36.         if( !error )
  37.         {
  38.                 if( !(outfile=fopen(argv[2],"wt")) )
  39.                 {
  40.                         printf("cant open outfile!\n");
  41.                         error++;
  42.                 }
  43.         }
  44.  
  45.         if( !error )
  46.         {
  47.                 if( fseek(infile,0,SEEK_END) )
  48.                 {
  49.                         printf("Cannot fseek() infile!\n");
  50.                         error++;
  51.                 }
  52.         }
  53.  
  54.         if( !error )
  55.         {
  56.                 inlen=(int)ftell(infile);
  57.  
  58.                 if( inlen==(-1L) )
  59.                 {
  60.                         printf("Cannot ftell() length of infile!\n");
  61.                         inlen=0;
  62.                         error++;
  63.                 }
  64.         }
  65.  
  66.         if( !error )
  67.         {
  68.                 if( !inlen )
  69.                 {
  70.                         printf("Infile is zero length!\n");
  71.                         error++;
  72.                 }
  73.         }
  74.  
  75.         if( !error && fseek(infile,0,SEEK_SET) )
  76.         {
  77.                 printf("Cannot fseek() infile!\n");
  78.                 error++;
  79.         }
  80.  
  81.  
  82.         if( !error )
  83.         {
  84.                 // how many address bits to use
  85.                 temp=inlen-1;
  86.                 abits=1;
  87.                 while( temp>>=1 ) abits++;
  88.         }
  89.  
  90.  
  91.         if( !error ) error+=emit_header(outfile, abits);
  92.  
  93.         if( !error ) error+=emit_body(infile, outfile, inlen, abits);
  94.  
  95.         if( !error ) error+=emit_footer(outfile);
  96.  
  97.  
  98.  
  99.  
  100.  
  101.         if( outfile ) fclose(outfile);
  102.         if( infile  ) fclose(infile);
  103.  
  104.         return error;
  105. }
  106.  
  107. int emit_body(FILE * infile, FILE * outfile, int len, int bits)
  108. {
  109.         int i;
  110.         unsigned char b;
  111.  
  112.         for(i=0;i<len;i++)
  113.         {
  114.                 if( 1==fread(&b,1,1,infile) )
  115.                 {
  116.                         if( b!=0xFF )
  117.                                 fprintf(outfile, "\t\t%d'h%X: out_word = 8'h%02X;\n", bits,i,(int)b);
  118.                 }
  119.                 else // error reading byte
  120.                 {
  121.                         return 1;
  122.                 }
  123.         }
  124.  
  125.         return 0;
  126. }
  127.  
  128.  
  129. int emit_header(FILE * file, int abits)
  130. {
  131.         fprintf(file,"// bin2v output\n");
  132.         fprintf(file,"// \n\n");
  133.  
  134.         fprintf(file,"module bin2v(\n\n");
  135.  
  136.         fprintf(file,"\tinput  wire [%2d:0] in_addr,\n\n",abits-1);
  137.  
  138.         fprintf(file,"\toutput reg  [ 7:0] out_word\n\n");
  139.  
  140.         fprintf(file,");\n\n");
  141.  
  142.         fprintf(file,"\talways @*\n");
  143.  
  144.         fprintf(file,"\tcase( in_addr )\n\n");
  145.  
  146.         return 0;
  147. }
  148.  
  149. int emit_footer(FILE * file)
  150. {
  151.         fprintf(file,"\n\t\tdefault: out_word = 8'hFF;\n\n");
  152.  
  153.         fprintf(file,"\tendcase\n\n");
  154.  
  155.         fprintf(file,"endmodule\n");
  156.  
  157.         return 0;
  158. }
  159.  
  160.  
  161.