2014-06-07 06:24:19 -04:00
|
|
|
# This file is Copyright (c) 2014 Florent Kermarrec <florent@enjoy-digital.fr>
|
|
|
|
# License: BSD
|
|
|
|
|
2015-04-13 14:54:19 -04:00
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import sys
|
2014-06-07 06:24:19 -04:00
|
|
|
|
|
|
|
from migen.fhdl.structure import _Fragment
|
2015-09-10 13:53:15 -04:00
|
|
|
from migen.build.generic_platform import *
|
|
|
|
from migen.build import tools
|
|
|
|
from migen.build.xilinx import common
|
2014-06-07 06:24:19 -04:00
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2014-06-07 06:24:19 -04:00
|
|
|
def _format_constraint(c):
|
2015-04-13 14:07:07 -04:00
|
|
|
if isinstance(c, Pins):
|
|
|
|
return "set_property LOC " + c.identifiers[0]
|
|
|
|
elif isinstance(c, IOStandard):
|
|
|
|
return "set_property IOSTANDARD " + c.name
|
|
|
|
elif isinstance(c, Drive):
|
|
|
|
return "set_property DRIVE " + str(c.strength)
|
|
|
|
elif isinstance(c, Misc):
|
|
|
|
return "set_property " + c.misc.replace("=", " ")
|
|
|
|
else:
|
2015-04-13 15:11:13 -04:00
|
|
|
raise ValueError("unknown constraint {}".format(c))
|
2014-06-07 06:24:19 -04:00
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2015-03-06 16:56:27 -05:00
|
|
|
def _format_xdc(signame, resname, *constraints):
|
2015-04-13 14:07:07 -04:00
|
|
|
fmt_c = [_format_constraint(c) for c in constraints]
|
|
|
|
fmt_r = resname[0] + ":" + str(resname[1])
|
|
|
|
if resname[2] is not None:
|
|
|
|
fmt_r += "." + resname[2]
|
2015-04-13 15:11:13 -04:00
|
|
|
r = " ## {}\n".format(fmt_r)
|
2015-04-13 14:07:07 -04:00
|
|
|
for c in fmt_c:
|
|
|
|
r += c + " [get_ports " + signame + "]\n"
|
|
|
|
return r
|
2014-06-07 06:24:19 -04:00
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2014-06-07 06:24:19 -04:00
|
|
|
def _build_xdc(named_sc, named_pc):
|
2015-04-13 14:07:07 -04:00
|
|
|
r = ""
|
|
|
|
for sig, pins, others, resname in named_sc:
|
|
|
|
if len(pins) > 1:
|
|
|
|
for i, p in enumerate(pins):
|
|
|
|
r += _format_xdc(sig + "[" + str(i) + "]", resname, Pins(p), *others)
|
|
|
|
elif pins:
|
|
|
|
r += _format_xdc(sig, resname, Pins(pins[0]), *others)
|
|
|
|
else:
|
|
|
|
r += _format_xdc(sig, resname, *others)
|
|
|
|
if named_pc:
|
|
|
|
r += "\n" + "\n\n".join(named_pc)
|
|
|
|
return r
|
2014-06-07 06:24:19 -04:00
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2014-07-27 21:30:08 -04:00
|
|
|
def _run_vivado(build_name, vivado_path, source, ver=None):
|
2015-04-13 14:07:07 -04:00
|
|
|
if sys.platform == "win32" or sys.platform == "cygwin":
|
2015-09-10 13:53:15 -04:00
|
|
|
build_script_contents = "REM Autogenerated by Migen\n"
|
2015-04-13 14:07:07 -04:00
|
|
|
build_script_contents += "vivado -mode batch -source " + build_name + ".tcl\n"
|
|
|
|
build_script_file = "build_" + build_name + ".bat"
|
|
|
|
tools.write_to_file(build_script_file, build_script_contents)
|
|
|
|
r = subprocess.call([build_script_file])
|
|
|
|
else:
|
2015-09-10 13:53:15 -04:00
|
|
|
build_script_contents = "# Autogenerated by Migen\nset -e\n"
|
2015-04-13 14:07:07 -04:00
|
|
|
settings = common.settings(vivado_path, ver)
|
|
|
|
build_script_contents += "source " + settings + "\n"
|
|
|
|
build_script_contents += "vivado -mode batch -source " + build_name + ".tcl\n"
|
|
|
|
build_script_file = "build_" + build_name + ".sh"
|
|
|
|
tools.write_to_file(build_script_file, build_script_contents)
|
|
|
|
r = subprocess.call(["bash", build_script_file])
|
|
|
|
|
|
|
|
if r != 0:
|
|
|
|
raise OSError("Subprocess failed")
|
2014-06-07 06:24:19 -04:00
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2015-03-13 18:17:45 -04:00
|
|
|
class XilinxVivadoToolchain:
|
2015-04-13 14:07:07 -04:00
|
|
|
def __init__(self):
|
|
|
|
self.bitstream_commands = []
|
|
|
|
self.additional_commands = []
|
|
|
|
self.pre_synthesis_commands = []
|
|
|
|
self.with_phys_opt = False
|
|
|
|
|
|
|
|
def _build_batch(self, platform, sources, build_name):
|
|
|
|
tcl = []
|
2015-04-16 07:07:28 -04:00
|
|
|
for filename, language, library in sources:
|
2015-10-18 21:40:44 -04:00
|
|
|
filename_tcl = "{" + filename + "}"
|
|
|
|
tcl.append("add_files " + filename_tcl)
|
|
|
|
tcl.append("set_property library {} [get_files {}]"
|
|
|
|
.format(library, filename_tcl))
|
2015-04-13 14:07:07 -04:00
|
|
|
|
2015-04-13 15:11:13 -04:00
|
|
|
tcl.append("read_xdc {}.xdc".format(build_name))
|
2015-04-13 14:07:07 -04:00
|
|
|
tcl.extend(c.format(build_name=build_name) for c in self.pre_synthesis_commands)
|
2015-04-13 15:11:13 -04:00
|
|
|
tcl.append("synth_design -top top -part {} -include_dirs {{{}}}".format(platform.device, " ".join(platform.verilog_include_paths)))
|
|
|
|
tcl.append("report_utilization -hierarchical -file {}_utilization_hierarchical_synth.rpt".format(build_name))
|
|
|
|
tcl.append("report_utilization -file {}_utilization_synth.rpt".format(build_name))
|
2015-04-13 14:07:07 -04:00
|
|
|
tcl.append("place_design")
|
|
|
|
if self.with_phys_opt:
|
|
|
|
tcl.append("phys_opt_design -directive AddRetime")
|
2015-04-13 15:11:13 -04:00
|
|
|
tcl.append("report_utilization -hierarchical -file {}_utilization_hierarchical_place.rpt".format(build_name))
|
|
|
|
tcl.append("report_utilization -file {}_utilization_place.rpt".format(build_name))
|
|
|
|
tcl.append("report_io -file {}_io.rpt".format(build_name))
|
|
|
|
tcl.append("report_control_sets -verbose -file {}_control_sets.rpt".format(build_name))
|
|
|
|
tcl.append("report_clock_utilization -file {}_clock_utilization.rpt".format(build_name))
|
2015-04-13 14:07:07 -04:00
|
|
|
tcl.append("route_design")
|
2015-04-13 15:11:13 -04:00
|
|
|
tcl.append("report_route_status -file {}_route_status.rpt".format(build_name))
|
|
|
|
tcl.append("report_drc -file {}_drc.rpt".format(build_name))
|
|
|
|
tcl.append("report_timing_summary -max_paths 10 -file {}_timing.rpt".format(build_name))
|
|
|
|
tcl.append("report_power -file {}_power.rpt".format(build_name))
|
2015-04-13 14:07:07 -04:00
|
|
|
for bitstream_command in self.bitstream_commands:
|
|
|
|
tcl.append(bitstream_command.format(build_name=build_name))
|
2015-04-13 15:11:13 -04:00
|
|
|
tcl.append("write_bitstream -force {}.bit ".format(build_name))
|
2015-04-13 14:07:07 -04:00
|
|
|
for additional_command in self.additional_commands:
|
|
|
|
tcl.append(additional_command.format(build_name=build_name))
|
|
|
|
tcl.append("quit")
|
|
|
|
tools.write_to_file(build_name + ".tcl", "\n".join(tcl))
|
|
|
|
|
|
|
|
def build(self, platform, fragment, build_dir="build", build_name="top",
|
|
|
|
vivado_path="/opt/Xilinx/Vivado", source=True, run=True):
|
|
|
|
tools.mkdir_noerror(build_dir)
|
|
|
|
os.chdir(build_dir)
|
|
|
|
|
|
|
|
if not isinstance(fragment, _Fragment):
|
|
|
|
fragment = fragment.get_fragment()
|
|
|
|
platform.finalize(fragment)
|
|
|
|
v_output = platform.get_verilog(fragment)
|
|
|
|
named_sc, named_pc = platform.resolve_signals(v_output.ns)
|
|
|
|
v_file = build_name + ".v"
|
|
|
|
v_output.write(v_file)
|
2015-04-16 07:07:28 -04:00
|
|
|
sources = platform.sources | {(v_file, "verilog", "work")}
|
2015-04-13 14:07:07 -04:00
|
|
|
self._build_batch(platform, sources, build_name)
|
|
|
|
tools.write_to_file(build_name + ".xdc", _build_xdc(named_sc, named_pc))
|
|
|
|
if run:
|
|
|
|
_run_vivado(build_name, vivado_path, source)
|
|
|
|
|
|
|
|
os.chdir("..")
|
|
|
|
|
|
|
|
return v_output.ns
|
|
|
|
|
|
|
|
def add_period_constraint(self, platform, clk, period):
|
|
|
|
platform.add_platform_command("""create_clock -name {clk} -period """ + \
|
|
|
|
str(period) + """ [get_ports {clk}]""", clk=clk)
|