litex/milkymist/framebuffer/__init__.py

228 lines
6.3 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
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 *
2012-06-24 13:15:19 -04:00
from migen.flow import plumbing
2012-10-09 15:11:26 -04:00
from migen.actorlib import misc, dma_asmi, structuring, sim, spi
2012-06-17 11:22:02 -04:00
from migen.bank.description import *
from migen.bank import csrgen
_hbits = 11
_vbits = 11
2012-06-29 11:09:16 -04:00
_bpp = 32
_bpc = 10
_pixel_layout = [
2012-11-29 17:38:04 -05:00
("b", _bpc),
("g", _bpc),
("r", _bpc),
("pad", _bpp-3*_bpc)
2012-06-29 11:09:16 -04:00
]
_bpc_dac = 8
_dac_layout = [
2012-11-29 17:38:04 -05:00
("hsync", 1),
("vsync", 1),
("b", _bpc_dac),
("g", _bpc_dac),
("r", _bpc_dac)
2012-06-29 11:09:16 -04:00
]
2012-10-09 15:11:26 -04:00
class _FrameInitiator(spi.SingleGenerator):
2012-06-17 12:36:23 -04:00
def __init__(self, asmi_bits, length_bits, alignment_bits):
2012-06-17 11:22:02 -04:00
layout = [
2012-11-29 17:38:04 -05:00
("hres", _hbits, 640),
("hsync_start", _hbits, 656),
("hsync_end", _hbits, 752),
("hscan", _hbits, 799),
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, 524),
2012-10-09 15:11:26 -04:00
2012-11-29 17:38:04 -05:00
("base", asmi_bits, 0, alignment_bits),
("length", length_bits, 640*480*4, alignment_bits)
2012-06-17 11:22:02 -04:00
]
2012-12-18 08:55:58 -05:00
spi.SingleGenerator.__init__(self, layout, spi.MODE_CONTINUOUS)
2012-06-17 07:41:26 -04:00
2012-06-29 11:09:16 -04:00
class VTG(Actor):
def __init__(self):
2012-12-18 08:55:58 -05:00
Actor.__init__(self,
2012-06-29 11:09:16 -04:00
("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),
("vscan", _vbits)]),
2012-06-29 11:09:16 -04:00
("pixels", Sink, _pixel_layout),
("dac", Source, _dac_layout)
)
def get_fragment(self):
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
2012-07-01 11:03:40 -04:00
comb = [
active.eq(hactive & vactive),
If(active,
2012-07-01 15:44:33 -04:00
self.token("dac").r.eq(self.token("pixels").r[skip:]),
self.token("dac").g.eq(self.token("pixels").g[skip:]),
self.token("dac").b.eq(self.token("pixels").b[skip:])
2012-07-01 11:03:40 -04:00
),
generate_en.eq(self.endpoints["timing"].stb & (~active | self.endpoints["pixels"].stb)),
2012-07-01 11:03:40 -04:00
self.endpoints["pixels"].ack.eq(self.endpoints["dac"].ack & active),
self.endpoints["dac"].stb.eq(generate_en)
]
tp = self.token("timing")
sync = [
self.endpoints["timing"].ack.eq(0),
If(generate_en & self.endpoints["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)),
2012-07-01 12:43:39 -04:00
If(hcounter == tp.hsync_start, self.token("dac").hsync.eq(1)),
If(hcounter == tp.hsync_end, self.token("dac").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),
self.endpoints["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)),
2012-07-01 12:43:39 -04:00
If(vcounter == tp.vsync_start, self.token("dac").vsync.eq(1)),
If(vcounter == tp.vsync_end, self.token("dac").vsync.eq(0))
2012-07-01 11:03:40 -04:00
)
]
return Fragment(comb, sync)
2012-06-29 11:09:16 -04:00
class FIFO(Actor):
def __init__(self):
2012-12-18 08:55:58 -05:00
Actor.__init__(self, ("dac", Sink, _dac_layout))
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
def get_fragment(self):
2012-07-01 09:22:57 -04:00
data_width = 2+3*_bpc_dac
2012-12-06 14:57:00 -05:00
fifo_full = Signal()
fifo_write_en = Signal()
fifo_data_out = Signal(data_width)
fifo_data_in = Signal(data_width)
2012-07-01 09:22:57 -04:00
asfifo = Instance("asfifo",
Instance.Parameter("data_width", data_width),
Instance.Parameter("address_width", 8),
2012-12-06 14:57:00 -05:00
Instance.Output("data_out", fifo_data_out),
Instance.Output("empty"),
2012-11-29 17:38:04 -05:00
Instance.Input("read_en", 1),
Instance.ClockPort("clk_read", "vga"),
2012-12-06 14:57:00 -05:00
Instance.Input("data_in", fifo_data_in),
Instance.Output("full", fifo_full),
Instance.Input("write_en", fifo_write_en),
Instance.ClockPort("clk_write"),
2012-12-06 14:57:00 -05:00
Instance.Input("rst", 0))
2012-07-01 09:22:57 -04:00
t = self.token("dac")
2012-07-01 16:30:07 -04:00
return Fragment(
[
Cat(self.vga_hsync_n, self.vga_vsync_n, self.vga_r, self.vga_g, self.vga_b).eq(asfifo.get_io("data_out")),
2012-07-01 16:30:07 -04:00
2012-12-06 14:57:00 -05:00
self.endpoints["dac"].ack.eq(~fifo_full),
fifo_write_en.eq(self.endpoints["dac"].stb),
fifo_data_in.eq(Cat(~t.hsync, ~t.vsync, t.r, t.g, t.b)),
2012-07-01 16:30:07 -04:00
2012-12-06 14:57:00 -05:00
self.busy.eq(0)
2012-07-01 16:30:07 -04:00
],
2013-02-24 07:07:25 -05:00
specials={asfifo})
2012-06-29 10:11:05 -04:00
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
2012-06-17 07:41:26 -04:00
class Framebuffer:
2012-07-03 13:04:44 -04:00
def __init__(self, address, asmiport, simulation=False):
2012-06-17 11:22:02 -04:00
asmi_bits = asmiport.hub.aw
alignment_bits = bits_for(asmiport.hub.dw//8) - 1
2012-06-17 12:36:23 -04:00
length_bits = _hbits + _vbits + 2 - alignment_bits
2012-06-29 10:11:05 -04:00
pack_factor = asmiport.hub.dw//_bpp
packed_pixels = structuring.pack_layout(_pixel_layout, pack_factor)
2012-06-17 12:36:23 -04:00
2012-12-12 16:52:55 -05:00
fi = _FrameInitiator(asmi_bits, length_bits, alignment_bits)
adrloop = misc.IntSequence(length_bits, asmi_bits)
adrbuffer = AbstractActor(plumbing.Buffer)
dma = dma_asmi.Reader(asmiport)
datbuffer = AbstractActor(plumbing.Buffer)
cast = structuring.Cast(asmiport.hub.dw, packed_pixels)
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()
2012-07-06 18:12:34 -04:00
g.add_connection(fi, adrloop, source_subr=["length", "base"])
g.add_connection(adrloop, adrbuffer)
2012-06-17 12:36:23 -04:00
g.add_connection(adrbuffer, dma)
2012-07-12 12:56:17 -04:00
g.add_connection(dma, datbuffer)
g.add_connection(datbuffer, 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(fi, vtg, sink_ep="timing", source_subr=[
"hres", "hsync_start", "hsync_end", "hscan",
"vres", "vsync_start", "vsync_end", "vscan"])
g.add_connection(vtg, fifo)
self._comp_actor = CompositeActor(g, debugger=False)
2012-06-17 11:22:02 -04:00
2012-12-12 16:52:55 -05:00
self.bank = csrgen.Bank(fi.get_registers() + self._comp_actor.get_registers(),
2012-08-03 12:51:18 -04:00
address=address)
2012-06-17 11:22:02 -04:00
# Pads
2012-06-17 07:41:26 -04:00
self.vga_psave_n = Signal()
2012-07-03 13:04:44 -04:00
if not simulation:
2012-12-12 16:52:55 -05:00
self.vga_hsync_n = fifo.vga_hsync_n
self.vga_vsync_n = fifo.vga_vsync_n
2012-06-17 07:41:26 -04:00
self.vga_sync_n = Signal()
self.vga_blank_n = Signal()
2012-07-03 13:04:44 -04:00
if not simulation:
2012-12-12 16:52:55 -05:00
self.vga_r = fifo.vga_r
self.vga_g = fifo.vga_g
self.vga_b = fifo.vga_b
2012-06-17 07:41:26 -04:00
def get_fragment(self):
2012-06-17 11:22:02 -04:00
comb = [
self.vga_sync_n.eq(0),
self.vga_psave_n.eq(1),
self.vga_blank_n.eq(1)
]
2012-06-17 12:36:23 -04:00
return self.bank.get_fragment() \
+ self._comp_actor.get_fragment() \
+ Fragment(comb)