mirror of
https://github.com/myhdl/myhdl.git
synced 2025-01-24 21:52:56 +08:00
Make the module decorator work on methods; migrate test
This commit is contained in:
parent
7fd658f966
commit
1182071e29
@ -54,14 +54,14 @@ def _getCallInfo():
|
|||||||
3: the function that defines instances
|
3: the function that defines instances
|
||||||
4: the caller of the module function, e.g. a ModuleInstance.
|
4: the caller of the module function, e.g. a ModuleInstance.
|
||||||
"""
|
"""
|
||||||
|
stack = inspect.stack()
|
||||||
funcrec = inspect.stack()[3]
|
funcrec = stack[3]
|
||||||
name = funcrec[3]
|
name = funcrec[3]
|
||||||
frame = funcrec[0]
|
frame = funcrec[0]
|
||||||
symdict = dict(frame.f_globals)
|
symdict = dict(frame.f_globals)
|
||||||
symdict.update(frame.f_locals)
|
symdict.update(frame.f_locals)
|
||||||
modctxt = False
|
modctxt = False
|
||||||
callerrec = inspect.stack()[4]
|
callerrec = stack[4]
|
||||||
f_locals = callerrec[0].f_locals
|
f_locals = callerrec[0].f_locals
|
||||||
if 'self' in f_locals:
|
if 'self' in f_locals:
|
||||||
modctxt = isinstance(f_locals['self'], _ModuleInstance)
|
modctxt = isinstance(f_locals['self'], _ModuleInstance)
|
||||||
@ -85,6 +85,17 @@ class _Module(object):
|
|||||||
self.count += 1
|
self.count += 1
|
||||||
return modinst
|
return modinst
|
||||||
|
|
||||||
|
# This is the way to make the module decorator work on methods
|
||||||
|
# Turn it into a descriptor, used when accessed as an attribute
|
||||||
|
# In that case, the object is bound to the call method
|
||||||
|
# like done automatically for classic bound methods
|
||||||
|
# http://stackoverflow.com/a/3296318/574895
|
||||||
|
def __get__(self, obj, objtype):
|
||||||
|
"""Support instance methods."""
|
||||||
|
import functools
|
||||||
|
return functools.partial(self.__call__, obj)
|
||||||
|
|
||||||
|
|
||||||
class _ModuleInstance(object):
|
class _ModuleInstance(object):
|
||||||
|
|
||||||
def __init__(self, mod, *args, **kwargs):
|
def __init__(self, mod, *args, **kwargs):
|
||||||
@ -102,7 +113,6 @@ class _ModuleInstance(object):
|
|||||||
self.subs = _flatten(mod.modfunc(*args, **kwargs))
|
self.subs = _flatten(mod.modfunc(*args, **kwargs))
|
||||||
self.verify()
|
self.verify()
|
||||||
self.update()
|
self.update()
|
||||||
# self.inferInterface(*args, **kwargs)
|
|
||||||
self.name = self.__name__ = mod.__name__ + '_' + str(mod.count)
|
self.name = self.__name__ = mod.__name__ + '_' + str(mod.count)
|
||||||
self.verilog_code = self.vhdl_code = None
|
self.verilog_code = self.vhdl_code = None
|
||||||
if hasattr(mod, 'verilog_code'):
|
if hasattr(mod, 'verilog_code'):
|
||||||
|
@ -7,6 +7,7 @@ class HdlObj(object):
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@module
|
||||||
def method_func(self, clk, srst, x, y):
|
def method_func(self, clk, srst, x, y):
|
||||||
z = Signal(intbv(0, min=y.min, max=y.max))
|
z = Signal(intbv(0, min=y.min, max=y.max))
|
||||||
ifx = self._mfunc(x, z)
|
ifx = self._mfunc(x, z)
|
||||||
@ -19,12 +20,14 @@ class HdlObj(object):
|
|||||||
|
|
||||||
return hdl, ifx
|
return hdl, ifx
|
||||||
|
|
||||||
|
@module
|
||||||
def _mfunc(self, x, y):
|
def _mfunc(self, x, y):
|
||||||
@always_comb
|
@always_comb
|
||||||
def _hdl():
|
def _hdl():
|
||||||
y.next = x + 1
|
y.next = x + 1
|
||||||
return _hdl
|
return _hdl
|
||||||
|
|
||||||
|
@module
|
||||||
def _func(x,y):
|
def _func(x,y):
|
||||||
@always_comb
|
@always_comb
|
||||||
def _hdl():
|
def _hdl():
|
||||||
@ -35,6 +38,7 @@ class HdlObjObj(object):
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@module
|
||||||
def method_func(self, clk, srst, x, y):
|
def method_func(self, clk, srst, x, y):
|
||||||
z1 = Signal(intbv(0, min=y.min, max=y.max))
|
z1 = Signal(intbv(0, min=y.min, max=y.max))
|
||||||
z2 = Signal(intbv(0, min=y.min, max=y.max))
|
z2 = Signal(intbv(0, min=y.min, max=y.max))
|
||||||
@ -55,6 +59,7 @@ class HdlObjAttrSimple(object):
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.AConstant = 3
|
self.AConstant = 3
|
||||||
|
|
||||||
|
@module
|
||||||
def method_func(self, clk, srst, x, y):
|
def method_func(self, clk, srst, x, y):
|
||||||
|
|
||||||
# limitation for class method conversion, the object attributes
|
# limitation for class method conversion, the object attributes
|
||||||
@ -78,6 +83,7 @@ class HdlObjAttr(object):
|
|||||||
self.z = Signal(intbv(0, min=y.min, max=y.max))
|
self.z = Signal(intbv(0, min=y.min, max=y.max))
|
||||||
self.hobj = HdlObj()
|
self.hobj = HdlObj()
|
||||||
|
|
||||||
|
@module
|
||||||
def method_func(self):
|
def method_func(self):
|
||||||
ifx = self.hobj._mfunc(self.x, self.z)
|
ifx = self.hobj._mfunc(self.x, self.z)
|
||||||
@always(self.clk.posedge)
|
@always(self.clk.posedge)
|
||||||
@ -89,6 +95,7 @@ class HdlObjAttr(object):
|
|||||||
|
|
||||||
return hdl, ifx
|
return hdl, ifx
|
||||||
|
|
||||||
|
@module
|
||||||
def ObjBench(hObj):
|
def ObjBench(hObj):
|
||||||
|
|
||||||
clk = Signal(False)
|
clk = Signal(False)
|
||||||
@ -145,13 +152,13 @@ def ObjBench(hObj):
|
|||||||
|
|
||||||
|
|
||||||
def test_hdlobj():
|
def test_hdlobj():
|
||||||
assert verify(ObjBench, HdlObj) == 0
|
assert verify(ObjBench(HdlObj)) == 0
|
||||||
|
|
||||||
def test_hdlobjobj():
|
def test_hdlobjobj():
|
||||||
assert verify(ObjBench, HdlObjObj) == 0
|
assert verify(ObjBench(HdlObjObj)) == 0
|
||||||
|
|
||||||
def test_hdlobjattrsimple():
|
def test_hdlobjattrsimple():
|
||||||
assert verify(ObjBench, HdlObjAttrSimple) == 0
|
assert verify(ObjBench(HdlObjAttrSimple)) == 0
|
||||||
|
|
||||||
#def test_hdlobjattr():
|
#def test_hdlobjattr():
|
||||||
# # object attributes currently not supported, these
|
# # object attributes currently not supported, these
|
||||||
|
Loading…
x
Reference in New Issue
Block a user