2013-05-22 11:11:09 -04:00
|
|
|
from migen.fhdl.std import *
|
2011-12-08 15:15:02 -05:00
|
|
|
|
2012-03-16 11:54:47 -04:00
|
|
|
(SP_WITHDRAW, SP_CE) = range(2)
|
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2013-05-22 11:11:09 -04:00
|
|
|
class RoundRobin(Module):
|
2015-04-13 14:07:07 -04:00
|
|
|
def __init__(self, n, switch_policy=SP_WITHDRAW):
|
|
|
|
self.request = Signal(n)
|
|
|
|
self.grant = Signal(max=max(2, n))
|
|
|
|
self.switch_policy = switch_policy
|
|
|
|
if self.switch_policy == SP_CE:
|
|
|
|
self.ce = Signal()
|
2014-10-17 05:08:37 -04:00
|
|
|
|
2015-04-13 14:07:07 -04:00
|
|
|
###
|
2013-05-22 11:11:09 -04:00
|
|
|
|
2015-04-13 14:07:07 -04:00
|
|
|
if n > 1:
|
|
|
|
cases = {}
|
|
|
|
for i in range(n):
|
|
|
|
switch = []
|
|
|
|
for j in reversed(range(i+1,i+n)):
|
|
|
|
t = j % n
|
|
|
|
switch = [
|
|
|
|
If(self.request[t],
|
|
|
|
self.grant.eq(t)
|
|
|
|
).Else(
|
|
|
|
*switch
|
|
|
|
)
|
|
|
|
]
|
|
|
|
if self.switch_policy == SP_WITHDRAW:
|
|
|
|
case = [If(~self.request[i], *switch)]
|
|
|
|
else:
|
|
|
|
case = switch
|
|
|
|
cases[i] = case
|
|
|
|
statement = Case(self.grant, cases)
|
|
|
|
if self.switch_policy == SP_CE:
|
|
|
|
statement = If(self.ce, statement)
|
|
|
|
self.sync += statement
|
|
|
|
else:
|
|
|
|
self.comb += self.grant.eq(0)
|