litex/migen/genlib/misc.py

89 lines
2.1 KiB
Python
Raw Normal View History

from migen.fhdl.structure import *
from migen.fhdl.module import Module
2015-09-17 05:22:44 -04:00
from migen.fhdl.bitcontainer import bits_for
2012-02-11 05:52:15 -05:00
2015-04-13 14:45:35 -04:00
2012-02-11 05:52:15 -05:00
def split(v, *counts):
r = []
offset = 0
for n in counts:
if n != 0:
r.append(v[offset:offset+n])
else:
r.append(None)
offset += n
return tuple(r)
2012-02-11 14:57:08 -05:00
2015-04-13 14:45:35 -04:00
def displacer(signal, shift, output, n=None, reverse=False):
if shift is None:
return output.eq(signal)
if n is None:
n = 2**flen(shift)
w = flen(signal)
if reverse:
r = reversed(range(n))
else:
r = range(n)
l = [Replicate(shift == i, w) & signal for i in r]
return output.eq(Cat(*l))
2012-02-11 14:57:08 -05:00
2015-04-13 14:45:35 -04:00
def chooser(signal, shift, output, n=None, reverse=False):
if shift is None:
return output.eq(signal)
if n is None:
n = 2**flen(shift)
w = flen(output)
cases = {}
for i in range(n):
if reverse:
s = n - i - 1
else:
s = i
cases[i] = [output.eq(signal[s*w:(s+1)*w])]
return Case(shift, cases).makedefault()
2015-04-13 14:45:35 -04:00
def timeline(trigger, events):
lastevent = max([e[0] for e in events])
counter = Signal(max=lastevent+1)
2014-10-17 05:08:37 -04:00
counterlogic = If(counter != 0,
counter.eq(counter + 1)
).Elif(trigger,
counter.eq(1)
)
# insert counter reset if it doesn't naturally overflow
# (test if lastevent+1 is a power of 2)
if (lastevent & (lastevent + 1)) != 0:
counterlogic = If(counter == lastevent,
counter.eq(0)
).Else(
counterlogic
)
2014-10-17 05:08:37 -04:00
def get_cond(e):
if e[0] == 0:
return trigger & (counter == 0)
else:
return counter == e[0]
sync = [If(get_cond(e), *e[1]) for e in events]
sync.append(counterlogic)
return sync
2015-04-13 14:45:35 -04:00
class WaitTimer(Module):
def __init__(self, t):
self.wait = Signal()
self.done = Signal()
# # #
count = Signal(bits_for(t), reset=t)
self.comb += self.done.eq(count == 0)
self.sync += \
If(self.wait,
If(~self.done, count.eq(count - 1))
).Else(count.eq(count.reset))