Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
4443d3b3aThis file is pinned to the selected release. Download the original to open it in your design tool.
#
# This file is part of LiteX.
#
# Copyright (c) 2014-2019 Florent Kermarrec <florent@enjoy-digital.fr>
# Copyright (c) 2014-2015 Robert Jordens <jordens@gmail.com>
# Copyright (c) 2014-2015 Sebastien Bourdeauducq <sb@m-labs.hk>
# Copyright (c) 2017 bunnie <bunnie@kosagi.com>
# Copyright (c) 2018-2017 Tim 'mithro' Ansell <me@mith.ro>
# Copyright (c) 2018 William D. Jones <thor0505@comcast.net>
# Copyright (c) 2019 Larry Doolittle <ldoolitt@recycle.lbl.gov>
# SPDX-License-Identifier: BSD-2-Clause
import os
import subprocess
import sys
from shutil import which
from migen.fhdl.structure import _Fragment
from litex.build.generic_toolchain import GenericToolchain
from litex.build.generic_platform import *
from litex.build import tools
from litex.build.xilinx import common
# XilinxISEToolchain -------------------------------------------------------------------------------
class XilinxISEToolchain(GenericToolchain):
attr_translate = {
"keep": ("keep", "true"),
"no_retiming": ("register_balancing", "no"),
"async_reg": None,
"mr_ff": None,
"ars_ff1": None,
"ars_ff2": None,
"no_shreg_extract": ("shreg_extract", "no")
}
def __init__(self):
super().__init__()
self.xst_opt = "-ifmt MIXED\n-use_new_parser yes\n-opt_mode SPEED\n-register_balancing yes"
self.map_opt = "-ol high -w"
self.par_opt = "-ol high -w"
self.synplify_cmd = "synplify_premier_dp"
self.synplify_opt = ""
self.ngdbuild_opt = ""
self.bitgen_opt = "-g Binary:Yes -w"
self.ise_commands = ""
self._mode = "xst"
self._isemode = "xst"
def build(self, platform, fragment,
mode = "xst",
**kwargs):
if mode == "edif":
raise NotImplementedError("ISE EDIF mode is no longer supported.")
if mode not in ["xst", "cpld", "yosys", "synplify"]:
raise ValueError(f"Unsupported ISE synthesis mode: {mode}.")
self._mode = mode
self._isemode = mode if mode in ["xst", "cpld"] else "edif"
if mode == "yosys":
self.ngdbuild_opt += "-p " + platform.device
return GenericToolchain.build(self, platform, fragment, **kwargs)
# Constraints (.ucf) ---------------------------------------------------------------------------
@classmethod
def _format_constraint(cls, c):
if isinstance(c, Pins):
return "LOC=" + c.identifiers[0]
elif isinstance(c, IOStandard):
return "IOSTANDARD=" + c.name
elif isinstance(c, Drive):
return "DRIVE=" + str(c.strength)
elif isinstance(c, Misc):
return c.misc
def _format_ucf(self, signame, pin, others, resname):
fmt_c = []
for c in ([] if pin is None else [Pins(pin)]) + others:
fc = self._format_constraint(c)
if fc is not None:
fmt_c.append(fc)
fmt_r = resname[0] + ":" + str(resname[1])
if resname[2] is not None:
fmt_r += "." + resname[2]
return "NET \"" + signame + "\" " + " | ".join(fmt_c) + "; # " + fmt_r + "\n"
def build_io_constraints(self):
r = ""
for sig, pins, others, resname in self.named_sc:
if "X" in pins:
r += "# " + sig + " skipped (no physical pin).\n"
continue
if len(pins) > 1:
for i, p in enumerate(pins):
r += self._format_ucf(sig + "(" + str(i) + ")", p, others, resname)
elif pins:
r += self._format_ucf(sig, pins[0], others, resname)
else:
r += self._format_ucf(sig, None, others, resname)
if self.named_pc:
r += "\n" + "\n\n".join(self.named_pc)
tools.write_to_file(self._build_name + ".ucf", r)
return (self._build_name + ".ucf", "UCF")
# Project (.xst/.prj) --------------------------------------------------------------------------
def build_project(self):
if self._mode == "synplify":
return self._build_synplify_project()
if self._mode not in ["xst", "cpld"]:
return
prj_contents = ""
for filename, language, library, *copy in self.platform.sources:
prj_contents += language + " " + library + " " + tools.cygpath(filename) + "\n"
tools.write_to_file(self._build_name + ".prj", prj_contents)
xst_contents = """run
-ifn {build_name}.prj
-top {build_name}
{xst_opt}
-ofn {build_name}.ngc
-p {device}
""".format(build_name=self._build_name, xst_opt=self.xst_opt, device=self.platform.device)
if self.platform.verilog_include_paths:
xst_contents += "-vlgincdir {"
for path in self.platform.verilog_include_paths:
xst_contents += tools.cygpath(path) + " "
xst_contents += "}"
tools.write_to_file(self._build_name + ".xst", xst_contents)
def _build_synplify_project(self):
device = self.platform.device
if not device.startswith(("xc6s", "xa6s", "xq6s")):
raise ValueError(
"Synplify support is currently only available for Spartan-6 ISE designs."
)
part, package, speed = self._split_synplify_spartan6_device(device)
technology = "spartan6"
prj_contents = """
set_option -technology {technology}
set_option -part {part}
set_option -package {package}
set_option -speed_grade {speed}
set_option -vlog_std v2001
set_option -compiler_compatible 1
set_option -automatic_compile_point 1
set_option -top_module {build_name}
project -result_file {build_name}.edif
""".format(
build_name = self._build_name,
package = package,
part = part,
speed = speed,
technology = technology,
)
if self.platform.verilog_include_paths:
prj_contents += "set_option -include_path {"
for path in self.platform.verilog_include_paths:
prj_contents += tools.cygpath(path) + ";"
prj_contents += "}\n"
if self.synplify_opt:
prj_contents += self.synplify_opt + "\n"
for filename, language, library, *copy in self.platform.sources:
if language is not None:
prj_contents += "add_file -{} {{{}}}\n".format(language, tools.cygpath(filename))
tools.write_to_file(self._build_name + "_synplify.prj", prj_contents)
@staticmethod
def _split_synplify_spartan6_device(device):
part = device.split("-")[0]
package = None
speed = None
for item in device.split("-")[1:]:
if item and item[0].isdigit():
speed = "-" + item
else:
package = item
if package is None or speed is None:
raise ValueError(
f"Unable to parse Spartan-6 device string for Synplify: {device}."
)
return part, package, speed
# ISE Run --------------------------------------------------------------------------------------
def build_script(self):
if sys.platform == "win32" or sys.platform == "cygwin":
script_ext = ".bat"
build_script_contents = "@echo off\nrem Autogenerated by LiteX / git: " + tools.get_litex_git_revision() + "\n"
fail_stmt = " || exit /b"
else:
script_ext = ".sh"
build_script_contents = "# Autogenerated by LiteX / git: " + tools.get_litex_git_revision() + "\nset -e\n"
if os.getenv("LITEX_ENV_ISE", False):
build_script_contents += "source \"" + os.path.join(os.getenv("LITEX_ENV_ISE"), "settings64.sh") + "\"\n"
fail_stmt = ""
if self._mode == "yosys":
build_script_contents += common._build_yosys_project(self.platform, "-ise ", self._build_name) + fail_stmt
if self._mode == "synplify":
build_script_contents += "{cmd} -batch -runall {build_name}_synplify.prj{fail_stmt}\n".format(
build_name = self._build_name,
cmd = self.synplify_cmd,
fail_stmt = fail_stmt,
)
if self._isemode == "edif":
ext = "ngo"
build_script_contents += """
edif2ngd {build_name}.edif {build_name}.{ext}{fail_stmt}
"""
else:
ext = "ngc"
build_script_contents += """
xst -ifn {build_name}.xst{fail_stmt}
"""
# This generates a .v file for post synthesis simulation
build_script_contents += """
netgen -ofmt verilog -w -sim {build_name}.{ext} {build_name}_synth.v{fail_stmt}
"""
build_script_contents += """
ngdbuild {ngdbuild_opt} -uc {build_name}.ucf {build_name}.{ext} {build_name}.ngd{fail_stmt}
"""
if self._isemode == "cpld":
build_script_contents += """
cpldfit -ofmt verilog {par_opt} -p {device} {build_name}.ngd{fail_stmt}
taengine -f {build_name}.vm6 -detail -iopath -l {build_name}.tim{fail_stmt}
hprep6 -s IEEE1532 -i {build_name}.vm6{fail_stmt}
"""
else:
build_script_contents += """
map {map_opt} -o {build_name}_map.ncd {build_name}.ngd {build_name}.pcf{fail_stmt}
par {par_opt} {build_name}_map.ncd {build_name}.ncd {build_name}.pcf{fail_stmt}
bitgen {bitgen_opt} {build_name}.ncd {build_name}.bit{fail_stmt}
"""
build_script_contents = build_script_contents.format(build_name=self._build_name,
ngdbuild_opt=self.ngdbuild_opt, bitgen_opt=self.bitgen_opt, ext=ext,
par_opt=self.par_opt, map_opt=self.map_opt,
device=self.platform.device, fail_stmt=fail_stmt)
build_script_contents += self.ise_commands.format(build_name=self._build_name)
build_script_file = "build_" + self._build_name + script_ext
tools.write_to_file(build_script_file, build_script_contents, force_unix=False)
return build_script_file
def run_script(self, script):
if sys.platform == "win32" or sys.platform == "cygwin":
shell = ["cmd", "/c"]
else:
shell = ["bash"]
command = shell + [script]
if which("ise") is None and not os.getenv("LITEX_ENV_ISE", ""):
msg = "Unable to find or source ISE toolchain, please either:\n"
msg += "- Source ISE's settings manually.\n"
msg += "- Or set LITEX_ENV_ISE environment variant to ISE's settings path.\n"
msg += "- Or add ISE toolchain to your $PATH."
raise OSError(msg)
if tools.subprocess_call_filtered(command, common.colors) != 0:
raise OSError("Error occured during ISE's script execution.")
# ISE is broken and you must use *separate* TNM_NET objects for period
# constraints and other constraints otherwise it will be unable to trace
# them through clock objects like DCM and PLL objects.
def add_period_constraint(self, platform, clk, period, keep=True, name=None):
if clk is None:
return
if hasattr(clk, "p"):
clk = clk.p
if keep:
clk.attr.add("keep")
platform.add_platform_command(
"""
NET "{clk}" TNM_NET = "PRD{clk}";
TIMESPEC "TS{clk}" = PERIOD "PRD{clk}" """ + str(period) + """ ns HIGH 50%;
""",
clk=clk,
)
def add_false_path_constraint(self, platform, from_, to):
from_.attr.add("keep")
to.attr.add("keep")
platform.add_platform_command(
"""
NET "{from_}" TNM_NET = "TIG{from_}";
NET "{to}" TNM_NET = "TIG{to}";
TIMESPEC "TS{from_}TO{to}" = FROM "TIG{from_}" TO "TIG{to}" TIG;
""",
from_=from_,
to=to,
)