forked from f4pga/prjxray
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbfixup.py
More file actions
executable file
·365 lines (300 loc) · 10.3 KB
/
dbfixup.py
File metadata and controls
executable file
·365 lines (300 loc) · 10.3 KB
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
#/usr/bin/env python3
import sys, os, re
from prjxray import util
clb_int_zero_db = [
# CLB interconnet
# Ex:
# segbits_hclk_l.db:73:HCLK_L.HCLK_LEAF_CLK_B_BOTL4.HCLK_CK_BUFHCLK10 00_21 04_21
# segbits_int_l.db:207:INT_L.CLK_L0.GCLK_L_B8_WEST !01_21 00_21 00_25 01_20 01_24
"00_21 00_22 00_26 01_28|00_25 01_20 01_21 01_24",
"00_23 00_30 01_22 01_25|00_27 00_29 01_26 01_29",
"01_12 01_14 01_16 01_18|00_10 00_11 01_09 01_10",
"00_13 01_17 00_15 00_17|00_18 00_19 01_13 00_14",
"00_34 00_38 01_33 01_37|00_35 00_39 01_38 01_40",
"00_33 00_41 01_32 01_34|00_37 00_42 01_36 01_41",
]
def zero_range(tag, bits, wordmin, wordmax):
"""
If any bits occur wordmin <= word <= wordmax,
default bits in wordmin <= word <= wordmax to 0
"""
# The bit index, if any, that needs to be one hotted
bitidx = None
for bit in bits:
if bit[0] == "!":
continue
fidx, bidx = [int(s) for s in bit.split("_")]
if wordmin <= fidx <= wordmax:
if bitidx is not None and bidx != bitidx:
print("Old bit index: %u, new: %u" % (bitidx, bidx))
print("%s bits: %s" % (tag, str(bits)))
raise ValueError("%s: inconsistent bit index" % tag)
bitidx = bidx
if bitidx is None:
return
for fidx in range(wordmin, wordmax + 1):
bit = "%02d_%02d" % (fidx, bitidx)
# Preserve 1 bits, set others to 0
if bit not in bits:
bits.add("!" + bit)
def bits_str(bits):
"""Convert a set into canonical form"""
return ' '.join(sorted(list(bits)))
def zero_groups(tag, bits, zero_db, strict=True, verbose=False):
"""
See if a line occurs within a bit group
If it does, add 0 bits
Ex: 01_02 04_05
Means find a line that has either of these bits
If either of them occurs, default bits in that set to zero
Ex: 01_02 04_05|07_08 10_11
If any bits from the first group occur,
default bits in the second group to zero
Ex: 01_02 04_05,ALL_ZERO
ALL_ZERO is an enum that is part of the group but is all 0
It must have 0 candidates
strict: assert that the size of the given group is the size of the given mask
"""
for zdb in zero_db:
allzero_tag = None
if "," in zdb:
zdb, allzero_tag = zdb.split(",")
if "|" in zdb:
a, b = zdb.split("|")
a = a.split()
b = b.split()
else:
a = zdb.split()
b = a
bitmatch = False
for bit in a:
if bit in bits:
bitmatch = True
if not (bitmatch or allzero_tag == tag):
continue
bits_orig = set(bits)
for bit in b:
if bit not in bits:
bits.add("!" + bit)
verbose and print(
"Grouped %s: %s => %s" %
(tag, bits_str(bits_orig), bits_str(bits)))
if a == b and strict:
assert len(bits) == len(
a), "Mask size %u != DB entry size %u: %s" % (
len(a), len(bits), bits_str(bits))
def add_zero_bits(
fn_in, fn_out, zero_db, clb_int=False, strict=True, verbose=False):
'''
Add multibit entries
This requires adding some zero bits (ex: !31_09)
If an entry has any of the
'''
new_lines = set()
changes = 0
llast = None
drops = 0
with open(fn_in, "r") as f:
for line in f:
# Hack: skip duplicate lines
# This happens while merging a new multibit entry
line = line.strip()
if line == llast:
continue
tag, bits, mode = util.parse_db_line(line)
# an enum that needs masking
# check below asserts that a mask was actually applied
if mode and mode != "<0 candidates>" and not strict:
verbose and print(
"WARNING: dropping unresolved line: %s" % line)
drops += 1
continue
assert mode not in (
"<const0>",
"<const1>"), "Entries must be resolved. line: %s" % (line, )
if mode == "always":
new_line = line
else:
if mode:
assert mode == "<0 candidates>", line
bits = set()
else:
bits = set(bits)
"""
This appears to be a large range of one hot interconnect bits
They are immediately before the first CLB real bits
"""
if clb_int:
zero_range(tag, bits, 22, 25)
zero_groups(tag, bits, zero_db, strict=strict, verbose=verbose)
new_line = " ".join([tag] + sorted(bits))
if re.match(r'.*<.*>.*', new_line):
print("Original line: %s" % line)
assert 0, "Failed to remove line mode: %s" % (new_line)
if new_line != line:
changes += 1
new_lines.add(new_line)
llast = line
if drops:
print("WARNING: %s dropped %s unresolved lines" % (fn_in, drops))
with open(fn_out, "w") as f:
for line in sorted(new_lines):
print(line, file=f)
return changes
def update_mask(db_root, mask_db, src_dbs, offset=0):
bits = set()
mask_db_file = "%s/mask_%s.db" % (db_root, mask_db)
if os.path.exists(mask_db_file):
with open(mask_db_file, "r") as f:
for line in f:
line = line.split()
assert len(line) == 2
assert line[0] == "bit"
bits.add(line[1])
for src_db in src_dbs:
seg_db_file = "%s/segbits_%s.db" % (db_root, src_db)
if not os.path.exists(seg_db_file):
continue
with open(seg_db_file, "r") as f:
for line in f:
line = line.split()
for bit in line[1:]:
if bit[0] == "!":
continue
if offset != 0:
m = re.match(r"(\d+)_(\d+)", bit)
bit = "%02d_%02d" % (
int(m.group(1)), int(m.group(2)) + offset)
bits.add(bit)
if len(bits) > 0:
with open(mask_db_file, "w") as f:
for bit in sorted(bits):
print("bit %s" % bit, file=f)
def load_zero_db(fn):
# Remove comments and convert to list of lines
ret = []
for l in open(fn, "r"):
pos = l.find("#")
if pos >= 0:
l = l[0:pos]
l = l.strip()
if not l:
continue
ret.append(l)
return ret
def update_seg_fns(
fn_inouts, zero_db, clb_int, lazy=False, strict=True, verbose=False):
seg_files = 0
seg_lines = 0
for fn_in, fn_out in fn_inouts:
verbose and print("zb %s: %s" % (fn_in, os.path.exists(fn_in)))
if lazy and not os.path.exists(fn_in):
continue
changes = add_zero_bits(
fn_in,
fn_out,
zero_db,
clb_int=clb_int,
strict=strict,
verbose=verbose)
if changes is not None:
seg_files += 1
seg_lines += changes
print(
"Segbit: checked %u files w/ %u changed lines" %
(seg_files, seg_lines))
def update_masks(db_root):
for mask_db, src_dbs in [
("clbll_l", ("clbll_l", "int_l")),
("clbll_r", ("clbll_r", "int_r")),
("clblm_l", ("clblm_l", "int_l")),
("clblm_r", ("clblm_r", "int_r")),
("hclk_l", ("hclk_l", )),
("hclk_r", ("hclk_r", )),
("bram_l", ("bram_l", )),
("bram_r", ("bram_r", )),
("dsp_l", ("dsp_l", )),
("dsp_r", ("dsp_r", )),
]:
update_mask(db_root, mask_db, src_dbs)
for mask_db, src_dbs in [
("bram_l", ("int_l", )),
("bram_r", ("int_r", )),
("dsp_l", ("int_l", )),
("dsp_r", ("int_r", )),
]:
for k in range(5):
update_mask(db_root, mask_db, src_dbs, offset=64 * k)
print("Mask: checked files")
def update_segs(
db_root,
clb_int,
seg_fn_in,
seg_fn_out,
zero_db_fn,
strict=True,
verbose=False):
if clb_int:
zero_db = clb_int_zero_db
lazy = True
def gen_fns():
for tile_type in ["int_l", "int_r", "clbll_l", "clbll_r",
"clblm_l", "clblm_r"]:
fn = "%s/segbits_%s.db" % (db_root, tile_type)
yield (fn, fn)
fn_inouts = list(gen_fns())
else:
assert seg_fn_in
assert zero_db_fn
lazy = False
if not seg_fn_out:
seg_fn_out = seg_fn_in
fn_inouts = [(seg_fn_in, seg_fn_out)]
zero_db = load_zero_db(zero_db_fn)
print("CLB INT mode: %s" % clb_int)
print("Segbit groups: %s" % len(zero_db))
update_seg_fns(
fn_inouts, zero_db, clb_int, lazy=lazy, strict=strict, verbose=verbose)
def run(
db_root,
clb_int=False,
zero_db_fn=None,
seg_fn_in=None,
seg_fn_out=None,
strict=None,
verbose=False):
if strict is None:
strict = not clb_int
# Probably should split this into two programs
update_segs(
db_root,
clb_int=clb_int,
seg_fn_in=seg_fn_in,
seg_fn_out=seg_fn_out,
zero_db_fn=zero_db_fn,
strict=strict,
verbose=verbose)
if clb_int:
update_masks(db_root)
def main():
import argparse
parser = argparse.ArgumentParser(description='Create multi-bit entries')
util.db_root_arg(parser)
parser.add_argument('--verbose', action='store_true', help='')
parser.add_argument(
'--clb-int', action='store_true', help='Fixup CLB interconnect')
parser.add_argument('--zero-db', help='Apply custom patches')
parser.add_argument('--seg-fn-in', help='')
parser.add_argument('--seg-fn-out', help='')
util.add_bool_arg(parser, "--strict", default=None)
args = parser.parse_args()
run(
args.db_root,
args.clb_int,
args.zero_db,
args.seg_fn_in,
args.seg_fn_out,
strict=args.strict,
verbose=args.verbose)
if __name__ == '__main__':
main()