From 8cf87613a6472f73ee3524febf1ce924a94ed342 Mon Sep 17 00:00:00 2001 From: Edward Kotarski Date: Mon, 30 Oct 2023 16:19:52 +0000 Subject: [PATCH 1/2] do it --- fcov/context.py | 31 +++++++++++++++++++++++++++++++ fcov/covergroup.py | 8 ++++++-- fcov/coverpoint.py | 7 ++++++- fcov/example.py | 12 +++++++----- 4 files changed, 50 insertions(+), 8 deletions(-) create mode 100644 fcov/context.py diff --git a/fcov/context.py b/fcov/context.py new file mode 100644 index 0000000..9d3ea59 --- /dev/null +++ b/fcov/context.py @@ -0,0 +1,31 @@ +from copy import copy +from types import SimpleNamespace +from typing import Any + + +class Context: + _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() + + class NO_DEFAULT: ... + + @classmethod + def get(cls): + if len(cls._stack): + return cls._stack[-1] + return SimpleNamespace() diff --git a/fcov/covergroup.py b/fcov/covergroup.py index 36fa307..5dc3c4d 100644 --- a/fcov/covergroup.py +++ b/fcov/covergroup.py @@ -1,3 +1,7 @@ +from types import SimpleNamespace +from .context import Context + + 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=Context.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..a9b99e9 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 Context + 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=Context.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..40e8b07 100644 --- a/fcov/example.py +++ b/fcov/example.py @@ -1,3 +1,4 @@ +from .context import Context 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,8 @@ def setup(self): class MyBigCoverGroup(Covergroup): - def setup(self): + def setup(self, ctx): + print(ctx.isa) self.add_covergroup( MyCovergroup(name="my_covergroup", description="A group of coverpoints") ) @@ -103,8 +105,8 @@ def create_trace(self): if __name__ == "__main__": # testbench - - cvg = MyBigCoverGroup(name="my_big_covergroup", description="A group of stuff") + with Context(isa="THIS IS AN ISA"): + cvg = MyBigCoverGroup(name="my_big_covergroup", description="A group of stuff") cvg.print_tree() cvg.my_covergroup.print_tree() From f6a23676adee59c58385c27ba2047f7354f4c7eb Mon Sep 17 00:00:00 2001 From: Edward Kotarski Date: Mon, 30 Oct 2023 17:30:49 +0000 Subject: [PATCH 2/2] tidy up a little --- fcov/context.py | 8 +++----- fcov/covergroup.py | 4 ++-- fcov/coverpoint.py | 4 ++-- fcov/example.py | 5 ++--- 4 files changed, 9 insertions(+), 12 deletions(-) diff --git a/fcov/context.py b/fcov/context.py index 9d3ea59..f7972df 100644 --- a/fcov/context.py +++ b/fcov/context.py @@ -1,9 +1,7 @@ from copy import copy from types import SimpleNamespace -from typing import Any - -class Context: +class ContextBase: _stack: list["SimpleNamespace"] = [SimpleNamespace()] def __init__(self, replace=False, overlay=True, **kwargs): @@ -22,10 +20,10 @@ def __enter__(self): def __exit__(self, _extype, _exval, _extb): self._stack.pop() - class NO_DEFAULT: ... - @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 5dc3c4d..fb7ae9d 100644 --- a/fcov/covergroup.py +++ b/fcov/covergroup.py @@ -1,5 +1,5 @@ from types import SimpleNamespace -from .context import Context +from .context import CoverageContext class Covergroup: @@ -18,7 +18,7 @@ def __init__(self, name, description): self.coverpoints = {} self.covergroups = {} - self.setup(ctx=Context.get()) + self.setup(ctx=CoverageContext.get()) def add_coverpoint(self, coverpoint): """ diff --git a/fcov/coverpoint.py b/fcov/coverpoint.py index a9b99e9..e1e10ce 100644 --- a/fcov/coverpoint.py +++ b/fcov/coverpoint.py @@ -6,7 +6,7 @@ from rich.console import Console from rich.table import Table -from .context import Context +from .context import CoverageContext from .axis import Axis from .cursor import Cursor @@ -44,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(ctx=Context.get()) + self.setup(ctx=CoverageContext.get()) self.axis_names = [x.name for x in self.axes] # TODO Check if goalsDict only has 1 entry diff --git a/fcov/example.py b/fcov/example.py index 40e8b07..6d3a29e 100644 --- a/fcov/example.py +++ b/fcov/example.py @@ -1,4 +1,4 @@ -from .context import Context +from .context import CoverageContext from .covergroup import Covergroup from .coverpoint import Coverpoint from .sampler import Sampler @@ -75,7 +75,6 @@ def setup(self, ctx): class MyBigCoverGroup(Covergroup): def setup(self, ctx): - print(ctx.isa) self.add_covergroup( MyCovergroup(name="my_covergroup", description="A group of coverpoints") ) @@ -105,7 +104,7 @@ def create_trace(self): if __name__ == "__main__": # testbench - with Context(isa="THIS IS AN ISA"): + with CoverageContext(isa="THIS IS AN ISA"): cvg = MyBigCoverGroup(name="my_big_covergroup", description="A group of stuff") cvg.print_tree()