litex/milkymist/framebuffer/__init__.py

213 lines
5.4 KiB
Python
Raw Normal View History

2012-06-17 07:41:26 -04:00
from migen.fhdl.structure import *
2013-02-24 07:07:25 -05:00
from migen.fhdl.specials import Instance
from migen.fhdl.module import Module
from migen.genlib.record import Record
2013-04-25 13:43:26 -04:00
from migen.genlib.fifo import AsyncFIFO
2012-06-17 11:22:02 -04:00
from migen.flow.actor import *
from migen.flow.network import *
2012-12-14 09:54:16 -05:00
from migen.flow.transactions import *
from migen.bank.description import CSRStorage
from migen.actorlib import dma_asmi, structuring, sim, spi
2012-06-17 11:22:02 -04:00
2013-03-29 12:15:11 -04:00
_hbits = 11
_vbits = 12
2012-06-17 11:22:02 -04:00
2012-06-29 11:09:16 -04:00
_bpp = 32
_bpc = 10
_pixel_layout_s = [
2013-03-25 13:32:25 -04:00
("pad", _bpp-3*_bpc),
2012-11-29 17:38:04 -05:00
("r", _bpc),
("g", _bpc),
2013-03-25 13:32:25 -04:00
("b", _bpc)
2012-06-29 11:09:16 -04:00
]
_pixel_layout = [
("p0", _pixel_layout_s),
("p1", _pixel_layout_s)
]
2012-06-29 11:09:16 -04:00
_bpc_dac = 8
_dac_layout_s = [
("r", _bpc_dac),
("g", _bpc_dac),
("b", _bpc_dac)
]
2012-06-29 11:09:16 -04:00
_dac_layout = [
2012-11-29 17:38:04 -05:00
("hsync", 1),
("vsync", 1),
("p0", _dac_layout_s),
("p1", _dac_layout_s)
2012-06-29 11:09:16 -04:00
]
2012-10-09 15:11:26 -04:00
class _FrameInitiator(spi.SingleGenerator):
def __init__(self):
2012-06-17 11:22:02 -04:00
layout = [
("hres", _hbits, 640, 1),
("hsync_start", _hbits, 656, 1),
("hsync_end", _hbits, 752, 1),
("hscan", _hbits, 800, 1),
2012-10-09 15:11:26 -04:00
2012-11-29 17:38:04 -05:00
("vres", _vbits, 480),
("vsync_start", _vbits, 492),
("vsync_end", _vbits, 494),
("vscan", _vbits, 525)
2012-06-17 11:22:02 -04:00
]
spi.SingleGenerator.__init__(self, layout, spi.MODE_EXTERNAL)
2012-06-17 07:41:26 -04:00
2013-04-10 15:34:15 -04:00
class VTG(Module):
2012-06-29 11:09:16 -04:00
def __init__(self):
2013-04-10 15:34:15 -04:00
self.timing = Sink([
2012-11-29 17:38:04 -05:00
("hres", _hbits),
("hsync_start", _hbits),
("hsync_end", _hbits),
("hscan", _hbits),
("vres", _vbits),
("vsync_start", _vbits),
("vsync_end", _vbits),
2013-04-10 15:34:15 -04:00
("vscan", _vbits)])
self.pixels = Sink(_pixel_layout)
self.dac = Source(_dac_layout)
self.busy = Signal()
2012-07-01 11:03:40 -04:00
hactive = Signal()
vactive = Signal()
active = Signal()
generate_en = Signal()
2012-11-29 17:38:04 -05:00
hcounter = Signal(_hbits)
vcounter = Signal(_vbits)
2012-07-01 11:03:40 -04:00
2012-07-01 15:44:33 -04:00
skip = _bpc - _bpc_dac
self.comb += [
2012-07-01 11:03:40 -04:00
active.eq(hactive & vactive),
If(active,
2013-04-10 15:34:15 -04:00
[getattr(getattr(self.dac.payload, p), c).eq(getattr(getattr(self.pixels.payload, p), c)[skip:])
for p in ["p0", "p1"] for c in ["r", "g", "b"]]
2012-07-01 11:03:40 -04:00
),
2013-04-10 15:34:15 -04:00
generate_en.eq(self.timing.stb & (~active | self.pixels.stb)),
self.pixels.ack.eq(self.dac.ack & active),
self.dac.stb.eq(generate_en),
self.busy.eq(generate_en)
2012-07-01 11:03:40 -04:00
]
2013-04-10 15:34:15 -04:00
tp = self.timing.payload
self.sync += [
2013-04-10 15:34:15 -04:00
self.timing.ack.eq(0),
If(generate_en & self.dac.ack,
2012-07-01 11:03:40 -04:00
hcounter.eq(hcounter + 1),
If(hcounter == 0, hactive.eq(1)),
If(hcounter == tp.hres, hactive.eq(0)),
2013-04-10 15:34:15 -04:00
If(hcounter == tp.hsync_start, self.dac.payload.hsync.eq(1)),
If(hcounter == tp.hsync_end, self.dac.payload.hsync.eq(0)),
2012-07-01 11:03:40 -04:00
If(hcounter == tp.hscan,
hcounter.eq(0),
If(vcounter == tp.vscan,
2012-07-06 18:12:34 -04:00
vcounter.eq(0),
2013-04-10 15:34:15 -04:00
self.timing.ack.eq(1)
2012-07-01 11:03:40 -04:00
).Else(
vcounter.eq(vcounter + 1)
)
),
If(vcounter == 0, vactive.eq(1)),
If(vcounter == tp.vres, vactive.eq(0)),
2013-04-10 15:34:15 -04:00
If(vcounter == tp.vsync_start, self.dac.payload.vsync.eq(1)),
If(vcounter == tp.vsync_end, self.dac.payload.vsync.eq(0))
2012-07-01 11:03:40 -04:00
)
]
2012-06-29 11:09:16 -04:00
2013-04-10 15:34:15 -04:00
class FIFO(Module):
2012-06-29 11:09:16 -04:00
def __init__(self):
2013-04-10 15:34:15 -04:00
self.dac = Sink(_dac_layout)
self.busy = Signal()
2012-06-29 11:09:16 -04:00
self.vga_hsync_n = Signal()
self.vga_vsync_n = Signal()
2012-11-29 17:38:04 -05:00
self.vga_r = Signal(_bpc_dac)
self.vga_g = Signal(_bpc_dac)
self.vga_b = Signal(_bpc_dac)
2012-06-29 11:09:16 -04:00
###
data_width = 2+2*3*_bpc_dac
2013-04-25 13:43:26 -04:00
fifo = AsyncFIFO(data_width, 256)
self.add_submodule(fifo, {"write": "sys", "read": "vga"})
2013-04-10 15:34:15 -04:00
fifo_in = self.dac.payload
fifo_out = Record(_dac_layout)
self.comb += [
2013-04-25 13:43:26 -04:00
self.dac.ack.eq(fifo.writable),
fifo.we.eq(self.dac.stb),
fifo.din.eq(fifo_in.raw_bits()),
fifo_out.raw_bits().eq(fifo.dout),
self.busy.eq(0)
]
2012-06-29 10:11:05 -04:00
pix_parity = Signal()
self.sync.vga += [
pix_parity.eq(~pix_parity),
self.vga_hsync_n.eq(~fifo_out.hsync),
self.vga_vsync_n.eq(~fifo_out.vsync),
If(pix_parity,
self.vga_r.eq(fifo_out.p1.r),
self.vga_g.eq(fifo_out.p1.g),
self.vga_b.eq(fifo_out.p1.b)
).Else(
self.vga_r.eq(fifo_out.p0.r),
self.vga_g.eq(fifo_out.p0.g),
self.vga_b.eq(fifo_out.p0.b)
)
]
2013-04-25 13:43:26 -04:00
self.comb += fifo.re.eq(pix_parity)
2012-07-03 13:04:44 -04:00
def sim_fifo_gen():
while True:
2012-12-14 09:54:16 -05:00
t = Token("dac")
2012-07-03 13:04:44 -04:00
yield t
2012-07-07 05:34:22 -04:00
print("H/V:" + str(t.value["hsync"]) + str(t.value["vsync"])
+ " " + str(t.value["r"]) + " " + str(t.value["g"]) + " " + str(t.value["b"]))
2012-07-03 13:04:44 -04:00
class Framebuffer(Module):
2013-03-26 12:57:17 -04:00
def __init__(self, pads, asmiport, simulation=False):
pack_factor = asmiport.hub.dw//(2*_bpp)
2012-06-29 10:11:05 -04:00
packed_pixels = structuring.pack_layout(_pixel_layout, pack_factor)
2012-06-17 12:36:23 -04:00
fi = _FrameInitiator()
dma = spi.DMAReadController(dma_asmi.Reader(asmiport), spi.MODE_EXTERNAL, length_reset=640*480*4)
cast = structuring.Cast(asmiport.hub.dw, packed_pixels, reverse_to=True)
2012-12-12 16:52:55 -05:00
unpack = structuring.Unpack(pack_factor, _pixel_layout)
vtg = VTG()
2012-07-03 13:04:44 -04:00
if simulation:
2012-12-12 16:52:55 -05:00
fifo = sim.SimActor(sim_fifo_gen(), ("dac", Sink, _dac_layout))
2012-07-03 13:04:44 -04:00
else:
2012-12-12 16:52:55 -05:00
fifo = FIFO()
2012-06-17 11:22:02 -04:00
2012-06-17 12:36:23 -04:00
g = DataFlowGraph()
g.add_connection(fi, vtg, sink_ep="timing")
g.add_connection(dma, cast)
2012-06-29 10:11:05 -04:00
g.add_connection(cast, unpack)
2012-06-29 11:09:16 -04:00
g.add_connection(unpack, vtg, sink_ep="pixels")
g.add_connection(vtg, fifo)
self.submodules += CompositeActor(g)
self._enable = CSRStorage()
self.comb += [
fi.trigger.eq(self._enable.storage),
dma.generator.trigger.eq(self._enable.storage),
]
self._fi = fi
self._dma = dma
2012-06-17 11:22:02 -04:00
2013-03-26 12:57:17 -04:00
# Drive pads
2012-07-03 13:04:44 -04:00
if not simulation:
2013-03-26 12:57:17 -04:00
self.comb += [
pads.hsync_n.eq(fifo.vga_hsync_n),
pads.vsync_n.eq(fifo.vga_vsync_n),
pads.r.eq(fifo.vga_r),
pads.g.eq(fifo.vga_g),
pads.b.eq(fifo.vga_b)
]
self.comb += pads.psave_n.eq(1)
2013-03-30 12:28:15 -04:00
def get_csrs(self):
return [self._enable] + self._fi.get_csrs() + self._dma.get_csrs()