Skip to content

Add broken tests #326

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions fault/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from fault.select_path import SelectPath
import fault.expression as expression

import magma as m


class Action(ABC):
@abstractmethod
Expand All @@ -25,8 +27,8 @@ def __repr__(self):
class PortAction(Action):
def __init__(self, port, value):
super().__init__()
self.port = port
self.value = value
self.port = m.protocol_type.magma_value(port)
self.value = m.protocol_type.magma_value(value)

def __str__(self):
type_name = type(self).__name__
Expand Down
2 changes: 1 addition & 1 deletion fault/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class CircuitWrapper(Wrapper):

class PortWrapper(expression.Expression):
def __init__(self, port, parent):
self.port = port
self.port = m.protocol_type.magma_value(port)
self.parent = parent
self.init_done = True

Expand Down
110 changes: 110 additions & 0 deletions tests/test_magma_protocol.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import tempfile
from typing import Optional

import pytest

import magma as m
import fault
from hwtypes import BitVector
Expand Down Expand Up @@ -49,3 +52,110 @@ class Bar(m.Circuit):

with tempfile.TemporaryDirectory(dir=".") as _dir:
tester.compile_and_run("verilator", directory=_dir)


class SimpleMagmaProtocolMeta(m.MagmaProtocolMeta):
_CACHE = {}

def _to_magma_(cls):
return cls.T

def _qualify_magma_(cls, direction):
return cls[cls.T.qualify(direction)]

def _flip_magma_(cls):
return cls[cls.T.flip()]

def _from_magma_value_(cls, val):
return cls(val)

def __getitem__(cls, T):
try:
base = cls.base
except AttributeError:
base = cls
dct = {"T": T, "base": base}
derived = type(cls)(f"{base.__name__}[{T}]", (cls,), dct)
return SimpleMagmaProtocolMeta._CACHE.setdefault(T, derived)

def __repr__(cls):
return str(cls)

def __str__(cls):
return cls.__name__


class BrokenProtocol(m.MagmaProtocol, metaclass=SimpleMagmaProtocolMeta):
def __init__(self, val=None):
if val is None:
self._val = self.T()
elif isinstance(val, self.T):
self._val = val
else:
self._val = self.T(val)

def _get_magma_value_(self):
return self._val


class FixedProtocol(BrokenProtocol):
@property
def debug_name(self):
# Beyond not liking the number of names already reserved by
# MagmaProtocol I have not strong feeling about adding `debug_name`
# to the list.
return self._get_magma_value_().debug_name

def __len__(self):
# !!! Please do not reserve len !!!
# I have container types which use it
cls = type(self)
magma_t = cls._to_magma_()
if issubclass(magma_t, m.Digital):
# for some reason this breaks on m.Bits
# dont know if bug
return len(magma_t)
else:
return len(self._get_magma_value_())


def gen_DUT(T, BoxT, i, o):
if i:
i_t = BoxT[T]
else:
i_t = T

if o:
o_t = BoxT[T]
else:
o_t = T

class DUT(m.Circuit):
io = m.IO(
I=m.In(i_t),
O=m.Out(o_t)
)
if i and not o:
io.O @= io.I._get_magma_value_()
elif o and not i:
io.O @= o_t._from_magma_value_(io.I)
else:
io.O @= io.I
return DUT


@pytest.mark.parametrize('T', [m.Bit, m.Bits[16]])
@pytest.mark.parametrize('BoxT', [BrokenProtocol])
@pytest.mark.parametrize('proto_in, proto_out', [
(True, False),
(False, True),
(True, True),
])
def test_protocol_as_input_and_output(T, BoxT, proto_in, proto_out):
DUT = gen_DUT(T, BoxT, proto_in, proto_out)
tester = fault.Tester(DUT)
tester.circuit.I = BoxT[T](T(0))
tester.eval()
tester.circuit.O.expect(T(0))
with tempfile.TemporaryDirectory(dir=".") as tempdir:
tester.compile_and_run("verilator", directory=tempdir)