Improve cpuif customization support. Add docs & testcases

This commit is contained in:
Alex Mykyta
2022-02-24 22:42:58 -08:00
parent 5324b594bf
commit da3ed05492
10 changed files with 179 additions and 17 deletions

View File

@@ -16,15 +16,33 @@ class CpuifTestMode:
cpuif_cls = None # type: CpuifBase
# Files required by the DUT
# Paths are relative to the class that assigns this
rtl_files = [] # type: List[str]
# Files required by the sim testbench
# Paths are relative to the class that assigns this
tb_files = [] # type: List[str]
# Path is relative to the class that assigns this
tb_template = ""
def _translate_paths(self, files: List[str]) -> List[str]:
class_dir = os.path.dirname(inspect.getfile(self.__class__))
def _get_class_dir_of_variable(self, varname:str) -> str:
"""
Traverse up the MRO and find the first class that explicitly assigns
the variable of name varname. Returns the directory that contains the
class definition.
"""
for cls in inspect.getmro(self.__class__):
if varname in cls.__dict__:
class_dir = os.path.dirname(inspect.getfile(cls))
return class_dir
raise RuntimeError
def _get_file_paths(self, varname:str) -> List[str]:
class_dir = self._get_class_dir_of_variable(varname)
files = getattr(self, varname)
cwd = os.getcwd()
new_files = []
@@ -33,18 +51,23 @@ class CpuifTestMode:
os.path.join(class_dir, file),
cwd
)
if relpath not in new_files:
new_files.append(relpath)
new_files.append(relpath)
return new_files
def get_sim_files(self) -> List[str]:
return self._translate_paths(self.rtl_files + self.tb_files)
files = self._get_file_paths("rtl_files") + self._get_file_paths("tb_files")
unique_files = []
[unique_files.append(f) for f in files if f not in unique_files]
return unique_files
def get_synth_files(self) -> List[str]:
return self._translate_paths(self.rtl_files)
return self._get_file_paths("rtl_files")
def get_tb_inst(self, tb_cls: 'SimTestCase', exporter: 'RegblockExporter') -> str:
class_dir = os.path.dirname(inspect.getfile(self.__class__))
class_dir = self._get_class_dir_of_variable("tb_template")
loader = jj.FileSystemLoader(class_dir)
jj_env = jj.Environment(
loader=loader,

View File

View File

@@ -0,0 +1,7 @@
addrmap top {
reg {
field {
sw=rw; hw=r;
} f = 0;
} r1;
};

View File

@@ -0,0 +1,49 @@
import os
from peakrdl.regblock.cpuif.apb3 import APB3_Cpuif
from ..lib.cpuifs.apb3 import APB3
from ..lib.base_testcase import BaseTestCase
#-------------------------------------------------------------------------------
class ClassOverride_Cpuif(APB3_Cpuif):
@property
def port_declaration(self) -> str:
return "user_apb3_intf.slave s_apb"
class ClassOverride_cpuiftestmode(APB3):
cpuif_cls = ClassOverride_Cpuif
class Test_class_override(BaseTestCase):
cpuif = ClassOverride_cpuiftestmode()
def test_override_success(self):
output_file = os.path.join(self.get_run_dir(), "regblock.sv")
with open(output_file, "r") as f:
self.assertIn(
"user_apb3_intf.slave s_apb",
f.read()
)
#-------------------------------------------------------------------------------
class TemplateOverride_Cpuif(APB3_Cpuif):
# contains the text "USER TEMPLATE OVERRIDE"
template_path = "user_apb3_tmpl.sv"
class TemplateOverride_cpuiftestmode(APB3):
cpuif_cls = TemplateOverride_Cpuif
class Test_template_override(BaseTestCase):
cpuif = TemplateOverride_cpuiftestmode()
def test_override_success(self):
output_file = os.path.join(self.get_run_dir(), "regblock.sv")
with open(output_file, "r") as f:
self.assertIn(
"USER TEMPLATE OVERRIDE",
f.read()
)

View File

@@ -0,0 +1 @@
// USER TEMPLATE OVERRIDE