test_feature.py 2.53 KB
Newer Older
1
import logging
Martin Jeřábek's avatar
Martin Jeřábek committed
2
from pathlib import Path
3 4
from .test_common import add_sources, TestsBase, dict_merge, \
                         get_common_modelsim_init_files, get_seed
5
from textwrap import dedent
6 7 8

log = logging.getLogger(__name__)

Martin Jeřábek's avatar
Martin Jeřábek committed
9

10
class FeatureTests(TestsBase):
Martin Jeřábek's avatar
Martin Jeřábek committed
11 12 13
    def __init__(self, *args, **kwds):
        super().__init__(*args, **kwds)
        self._tests = self.config['tests'].keys()
14

Martin Jeřábek's avatar
Martin Jeřábek committed
15 16 17 18 19 20
    def add_sources(self) -> None:
        sources = ['feature/{}_feature_tb.vhd'.format(name) for name in self._tests]
        sources.append('feature/tb_feature.vhd')
        sources.append('feature/pkg_feature_exec_dispatch.vhd')
        add_sources(self.lib, sources)

Martin Jeřábek's avatar
Martin Jeřábek committed
21
        wrname = self.build / 'pkg_feature_exec_dispatch-body.vhd'
Martin Jeřábek's avatar
Martin Jeřábek committed
22
        self._create_wrapper(wrname)
Martin Jeřábek's avatar
Martin Jeřábek committed
23
        # add_sources(self.lib, [str(wrname)])
Martin Jeřábek's avatar
Martin Jeřábek committed
24 25 26 27 28
        self.lib.add_source_file(str(wrname))
        tb = self.lib.get_test_benches('*tb_feature')[0]
        tb.scan_tests_from_file(str(wrname))

    def configure(self) -> None:
Martin Jeřábek's avatar
Martin Jeřábek committed
29 30 31
        tb = self.lib.get_test_benches('*tb_feature')[0]
        default = self.config['default']
        self.add_modelsim_gui_file(tb, default, 'feature')
32 33 34 35 36 37 38 39 40 41 42 43

        # generate & set per-test modelsim tcl file
        tcl = self.build / 'modelsim_init_feature.tcl'
        with tcl.open('wt', encoding='utf-8') as f:
            print(dedent('''\
                global TCOMP
                set TCOMP tb_feature/test_comp
                '''), file=f)
        init_files = get_common_modelsim_init_files()
        init_files += [str(tcl)]
        tb.set_sim_option("modelsim.init_files.after_load", init_files)

Martin Jeřábek's avatar
Martin Jeřábek committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57
        for name, cfg in self.config['tests'].items():
            if cfg is None:
                cfg = dict()

            if 'wave' in cfg:
                log.warn('"wave" in feature test config {} is ignored' +
                         ' (set it in default instead)'.format(name))
            dict_merge(cfg, default)

            generics = {
                'timeout'      : cfg['timeout'],
                'iterations'   : cfg['iterations'],
                'log_level'    : cfg['log_level'] + '_l',
                'error_tol'    : cfg['error_tolerance'],
58 59
                'test_name'    : name,
                'seed'         : get_seed(cfg)
Martin Jeřábek's avatar
Martin Jeřábek committed
60 61
            }
            tb.add_config(name, generics=generics)
Martin Jeřábek's avatar
Martin Jeřábek committed
62

Martin Jeřábek's avatar
Martin Jeřábek committed
63
    def _create_wrapper(self, ofile: Path) -> None:
Martin Jeřábek's avatar
Martin Jeřábek committed
64 65 66 67
        template = self.jinja_env.get_template('pkg_feature_exec_dispath-body.vhd')
        c = template.render(tests=self._tests)
        with ofile.open('wt', encoding='utf-8') as f:
            f.write(c)