2013-05-22 11:11:09 -04:00
|
|
|
from migen.fhdl.std import *
|
2013-02-22 17:19:37 -05:00
|
|
|
from migen.genlib.record import *
|
|
|
|
from migen.genlib.fsm import *
|
2012-01-15 16:08:33 -05:00
|
|
|
from migen.flow.actor import *
|
|
|
|
|
|
|
|
# Generates integers from start to maximum-1
|
2013-04-10 13:12:42 -04:00
|
|
|
class IntSequence(Module):
|
2012-07-06 18:10:23 -04:00
|
|
|
def __init__(self, nbits, offsetbits=0, step=1):
|
2013-04-10 13:12:42 -04:00
|
|
|
parameters_layout = [("maximum", nbits)]
|
|
|
|
if offsetbits:
|
|
|
|
parameters_layout.append(("offset", offsetbits))
|
2012-06-17 15:19:47 -04:00
|
|
|
|
2013-04-10 13:12:42 -04:00
|
|
|
self.parameters = Sink(parameters_layout)
|
|
|
|
self.source = Source([("value", max(nbits, offsetbits))])
|
|
|
|
self.busy = Signal()
|
|
|
|
|
|
|
|
###
|
2012-01-15 16:08:33 -05:00
|
|
|
|
|
|
|
load = Signal()
|
|
|
|
ce = Signal()
|
|
|
|
last = Signal()
|
|
|
|
|
2013-04-10 13:12:42 -04:00
|
|
|
maximum = Signal(nbits)
|
|
|
|
if offsetbits:
|
|
|
|
offset = Signal(offsetbits)
|
|
|
|
counter = Signal(nbits)
|
2012-01-15 16:08:33 -05:00
|
|
|
|
2013-04-10 13:12:42 -04:00
|
|
|
if step > 1:
|
|
|
|
self.comb += last.eq(counter + step >= maximum)
|
2012-01-15 16:08:33 -05:00
|
|
|
else:
|
2013-04-10 13:12:42 -04:00
|
|
|
self.comb += last.eq(counter + 1 == maximum)
|
|
|
|
self.sync += [
|
2012-01-15 16:08:33 -05:00
|
|
|
If(load,
|
2012-06-17 15:19:47 -04:00
|
|
|
counter.eq(0),
|
2013-04-10 13:12:42 -04:00
|
|
|
maximum.eq(self.parameters.payload.maximum),
|
|
|
|
offset.eq(self.parameters.payload.offset) if offsetbits else None
|
2012-06-17 15:19:47 -04:00
|
|
|
).Elif(ce,
|
|
|
|
If(last,
|
|
|
|
counter.eq(0)
|
|
|
|
).Else(
|
2013-04-10 13:12:42 -04:00
|
|
|
counter.eq(counter + step)
|
2012-06-17 15:19:47 -04:00
|
|
|
)
|
|
|
|
)
|
2012-01-15 16:08:33 -05:00
|
|
|
]
|
2013-04-10 13:12:42 -04:00
|
|
|
if offsetbits:
|
|
|
|
self.comb += self.source.payload.value.eq(counter + offset)
|
2012-07-06 18:10:23 -04:00
|
|
|
else:
|
2013-04-10 13:12:42 -04:00
|
|
|
self.comb += self.source.payload.value.eq(counter)
|
2012-01-15 16:08:33 -05:00
|
|
|
|
|
|
|
fsm = FSM("IDLE", "ACTIVE")
|
2013-04-10 13:12:42 -04:00
|
|
|
self.submodules += fsm
|
2012-01-15 16:08:33 -05:00
|
|
|
fsm.act(fsm.IDLE,
|
|
|
|
load.eq(1),
|
2013-04-10 13:12:42 -04:00
|
|
|
self.parameters.ack.eq(1),
|
|
|
|
If(self.parameters.stb, fsm.next_state(fsm.ACTIVE))
|
2012-01-15 16:08:33 -05:00
|
|
|
)
|
|
|
|
fsm.act(fsm.ACTIVE,
|
|
|
|
self.busy.eq(1),
|
2013-04-10 13:12:42 -04:00
|
|
|
self.source.stb.eq(1),
|
|
|
|
If(self.source.ack,
|
2012-01-15 16:08:33 -05:00
|
|
|
ce.eq(1),
|
|
|
|
If(last, fsm.next_state(fsm.IDLE))
|
|
|
|
)
|
|
|
|
)
|