forked from f4pga/prjxray
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bitread.cc
307 lines (267 loc) · 8.07 KB
/
bitread.cc
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
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <set>
#include <string>
#include <vector>
#include <absl/strings/numbers.h>
#include <absl/strings/str_cat.h>
#include <absl/strings/str_split.h>
#include <gflags/gflags.h>
#include <prjxray/memory_mapped_file.h>
#include <prjxray/xilinx/architectures.h>
#include <prjxray/xilinx/bitstream_reader.h>
#include <prjxray/xilinx/configuration.h>
DEFINE_bool(c, false, "output '*' for repeating patterns");
DEFINE_bool(C, false, "do not ignore the checksum in each frame");
DEFINE_int32(f,
-1,
"only dump the specified frame (might be used more than once)");
DEFINE_string(F,
"",
"<first_frame_address>:<last_frame_address> only dump frame in "
"the specified range");
DEFINE_string(o, "", "write machine-readable output file with config frames");
DEFINE_bool(p, false, "output a binary netpgm image");
DEFINE_bool(x,
false,
"use format 'bit_%%08x_%%03d_%%02d_t%%d_h%%d_r%%d_c%%d_m%%d'\n"
"The fields have the following meaning:\n"
" - complete 32 bit hex frame id\n"
" - word index with that frame (decimal)\n"
" - bit index with that word (decimal)\n"
" - decoded frame type from frame id\n"
" - decoded top/botttom from frame id (top=0)\n"
" - decoded row address from frame id\n"
" - decoded column address from frame id\n"
" - decoded minor address from frame id\n");
DEFINE_bool(y, false, "use format 'bit_%%08x_%%03d_%%02d'");
DEFINE_bool(z, false, "skip zero frames (frames with all bits cleared) in o");
DEFINE_string(part_file, "", "YAML file describing a Xilinx part");
DEFINE_string(architecture,
"Series7",
"Architecture of the provided bitstream");
namespace xilinx = prjxray::xilinx;
std::set<uint32_t> frames;
uint32_t frame_range_begin = 0, frame_range_end = 0;
std::vector<uint32_t> zero_frame(101);
struct BitReader {
BitReader(const std::vector<uint8_t>& bytes) : bytes_(bytes) {}
const std::vector<uint8_t>& bytes_;
template <typename T>
int operator()(T& arg) {
using ArchType = std::decay_t<decltype(arg)>;
auto reader =
xilinx::BitstreamReader<ArchType>::InitWithBytes(bytes_);
if (!reader) {
std::cerr << "Input doesn't look like a bitstream"
<< std::endl;
return 1;
}
std::cout << "Config size: " << reader->words().size()
<< " words" << std::endl;
auto part = ArchType::Part::FromFile(FLAGS_part_file);
if (!part) {
std::cerr << "Part file not found or invalid"
<< std::endl;
return 1;
}
auto config = xilinx::Configuration<ArchType>::InitWithPackets(
*part, *reader);
if (!config) {
std::cerr
<< "Bitstream does not appear to be for this part"
<< std::endl;
return 1;
}
std::cout << "Number of configuration frames: "
<< config->frames().size() << std::endl;
FILE* f = stdout;
if (!FLAGS_o.empty()) {
f = fopen(FLAGS_o.c_str(), "w");
if (f == nullptr) {
printf(
"Can't open output file '%s' for "
"writing!\n",
FLAGS_o.c_str());
return 1;
}
} else {
fprintf(f, "\n");
}
std::vector<std::vector<bool>> pgmdata;
std::vector<int> pgmsep;
int word_length = sizeof(typename ArchType::WordType) * 8;
for (auto& it : config->frames()) {
if (FLAGS_z && it.second == zero_frame)
continue;
if (!frames.empty() && !frames.count(it.first))
continue;
if (frame_range_begin != frame_range_end &&
(it.first < frame_range_begin ||
frame_range_end <= it.first))
continue;
if (FLAGS_o.empty())
printf(
"Frame 0x%08x (Type=%d Top=%d Row=%d "
"Column=%d "
"Minor=%d):\n",
static_cast<uint32_t>(it.first),
static_cast<unsigned int>(
it.first.block_type()),
it.first.is_bottom_half_rows() ? 1 : 0,
it.first.row(), it.first.column(),
it.first.minor());
if (FLAGS_p) {
if (it.first.minor() == 0 && !pgmdata.empty())
pgmsep.push_back(pgmdata.size());
pgmdata.push_back(std::vector<bool>());
for (size_t i = 0; i < it.second.size(); i++)
for (int k = 0; k < word_length; k++)
pgmdata.back().push_back(
(it.second.at(i) &
(1 << k)) != 0);
} else if (FLAGS_x || FLAGS_y) {
for (int i = 0; i < (int)it.second.size();
i++) {
for (int k = 0; k < word_length; k++) {
if (((i != 50 || k > 12 ||
FLAGS_C)) &&
((it.second.at(i) &
(1 << k)) != 0)) {
if (FLAGS_x)
fprintf(
f,
"bit_%08x_%"
"03d_%"
"02d_t%d_h%"
"d_r%d_c%"
"d_m%d\n",
static_cast<
uint32_t>(
it.first),
i, k,
static_cast<
unsigned int>(
it.first
.block_type()),
it.first.is_bottom_half_rows()
? 1
: 0,
it.first
.row(),
it.first
.column(),
it.first
.minor());
else
fprintf(
f,
"bit_%08x_%"
"03d_"
"%02d\n",
static_cast<
uint32_t>(
it.first),
i, k);
}
}
}
if (FLAGS_o.empty())
fprintf(f, "\n");
} else {
if (!FLAGS_o.empty())
fprintf(
f, ".frame 0x%08x\n",
static_cast<uint32_t>(it.first));
for (size_t i = 0; i < it.second.size(); i++)
fprintf(f, "%08x%s",
it.second.at(i) &
((i != 50 || FLAGS_C)
? 0xffffffff
: 0xffffe000),
(i % 6) == 5 ? "\n" : " ");
fprintf(f, "\n\n");
}
}
if (FLAGS_p) {
int width = pgmdata.size() + pgmsep.size();
int height = ArchType::words_per_frame * word_length;
fprintf(f, "P5 %d %d 15\n", width, height);
for (int y = 0,
bit = ArchType::words_per_frame * word_length -
1;
y < height; y++, bit--) {
for (int x = 0, frame = 0, sep = 0; x < width;
x++, frame++) {
if (sep < int(pgmsep.size()) &&
frame == pgmsep.at(sep)) {
fputc(8, f);
x++, sep++;
}
if (bit >=
(int)pgmdata.at(frame).size()) {
fputc(0, f);
continue;
}
fputc(
pgmdata.at(frame).at(bit) ? 15 : 0,
f);
}
if (bit % word_length == 0 && y) {
for (int x = 0; x < width; x++)
fputc(8, f);
y++;
}
}
}
if (!FLAGS_o.empty())
fclose(f);
printf("DONE\n");
return 0;
}
};
int main(int argc, char** argv) {
gflags::SetUsageMessage(
absl::StrCat("Usage: ", argv[0], " [options] [bitfile]"));
gflags::ParseCommandLineFlags(&argc, &argv, true);
if (FLAGS_f >= 0) {
frames.insert(FLAGS_f);
}
if (!FLAGS_F.empty()) {
std::pair<std::string, std::string> p =
absl::StrSplit(FLAGS_F, ":");
frame_range_begin = strtol(p.first.c_str(), nullptr, 0);
frame_range_end = strtol(p.second.c_str(), nullptr, 0) + 1;
}
std::vector<uint8_t> in_bytes;
if (argc == 2) {
auto in_file_name = argv[1];
auto in_file =
prjxray::MemoryMappedFile::InitWithFile(in_file_name);
if (!in_file) {
std::cerr << "Can't open input file '" << in_file_name
<< "' for reading!" << std::endl;
return 1;
}
std::cout << "Bitstream size: " << in_file->size() << " bytes"
<< std::endl;
in_bytes = std::vector<uint8_t>(
static_cast<uint8_t*>(in_file->data()),
static_cast<uint8_t*>(in_file->data()) + in_file->size());
} else {
while (1) {
int c = getchar();
if (c == EOF)
break;
in_bytes.push_back(c);
}
std::cout << "Bitstream size: " << in_bytes.size() << " bytes"
<< std::endl;
}
xilinx::Architecture::Container arch_container =
xilinx::ArchitectureFactory::create_architecture(
FLAGS_architecture);
return absl::visit(BitReader(in_bytes), arch_container);
}