blob: c417ab3dd33b0ba93eb50d64a05ba8a1e0797011 [file] [log] [blame]
Weicai Yanga495d202019-12-05 15:36:27 -08001# Copyright lowRISC contributors.
2# Licensed under the Apache License, Version 2.0, see LICENSE for details.
3# SPDX-License-Identifier: Apache-2.0
4
5import logging as log
Weicai Yang92b46e32020-10-01 17:45:37 -07006from pathlib import Path
Weicai Yanga495d202019-12-05 15:36:27 -08007
8from mako import exceptions
9from mako.template import Template
10from pkg_resources import resource_filename
11
Weicai Yanga495d202019-12-05 15:36:27 -080012from .xbar import Xbar
13
14
Eunchan Kim8f2cb382020-05-13 11:53:09 -070015def generate_tb(xbar: Xbar,
16 dv_path: Path,
17 library_name: str = "ip") -> str: # xbar: Xbar -> str
Weicai Yanga495d202019-12-05 15:36:27 -080018 # list all the generate files for TB
19 tb_files = [
20 "xbar_env_pkg__params.sv", "tb__xbar_connect.sv", "xbar.sim.core",
Weicai Yang92b46e32020-10-01 17:45:37 -070021 "xbar.bind.core", "xbar.bind.sv", "xbar.sim_cfg.hjson",
Weicai Yang547c1f02020-11-05 22:02:33 -080022 "xbar.testplan.hjson", "xbar_cov_excl.el", "xbar_cover.cfg"
Weicai Yanga495d202019-12-05 15:36:27 -080023 ]
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 Yanga60ae7d2020-02-21 14:32:50 -080031 elif fname == "xbar.bind.core":
32 fname = "xbar_%s_bind.core" % (xbar.name)
Weicai Yanga495d202019-12-05 15:36:27 -080033 elif fname == "xbar.bind.sv":
34 fname = "xbar_%s_bind.sv" % (xbar.name)
Weicai Yangd401c8f2020-02-07 18:07:43 -080035 elif fname == "xbar.sim_cfg.hjson":
36 fname = "xbar_%s_sim_cfg.hjson" % (xbar.name)
Weicai Yangfd2c22e2020-02-11 18:37:14 -080037 elif fname == "xbar.testplan.hjson":
38 fname = "xbar_%s_testplan.hjson" % (xbar.name)
Weicai Yanga495d202019-12-05 15:36:27 -080039
Weicai Yangfd2c22e2020-02-11 18:37:14 -080040 # save testplan at data directory
41 if fname == "xbar_%s_testplan.hjson" % (xbar.name):
Weicai Yanga60ae7d2020-02-21 14:32:50 -080042 data_filepath = dv_path / '../../data/autogen'
43 data_filepath.mkdir(parents=True, exist_ok=True)
44 dv_filepath = data_filepath / fname
Weicai Yangfd2c22e2020-02-11 18:37:14 -080045 else:
46 dv_filepath = dv_path / fname
47
Weicai Yanga495d202019-12-05 15:36:27 -080048 with dv_filepath.open(mode='w', encoding='UTF-8') as fout:
49 try:
Eunchan Kim8f2cb382020-05-13 11:53:09 -070050 fout.write(tpl.render(xbar=xbar, library_name=library_name))
Eunchan Kim837c7962020-04-30 12:15:49 -070051 except: # noqa: E722 for general exception handling
Weicai Yanga495d202019-12-05 15:36:27 -080052 log.error(exceptions.text_error_template().render())