litex/milkymist/timer/__init__.py

27 lines
691 B
Python
Raw Normal View History

2012-05-21 13:46:04 -04:00
from migen.fhdl.structure import *
from migen.fhdl.module import Module
2012-05-21 13:46:04 -04:00
from migen.bank.description import *
from migen.bank.eventmanager import *
2013-03-30 12:28:15 -04:00
class Timer(Module, AutoCSR):
def __init__(self, width=32):
2013-03-30 12:28:15 -04:00
self._en = CSRStorage()
self._value = CSRStorage(width, write_from_dev=True)
self._reload = CSRStorage(width)
2012-05-21 13:46:04 -04:00
self.submodules.ev = EventManager()
self.ev.zero = EventSourceLevel()
self.ev.finalize()
###
2012-05-21 13:46:04 -04:00
self.comb += [
2013-03-30 12:28:15 -04:00
If(self._value.storage == 0,
self._value.dat_w.eq(self._reload.storage)
2012-05-21 13:46:04 -04:00
).Else(
2013-03-30 12:28:15 -04:00
self._value.dat_w.eq(self._value.storage - 1)
2012-05-21 13:46:04 -04:00
),
2013-03-30 12:28:15 -04:00
self._value.we.eq(self._en.storage),
self.ev.zero.trigger.eq(self._value.storage != 0)
2012-05-21 13:46:04 -04:00
]