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 |
| 7 | import glob |
| 8 | import os |
| 9 | import shutil |
| 10 | import sys |
| 11 | |
| 12 | import wget |
| 13 | |
| 14 | USAGE = """./get_lfsr_coeffs.py [-t <temporary folder>] [-o <outfile>] [-f] |
| 15 | |
| 16 | Downloads lfsr constants from https://users.ece.cmu.edu/~koopman/lfsr/ |
| 17 | and dumps them in SystemVerilog format (for use in prim_lfsr.sv). |
| 18 | """ |
| 19 | |
| 20 | MIN_LFSR_LEN = 4 |
| 21 | MAX_LFSR_LEN = 64 |
| 22 | BASE_URL = 'https://users.ece.cmu.edu/~koopman/lfsr/' |
| 23 | |
| 24 | |
| 25 | def main(): |
| 26 | parser = argparse.ArgumentParser( |
| 27 | prog="get-lfsr-coeffs", |
| 28 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 29 | usage=USAGE, |
| 30 | description=__doc__, |
| 31 | epilog='defaults or the filename - can be used for stdin/stdout') |
| 32 | parser.add_argument( |
| 33 | '-t', |
| 34 | '--tempfolder', |
| 35 | help="""temporary folder to download the lfsr constant files |
| 36 | to (defaults to lfsr_tmp)""", |
| 37 | default='lfsr_tmp') |
| 38 | parser.add_argument('-f', |
| 39 | '--force', |
| 40 | help='overwrites tempfolder', |
| 41 | action='store_true') |
| 42 | parser.add_argument('-o', |
| 43 | '--output', |
| 44 | type=argparse.FileType('w'), |
| 45 | default=sys.stdout, |
| 46 | metavar='file', |
| 47 | help='Output file (default stdout)') |
| 48 | |
| 49 | args = parser.parse_args() |
| 50 | outfile = args.output |
| 51 | |
| 52 | if args.force and os.path.exists(args.tempfolder): |
| 53 | shutil.rmtree(args.tempfolder) |
| 54 | |
| 55 | if not os.path.exists(args.tempfolder): |
| 56 | # download coefficient files |
| 57 | os.makedirs(args.tempfolder, exist_ok=args.force) |
| 58 | os.chdir(args.tempfolder) |
| 59 | for k in range(MIN_LFSR_LEN, MAX_LFSR_LEN + 1): |
| 60 | url = '%s%d.txt' % (BASE_URL, k) |
| 61 | print("\nDownloading %d bit LFSR coeffs from %s..." % (k, url)) |
| 62 | wget.download(url) |
| 63 | print("") |
| 64 | |
| 65 | # select first coefficient in each file and print to SV LUT |
| 66 | with outfile: |
| 67 | decl_str = "localparam logic [%d:0] coeffs [%d:%d] = '{ " % ( |
| 68 | MAX_LFSR_LEN - 1, MIN_LFSR_LEN, MAX_LFSR_LEN) |
| 69 | outfile.write(decl_str) |
| 70 | comma = ',\n' |
| 71 | spaces = '' |
| 72 | for k in range(MIN_LFSR_LEN, MAX_LFSR_LEN + 1): |
| 73 | filename = '%d.txt' % k |
| 74 | with open(filename) as infile: |
| 75 | # read the first line |
| 76 | poly_coeffs = infile.readline().strip() |
| 77 | if k == MAX_LFSR_LEN: |
| 78 | comma = "" |
| 79 | if k == MIN_LFSR_LEN + 1: |
| 80 | for l in range(len(decl_str)): |
| 81 | spaces += ' ' |
| 82 | outfile.write("%s%d'h%s%s" % |
| 83 | (spaces, MAX_LFSR_LEN, poly_coeffs, comma)) |
| 84 | outfile.write(' };\n') |
| 85 | else: |
| 86 | print("Temporary directory already exists, abort...") |
| 87 | |
| 88 | |
| 89 | if __name__ == '__main__': |
| 90 | main() |