2011-12-16 15:30:14 -05:00
|
|
|
from migen.fhdl.structure import *
|
2011-12-10 19:11:13 -05:00
|
|
|
|
2012-01-20 17:07:32 -05:00
|
|
|
class Timeline:
|
2011-12-10 19:11:13 -05:00
|
|
|
def __init__(self, trigger, events):
|
|
|
|
self.trigger = trigger
|
|
|
|
self.events = events
|
2011-12-13 09:25:46 -05:00
|
|
|
self.lastevent = max([e[0] for e in events])
|
2011-12-10 19:11:13 -05:00
|
|
|
|
2011-12-16 10:02:55 -05:00
|
|
|
def get_fragment(self):
|
2011-12-18 15:47:48 -05:00
|
|
|
counter = Signal(BV(bits_for(self.lastevent)))
|
|
|
|
|
|
|
|
counterlogic = If(counter != Constant(0, counter.bv),
|
|
|
|
counter.eq(counter + Constant(1, counter.bv))
|
2011-12-16 15:30:14 -05:00
|
|
|
).Elif(self.trigger,
|
2011-12-18 15:47:48 -05:00
|
|
|
counter.eq(Constant(1, counter.bv))
|
2011-12-16 15:30:14 -05:00
|
|
|
)
|
2011-12-10 19:11:13 -05:00
|
|
|
# insert counter reset if it doesn't automatically overflow
|
|
|
|
# (test if self.lastevent+1 is a power of 2)
|
|
|
|
if (self.lastevent & (self.lastevent + 1)) != 0:
|
2011-12-18 15:47:48 -05:00
|
|
|
counterlogic = If(counter == self.lastevent,
|
|
|
|
counter.eq(Constant(0, counter.bv))
|
2011-12-16 15:30:14 -05:00
|
|
|
).Else(
|
|
|
|
counterlogic
|
|
|
|
)
|
2011-12-16 10:02:55 -05:00
|
|
|
def get_cond(e):
|
2011-12-13 09:25:46 -05:00
|
|
|
if e[0] == 0:
|
2011-12-18 15:47:48 -05:00
|
|
|
return self.trigger & (counter == Constant(0, counter.bv))
|
2011-12-10 19:11:13 -05:00
|
|
|
else:
|
2011-12-18 15:47:48 -05:00
|
|
|
return counter == Constant(e[0], counter.bv)
|
2011-12-16 15:30:14 -05:00
|
|
|
sync = [If(get_cond(e), *e[1]) for e in self.events]
|
2011-12-10 19:11:13 -05:00
|
|
|
sync.append(counterlogic)
|
2011-12-16 15:30:14 -05:00
|
|
|
return Fragment(sync=sync)
|