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 | r"""Top Module Generator |
| 6 | """ |
| 7 | import argparse |
| 8 | import logging as log |
Weicai Yang | a667036 | 2020-11-24 17:19:52 -0800 | [diff] [blame] | 9 | import random |
Srikrishna Iyer | 689981e | 2021-09-16 12:12:37 -0700 | [diff] [blame] | 10 | import shutil |
Michael Schaffner | 6015796 | 2020-05-01 19:11:28 -0700 | [diff] [blame] | 11 | import sys |
Philipp Wagner | f9ee797 | 2021-10-28 16:21:02 +0100 | [diff] [blame] | 12 | import tempfile |
Weicai Yang | a60ae7d | 2020-02-21 14:32:50 -0800 | [diff] [blame] | 13 | from collections import OrderedDict |
Weicai Yang | a667036 | 2020-11-24 17:19:52 -0800 | [diff] [blame] | 14 | from copy import deepcopy |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 15 | from io import StringIO |
| 16 | from pathlib import Path |
Cindy Chen | c8389ed | 2021-11-04 10:53:07 -0700 | [diff] [blame] | 17 | from typing import Dict, List, Optional, Tuple |
Michael Schaffner | 3452c71 | 2022-01-21 20:15:12 -0800 | [diff] [blame] | 18 | from itertools import chain |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 19 | |
| 20 | import hjson |
Srikrishna Iyer | aea37dd | 2021-11-20 21:11:40 -0800 | [diff] [blame] | 21 | import tlgen |
Philipp Wagner | 270e28b | 2021-03-16 20:23:04 +0000 | [diff] [blame] | 22 | from ipgen import (IpBlockRenderer, IpConfig, IpDescriptionOnlyRenderer, |
| 23 | IpTemplate, TemplateRenderError) |
Eunchan Kim | cb28a17 | 2019-10-08 16:35:48 -0700 | [diff] [blame] | 24 | from mako import exceptions |
Weicai Yang | a60ae7d | 2020-02-21 14:32:50 -0800 | [diff] [blame] | 25 | from mako.template import Template |
Srikrishna Iyer | a463e17 | 2022-02-02 12:09:39 -0800 | [diff] [blame] | 26 | from reggen import access, gen_rtl, gen_sec_cm_testplan, window |
Rupert Swarbrick | b650026 | 2021-02-23 15:16:16 +0000 | [diff] [blame] | 27 | from reggen.inter_signal import InterSignal |
Rupert Swarbrick | 269bb3d | 2021-02-23 15:41:56 +0000 | [diff] [blame] | 28 | from reggen.ip_block import IpBlock |
Michael Schaffner | 3452c71 | 2022-01-21 20:15:12 -0800 | [diff] [blame] | 29 | from reggen.countermeasure import CounterMeasure |
Rupert Swarbrick | b650026 | 2021-02-23 15:16:16 +0000 | [diff] [blame] | 30 | from reggen.lib import check_list |
Rupert Swarbrick | 5aa249f | 2021-07-19 18:35:11 +0100 | [diff] [blame] | 31 | from topgen import get_hjsonobj_xbars |
Eunchan Kim | 2c813b4 | 2020-08-03 16:22:56 -0700 | [diff] [blame] | 32 | from topgen import intermodule as im |
Timothy Chen | 9443221 | 2021-03-01 22:29:18 -0800 | [diff] [blame] | 33 | from topgen import lib as lib |
Rupert Swarbrick | bc2bc58 | 2021-02-09 13:30:37 +0000 | [diff] [blame] | 34 | from topgen import merge_top, search_ips, validate_top |
Srikrishna Iyer | 689981e | 2021-09-16 12:12:37 -0700 | [diff] [blame] | 35 | from topgen.c_test import TopGenCTest |
Srikrishna Iyer | aea37dd | 2021-11-20 21:11:40 -0800 | [diff] [blame] | 36 | from topgen.clocks import Clocks |
Rupert Swarbrick | 1018410 | 2021-03-31 09:12:42 +0100 | [diff] [blame] | 37 | from topgen.gen_dv import gen_dv |
Michael Schaffner | 426ed20 | 2021-08-02 17:44:42 -0700 | [diff] [blame] | 38 | from topgen.gen_top_docs import gen_top_docs |
Srikrishna Iyer | aea37dd | 2021-11-20 21:11:40 -0800 | [diff] [blame] | 39 | from topgen.merge import connect_clocks, create_alert_lpgs, extract_clocks |
Timothy Chen | 63c6606 | 2021-07-24 00:25:22 -0700 | [diff] [blame] | 40 | from topgen.resets import Resets |
Srikrishna Iyer | aea37dd | 2021-11-20 21:11:40 -0800 | [diff] [blame] | 41 | from topgen.top import Top |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 42 | |
| 43 | # Common header for generated files |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 44 | warnhdr = """// |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 45 | // ------------------- W A R N I N G: A U T O - G E N E R A T E D C O D E !! -------------------// |
| 46 | // PLEASE DO NOT HAND-EDIT THIS FILE. IT HAS BEEN AUTO-GENERATED WITH THE FOLLOWING COMMAND: |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 47 | """ |
| 48 | genhdr = """// Copyright lowRISC contributors. |
Michael Schaffner | 7b0807d | 2020-10-27 19:54:52 -0700 | [diff] [blame] | 49 | // Licensed under the Apache License, Version 2.0, see LICENSE for details. |
| 50 | // SPDX-License-Identifier: Apache-2.0 |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 51 | """ + warnhdr |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 52 | |
Srikrishna Iyer | 689981e | 2021-09-16 12:12:37 -0700 | [diff] [blame] | 53 | GENCMD = ("// util/topgen.py -t hw/top_{topname}/data/top_{topname}.hjson\n" |
| 54 | "// -o hw/top_{topname}") |
| 55 | |
Philipp Wagner | ea3cd4c | 2020-05-07 17:28:18 +0100 | [diff] [blame] | 56 | SRCTREE_TOP = Path(__file__).parent.parent.resolve() |
Eunchan Kim | c6a6a3b | 2019-09-12 14:27:43 -0700 | [diff] [blame] | 57 | |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 58 | TOPGEN_TEMPLATE_PATH = Path(__file__).parent / "topgen/templates" |
Philipp Wagner | fb443ab | 2021-03-05 11:10:28 +0000 | [diff] [blame] | 59 | |
Philipp Wagner | 270e28b | 2021-03-16 20:23:04 +0000 | [diff] [blame] | 60 | |
| 61 | def ipgen_render(template_name: str, topname: str, params: Dict, |
| 62 | out_path: Path): |
| 63 | """ Render an IP template for a specific toplevel using ipgen. |
| 64 | |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 65 | The generated IP block is placed in the "ip_autogen" directory of the |
Philipp Wagner | 270e28b | 2021-03-16 20:23:04 +0000 | [diff] [blame] | 66 | toplevel. |
| 67 | |
| 68 | Aborts the program execution in case of an error. |
| 69 | """ |
Cindy Liu | 88109a0 | 2022-03-30 19:19:24 -0700 | [diff] [blame] | 70 | module_name = params.get("module_instance_name", template_name) |
| 71 | instance_name = f"top_{topname}_{module_name}" |
Philipp Wagner | 270e28b | 2021-03-16 20:23:04 +0000 | [diff] [blame] | 72 | ip_template = IpTemplate.from_template_path( |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 73 | SRCTREE_TOP / "hw/ip_templates" / template_name) |
Philipp Wagner | 270e28b | 2021-03-16 20:23:04 +0000 | [diff] [blame] | 74 | |
| 75 | try: |
| 76 | ip_config = IpConfig(ip_template.params, instance_name, params) |
| 77 | except ValueError as e: |
| 78 | log.error(f"Unable to render IP template {template_name!r}: {str(e)}") |
| 79 | sys.exit(1) |
| 80 | |
| 81 | try: |
| 82 | renderer = IpBlockRenderer(ip_template, ip_config) |
Cindy Liu | 88109a0 | 2022-03-30 19:19:24 -0700 | [diff] [blame] | 83 | renderer.render(out_path / "ip_autogen" / module_name, |
Philipp Wagner | 270e28b | 2021-03-16 20:23:04 +0000 | [diff] [blame] | 84 | overwrite_output_dir=True) |
| 85 | except TemplateRenderError as e: |
| 86 | log.error(e.verbose_str()) |
| 87 | sys.exit(1) |
| 88 | |
Eunchan Kim | fd561be | 2020-04-24 15:42:11 -0700 | [diff] [blame] | 89 | |
Rupert Swarbrick | eb619e6 | 2021-03-05 15:01:54 +0000 | [diff] [blame] | 90 | def generate_top(top, name_to_block, tpl_filename, **kwargs): |
Sam Elliott | 7e36bd7 | 2020-04-22 14:05:49 +0100 | [diff] [blame] | 91 | top_tpl = Template(filename=tpl_filename) |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 92 | |
Eunchan Kim | cb28a17 | 2019-10-08 16:35:48 -0700 | [diff] [blame] | 93 | try: |
Rupert Swarbrick | eb619e6 | 2021-03-05 15:01:54 +0000 | [diff] [blame] | 94 | return top_tpl.render(top=top, name_to_block=name_to_block, **kwargs) |
Eunchan Kim | 6599ba9 | 2020-04-13 15:27:16 -0700 | [diff] [blame] | 95 | except: # noqa: E722 |
Eunchan Kim | cb28a17 | 2019-10-08 16:35:48 -0700 | [diff] [blame] | 96 | log.error(exceptions.text_error_template().render()) |
Sam Elliott | 7e36bd7 | 2020-04-22 14:05:49 +0100 | [diff] [blame] | 97 | return "" |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 98 | |
| 99 | |
| 100 | def generate_xbars(top, out_path): |
Eunchan Kim | ba970df | 2020-04-17 10:21:01 -0700 | [diff] [blame] | 101 | topname = top["name"] |
| 102 | gencmd = ("// util/topgen.py -t hw/top_{topname}/data/top_{topname}.hjson " |
| 103 | "-o hw/top_{topname}/\n\n".format(topname=topname)) |
Eunchan Kim | 6df9a1f | 2019-10-09 14:46:05 -0700 | [diff] [blame] | 104 | |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 105 | for obj in top["xbar"]: |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 106 | xbar_path = out_path / "ip/xbar_{}/data/autogen".format(obj["name"]) |
Eunchan Kim | c745294 | 2019-12-19 17:04:37 -0800 | [diff] [blame] | 107 | xbar_path.mkdir(parents=True, exist_ok=True) |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 108 | xbar = tlgen.validate(obj) |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 109 | xbar.ip_path = "hw/top_" + top["name"] + "/ip/{dut}" |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 110 | |
Eunchan Kim | 6df9a1f | 2019-10-09 14:46:05 -0700 | [diff] [blame] | 111 | # Generate output of crossbar with complete fields |
| 112 | xbar_hjson_path = xbar_path / "xbar_{}.gen.hjson".format(xbar.name) |
| 113 | xbar_hjson_path.write_text(genhdr + gencmd + |
Eunchan Kim | 2af98ed | 2019-10-09 15:33:27 -0700 | [diff] [blame] | 114 | hjson.dumps(obj, for_json=True)) |
Eunchan Kim | 6df9a1f | 2019-10-09 14:46:05 -0700 | [diff] [blame] | 115 | |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 116 | if not tlgen.elaborate(xbar): |
| 117 | log.error("Elaboration failed." + repr(xbar)) |
| 118 | |
Eunchan Kim | cb28a17 | 2019-10-08 16:35:48 -0700 | [diff] [blame] | 119 | try: |
Eunchan Kim | 9191f26 | 2020-07-30 16:37:40 -0700 | [diff] [blame] | 120 | results = tlgen.generate(xbar, "top_" + top["name"]) |
Eunchan Kim | 6599ba9 | 2020-04-13 15:27:16 -0700 | [diff] [blame] | 121 | except: # noqa: E722 |
Eunchan Kim | cb28a17 | 2019-10-08 16:35:48 -0700 | [diff] [blame] | 122 | log.error(exceptions.text_error_template().render()) |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 123 | |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 124 | ip_path = out_path / "ip/xbar_{}".format(obj["name"]) |
Eunchan Kim | 9191f26 | 2020-07-30 16:37:40 -0700 | [diff] [blame] | 125 | |
| 126 | for filename, filecontent in results: |
| 127 | filepath = ip_path / filename |
| 128 | filepath.parent.mkdir(parents=True, exist_ok=True) |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 129 | with filepath.open(mode="w", encoding="UTF-8") as fout: |
Eunchan Kim | 9191f26 | 2020-07-30 16:37:40 -0700 | [diff] [blame] | 130 | fout.write(filecontent) |
| 131 | |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 132 | dv_path = out_path / "ip/xbar_{}/dv/autogen".format(obj["name"]) |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 133 | dv_path.mkdir(parents=True, exist_ok=True) |
| 134 | |
Weicai Yang | e4315d2 | 2020-01-09 10:37:42 -0800 | [diff] [blame] | 135 | # generate testbench for xbar |
Eunchan Kim | 8f2cb38 | 2020-05-13 11:53:09 -0700 | [diff] [blame] | 136 | tlgen.generate_tb(xbar, dv_path, "top_" + top["name"]) |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 137 | |
Eunchan Kim | e0d37fe | 2020-08-03 12:05:21 -0700 | [diff] [blame] | 138 | # Read back the comportable IP and amend to Xbar |
| 139 | xbar_ipfile = ip_path / ("data/autogen/xbar_%s.hjson" % obj["name"]) |
| 140 | with xbar_ipfile.open() as fxbar: |
| 141 | xbar_ipobj = hjson.load(fxbar, |
| 142 | use_decimal=True, |
| 143 | object_pairs_hook=OrderedDict) |
| 144 | |
Srikrishna Iyer | aea37dd | 2021-11-20 21:11:40 -0800 | [diff] [blame] | 145 | r_inter_signal_list = check_list( |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 146 | xbar_ipobj.get("inter_signal_list", []), |
| 147 | "inter_signal_list field") |
| 148 | obj["inter_signal_list"] = [ |
Srikrishna Iyer | aea37dd | 2021-11-20 21:11:40 -0800 | [diff] [blame] | 149 | InterSignal.from_raw( |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 150 | "entry {} of the inter_signal_list field".format(idx + 1), |
Srikrishna Iyer | aea37dd | 2021-11-20 21:11:40 -0800 | [diff] [blame] | 151 | entry) for idx, entry in enumerate(r_inter_signal_list) |
Rupert Swarbrick | b650026 | 2021-02-23 15:16:16 +0000 | [diff] [blame] | 152 | ] |
Eunchan Kim | e0d37fe | 2020-08-03 12:05:21 -0700 | [diff] [blame] | 153 | |
Eunchan Kim | 6a4b49e | 2020-02-18 10:33:39 -0800 | [diff] [blame] | 154 | |
Michael Schaffner | 666dde1 | 2019-10-25 11:57:54 -0700 | [diff] [blame] | 155 | def generate_alert_handler(top, out_path): |
Philipp Wagner | 0d7bd2c | 2021-03-19 10:33:52 +0000 | [diff] [blame] | 156 | topname = top["name"] |
| 157 | |
Michael Schaffner | 666dde1 | 2019-10-25 11:57:54 -0700 | [diff] [blame] | 158 | # default values |
Eunchan Kim | c745294 | 2019-12-19 17:04:37 -0800 | [diff] [blame] | 159 | esc_cnt_dw = 32 |
| 160 | accu_cnt_dw = 16 |
Michael Schaffner | 3b511b8 | 2021-10-01 11:11:09 -0700 | [diff] [blame] | 161 | async_on = [] |
Michael Schaffner | 666dde1 | 2019-10-25 11:57:54 -0700 | [diff] [blame] | 162 | # leave this constant |
Eunchan Kim | c745294 | 2019-12-19 17:04:37 -0800 | [diff] [blame] | 163 | n_classes = 4 |
Michael Schaffner | 4fe7846 | 2021-09-28 17:06:07 -0700 | [diff] [blame] | 164 | # low power groups |
Michael Schaffner | f668fff | 2021-09-20 17:36:16 -0700 | [diff] [blame] | 165 | n_lpg = 1 |
Michael Schaffner | 3b511b8 | 2021-10-01 11:11:09 -0700 | [diff] [blame] | 166 | lpg_map = [] |
Michael Schaffner | 666dde1 | 2019-10-25 11:57:54 -0700 | [diff] [blame] | 167 | |
Michael Schaffner | 4fe7846 | 2021-09-28 17:06:07 -0700 | [diff] [blame] | 168 | # Count number of alerts and LPGs |
Michael Schaffner | 666dde1 | 2019-10-25 11:57:54 -0700 | [diff] [blame] | 169 | n_alerts = sum([x["width"] if "width" in x else 1 for x in top["alert"]]) |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 170 | n_lpg = len(top["alert_lpgs"]) |
Michael Schaffner | 4fe7846 | 2021-09-28 17:06:07 -0700 | [diff] [blame] | 171 | n_lpg_width = n_lpg.bit_length() |
Michael Schaffner | 3b511b8 | 2021-10-01 11:11:09 -0700 | [diff] [blame] | 172 | # format used to print out indices in binary format |
| 173 | async_on_format = "1'b{:01b}" |
| 174 | lpg_idx_format = str(n_lpg_width) + "'d{:d}" |
Michael Schaffner | 4fe7846 | 2021-09-28 17:06:07 -0700 | [diff] [blame] | 175 | |
| 176 | # Double check that all these values are greated than 0 |
| 177 | if esc_cnt_dw < 1: |
| 178 | raise ValueError("esc_cnt_dw must be larger than 0") |
| 179 | if accu_cnt_dw < 1: |
| 180 | raise ValueError("accu_cnt_dw must be larger than 0") |
| 181 | if n_lpg < 1: |
| 182 | raise ValueError("n_lpg must be larger than 0") |
Michael Schaffner | 666dde1 | 2019-10-25 11:57:54 -0700 | [diff] [blame] | 183 | |
| 184 | if n_alerts < 1: |
| 185 | # set number of alerts to 1 such that the config is still valid |
| 186 | # that input will be tied off |
| 187 | n_alerts = 1 |
Eunchan Kim | c745294 | 2019-12-19 17:04:37 -0800 | [diff] [blame] | 188 | log.warning("no alerts are defined in the system") |
Michael Schaffner | 666dde1 | 2019-10-25 11:57:54 -0700 | [diff] [blame] | 189 | else: |
Michael Schaffner | 3b511b8 | 2021-10-01 11:11:09 -0700 | [diff] [blame] | 190 | async_on = [] |
| 191 | lpg_map = [] |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 192 | for alert in top["alert"]: |
| 193 | for k in range(alert["width"]): |
| 194 | async_on.append(async_on_format.format(int(alert["async"]))) |
| 195 | lpg_map.append(lpg_idx_format.format(int(alert["lpg_idx"]))) |
Michael Schaffner | 666dde1 | 2019-10-25 11:57:54 -0700 | [diff] [blame] | 196 | |
Philipp Wagner | 0d7bd2c | 2021-03-19 10:33:52 +0000 | [diff] [blame] | 197 | params = { |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 198 | "n_alerts": n_alerts, |
| 199 | "esc_cnt_dw": esc_cnt_dw, |
| 200 | "accu_cnt_dw": accu_cnt_dw, |
| 201 | "async_on": async_on, |
| 202 | "n_classes": n_classes, |
| 203 | "n_lpg": n_lpg, |
| 204 | "lpg_map": lpg_map, |
Philipp Wagner | 0d7bd2c | 2021-03-19 10:33:52 +0000 | [diff] [blame] | 205 | } |
Michael Schaffner | 666dde1 | 2019-10-25 11:57:54 -0700 | [diff] [blame] | 206 | |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 207 | ipgen_render("alert_handler", topname, params, out_path) |
Michael Schaffner | 666dde1 | 2019-10-25 11:57:54 -0700 | [diff] [blame] | 208 | |
| 209 | |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 210 | def generate_plic(top, out_path): |
Eunchan Kim | ba970df | 2020-04-17 10:21:01 -0700 | [diff] [blame] | 211 | topname = top["name"] |
Philipp Wagner | c720ac8 | 2021-03-03 15:34:53 +0000 | [diff] [blame] | 212 | params = {} |
| 213 | |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 214 | # Count number of interrupts |
Eunchan Kim | 88a8615 | 2020-04-13 16:12:08 -0700 | [diff] [blame] | 215 | # Interrupt source 0 is tied to 0 to conform RISC-V PLIC spec. |
| 216 | # So, total number of interrupts are the number of entries in the list + 1 |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 217 | params["src"] = sum( |
Srikrishna Iyer | aea37dd | 2021-11-20 21:11:40 -0800 | [diff] [blame] | 218 | [x["width"] if "width" in x else 1 for x in top["interrupt"]]) + 1 |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 219 | |
| 220 | # Target and priority: Currently fixed |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 221 | params["target"] = int(top["num_cores"], 0) if "num_cores" in top else 1 |
| 222 | params["prio"] = 3 |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 223 | |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 224 | ipgen_render("rv_plic", topname, params, out_path) |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 225 | |
Eunchan Kim | ba970df | 2020-04-17 10:21:01 -0700 | [diff] [blame] | 226 | |
Michael Schaffner | 3452c71 | 2022-01-21 20:15:12 -0800 | [diff] [blame] | 227 | # TODO: For generated IPs that are generated legacy style (i.e., without IPgen) |
| 228 | # we have to search both the source and destination RTL directories, since not |
| 229 | # all files are copied over. This is a workaround which can be removed once |
| 230 | # all generated IPs have transitioned to IPgen. |
| 231 | def generate_regfile_from_path(hjson_path: Path, |
| 232 | generated_rtl_path: Path, |
| 233 | original_rtl_path: Path = None): |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 234 | """Generate RTL register file from path and check countermeasure labels""" |
Michael Schaffner | 3452c71 | 2022-01-21 20:15:12 -0800 | [diff] [blame] | 235 | obj = IpBlock.from_path(str(hjson_path), []) |
| 236 | |
| 237 | # If this block has countermeasures, we grep for RTL annotations in |
| 238 | # all .sv implementation files and check whether they match up |
| 239 | # with what is defined inside the Hjson. |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 240 | sv_files = generated_rtl_path.glob("*.sv") |
Michael Schaffner | 3452c71 | 2022-01-21 20:15:12 -0800 | [diff] [blame] | 241 | if original_rtl_path is not None: |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 242 | sv_files = chain(sv_files, original_rtl_path.glob("*.sv")) |
Michael Schaffner | 3452c71 | 2022-01-21 20:15:12 -0800 | [diff] [blame] | 243 | rtl_names = CounterMeasure.search_rtl_files(sv_files) |
| 244 | obj.check_cm_annotations(rtl_names, str(hjson_path)) |
| 245 | gen_rtl.gen_rtl(obj, str(generated_rtl_path)) |
Srikrishna Iyer | a463e17 | 2022-02-02 12:09:39 -0800 | [diff] [blame] | 246 | gen_sec_cm_testplan.gen_sec_cm_testplan(obj, hjson_path.parent) |
Michael Schaffner | 3452c71 | 2022-01-21 20:15:12 -0800 | [diff] [blame] | 247 | |
| 248 | |
Michael Schaffner | 43ce8d5 | 2021-02-10 17:04:57 -0800 | [diff] [blame] | 249 | def generate_pinmux(top, out_path): |
Michael Schaffner | 6015796 | 2020-05-01 19:11:28 -0700 | [diff] [blame] | 250 | |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 251 | topname = top["name"] |
| 252 | pinmux = top["pinmux"] |
Michael Schaffner | 74c4ff2 | 2021-03-30 15:43:46 -0700 | [diff] [blame] | 253 | |
| 254 | # Generation without pinmux and pinout configuration is not supported. |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 255 | assert "pinmux" in top |
| 256 | assert "pinout" in top |
Michael Schaffner | 57c490d | 2020-04-29 15:08:55 -0700 | [diff] [blame] | 257 | |
| 258 | # Get number of wakeup detectors |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 259 | if "num_wkup_detect" in pinmux: |
| 260 | num_wkup_detect = pinmux["num_wkup_detect"] |
Michael Schaffner | 57c490d | 2020-04-29 15:08:55 -0700 | [diff] [blame] | 261 | else: |
| 262 | num_wkup_detect = 1 |
| 263 | |
| 264 | if num_wkup_detect <= 0: |
| 265 | # TODO: add support for no wakeup counter case |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 266 | log.error("Topgen does currently not support generation of a top " + |
| 267 | "without DIOs.") |
Michael Schaffner | 57c490d | 2020-04-29 15:08:55 -0700 | [diff] [blame] | 268 | return |
| 269 | |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 270 | if "wkup_cnt_width" in pinmux: |
| 271 | wkup_cnt_width = pinmux["wkup_cnt_width"] |
Michael Schaffner | 57c490d | 2020-04-29 15:08:55 -0700 | [diff] [blame] | 272 | else: |
| 273 | wkup_cnt_width = 8 |
| 274 | |
| 275 | if wkup_cnt_width <= 1: |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 276 | log.error("Wakeup counter width must be greater equal 2.") |
Eunchan Kim | 436d224 | 2019-10-29 17:25:51 -0700 | [diff] [blame] | 277 | return |
| 278 | |
Michael Schaffner | 74c4ff2 | 2021-03-30 15:43:46 -0700 | [diff] [blame] | 279 | # MIO Pads |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 280 | n_mio_pads = pinmux["io_counts"]["muxed"]["pads"] |
Michael Schaffner | 74c4ff2 | 2021-03-30 15:43:46 -0700 | [diff] [blame] | 281 | |
Eunchan Kim | 436d224 | 2019-10-29 17:25:51 -0700 | [diff] [blame] | 282 | # Total inputs/outputs |
Michael Schaffner | 74c4ff2 | 2021-03-30 15:43:46 -0700 | [diff] [blame] | 283 | # Reuse the counts from the merge phase |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 284 | n_mio_periph_in = (pinmux["io_counts"]["muxed"]["inouts"] + |
| 285 | pinmux["io_counts"]["muxed"]["inputs"]) |
| 286 | n_mio_periph_out = (pinmux["io_counts"]["muxed"]["inouts"] + |
| 287 | pinmux["io_counts"]["muxed"]["outputs"]) |
| 288 | n_dio_periph_in = (pinmux["io_counts"]["dedicated"]["inouts"] + |
| 289 | pinmux["io_counts"]["dedicated"]["inputs"]) |
| 290 | n_dio_periph_out = (pinmux["io_counts"]["dedicated"]["inouts"] + |
| 291 | pinmux["io_counts"]["dedicated"]["outputs"]) |
| 292 | n_dio_pads = (pinmux["io_counts"]["dedicated"]["inouts"] + |
| 293 | pinmux["io_counts"]["dedicated"]["inputs"] + |
| 294 | pinmux["io_counts"]["dedicated"]["outputs"]) |
Michael Schaffner | 57c490d | 2020-04-29 15:08:55 -0700 | [diff] [blame] | 295 | |
Michael Schaffner | 43ce8d5 | 2021-02-10 17:04:57 -0800 | [diff] [blame] | 296 | # TODO: derive this value |
Michael Schaffner | 74c4ff2 | 2021-03-30 15:43:46 -0700 | [diff] [blame] | 297 | attr_dw = 13 |
Michael Schaffner | 43ce8d5 | 2021-02-10 17:04:57 -0800 | [diff] [blame] | 298 | |
Michael Schaffner | 74c4ff2 | 2021-03-30 15:43:46 -0700 | [diff] [blame] | 299 | # Generation with zero MIO/DIO pads is currently not supported. |
| 300 | assert (n_mio_pads > 0) |
| 301 | assert (n_dio_pads > 0) |
Michael Schaffner | 57c490d | 2020-04-29 15:08:55 -0700 | [diff] [blame] | 302 | |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 303 | log.info("Generating pinmux with following info from hjson:") |
| 304 | log.info("attr_dw: %d" % attr_dw) |
| 305 | log.info("num_wkup_detect: %d" % num_wkup_detect) |
| 306 | log.info("wkup_cnt_width: %d" % wkup_cnt_width) |
| 307 | log.info("n_mio_periph_in: %d" % n_mio_periph_in) |
| 308 | log.info("n_mio_periph_out: %d" % n_mio_periph_out) |
| 309 | log.info("n_dio_periph_in: %d" % n_dio_periph_in) |
| 310 | log.info("n_dio_periph_out: %d" % n_dio_periph_out) |
| 311 | log.info("n_dio_pads: %d" % n_dio_pads) |
Eunchan Kim | 436d224 | 2019-10-29 17:25:51 -0700 | [diff] [blame] | 312 | |
| 313 | # Target path |
| 314 | # rtl: pinmux_reg_pkg.sv & pinmux_reg_top.sv |
| 315 | # data: pinmux.hjson |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 316 | rtl_path = out_path / "ip/pinmux/rtl/autogen" |
Eunchan Kim | 436d224 | 2019-10-29 17:25:51 -0700 | [diff] [blame] | 317 | rtl_path.mkdir(parents=True, exist_ok=True) |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 318 | data_path = out_path / "ip/pinmux/data/autogen" |
Eunchan Kim | 436d224 | 2019-10-29 17:25:51 -0700 | [diff] [blame] | 319 | data_path.mkdir(parents=True, exist_ok=True) |
| 320 | |
| 321 | # Template path |
Weicai Yang | 5c02d78 | 2020-12-08 12:17:51 -0800 | [diff] [blame] | 322 | tpl_path = Path( |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 323 | __file__).resolve().parent / "../hw/ip/pinmux/data/pinmux.hjson.tpl" |
Michael Schaffner | 3452c71 | 2022-01-21 20:15:12 -0800 | [diff] [blame] | 324 | original_rtl_path = Path( |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 325 | __file__).resolve().parent / "../hw/ip/pinmux/rtl" |
Eunchan Kim | 436d224 | 2019-10-29 17:25:51 -0700 | [diff] [blame] | 326 | |
| 327 | # Generate register package and RTLs |
Eunchan Kim | ba970df | 2020-04-17 10:21:01 -0700 | [diff] [blame] | 328 | gencmd = ("// util/topgen.py -t hw/top_{topname}/data/top_{topname}.hjson " |
| 329 | "-o hw/top_{topname}/\n\n".format(topname=topname)) |
Eunchan Kim | 436d224 | 2019-10-29 17:25:51 -0700 | [diff] [blame] | 330 | |
| 331 | hjson_gen_path = data_path / "pinmux.hjson" |
| 332 | |
| 333 | out = StringIO() |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 334 | with tpl_path.open(mode="r", encoding="UTF-8") as fin: |
Eunchan Kim | 436d224 | 2019-10-29 17:25:51 -0700 | [diff] [blame] | 335 | hjson_tpl = Template(fin.read()) |
| 336 | try: |
Michael Schaffner | 6015796 | 2020-05-01 19:11:28 -0700 | [diff] [blame] | 337 | out = hjson_tpl.render( |
| 338 | n_mio_periph_in=n_mio_periph_in, |
| 339 | n_mio_periph_out=n_mio_periph_out, |
| 340 | n_mio_pads=n_mio_pads, |
| 341 | # each DIO has in, out and oe wires |
| 342 | # some of these have to be tied off in the |
| 343 | # top, depending on the type. |
| 344 | n_dio_periph_in=n_dio_pads, |
| 345 | n_dio_periph_out=n_dio_pads, |
| 346 | n_dio_pads=n_dio_pads, |
Michael Schaffner | 43ce8d5 | 2021-02-10 17:04:57 -0800 | [diff] [blame] | 347 | attr_dw=attr_dw, |
Michael Schaffner | 6015796 | 2020-05-01 19:11:28 -0700 | [diff] [blame] | 348 | n_wkup_detect=num_wkup_detect, |
Srikrishna Iyer | aea37dd | 2021-11-20 21:11:40 -0800 | [diff] [blame] | 349 | wkup_cnt_width=wkup_cnt_width) |
Eunchan Kim | 6599ba9 | 2020-04-13 15:27:16 -0700 | [diff] [blame] | 350 | except: # noqa: E722 |
Eunchan Kim | 436d224 | 2019-10-29 17:25:51 -0700 | [diff] [blame] | 351 | log.error(exceptions.text_error_template().render()) |
| 352 | log.info("PINMUX HJSON: %s" % out) |
| 353 | |
| 354 | if out == "": |
| 355 | log.error("Cannot generate pinmux HJSON") |
| 356 | return |
| 357 | |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 358 | with hjson_gen_path.open(mode="w", encoding="UTF-8") as fout: |
Eunchan Kim | 436d224 | 2019-10-29 17:25:51 -0700 | [diff] [blame] | 359 | fout.write(genhdr + gencmd + out) |
| 360 | |
Michael Schaffner | 3452c71 | 2022-01-21 20:15:12 -0800 | [diff] [blame] | 361 | # Generate reg file |
| 362 | generate_regfile_from_path(hjson_gen_path, rtl_path, original_rtl_path) |
Eunchan Kim | 436d224 | 2019-10-29 17:25:51 -0700 | [diff] [blame] | 363 | |
Michael Schaffner | 6015796 | 2020-05-01 19:11:28 -0700 | [diff] [blame] | 364 | |
Timothy Chen | f56c1b5 | 2020-04-28 17:00:43 -0700 | [diff] [blame] | 365 | def generate_clkmgr(top, cfg_path, out_path): |
| 366 | |
| 367 | # Target paths |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 368 | rtl_path = out_path / "ip/clkmgr/rtl/autogen" |
Timothy Chen | f56c1b5 | 2020-04-28 17:00:43 -0700 | [diff] [blame] | 369 | rtl_path.mkdir(parents=True, exist_ok=True) |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 370 | data_path = out_path / "ip/clkmgr/data/autogen" |
Timothy Chen | f56c1b5 | 2020-04-28 17:00:43 -0700 | [diff] [blame] | 371 | data_path.mkdir(parents=True, exist_ok=True) |
| 372 | |
| 373 | # Template paths |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 374 | hjson_tpl = cfg_path / "../ip/clkmgr/data/clkmgr.hjson.tpl" |
| 375 | rtl_tpl = cfg_path / "../ip/clkmgr/data/clkmgr.sv.tpl" |
| 376 | pkg_tpl = cfg_path / "../ip/clkmgr/data/clkmgr_pkg.sv.tpl" |
| 377 | original_rtl_path = cfg_path / "../ip/clkmgr/rtl" |
Timothy Chen | f56c1b5 | 2020-04-28 17:00:43 -0700 | [diff] [blame] | 378 | |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 379 | hjson_out = data_path / "clkmgr.hjson" |
| 380 | rtl_out = rtl_path / "clkmgr.sv" |
| 381 | pkg_out = rtl_path / "clkmgr_pkg.sv" |
Timothy Chen | f56c1b5 | 2020-04-28 17:00:43 -0700 | [diff] [blame] | 382 | |
| 383 | tpls = [hjson_tpl, rtl_tpl, pkg_tpl] |
| 384 | outputs = [hjson_out, rtl_out, pkg_out] |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 385 | names = ["clkmgr.hjson", "clkmgr.sv", "clkmgr_pkg.sv"] |
Timothy Chen | f56c1b5 | 2020-04-28 17:00:43 -0700 | [diff] [blame] | 386 | |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 387 | clocks = top["clocks"] |
Rupert Swarbrick | 127b109 | 2021-07-16 17:10:39 +0100 | [diff] [blame] | 388 | assert isinstance(clocks, Clocks) |
Timothy Chen | f56c1b5 | 2020-04-28 17:00:43 -0700 | [diff] [blame] | 389 | |
Rupert Swarbrick | 710e3c9 | 2021-07-13 15:28:59 +0100 | [diff] [blame] | 390 | typed_clocks = clocks.typed_clocks() |
| 391 | hint_names = typed_clocks.hint_names() |
Rupert Swarbrick | 56544b0 | 2021-06-17 14:20:40 +0100 | [diff] [blame] | 392 | |
Timothy Chen | f56c1b5 | 2020-04-28 17:00:43 -0700 | [diff] [blame] | 393 | for idx, tpl in enumerate(tpls): |
Sam Elliott | 6dd4e4a | 2020-07-30 18:52:22 +0100 | [diff] [blame] | 394 | out = "" |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 395 | with tpl.open(mode="r", encoding="UTF-8") as fin: |
Timothy Chen | f56c1b5 | 2020-04-28 17:00:43 -0700 | [diff] [blame] | 396 | tpl = Template(fin.read()) |
| 397 | try: |
| 398 | out = tpl.render(cfg=top, |
Rupert Swarbrick | ab5c149 | 2021-07-19 13:00:37 +0100 | [diff] [blame] | 399 | clocks=clocks, |
| 400 | typed_clocks=typed_clocks, |
Rupert Swarbrick | 710e3c9 | 2021-07-13 15:28:59 +0100 | [diff] [blame] | 401 | hint_names=hint_names) |
Timothy Chen | f56c1b5 | 2020-04-28 17:00:43 -0700 | [diff] [blame] | 402 | except: # noqa: E722 |
| 403 | log.error(exceptions.text_error_template().render()) |
| 404 | |
| 405 | if out == "": |
| 406 | log.error("Cannot generate {}".format(names[idx])) |
| 407 | return |
| 408 | |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 409 | with outputs[idx].open(mode="w", encoding="UTF-8") as fout: |
Timothy Chen | f56c1b5 | 2020-04-28 17:00:43 -0700 | [diff] [blame] | 410 | fout.write(genhdr + out) |
| 411 | |
| 412 | # Generate reg files |
Michael Schaffner | 3452c71 | 2022-01-21 20:15:12 -0800 | [diff] [blame] | 413 | generate_regfile_from_path(hjson_out, rtl_path, original_rtl_path) |
Eunchan Kim | 436d224 | 2019-10-29 17:25:51 -0700 | [diff] [blame] | 414 | |
Eunchan Kim | c75c6c1 | 2020-05-12 12:48:34 -0700 | [diff] [blame] | 415 | |
Timothy Chen | 4ba2531 | 2020-06-17 13:08:57 -0700 | [diff] [blame] | 416 | # generate pwrmgr |
| 417 | def generate_pwrmgr(top, out_path): |
| 418 | log.info("Generating pwrmgr") |
| 419 | |
Timothy Chen | 6faf4f1 | 2020-09-18 18:52:47 -0700 | [diff] [blame] | 420 | # Count number of wakeups |
Timothy Chen | 4ba2531 | 2020-06-17 13:08:57 -0700 | [diff] [blame] | 421 | n_wkups = len(top["wakeups"]) |
| 422 | log.info("Found {} wakeup signals".format(n_wkups)) |
| 423 | |
Timothy Chen | 6faf4f1 | 2020-09-18 18:52:47 -0700 | [diff] [blame] | 424 | # Count number of reset requests |
| 425 | n_rstreqs = len(top["reset_requests"]) |
| 426 | log.info("Found {} reset request signals".format(n_rstreqs)) |
| 427 | |
Timothy Chen | 4ba2531 | 2020-06-17 13:08:57 -0700 | [diff] [blame] | 428 | if n_wkups < 1: |
| 429 | n_wkups = 1 |
Eunchan Kim | 9191f26 | 2020-07-30 16:37:40 -0700 | [diff] [blame] | 430 | log.warning( |
Michael Schaffner | 52bce9f | 2021-07-13 15:08:08 -0700 | [diff] [blame] | 431 | "The design has no wakeup sources. Low power not supported.") |
| 432 | |
| 433 | if n_rstreqs < 1: |
| 434 | n_rstreqs = 1 |
Srikrishna Iyer | aea37dd | 2021-11-20 21:11:40 -0800 | [diff] [blame] | 435 | log.warning("The design has no reset request sources. " |
| 436 | "Reset requests are not supported.") |
Timothy Chen | 4ba2531 | 2020-06-17 13:08:57 -0700 | [diff] [blame] | 437 | |
| 438 | # Define target path |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 439 | rtl_path = out_path / "ip/pwrmgr/rtl/autogen" |
Timothy Chen | 4ba2531 | 2020-06-17 13:08:57 -0700 | [diff] [blame] | 440 | rtl_path.mkdir(parents=True, exist_ok=True) |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 441 | doc_path = out_path / "ip/pwrmgr/data/autogen" |
Timothy Chen | 4ba2531 | 2020-06-17 13:08:57 -0700 | [diff] [blame] | 442 | doc_path.mkdir(parents=True, exist_ok=True) |
| 443 | |
| 444 | # So, read template files from ip directory. |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 445 | tpl_path = Path(__file__).resolve().parent / "../hw/ip/pwrmgr/data" |
| 446 | hjson_tpl_path = tpl_path / "pwrmgr.hjson.tpl" |
| 447 | original_rtl_path = Path(__file__).resolve().parent / "../hw/ip/pwrmgr/rtl" |
Timothy Chen | 4ba2531 | 2020-06-17 13:08:57 -0700 | [diff] [blame] | 448 | |
| 449 | # Render and write out hjson |
| 450 | out = StringIO() |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 451 | with hjson_tpl_path.open(mode="r", encoding="UTF-8") as fin: |
Timothy Chen | 4ba2531 | 2020-06-17 13:08:57 -0700 | [diff] [blame] | 452 | hjson_tpl = Template(fin.read()) |
| 453 | try: |
Timothy Chen | bea7b6a | 2021-01-21 13:59:20 -0800 | [diff] [blame] | 454 | out = hjson_tpl.render(NumWkups=n_wkups, |
| 455 | Wkups=top["wakeups"], |
| 456 | NumRstReqs=n_rstreqs) |
Timothy Chen | 4ba2531 | 2020-06-17 13:08:57 -0700 | [diff] [blame] | 457 | |
| 458 | except: # noqa: E722 |
| 459 | log.error(exceptions.text_error_template().render()) |
| 460 | log.info("pwrmgr hjson: %s" % out) |
| 461 | |
| 462 | if out == "": |
| 463 | log.error("Cannot generate pwrmgr config file") |
| 464 | return |
| 465 | |
| 466 | hjson_path = doc_path / "pwrmgr.hjson" |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 467 | with hjson_path.open(mode="w", encoding="UTF-8") as fout: |
Timothy Chen | 4ba2531 | 2020-06-17 13:08:57 -0700 | [diff] [blame] | 468 | fout.write(genhdr + out) |
| 469 | |
| 470 | # Generate reg files |
Michael Schaffner | 3452c71 | 2022-01-21 20:15:12 -0800 | [diff] [blame] | 471 | generate_regfile_from_path(hjson_path, rtl_path, original_rtl_path) |
Timothy Chen | 4ba2531 | 2020-06-17 13:08:57 -0700 | [diff] [blame] | 472 | |
| 473 | |
Timothy Chen | d5b1c0f | 2020-08-11 14:10:40 -0700 | [diff] [blame] | 474 | # generate rstmgr |
| 475 | def generate_rstmgr(topcfg, out_path): |
| 476 | log.info("Generating rstmgr") |
| 477 | |
| 478 | # Define target path |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 479 | rtl_path = out_path / "ip/rstmgr/rtl/autogen" |
Timothy Chen | d5b1c0f | 2020-08-11 14:10:40 -0700 | [diff] [blame] | 480 | rtl_path.mkdir(parents=True, exist_ok=True) |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 481 | doc_path = out_path / "ip/rstmgr/data/autogen" |
Timothy Chen | d5b1c0f | 2020-08-11 14:10:40 -0700 | [diff] [blame] | 482 | doc_path.mkdir(parents=True, exist_ok=True) |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 483 | tpl_path = Path(__file__).resolve().parent / "../hw/ip/rstmgr/data" |
| 484 | original_rtl_path = Path(__file__).resolve().parent / "../hw/ip/rstmgr/rtl" |
Timothy Chen | d5b1c0f | 2020-08-11 14:10:40 -0700 | [diff] [blame] | 485 | |
| 486 | # Read template files from ip directory. |
| 487 | tpls = [] |
| 488 | outputs = [] |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 489 | names = ["rstmgr.hjson", "rstmgr.sv", "rstmgr_pkg.sv"] |
Timothy Chen | d5b1c0f | 2020-08-11 14:10:40 -0700 | [diff] [blame] | 490 | |
| 491 | for x in names: |
| 492 | tpls.append(tpl_path / Path(x + ".tpl")) |
| 493 | if "hjson" in x: |
| 494 | outputs.append(doc_path / Path(x)) |
| 495 | else: |
| 496 | outputs.append(rtl_path / Path(x)) |
| 497 | |
| 498 | # Parameters needed for generation |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 499 | reset_obj = topcfg["resets"] |
Timothy Chen | d5b1c0f | 2020-08-11 14:10:40 -0700 | [diff] [blame] | 500 | |
Timothy Chen | 63c6606 | 2021-07-24 00:25:22 -0700 | [diff] [blame] | 501 | # The original resets dict is transformed to the reset class |
| 502 | assert isinstance(reset_obj, Resets) |
| 503 | |
Timothy Chen | d5b1c0f | 2020-08-11 14:10:40 -0700 | [diff] [blame] | 504 | # unique clocks |
Timothy Chen | 7c3de6e | 2021-07-19 16:46:45 -0700 | [diff] [blame] | 505 | clks = reset_obj.get_clocks() |
Timothy Chen | d5b1c0f | 2020-08-11 14:10:40 -0700 | [diff] [blame] | 506 | |
| 507 | # resets sent to reset struct |
Timothy Chen | 7c3de6e | 2021-07-19 16:46:45 -0700 | [diff] [blame] | 508 | output_rsts = reset_obj.get_top_resets() |
Timothy Chen | d5b1c0f | 2020-08-11 14:10:40 -0700 | [diff] [blame] | 509 | |
| 510 | # sw controlled resets |
Timothy Chen | 7c3de6e | 2021-07-19 16:46:45 -0700 | [diff] [blame] | 511 | sw_rsts = reset_obj.get_sw_resets() |
Timothy Chen | d5b1c0f | 2020-08-11 14:10:40 -0700 | [diff] [blame] | 512 | |
| 513 | # leaf resets |
Timothy Chen | 7c3de6e | 2021-07-19 16:46:45 -0700 | [diff] [blame] | 514 | leaf_rsts = reset_obj.get_generated_resets() |
Timothy Chen | d5b1c0f | 2020-08-11 14:10:40 -0700 | [diff] [blame] | 515 | |
Timothy Chen | 6faf4f1 | 2020-09-18 18:52:47 -0700 | [diff] [blame] | 516 | # Number of reset requests |
| 517 | n_rstreqs = len(topcfg["reset_requests"]) |
| 518 | |
Timothy Chen | d5b1c0f | 2020-08-11 14:10:40 -0700 | [diff] [blame] | 519 | # Generate templated files |
| 520 | for idx, t in enumerate(tpls): |
| 521 | out = StringIO() |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 522 | with t.open(mode="r", encoding="UTF-8") as fin: |
Timothy Chen | d5b1c0f | 2020-08-11 14:10:40 -0700 | [diff] [blame] | 523 | tpl = Template(fin.read()) |
| 524 | try: |
| 525 | out = tpl.render(clks=clks, |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 526 | power_domains=topcfg["power"]["domains"], |
Timothy Chen | 6faf4f1 | 2020-09-18 18:52:47 -0700 | [diff] [blame] | 527 | num_rstreqs=n_rstreqs, |
Timothy Chen | d5b1c0f | 2020-08-11 14:10:40 -0700 | [diff] [blame] | 528 | sw_rsts=sw_rsts, |
| 529 | output_rsts=output_rsts, |
Timothy Chen | 4c8905e | 2020-08-26 10:34:33 -0700 | [diff] [blame] | 530 | leaf_rsts=leaf_rsts, |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 531 | export_rsts=topcfg["exported_rsts"], |
| 532 | reset_obj=topcfg["resets"]) |
Timothy Chen | d5b1c0f | 2020-08-11 14:10:40 -0700 | [diff] [blame] | 533 | |
| 534 | except: # noqa: E722 |
| 535 | log.error(exceptions.text_error_template().render()) |
| 536 | |
| 537 | if out == "": |
| 538 | log.error("Cannot generate {}".format(names[idx])) |
| 539 | return |
| 540 | |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 541 | with outputs[idx].open(mode="w", encoding="UTF-8") as fout: |
Timothy Chen | d5b1c0f | 2020-08-11 14:10:40 -0700 | [diff] [blame] | 542 | fout.write(genhdr + out) |
| 543 | |
| 544 | # Generate reg files |
| 545 | hjson_path = outputs[0] |
Michael Schaffner | 3452c71 | 2022-01-21 20:15:12 -0800 | [diff] [blame] | 546 | generate_regfile_from_path(hjson_path, rtl_path, original_rtl_path) |
Timothy Chen | d5b1c0f | 2020-08-11 14:10:40 -0700 | [diff] [blame] | 547 | |
| 548 | |
Timothy Chen | 1daf582 | 2020-10-26 17:28:15 -0700 | [diff] [blame] | 549 | # generate flash |
| 550 | def generate_flash(topcfg, out_path): |
| 551 | log.info("Generating flash") |
| 552 | |
| 553 | # Define target path |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 554 | rtl_path = out_path / "ip/flash_ctrl/rtl/autogen" |
Timothy Chen | 1daf582 | 2020-10-26 17:28:15 -0700 | [diff] [blame] | 555 | rtl_path.mkdir(parents=True, exist_ok=True) |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 556 | doc_path = out_path / "ip/flash_ctrl/data/autogen" |
Timothy Chen | 1daf582 | 2020-10-26 17:28:15 -0700 | [diff] [blame] | 557 | doc_path.mkdir(parents=True, exist_ok=True) |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 558 | tpl_path = Path(__file__).resolve().parent / "../hw/ip/flash_ctrl/data" |
Michael Schaffner | 3452c71 | 2022-01-21 20:15:12 -0800 | [diff] [blame] | 559 | original_rtl_path = Path( |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 560 | __file__).resolve().parent / "../hw/ip/flash_ctrl/rtl" |
Timothy Chen | 1daf582 | 2020-10-26 17:28:15 -0700 | [diff] [blame] | 561 | |
| 562 | # Read template files from ip directory. |
| 563 | tpls = [] |
| 564 | outputs = [] |
Srikrishna Iyer | aea37dd | 2021-11-20 21:11:40 -0800 | [diff] [blame] | 565 | names = [ |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 566 | "flash_ctrl.hjson", "flash_ctrl.sv", "flash_ctrl_pkg.sv", |
| 567 | "flash_ctrl_region_cfg.sv" |
Srikrishna Iyer | aea37dd | 2021-11-20 21:11:40 -0800 | [diff] [blame] | 568 | ] |
Timothy Chen | 1daf582 | 2020-10-26 17:28:15 -0700 | [diff] [blame] | 569 | |
| 570 | for x in names: |
| 571 | tpls.append(tpl_path / Path(x + ".tpl")) |
| 572 | if "hjson" in x: |
| 573 | outputs.append(doc_path / Path(x)) |
| 574 | else: |
| 575 | outputs.append(rtl_path / Path(x)) |
| 576 | |
| 577 | # Parameters needed for generation |
Srikrishna Iyer | aea37dd | 2021-11-20 21:11:40 -0800 | [diff] [blame] | 578 | flash_mems = [ |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 579 | module for module in topcfg["module"] if module["type"] == "flash_ctrl" |
Srikrishna Iyer | aea37dd | 2021-11-20 21:11:40 -0800 | [diff] [blame] | 580 | ] |
Timothy Chen | 1daf582 | 2020-10-26 17:28:15 -0700 | [diff] [blame] | 581 | if len(flash_mems) > 1: |
| 582 | log.error("This design does not currently support multiple flashes") |
| 583 | return |
| 584 | |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 585 | cfg = flash_mems[0]["memory"]["mem"]["config"] |
Timothy Chen | 1daf582 | 2020-10-26 17:28:15 -0700 | [diff] [blame] | 586 | |
| 587 | # Generate templated files |
| 588 | for idx, t in enumerate(tpls): |
| 589 | out = StringIO() |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 590 | with t.open(mode="r", encoding="UTF-8") as fin: |
Timothy Chen | 1daf582 | 2020-10-26 17:28:15 -0700 | [diff] [blame] | 591 | tpl = Template(fin.read()) |
| 592 | try: |
| 593 | out = tpl.render(cfg=cfg) |
| 594 | |
| 595 | except: # noqa: E722 |
| 596 | log.error(exceptions.text_error_template().render()) |
| 597 | |
| 598 | if out == "": |
| 599 | log.error("Cannot generate {}".format(names[idx])) |
| 600 | return |
| 601 | |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 602 | with outputs[idx].open(mode="w", encoding="UTF-8") as fout: |
Timothy Chen | 1daf582 | 2020-10-26 17:28:15 -0700 | [diff] [blame] | 603 | fout.write(genhdr + out) |
| 604 | |
| 605 | # Generate reg files |
| 606 | hjson_path = outputs[0] |
Michael Schaffner | 3452c71 | 2022-01-21 20:15:12 -0800 | [diff] [blame] | 607 | generate_regfile_from_path(hjson_path, rtl_path, original_rtl_path) |
Timothy Chen | 1daf582 | 2020-10-26 17:28:15 -0700 | [diff] [blame] | 608 | |
| 609 | |
Timothy Chen | 4c0e61f | 2021-11-11 14:32:53 -0800 | [diff] [blame] | 610 | def generate_top_only(top_only_dict, out_path, topname, alt_hjson_path): |
Timothy Chen | 322f254 | 2020-08-05 16:28:18 -0700 | [diff] [blame] | 611 | log.info("Generating top only modules") |
| 612 | |
Timothy Chen | 4c0e61f | 2021-11-11 14:32:53 -0800 | [diff] [blame] | 613 | for ip, reggen_only in top_only_dict.items(): |
| 614 | |
| 615 | if reggen_only and alt_hjson_path is not None: |
| 616 | hjson_dir = Path(alt_hjson_path) |
| 617 | else: |
| 618 | hjson_dir = Path(__file__).resolve( |
| 619 | ).parent / f"../hw/top_{topname}/ip/{ip}/data/" |
| 620 | |
| 621 | hjson_path = hjson_dir / f"{ip}.hjson" |
| 622 | |
Pirmin Vogel | fb8c447 | 2020-11-25 18:08:34 +0100 | [diff] [blame] | 623 | genrtl_dir = out_path / "ip/{}/rtl".format(ip) |
| 624 | genrtl_dir.mkdir(parents=True, exist_ok=True) |
Timothy Chen | 322f254 | 2020-08-05 16:28:18 -0700 | [diff] [blame] | 625 | log.info("Generating top modules {}, hjson: {}, output: {}".format( |
Pirmin Vogel | fb8c447 | 2020-11-25 18:08:34 +0100 | [diff] [blame] | 626 | ip, hjson_path, genrtl_dir)) |
Timothy Chen | 322f254 | 2020-08-05 16:28:18 -0700 | [diff] [blame] | 627 | |
| 628 | # Generate reg files |
Michael Schaffner | 3452c71 | 2022-01-21 20:15:12 -0800 | [diff] [blame] | 629 | generate_regfile_from_path(hjson_path, genrtl_dir) |
Timothy Chen | 322f254 | 2020-08-05 16:28:18 -0700 | [diff] [blame] | 630 | |
| 631 | |
Srikrishna Iyer | aea37dd | 2021-11-20 21:11:40 -0800 | [diff] [blame] | 632 | def generate_top_ral(top: Dict[str, object], name_to_block: Dict[str, IpBlock], |
| 633 | dv_base_names: List[str], out_path: str): |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 634 | # construct top ral block |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 635 | |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 636 | regwidth = int(top["datawidth"]) |
Rupert Swarbrick | 269bb3d | 2021-02-23 15:41:56 +0000 | [diff] [blame] | 637 | assert regwidth % 8 == 0 |
| 638 | addrsep = regwidth // 8 |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 639 | |
Rupert Swarbrick | 200d8b4 | 2021-03-08 12:32:11 +0000 | [diff] [blame] | 640 | # Generate a map from instance name to the block that it instantiates, |
| 641 | # together with a map of interface addresses. |
| 642 | inst_to_block = {} # type: Dict[str, str] |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 643 | if_addrs = {} # type: Dict[Tuple[str, Optional[str]], int] |
Timothy Chen | 62dabf7 | 2021-03-24 12:09:27 -0700 | [diff] [blame] | 644 | attrs = {} # type: Dict[str, str] |
Rupert Swarbrick | 200d8b4 | 2021-03-08 12:32:11 +0000 | [diff] [blame] | 645 | |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 646 | for module in top["module"]: |
| 647 | inst_name = module["name"] |
| 648 | block_name = module["type"] |
Rupert Swarbrick | 200d8b4 | 2021-03-08 12:32:11 +0000 | [diff] [blame] | 649 | block = name_to_block[block_name] |
Weicai Yang | cf02fd2 | 2021-03-22 20:49:45 -0700 | [diff] [blame] | 650 | if "attr" in module: |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 651 | if module["attr"] not in ["templated", "ipgen", "reggen_top", |
| 652 | "reggen_only"]: |
| 653 | raise ValueError("Unsupported value for attr field of {}: {!r}" |
Michael Schaffner | 74c4ff2 | 2021-03-30 15:43:46 -0700 | [diff] [blame] | 654 | .format(inst_name, module["attr"])) |
Weicai Yang | cf02fd2 | 2021-03-22 20:49:45 -0700 | [diff] [blame] | 655 | attrs[inst_name] = module["attr"] |
Rupert Swarbrick | 200d8b4 | 2021-03-08 12:32:11 +0000 | [diff] [blame] | 656 | |
| 657 | inst_to_block[inst_name] = block_name |
| 658 | for if_name in block.reg_blocks.keys(): |
| 659 | if_addr = int(module["base_addrs"][if_name], 0) |
| 660 | if_addrs[(inst_name, if_name)] = if_addr |
Rupert Swarbrick | bc2bc58 | 2021-02-09 13:30:37 +0000 | [diff] [blame] | 661 | |
Rupert Swarbrick | 269bb3d | 2021-02-23 15:41:56 +0000 | [diff] [blame] | 662 | # Collect up the memories to add |
Rupert Swarbrick | 1db6fcd | 2021-02-11 14:56:20 +0000 | [diff] [blame] | 663 | mems = [] |
Rupert Swarbrick | bc2bc58 | 2021-02-09 13:30:37 +0000 | [diff] [blame] | 664 | for item in list(top.get("memory", [])): |
Weicai Yang | 7d2e994 | 2021-07-27 23:16:24 -0700 | [diff] [blame] | 665 | mems.append(create_mem(item, addrsep, regwidth)) |
Rupert Swarbrick | bc2bc58 | 2021-02-09 13:30:37 +0000 | [diff] [blame] | 666 | |
Weicai Yang | 7d2e994 | 2021-07-27 23:16:24 -0700 | [diff] [blame] | 667 | # Top-level may override the mem setting. Store the new type to name_to_block |
| 668 | # If no other instance uses the orignal type, delete it |
| 669 | original_types = set() |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 670 | for module in top["module"]: |
| 671 | if "memory" in module.keys() and len(module["memory"]) > 0: |
| 672 | newtype = "{}_{}".format(module["type"], module["name"]) |
Weicai Yang | 7d2e994 | 2021-07-27 23:16:24 -0700 | [diff] [blame] | 673 | assert newtype not in name_to_block |
| 674 | |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 675 | block = deepcopy(name_to_block[module["type"]]) |
Weicai Yang | 7d2e994 | 2021-07-27 23:16:24 -0700 | [diff] [blame] | 676 | name_to_block[newtype] = block |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 677 | inst_to_block[module["name"]] = newtype |
Weicai Yang | 7d2e994 | 2021-07-27 23:16:24 -0700 | [diff] [blame] | 678 | |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 679 | original_types.add(module["type"]) |
Weicai Yang | 7d2e994 | 2021-07-27 23:16:24 -0700 | [diff] [blame] | 680 | |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 681 | for mem_name, item in module["memory"].items(): |
Weicai Yang | 7d2e994 | 2021-07-27 23:16:24 -0700 | [diff] [blame] | 682 | assert block.reg_blocks[mem_name] |
| 683 | assert len(block.reg_blocks[mem_name].windows) <= 1 |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 684 | item["name"] = mem_name |
Weicai Yang | 7d2e994 | 2021-07-27 23:16:24 -0700 | [diff] [blame] | 685 | |
| 686 | win = create_mem(item, addrsep, regwidth) |
| 687 | if len(block.reg_blocks[mem_name].windows) > 0: |
| 688 | blk_win = block.reg_blocks[mem_name].windows[0] |
| 689 | |
| 690 | # Top can only add new info for mem, shouldn't overwrite |
| 691 | # existing configuration |
| 692 | assert win.items == blk_win.items |
| 693 | assert win.byte_write == blk_win.byte_write |
| 694 | assert win.data_intg_passthru == blk_win.data_intg_passthru |
| 695 | |
| 696 | block.reg_blocks[mem_name].windows[0] = win |
| 697 | else: |
| 698 | block.reg_blocks[mem_name].windows.append(win) |
| 699 | |
| 700 | for t in original_types: |
| 701 | if t not in inst_to_block.values(): |
| 702 | del name_to_block[t] |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 703 | |
Weicai Yang | cf02fd2 | 2021-03-22 20:49:45 -0700 | [diff] [blame] | 704 | chip = Top(regwidth, name_to_block, inst_to_block, if_addrs, mems, attrs) |
Srikrishna Iyer | 1c0171a | 2019-10-29 11:59:46 -0700 | [diff] [blame] | 705 | |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 706 | # generate the top ral model with template |
Cindy Chen | e16009b | 2021-11-01 19:35:51 -0700 | [diff] [blame] | 707 | return gen_dv(chip, dv_base_names, str(out_path)) |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 708 | |
| 709 | |
Weicai Yang | 7d2e994 | 2021-07-27 23:16:24 -0700 | [diff] [blame] | 710 | def create_mem(item, addrsep, regwidth): |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 711 | byte_write = ("byte_write" in item and |
Weicai Yang | 7d2e994 | 2021-07-27 23:16:24 -0700 | [diff] [blame] | 712 | item["byte_write"].lower() == "true") |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 713 | data_intg_passthru = ("data_intg_passthru" in item and |
Weicai Yang | 7d2e994 | 2021-07-27 23:16:24 -0700 | [diff] [blame] | 714 | item["data_intg_passthru"].lower() == "true") |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 715 | size_in_bytes = int(item["size"], 0) |
Weicai Yang | 7d2e994 | 2021-07-27 23:16:24 -0700 | [diff] [blame] | 716 | num_regs = size_in_bytes // addrsep |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 717 | swaccess = access.SWAccess("top-level memory", item.get("swaccess", "rw")) |
Weicai Yang | 7d2e994 | 2021-07-27 23:16:24 -0700 | [diff] [blame] | 718 | |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 719 | return window.Window(name=item["name"], |
| 720 | desc="(generated from top-level)", |
Weicai Yang | 7d2e994 | 2021-07-27 23:16:24 -0700 | [diff] [blame] | 721 | unusual=False, |
| 722 | byte_write=byte_write, |
| 723 | data_intg_passthru=data_intg_passthru, |
| 724 | validbits=regwidth, |
| 725 | items=num_regs, |
| 726 | size_in_bytes=size_in_bytes, |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 727 | offset=int(item.get("base_addr", "0"), 0), |
Weicai Yang | 7d2e994 | 2021-07-27 23:16:24 -0700 | [diff] [blame] | 728 | swaccess=swaccess) |
| 729 | |
| 730 | |
Timothy Chen | 315b121 | 2021-01-22 11:08:03 -0800 | [diff] [blame] | 731 | def _process_top(topcfg, args, cfg_path, out_path, pass_idx): |
| 732 | # Create generated list |
| 733 | # These modules are generated through topgen |
Timothy Chen | 65ff510 | 2022-01-20 18:42:09 -0800 | [diff] [blame] | 734 | templated_list = lib.get_templated_modules(topcfg) |
| 735 | log.info("Templated list is {}".format(templated_list)) |
| 736 | |
| 737 | ipgen_list = lib.get_ipgen_modules(topcfg) |
| 738 | log.info("Ip gen list is {}".format(ipgen_list)) |
| 739 | |
| 740 | generated_list = templated_list + ipgen_list |
Timothy Chen | 315b121 | 2021-01-22 11:08:03 -0800 | [diff] [blame] | 741 | |
| 742 | # These modules are NOT generated but belong to a specific top |
| 743 | # and therefore not part of "hw/ip" |
Timothy Chen | 4c0e61f | 2021-11-11 14:32:53 -0800 | [diff] [blame] | 744 | top_only_dict = { |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 745 | module["type"]: lib.is_reggen_only(module) |
| 746 | for module in topcfg["module"] if lib.is_top_reggen(module) |
Timothy Chen | 4c0e61f | 2021-11-11 14:32:53 -0800 | [diff] [blame] | 747 | } |
| 748 | log.info("Filtered dict is {}".format(top_only_dict)) |
Timothy Chen | 315b121 | 2021-01-22 11:08:03 -0800 | [diff] [blame] | 749 | |
| 750 | topname = topcfg["name"] |
| 751 | |
| 752 | # Sweep the IP directory and gather the config files |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 753 | ip_dir = Path(__file__).parents[1] / "hw/ip" |
Timothy Chen | 315b121 | 2021-01-22 11:08:03 -0800 | [diff] [blame] | 754 | ips = search_ips(ip_dir) |
| 755 | |
| 756 | # exclude filtered IPs (to use top_${topname} one) and |
Timothy Chen | 4c0e61f | 2021-11-11 14:32:53 -0800 | [diff] [blame] | 757 | exclude_list = generated_list + list(top_only_dict.keys()) |
Timothy Chen | 315b121 | 2021-01-22 11:08:03 -0800 | [diff] [blame] | 758 | ips = [x for x in ips if not x.parents[1].name in exclude_list] |
| 759 | |
| 760 | # Hack alert |
| 761 | # Generate clkmgr.hjson here so that it can be included below |
| 762 | # Unlike other generated hjsons, clkmgr thankfully does not require |
| 763 | # ip.hjson information. All the information is embedded within |
| 764 | # the top hjson file |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 765 | topcfg["clocks"] = Clocks(topcfg["clocks"]) |
Rupert Swarbrick | 5aa249f | 2021-07-19 18:35:11 +0100 | [diff] [blame] | 766 | extract_clocks(topcfg) |
Timothy Chen | 315b121 | 2021-01-22 11:08:03 -0800 | [diff] [blame] | 767 | generate_clkmgr(topcfg, cfg_path, out_path) |
| 768 | |
| 769 | # It may require two passes to check if the module is needed. |
| 770 | # TODO: first run of topgen will fail due to the absent of rv_plic. |
| 771 | # It needs to run up to amend_interrupt in merge_top function |
| 772 | # then creates rv_plic.hjson then run xbar generation. |
| 773 | hjson_dir = Path(args.topcfg).parent |
| 774 | |
| 775 | for ip in generated_list: |
Timothy Chen | 744c3c0 | 2021-01-20 20:23:22 -0800 | [diff] [blame] | 776 | # For modules that are generated prior to gathering, we need to take it from |
Timothy Chen | 5302fff | 2021-01-22 14:36:54 -0800 | [diff] [blame] | 777 | # the output path. For modules not generated before, it may exist in a |
Timothy Chen | 744c3c0 | 2021-01-20 20:23:22 -0800 | [diff] [blame] | 778 | # pre-defined area already. |
Timothy Chen | 315b121 | 2021-01-22 11:08:03 -0800 | [diff] [blame] | 779 | log.info("Appending {}".format(ip)) |
Timothy Chen | 65ff510 | 2022-01-20 18:42:09 -0800 | [diff] [blame] | 780 | if ip in ipgen_list: |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 781 | ip_relpath = "ip_autogen" |
| 782 | desc_file_relpath = "data" |
Timothy Chen | 315b121 | 2021-01-22 11:08:03 -0800 | [diff] [blame] | 783 | else: |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 784 | ip_relpath = "ip" |
| 785 | desc_file_relpath = "data/autogen" |
Philipp Wagner | 270e28b | 2021-03-16 20:23:04 +0000 | [diff] [blame] | 786 | |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 787 | if ip == "clkmgr" or (pass_idx > 0): |
Srikrishna Iyer | aea37dd | 2021-11-20 21:11:40 -0800 | [diff] [blame] | 788 | ip_hjson = (Path(out_path) / ip_relpath / ip / desc_file_relpath / |
| 789 | f"{ip}.hjson") |
Philipp Wagner | 270e28b | 2021-03-16 20:23:04 +0000 | [diff] [blame] | 790 | else: |
Srikrishna Iyer | aea37dd | 2021-11-20 21:11:40 -0800 | [diff] [blame] | 791 | ip_hjson = (hjson_dir.parent / ip_relpath / ip / |
| 792 | desc_file_relpath / f"{ip}.hjson") |
Timothy Chen | 315b121 | 2021-01-22 11:08:03 -0800 | [diff] [blame] | 793 | ips.append(ip_hjson) |
| 794 | |
Timothy Chen | 4c0e61f | 2021-11-11 14:32:53 -0800 | [diff] [blame] | 795 | for ip, reggen_only in top_only_dict.items(): |
Timothy Chen | 315b121 | 2021-01-22 11:08:03 -0800 | [diff] [blame] | 796 | log.info("Appending {}".format(ip)) |
Timothy Chen | 4c0e61f | 2021-11-11 14:32:53 -0800 | [diff] [blame] | 797 | |
| 798 | if reggen_only and args.hjson_path: |
| 799 | ip_hjson = Path(args.hjson_path) / f"{ip}.hjson" |
| 800 | else: |
| 801 | ip_hjson = hjson_dir.parent / f"ip/{ip}/data/{ip}.hjson" |
| 802 | |
Timothy Chen | 315b121 | 2021-01-22 11:08:03 -0800 | [diff] [blame] | 803 | ips.append(ip_hjson) |
| 804 | |
| 805 | # load Hjson and pass validate from reggen |
| 806 | try: |
| 807 | ip_objs = [] |
Philipp Wagner | 270e28b | 2021-03-16 20:23:04 +0000 | [diff] [blame] | 808 | for ip_desc_file in ips: |
| 809 | ip_name = ip_desc_file.stem |
Timothy Chen | 315b121 | 2021-01-22 11:08:03 -0800 | [diff] [blame] | 810 | # Skip if it is not in the module list |
Philipp Wagner | 270e28b | 2021-03-16 20:23:04 +0000 | [diff] [blame] | 811 | if ip_name not in [ip["type"] for ip in topcfg["module"]]: |
Timothy Chen | 315b121 | 2021-01-22 11:08:03 -0800 | [diff] [blame] | 812 | log.info("Skip module %s as it isn't in the top module list" % |
Philipp Wagner | 270e28b | 2021-03-16 20:23:04 +0000 | [diff] [blame] | 813 | ip_name) |
Timothy Chen | 315b121 | 2021-01-22 11:08:03 -0800 | [diff] [blame] | 814 | continue |
| 815 | |
| 816 | # The auto-generated hjson might not yet exist. It will be created |
| 817 | # later, see generate_{ip_name}() calls below. For the initial |
Philipp Wagner | 270e28b | 2021-03-16 20:23:04 +0000 | [diff] [blame] | 818 | # validation, use the Hjson file with default values. |
| 819 | # TODO: All of this is a rather ugly hack that we need to get rid |
| 820 | # of as soon as we don't arbitrarily template IP description Hjson |
| 821 | # files any more. |
| 822 | if ip_name in generated_list and not ip_desc_file.is_file(): |
Timothy Chen | 65ff510 | 2022-01-20 18:42:09 -0800 | [diff] [blame] | 823 | if ip_name in ipgen_list: |
Philipp Wagner | 270e28b | 2021-03-16 20:23:04 +0000 | [diff] [blame] | 824 | log.info( |
| 825 | "To-be-auto-generated Hjson %s does not yet exist. " |
| 826 | "Falling back to the default configuration of template " |
Srikrishna Iyer | aea37dd | 2021-11-20 21:11:40 -0800 | [diff] [blame] | 827 | "%s for initial validation." % (ip_desc_file, ip_name)) |
Timothy Chen | 315b121 | 2021-01-22 11:08:03 -0800 | [diff] [blame] | 828 | |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 829 | tpl_path = SRCTREE_TOP / "hw/ip_templates" / ip_name |
Philipp Wagner | 270e28b | 2021-03-16 20:23:04 +0000 | [diff] [blame] | 830 | ip_template = IpTemplate.from_template_path(tpl_path) |
| 831 | ip_config = IpConfig(ip_template.params, |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 832 | f"top_{topname}_{ip_name}") |
Philipp Wagner | 270e28b | 2021-03-16 20:23:04 +0000 | [diff] [blame] | 833 | |
Philipp Wagner | 092b755 | 2021-10-27 21:34:09 +0100 | [diff] [blame] | 834 | try: |
| 835 | ip_desc = IpDescriptionOnlyRenderer( |
| 836 | ip_template, ip_config).render() |
| 837 | except TemplateRenderError as e: |
| 838 | log.error(e.verbose_str()) |
| 839 | sys.exit(1) |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 840 | s = "default description of IP template {}".format(ip_name) |
Philipp Wagner | 270e28b | 2021-03-16 20:23:04 +0000 | [diff] [blame] | 841 | ip_objs.append(IpBlock.from_text(ip_desc, [], s)) |
| 842 | else: |
| 843 | # TODO: Remove this block as soon as all IP templates use |
| 844 | # ipgen. |
| 845 | template_hjson_file = ip_dir / "{}/data/{}.hjson".format( |
| 846 | ip_name, ip_name) |
| 847 | log.info( |
| 848 | "To-be-auto-generated Hjson %s does not yet exist. " |
| 849 | "Falling back to Hjson description file %s shipped " |
| 850 | "with the IP template for initial validation." % |
| 851 | (ip_desc_file, template_hjson_file)) |
| 852 | |
| 853 | ip_objs.append( |
| 854 | IpBlock.from_path(str(template_hjson_file), [])) |
| 855 | else: |
| 856 | ip_objs.append(IpBlock.from_path(str(ip_desc_file), [])) |
Timothy Chen | 315b121 | 2021-01-22 11:08:03 -0800 | [diff] [blame] | 857 | |
| 858 | except ValueError: |
| 859 | raise SystemExit(sys.exc_info()[1]) |
| 860 | |
Rupert Swarbrick | 5aa249f | 2021-07-19 18:35:11 +0100 | [diff] [blame] | 861 | name_to_block = {} # type: Dict[str, IpBlock] |
| 862 | for block in ip_objs: |
| 863 | lblock = block.name.lower() |
| 864 | assert lblock not in name_to_block |
| 865 | name_to_block[lblock] = block |
| 866 | |
| 867 | connect_clocks(topcfg, name_to_block) |
| 868 | |
Timothy Chen | 315b121 | 2021-01-22 11:08:03 -0800 | [diff] [blame] | 869 | # Read the crossbars under the top directory |
| 870 | xbar_objs = get_hjsonobj_xbars(hjson_dir) |
| 871 | |
| 872 | log.info("Detected crossbars: %s" % |
| 873 | (", ".join([x["name"] for x in xbar_objs]))) |
| 874 | |
| 875 | # If specified, override the seed for random netlist constant computation. |
| 876 | if args.rnd_cnst_seed: |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 877 | log.warning("Commandline override of rnd_cnst_seed with {}.".format( |
Timothy Chen | 315b121 | 2021-01-22 11:08:03 -0800 | [diff] [blame] | 878 | args.rnd_cnst_seed)) |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 879 | topcfg["rnd_cnst_seed"] = args.rnd_cnst_seed |
Timothy Chen | 315b121 | 2021-01-22 11:08:03 -0800 | [diff] [blame] | 880 | # Otherwise, we either take it from the top_{topname}.hjson if present, or |
| 881 | # randomly generate a new seed if not. |
| 882 | else: |
| 883 | random.seed() |
| 884 | new_seed = random.getrandbits(64) |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 885 | if topcfg.setdefault("rnd_cnst_seed", new_seed) == new_seed: |
Timothy Chen | 315b121 | 2021-01-22 11:08:03 -0800 | [diff] [blame] | 886 | log.warning( |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 887 | "No rnd_cnst_seed specified, setting to {}.".format(new_seed)) |
Timothy Chen | 315b121 | 2021-01-22 11:08:03 -0800 | [diff] [blame] | 888 | |
| 889 | topcfg, error = validate_top(topcfg, ip_objs, xbar_objs) |
| 890 | if error != 0: |
| 891 | raise SystemExit("Error occured while validating top.hjson") |
| 892 | |
Rupert Swarbrick | eb619e6 | 2021-03-05 15:01:54 +0000 | [diff] [blame] | 893 | completecfg = merge_top(topcfg, name_to_block, xbar_objs) |
Timothy Chen | 315b121 | 2021-01-22 11:08:03 -0800 | [diff] [blame] | 894 | |
Timothy Chen | 744c3c0 | 2021-01-20 20:23:22 -0800 | [diff] [blame] | 895 | # Generate flash controller and flash memory |
| 896 | generate_flash(topcfg, out_path) |
| 897 | |
Timothy Chen | 315b121 | 2021-01-22 11:08:03 -0800 | [diff] [blame] | 898 | # Generate PLIC |
| 899 | if not args.no_plic and \ |
| 900 | not args.alert_handler_only and \ |
| 901 | not args.xbar_only: |
| 902 | generate_plic(completecfg, out_path) |
| 903 | if args.plic_only: |
| 904 | sys.exit() |
| 905 | |
Michael Schaffner | 4fe7846 | 2021-09-28 17:06:07 -0700 | [diff] [blame] | 906 | # Create Alert Handler LPGs before |
| 907 | # generating the Alert Handler |
| 908 | create_alert_lpgs(topcfg, name_to_block) |
| 909 | |
Timothy Chen | 315b121 | 2021-01-22 11:08:03 -0800 | [diff] [blame] | 910 | # Generate Alert Handler |
| 911 | if not args.xbar_only: |
| 912 | generate_alert_handler(completecfg, out_path) |
| 913 | if args.alert_handler_only: |
| 914 | sys.exit() |
| 915 | |
| 916 | # Generate Pinmux |
Michael Schaffner | 43ce8d5 | 2021-02-10 17:04:57 -0800 | [diff] [blame] | 917 | generate_pinmux(completecfg, out_path) |
Timothy Chen | 315b121 | 2021-01-22 11:08:03 -0800 | [diff] [blame] | 918 | |
| 919 | # Generate Pwrmgr |
| 920 | generate_pwrmgr(completecfg, out_path) |
| 921 | |
| 922 | # Generate rstmgr |
| 923 | generate_rstmgr(completecfg, out_path) |
| 924 | |
Timothy Chen | 315b121 | 2021-01-22 11:08:03 -0800 | [diff] [blame] | 925 | # Generate top only modules |
| 926 | # These modules are not templated, but are not in hw/ip |
Timothy Chen | 4c0e61f | 2021-11-11 14:32:53 -0800 | [diff] [blame] | 927 | generate_top_only(top_only_dict, out_path, topname, args.hjson_path) |
Timothy Chen | 315b121 | 2021-01-22 11:08:03 -0800 | [diff] [blame] | 928 | |
Rupert Swarbrick | eb619e6 | 2021-03-05 15:01:54 +0000 | [diff] [blame] | 929 | return completecfg, name_to_block |
Timothy Chen | 315b121 | 2021-01-22 11:08:03 -0800 | [diff] [blame] | 930 | |
| 931 | |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 932 | def main(): |
| 933 | parser = argparse.ArgumentParser(prog="topgen") |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 934 | parser.add_argument("--topcfg", |
| 935 | "-t", |
Eunchan Kim | c6a6a3b | 2019-09-12 14:27:43 -0700 | [diff] [blame] | 936 | required=True, |
| 937 | help="`top_{name}.hjson` file.") |
Eunchan Kim | 632c6f7 | 2019-09-30 11:11:51 -0700 | [diff] [blame] | 938 | parser.add_argument( |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 939 | "--outdir", |
| 940 | "-o", |
| 941 | help="""Target TOP directory. |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 942 | Module is created under rtl/. (default: dir(topcfg)/..) |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 943 | """) # yapf: disable |
Timothy Chen | 4c0e61f | 2021-11-11 14:32:53 -0800 | [diff] [blame] | 944 | parser.add_argument( |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 945 | "--hjson-path", |
| 946 | help=""" |
Timothy Chen | 4c0e61f | 2021-11-11 14:32:53 -0800 | [diff] [blame] | 947 | If defined, topgen uses supplied path to search for ip hjson. |
| 948 | This applies only to ip's with the `reggen_only` attribute. |
| 949 | If an hjson is located both in the conventional path and the alternate |
| 950 | path, the alternate path has priority. |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 951 | """) |
| 952 | parser.add_argument("--verbose", "-v", action="store_true", help="Verbose") |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 953 | |
| 954 | # Generator options: 'no' series. cannot combined with 'only' series |
| 955 | parser.add_argument( |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 956 | "--no-top", |
| 957 | action="store_true", |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 958 | help="If defined, topgen doesn't generate top_{name} RTLs.") |
| 959 | parser.add_argument( |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 960 | "--no-xbar", |
| 961 | action="store_true", |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 962 | help="If defined, topgen doesn't generate crossbar RTLs.") |
| 963 | parser.add_argument( |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 964 | "--no-plic", |
| 965 | action="store_true", |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 966 | help="If defined, topgen doesn't generate the interrup controller RTLs." |
| 967 | ) |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 968 | |
| 969 | # Generator options: 'only' series. cannot combined with 'no' series |
| 970 | parser.add_argument( |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 971 | "--top-only", |
| 972 | action="store_true", |
Eunchan Kim | 6599ba9 | 2020-04-13 15:27:16 -0700 | [diff] [blame] | 973 | help="If defined, the tool generates top RTL only") # yapf:disable |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 974 | parser.add_argument( |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 975 | "--xbar-only", |
| 976 | action="store_true", |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 977 | help="If defined, the tool generates crossbar RTLs only") |
| 978 | parser.add_argument( |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 979 | "--plic-only", |
| 980 | action="store_true", |
Philipp Wagner | 14a3fee | 2019-11-21 10:07:02 +0000 | [diff] [blame] | 981 | help="If defined, the tool generates RV_PLIC RTL and Hjson only") |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 982 | parser.add_argument( |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 983 | "--alert-handler-only", |
| 984 | action="store_true", |
Michael Schaffner | 666dde1 | 2019-10-25 11:57:54 -0700 | [diff] [blame] | 985 | help="If defined, the tool generates alert handler hjson only") |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 986 | # Generator options: generate dv ral model |
| 987 | parser.add_argument( |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 988 | "--top_ral", |
| 989 | "-r", |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 990 | default=False, |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 991 | action="store_true", |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 992 | help="If set, the tool generates top level RAL model for DV") |
Srikrishna Iyer | aea37dd | 2021-11-20 21:11:40 -0800 | [diff] [blame] | 993 | parser.add_argument( |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 994 | "--dv-base-names", |
Srikrishna Iyer | aea37dd | 2021-11-20 21:11:40 -0800 | [diff] [blame] | 995 | nargs="+", |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 996 | help="Names or prefix for the DV register classes from which " |
| 997 | "the register models are derived.") |
Michael Schaffner | 7b0807d | 2020-10-27 19:54:52 -0700 | [diff] [blame] | 998 | # Generator options for compile time random netlist constants |
Weicai Yang | a667036 | 2020-11-24 17:19:52 -0800 | [diff] [blame] | 999 | parser.add_argument( |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 1000 | "--rnd_cnst_seed", |
Weicai Yang | a667036 | 2020-11-24 17:19:52 -0800 | [diff] [blame] | 1001 | type=int, |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 1002 | metavar="<seed>", |
| 1003 | help="Custom seed for RNG to compute netlist constants.") |
Srikrishna Iyer | e42c8f6 | 2021-08-20 01:06:40 -0700 | [diff] [blame] | 1004 | # Miscellaneous: only return the list of blocks and exit. |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 1005 | parser.add_argument("--get_blocks", |
Srikrishna Iyer | e42c8f6 | 2021-08-20 01:06:40 -0700 | [diff] [blame] | 1006 | default=False, |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 1007 | action="store_true", |
Srikrishna Iyer | e42c8f6 | 2021-08-20 01:06:40 -0700 | [diff] [blame] | 1008 | help="Only return the list of blocks and exit.") |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 1009 | |
| 1010 | args = parser.parse_args() |
| 1011 | |
| 1012 | # check combinations |
| 1013 | if args.top_ral: |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 1014 | args.no_top = True |
| 1015 | |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 1016 | if (args.no_top or args.no_xbar or |
| 1017 | args.no_plic) and (args.top_only or args.xbar_only or |
Michael Schaffner | 666dde1 | 2019-10-25 11:57:54 -0700 | [diff] [blame] | 1018 | args.plic_only or args.alert_handler_only): |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 1019 | log.error( |
| 1020 | "'no' series options cannot be used with 'only' series options") |
| 1021 | raise SystemExit(sys.exc_info()[1]) |
| 1022 | |
Srikrishna Iyer | e42c8f6 | 2021-08-20 01:06:40 -0700 | [diff] [blame] | 1023 | # Don't print warnings when querying the list of blocks. |
Srikrishna Iyer | aea37dd | 2021-11-20 21:11:40 -0800 | [diff] [blame] | 1024 | log_level = (log.ERROR |
| 1025 | if args.get_blocks else log.DEBUG if args.verbose else None) |
Rupert Swarbrick | 9bfdaab | 2021-08-27 16:16:04 +0100 | [diff] [blame] | 1026 | |
Srikrishna Iyer | e42c8f6 | 2021-08-20 01:06:40 -0700 | [diff] [blame] | 1027 | log.basicConfig(format="%(levelname)s: %(message)s", level=log_level) |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 1028 | |
| 1029 | if not args.outdir: |
| 1030 | outdir = Path(args.topcfg).parent / ".." |
| 1031 | log.info("TOP directory not given. Use %s", (outdir)) |
| 1032 | elif not Path(args.outdir).is_dir(): |
| 1033 | log.error("'--outdir' should point to writable directory") |
| 1034 | raise SystemExit(sys.exc_info()[1]) |
| 1035 | else: |
| 1036 | outdir = Path(args.outdir) |
| 1037 | |
Timothy Chen | 4c0e61f | 2021-11-11 14:32:53 -0800 | [diff] [blame] | 1038 | if args.hjson_path is not None: |
Srikrishna Iyer | 934ef17 | 2021-11-20 21:07:17 -0800 | [diff] [blame] | 1039 | log.info(f"Alternate hjson path is {args.hjson_path}") |
Timothy Chen | 4c0e61f | 2021-11-11 14:32:53 -0800 | [diff] [blame] | 1040 | |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 1041 | out_path = Path(outdir) |
Timothy Chen | f56c1b5 | 2020-04-28 17:00:43 -0700 | [diff] [blame] | 1042 | cfg_path = Path(args.topcfg).parents[1] |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 1043 | |
Eunchan Kim | 66f7ae2 | 2020-08-03 23:24:53 -0700 | [diff] [blame] | 1044 | try: |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 1045 | with open(args.topcfg, "r") as ftop: |
Eunchan Kim | 66f7ae2 | 2020-08-03 23:24:53 -0700 | [diff] [blame] | 1046 | topcfg = hjson.load(ftop, |
| 1047 | use_decimal=True, |
| 1048 | object_pairs_hook=OrderedDict) |
| 1049 | except ValueError: |
| 1050 | raise SystemExit(sys.exc_info()[1]) |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 1051 | |
Timothy Chen | 315b121 | 2021-01-22 11:08:03 -0800 | [diff] [blame] | 1052 | # TODO, long term, the levels of dependency should be automatically determined instead |
| 1053 | # of hardcoded. The following are a few examples: |
| 1054 | # Example 1: pinmux depends on amending all modules before calculating the correct number of |
| 1055 | # pins. |
| 1056 | # This would be 1 level of dependency and require 2 passes. |
| 1057 | # Example 2: pinmux depends on amending all modules, and pwrmgr depends on pinmux generation to |
| 1058 | # know correct number of wakeups. This would be 2 levels of dependency and require 3 |
| 1059 | # passes. |
| 1060 | # |
| 1061 | # How does mulit-pass work? |
| 1062 | # In example 1, the first pass gathers all modules and merges them. However, the merge process |
| 1063 | # uses a stale pinmux. The correct pinmux is then generated using the merged configuration. The |
| 1064 | # second pass now merges all the correct modules (including the generated pinmux) and creates |
| 1065 | # the final merged config. |
| 1066 | # |
| 1067 | # In example 2, the first pass gathers all modules and merges them. However, the merge process |
| 1068 | # uses a stale pinmux and pwrmgr. The correct pinmux is then generated using the merged |
| 1069 | # configuration. However, since pwrmgr is dependent on this new pinmux, it is still generated |
| 1070 | # incorrectly. The second pass merge now has an updated pinmux but stale pwrmgr. The correct |
| 1071 | # pwrmgr can now be generated. The final pass then merges all the correct modules and creates |
| 1072 | # the final configuration. |
| 1073 | # |
| 1074 | # This fix is related to #2083 |
| 1075 | process_dependencies = 1 |
Philipp Wagner | f9ee797 | 2021-10-28 16:21:02 +0100 | [diff] [blame] | 1076 | |
| 1077 | # topgen generates IP blocks and associated Hjson configuration in multiple |
| 1078 | # steps. After each step, the IP Hjson configuration is read back and then |
| 1079 | # combined into the toplevel configuration. To generate the chip-level RAL, |
| 1080 | # we need to run the full generation step, but ultimately only care about |
| 1081 | # the toplevel configuration (a single Hjson file). Since we don't have a |
| 1082 | # better way at the moment dump all output into a temporary directory, and |
| 1083 | # delete it after the fact, retaining only the toplevel configuration. |
| 1084 | if args.top_ral: |
| 1085 | out_path_gen = Path(tempfile.mkdtemp()) |
| 1086 | else: |
| 1087 | out_path_gen = out_path |
| 1088 | |
Timothy Chen | 315b121 | 2021-01-22 11:08:03 -0800 | [diff] [blame] | 1089 | for pass_idx in range(process_dependencies + 1): |
| 1090 | log.debug("Generation pass {}".format(pass_idx)) |
| 1091 | if pass_idx < process_dependencies: |
| 1092 | cfg_copy = deepcopy(topcfg) |
Philipp Wagner | f9ee797 | 2021-10-28 16:21:02 +0100 | [diff] [blame] | 1093 | _process_top(cfg_copy, args, cfg_path, out_path_gen, pass_idx) |
Timothy Chen | 315b121 | 2021-01-22 11:08:03 -0800 | [diff] [blame] | 1094 | else: |
Philipp Wagner | f9ee797 | 2021-10-28 16:21:02 +0100 | [diff] [blame] | 1095 | completecfg, name_to_block = _process_top(topcfg, args, cfg_path, |
| 1096 | out_path_gen, pass_idx) |
Timothy Chen | f56c1b5 | 2020-04-28 17:00:43 -0700 | [diff] [blame] | 1097 | |
Eunchan Kim | 66f7ae2 | 2020-08-03 23:24:53 -0700 | [diff] [blame] | 1098 | topname = topcfg["name"] |
Eunchan Kim | ba970df | 2020-04-17 10:21:01 -0700 | [diff] [blame] | 1099 | |
Philipp Wagner | f9ee797 | 2021-10-28 16:21:02 +0100 | [diff] [blame] | 1100 | # Create the chip-level RAL only |
| 1101 | if args.top_ral: |
| 1102 | # See above: we only need `completeconfig` and `name_to_block`, not all |
| 1103 | # the other files (e.g. RTL files) generated through topgen. |
| 1104 | shutil.rmtree(out_path_gen, ignore_errors=True) |
| 1105 | |
| 1106 | exit_code = generate_top_ral(completecfg, name_to_block, |
Cindy Chen | e16009b | 2021-11-01 19:35:51 -0700 | [diff] [blame] | 1107 | args.dv_base_names, out_path) |
Philipp Wagner | f9ee797 | 2021-10-28 16:21:02 +0100 | [diff] [blame] | 1108 | sys.exit(exit_code) |
| 1109 | |
Srikrishna Iyer | e42c8f6 | 2021-08-20 01:06:40 -0700 | [diff] [blame] | 1110 | if args.get_blocks: |
| 1111 | print("\n".join(name_to_block.keys())) |
| 1112 | sys.exit(0) |
| 1113 | |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 1114 | # Generate xbars |
| 1115 | if not args.no_xbar or args.xbar_only: |
| 1116 | generate_xbars(completecfg, out_path) |
| 1117 | |
Eunchan Kim | e0d37fe | 2020-08-03 12:05:21 -0700 | [diff] [blame] | 1118 | # All IPs are generated. Connect phase now |
Eunchan Kim | 2c813b4 | 2020-08-03 16:22:56 -0700 | [diff] [blame] | 1119 | # Find {memory, module} <-> {xbar} connections first. |
Rupert Swarbrick | a5e687f | 2021-03-01 11:51:41 +0000 | [diff] [blame] | 1120 | im.autoconnect(completecfg, name_to_block) |
Eunchan Kim | 2c813b4 | 2020-08-03 16:22:56 -0700 | [diff] [blame] | 1121 | |
| 1122 | # Generic Inter-module connection |
| 1123 | im.elab_intermodule(completecfg) |
Eunchan Kim | e0d37fe | 2020-08-03 12:05:21 -0700 | [diff] [blame] | 1124 | |
Eunchan Kim | e0d37fe | 2020-08-03 12:05:21 -0700 | [diff] [blame] | 1125 | # Generate top.gen.hjson right before rendering |
Philipp Wagner | fb443ab | 2021-03-05 11:10:28 +0000 | [diff] [blame] | 1126 | genhjson_dir = out_path / "data/autogen" |
Pirmin Vogel | 71bfd9b | 2020-11-24 17:28:20 +0100 | [diff] [blame] | 1127 | genhjson_dir.mkdir(parents=True, exist_ok=True) |
| 1128 | genhjson_path = genhjson_dir / ("top_%s.gen.hjson" % completecfg["name"]) |
Michael Schaffner | 7b0807d | 2020-10-27 19:54:52 -0700 | [diff] [blame] | 1129 | |
| 1130 | # Header for HJSON |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 1131 | gencmd = """// |
Michael Schaffner | 7b0807d | 2020-10-27 19:54:52 -0700 | [diff] [blame] | 1132 | // util/topgen.py -t hw/top_{topname}/data/top_{topname}.hjson \\ |
| 1133 | // -o hw/top_{topname}/ \\ |
| 1134 | // --hjson-only \\ |
| 1135 | // --rnd_cnst_seed {seed} |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 1136 | """.format(topname=topname, seed=completecfg["rnd_cnst_seed"]) |
Eunchan Kim | e0d37fe | 2020-08-03 12:05:21 -0700 | [diff] [blame] | 1137 | |
Eunchan Kim | 66f7ae2 | 2020-08-03 23:24:53 -0700 | [diff] [blame] | 1138 | genhjson_path.write_text(genhdr + gencmd + |
Pirmin Vogel | 432474a | 2022-04-22 17:06:40 +0200 | [diff] [blame] | 1139 | hjson.dumps(completecfg, for_json=True) + '\n') |
Eunchan Kim | e0d37fe | 2020-08-03 12:05:21 -0700 | [diff] [blame] | 1140 | |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 1141 | if not args.no_top or args.top_only: |
Srikrishna Iyer | aea37dd | 2021-11-20 21:11:40 -0800 | [diff] [blame] | 1142 | |
| 1143 | def render_template(template_path: str, rendered_path: Path, |
| 1144 | **other_info): |
Rupert Swarbrick | eb619e6 | 2021-03-05 15:01:54 +0000 | [diff] [blame] | 1145 | template_contents = generate_top(completecfg, name_to_block, |
Philipp Wagner | fb443ab | 2021-03-05 11:10:28 +0000 | [diff] [blame] | 1146 | str(template_path), **other_info) |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 1147 | |
Philipp Wagner | fb443ab | 2021-03-05 11:10:28 +0000 | [diff] [blame] | 1148 | rendered_path.parent.mkdir(exist_ok=True, parents=True) |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 1149 | with rendered_path.open(mode="w", encoding="UTF-8") as fout: |
Sam Elliott | 7e36bd7 | 2020-04-22 14:05:49 +0100 | [diff] [blame] | 1150 | fout.write(template_contents) |
Eunchan Kim | 436d224 | 2019-10-29 17:25:51 -0700 | [diff] [blame] | 1151 | |
Michael Schaffner | 7b0807d | 2020-10-27 19:54:52 -0700 | [diff] [blame] | 1152 | # Header for SV files |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 1153 | gencmd = warnhdr + """// |
Michael Schaffner | 7b0807d | 2020-10-27 19:54:52 -0700 | [diff] [blame] | 1154 | // util/topgen.py -t hw/top_{topname}/data/top_{topname}.hjson \\ |
Michael Schaffner | 7b0807d | 2020-10-27 19:54:52 -0700 | [diff] [blame] | 1155 | // -o hw/top_{topname}/ \\ |
| 1156 | // --rnd_cnst_seed {seed} |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 1157 | """.format(topname=topname, seed=topcfg["rnd_cnst_seed"]) |
Michael Schaffner | 7b0807d | 2020-10-27 19:54:52 -0700 | [diff] [blame] | 1158 | |
Sam Elliott | 7e36bd7 | 2020-04-22 14:05:49 +0100 | [diff] [blame] | 1159 | # SystemVerilog Top: |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 1160 | # "toplevel.sv.tpl" -> "rtl/autogen/top_{topname}.sv" |
Philipp Wagner | fb443ab | 2021-03-05 11:10:28 +0000 | [diff] [blame] | 1161 | render_template(TOPGEN_TEMPLATE_PATH / "toplevel.sv.tpl", |
| 1162 | out_path / f"rtl/autogen/top_{topname}.sv", |
| 1163 | gencmd=gencmd) |
Eunchan Kim | 436d224 | 2019-10-29 17:25:51 -0700 | [diff] [blame] | 1164 | |
Michael Schaffner | 74c4ff2 | 2021-03-30 15:43:46 -0700 | [diff] [blame] | 1165 | # Multiple chip-levels (ASIC, FPGA, Verilator, etc) |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 1166 | for target in topcfg["targets"]: |
| 1167 | target_name = target["name"] |
Michael Schaffner | 74c4ff2 | 2021-03-30 15:43:46 -0700 | [diff] [blame] | 1168 | render_template(TOPGEN_TEMPLATE_PATH / "chiplevel.sv.tpl", |
Srikrishna Iyer | aea37dd | 2021-11-20 21:11:40 -0800 | [diff] [blame] | 1169 | out_path / |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 1170 | f"rtl/autogen/chip_{topname}_{target_name}.sv", |
Michael Schaffner | 74c4ff2 | 2021-03-30 15:43:46 -0700 | [diff] [blame] | 1171 | gencmd=gencmd, |
| 1172 | target=target) |
| 1173 | |
Srikrishna Iyer | d7bed87 | 2020-10-14 11:51:10 -0700 | [diff] [blame] | 1174 | # The C / SV file needs some complex information, so we initialize this |
Sam Elliott | d179047 | 2020-07-24 23:25:10 +0100 | [diff] [blame] | 1175 | # object to store it. |
Srikrishna Iyer | 689981e | 2021-09-16 12:12:37 -0700 | [diff] [blame] | 1176 | c_helper = TopGenCTest(completecfg, name_to_block) |
Sam Elliott | 7e36bd7 | 2020-04-22 14:05:49 +0100 | [diff] [blame] | 1177 | |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 1178 | # "toplevel_pkg.sv.tpl" -> "rtl/autogen/top_{topname}_pkg.sv" |
Philipp Wagner | fb443ab | 2021-03-05 11:10:28 +0000 | [diff] [blame] | 1179 | render_template(TOPGEN_TEMPLATE_PATH / "toplevel_pkg.sv.tpl", |
| 1180 | out_path / f"rtl/autogen/top_{topname}_pkg.sv", |
Michael Schaffner | 7b0807d | 2020-10-27 19:54:52 -0700 | [diff] [blame] | 1181 | helper=c_helper, |
Weicai Yang | a667036 | 2020-11-24 17:19:52 -0800 | [diff] [blame] | 1182 | gencmd=gencmd) |
Michael Schaffner | 7b0807d | 2020-10-27 19:54:52 -0700 | [diff] [blame] | 1183 | |
| 1184 | # compile-time random netlist constants |
Philipp Wagner | fb443ab | 2021-03-05 11:10:28 +0000 | [diff] [blame] | 1185 | render_template(TOPGEN_TEMPLATE_PATH / "toplevel_rnd_cnst_pkg.sv.tpl", |
Srikrishna Iyer | aea37dd | 2021-11-20 21:11:40 -0800 | [diff] [blame] | 1186 | out_path / |
| 1187 | f"rtl/autogen/top_{topname}_rnd_cnst_pkg.sv", |
Philipp Wagner | fb443ab | 2021-03-05 11:10:28 +0000 | [diff] [blame] | 1188 | gencmd=gencmd) |
Srikrishna Iyer | d7bed87 | 2020-10-14 11:51:10 -0700 | [diff] [blame] | 1189 | |
| 1190 | # C Header + C File + Clang-format file |
| 1191 | |
Pirmin Vogel | 5c7c19a | 2020-12-01 13:53:13 +0100 | [diff] [blame] | 1192 | # Since SW does not use FuseSoC and instead expects those files always |
| 1193 | # to be in hw/top_{topname}/sw/autogen, we currently create these files |
| 1194 | # twice: |
| 1195 | # - Once under out_path/sw/autogen |
| 1196 | # - Once under hw/top_{topname}/sw/autogen |
Cindy Liu | 2b8c7f5 | 2022-03-22 22:55:14 -0700 | [diff] [blame] | 1197 | root_paths = [out_path.resolve(), SRCTREE_TOP] |
| 1198 | out_paths = [ |
| 1199 | out_path.resolve(), |
| 1200 | (SRCTREE_TOP / "hw/top_{}/".format(topname)).resolve() |
| 1201 | ] |
| 1202 | for idx, path in enumerate(out_paths): |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 1203 | # "clang-format" -> "sw/autogen/.clang-format" |
| 1204 | cformat_tplpath = TOPGEN_TEMPLATE_PATH / "clang-format" |
| 1205 | cformat_dir = path / "sw/autogen" |
Pirmin Vogel | 5c7c19a | 2020-12-01 13:53:13 +0100 | [diff] [blame] | 1206 | cformat_dir.mkdir(parents=True, exist_ok=True) |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 1207 | cformat_path = cformat_dir / ".clang-format" |
Pirmin Vogel | 5c7c19a | 2020-12-01 13:53:13 +0100 | [diff] [blame] | 1208 | cformat_path.write_text(cformat_tplpath.read_text()) |
Sam Elliott | 7e36bd7 | 2020-04-22 14:05:49 +0100 | [diff] [blame] | 1209 | |
Cindy Liu | 2b8c7f5 | 2022-03-22 22:55:14 -0700 | [diff] [blame] | 1210 | # Save the header macro prefix into `c_helper` |
| 1211 | rel_header_dir = cformat_dir.relative_to(root_paths[idx]) |
| 1212 | c_helper.header_macro_prefix = ( |
| 1213 | "OPENTITAN_" + str(rel_header_dir).replace("/", "_").upper()) |
| 1214 | |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 1215 | # "top_{topname}.h.tpl" -> "sw/autogen/top_{topname}.h" |
Philipp Wagner | fb443ab | 2021-03-05 11:10:28 +0000 | [diff] [blame] | 1216 | cheader_path = cformat_dir / f"top_{topname}.h" |
| 1217 | render_template(TOPGEN_TEMPLATE_PATH / "toplevel.h.tpl", |
| 1218 | cheader_path, |
| 1219 | helper=c_helper) |
Sam Elliott | 7e36bd7 | 2020-04-22 14:05:49 +0100 | [diff] [blame] | 1220 | |
Cindy Liu | 2b8c7f5 | 2022-03-22 22:55:14 -0700 | [diff] [blame] | 1221 | # Save the relative header path into `c_helper` |
| 1222 | rel_header_path = cheader_path.relative_to(root_paths[idx]) |
Pirmin Vogel | 5c7c19a | 2020-12-01 13:53:13 +0100 | [diff] [blame] | 1223 | c_helper.header_path = str(rel_header_path) |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 1224 | |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 1225 | # "toplevel.c.tpl" -> "sw/autogen/top_{topname}.c" |
Philipp Wagner | fb443ab | 2021-03-05 11:10:28 +0000 | [diff] [blame] | 1226 | render_template(TOPGEN_TEMPLATE_PATH / "toplevel.c.tpl", |
| 1227 | cformat_dir / f"top_{topname}.c", |
| 1228 | helper=c_helper) |
Sam Elliott | 519f746 | 2020-05-11 16:53:24 +0100 | [diff] [blame] | 1229 | |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 1230 | # "toplevel_memory.ld.tpl" -> "sw/autogen/top_{topname}_memory.ld" |
Philipp Wagner | fb443ab | 2021-03-05 11:10:28 +0000 | [diff] [blame] | 1231 | render_template(TOPGEN_TEMPLATE_PATH / "toplevel_memory.ld.tpl", |
| 1232 | cformat_dir / f"top_{topname}_memory.ld") |
Sam Elliott | f7cb5f4 | 2020-07-27 21:29:26 +0100 | [diff] [blame] | 1233 | |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 1234 | # "toplevel_memory.h.tpl" -> "sw/autogen/top_{topname}_memory.h" |
Philipp Wagner | fb443ab | 2021-03-05 11:10:28 +0000 | [diff] [blame] | 1235 | memory_cheader_path = cformat_dir / f"top_{topname}_memory.h" |
| 1236 | render_template(TOPGEN_TEMPLATE_PATH / "toplevel_memory.h.tpl", |
Timothy Chen | 4ca4a2f | 2021-03-19 13:51:39 -0700 | [diff] [blame] | 1237 | memory_cheader_path, |
| 1238 | helper=c_helper) |
Pirmin Vogel | 5c7c19a | 2020-12-01 13:53:13 +0100 | [diff] [blame] | 1239 | |
Cindy Chen | e1184aa | 2020-09-01 11:45:07 -0700 | [diff] [blame] | 1240 | # generate chip level xbar and alert_handler TB |
Weicai Yang | a667036 | 2020-11-24 17:19:52 -0800 | [diff] [blame] | 1241 | tb_files = [ |
| 1242 | "xbar_env_pkg__params.sv", "tb__xbar_connect.sv", |
Weicai Yang | 3a2d0bc | 2021-08-25 15:41:41 -0700 | [diff] [blame] | 1243 | "tb__alert_handler_connect.sv", "xbar_tgl_excl.cfg" |
Weicai Yang | a667036 | 2020-11-24 17:19:52 -0800 | [diff] [blame] | 1244 | ] |
Weicai Yang | 1777ebb | 2020-05-19 17:32:24 -0700 | [diff] [blame] | 1245 | for fname in tb_files: |
| 1246 | tpl_fname = "%s.tpl" % (fname) |
Philipp Wagner | fb443ab | 2021-03-05 11:10:28 +0000 | [diff] [blame] | 1247 | xbar_chip_data_path = TOPGEN_TEMPLATE_PATH / tpl_fname |
Rupert Swarbrick | eb619e6 | 2021-03-05 15:01:54 +0000 | [diff] [blame] | 1248 | template_contents = generate_top(completecfg, name_to_block, |
Weicai Yang | 1777ebb | 2020-05-19 17:32:24 -0700 | [diff] [blame] | 1249 | str(xbar_chip_data_path)) |
| 1250 | |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 1251 | rendered_dir = out_path / "dv/autogen" |
Weicai Yang | 1777ebb | 2020-05-19 17:32:24 -0700 | [diff] [blame] | 1252 | rendered_dir.mkdir(parents=True, exist_ok=True) |
| 1253 | rendered_path = rendered_dir / fname |
| 1254 | |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 1255 | with rendered_path.open(mode="w", encoding="UTF-8") as fout: |
Weicai Yang | 1777ebb | 2020-05-19 17:32:24 -0700 | [diff] [blame] | 1256 | fout.write(template_contents) |
| 1257 | |
Philipp Wagner | 4407109 | 2021-03-05 19:29:12 +0000 | [diff] [blame] | 1258 | # generate parameters for chip-level environment package |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 1259 | tpl_fname = "chip_env_pkg__params.sv.tpl" |
Philipp Wagner | fb443ab | 2021-03-05 11:10:28 +0000 | [diff] [blame] | 1260 | alert_handler_chip_data_path = TOPGEN_TEMPLATE_PATH / tpl_fname |
Rupert Swarbrick | eb619e6 | 2021-03-05 15:01:54 +0000 | [diff] [blame] | 1261 | template_contents = generate_top(completecfg, name_to_block, |
Cindy Chen | e1184aa | 2020-09-01 11:45:07 -0700 | [diff] [blame] | 1262 | str(alert_handler_chip_data_path)) |
| 1263 | |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 1264 | rendered_dir = out_path / "dv/env/autogen" |
Cindy Chen | e1184aa | 2020-09-01 11:45:07 -0700 | [diff] [blame] | 1265 | rendered_dir.mkdir(parents=True, exist_ok=True) |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 1266 | rendered_path = rendered_dir / "chip_env_pkg__params.sv" |
Cindy Chen | e1184aa | 2020-09-01 11:45:07 -0700 | [diff] [blame] | 1267 | |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 1268 | with rendered_path.open(mode="w", encoding="UTF-8") as fout: |
Cindy Chen | e1184aa | 2020-09-01 11:45:07 -0700 | [diff] [blame] | 1269 | fout.write(template_contents) |
| 1270 | |
Michael Schaffner | 426ed20 | 2021-08-02 17:44:42 -0700 | [diff] [blame] | 1271 | # generate documentation for toplevel |
| 1272 | gen_top_docs(completecfg, c_helper, out_path) |
| 1273 | |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 1274 | # Auto-generate tests in "sw/device/tests/autogen" area. |
Srikrishna Iyer | 689981e | 2021-09-16 12:12:37 -0700 | [diff] [blame] | 1275 | gencmd = warnhdr + GENCMD.format(topname=topname) |
Cindy Chen | 21447a2 | 2022-04-18 17:55:25 -0700 | [diff] [blame] | 1276 | for fname in ["plic_all_irqs_test.c", "alert_test.c", "meson.build", "BUILD"]: |
Cindy Liu | c6db72f | 2022-03-24 14:12:53 -0700 | [diff] [blame] | 1277 | outfile = SRCTREE_TOP / "sw/device/tests/autogen" / fname |
Srikrishna Iyer | 689981e | 2021-09-16 12:12:37 -0700 | [diff] [blame] | 1278 | render_template(TOPGEN_TEMPLATE_PATH / f"{fname}.tpl", |
| 1279 | outfile, |
| 1280 | helper=c_helper, |
| 1281 | gencmd=gencmd) |
Srikrishna Iyer | 689981e | 2021-09-16 12:12:37 -0700 | [diff] [blame] | 1282 | |
Sam Elliott | 37d4fbe | 2020-04-22 14:05:49 +0100 | [diff] [blame] | 1283 | |
lowRISC Contributors | 802543a | 2019-08-31 12:12:56 +0100 | [diff] [blame] | 1284 | if __name__ == "__main__": |
| 1285 | main() |