2012-02-06 11:39:32 -05:00
|
|
|
from migen.fhdl.structure import *
|
2013-03-10 14:29:05 -04:00
|
|
|
from migen.fhdl.module import Module
|
2012-02-06 11:39:32 -05:00
|
|
|
from migen.bank.description import *
|
2013-02-22 17:19:37 -05:00
|
|
|
from migen.genlib.misc import optree
|
2012-02-06 11:39:32 -05:00
|
|
|
|
2013-03-10 14:29:05 -04:00
|
|
|
class EventSource(HUID):
|
2012-02-06 11:39:32 -05:00
|
|
|
def __init__(self):
|
2013-03-10 14:29:05 -04:00
|
|
|
HUID.__init__(self)
|
2012-02-06 11:39:32 -05:00
|
|
|
self.trigger = Signal()
|
2012-05-21 13:56:23 -04:00
|
|
|
self.pending = Signal()
|
2012-02-06 11:39:32 -05:00
|
|
|
|
|
|
|
class EventSourcePulse(EventSource):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class EventSourceLevel(EventSource):
|
|
|
|
pass
|
|
|
|
|
2013-03-10 14:29:05 -04:00
|
|
|
class EventManager(Module, AutoReg):
|
|
|
|
def __init__(self):
|
2012-02-06 11:39:32 -05:00
|
|
|
self.irq = Signal()
|
2013-03-10 14:29:05 -04:00
|
|
|
|
|
|
|
def do_finalize(self):
|
|
|
|
sources_u = [v for v in self.__dict__.values() if isinstance(v, EventSource)]
|
|
|
|
sources = sorted(sources_u, key=lambda x: x.huid)
|
|
|
|
n = len(sources)
|
2012-02-06 11:39:32 -05:00
|
|
|
self.status = RegisterRaw("status", n)
|
|
|
|
self.pending = RegisterRaw("pending", n)
|
|
|
|
self.enable = RegisterFields("enable",
|
|
|
|
[Field("s" + str(i), access_bus=READ_WRITE, access_dev=READ_ONLY) for i in range(n)])
|
2013-03-10 14:29:05 -04:00
|
|
|
|
2012-02-06 11:39:32 -05:00
|
|
|
# status
|
2013-03-10 14:29:05 -04:00
|
|
|
for i, source in enumerate(sources):
|
2012-02-06 11:39:32 -05:00
|
|
|
if isinstance(source, EventSourcePulse):
|
2013-03-10 14:29:05 -04:00
|
|
|
self.comb += self.status.w[i].eq(0)
|
2012-02-06 11:39:32 -05:00
|
|
|
elif isinstance(source, EventSourceLevel):
|
2013-03-10 14:29:05 -04:00
|
|
|
self.comb += self.status.w[i].eq(source.trigger)
|
2012-02-06 11:39:32 -05:00
|
|
|
else:
|
|
|
|
raise TypeError
|
|
|
|
|
|
|
|
# pending
|
2013-03-10 14:29:05 -04:00
|
|
|
for i, source in enumerate(sources):
|
2012-02-06 11:39:32 -05:00
|
|
|
# W1C
|
2013-03-10 14:29:05 -04:00
|
|
|
self.sync += If(self.pending.re & self.pending.r[i], source.pending.eq(0))
|
2012-02-06 11:39:32 -05:00
|
|
|
if isinstance(source, EventSourcePulse):
|
|
|
|
# set on a positive trigger pulse
|
2013-03-10 14:29:05 -04:00
|
|
|
self.sync += If(source.trigger, source.pending.eq(1))
|
2012-02-06 11:39:32 -05:00
|
|
|
elif isinstance(source, EventSourceLevel):
|
|
|
|
# set on the falling edge of the trigger
|
|
|
|
old_trigger = Signal()
|
2013-03-10 14:29:05 -04:00
|
|
|
self.sync += [
|
2012-02-06 11:39:32 -05:00
|
|
|
old_trigger.eq(source.trigger),
|
2012-05-21 13:56:23 -04:00
|
|
|
If(~source.trigger & old_trigger, source.pending.eq(1))
|
2012-02-06 11:39:32 -05:00
|
|
|
]
|
|
|
|
else:
|
|
|
|
raise TypeError
|
2013-03-10 14:29:05 -04:00
|
|
|
self.comb += self.pending.w[i].eq(source.pending)
|
2012-02-06 11:39:32 -05:00
|
|
|
|
|
|
|
# IRQ
|
|
|
|
irqs = [self.pending.w[i] & field.r for i, field in enumerate(self.enable.fields)]
|
2013-03-10 14:29:05 -04:00
|
|
|
self.comb += self.irq.eq(optree("|", irqs))
|
|
|
|
|
|
|
|
def __setattr__(self, name, value):
|
|
|
|
if isinstance(value, EventSource) and self.finalized:
|
|
|
|
raise FinalizeError
|
|
|
|
object.__setattr__(self, name, value)
|