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