blob: 61feef994efe9e75f485a2308eabbe5184ea2888 [file] [log] [blame]
Michael Schaffnerac16bb62020-12-30 17:48:11 -08001#!/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
5r"""Given an ECC encoding matrix, this script generates random life cycle
6state encodings that can be incrementally written to a memory protected with
7the ECC code specified.
8"""
9import argparse
10import logging as log
11import random
12from pathlib import Path
13
14import hjson
Michael Schaffner1fff9852021-01-08 14:06:35 -080015from lib.common import wrapped_docstring
16from lib.LcStEnc import LcStEnc
Michael Schaffnerac16bb62020-12-30 17:48:11 -080017from mako.template import Template
18
Michael Schaffnerac16bb62020-12-30 17:48:11 -080019# State encoding definition
Michael Schaffner1fff9852021-01-08 14:06:35 -080020LC_STATE_DEFINITION_FILE = "hw/ip/lc_ctrl/data/lc_ctrl_state.hjson"
Michael Schaffnerac16bb62020-12-30 17:48:11 -080021# Code templates to render
Michael Schaffner1fff9852021-01-08 14:06:35 -080022TEMPLATES = ["hw/ip/lc_ctrl/rtl/lc_ctrl_state_pkg.sv.tpl"]
Michael Schaffnerac16bb62020-12-30 17:48:11 -080023
24
25def main():
Rupert Swarbricka3605f82021-08-31 17:08:34 +010026 log.basicConfig(level=log.WARNING,
Michael Schaffner3c6b2f32021-01-28 14:53:26 -080027 format="%(levelname)s: %(message)s")
Michael Schaffnerac16bb62020-12-30 17:48:11 -080028
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 Schaffnerac16bb62020-12-30 17:48:11 -080039 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 Schaffner16532b22021-01-29 20:57:09 -080059 try:
60 lc_st_enc = LcStEnc(config)
61 except RuntimeError as err:
62 log.error(err)
63 exit(1)
Michael Schaffnerac16bb62020-12-30 17:48:11 -080064
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
75if __name__ == "__main__":
76 main()