summaryrefslogtreecommitdiffstats
path: root/fpga/xilinx/programmer/bit2svf/bit2svf.c
blob: 9c42f652a288c584bea73448faa37542a972362e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
 /**[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;
}