+++ /dev/null
-import contextlib
-import functools
-import warnings
-from collections import OrderedDict
-from collections.abc import Iterable
-from contextlib import contextmanager
-
-from .tools import *
-
-
-__all__ = ["flatten", "union" , "log2_int", "bits_for", "memoize", "final", "deprecated"]
-
-
-def flatten(i):
- for e in i:
- if isinstance(e, Iterable):
- yield from flatten(e)
- else:
- yield e
-
-
-def union(i, start=None):
- r = start
- for e in i:
- if r is None:
- r = e
- else:
- r |= e
- return r
-
-
-def memoize(f):
- memo = OrderedDict()
- @functools.wraps(f)
- def g(*args):
- if args not in memo:
- memo[args] = f(*args)
- return memo[args]
- return g
-
-
-def final(cls):
- def init_subclass():
- raise TypeError("Subclassing {}.{} is not supported"
- .format(cls.__module__, cls.__name__))
- cls.__init_subclass__ = init_subclass
- return cls
-
-
-def deprecated(message, stacklevel=2):
- def decorator(f):
- @functools.wraps(f)
- def wrapper(*args, **kwargs):
- warnings.warn(message, DeprecationWarning, stacklevel=stacklevel)
- return f(*args, **kwargs)
- return wrapper
- return decorator
-
-
-def _ignore_deprecated(f=None):
- if f is None:
- @contextlib.contextmanager
- def context_like():
- with warnings.catch_warnings():
- warnings.filterwarnings(action="ignore", category=DeprecationWarning)
- yield
- return context_like()
- else:
- @functools.wraps(f)
- def decorator_like(*args, **kwargs):
- with warnings.catch_warnings():
- warnings.filterwarnings(action="ignore", category=DeprecationWarning)
- f(*args, **kwargs)
- return decorator_like
-
-
-def extend(cls):
- def decorator(f):
- if isinstance(f, property):
- name = f.fget.__name__
- else:
- name = f.__name__
- setattr(cls, name, f)
- return decorator
--- /dev/null
+import contextlib
+import functools
+import warnings
+from collections import OrderedDict
+from collections.abc import Iterable
+from contextlib import contextmanager
+
+from .utils import *
+
+
+__all__ = ["flatten", "union" , "log2_int", "bits_for", "memoize", "final", "deprecated"]
+
+
+def flatten(i):
+ for e in i:
+ if isinstance(e, Iterable):
+ yield from flatten(e)
+ else:
+ yield e
+
+
+def union(i, start=None):
+ r = start
+ for e in i:
+ if r is None:
+ r = e
+ else:
+ r |= e
+ return r
+
+
+def memoize(f):
+ memo = OrderedDict()
+ @functools.wraps(f)
+ def g(*args):
+ if args not in memo:
+ memo[args] = f(*args)
+ return memo[args]
+ return g
+
+
+def final(cls):
+ def init_subclass():
+ raise TypeError("Subclassing {}.{} is not supported"
+ .format(cls.__module__, cls.__name__))
+ cls.__init_subclass__ = init_subclass
+ return cls
+
+
+def deprecated(message, stacklevel=2):
+ def decorator(f):
+ @functools.wraps(f)
+ def wrapper(*args, **kwargs):
+ warnings.warn(message, DeprecationWarning, stacklevel=stacklevel)
+ return f(*args, **kwargs)
+ return wrapper
+ return decorator
+
+
+def _ignore_deprecated(f=None):
+ if f is None:
+ @contextlib.contextmanager
+ def context_like():
+ with warnings.catch_warnings():
+ warnings.filterwarnings(action="ignore", category=DeprecationWarning)
+ yield
+ return context_like()
+ else:
+ @functools.wraps(f)
+ def decorator_like(*args, **kwargs):
+ with warnings.catch_warnings():
+ warnings.filterwarnings(action="ignore", category=DeprecationWarning)
+ f(*args, **kwargs)
+ return decorator_like
+
+
+def extend(cls):
+ def decorator(f):
+ if isinstance(f, property):
+ name = f.fget.__name__
+ else:
+ name = f.__name__
+ setattr(cls, name, f)
+ return decorator
from vcd import VCDWriter
from vcd.gtkw import GTKWSave
-from .._tools import flatten
+from .._utils import flatten
from ..hdl.ast import *
from ..hdl.ir import *
from ..hdl.xfrm import ValueVisitor, StatementVisitor
from collections import defaultdict, OrderedDict
from contextlib import contextmanager
-from .._tools import bits_for, flatten
+from .._utils import bits_for, flatten
from ..hdl import ast, rec, ir, mem, xfrm
-from ... import tools
+from ... import utils
from ...hdl import ast
-from ..._tools import deprecated
+from ..._utils import deprecated
__all__ = ["log2_int", "bits_for", "value_bits_sign"]
-@deprecated("instead of `log2_int`, use `nmigen.tools.log2_int`")
+@deprecated("instead of `log2_int`, use `nmigen.utils.log2_int`")
def log2_int(n, need_pow2=True):
- return tools.log2_int(n, need_pow2)
+ return utils.log2_int(n, need_pow2)
-@deprecated("instead of `bits_for`, use `nmigen.tools.bits_for`")
+@deprecated("instead of `bits_for`, use `nmigen.utils.bits_for`")
def bits_for(n, require_sign_bit=False):
- return tools.bits_for(n, require_sign_bit)
+ return utils.bits_for(n, require_sign_bit)
@deprecated("instead of `value_bits_sign(v)`, use `v.shape()`")
from ...hdl.xfrm import ResetInserter as NativeResetInserter
from ...hdl.xfrm import EnableInserter as NativeEnableInserter
from ...hdl.xfrm import DomainRenamer as NativeDomainRenamer
-from ..._tools import deprecated
+from ..._utils import deprecated
__all__ = ["ResetInserter", "CEInserter", "ClockDomainsRenamer"]
from collections.abc import Iterable
-from ..._tools import flatten, deprecated
+from ..._utils import flatten, deprecated
from ...hdl import dsl, ir
import warnings
-from ..._tools import deprecated, extend
+from ..._utils import deprecated, extend
from ...hdl.ast import *
from ...hdl.ir import Elaboratable
from ...hdl.mem import Memory as NativeMemory
from collections import OrderedDict
-from ..._tools import deprecated, extend
+from ..._utils import deprecated, extend
from ...hdl import ast
from ...hdl.ast import (DUID, Value, Signal, Mux, Slice as _Slice, Cat, Repl, Const, C,
ClockSignal, ResetSignal,
import warnings
-from ..._tools import deprecated
+from ..._utils import deprecated
from ...lib.cdc import FFSynchronizer as NativeFFSynchronizer
from ...hdl.ast import *
from ..fhdl.module import CompatModule
-from ..._tools import deprecated, extend
+from ..._utils import deprecated, extend
from ...lib.fifo import (FIFOInterface as NativeFIFOInterface,
SyncFIFO as NativeSyncFIFO, SyncFIFOBuffered as NativeSyncFIFOBuffered,
AsyncFIFO as NativeAsyncFIFO, AsyncFIFOBuffered as NativeAsyncFIFOBuffered)
from collections import OrderedDict
-from ..._tools import deprecated, _ignore_deprecated
+from ..._utils import deprecated, _ignore_deprecated
from ...hdl.xfrm import ValueTransformer, StatementTransformer
from ...hdl.ast import *
from ..fhdl.module import CompatModule, CompatFinalizeError
-from ..._tools import deprecated
+from ..._utils import deprecated
from ...lib.cdc import ResetSynchronizer as NativeResetSynchronizer
from enum import Enum
from .. import tracer
-from .._tools import *
+from .._utils import *
__all__ = [
from enum import Enum
import warnings
-from .._tools import flatten, bits_for, deprecated
+from .._utils import flatten, bits_for, deprecated
from .. import tracer
from .ast import *
from .ir import *
import traceback
import sys
-from .._tools import *
+from .._utils import *
from .ast import *
from .cd import *
from functools import reduce
from .. import tracer
-from .._tools import union, deprecated
+from .._utils import union, deprecated
from .ast import *
from collections import OrderedDict
from collections.abc import Iterable
-from .._tools import flatten, deprecated
+from .._utils import flatten, deprecated
from .. import tracer
from .ast import *
from .ast import _StatementList
-from .._tools import deprecated
+from .._utils import deprecated
from .. import *
from .. import *
from ..asserts import *
-from .._tools import log2_int, deprecated
+from .._utils import log2_int, deprecated
from .coding import GrayEncoder
from .cdc import FFSynchronizer
-from ..._tools import _ignore_deprecated
+from ..._utils import _ignore_deprecated
from ...compat import *
from ...compat.fhdl import verilog
import unittest
-from ..._tools import _ignore_deprecated
+from ..._utils import _ignore_deprecated
from ...compat import *
from collections import OrderedDict
from ..build.dsl import *
-from .tools import *
+from .utils import *
class PinsTestCase(FHDLTestCase):
from ..lib.io import *
from ..build.dsl import *
from ..build.res import *
-from .tools import *
+from .utils import *
class ResourceManagerTestCase(FHDLTestCase):
from ..hdl.ir import Fragment
from ..compat import *
-from .tools import *
+from .utils import *
class CompatTestCase(FHDLTestCase):
import subprocess
from pathlib import Path
-from .tools import *
+from .utils import *
def example_test(name):
from enum import Enum
from ..hdl.ast import *
-from .tools import *
+from .utils import *
class UnsignedEnum(Enum):
from ..hdl.cd import *
-from .tools import *
+from .utils import *
class ClockDomainTestCase(FHDLTestCase):
from ..hdl.ast import *
from ..hdl.cd import *
from ..hdl.dsl import *
-from .tools import *
+from .utils import *
class DSLTestCase(FHDLTestCase):
from ..hdl.cd import *
from ..hdl.ir import *
from ..hdl.mem import *
-from .tools import *
+from .utils import *
class BadElaboratable(Elaboratable):
from ..hdl.ast import *
from ..hdl.mem import *
-from .tools import *
+from .utils import *
class MemoryTestCase(FHDLTestCase):
from ..hdl.ast import *
from ..hdl.rec import *
-from .tools import *
+from .utils import *
class UnsignedEnum(Enum):
from ..hdl.ir import *
from ..hdl.xfrm import *
from ..hdl.mem import *
-from .tools import *
+from .utils import *
class DomainRenamerTestCase(FHDLTestCase):
-from .tools import *
+from .utils import *
from ..hdl import *
from ..back.pysim import *
from ..lib.cdc import *
-from .tools import *
+from .utils import *
from ..hdl import *
from ..asserts import *
from ..back.pysim import *
-from .tools import *
+from .utils import *
from ..hdl import *
from ..asserts import *
from ..back.pysim import *
-from .tools import *
+from .utils import *
from ..hdl import *
from ..hdl.rec import *
from ..back.pysim import *
from contextlib import contextmanager
-from .tools import *
-from .._tools import flatten, union
+from .utils import *
+from .._utils import flatten, union
from ..hdl.ast import *
from ..hdl.cd import *
from ..hdl.mem import *
+++ /dev/null
-import os
-import re
-import shutil
-import subprocess
-import textwrap
-import traceback
-import unittest
-import warnings
-from contextlib import contextmanager
-
-from ..hdl.ast import *
-from ..hdl.ir import *
-from ..back import rtlil
-from .._toolchain import require_tool
-
-
-__all__ = ["FHDLTestCase"]
-
-
-class FHDLTestCase(unittest.TestCase):
- def assertRepr(self, obj, repr_str):
- if isinstance(obj, list):
- obj = Statement.cast(obj)
- def prepare_repr(repr_str):
- repr_str = re.sub(r"\s+", " ", repr_str)
- repr_str = re.sub(r"\( (?=\()", "(", repr_str)
- repr_str = re.sub(r"\) (?=\))", ")", repr_str)
- return repr_str.strip()
- self.assertEqual(prepare_repr(repr(obj)), prepare_repr(repr_str))
-
- @contextmanager
- def assertRaises(self, exception, msg=None):
- with super().assertRaises(exception) as cm:
- yield
- if msg is not None:
- # WTF? unittest.assertRaises is completely broken.
- self.assertEqual(str(cm.exception), msg)
-
- @contextmanager
- def assertRaisesRegex(self, exception, regex=None):
- with super().assertRaises(exception) as cm:
- yield
- if regex is not None:
- # unittest.assertRaisesRegex also seems broken...
- self.assertRegex(str(cm.exception), regex)
-
- @contextmanager
- def assertWarns(self, category, msg=None):
- with warnings.catch_warnings(record=True) as warns:
- yield
- self.assertEqual(len(warns), 1)
- self.assertEqual(warns[0].category, category)
- if msg is not None:
- self.assertEqual(str(warns[0].message), msg)
-
- def assertFormal(self, spec, mode="bmc", depth=1):
- caller, *_ = traceback.extract_stack(limit=2)
- spec_root, _ = os.path.splitext(caller.filename)
- spec_dir = os.path.dirname(spec_root)
- spec_name = "{}_{}".format(
- os.path.basename(spec_root).replace("test_", "spec_"),
- caller.name.replace("test_", "")
- )
-
- # The sby -f switch seems not fully functional when sby is reading from stdin.
- if os.path.exists(os.path.join(spec_dir, spec_name)):
- shutil.rmtree(os.path.join(spec_dir, spec_name))
-
- if mode == "hybrid":
- # A mix of BMC and k-induction, as per personal communication with Clifford Wolf.
- script = "setattr -unset init w:* a:nmigen.sample_reg %d"
- mode = "bmc"
- else:
- script = ""
-
- config = textwrap.dedent("""\
- [options]
- mode {mode}
- depth {depth}
- wait on
-
- [engines]
- smtbmc
-
- [script]
- read_ilang top.il
- prep
- {script}
-
- [file top.il]
- {rtlil}
- """).format(
- mode=mode,
- depth=depth,
- script=script,
- rtlil=rtlil.convert(Fragment.get(spec, platform="formal"))
- )
- with subprocess.Popen([require_tool("sby"), "-f", "-d", spec_name], cwd=spec_dir,
- universal_newlines=True,
- stdin=subprocess.PIPE, stdout=subprocess.PIPE) as proc:
- stdout, stderr = proc.communicate(config)
- if proc.returncode != 0:
- self.fail("Formal verification failed:\n" + stdout)
--- /dev/null
+import os
+import re
+import shutil
+import subprocess
+import textwrap
+import traceback
+import unittest
+import warnings
+from contextlib import contextmanager
+
+from ..hdl.ast import *
+from ..hdl.ir import *
+from ..back import rtlil
+from .._toolchain import require_tool
+
+
+__all__ = ["FHDLTestCase"]
+
+
+class FHDLTestCase(unittest.TestCase):
+ def assertRepr(self, obj, repr_str):
+ if isinstance(obj, list):
+ obj = Statement.cast(obj)
+ def prepare_repr(repr_str):
+ repr_str = re.sub(r"\s+", " ", repr_str)
+ repr_str = re.sub(r"\( (?=\()", "(", repr_str)
+ repr_str = re.sub(r"\) (?=\))", ")", repr_str)
+ return repr_str.strip()
+ self.assertEqual(prepare_repr(repr(obj)), prepare_repr(repr_str))
+
+ @contextmanager
+ def assertRaises(self, exception, msg=None):
+ with super().assertRaises(exception) as cm:
+ yield
+ if msg is not None:
+ # WTF? unittest.assertRaises is completely broken.
+ self.assertEqual(str(cm.exception), msg)
+
+ @contextmanager
+ def assertRaisesRegex(self, exception, regex=None):
+ with super().assertRaises(exception) as cm:
+ yield
+ if regex is not None:
+ # unittest.assertRaisesRegex also seems broken...
+ self.assertRegex(str(cm.exception), regex)
+
+ @contextmanager
+ def assertWarns(self, category, msg=None):
+ with warnings.catch_warnings(record=True) as warns:
+ yield
+ self.assertEqual(len(warns), 1)
+ self.assertEqual(warns[0].category, category)
+ if msg is not None:
+ self.assertEqual(str(warns[0].message), msg)
+
+ def assertFormal(self, spec, mode="bmc", depth=1):
+ caller, *_ = traceback.extract_stack(limit=2)
+ spec_root, _ = os.path.splitext(caller.filename)
+ spec_dir = os.path.dirname(spec_root)
+ spec_name = "{}_{}".format(
+ os.path.basename(spec_root).replace("test_", "spec_"),
+ caller.name.replace("test_", "")
+ )
+
+ # The sby -f switch seems not fully functional when sby is reading from stdin.
+ if os.path.exists(os.path.join(spec_dir, spec_name)):
+ shutil.rmtree(os.path.join(spec_dir, spec_name))
+
+ if mode == "hybrid":
+ # A mix of BMC and k-induction, as per personal communication with Clifford Wolf.
+ script = "setattr -unset init w:* a:nmigen.sample_reg %d"
+ mode = "bmc"
+ else:
+ script = ""
+
+ config = textwrap.dedent("""\
+ [options]
+ mode {mode}
+ depth {depth}
+ wait on
+
+ [engines]
+ smtbmc
+
+ [script]
+ read_ilang top.il
+ prep
+ {script}
+
+ [file top.il]
+ {rtlil}
+ """).format(
+ mode=mode,
+ depth=depth,
+ script=script,
+ rtlil=rtlil.convert(Fragment.get(spec, platform="formal"))
+ )
+ with subprocess.Popen([require_tool("sby"), "-f", "-d", spec_name], cwd=spec_dir,
+ universal_newlines=True,
+ stdin=subprocess.PIPE, stdout=subprocess.PIPE) as proc:
+ stdout, stderr = proc.communicate(config)
+ if proc.returncode != 0:
+ self.fail("Formal verification failed:\n" + stdout)
+++ /dev/null
-__all__ = ["log2_int", "bits_for"]
-
-
-def log2_int(n, need_pow2=True):
- if n == 0:
- return 0
- r = (n - 1).bit_length()
- if need_pow2 and (1 << r) != n:
- raise ValueError("{} is not a power of 2".format(n))
- return r
-
-
-def bits_for(n, require_sign_bit=False):
- if n > 0:
- r = log2_int(n + 1, False)
- else:
- require_sign_bit = True
- r = log2_int(-n, False)
- if require_sign_bit:
- r += 1
- return r
--- /dev/null
+__all__ = ["log2_int", "bits_for"]
+
+
+def log2_int(n, need_pow2=True):
+ if n == 0:
+ return 0
+ r = (n - 1).bit_length()
+ if need_pow2 and (1 << r) != n:
+ raise ValueError("{} is not a power of 2".format(n))
+ return r
+
+
+def bits_for(n, require_sign_bit=False):
+ if n > 0:
+ r = log2_int(n + 1, False)
+ else:
+ require_sign_bit = True
+ r = log2_int(-n, False)
+ if require_sign_bit:
+ r += 1
+ return r