2014-06-07 06:24:19 -04:00
|
|
|
# This file is Copyright (c) 2014 Florent Kermarrec <florent@enjoy-digital.fr>
|
|
|
|
# License: BSD
|
|
|
|
|
|
|
|
import os, subprocess, sys
|
|
|
|
|
|
|
|
from migen.fhdl.std import *
|
|
|
|
from migen.fhdl.structure import _Fragment
|
|
|
|
from mibuild.generic_platform import *
|
2014-08-06 07:26:00 -04:00
|
|
|
|
|
|
|
from mibuild import tools, xilinx_common
|
2014-06-07 06:24:19 -04:00
|
|
|
|
|
|
|
def _format_constraint(c):
|
|
|
|
if isinstance(c, Pins):
|
|
|
|
return "set_property LOC " + c.identifiers[0]
|
|
|
|
elif isinstance(c, IOStandard):
|
2014-10-17 05:08:37 -04:00
|
|
|
return "set_property IOSTANDARD " + c.name
|
2014-06-07 06:24:19 -04:00
|
|
|
elif isinstance(c, Drive):
|
2014-06-24 11:22:11 -04:00
|
|
|
return "set_property DRIVE " + str(c.strength)
|
2014-06-07 06:24:19 -04:00
|
|
|
elif isinstance(c, Misc):
|
2014-07-29 09:04:27 -04:00
|
|
|
return "set_property " + c.misc.replace("=", " ")
|
2014-06-07 06:24:19 -04:00
|
|
|
|
|
|
|
def _format_xdc(signame, pin, others, resname):
|
|
|
|
fmt_c = [_format_constraint(c) for c in ([Pins(pin)] + others)]
|
|
|
|
fmt_r = resname[0] + ":" + str(resname[1])
|
|
|
|
if resname[2] is not None:
|
|
|
|
fmt_r += "." + resname[2]
|
2014-10-17 05:08:37 -04:00
|
|
|
r = " ## %s\n" %fmt_r
|
2014-06-07 06:24:19 -04:00
|
|
|
for c in fmt_c:
|
|
|
|
r += c + " [get_ports " + signame + "]\n"
|
|
|
|
return r
|
|
|
|
|
|
|
|
def _build_xdc(named_sc, named_pc):
|
|
|
|
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) + "]", p, others, resname)
|
|
|
|
else:
|
|
|
|
r += _format_xdc(sig, pins[0], others, resname)
|
|
|
|
if named_pc:
|
|
|
|
r += "\n" + "\n\n".join(named_pc)
|
|
|
|
return r
|
|
|
|
|
2014-12-17 03:21:16 -05:00
|
|
|
def _build_files(device, sources, vincpaths, build_name, bitstream_commands, additional_commands):
|
2014-08-17 16:56:29 -04:00
|
|
|
tcl = []
|
2014-06-07 06:24:19 -04:00
|
|
|
for filename, language in sources:
|
2014-08-17 16:56:29 -04:00
|
|
|
tcl.append("add_files " + filename.replace("\\", "/"))
|
2014-06-07 06:24:19 -04:00
|
|
|
|
2014-08-17 16:56:29 -04:00
|
|
|
tcl.append("read_xdc %s.xdc" %build_name)
|
|
|
|
tcl.append("synth_design -top top -part %s -include_dirs {%s}" %(device, " ".join(vincpaths)))
|
2014-10-31 07:47:21 -04:00
|
|
|
tcl.append("report_utilization -hierarchical -file %s_utilization_hierarchical_synth.rpt" %(build_name))
|
2014-08-17 16:56:29 -04:00
|
|
|
tcl.append("report_utilization -file %s_utilization_synth.rpt" %(build_name))
|
|
|
|
tcl.append("place_design")
|
2014-10-31 07:47:21 -04:00
|
|
|
tcl.append("report_utilization -hierarchical -file %s_utilization_hierarchical_place.rpt" %(build_name))
|
2014-08-17 16:56:29 -04:00
|
|
|
tcl.append("report_utilization -file %s_utilization_place.rpt" %(build_name))
|
|
|
|
tcl.append("report_io -file %s_io.rpt" %(build_name))
|
|
|
|
tcl.append("report_control_sets -verbose -file %s_control_sets.rpt" %(build_name))
|
|
|
|
tcl.append("report_clock_utilization -file %s_clock_utilization.rpt" %(build_name))
|
|
|
|
tcl.append("route_design")
|
|
|
|
tcl.append("report_route_status -file %s_route_status.rpt" %(build_name))
|
|
|
|
tcl.append("report_drc -file %s_drc.rpt" %(build_name))
|
2014-09-03 20:27:12 -04:00
|
|
|
tcl.append("report_timing_summary -max_paths 10 -file %s_timing.rpt" %(build_name))
|
2014-08-17 16:56:29 -04:00
|
|
|
tcl.append("report_power -file %s_power.rpt" %(build_name))
|
2014-12-17 03:21:16 -05:00
|
|
|
for bitstream_command in bitstream_commands:
|
|
|
|
tcl.append(bitstream_command.format(build_name=build_name))
|
2014-08-17 16:56:29 -04:00
|
|
|
tcl.append("write_bitstream -force %s.bit " %build_name)
|
2014-12-17 03:21:16 -05:00
|
|
|
for additional_command in additional_commands:
|
|
|
|
tcl.append(additional_command.format(build_name=build_name))
|
2014-08-17 16:56:29 -04:00
|
|
|
tcl.append("quit")
|
|
|
|
tools.write_to_file(build_name + ".tcl", "\n".join(tcl))
|
2014-06-07 06:24:19 -04:00
|
|
|
|
2014-07-27 21:30:08 -04:00
|
|
|
def _run_vivado(build_name, vivado_path, source, ver=None):
|
2014-10-31 07:48:30 -04:00
|
|
|
if sys.platform == "win32" or sys.platform == "cygwin":
|
2014-11-03 04:39:12 -05:00
|
|
|
build_script_contents = "REM Autogenerated by mibuild\n"
|
2014-10-31 07:48:30 -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:
|
2014-11-03 04:39:12 -05:00
|
|
|
build_script_contents = "# Autogenerated by mibuild\nset -e\n"
|
2014-08-06 07:26:00 -04:00
|
|
|
settings = xilinx_common.settings(vivado_path, ver)
|
2014-07-27 21:30:08 -04:00
|
|
|
build_script_contents += "source " + settings + "\n"
|
2014-10-31 07:48:30 -04:00
|
|
|
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])
|
2014-06-07 06:24:19 -04:00
|
|
|
|
|
|
|
if r != 0:
|
|
|
|
raise OSError("Subprocess failed")
|
|
|
|
|
2014-08-06 07:26:00 -04:00
|
|
|
class XilinxVivadoPlatform(xilinx_common.XilinxGenericPlatform):
|
2014-12-21 04:37:11 -05:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
xilinx_common.XilinxGenericPlatform.__init__(self, *args, **kwargs)
|
|
|
|
self.bitstream_commands = []
|
|
|
|
self.additional_commands = []
|
|
|
|
|
2014-06-07 06:24:19 -04:00
|
|
|
def build(self, fragment, build_dir="build", build_name="top",
|
2014-12-17 03:21:16 -05:00
|
|
|
vivado_path="/opt/Xilinx/Vivado", source=True, run=True):
|
2014-06-07 06:24:19 -04:00
|
|
|
tools.mkdir_noerror(build_dir)
|
|
|
|
os.chdir(build_dir)
|
|
|
|
|
|
|
|
if not isinstance(fragment, _Fragment):
|
|
|
|
fragment = fragment.get_fragment()
|
|
|
|
self.finalize(fragment)
|
|
|
|
v_src, named_sc, named_pc = self.get_verilog(fragment)
|
|
|
|
v_file = build_name + ".v"
|
|
|
|
tools.write_to_file(v_file, v_src)
|
|
|
|
sources = self.sources + [(v_file, "verilog")]
|
2014-12-17 03:21:16 -05:00
|
|
|
_build_files(self.device, sources, self.verilog_include_paths, build_name,
|
|
|
|
self.bitstream_commands, self.additional_commands)
|
2014-06-07 06:24:19 -04:00
|
|
|
tools.write_to_file(build_name + ".xdc", _build_xdc(named_sc, named_pc))
|
|
|
|
if run:
|
|
|
|
_run_vivado(build_name, vivado_path, source)
|
2014-10-17 05:08:37 -04:00
|
|
|
|
2014-06-07 06:24:19 -04:00
|
|
|
os.chdir("..")
|
|
|
|
|
|
|
|
def add_period_constraint(self, clk, period):
|
2014-06-20 11:10:09 -04:00
|
|
|
self.add_platform_command("""create_clock -name {clk} -period """ +\
|
|
|
|
str(period) + """ [get_ports {clk}]""", clk=clk)
|