forked from trou/debit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitstream_v4.c
448 lines (393 loc) · 12 KB
/
bitstream_v4.c
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*
* Copyright (C) 2006, 2007 Jean-Baptiste Note <jean-baptiste.note@m4x.org>
*
* This file is part of debit.
*
* Debit 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, either version 3 of the License, or
* (at your option) any later version.
*
* Debit 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 debit. If not, see <http://www.gnu.org/licenses/>.
*/
#include <glib.h>
#include <glib/gprintf.h>
#include "bitstream_parser.h"
#include "sites.h"
#include "bitstream.h"
#include "design.h"
#include "cfgbit.h"
/** \file
*
* This file is the interface to use to access the data in the
* bitstream. Some of the facilities programmed here should be
* abstracted from the bitstream type (currently xc2v2000) with an
* appropriate database. What form this database should take remains to
* be seen.
*
* There are two kinds of functions here. Untyped query functions, and
* typed query functions. Typed query functions are typically wrappers
* to untyped query functions. Typed query functions are also present in
* localpips.c
*
* We should probably go from a bit query to a byte query (of up to 4
* bytes, or one guint32).
*/
#if defined(VIRTEX4)
#define STDWIDTH 10
const type_bits_t type_bits[NR_SITE_TYPE] = {
[SITE_TYPE_NEUTRAL] = {
.y_width = STDWIDTH,
},
[IOB] = {
.col_type = V4C_IOB,
.y_width = STDWIDTH,
},
[CLB] = {
.col_type = V4C_CLB,
.y_width = STDWIDTH,
},
[DSP48] = {
.col_type = V4C_DSP48,
.y_width = STDWIDTH,
},
[GCLKC] = {
.col_type = V4C_GCLK,
.y_width = STDWIDTH,
},
[BRAM] = {
.col_type = V4C_BRAM_INT,
.y_width = STDWIDTH,
},
};
#elif defined(VIRTEX5)
#define STDWIDTH 8
const type_bits_t type_bits[NR_SITE_TYPE] = {
[SITE_TYPE_NEUTRAL] = {
.y_width = STDWIDTH,
},
[IOB] = {
.col_type = V5C_IOB,
.y_width = STDWIDTH,
},
[CLB] = {
.col_type = V5C_CLB,
.y_width = STDWIDTH,
},
[DSP48] = {
.col_type = V5C_DSP48,
.y_width = STDWIDTH,
},
[GCLKC] = {
.col_type = V5C_GCLK,
.y_width = STDWIDTH,
},
[BRAM] = {
.col_type = V5C_BRAM_INT,
.y_width = STDWIDTH,
},
};
#endif
/*
* Untyped query functions
*/
#if defined (VIRTEX4)
/*
* Helper function
*/
static unsigned char
mirror_byte(const unsigned char dat) {
unsigned char res = 0;
int i;
for (i = 0; i < 8; i++)
if (dat & (1 << i))
res |= (1 << (7-i));
return res;
}
/* static unsigned char */
/* mirror_byte(const unsigned char dat) { */
/* return ((dat * 0x0802LU & 0x22110LU) | (dat * 0x8020LU & 0x88440LU)) * 0x10101LU >> 16; */
/* } */
/* static unsigned char */
/* mirror_byte(const unsigned char dat) { */
/* unsigned result = (dat >> 4) | (dat << 4); */
/* result = ((result & 0xcc) >> 2 ) | ((result & 0x33) << 2); */
/* result = ((result & 0xaa) >> 1 ) | ((result & 0x55) << 1); */
/* return result; */
/* } */
/** \brief Get one config byte from a site
*
* @param bitstream the bitstream data
* @param site the site queried
* @param cfgbyte the bit asked for
*
* @return the configuration byte asked for
*/
static guchar
query_bitstream_site_byte(const bitstream_parsed_t *bitstream,
const csite_descr_t *site,
const int cfgbyte) {
const chip_struct_t *chip_struct = bitstream->chip_struct;
const site_type_t lsite_type = site->type;
const unsigned x = site->type_coord.x;
const unsigned y = site->type_coord.y;
const unsigned ymid = chip_struct->row_count;
/* Rows are packed by groups of 16 */
unsigned row = (y >> 4);
/* a bittest should be sufficient for top, but is hard to compute
(depends on bitlength of the value, which is costly to compute) */
const unsigned top = (row >= ymid) ? 0 : 1;
const unsigned row_local = y & 0xF;
/* We must skip 4 bytes of SECDED and CLK information in the middle of
* the frame */
const unsigned row_second_half = (y >> 1) & 0x4;
const guint frame_x = byte_x(cfgbyte);
/* Middle word contains SECDED and clk information, so we skip it sometimes */
guint frame_y = row_local * STDWIDTH + row_second_half + byte_y(cfgbyte);
const gchar *frame;
unsigned char byte;
/* When top is one, the row numbering is inverted, and bits are mirrored */
row = top ? (ymid - 1 - row) : row - ymid;
frame = get_frame(bitstream, type_bits[lsite_type].col_type, row, top, x, frame_x);
frame_y = top ? (164 - 1 - frame_y) : frame_y;
/* The adressing here is a bit strange, due to the frame byte order */
byte = frame[frame_y ^ 0x3];
return top ? byte : mirror_byte(byte);
}
#elif defined(VIRTEX5)
static guchar
query_bitstream_site_byte(const bitstream_parsed_t *bitstream,
const csite_descr_t *site,
const int cfgbyte) {
const chip_struct_t *chip_struct = bitstream->chip_struct;
const site_type_t lsite_type = site->type;
const unsigned x = site->type_coord.x;
const unsigned y = site->type_coord.y;
const unsigned ymid = chip_struct->row_count;
/* Rows are packed by groups of 20 */
unsigned row = (y / 20);
/* a bittest should be sufficient for top, but is hard to compute
(depends on bitlength of the value, which is costly to compute) */
const unsigned top = (row >= ymid) ? 0 : 1;
const unsigned row_local = y % 20;
const unsigned row_second_half = (row_local >= 10) ? 4 : 0;
const guint frame_x = byte_x(cfgbyte);
/* Middle word contains SECDED and clk information, so we skip it sometimes */
guint frame_y = row_local * STDWIDTH + row_second_half + byte_y(cfgbyte);
const gchar *frame;
/* When top is one, the row numbering is inverted */
row = top ? (ymid - 1 - row) : row - ymid;
frame = get_frame(bitstream, type_bits[lsite_type].col_type, row, top, x, frame_x);
/* The adressing here is a bit strange, due to the frame byte order */
return frame[frame_y ^ 0x3];
}
#endif
/** \brief Get some (up to 4) config bytes from a site
*
* @param bitstream the bitstream data
* @param site the site queried
* @param cfgbits the array of bytes asked for
* @param nbytes the number of bytes asked for
*
* @return the configuration bytes asked for, packed into a guint32
* @see query_bitstream_site_byte
*/
__attribute__((unused)) static guint32
query_bitstream_site_bytes(const bitstream_parsed_t * bitstream, const csite_descr_t *site,
const guint *cfgbytes, const gsize nbytes) {
guint32 result = 0;
gsize i;
for(i = 0; i < nbytes; i++)
result |= query_bitstream_site_byte(bitstream, site, cfgbytes[i]) << (i<<3);
return result;
}
/** \brief Get one config bit from a site
*
* @param bitstream the bitstream data
* @param site the site queried
* @param cfgbit the bit asked for
*
* @return the configuration bit asked for
* @see query_bitstream_site_byte
*/
static inline gboolean
query_bitstream_site_bit(const bitstream_parsed_t *bitstream,
const csite_descr_t *site,
const guint cfgbit) {
const guchar cfgbyte_dat = query_bitstream_site_byte(bitstream, site, byte_addr(cfgbit));
const guint cfgbyte_off = bit_offset(cfgbit);
if ((cfgbyte_dat >> cfgbyte_off) & 1)
return TRUE;
return FALSE;
}
/** \brief Get some (up to 32) config bits from a site
*
* @param bitstream the bitstream data
* @param site the site queried
* @param cfgbits the array of bits asked for
* @param nbits the number of bits asked for
*
* @return the configuration bits asked for, packed into a guint32
*
* \bug For now we use the site as a CLB site, whether I think Eric intended
* this to be a global, type-independent site.
*/
guint32
query_bitstream_site_bits(const bitstream_parsed_t * bitstream,
const csite_descr_t *site,
const guint *cfgbits, const gsize nbits) {
/* unsigned char *data = bitstream->bincols[pip_type]; */
/* The data has already been sliced according to its type. It is not
obvious that this is what we should do */
guint32 result = 0;
gsize i;
for(i = 0; i < nbits; i++)
/* XXX move this to non-conditional */
if (query_bitstream_site_bit(bitstream, site, cfgbits[i]))
result |= 1 << i;
return result;
}
/*
* Typed queries
*/
/*
* There's a nice and tidy canonical way to do this...
*/
static inline
guint16 reverse_bits(guint16 input) {
guint16 res = 0;
guint i;
for(i = 0; i < 16; i++)
if (input & (1<<i))
res |= 1 << (15-i);
return res;
}
/** \brief Get the LUT configuration bits from the bitstream
*
* @param bitstream the bitstream data
* @param site the site queried
* @param luts array of 4 guint16, used to return the four LUT values of
* the slice
*
* @return the configuration bits
*/
void
query_bitstream_luts(const bitstream_parsed_t *bitstream,
const csite_descr_t *site, guint16 luts[]) {
guint i;
(void) bitstream;
(void) site;
/* query eight luts. Bits are MSB first, but in reverse order */
for (i=0; i<8; i++) {
luts[i] = 0;
}
return;
}
void
set_bitstream_lut(const bitstream_parsed_t *bitstream,
const csite_descr_t *site,
const guint16 lut_val, const unsigned lut_i) {
(void) bitstream;
(void) site;
(void) lut_val;
(void) lut_i;
}
void set_bitstream_site_bits(const bitstream_parsed_t * bitstream,
const csite_descr_t *site,
const uint32_t vals,
const guint cfgbits[], const gsize nbits) {
(void) bitstream;
(void) site;
(void) vals;
(void) cfgbits;
(void) nbits;
}
typedef int property_t;
/*
* Same kind of interface, for different properties. Merge with above
* once the config bits are unified.
*/
__attribute__((unused)) static guint16
query_bistream_config(const bitstream_parsed_t *bitstream,
const site_t *site, const guint subsite,
const property_t *prop) {
(void) bitstream;
(void) site;
(void) subsite;
(void) prop;
return 0;
}
/** \brief Get the bram data bits from a site
*
* @param bitstream the bitstream data
* @param site the site queried
* @return the bram data array
*/
guint16 *
query_bitstream_bram_data(const bitstream_parsed_t *bitstream,
const csite_descr_t *site) {
/* Actually this is only bit reordering */
guint16 *bram_data = g_new0(guint16,64*16);
(void) bitstream;
(void) site;
return bram_data;
}
/** \brief Get the bram parity bits from a site
*
* @param bitstream the bitstream data
* @param site the site queried
* @return the bram parity bits array
*/
__attribute__((unused)) static const gchar *
query_bitstream_bram_parity(const bitstream_parsed_t *bitstream,
const site_t *site) {
/* Actually this is only bit reordering */
(void) bitstream;
(void) site;
return NULL;
}
/** \brief Get the total configuration size in bytes for a site type
*
* @param bitstream the bitstream data
* @param site the site queried
* @return the bram parity bits array
*/
gsize
query_bitstream_type_size(const bitstream_parsed_t *parsed,
const site_type_t type) {
const chip_struct_t *chip_struct = parsed->chip_struct;
/* Get the col type from the site type */
const design_col_t col_type = type_bits[type].col_type;
/* per site, 80 bits in each frame -- 10 bytes */
return type_frame_count(chip_struct, col_type) * STDWIDTH;
}
/** \brief Get all configuration bits from a site
*
* @param bitstream the bitstream data
* @param site the site queried
* @data the data buffer whereto write the data. It must be sufficiently
* large for receiving the data
* @return error code
*
* @see query_bitstream_type_size
* @see query_bitstream_byte
*/
int
query_bitstream_site_data(gchar *data, const gsize nbytes,
const bitstream_parsed_t *parsed,
const csite_descr_t *site) {
const unsigned width = type_bits[site->type].y_width;
gsize i;
for (i = 0; i < nbytes; i++) {
unsigned pos = bitpos_to_cfgbit(i << 3, width);
g_print("From site, getting pos %i ", pos);
data[i] = query_bitstream_site_byte(parsed, site, pos);
}
return 0;
}