blob: e01b0a614f87c4a8a25f48b792001448da742e04 [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"""Generate RTL and documentation collateral from OTP memory
6map definition file (hjson).
7"""
8import argparse
9import logging as log
10import random
11from pathlib import Path
12
13import hjson
Michael Schaffner1fff9852021-01-08 14:06:35 -080014from lib.common import wrapped_docstring
15from lib.OtpMemMap import OtpMemMap
Michael Schaffnerac16bb62020-12-30 17:48:11 -080016from mako.template import Template
17
Michael Schaffnerac16bb62020-12-30 17:48:11 -080018TABLE_HEADER_COMMENT = '''<!--
19DO NOT EDIT THIS FILE DIRECTLY.
Michael Schaffner1fff9852021-01-08 14:06:35 -080020It has been generated with ./util/design/gen-otp-mmap.py
Michael Schaffnerac16bb62020-12-30 17:48:11 -080021-->
22
23'''
24
25# memory map source
Michael Schaffner1fff9852021-01-08 14:06:35 -080026MMAP_DEFINITION_FILE = "hw/ip/otp_ctrl/data/otp_ctrl_mmap.hjson"
Michael Schaffnerac16bb62020-12-30 17:48:11 -080027# documentation tables to generate
Michael Schaffner1fff9852021-01-08 14:06:35 -080028PARTITIONS_TABLE_FILE = "hw/ip/otp_ctrl/doc/otp_ctrl_partitions.md"
29DIGESTS_TABLE_FILE = "hw/ip/otp_ctrl/doc/otp_ctrl_digests.md"
30MMAP_TABLE_FILE = "hw/ip/otp_ctrl/doc/otp_ctrl_mmap.md"
Michael Schaffnerac16bb62020-12-30 17:48:11 -080031# code templates to render
Michael Schaffner1fff9852021-01-08 14:06:35 -080032TEMPLATES = [
33 "hw/ip/otp_ctrl/data/otp_ctrl.hjson.tpl",
34 "hw/ip/otp_ctrl/rtl/otp_ctrl_part_pkg.sv.tpl"
35]
Michael Schaffnerac16bb62020-12-30 17:48:11 -080036
37
38def main():
Rupert Swarbrick4af3c812021-08-31 17:02:51 +010039 log.basicConfig(level=log.WARNING,
Michael Schaffner3c6b2f32021-01-28 14:53:26 -080040 format="%(levelname)s: %(message)s")
Michael Schaffnerac16bb62020-12-30 17:48:11 -080041
42 parser = argparse.ArgumentParser(
43 prog="gen-otp-mmap",
44 description=wrapped_docstring(),
45 formatter_class=argparse.RawDescriptionHelpFormatter)
46
47 # Generator options for compile time random netlist constants
48 parser.add_argument('--seed',
49 type=int,
50 metavar='<seed>',
51 help='Custom seed for RNG to compute default values.')
52
53 args = parser.parse_args()
54
55 with open(MMAP_DEFINITION_FILE, 'r') as infile:
56 config = hjson.load(infile)
57
58 # If specified, override the seed for random netlist constant computation.
59 if args.seed:
60 log.warning('Commandline override of seed with {}.'.format(
61 args.seed))
62 config['seed'] = args.seed
63 # Otherwise, we either take it from the .hjson if present, or
64 # randomly generate a new seed if not.
65 else:
66 random.seed()
67 new_seed = random.getrandbits(64)
68 if config.setdefault('seed', new_seed) == new_seed:
69 log.warning(
70 'No seed specified, setting to {}.'.format(new_seed))
71
Michael Schaffner6d32d8d2021-01-29 20:37:43 -080072 try:
73 otp_mmap = OtpMemMap(config)
74 except RuntimeError as err:
75 log.error(err)
76 exit(1)
Michael Schaffnerac16bb62020-12-30 17:48:11 -080077
78 with open(PARTITIONS_TABLE_FILE, 'w') as outfile:
79 outfile.write(TABLE_HEADER_COMMENT +
80 otp_mmap.create_partitions_table())
81
82 with open(DIGESTS_TABLE_FILE, 'w') as outfile:
83 outfile.write(TABLE_HEADER_COMMENT +
84 otp_mmap.create_digests_table())
85
86 with open(MMAP_TABLE_FILE, 'w') as outfile:
87 outfile.write(TABLE_HEADER_COMMENT + otp_mmap.create_mmap_table())
88
89 # render all templates
90 for template in TEMPLATES:
91 with open(template, 'r') as tplfile:
92 tpl = Template(tplfile.read())
93 with open(
94 Path(template).parent.joinpath(Path(template).stem),
95 'w') as outfile:
96 outfile.write(tpl.render(otp_mmap=otp_mmap))
97
98
99if __name__ == "__main__":
100 main()