|
1 | 1 | from .. import * |
| 2 | +from math import gcd |
2 | 3 |
|
3 | 4 |
|
4 | | -__all__ = ["MultiReg", "ResetSynchronizer"] |
| 5 | +__all__ = ["MultiReg", "ResetSynchronizer", "PulseSynchronizer", "Gearbox"] |
| 6 | + |
| 7 | + |
| 8 | +def _incr(signal, modulo): |
| 9 | + if modulo == 2 ** len(signal): |
| 10 | + return signal + 1 |
| 11 | + else: |
| 12 | + return Mux(signal == modulo - 1, 0, signal + 1) |
5 | 13 |
|
6 | 14 |
|
7 | 15 | class MultiReg(Elaboratable): |
@@ -114,3 +122,128 @@ def elaborate(self, platform): |
114 | 122 | ResetSignal(self.domain).eq(self._regs[-1]) |
115 | 123 | ] |
116 | 124 | return m |
| 125 | + |
| 126 | + |
| 127 | +class PulseSynchronizer(Elaboratable): |
| 128 | + """A one-clock pulse on the input produces a one-clock pulse on the output. |
| 129 | +
|
| 130 | + If the output clock is faster than the input clock, then the input may be safely asserted at |
| 131 | + 100% duty cycle. Otherwise, if the clock ratio is n : 1, the input may be asserted at most once |
| 132 | + in every n input clocks, else pulses may be dropped. |
| 133 | +
|
| 134 | + Other than this there is no constraint on the ratio of input and output clock frequency. |
| 135 | +
|
| 136 | + Parameters |
| 137 | + ---------- |
| 138 | +
|
| 139 | + idomain : str |
| 140 | + Name of input clock domain. |
| 141 | + odomain : str |
| 142 | + Name of output clock domain. |
| 143 | + sync_stages : int |
| 144 | + Number of synchronisation flops between the two clock domains. 2 is the default, and |
| 145 | + minimum safe value. High-frequency designs may choose to increase this. |
| 146 | + """ |
| 147 | + def __init__(self, idomain, odomain, sync_stages=2): |
| 148 | + if not isinstance(sync_stages, int) or sync_stages < 1: |
| 149 | + raise TypeError("sync_stages must be a positive integer, not '{!r}'".format(sync_stages)) |
| 150 | + |
| 151 | + self.i = Signal() |
| 152 | + self.o = Signal() |
| 153 | + self.idomain = idomain |
| 154 | + self.odomain = odomain |
| 155 | + self.sync_stages = sync_stages |
| 156 | + |
| 157 | + def elaborate(self, platform): |
| 158 | + m = Module() |
| 159 | + |
| 160 | + itoggle = Signal() |
| 161 | + otoggle = Signal() |
| 162 | + mreg = m.submodules.mreg = \ |
| 163 | + MultiReg(itoggle, otoggle, odomain=self.odomain, n=self.sync_stages) |
| 164 | + otoggle_prev = Signal() |
| 165 | + |
| 166 | + m.d[self.idomain] += itoggle.eq(itoggle ^ self.i) |
| 167 | + m.d[self.odomain] += otoggle_prev.eq(otoggle) |
| 168 | + m.d.comb += self.o.eq(otoggle ^ otoggle_prev) |
| 169 | + |
| 170 | + return m |
| 171 | + |
| 172 | + |
| 173 | +class Gearbox(Elaboratable): |
| 174 | + """Adapt the width of a continous datastream. |
| 175 | +
|
| 176 | + Input: m bits wide, clock frequency f MHz. |
| 177 | + Output: n bits wide, clock frequency m / n * f MHz. |
| 178 | +
|
| 179 | + Used to adjust width of a datastream when interfacing system logic to a SerDes. The input and |
| 180 | + output clocks must be derived from the same reference clock, to maintain distance between |
| 181 | + read and write pointers. |
| 182 | +
|
| 183 | + Parameters |
| 184 | + ---------- |
| 185 | + iwidth : int |
| 186 | + Bit width of the input |
| 187 | + idomain : str |
| 188 | + Name of input clock domain |
| 189 | + owidth : int |
| 190 | + Bit width of the output |
| 191 | + odomain : str |
| 192 | + Name of output clock domain |
| 193 | +
|
| 194 | + Attributes |
| 195 | + ---------- |
| 196 | + i : Signal(iwidth), in |
| 197 | + Input datastream. Sampled on every input clock. |
| 198 | + o : Signal(owidth), out |
| 199 | + Output datastream. Transitions on every output clock. |
| 200 | + """ |
| 201 | + def __init__(self, iwidth, idomain, owidth, odomain): |
| 202 | + if not isinstance(iwidth, int) or iwidth < 1: |
| 203 | + raise TypeError("iwidth must be a positive integer, not '{!r}'".format(iwidth)) |
| 204 | + if not isinstance(owidth, int) or owidth < 1: |
| 205 | + raise TypeError("owidth must be a positive integer, not '{!r}'".format(owidth)) |
| 206 | + |
| 207 | + self.i = Signal(iwidth) |
| 208 | + self.o = Signal(owidth) |
| 209 | + self.iwidth = iwidth |
| 210 | + self.idomain = idomain |
| 211 | + self.owidth = owidth |
| 212 | + self.odomain = odomain |
| 213 | + |
| 214 | + storagesize = iwidth * owidth // gcd(iwidth, owidth) |
| 215 | + while storagesize // iwidth < 4: |
| 216 | + storagesize *= 2 |
| 217 | + while storagesize // owidth < 4: |
| 218 | + storagesize *= 2 |
| 219 | + |
| 220 | + self._storagesize = storagesize |
| 221 | + self._ichunks = storagesize // self.iwidth |
| 222 | + self._ochunks = storagesize // self.owidth |
| 223 | + assert(self._ichunks * self.iwidth == storagesize) |
| 224 | + assert(self._ochunks * self.owidth == storagesize) |
| 225 | + |
| 226 | + def elaborate(self, platform): |
| 227 | + m = Module() |
| 228 | + |
| 229 | + storage = Signal(self._storagesize, attrs={"no_retiming": True}) |
| 230 | + i_faster = self._ichunks > self._ochunks |
| 231 | + iptr = Signal(max=self._ichunks - 1, reset=(self._ichunks // 2 if i_faster else 0)) |
| 232 | + optr = Signal(max=self._ochunks - 1, reset=(0 if i_faster else self._ochunks // 2)) |
| 233 | + |
| 234 | + m.d[self.idomain] += iptr.eq(_incr(iptr, self._storagesize)) |
| 235 | + m.d[self.odomain] += optr.eq(_incr(optr, self._storagesize)) |
| 236 | + |
| 237 | + with m.Switch(iptr): |
| 238 | + for n in range(self._ichunks): |
| 239 | + s = slice(n * self.iwidth, (n + 1) * self.iwidth) |
| 240 | + with m.Case(n): |
| 241 | + m.d[self.idomain] += storage[s].eq(self.i) |
| 242 | + |
| 243 | + with m.Switch(optr): |
| 244 | + for n in range(self._ochunks): |
| 245 | + s = slice(n * self.owidth, (n + 1) * self.owidth) |
| 246 | + with m.Case(n): |
| 247 | + m.d[self.odomain] += self.o.eq(storage[s]) |
| 248 | + |
| 249 | + return m |
0 commit comments