Rupert Swarbrick | 1018410 | 2021-03-31 09:12:42 +0100 | [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 |
Cindy Chen | c8389ed | 2021-11-04 10:53:07 -0700 | [diff] [blame] | 6 | from typing import List, Optional, Tuple |
Rupert Swarbrick | 1018410 | 2021-03-31 09:12:42 +0100 | [diff] [blame] | 7 | |
| 8 | from mako import exceptions # type: ignore |
| 9 | from mako.lookup import TemplateLookup # type: ignore |
| 10 | from pkg_resources import resource_filename |
| 11 | |
| 12 | from reggen.gen_dv import gen_core_file |
| 13 | |
| 14 | from .top import Top |
| 15 | |
| 16 | |
| 17 | def sv_base_addr(top: Top, if_name: Tuple[str, Optional[str]]) -> str: |
| 18 | '''Get the base address of a device interface in SV syntax''' |
| 19 | return "{}'h{:x}".format(top.regwidth, top.if_addrs[if_name]) |
| 20 | |
| 21 | |
| 22 | def gen_dv(top: Top, |
Cindy Chen | c8389ed | 2021-11-04 10:53:07 -0700 | [diff] [blame] | 23 | dv_base_names: List[str], |
Rupert Swarbrick | 1018410 | 2021-03-31 09:12:42 +0100 | [diff] [blame] | 24 | outdir: str) -> int: |
| 25 | '''Generate DV RAL model for a Top''' |
| 26 | # Read template |
| 27 | lookup = TemplateLookup(directories=[resource_filename('topgen', '.'), |
| 28 | resource_filename('reggen', '.')]) |
| 29 | uvm_reg_tpl = lookup.get_template('top_uvm_reg.sv.tpl') |
| 30 | |
| 31 | # Expand template |
| 32 | try: |
| 33 | to_write = uvm_reg_tpl.render(top=top, |
Cindy Chen | e16009b | 2021-11-01 19:35:51 -0700 | [diff] [blame] | 34 | dv_base_names=dv_base_names) |
Rupert Swarbrick | 1018410 | 2021-03-31 09:12:42 +0100 | [diff] [blame] | 35 | except: # noqa: E722 |
| 36 | log.error(exceptions.text_error_template().render()) |
| 37 | return 1 |
| 38 | |
| 39 | # Dump to output file |
| 40 | dest_path = '{}/chip_ral_pkg.sv'.format(outdir) |
| 41 | with open(dest_path, 'w') as fout: |
| 42 | fout.write(to_write) |
| 43 | |
Cindy Chen | e16009b | 2021-11-01 19:35:51 -0700 | [diff] [blame] | 44 | gen_core_file(outdir, 'chip', dv_base_names, ['chip_ral_pkg.sv']) |
Rupert Swarbrick | 1018410 | 2021-03-31 09:12:42 +0100 | [diff] [blame] | 45 | |
| 46 | return 0 |