diff --git a/fcov/context.py b/fcov/context.py new file mode 100644 index 0000000..f7972df --- /dev/null +++ b/fcov/context.py @@ -0,0 +1,29 @@ +from copy import copy +from types import SimpleNamespace + +class ContextBase: + _stack: list["SimpleNamespace"] = [SimpleNamespace()] + + def __init__(self, replace=False, overlay=True, **kwargs): + if overlay: + self.data = copy(self._stack[-1]) + for k, v in kwargs.items(): + if not replace and hasattr(self.data, k): + raise KeyError(f"Context already contains `{k}` and `replace` is False") + setattr(self.data, k, v) + else: + self.data = SimpleNamespace(**kwargs) + + def __enter__(self): + self._stack.append(self.data) + + def __exit__(self, _extype, _exval, _extb): + self._stack.pop() + + @classmethod + def get(cls): + if len(cls._stack): + return cls._stack[-1] + return SimpleNamespace() + +class CoverageContext(ContextBase): ... \ No newline at end of file diff --git a/fcov/covergroup.py b/fcov/covergroup.py index 36fa307..fb7ae9d 100644 --- a/fcov/covergroup.py +++ b/fcov/covergroup.py @@ -1,3 +1,7 @@ +from types import SimpleNamespace +from .context import CoverageContext + + class Covergroup: """This class groups coverpoints together, and adds them to the hierarchy""" @@ -14,7 +18,7 @@ def __init__(self, name, description): self.coverpoints = {} self.covergroups = {} - self.setup() + self.setup(ctx=CoverageContext.get()) def add_coverpoint(self, coverpoint): """ @@ -41,7 +45,7 @@ def add_covergroup(self, covergroup): self.covergroups[covergroup.name] = covergroup setattr(self, covergroup.name, covergroup) - def setup(self): + def setup(self, ctx: SimpleNamespace): raise NotImplementedError("This needs to be implemented by the coverpoint") def print_tree(self, indent=0): diff --git a/fcov/coverpoint.py b/fcov/coverpoint.py index 54b866b..e1e10ce 100644 --- a/fcov/coverpoint.py +++ b/fcov/coverpoint.py @@ -6,6 +6,8 @@ from rich.console import Console from rich.table import Table +from .context import CoverageContext + from .axis import Axis from .cursor import Cursor from .goal import GoalItem @@ -42,7 +44,7 @@ def __init__(self, name: str, description: str, trigger=None): # Instance of Cursor class to increment hit count for a bucket self.cursor = Cursor(self) - self.setup() + self.setup(ctx=CoverageContext.get()) self.axis_names = [x.name for x in self.axes] # TODO Check if goalsDict only has 1 entry @@ -53,6 +55,9 @@ def __init__(self, name: str, description: str, trigger=None): if goal: self._cvg_goals[cursor] = goal + def setup(self, ctx: SimpleNamespace): + raise NotImplementedError("This needs to be implemented by the coverpoint") + def all_axis_value_combinations(self): axis_values = [] for axis in self.axes: diff --git a/fcov/example.py b/fcov/example.py index 01fbe0d..6d3a29e 100644 --- a/fcov/example.py +++ b/fcov/example.py @@ -1,3 +1,4 @@ +from .context import CoverageContext from .covergroup import Covergroup from .coverpoint import Coverpoint from .sampler import Sampler @@ -20,7 +21,7 @@ class MyCoverpoint(Coverpoint): def __init__(self, name: str, description: str, trigger=None): super().__init__(name, description, trigger=None) - def setup(self): + def setup(self, ctx): self.add_axis( name="my_axis_1", values=[0, 1, [2, 3]], @@ -65,7 +66,7 @@ def sample(self, trace): # covergroups class MyCovergroup(Covergroup): - def setup(self): + def setup(self, ctx): self.add_coverpoint(MyCoverpoint(name="my_coverpoint", description="A lovely coverpoint")) self.add_coverpoint( MyCoverpoint(name="another_coverpoint", description="A rather spiffing coverpoint") @@ -73,7 +74,7 @@ def setup(self): class MyBigCoverGroup(Covergroup): - def setup(self): + def setup(self, ctx): self.add_covergroup( MyCovergroup(name="my_covergroup", description="A group of coverpoints") ) @@ -103,8 +104,8 @@ def create_trace(self): if __name__ == "__main__": # testbench - - cvg = MyBigCoverGroup(name="my_big_covergroup", description="A group of stuff") + with CoverageContext(isa="THIS IS AN ISA"): + cvg = MyBigCoverGroup(name="my_big_covergroup", description="A group of stuff") cvg.print_tree() cvg.my_covergroup.print_tree()