Weicai Yang | a495d20 | 2019-12-05 15:36:27 -0800 | [diff] [blame] | 1 | # Copyright lowRISC contributors. |
| 2 | # Licensed under the Apache License, Version 2.0, see LICENSE for details. |
| 3 | # SPDX-License-Identifier: Apache-2.0 |
| 4 | |
| 5 | import logging as log |
Weicai Yang | 92b46e3 | 2020-10-01 17:45:37 -0700 | [diff] [blame] | 6 | from pathlib import Path |
Weicai Yang | a495d20 | 2019-12-05 15:36:27 -0800 | [diff] [blame] | 7 | |
| 8 | from mako import exceptions |
| 9 | from mako.template import Template |
| 10 | from pkg_resources import resource_filename |
| 11 | |
Weicai Yang | a495d20 | 2019-12-05 15:36:27 -0800 | [diff] [blame] | 12 | from .xbar import Xbar |
| 13 | |
| 14 | |
Eunchan Kim | 8f2cb38 | 2020-05-13 11:53:09 -0700 | [diff] [blame] | 15 | def generate_tb(xbar: Xbar, |
| 16 | dv_path: Path, |
| 17 | library_name: str = "ip") -> str: # xbar: Xbar -> str |
Weicai Yang | a495d20 | 2019-12-05 15:36:27 -0800 | [diff] [blame] | 18 | # list all the generate files for TB |
| 19 | tb_files = [ |
| 20 | "xbar_env_pkg__params.sv", "tb__xbar_connect.sv", "xbar.sim.core", |
Weicai Yang | 92b46e3 | 2020-10-01 17:45:37 -0700 | [diff] [blame] | 21 | "xbar.bind.core", "xbar.bind.sv", "xbar.sim_cfg.hjson", |
Weicai Yang | 547c1f0 | 2020-11-05 22:02:33 -0800 | [diff] [blame] | 22 | "xbar.testplan.hjson", "xbar_cov_excl.el", "xbar_cover.cfg" |
Weicai Yang | a495d20 | 2019-12-05 15:36:27 -0800 | [diff] [blame] | 23 | ] |
| 24 | |
| 25 | for fname in tb_files: |
| 26 | tpl = Template(filename=resource_filename('tlgen', fname + '.tpl')) |
| 27 | |
| 28 | # some files need to be renamed |
| 29 | if fname == "xbar.sim.core": |
| 30 | fname = "xbar_%s_sim.core" % (xbar.name) |
Weicai Yang | a60ae7d | 2020-02-21 14:32:50 -0800 | [diff] [blame] | 31 | elif fname == "xbar.bind.core": |
| 32 | fname = "xbar_%s_bind.core" % (xbar.name) |
Weicai Yang | a495d20 | 2019-12-05 15:36:27 -0800 | [diff] [blame] | 33 | elif fname == "xbar.bind.sv": |
| 34 | fname = "xbar_%s_bind.sv" % (xbar.name) |
Weicai Yang | d401c8f | 2020-02-07 18:07:43 -0800 | [diff] [blame] | 35 | elif fname == "xbar.sim_cfg.hjson": |
| 36 | fname = "xbar_%s_sim_cfg.hjson" % (xbar.name) |
Weicai Yang | fd2c22e | 2020-02-11 18:37:14 -0800 | [diff] [blame] | 37 | elif fname == "xbar.testplan.hjson": |
| 38 | fname = "xbar_%s_testplan.hjson" % (xbar.name) |
Weicai Yang | a495d20 | 2019-12-05 15:36:27 -0800 | [diff] [blame] | 39 | |
Weicai Yang | fd2c22e | 2020-02-11 18:37:14 -0800 | [diff] [blame] | 40 | # save testplan at data directory |
| 41 | if fname == "xbar_%s_testplan.hjson" % (xbar.name): |
Weicai Yang | a60ae7d | 2020-02-21 14:32:50 -0800 | [diff] [blame] | 42 | data_filepath = dv_path / '../../data/autogen' |
| 43 | data_filepath.mkdir(parents=True, exist_ok=True) |
| 44 | dv_filepath = data_filepath / fname |
Weicai Yang | fd2c22e | 2020-02-11 18:37:14 -0800 | [diff] [blame] | 45 | else: |
| 46 | dv_filepath = dv_path / fname |
| 47 | |
Weicai Yang | a495d20 | 2019-12-05 15:36:27 -0800 | [diff] [blame] | 48 | with dv_filepath.open(mode='w', encoding='UTF-8') as fout: |
| 49 | try: |
Eunchan Kim | 8f2cb38 | 2020-05-13 11:53:09 -0700 | [diff] [blame] | 50 | fout.write(tpl.render(xbar=xbar, library_name=library_name)) |
Eunchan Kim | 837c796 | 2020-04-30 12:15:49 -0700 | [diff] [blame] | 51 | except: # noqa: E722 for general exception handling |
Weicai Yang | a495d20 | 2019-12-05 15:36:27 -0800 | [diff] [blame] | 52 | log.error(exceptions.text_error_template().render()) |