Michael Schaffner | ac16bb6 | 2020-12-30 17:48:11 -0800 | [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 | r"""Given an ECC encoding matrix, this script generates random life cycle |
| 6 | state encodings that can be incrementally written to a memory protected with |
| 7 | the ECC code specified. |
| 8 | """ |
| 9 | import argparse |
| 10 | import logging as log |
| 11 | import random |
| 12 | from pathlib import Path |
| 13 | |
| 14 | import hjson |
Michael Schaffner | 1fff985 | 2021-01-08 14:06:35 -0800 | [diff] [blame] | 15 | from lib.common import wrapped_docstring |
| 16 | from lib.LcStEnc import LcStEnc |
Michael Schaffner | ac16bb6 | 2020-12-30 17:48:11 -0800 | [diff] [blame] | 17 | from mako.template import Template |
| 18 | |
Michael Schaffner | ac16bb6 | 2020-12-30 17:48:11 -0800 | [diff] [blame] | 19 | # State encoding definition |
Michael Schaffner | 1fff985 | 2021-01-08 14:06:35 -0800 | [diff] [blame] | 20 | LC_STATE_DEFINITION_FILE = "hw/ip/lc_ctrl/data/lc_ctrl_state.hjson" |
Michael Schaffner | ac16bb6 | 2020-12-30 17:48:11 -0800 | [diff] [blame] | 21 | # Code templates to render |
Michael Schaffner | 1fff985 | 2021-01-08 14:06:35 -0800 | [diff] [blame] | 22 | TEMPLATES = ["hw/ip/lc_ctrl/rtl/lc_ctrl_state_pkg.sv.tpl"] |
Michael Schaffner | ac16bb6 | 2020-12-30 17:48:11 -0800 | [diff] [blame] | 23 | |
| 24 | |
| 25 | def main(): |
Rupert Swarbrick | a3605f8 | 2021-08-31 17:08:34 +0100 | [diff] [blame] | 26 | log.basicConfig(level=log.WARNING, |
Michael Schaffner | 3c6b2f3 | 2021-01-28 14:53:26 -0800 | [diff] [blame] | 27 | format="%(levelname)s: %(message)s") |
Michael Schaffner | ac16bb6 | 2020-12-30 17:48:11 -0800 | [diff] [blame] | 28 | |
| 29 | parser = argparse.ArgumentParser( |
| 30 | prog="gen-lc-state-enc", |
| 31 | description=wrapped_docstring(), |
| 32 | formatter_class=argparse.RawDescriptionHelpFormatter) |
| 33 | |
| 34 | parser.add_argument('-s', |
| 35 | '--seed', |
| 36 | type=int, |
| 37 | metavar='<seed>', |
| 38 | help='Custom seed for RNG.') |
Michael Schaffner | ac16bb6 | 2020-12-30 17:48:11 -0800 | [diff] [blame] | 39 | args = parser.parse_args() |
| 40 | |
| 41 | with open(LC_STATE_DEFINITION_FILE, 'r') as infile: |
| 42 | config = hjson.load(infile) |
| 43 | |
| 44 | # If specified, override the seed for random netlist constant computation. |
| 45 | if args.seed: |
| 46 | log.warning('Commandline override of seed with {}.'.format( |
| 47 | args.seed)) |
| 48 | config['seed'] = args.seed |
| 49 | # Otherwise, we either take it from the .hjson if present, or |
| 50 | # randomly generate a new seed if not. |
| 51 | else: |
| 52 | random.seed() |
| 53 | new_seed = random.getrandbits(64) |
| 54 | if config.setdefault('seed', new_seed) == new_seed: |
| 55 | log.warning( |
| 56 | 'No seed specified, setting to {}.'.format(new_seed)) |
| 57 | |
| 58 | # validate config and generate encoding |
Michael Schaffner | 16532b2 | 2021-01-29 20:57:09 -0800 | [diff] [blame] | 59 | try: |
| 60 | lc_st_enc = LcStEnc(config) |
| 61 | except RuntimeError as err: |
| 62 | log.error(err) |
| 63 | exit(1) |
Michael Schaffner | ac16bb6 | 2020-12-30 17:48:11 -0800 | [diff] [blame] | 64 | |
| 65 | # render all templates |
| 66 | for template in TEMPLATES: |
| 67 | with open(template, 'r') as tplfile: |
| 68 | tpl = Template(tplfile.read()) |
| 69 | with open( |
| 70 | Path(template).parent.joinpath(Path(template).stem), |
| 71 | 'w') as outfile: |
| 72 | outfile.write(tpl.render(lc_st_enc=lc_st_enc)) |
| 73 | |
| 74 | |
| 75 | if __name__ == "__main__": |
| 76 | main() |