-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathfiledump.c
192 lines (163 loc) · 4.72 KB
/
filedump.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
/*
* Copyright (C) 2006, 2007 Jean-Baptiste Note <[email protected]>
*
* 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 <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdint.h>
#include <glib.h>
#include "bitstream_parser.h"
#include "filedump.h"
/* XXX This is currently needed but should be removed */
#include "design.h"
typedef void (*dump_hook_t)(FILE *out, const void *_data, const unsigned num);
static void dump_bin_rev(FILE *out, const void *_data, const unsigned num);
/**
* Virtex-II dump function
*/
static void
dump_bin_rev(FILE *out, const void *_data, const unsigned num) {
const unsigned char *const data = _data;
unsigned i;
for( i = 0; i < num; i++) {
unsigned char atom = data[num-1-i];
putc(atom, out);
}
}
static void
dump_bin(FILE *out, const void *_data, const unsigned num) {
const unsigned char *const data = _data;
unsigned i;
/* Dump weak weights first of the in-frame le word, without unaligned loads */
for( i = 0; i < num; i++)
putc(data[i ^ 3], out);
}
typedef void (*naming_hook_t)(char *buf, unsigned buf_len,
const unsigned type,
const unsigned index,
const unsigned frameid);
static FILE *open_frame_file(const unsigned type,
const unsigned index,
const unsigned frameid,
const gchar *odir,
naming_hook_t name) {
FILE *f;
char fn[64];
gchar *filename = NULL;
name(fn, sizeof(fn), type, index, frameid);
filename = g_build_filename(odir,fn,NULL);
f = fopen(filename, "w");
if (!f)
g_warning("could not open file %s", filename);
g_free(filename);
return f;
}
static FILE *
open_unk_frame_file(const frame_record_t *frame,
const gchar *odir) {
FILE *f;
char fn[64];
char farname[64];
gchar *filename = NULL;
snprintf_far(farname, sizeof(farname), frame->far);
snprintf(fn, sizeof(fn), "frame_%s.bin", farname);
filename = g_build_filename(odir,fn,NULL);
f = fopen(filename, "w");
if (!f)
g_warning("could not open file %s", filename);
g_free(filename);
return f;
}
static void
seq_frame_name(char *buf, unsigned buf_len,
const unsigned type,
const unsigned index,
const unsigned frameid) {
snprintf(buf, buf_len, "frame_%02x_%02x_%02x",
type, index, frameid);
}
typedef struct _dumping {
dump_hook_t dump;
naming_hook_t naming;
unsigned framelen;
const gchar *dir;
} dumping_t;
/* TODO: merge these two when we switch to a unified frame record model.
framelen should be a const in the parsed thing.
*/
static void
design_write_frames_iter(const char *frame,
guint type, guint index, guint frameidx,
void *data) {
dumping_t *dumping = data;
dump_hook_t dump = dumping->dump;
naming_hook_t naming = dumping->naming;
unsigned framelen = dumping->framelen;
const gchar *odir = dumping->dir;
gsize frame_len = framelen * sizeof(uint32_t);
FILE *f;
f = open_frame_file(type, index, frameidx, odir, naming);
if (!f) {
g_warning("could not open file for frame");
return;
}
dump(f, frame, frame_len);
fclose(f);
}
static void
design_write_unk_frames_iter(const frame_record_t *framerec,
void *data) {
dumping_t *dumping = data;
dump_hook_t dump = dumping->dump;
FILE *f;
f = open_unk_frame_file(framerec, dumping->dir);
if (!f) {
g_warning("could not open file for frame");
return;
}
dump(f, framerec->frame, framerec->framelen * sizeof(uint32_t));
fclose(f);
}
/**
* for virtex-II
*/
void design_write_frames(const bitstream_parsed_t *parsed,
const gchar *outdir) {
dumping_t data;
const chip_struct_t *chip = parsed->chip_struct;
data.dump = dump_bin_rev;
data.dir = outdir;
data.framelen = chip->framelen;
if (TRUE)
data.naming = typed_frame_name;
else
data.naming = seq_frame_name;
iterate_over_frames(parsed, design_write_frames_iter, &data);
}
/**
* for virtex-4 and virtex-5
*/
void
design_dump_frames(const bitstream_parsed_t *parsed,
const gchar *outdir) {
dumping_t data;
data.dump = dump_bin;
data.dir = outdir;
data.naming = seq_frame_name;
iterate_over_unk_frames(parsed, design_write_unk_frames_iter, &data);
}