 /**[txh]********************************************************************
                                                                                
  Copyright (c) 2005 Juan Pablo D. Borgna <jpborgna en inti gov ar>
  Copyright (c) 2005 Instituto Nacional de Tecnología Industrial                
                                                                                
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; version 2.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 02111-1307, USA

  Description: Dumps the contents of a xilinx bit file in stdout.
                                                                                
***************************************************************************/    
/*****************************************************************************  
                                                                                
 Target:      Any                                                               
 Language:    C
 Compiler:    gcc 3.3.5 (Debian GNU/Linux)
 Text editor: SETEdit 0.5.5                                                     
                                                                                
*****************************************************************************/  

#include <stdio.h>
#include <stdlib.h>
#include "bitfile.h"

#define BSIZE 1048576  /* 1k */


/* read a bit file from stdin */
int main(int argc, char *argv[])
{
 struct bithead bh;
 FILE *bitfile, *outfile;
 int remaining,readed;
 char buff[BSIZE];
 
 fprintf(stderr,"\ndumpbit - bit file stream dumper - v1.0\n");
 fprintf(stderr,"Copyright (c) 2005 Juan Pablo D. Borgna/INTI\n\n");

 if (argc==1 || argc>3)
   {
    fprintf(stderr,"Insufficient args %s filename.bit filename\n",argv[0]);
    return 1;
   }

 if ((bitfile=fopen(argv[1],"rb"))==NULL)
   {
    perror("BITFILE");
    return 2;
   }

 if ((outfile=fopen(argv[2],"wb"))==NULL)
   {
    perror("OUTFILE");
    return 3;
   }

 initbh(&bh);
 if (readhead(&bh, bitfile))
   {
    fprintf(stderr,"Invalid bit file header.\n");
    return 3;
   }

 fprintf(stderr,"\n");
 fprintf(stderr,"Bit file created on %s at %s.\n", bh.date, bh.time);
 fprintf(stderr,"Created from file %s for Xilinx part %s.\n", bh.filename, bh.part);
 fprintf(stderr,"Bitstream length is %d bytes.\n", bh.length);
 fprintf(stderr,"\n");

 remaining=bh.length;

 while(remaining)
   {
    readed=fread(buff,1,BSIZE,bitfile);
    fwrite(buff,readed,1,outfile);
    remaining-=readed;
   }

 fclose(bitfile);
 freebh(&bh);
 return 0;
}
