litex/migen/corelogic/record.py

79 lines
1.9 KiB
Python
Raw Normal View History

2012-01-06 05:20:44 -05:00
from migen.fhdl.structure import *
from migen.fhdl.structure import _make_signal_name
class Record:
def __init__(self, template, name=None):
self.name = name or _make_signal_name()
for f in template:
if isinstance(f, tuple):
if isinstance(f[1], BV):
setattr(self, f[0], Signal(f[1], self.name + "_" + f[0]))
elif isinstance(f[1], Signal) or isinstance(f[1], Record):
setattr(self, f[0], f[1])
elif isinstance(f[1], list):
setattr(self, f[0], Record(f[1], self.name + "_" + f[0]))
else:
raise TypeError
else:
setattr(self, f, Signal(BV(1), self.name + "_" + f))
def template(self):
l = []
2012-01-06 17:00:23 -05:00
for key in sorted(self.__dict__):
2012-01-06 05:20:44 -05:00
e = self.__dict__[key]
if isinstance(e, Signal):
l.append((key, e.bv))
elif isinstance(e, Record):
l.append((key, e.template()))
return l
def copy(self, name=None):
return Record(self.template(), name or _make_signal_name())
def subrecord(self, *descr):
fields = {}
for item in descr:
path = item.split('/')
last = path.pop()
pos_self = self
pos_fields = fields
for hop in path:
pos_self = getattr(pos_self, hop)
try:
pos_fields = fields[hop]
except KeyError:
pos_fields = fields[hop] = {}
if not isinstance(pos_fields, dict):
raise ValueError
if last in pos_fields:
raise ValueError
pos_fields[last] = getattr(pos_self, last)
def dict_to_list(d):
l = []
for key in d:
e = d[key]
if isinstance(e, dict):
l.append((key, dict_to_list(e)))
else:
l.append((key, e))
return l
return Record(dict_to_list(fields), "subrecord")
2012-01-06 17:00:23 -05:00
def compatible(self, other):
tpl1 = self.template()
tpl2 = other.template()
return tpl1 == tpl2
2012-01-06 05:20:44 -05:00
def flatten(self):
l = []
for key in sorted(self.__dict__):
e = self.__dict__[key]
if isinstance(e, Signal):
l.append(e)
elif isinstance(e, Record):
l += e.flatten()
return l
2012-01-06 18:33:28 -05:00
def __repr__(self):
return repr(self.template())