fix nmigen imports
[nmigen-soc.git] / nmigen_soc / scheduler.py
1 from nmigen import Signal, Elaboratable, Module
2
3
4 __all__ = ["RoundRobin"]
5
6
7 class RoundRobin(Elaboratable):
8 """A round-robin scheduler.
9
10 Parameters
11 ----------
12 n : int
13 Maximum number of requests to handle.
14
15 Attributes
16 ----------
17 request : Signal(n)
18 Signal where a '1' on the i-th bit represents an incoming request
19 from the i-th device.
20 grant : Signal(range(n))
21 Signal that equals to the index of the device which is currently
22 granted access.
23 stb : Signal()
24 Strobe signal to enable granting access to the next device
25 requesting. Externally driven.
26 """
27
28 def __init__(self, n):
29 self.n = n
30 self.request = Signal(n)
31 self.grant = Signal(range(n))
32 self.stb = Signal()
33
34 def elaborate(self, platform):
35 m = Module()
36
37 with m.If(self.stb):
38 with m.Switch(self.grant):
39 for i in range(self.n):
40 with m.Case(i):
41 for j in reversed(range(i + 1, i + self.n)):
42 # If i+1 <= j < n, then t == j; (after i)
43 # If n <= j < i+n, then t == j - n (before i)
44 t = j % self.n
45 with m.If(self.request[t]):
46 m.d.sync += self.grant.eq(t)
47
48 return m