mirror of
https://github.com/enjoy-digital/litex.git
synced 2025-01-04 09:52:26 -05:00
Better record layout parameterization mechanism
This commit is contained in:
parent
98f79021cd
commit
99c53ed9e8
3 changed files with 35 additions and 15 deletions
|
@ -13,7 +13,8 @@ _layout = [
|
||||||
|
|
||||||
class Interface(Record):
|
class Interface(Record):
|
||||||
def __init__(self, data_width=8):
|
def __init__(self, data_width=8):
|
||||||
Record.__init__(self, _layout, data_width=data_width)
|
Record.__init__(self, set_layout_parameters(_layout,
|
||||||
|
data_width=data_width))
|
||||||
|
|
||||||
class Interconnect(Module):
|
class Interconnect(Module):
|
||||||
def __init__(self, master, slaves):
|
def __init__(self, master, slaves):
|
||||||
|
|
|
@ -22,8 +22,9 @@ _layout = [
|
||||||
|
|
||||||
class Interface(Record):
|
class Interface(Record):
|
||||||
def __init__(self, data_width=32):
|
def __init__(self, data_width=32):
|
||||||
Record.__init__(self, _layout, data_width=data_width,
|
Record.__init__(self, set_layout_parameters(_layout,
|
||||||
sel_width=data_width//8)
|
data_width=data_width,
|
||||||
|
sel_width=data_width//8))
|
||||||
|
|
||||||
class InterconnectPointToPoint(Module):
|
class InterconnectPointToPoint(Module):
|
||||||
def __init__(self, master, slave):
|
def __init__(self, master, slave):
|
||||||
|
|
|
@ -11,21 +11,42 @@ from migen.genlib.misc import optree
|
||||||
# size can be an int, or a (int, bool) tuple for signed numbers
|
# size can be an int, or a (int, bool) tuple for signed numbers
|
||||||
# sublayout must be a list
|
# sublayout must be a list
|
||||||
|
|
||||||
def layout_len(layout, **layout_dict):
|
def set_layout_parameters(layout, **layout_dict):
|
||||||
r = 0
|
def resolve(p):
|
||||||
|
if isinstance(p, str):
|
||||||
|
try:
|
||||||
|
return layout_dict[p]
|
||||||
|
except KeyError:
|
||||||
|
return p
|
||||||
|
else:
|
||||||
|
return p
|
||||||
|
|
||||||
|
r = []
|
||||||
for f in layout:
|
for f in layout:
|
||||||
if isinstance(f[1], (int, tuple, str)): # cases 1/2
|
if isinstance(f[1], (int, tuple, str)): # cases 1/2
|
||||||
if(len(f) == 3):
|
if len(f) == 3:
|
||||||
|
r.append((f[0], resolve(f[1]), f[2]))
|
||||||
|
else:
|
||||||
|
r.append((f[0], resolve(f[1])))
|
||||||
|
elif isinstance(f[1], list): # case 3
|
||||||
|
r.append((f[0], set_layout_parameters(f[1], **layout_dict)))
|
||||||
|
else:
|
||||||
|
raise TypeError
|
||||||
|
return r
|
||||||
|
|
||||||
|
def layout_len(layout):
|
||||||
|
r = 0
|
||||||
|
for f in layout:
|
||||||
|
if isinstance(f[1], (int, tuple)): # cases 1/2
|
||||||
|
if len(f) == 3:
|
||||||
fname, fsize, fdirection = f
|
fname, fsize, fdirection = f
|
||||||
else:
|
else:
|
||||||
fname, fsize = f
|
fname, fsize = f
|
||||||
elif isinstance(f[1], list): # case 3
|
elif isinstance(f[1], list): # case 3
|
||||||
fname, fsublayout = f
|
fname, fsublayout = f
|
||||||
fsize = layout_len(fsublayout, **layout_dict)
|
fsize = layout_len(fsublayout)
|
||||||
else:
|
else:
|
||||||
raise TypeError
|
raise TypeError
|
||||||
if isinstance(fsize, str):
|
|
||||||
fsize = layout_dict[fsize]
|
|
||||||
if isinstance(fsize, tuple):
|
if isinstance(fsize, tuple):
|
||||||
r += fsize[0]
|
r += fsize[0]
|
||||||
else:
|
else:
|
||||||
|
@ -57,23 +78,20 @@ def layout_partial(layout, *elements):
|
||||||
return r
|
return r
|
||||||
|
|
||||||
class Record:
|
class Record:
|
||||||
def __init__(self, layout, name=None, **layout_dict):
|
def __init__(self, layout, name=None):
|
||||||
self.name = get_obj_var_name(name, "")
|
self.name = get_obj_var_name(name, "")
|
||||||
self.layout = layout
|
self.layout = layout
|
||||||
self.layout_dict = layout_dict
|
|
||||||
|
|
||||||
if self.name:
|
if self.name:
|
||||||
prefix = self.name + "_"
|
prefix = self.name + "_"
|
||||||
else:
|
else:
|
||||||
prefix = ""
|
prefix = ""
|
||||||
for f in self.layout:
|
for f in self.layout:
|
||||||
if isinstance(f[1], (int, tuple, str)): # cases 1/2
|
if isinstance(f[1], (int, tuple)): # cases 1/2
|
||||||
if(len(f) == 3):
|
if(len(f) == 3):
|
||||||
fname, fsize, fdirection = f
|
fname, fsize, fdirection = f
|
||||||
else:
|
else:
|
||||||
fname, fsize = f
|
fname, fsize = f
|
||||||
if isinstance(fsize, str):
|
|
||||||
fsize = layout_dict[fsize]
|
|
||||||
finst = Signal(fsize, name=prefix + fname)
|
finst = Signal(fsize, name=prefix + fname)
|
||||||
elif isinstance(f[1], list): # case 3
|
elif isinstance(f[1], list): # case 3
|
||||||
fname, fsublayout = f
|
fname, fsublayout = f
|
||||||
|
@ -144,7 +162,7 @@ class Record:
|
||||||
return r
|
return r
|
||||||
|
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
return layout_len(self.layout, **self.layout_dict)
|
return layout_len(self.layout)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "<Record " + ":".join(f[0] for f in self.layout) + " at " + hex(id(self)) + ">"
|
return "<Record " + ":".join(f[0] for f in self.layout) + " at " + hex(id(self)) + ">"
|
||||||
|
|
Loading…
Reference in a new issue