litex/migen/corelogic/timeline.py

32 lines
1.1 KiB
Python
Raw Normal View History

2011-12-16 15:30:14 -05:00
from migen.fhdl.structure import *
2011-12-10 19:11:13 -05:00
class Inst:
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-16 15:30:14 -05:00
declare_signal(self, "_counter", BV(bits_for(self.lastevent)))
2011-12-10 19:11:13 -05:00
2011-12-16 10:02:55 -05:00
def get_fragment(self):
2011-12-16 15:30:14 -05:00
counterlogic = If(self._counter != Constant(0, self._counter.bv),
self._counter.eq(self._counter + Constant(1, self._counter.bv))
).Elif(self.trigger,
self._counter.eq(Constant(1, self._counter.bv))
)
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-16 15:30:14 -05:00
counterlogic = If(self._counter == self.lastevent,
self._counter.eq(Constant(0, self._counter.bv))
).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-16 15:30:14 -05:00
return self.trigger & (self._counter == Constant(0, self._counter.bv))
2011-12-10 19:11:13 -05:00
else:
2011-12-16 15:30:14 -05:00
return self._counter == Constant(e[0], self._counter.bv)
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)