2013-05-22 11:10:13 -04:00
|
|
|
from migen.fhdl.std import *
|
2013-06-25 16:25:10 -04:00
|
|
|
from migen.genlib.fsm import FSM, NextState
|
2013-05-06 03:58:12 -04:00
|
|
|
from migen.bank.description import *
|
2013-05-08 16:31:01 -04:00
|
|
|
from migen.bank.eventmanager import *
|
2013-05-06 03:58:12 -04:00
|
|
|
from migen.flow.actor import *
|
2013-06-11 08:18:16 -04:00
|
|
|
from migen.actorlib import dma_lasmi
|
2013-05-06 03:58:12 -04:00
|
|
|
|
2013-11-09 09:27:32 -05:00
|
|
|
from misoclib.dvisampler.common import frame_layout
|
2013-05-06 03:58:12 -04:00
|
|
|
|
2013-05-08 16:31:01 -04:00
|
|
|
# Slot status: EMPTY=0 LOADED=1 PENDING=2
|
|
|
|
class _Slot(Module, AutoCSR):
|
|
|
|
def __init__(self, addr_bits, alignment_bits):
|
|
|
|
self.ev_source = EventSourceLevel()
|
|
|
|
self.address = Signal(addr_bits)
|
2013-11-13 12:57:10 -05:00
|
|
|
self.address_reached = Signal(addr_bits)
|
2013-05-08 16:31:01 -04:00
|
|
|
self.address_valid = Signal()
|
|
|
|
self.address_done = Signal()
|
|
|
|
|
|
|
|
self._r_status = CSRStorage(2, write_from_dev=True)
|
2013-11-13 12:57:10 -05:00
|
|
|
self._r_address = CSRStorage(addr_bits + alignment_bits, alignment_bits=alignment_bits, write_from_dev=True)
|
2013-05-08 16:31:01 -04:00
|
|
|
|
|
|
|
###
|
|
|
|
|
|
|
|
self.comb += [
|
|
|
|
self.address.eq(self._r_address.storage),
|
|
|
|
self.address_valid.eq(self._r_status.storage[0]),
|
|
|
|
self._r_status.dat_w.eq(2),
|
|
|
|
self._r_status.we.eq(self.address_done),
|
2013-11-13 12:57:10 -05:00
|
|
|
self._r_address.dat_w.eq(self.address_reached),
|
|
|
|
self._r_address.we.eq(self.address_done),
|
2013-05-08 16:31:01 -04:00
|
|
|
self.ev_source.trigger.eq(self._r_status.storage[1])
|
|
|
|
]
|
|
|
|
|
|
|
|
class _SlotArray(Module, AutoCSR):
|
|
|
|
def __init__(self, nslots, addr_bits, alignment_bits):
|
2013-05-09 04:51:34 -04:00
|
|
|
self.submodules.ev = EventManager()
|
2013-05-08 16:31:01 -04:00
|
|
|
self.address = Signal(addr_bits)
|
2013-11-13 12:57:10 -05:00
|
|
|
self.address_reached = Signal(addr_bits)
|
2013-05-08 16:31:01 -04:00
|
|
|
self.address_valid = Signal()
|
|
|
|
self.address_done = Signal()
|
|
|
|
|
|
|
|
###
|
|
|
|
|
|
|
|
slots = [_Slot(addr_bits, alignment_bits) for i in range(nslots)]
|
|
|
|
for n, slot in enumerate(slots):
|
|
|
|
setattr(self.submodules, "slot"+str(n), slot)
|
|
|
|
setattr(self.ev, "slot"+str(n), slot.ev_source)
|
|
|
|
self.ev.finalize()
|
|
|
|
|
|
|
|
change_slot = Signal()
|
|
|
|
current_slot = Signal(max=nslots)
|
2013-05-09 04:51:50 -04:00
|
|
|
self.sync += If(change_slot, [If(slot.address_valid, current_slot.eq(n)) for n, slot in reversed(list(enumerate(slots)))])
|
2013-05-08 16:31:01 -04:00
|
|
|
self.comb += change_slot.eq(~self.address_valid | self.address_done)
|
|
|
|
|
|
|
|
self.comb += [
|
|
|
|
self.address.eq(Array(slot.address for slot in slots)[current_slot]),
|
|
|
|
self.address_valid.eq(Array(slot.address_valid for slot in slots)[current_slot])
|
|
|
|
]
|
2013-11-13 12:57:10 -05:00
|
|
|
self.comb += [slot.address_reached.eq(self.address_reached) for slot in slots]
|
2013-05-08 16:31:01 -04:00
|
|
|
self.comb += [slot.address_done.eq(self.address_done & (current_slot == n)) for n, slot in enumerate(slots)]
|
|
|
|
|
2013-05-06 03:58:12 -04:00
|
|
|
class DMA(Module):
|
2013-06-11 08:18:16 -04:00
|
|
|
def __init__(self, lasmim, nslots):
|
|
|
|
bus_aw = lasmim.aw
|
|
|
|
bus_dw = lasmim.dw
|
2013-05-08 16:31:01 -04:00
|
|
|
alignment_bits = bits_for(bus_dw//8) - 1
|
|
|
|
|
2013-05-06 03:58:12 -04:00
|
|
|
self.frame = Sink(frame_layout)
|
2013-05-08 16:31:01 -04:00
|
|
|
self._r_frame_size = CSRStorage(bus_aw + alignment_bits, alignment_bits=alignment_bits)
|
|
|
|
self.submodules._slot_array = _SlotArray(nslots, bus_aw, alignment_bits)
|
|
|
|
self.ev = self._slot_array.ev
|
2013-05-06 03:58:12 -04:00
|
|
|
|
|
|
|
###
|
|
|
|
|
2013-05-08 16:31:01 -04:00
|
|
|
# start of frame detection
|
2013-05-06 03:58:12 -04:00
|
|
|
sof = Signal()
|
|
|
|
parity_r = Signal()
|
|
|
|
self.sync += If(self.frame.stb & self.frame.ack, parity_r.eq(self.frame.payload.parity))
|
2013-05-08 16:31:01 -04:00
|
|
|
self.comb += sof.eq(parity_r ^ self.frame.payload.parity)
|
2013-05-06 03:58:12 -04:00
|
|
|
|
2013-05-08 16:31:01 -04:00
|
|
|
# address generator + maximum memory word count to prevent DMA buffer overrun
|
|
|
|
reset_words = Signal()
|
|
|
|
count_word = Signal()
|
|
|
|
last_word = Signal()
|
|
|
|
current_address = Signal(bus_aw)
|
|
|
|
mwords_remaining = Signal(bus_aw)
|
2013-11-13 12:57:10 -05:00
|
|
|
self.comb += [
|
|
|
|
self._slot_array.address_reached.eq(current_address),
|
|
|
|
last_word.eq(mwords_remaining == 1)
|
|
|
|
]
|
2013-05-06 03:58:12 -04:00
|
|
|
self.sync += [
|
2013-05-08 16:31:01 -04:00
|
|
|
If(reset_words,
|
|
|
|
current_address.eq(self._slot_array.address),
|
|
|
|
mwords_remaining.eq(self._r_frame_size.storage)
|
|
|
|
).Elif(count_word,
|
|
|
|
current_address.eq(current_address + 1),
|
|
|
|
mwords_remaining.eq(mwords_remaining - 1)
|
|
|
|
)
|
2013-05-06 03:58:12 -04:00
|
|
|
]
|
|
|
|
|
2013-05-08 16:31:01 -04:00
|
|
|
# pack pixels into memory words
|
|
|
|
write_pixel = Signal()
|
|
|
|
last_pixel = Signal()
|
|
|
|
cur_memory_word = Signal(bus_dw)
|
|
|
|
encoded_pixel = Signal(32)
|
2013-05-06 03:58:12 -04:00
|
|
|
self.comb += [
|
2013-05-08 16:31:01 -04:00
|
|
|
encoded_pixel.eq(Cat(
|
2013-05-09 05:27:24 -04:00
|
|
|
self.frame.payload.b[6:], self.frame.payload.b,
|
|
|
|
self.frame.payload.g[6:], self.frame.payload.g,
|
|
|
|
self.frame.payload.r[6:], self.frame.payload.r))
|
2013-05-06 03:58:12 -04:00
|
|
|
]
|
2013-05-08 16:31:01 -04:00
|
|
|
pack_factor = bus_dw//32
|
|
|
|
assert(pack_factor & (pack_factor - 1) == 0) # only support powers of 2
|
|
|
|
pack_counter = Signal(max=pack_factor)
|
|
|
|
self.comb += last_pixel.eq(pack_counter == (pack_factor - 1))
|
|
|
|
self.sync += If(write_pixel,
|
2013-05-08 16:50:40 -04:00
|
|
|
[If(pack_counter == (pack_factor-i-1),
|
|
|
|
cur_memory_word[32*i:32*(i+1)].eq(encoded_pixel)) for i in range(pack_factor)],
|
2013-05-08 16:31:01 -04:00
|
|
|
pack_counter.eq(pack_counter + 1)
|
|
|
|
)
|
|
|
|
|
|
|
|
# bus accessor
|
2013-06-25 13:14:13 -04:00
|
|
|
self.submodules._bus_accessor = dma_lasmi.Writer(lasmim)
|
2013-05-08 16:31:01 -04:00
|
|
|
self.comb += [
|
|
|
|
self._bus_accessor.address_data.payload.a.eq(current_address),
|
|
|
|
self._bus_accessor.address_data.payload.d.eq(cur_memory_word)
|
|
|
|
]
|
|
|
|
|
|
|
|
# control FSM
|
2013-06-25 16:25:10 -04:00
|
|
|
fsm = FSM()
|
2013-05-08 16:31:01 -04:00
|
|
|
self.submodules += fsm
|
|
|
|
|
2013-06-25 16:25:10 -04:00
|
|
|
fsm.act("WAIT_SOF",
|
2013-05-08 16:31:01 -04:00
|
|
|
reset_words.eq(1),
|
2013-05-08 16:50:40 -04:00
|
|
|
self.frame.ack.eq(~self._slot_array.address_valid | ~sof),
|
2013-06-25 16:25:10 -04:00
|
|
|
If(self._slot_array.address_valid & sof & self.frame.stb, NextState("TRANSFER_PIXEL"))
|
2013-05-08 16:31:01 -04:00
|
|
|
)
|
2013-06-25 16:25:10 -04:00
|
|
|
fsm.act("TRANSFER_PIXEL",
|
2013-05-08 16:31:01 -04:00
|
|
|
self.frame.ack.eq(1),
|
|
|
|
If(self.frame.stb,
|
|
|
|
write_pixel.eq(1),
|
2013-06-25 16:25:10 -04:00
|
|
|
If(last_pixel, NextState("TO_MEMORY"))
|
2013-05-08 16:31:01 -04:00
|
|
|
)
|
|
|
|
)
|
2013-06-25 16:25:10 -04:00
|
|
|
fsm.act("TO_MEMORY",
|
2013-05-08 16:31:01 -04:00
|
|
|
self._bus_accessor.address_data.stb.eq(1),
|
|
|
|
If(self._bus_accessor.address_data.ack,
|
|
|
|
count_word.eq(1),
|
|
|
|
If(last_word,
|
2013-06-25 16:25:10 -04:00
|
|
|
NextState("EOF")
|
2013-05-08 16:31:01 -04:00
|
|
|
).Else(
|
2013-06-25 16:25:10 -04:00
|
|
|
NextState("TRANSFER_PIXEL")
|
2013-05-08 16:31:01 -04:00
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
2013-06-25 16:25:10 -04:00
|
|
|
fsm.act("EOF",
|
2013-05-08 16:31:01 -04:00
|
|
|
If(~self._bus_accessor.busy,
|
|
|
|
self._slot_array.address_done.eq(1),
|
2013-06-25 16:25:10 -04:00
|
|
|
NextState("WAIT_SOF")
|
2013-05-08 16:31:01 -04:00
|
|
|
)
|
|
|
|
)
|
2013-05-06 03:58:12 -04:00
|
|
|
|
|
|
|
def get_csrs(self):
|
2013-05-08 16:31:01 -04:00
|
|
|
return [self._r_frame_size] + self._slot_array.get_csrs()
|