2011-12-16 15:30:14 -05:00
|
|
|
from migen.fhdl.structure import *
|
2011-12-08 15:15:02 -05:00
|
|
|
|
2012-03-16 11:54:47 -04:00
|
|
|
(SP_WITHDRAW, SP_CE) = range(2)
|
|
|
|
|
2012-01-20 17:07:32 -05:00
|
|
|
class RoundRobin:
|
2012-03-16 11:54:47 -04:00
|
|
|
def __init__(self, n, switch_policy=SP_WITHDRAW):
|
2011-12-08 15:15:02 -05:00
|
|
|
self.n = n
|
2011-12-16 15:30:14 -05:00
|
|
|
self.bn = bits_for(self.n-1)
|
2011-12-18 15:54:28 -05:00
|
|
|
self.request = Signal(BV(self.n))
|
|
|
|
self.grant = Signal(BV(self.bn))
|
2012-03-16 11:54:47 -04:00
|
|
|
self.switch_policy = switch_policy
|
|
|
|
if self.switch_policy == SP_CE:
|
|
|
|
self.ce = Signal()
|
2011-12-08 15:15:02 -05:00
|
|
|
|
2011-12-16 10:02:55 -05:00
|
|
|
def get_fragment(self):
|
2012-03-31 12:01:40 -04:00
|
|
|
if self.n > 1:
|
|
|
|
cases = []
|
|
|
|
for i in range(self.n):
|
|
|
|
switch = []
|
|
|
|
for j in reversed(range(i+1,i+self.n)):
|
|
|
|
t = j % self.n
|
|
|
|
switch = [
|
|
|
|
If(self.request[t],
|
2012-11-28 17:18:43 -05:00
|
|
|
self.grant.eq(t)
|
2012-03-31 12:01:40 -04:00
|
|
|
).Else(
|
|
|
|
*switch
|
|
|
|
)
|
|
|
|
]
|
|
|
|
if self.switch_policy == SP_WITHDRAW:
|
|
|
|
case = [If(~self.request[i], *switch)]
|
|
|
|
else:
|
|
|
|
case = switch
|
2012-11-28 17:18:43 -05:00
|
|
|
cases.append([i] + case)
|
2012-03-31 12:01:40 -04:00
|
|
|
statement = Case(self.grant, *cases)
|
|
|
|
if self.switch_policy == SP_CE:
|
|
|
|
statement = If(self.ce, statement)
|
|
|
|
return Fragment(sync=[statement])
|
|
|
|
else:
|
|
|
|
return Fragment([self.grant.eq(0)])
|