-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathldpc_tables_to_ram.py
executable file
·460 lines (379 loc) · 14.2 KB
/
ldpc_tables_to_ram.py
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
449
450
451
452
453
454
455
456
457
458
459
460
#!/usr/bin/env python3
#
# DVB FPGA
#
# Copyright 2019-2022 by suoto <andre820@gmail.com>
#
# This source describes Open Hardware and is licensed under the CERN-OHL-W v2.
#
# You may redistribute and modify this source and make products using it under
# the terms of the CERN-OHL-W v2 (https://ohwr.org/cern_ohl_w_v2.txt).
#
# This source is distributed WITHOUT ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING
# OF MERCHANTABILITY, SATISFACTORY QUALITY AND FITNESS FOR A PARTICULAR PURPOSE.
# Please see the CERN-OHL-W v2 for applicable conditions.
#
# Source location: https://github.com/phase4ground/dvb_fpga
#
# As per CERN-OHL-W v2 section 4.1, should You produce hardware based on this
# source, You must maintain the Source Location visible on the external case of
# the DVB Encoder or other products you make using this source.
"""
Simple script to generate a VHDL package with the LDPC tables. Used tables from
https://github.com/aicodix/tables
"""
import logging
import math
import os.path as p
import re
import sys
from glob import glob
from os import makedirs
from tabulate import tabulate
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
_logger = logging.getLogger(__name__)
ROOT = p.abspath(p.dirname(__file__))
HEADER = """\
--
-- DVB FPGA
--
-- Copyright 2019-2022 by suoto <andre820@gmail.com>
--
-- This source describes Open Hardware and is licensed under the CERN-OHL-W v2.
--
-- You may redistribute and modify this source and make products using it under
-- the terms of the CERN-OHL-W v2 (https://ohwr.org/cern_ohl_w_v2.txt).
--
-- This source is distributed WITHOUT ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING
-- OF MERCHANTABILITY, SATISFACTORY QUALITY AND FITNESS FOR A PARTICULAR PURPOSE.
-- Please see the CERN-OHL-W v2 for applicable conditions.
--
-- Source location: https://github.com/phase4ground/dvb_fpga
--
-- As per CERN-OHL-W v2 section 4.1, should You produce hardware based on this
-- source, You must maintain the Source Location visible on the external case of
-- the DVB Encoder or other products you make using this source.
"""
_PARAM_RE = re.compile(r"ldpc_table_(FECFRAME_(?:NORMAL|SHORT))_(C\d_\d+).csv")
class LdpcTable:
"""
Helper class to handle extracting data from a CSV file with LDPC coefficients
"""
def __init__(self, path):
self._path = path
self._table = tuple(self._read())
self._widths = self._getColumnWidths()
# Extract the frame type and code rate of this table from the path
match = _PARAM_RE.search(self._path)
self.frame_type = match.group(1).lower()
self.code_rate = match.group(2)
_logger.debug(
"Config of path '%s is %s, %s",
repr(self._path),
repr(self.frame_type),
repr(self.code_rate),
)
@property
def table(self):
return tuple(self._table)
def _read(self):
_logger.info("Reading %s", self._path)
for line in (x.strip() for x in open(self._path).readlines()):
if not line:
continue
yield tuple(int(x) for x in line.split(","))
def _getColumnWidths(self):
max_values = {}
for line in self._table:
max_values[0] = max(len(line), max_values.get(0, 0))
for cnum, col in enumerate(line):
max_values[cnum + 1] = max(col, max_values.get(cnum + 1, 0))
widths = {}
for col, value in max_values.items():
widths[col] = math.ceil(math.log2(value))
return widths
def _renderRamLine(self, line):
items = [f"0 => {len(line)}"]
for i in range(1, len(self._widths)):
try:
item = line[i - 1]
except IndexError:
# item = f"({self._widths[i] - 1} downto 0 => 'U')"
item = -1
items.append(f"{i} => {item}")
return ", ".join([str(x) for x in items])
@property
def name(self):
return p.basename(self._path).split(".")[0]
def _getWidthArray(self):
return (
f"constant {self.name.upper()}_COLUMN_WIDTHS : integer_vector_t := ("
+ ", ".join(
["%d => %d" % (key, value) for key, value in self._widths.items()]
)
+ ");"
)
def getStats(self):
width = sum(self._widths.values())
total = self.length * width
return dict(
depth=self.length,
width=width,
entries=len(self._widths),
total=(total + 7) // 8,
brams_18k=max((width + 17) // 18, self.length * width / 1024 / 18),
brams_36k=max((width + 35) // 36, self.length * width / 1024 / 36),
)
@property
def length(self):
return len(self._table)
@property
def widths(self):
return tuple(self._widths.values())
def render(self):
_logger.debug("widths=%s", self._widths)
width = sum(self._widths.values())
brams_18k = max((width + 17) // 18, self.length * width / 1024 / 18)
brams_36k = max((width + 35) // 36, self.length * width / 1024 / 36)
columns = len(self._widths)
lines = [
f" -- From {p.relpath(self._path, ROOT)}, table is {self.length}x{width} ({self.length*width/8.} bytes)",
f" -- Resource estimation: {brams_18k} x 18 kB BRAMs or {brams_36k} x 36 kB BRAMs",
f" {self._getWidthArray()}",
f"",
f" constant {self.name.upper()} : integer_array_t"
f"(0 to {self.length - 1})(0 to {columns - 1}) := (",
]
for i, line in enumerate(self._table):
this_line = f" {i} => integer_vector_t'({self._renderRamLine(line)})"
if i < self.length - 1:
lines.append(f"{this_line},")
else:
lines.append(f"{this_line}")
lines.append(" );")
_logger.info(
"RAM has %.2f kb (%d x %d), or %d BRAMs",
self.length * width / 1024.0,
self.length,
width,
brams_18k,
)
return "\n".join(lines)
def _generateRowCountRom(tables):
"""
Generates the contents of the counter config part of the LDPC metadata table
"""
for table in tables:
count_cfg = []
row_length_prev = None
cnt = 0
for row_length in (len(row) for row in table.table):
if row_length_prev is not None and row_length != row_length_prev:
count_cfg += [(cnt, row_length_prev)]
cnt = 0
cnt += 1
row_length_prev = row_length
if row_length_prev is not None:
count_cfg += [(cnt, row_length_prev)]
# Two stages is enough to handle all tables, we'll force to that even
# if 1 stage would suffice to make the HDL side easier
if len(count_cfg) != 2:
cnt, row_length = count_cfg[0]
count_cfg = [(cnt // 2, row_length), (cnt - cnt // 2, row_length)]
yield (table.frame_type, table.code_rate), count_cfg
LDPC_Q = {
("fecframe_normal", "C1_4"): 135,
("fecframe_normal", "C1_3"): 120,
("fecframe_normal", "C2_5"): 108,
("fecframe_normal", "C1_2"): 90,
("fecframe_normal", "C3_5"): 72,
("fecframe_normal", "C2_3"): 60,
("fecframe_normal", "C3_4"): 45,
("fecframe_normal", "C4_5"): 36,
("fecframe_normal", "C5_6"): 30,
("fecframe_normal", "C8_9"): 20,
("fecframe_normal", "C9_10"): 18,
("fecframe_short", "C1_4"): 36,
("fecframe_short", "C1_3"): 30,
("fecframe_short", "C2_5"): 27,
("fecframe_short", "C1_2"): 25,
("fecframe_short", "C3_5"): 18,
("fecframe_short", "C2_3"): 15,
("fecframe_short", "C3_4"): 12,
("fecframe_short", "C4_5"): 10,
("fecframe_short", "C5_6"): 8,
("fecframe_short", "C8_9"): 5,
}
def _generateTablesRom(tables):
count_cfg = dict(_generateRowCountRom(tables))
package = [
f" -- Record with LDPC metadata",
f" type ldpc_metadata_t is record",
f" addr : integer;",
f" q : integer;",
f" stage_0_loops: integer;",
f" stage_0_rows: integer;",
f" stage_1_loops: integer;",
f" stage_1_rows: integer;",
f" end record ldpc_metadata_t;",
f"",
f" -- Reduce the footprint of this",
f" constant LDPC_TABLE_DATA_WIDTH : integer := numbits(max(DVB_N_LDPC));",
f"",
f" -- Use this function to get the starting address of a given config within the LDPC_DATA_TABLE",
f" function get_ldpc_metadata (",
f" constant frame_type : frame_type_t;",
f" constant code_rate : code_rate_t) return ldpc_metadata_t;",
"",
]
# data_width = max(y for table in tables for y in table.widths)
data_width = "LDPC_TABLE_DATA_WIDTH"
table_length = sum(len(y) for table in tables for y in table.table)
address_width = math.ceil(math.log2(table_length))
package_body = [
# Generate the function that will receive the frame type and code rate
# and return the address of the table
f" -- Use this function to get the starting address of a given config within the LDPC_DATA_TABLE",
f" function get_ldpc_metadata (",
f" constant frame_type : frame_type_t;",
f" constant code_rate : code_rate_t) return ldpc_metadata_t is",
" begin",
]
_logger.debug("Address map:")
addr = 0
for table in tables:
_logger.debug("0x%.4x | %4d | %s", addr, addr, table.name)
key = (table.frame_type, table.code_rate)
result_tuple = [
addr,
count_cfg[key][0][0],
count_cfg[key][0][1],
count_cfg[key][1][0],
count_cfg[key][1][1],
]
print(result_tuple)
package_body += [
f" if frame_type = {table.frame_type} and code_rate = {table.code_rate} then",
f" return (",
f" addr => {addr},",
f" q => {LDPC_Q[key]},",
f" stage_0_loops => {count_cfg[key][0][0]},",
f" stage_0_rows => {count_cfg[key][0][1]},",
f" stage_1_loops => {count_cfg[key][1][0]},",
f" stage_1_rows => {count_cfg[key][1][1]});",
f" end if;",
]
addr += sum(len(x) for x in table.table)
package_body += [
"",
" -- Return a non existing index for any config not listed above",
" return (addr => -1, q => -1, stage_0_loops => -1, stage_0_rows => -1, stage_1_loops => -1, stage_1_rows => -1);",
" end function get_ldpc_metadata;",
"",
]
_logger.debug(
"Base widths are %s for data and %d for address", data_width, address_width
)
# Now generate the actual table
package += [
"",
f" constant LDPC_DATA_TABLE : std_logic_array_t"
f"(0 to {table_length - 1})"
f"({data_width} - 1 downto 0) := (",
]
addr = 0
rom_content = []
for table in tables:
rom_content += [
f" -- Table for {table.frame_type}, {table.code_rate}",
]
for row in table.table:
for i, column in enumerate(row):
# Append the is_last flag and the actual data
is_last = i == len(row) - 1
# vhdl_bin_value = bin(column)[2:]
# vhdl_bin_value = '0'*(data_width - len(vhdl_bin_value)) + vhdl_bin_value
entry = (
f" {addr:4d} => "
f"std_logic_vector(to_unsigned({column:5d}, {data_width}))"
)
if addr < table_length - 1:
entry += ", -- %5d / 0x%.4x" % (column, column)
if is_last:
entry += " -- last item of row"
rom_content += [entry]
addr += 1
package += ["\n".join(rom_content) + ");"]
return "\n".join(package), "\n".join(package_body)
def main():
lines = str(HEADER)
lines += "\n".join(
[
"library ieee;",
"use ieee.std_logic_1164.all;",
"use ieee.numeric_std.all;",
"",
"library fpga_cores;",
"use fpga_cores.common_pkg.all;",
"",
"use work.dvb_utils_pkg.all;",
"",
]
)
paths = sorted(glob(p.join(ROOT, "ldpc", "*.csv")))
rendered_tables = ""
stats = []
ldpc_tables = tuple(LdpcTable(x) for x in paths)
for ldpc_table in ldpc_tables:
stats += [["--", ldpc_table.name] + list(ldpc_table.getStats().values())]
rendered_tables += ldpc_table.render()
rendered_tables += "\n\n"
lines += "\n-- Summary of statistics\n\n"
lines += tabulate(
stats,
headers=(
"--",
"table",
"depth",
"width (bits)",
"width (entries)",
"total (bytes)",
"18k BRAMs",
"36k BRAMs",
),
)
lines += "\n".join(["", "", "package ldpc_tables_pkg is", "", "",])
lines += "\n".join(
[
"",
" -- LDPC_TABLE_FECFRAME_<frame_type>_<code_rate>_COLUMN_WIDTHS constants have the bit",
" -- width of each row",
"",
" -- LDPC_TABLE_FECFRAME_<frame_type>_<code_rate> is the actual LDPC where the number of",
" -- columns is normalized to the row with most columns and the first column of each row",
" -- contains the number of valid elements within the row. Elements outisde the valid range",
" -- are represented as -1",
"",
"",
"",
]
)
lines += rendered_tables
package, package_body = _generateTablesRom(ldpc_tables)
lines += "\n".join(
[
"",
package,
"",
"end package ldpc_tables_pkg;",
"",
"package body ldpc_tables_pkg is",
package_body,
"end package body ldpc_tables_pkg;",
]
)
target_file = p.abspath(p.join(ROOT, "..", "rtl", "ldpc", "ldpc_tables_pkg.vhd"))
if not p.exists(p.dirname(target_file)):
makedirs(p.dirname(target_file))
open(target_file, "w").write(lines)
main()