 /**[txh]********************************************************************
                                                                                
  Copyright (c) 2005 Juan Pablo D. Borgna <jpborgna en inti gov ar>
  Copyright (c) 2006-2007 Salvador E. Tropea <salvador en inti gov ar>
  Copyright (c) 2005-2007 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: Generates a SVF file from a xilinx .bit file.
                                                                                
***************************************************************************/    
/*****************************************************************************  
                                                                                
 Target:      Any                                                               
 Language:    C
 Compiler:    gcc 3.3.5 (Debian GNU/Linux)
 Text editor: SETEdit 0.5.5                                                     
                                                                                
*****************************************************************************/  

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <getopt.h>
#include "bitfile.h"
#include "parts.h"
#include "commands.h"
#include "global.h"
#include "bitshandle.h"

#define DEFAULT_RES "/usr/share/bit2svf/"

static char *theBitFile;
static char *theSVFFile;
static char *thePath=NULL;
static char *theDevice;
static long skip=-1, limit=-1;

static
struct option longopts[] =
{
  { "help",           0, 0, 'h' },
  { "length",         1, 0, 'l' },
  { "path",           1, 0, 'p' },
  { "skip",           1, 0, 's' },
  { 0, 0, 0, 0 }
};

static
void Usage()
{
 #define PrintHelp(a) printf(a)
 #define FlushHelp()  printf("\n"); fflush(stdout)
 PrintHelp("bit2svf [options] infile.bit outfile.svf DEVICE_NAME\n\n");
 PrintHelp("Valid options are:\n");
 PrintHelp("-h, --help               displays this text ;-).\n");
 PrintHelp("-l, --length=LIMIT       limit the number of bits.\n");
 PrintHelp("-p, --path=PATH          is the full path to the directory with DEVICES file\n");
 PrintHelp("                         and the templates files to be used.\n");
 PrintHelp("-s, --skip=OFFSET        skip OFFSET bits in the bitstream.\n");
 FlushHelp();
 exit(1);
}

static
void ParseCommandLine(int argc, char *argv[])
{
 int optc;

 while ((optc=getopt_long(argc,argv,"hl:p:s:",longopts,0))!=EOF)
   {
    switch (optc)
      {
       case 'l':
            limit=strtol(optarg,NULL,0);
            if (limit%8)
              {
               fprintf(stderr,"The length must be multiple of 8\n");
               exit(2);
              }
            limit/=8;
            break;

       case 'p':
            thePath=optarg;
            break;

       case 's':
            skip=strtol(optarg,NULL,0);
            if (skip%8)
              {
               fprintf(stderr,"The skip must be multiple of 8\n");
               exit(2);
              }
            skip/=8;
            break;

       case 'h':
       default:
            Usage();
      }
   }
 if (argc-optind<3)
   {
    fprintf(stderr,"Error: Missing arguments\n\n");
    Usage();
   }
 theBitFile=argv[optind];
 theSVFFile=argv[optind+1];
 theDevice=argv[optind+2];
}


/* main function */

int main(int argc, char *argv[])
{
 struct bithead bh;
 FILE *bitfile;
 FILE *cfile;
 FILE *tplate;
 char line[1001];
 part *selected=NULL;
 char *res_path=NULL;
 char *devices=NULL;
 int isBit;

 address=0l;
 step=0l;
 s_bytes=0l;
 s_bits=0l;
 stream_s=0l;
 cutlines=1;

 id=NULL;
 idmask=NULL;
 bsize=NULL;
 msize=0l;
 templ=NULL;

 fprintf(stderr,"\nbit2svf - SVF file generator - v1.3.1\n");
 fprintf(stderr,"Copyright (c) 2005 Juan Pablo D. Borgna/INTI\n");
 fprintf(stderr,"Copyright (c) 2006-2007 Salvador E. Tropea/INTI\n\n");

 ParseCommandLine(argc,argv);

 if ((bitfile=fopen(theBitFile,"rb"))==NULL)
   {
    perror("BITFILE");
    return 2;
   }

 if (!strcmp(theSVFFile,"-"))
    cfile=stdout;
 else
   {
    if ((cfile=fopen(theSVFFile,"wt"))==NULL)
      {
       perror("SVFFILE");
       return 3;
      }
   }


 if (thePath)
    res_path=thePath;
 else /* If is not a command line parameter */
    if (((res_path=getenv("BIT2SVF_RES"))==NULL) || (strlen(res_path)==0))
       res_path=strdup(DEFAULT_RES); /* if couldnt get it from env, default*/

 //asprintf(&devices,"%s/DEVICES",res_path);
 devices=(char *)malloc(strlen(res_path)+9);
 strcpy(devices,res_path);
 strcat(devices,"/");
 strcat(devices,"DEVICES");

 if (select_part_from_file(theDevice,devices,&selected)) return 12;

 fprintf(stderr,"Using DEVICES: %s\n",devices);

 id=strdup(selected->id);
 idmask=strdup(selected->idmask);
 msize=selected->msize;
 bsize=selected->bsize;

 //asprintf(&templ,"%s/%s",res_path,selected->alg_tpl);
 templ=(char *)malloc(strlen(res_path)+strlen(selected->alg_tpl)+6);
 strcpy(templ,res_path);
 strcat(templ,"/");
 strcat(templ,selected->alg_tpl);
 strcat(templ,".svft");

 if ((tplate=fopen(templ,"rt"))==NULL)
   {
    perror("TEMPLATE");
    return 4;
   }
 fprintf(stderr,"Using template: %s\n",templ);

 isBit=1;
 initbh(&bh);
 if (readhead(&bh, bitfile))
   {
    fprintf(stderr,"\nInvalid .bit file assuming .bin.\n");
    isBit=0;
    fseek(bitfile,0,SEEK_END);
    bh.length=ftell(bitfile);
    fseek(bitfile,0,SEEK_SET);
   }
 else
   {
    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);
 if (skip>=0)
   {
    if (bh.length<skip)
      {
       fprintf(stderr,"The number of bytes to skip is bigger than the current length.\n");
       return 13;
      }
    fseek(bitfile,skip,SEEK_CUR);
    bh.length-=skip;
    fprintf(stderr,"Skipping %ld bytes, new length %d.\n",skip,bh.length);
   }
 if (limit>=0)
   {
    if (bh.length<limit)
      {
       fprintf(stderr,"The specified length is bigger than the current length.\n");
       return 12;
      }
    bh.length=limit;
    fprintf(stderr,"Limiting bitstream length to %d bytes.\n",bh.length);
   }
 fprintf(stderr,"\n");

 stream_s=ftell(bitfile);
 s_bytes=bh.length;
 s_bits=8*s_bytes;

 fgets(line,1001,tplate);

 while(!feof(tplate))
  {

 if (!strncasecmp(line,"--",2))
   { /* block found */
    /* LITERAL */
    if (!strncasecmp(line,"--LITERAL START",15))
       if(repeat(tplate,cfile,bitfile,0)) return 6;
    /* end LITERAL */
    /* REPEAT */
    if (!strncasecmp(line,"--REPEAT START",14))
       if (repeat(tplate,cfile,bitfile,-1)) return 7;
    /* end REPEAT */
    /* REPEAT UNTIL */
    if (!strncasecmp(line,"--REPEAT UNTIL",14))
      if (repeat(tplate,cfile,bitfile,value_from(line+15))) return 8;
    /* end REPEAT UNTIL */
   }
 else if (!strncasecmp(line,"$",1))
        {
         int col=0;
         if(do_command(line,bitfile,cfile,&col)) return 9;
        }
 else if (strncasecmp(line,"//",2) && (line[0]!='\n')) /* And if its not a comment or a blank line */
        { /* Its a template error */
         fprintf(stderr,"Invalid line outside block in template: %s\n",line);
         return 10;
        }

   fgets(line,1001,tplate);
  }

 fclose(tplate);
 fclose(cfile);
 fclose(bitfile);
 freebh(&bh);

 fprintf(stderr,"Process finished successfully.\n");
 return 0;
}
