lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # Copyright lowRISC contributors. |
| 3 | # Licensed under the Apache License, Version 2.0, see LICENSE for details. |
| 4 | # SPDX-License-Identifier: Apache-2.0 |
| 5 | |
| 6 | import argparse |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 7 | import os |
| 8 | import shutil |
Michael Schaffner | 49c8207 | 2019-09-10 14:38:06 -0700 | [diff] [blame] | 9 | import subprocess |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 10 | import sys |
| 11 | |
| 12 | import wget |
| 13 | |
Michael Schaffner | 49c8207 | 2019-09-10 14:38:06 -0700 | [diff] [blame] | 14 | USAGE = """./get_lfsr_coeffs.py [-t <temporary folder>] [-o <outfile>] [-f] [--fib] |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 15 | |
Michael Schaffner | 49c8207 | 2019-09-10 14:38:06 -0700 | [diff] [blame] | 16 | Downloads LFSR constants from [1] and dumps them in SystemVerilog format |
| 17 | (for use in prim_lfsr.sv). These coeffs are for a Galois XOR type LFSR, and cover |
| 18 | implementations ranging from 4 to 64bits. |
| 19 | |
| 20 | Alternatively, the script can also extract the XNOR Fibonacci type LFSR coefficients |
| 21 | from the XILINX application note 52 [2] by specifying the --fib switch. Note that this |
| 22 | depends on the pdftotext utility for Linux. |
| 23 | |
| 24 | [1] https://users.ece.cmu.edu/~koopman/lfsr/ |
| 25 | |
| 26 | [2] https://www.xilinx.com/support/documentation/application_notes/xapp052.pdf |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 27 | """ |
| 28 | |
Michael Schaffner | 49c8207 | 2019-09-10 14:38:06 -0700 | [diff] [blame] | 29 | # configuration for Galois |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 30 | MIN_LFSR_LEN = 4 |
| 31 | MAX_LFSR_LEN = 64 |
| 32 | BASE_URL = 'https://users.ece.cmu.edu/~koopman/lfsr/' |
| 33 | |
Michael Schaffner | 49c8207 | 2019-09-10 14:38:06 -0700 | [diff] [blame] | 34 | # configuration for Fibonacci |
| 35 | FIB_URL = 'https://www.xilinx.com/support/documentation/application_notes/xapp052.pdf' |
| 36 | PDF_NAME = 'xapp052' |
| 37 | LINE_FILTER = [ |
| 38 | 'Table 3: Taps for Maximum-Length LFSR Counters', |
| 39 | 'XAPP 052 July 7,1996 (Version 1.1)' |
| 40 | ] |
| 41 | |
| 42 | |
| 43 | # helper function to write out coeffs |
| 44 | def dump_coeffs(lfsrType, widths, coeffs, outfile): |
| 45 | # widths consistency check |
| 46 | for k in range(widths[0], widths[-1] + 1): |
| 47 | # print("%d -- %d" % (k,widths[k-widths[0]])) |
| 48 | if k != widths[k - widths[0]]: |
| 49 | print("Error: widths is not consistently increasing") |
| 50 | sys.exit(1) |
| 51 | |
| 52 | # select first coefficient in each file and print to SV LUT |
| 53 | with outfile: |
| 54 | decl_str = "localparam int unsigned %s_LUT_OFF = %d;\n" \ |
| 55 | % (lfsrType, min(widths)) |
| 56 | outfile.write(decl_str) |
Michael Schaffner | 1b5fa9f | 2020-01-17 17:43:42 -0800 | [diff] [blame] | 57 | decl_str = "localparam logic [%d:0] %s_COEFFS [%d] = '{ " \ |
Michael Schaffner | 1fff985 | 2021-01-08 14:06:35 -0800 | [diff] [blame] | 58 | % (max(widths) - 1, lfsrType, max(widths) - min(widths) + 1) |
Michael Schaffner | 49c8207 | 2019-09-10 14:38:06 -0700 | [diff] [blame] | 59 | outfile.write(decl_str) |
| 60 | comma = ',\n' |
| 61 | spaces = '' |
| 62 | for k in widths: |
| 63 | if k == max(widths): |
| 64 | comma = "" |
| 65 | if k == min(widths) + 1: |
Michael Schaffner | 1fff985 | 2021-01-08 14:06:35 -0800 | [diff] [blame] | 66 | spaces += ' ' * len(decl_str) |
| 67 | outfile.write("%s%d'h%s%s" % |
| 68 | (spaces, max(widths), coeffs[k - widths[0]], comma)) |
Michael Schaffner | 49c8207 | 2019-09-10 14:38:06 -0700 | [diff] [blame] | 69 | outfile.write(' };\n') |
| 70 | |
| 71 | |
| 72 | # converts list with bit positions to a hex bit mask string |
| 73 | def to_bit_mask(bitPositions): |
| 74 | |
| 75 | bitMask = 0 |
| 76 | for b in bitPositions: |
| 77 | bitMask += 2**(b - 1) |
| 78 | |
| 79 | return "%X" % bitMask |
| 80 | |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 81 | |
| 82 | def main(): |
| 83 | parser = argparse.ArgumentParser( |
| 84 | prog="get-lfsr-coeffs", |
| 85 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 86 | usage=USAGE, |
| 87 | description=__doc__, |
| 88 | epilog='defaults or the filename - can be used for stdin/stdout') |
| 89 | parser.add_argument( |
| 90 | '-t', |
| 91 | '--tempfolder', |
| 92 | help="""temporary folder to download the lfsr constant files |
| 93 | to (defaults to lfsr_tmp)""", |
| 94 | default='lfsr_tmp') |
Michael Schaffner | 49c8207 | 2019-09-10 14:38:06 -0700 | [diff] [blame] | 95 | parser.add_argument('--fib', |
| 96 | help='download fibonacci coefficients', |
| 97 | action='store_true') |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 98 | parser.add_argument('-f', |
| 99 | '--force', |
| 100 | help='overwrites tempfolder', |
| 101 | action='store_true') |
| 102 | parser.add_argument('-o', |
| 103 | '--output', |
| 104 | type=argparse.FileType('w'), |
| 105 | default=sys.stdout, |
| 106 | metavar='file', |
| 107 | help='Output file (default stdout)') |
| 108 | |
| 109 | args = parser.parse_args() |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 110 | |
| 111 | if args.force and os.path.exists(args.tempfolder): |
| 112 | shutil.rmtree(args.tempfolder) |
| 113 | |
| 114 | if not os.path.exists(args.tempfolder): |
| 115 | # download coefficient files |
| 116 | os.makedirs(args.tempfolder, exist_ok=args.force) |
| 117 | os.chdir(args.tempfolder) |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 118 | |
Michael Schaffner | 49c8207 | 2019-09-10 14:38:06 -0700 | [diff] [blame] | 119 | if args.fib: |
| 120 | lfsrType = 'FIB_XNOR' |
| 121 | |
| 122 | wget.download(FIB_URL) |
| 123 | cmd = ['pdftotext %s.pdf' % PDF_NAME, '> %s.txt' % PDF_NAME] |
| 124 | subprocess.call(cmd, shell=True) |
| 125 | print("") |
Michael Schaffner | 1fff985 | 2021-01-08 14:06:35 -0800 | [diff] [blame] | 126 | cmd = [ |
| 127 | 'grep -A 350 "%s" %s.txt > table.txt' % |
| 128 | (LINE_FILTER[0], PDF_NAME) |
| 129 | ] |
Michael Schaffner | 49c8207 | 2019-09-10 14:38:06 -0700 | [diff] [blame] | 130 | subprocess.call(cmd, shell=True) |
| 131 | |
| 132 | # parse the table |
| 133 | widths = [] |
| 134 | coeffs = [] |
| 135 | columnType = 0 |
| 136 | with open('table.txt') as infile: |
| 137 | for line in infile: |
| 138 | line = line.strip() |
| 139 | if line and line not in LINE_FILTER: |
| 140 | if line == 'n': |
| 141 | columnType = 0 |
| 142 | # yes, this is a typo in the PDF :) |
| 143 | elif line == 'XNOR from': |
| 144 | columnType = 1 |
| 145 | elif columnType: |
| 146 | tmpCoeffs = [int(c) for c in line.split(',')] |
| 147 | coeffs += [tmpCoeffs] |
| 148 | else: |
| 149 | widths += [int(line)] |
| 150 | |
| 151 | # # printout for checking |
| 152 | # for (w,c) in zip(widths,coeffs): |
| 153 | # print("width: %d > coeffs: %s" % (w, str(c))) |
| 154 | |
| 155 | # convert to bitmask |
| 156 | for k in range(len(coeffs)): |
| 157 | coeffs[k] = to_bit_mask(coeffs[k]) |
| 158 | |
| 159 | else: |
| 160 | lfsrType = 'GAL_XOR' |
| 161 | |
| 162 | for k in range(MIN_LFSR_LEN, MAX_LFSR_LEN + 1): |
| 163 | url = '%s%d.txt' % (BASE_URL, k) |
| 164 | print("\nDownloading %d bit LFSR coeffs from %s..." % (k, url)) |
| 165 | wget.download(url) |
| 166 | print("") |
| 167 | |
| 168 | widths = [] |
| 169 | coeffs = [] |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 170 | for k in range(MIN_LFSR_LEN, MAX_LFSR_LEN + 1): |
| 171 | filename = '%d.txt' % k |
| 172 | with open(filename) as infile: |
| 173 | # read the first line |
Michael Schaffner | 49c8207 | 2019-09-10 14:38:06 -0700 | [diff] [blame] | 174 | widths += [k] |
| 175 | coeffs += [infile.readline().strip()] |
| 176 | |
| 177 | # write to stdout or file |
| 178 | dump_coeffs(lfsrType, widths, coeffs, outfile=args.output) |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 179 | else: |
| 180 | print("Temporary directory already exists, abort...") |
Michael Schaffner | 49c8207 | 2019-09-10 14:38:06 -0700 | [diff] [blame] | 181 | sys.exit(1) |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 182 | |
| 183 | |
| 184 | if __name__ == '__main__': |
| 185 | main() |