forked from matrix-io/xc3sprog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapfile_xc2c.cpp
218 lines (204 loc) · 4.42 KB
/
mapfile_xc2c.cpp
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
/*
* Warp Coolrunner II Jedecfile to Bitfiles suitable for programming
* and vice- versa
*
* Needs access to the Xilinx supplied .map files for transformation
*
* Copyright Uwe Bonnes 2009 [email protected]
*
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include "mapfile_xc2c.h"
#ifndef MAPDIR
#if defined (__linux__) || defined(__FreeBSD__) || defined(__MACH__)
#define MAPDIR "/opt/Xilinx/12.4/ISE_DS/ISE/xbr/data"
#elif defined(__WIN32__)
#define MAPDIR "c:"
#endif
#endif
MapFile_XC2C::MapFile_XC2C()
{
map = 0;
}
MapFile_XC2C::~MapFile_XC2C()
{
if (map)
free(map);
}
int MapFile_XC2C::readmap(FILE *fp)
{
int i=0, j=0;
int num = -1;
int x;
/* Lines with all TABs mark transfer bits and those bits should be set 0
other empty places should be set 1 */
int transferline = 1;
while (( x =fgetc(fp))!= EOF)
{
switch (x)
{
case 0x09:
case '\n':
if (num == -2)
num = -1;
if ((num == -1) && (transferline == 0))
{
num = -2;
}
map[i*block_num+j]=num;
num = -1;
j++;
if(x == '\n')
{
j = 0;
i++;
transferline = 1;
}
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if (num == -1)
num = x-'0';
else if (num >=0)
num = 10 * num + (x-'0');
transferline = 0;
break;
case '\r':
break;
case 'a' ... 'z':
transferline = 0;
num = -2;
break;
}
}
return 0;
}
int MapFile_XC2C::loadmapfile(const char *mapdir, const char *device)
{
FILE *fp;
const char * mapfile;
if (strncasecmp(device, "XC2C32", 6) == 0)
{
block_size = 260;
block_num = 48;
if (strncasecmp(device, "XC2C32A", 7) == 0)
mapfile = "xc2c32a";
else
mapfile = "xc2c32";
}
else if (strncasecmp(device, "XC2C64", 6) == 0)
{
block_size = 274;
block_num = 96;
if (strncasecmp(device, "XC2C64A", 7) == 0)
mapfile = "xc2c64a";
else
mapfile = "xc2c64";
}
else if (strncasecmp(device, "XC2C128", 7) == 0)
{
block_size = 752;
block_num = 80;
mapfile = "xc2c128";
}
else if (strncasecmp(device, "XC2C256", 7) == 0)
{
block_size = 1364;
block_num = 96;
mapfile = "xc2c256";
}
else if (strncasecmp(device, "XC2C384", 7) == 0)
{
block_size = 1868;
block_num = 120;
mapfile = "xc2c384";
}
else if (strncasecmp(device, "XC2C512", 7) == 0)
{
block_size = 1980;
block_num = 160;
mapfile = "xc2c1512";
}
if (!mapdir)
if(!(mapdir = getenv("XC_MAPDIR")))
mapdir = MAPDIR;
mapfilename = (char *) malloc(strlen(mapdir)+strlen(mapfile)+6);
if (mapfilename)
{
strcpy(mapfilename, mapdir);
strcat(mapfilename, "/");
strcat(mapfilename, mapfile);
strcat(mapfilename, ".map");
}
fp = fopen(mapfilename, "rb");
free(mapfilename);
if (fp == NULL)
{
fprintf(stderr,"Mapfile %s/%s.map not found: %s\n", mapdir, mapfile,
strerror(errno));
return 1;
}
if(map)
free (map);
/* there are twoo extra rows for security/done and usercode bits*/
map = (int *) malloc(block_size * (block_num + 2) * sizeof(unsigned int));
if (map == NULL)
return 2;
memset(map, 0, block_size * (block_num + 2)* sizeof(unsigned int));
readmap(fp);
fclose(fp);
return 0;
}
void MapFile_XC2C::jedecfile2bitfile(JedecFile *fuses, BitFile *bits)
{
int i, j;
bits->setLength(block_size*block_num);
for (i=0; i<block_num; i++)
{
for (j=0; j<block_size; j++)
{
int fuse_idx = map[j*block_num +i];
int fuse = (fuse_idx == -1)? 0:1;
int bitnum = (i+1)*block_size -j -1;
if (fuse_idx >=0 &&
fuse_idx < (int)fuses->getLength()
/* xc2c32a.map from 10.1 contain 0 .. 12278 versus 0..12277 */)
fuse = fuses->get_fuse(fuse_idx);
bits->set_bit(bitnum, fuse);
}
}
}
void MapFile_XC2C::bitfile2jedecfile( BitFile *bits, JedecFile *fuses)
{
int i, j;
fuses->setLength(block_size*block_num);
int maxnum =0;
for (i=0; i<block_num; i++)
{
for (j=0; j<block_size; j++)
{
int bitnum = (i+1)*block_size -j -1;
int bit = 1;
int fuse_idx= map[j*block_num +i];
bit = bits->get_bit(bitnum);
if (fuse_idx>= 0)
{
if (fuse_idx > maxnum)
maxnum = fuse_idx;
fuses->set_fuse(fuse_idx, bit);
}
}
}
fuses->setLength(maxnum+1);
}