summaryrefslogtreecommitdiffstats
path: root/fpga/xilinx/programmer/bit2svf/bitdevice.c
blob: cad4b7e471cd064942f84288a8dec4df48cef690 (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
/**************************************************************************

  Copyright (c) 2012 Timothy Pearson <kb9vqf@pearsoncomputing.net>

 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 specified device in a xilinx bit file to stdout

***************************************************************************/
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include "bitfile.h"

#define BSIZE 1048576  /* 1k */

void convertToUpperCase(char *str)
{
	while(*str != '\0') {
		*str = toupper((unsigned char)*str);
		str++;
	}
}

void findAndRemoveEndString(char *str, char* text)
{
	char *location = strstr(str, text);
	int len = strlen(str);
	int sublen = strlen(text);
	if (location && (location == (str+(len-sublen)))) {
		*location = 0;
	}
}

void stripPackageSuffix(char *str)
{
	findAndRemoveEndString(str, "FG256");
	findAndRemoveEndString(str, "CSG324");
}

/* read a bit file from stdin */
int main(int argc, char *argv[])
{
	struct bithead bh;
	FILE *bitfile;

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

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

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

	convertToUpperCase(bh.part);
	stripPackageSuffix(bh.part);
	printf("XC%s\n", bh.part);

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