Skip to content

Commit

Permalink
Make enables optional
Browse files Browse the repository at this point in the history
  • Loading branch information
cdonovick committed Sep 22, 2023
1 parent 7846f13 commit 060101c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 18 deletions.
56 changes: 38 additions & 18 deletions peak/family.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ class RegBase: pass

class AttrRegBase: pass

def gen_register(self, T, init):
def gen_register(self, T, init, has_enable=True):
cls = type(self)
key = (cls, T, init)
key = (cls, T, init, has_enable)
try:
return _REG_CACHE[key]
except KeyError:
Expand All @@ -178,23 +178,41 @@ def gen_register(self, T, init):
# avoids circular import
from peak import Peak

@family.assemble(locals(), globals())
class Register(Peak, cls.RegBase):
def __init__(self):
self.value: T = T(init)
if has_enable:
@family.assemble(locals(), globals())
class Register(Peak, cls.RegBase):
def __init__(self):
self.value: T = T(init)

def __call__(self, value: T, en: family.Bit) -> T:
assert value is not None
retvalue = self.value
if en:
self.value = value
return retvalue

def prev(self) -> T:
# This is not quite right and doesn't match
# magma semantics completely. May only be used
# as a peak method prior to __call__
return self.value
else:
@family.assemble(locals(), globals())
class Register(Peak, cls.RegBase):
def __init__(self):
self.value: T = T(init)

def __call__(self, value: T, en: family.Bit) -> T:
assert value is not None
retvalue = self.value
if en:
def __call__(self, value: T) -> T:
retvalue = self.value
self.value = value
return retvalue
return retvalue

def prev(self) -> T:
# This is not quite right and doesn't match
# magma semantics completely. May only be used
# as a peak method prior to __call__
return self.value

def prev(self) -> T:
# This is not quite right and doesn't match
# magma semantics completely. May only be used
# as a peak method prior to __call__
return self.value

return _REG_CACHE.setdefault(key, Register)

Expand All @@ -215,6 +233,8 @@ def __init__(self):
self.value: T = T(init)

def _poke_(self, value):
if not isinstance(value, T):
raise TypeError(f'Expected value of type {T}')
self.value = value

def _peak_(self):
Expand Down Expand Up @@ -358,9 +378,9 @@ def Signed(self):
def Unsigned(self):
return m.UInt

def gen_register(self, T, init):
def gen_register(self, T, init, has_enable=True):
return m.Register(T, init,
has_enable=True,
has_enable=has_enable,
reset_type=m.AsyncReset,
name_map=m.generator.ParamDict(CE='en', I='value'),
)
Expand Down
16 changes: 16 additions & 0 deletions tests/test_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,22 @@ def test_register():
tester.step(2)
tester.compile_and_run("verilator", flags=["-Wno-fatal"])

def test_register_no_en():
m_family = MagmaFamily()
py_family = PyFamily()
Data = py_family.BitVector[16]
Bit = py_family.Bit
m_Reg = m_family.gen_register(m_family.BitVector[16], 0, has_enable=False)
py_reg = py_family.gen_register(Data, 0, has_enable=False)()
tester = fault.Tester(m_Reg, m_Reg.CLK)

for _ in range(32):
val = Data.random(16)
gold = py_reg(val)
tester.circuit.value = val
tester.circuit.O.expect(gold)
tester.step(2)
tester.compile_and_run("verilator", flags=["-Wno-fatal"])

def test_attr_register():
@family_closure
Expand Down

0 comments on commit 060101c

Please sign in to comment.