2019-06-23 16:36:00 -04:00
|
|
|
# This file is Copyright (c) 2014-2019 Florent Kermarrec <florent@enjoy-digital.fr>
|
|
|
|
# This file is Copyright (c) 2019 msloniewski <marcin.sloniewski@gmail.com>
|
|
|
|
# This file is Copyright (c) 2019 vytautasb <v.buitvydas@limemicro.com>
|
2015-11-07 06:22:32 -05:00
|
|
|
# License: BSD
|
|
|
|
|
|
|
|
import os
|
|
|
|
import subprocess
|
2019-08-02 04:27:38 -04:00
|
|
|
import sys
|
2019-04-16 10:57:23 -04:00
|
|
|
import math
|
2015-11-07 06:22:32 -05:00
|
|
|
|
2018-02-23 07:38:19 -05:00
|
|
|
from migen.fhdl.structure import _Fragment
|
2015-11-07 06:22:32 -05:00
|
|
|
|
2015-11-07 06:26:46 -05:00
|
|
|
from litex.build.generic_platform import Pins, IOStandard, Misc
|
|
|
|
from litex.build import tools
|
2015-11-07 06:22:32 -05:00
|
|
|
|
|
|
|
|
|
|
|
def _format_constraint(c, signame, fmt_r):
|
|
|
|
if isinstance(c, Pins):
|
|
|
|
return "set_location_assignment -comment \"{name}\" " \
|
|
|
|
"-to {signame} Pin_{pin}".format(
|
|
|
|
signame=signame,
|
|
|
|
name=fmt_r,
|
|
|
|
pin=c.identifiers[0])
|
|
|
|
elif isinstance(c, IOStandard):
|
|
|
|
return "set_instance_assignment -name io_standard " \
|
|
|
|
"-comment \"{name}\" \"{std}\" -to {signame}".format(
|
|
|
|
signame=signame,
|
|
|
|
name=fmt_r,
|
|
|
|
std=c.name)
|
|
|
|
elif isinstance(c, Misc):
|
|
|
|
if not isinstance(c.misc, str) and len(c.misc) == 2:
|
|
|
|
return "set_instance_assignment -comment \"{name}\" " \
|
|
|
|
"-name {misc[0]} \"{misc[1]}\" -to {signame}".format(
|
|
|
|
signame=signame,
|
|
|
|
name=fmt_r,
|
|
|
|
misc=c.misc)
|
|
|
|
else:
|
|
|
|
return "set_instance_assignment -comment \"{name}\" " \
|
|
|
|
"-name {misc} " \
|
|
|
|
"-to {signame}".format(
|
|
|
|
signame=signame,
|
|
|
|
name=fmt_r,
|
|
|
|
misc=c.misc)
|
|
|
|
|
|
|
|
|
|
|
|
def _format_qsf(signame, pin, others, resname):
|
|
|
|
fmt_r = "{}:{}".format(*resname[:2])
|
|
|
|
if resname[2] is not None:
|
|
|
|
fmt_r += "." + resname[2]
|
|
|
|
|
|
|
|
fmt_c = [_format_constraint(c, signame, fmt_r) for c in
|
|
|
|
([Pins(pin)] + others)]
|
|
|
|
|
|
|
|
return '\n'.join(fmt_c)
|
|
|
|
|
|
|
|
|
2019-04-08 06:34:59 -04:00
|
|
|
def _build_qsf(named_sc, named_pc, build_name):
|
2015-11-07 06:22:32 -05:00
|
|
|
lines = []
|
|
|
|
for sig, pins, others, resname in named_sc:
|
|
|
|
if len(pins) > 1:
|
|
|
|
for i, p in enumerate(pins):
|
|
|
|
lines.append(
|
|
|
|
_format_qsf("{}[{}]".format(sig, i), p, others, resname))
|
|
|
|
else:
|
|
|
|
lines.append(_format_qsf(sig, pins[0], others, resname))
|
|
|
|
|
|
|
|
if named_pc:
|
|
|
|
lines.append("")
|
|
|
|
lines.append("\n\n".join(named_pc))
|
|
|
|
|
2019-04-08 06:34:59 -04:00
|
|
|
# Set top level name to "build_name" in .qsf file instead always use "top" name
|
|
|
|
lines.append("set_global_assignment -name top_level_entity " + build_name)
|
2015-11-07 06:22:32 -05:00
|
|
|
return "\n".join(lines)
|
|
|
|
|
|
|
|
|
2019-04-16 10:57:23 -04:00
|
|
|
def _build_sdc(clocks, false_paths, vns, build_name):
|
|
|
|
lines = []
|
|
|
|
for clk, period in sorted(clocks.items(), key=lambda x: x[0].duid):
|
|
|
|
lines.append(
|
|
|
|
"create_clock -name {clk} -period ".format(clk=vns.get_name(clk)) + str(period) +
|
|
|
|
" [get_ports {{{clk}}}]".format(clk=vns.get_name(clk)))
|
|
|
|
for from_, to in sorted(false_paths,
|
|
|
|
key=lambda x: (x[0].duid, x[1].duid)):
|
|
|
|
lines.append(
|
|
|
|
"set_false_path "
|
|
|
|
"-from [get_clocks {{{from_}}}] "
|
|
|
|
"-to [get_clocks {{{to}}}]".format(
|
|
|
|
from_=vns.get_name(from_), to=vns.get_name(to)))
|
|
|
|
tools.write_to_file("{}.sdc".format(build_name), "\n".join(lines))
|
|
|
|
|
|
|
|
|
2019-08-15 07:44:36 -04:00
|
|
|
def _build_files(device, ips, sources, vincpaths, named_sc, named_pc, build_name):
|
2015-11-07 06:22:32 -05:00
|
|
|
lines = []
|
2019-08-15 07:44:36 -04:00
|
|
|
for filename in ips:
|
|
|
|
lines.append("set_global_assignment -name QSYS_FILE {path} ".format(
|
|
|
|
path=filename.replace("\\", "/")))
|
2015-11-07 06:22:32 -05:00
|
|
|
for filename, language, library in sources:
|
2019-08-15 07:44:36 -04:00
|
|
|
# Enforce use of SystemVerilog since Quartus does not support global parameters in Verilog
|
2015-11-07 06:22:32 -05:00
|
|
|
if language == "verilog":
|
|
|
|
language = "systemverilog"
|
|
|
|
lines.append(
|
|
|
|
"set_global_assignment -name {lang}_FILE {path} "
|
|
|
|
"-library {lib}".format(
|
|
|
|
lang=language.upper(),
|
|
|
|
path=filename.replace("\\", "/"),
|
|
|
|
lib=library))
|
2019-04-16 10:57:23 -04:00
|
|
|
lines.append("set_global_assignment -name SDC_FILE {}.sdc".format(build_name))
|
2015-11-07 06:22:32 -05:00
|
|
|
|
|
|
|
for path in vincpaths:
|
|
|
|
lines.append("set_global_assignment -name SEARCH_PATH {}".format(
|
|
|
|
path.replace("\\", "/")))
|
|
|
|
|
2019-04-08 06:34:59 -04:00
|
|
|
lines.append(_build_qsf(named_sc, named_pc, build_name))
|
2015-11-07 06:22:32 -05:00
|
|
|
lines.append("set_global_assignment -name DEVICE {}".format(device))
|
|
|
|
tools.write_to_file("{}.qsf".format(build_name), "\n".join(lines))
|
|
|
|
|
|
|
|
|
2019-06-05 12:52:40 -04:00
|
|
|
def _run_quartus(build_name, quartus_path, create_rbf):
|
2019-08-02 04:27:38 -04:00
|
|
|
if sys.platform == "win32" or sys.platform == "cygwin":
|
|
|
|
build_script_contents = "REM Autogenerated by LiteX / git: " + tools.get_litex_git_revision() + "\n"
|
|
|
|
build_script_file = "build_" + build_name + ".bat"
|
|
|
|
command = build_script_file
|
|
|
|
else:
|
|
|
|
build_script_contents = "# Autogenerated by LiteX / git: " + tools.get_litex_git_revision() + "\nset -e\n"
|
|
|
|
build_script_file = "build_" + build_name + ".sh"
|
|
|
|
command = ["bash", build_script_file]
|
2019-04-23 00:03:12 -04:00
|
|
|
build_script_contents += """
|
2015-11-07 06:22:32 -05:00
|
|
|
|
|
|
|
set -e
|
|
|
|
|
|
|
|
quartus_map --read_settings_files=on --write_settings_files=off {build_name} -c {build_name}
|
|
|
|
quartus_fit --read_settings_files=off --write_settings_files=off {build_name} -c {build_name}
|
|
|
|
quartus_asm --read_settings_files=off --write_settings_files=off {build_name} -c {build_name}
|
2019-06-05 12:52:40 -04:00
|
|
|
quartus_sta {build_name} -c {build_name}"""
|
|
|
|
if create_rbf:
|
|
|
|
build_script_contents +="""
|
2018-11-12 05:26:35 -05:00
|
|
|
if [ -f "{build_name}.sof" ]
|
|
|
|
then
|
|
|
|
quartus_cpf -c {build_name}.sof {build_name}.rbf
|
|
|
|
fi
|
2015-11-07 06:22:32 -05:00
|
|
|
|
2019-06-05 12:52:40 -04:00
|
|
|
"""
|
|
|
|
build_script_contents = build_script_contents.format(build_name=build_name) # noqa
|
2015-11-07 06:22:32 -05:00
|
|
|
tools.write_to_file(build_script_file,
|
|
|
|
build_script_contents,
|
|
|
|
force_unix=True)
|
|
|
|
|
2019-08-02 04:27:38 -04:00
|
|
|
if subprocess.call(command):
|
2015-11-07 06:22:32 -05:00
|
|
|
raise OSError("Subprocess failed")
|
|
|
|
|
|
|
|
|
|
|
|
class AlteraQuartusToolchain:
|
2019-04-16 10:57:23 -04:00
|
|
|
def __init__(self):
|
|
|
|
self.clocks = dict()
|
|
|
|
self.false_paths = set()
|
|
|
|
|
2015-11-07 06:22:32 -05:00
|
|
|
def build(self, platform, fragment, build_dir="build", build_name="top",
|
2018-11-12 05:48:30 -05:00
|
|
|
toolchain_path=None, run=True, **kwargs):
|
|
|
|
if toolchain_path is None:
|
|
|
|
toolchain_path="/opt/Altera"
|
2015-11-30 07:33:39 -05:00
|
|
|
cwd = os.getcwd()
|
2017-01-12 19:33:48 -05:00
|
|
|
os.makedirs(build_dir, exist_ok=True)
|
2015-11-07 06:22:32 -05:00
|
|
|
os.chdir(build_dir)
|
|
|
|
|
|
|
|
if not isinstance(fragment, _Fragment):
|
|
|
|
fragment = fragment.get_fragment()
|
|
|
|
platform.finalize(fragment)
|
|
|
|
|
2015-12-02 08:18:09 -05:00
|
|
|
v_output = platform.get_verilog(fragment, name=build_name, **kwargs)
|
2015-11-07 06:22:32 -05:00
|
|
|
named_sc, named_pc = platform.resolve_signals(v_output.ns)
|
|
|
|
v_file = build_name + ".v"
|
|
|
|
v_output.write(v_file)
|
2019-08-15 07:44:36 -04:00
|
|
|
platform.add_source(v_file)
|
2015-11-07 06:22:32 -05:00
|
|
|
_build_files(platform.device,
|
2019-08-15 07:44:36 -04:00
|
|
|
platform.ips,
|
|
|
|
platform.sources,
|
2015-11-07 06:22:32 -05:00
|
|
|
platform.verilog_include_paths,
|
|
|
|
named_sc,
|
|
|
|
named_pc,
|
|
|
|
build_name)
|
2019-04-16 10:57:23 -04:00
|
|
|
|
|
|
|
_build_sdc(self.clocks, self.false_paths, v_output.ns, build_name)
|
2015-11-07 06:22:32 -05:00
|
|
|
if run:
|
2019-06-05 12:52:40 -04:00
|
|
|
_run_quartus(build_name, toolchain_path, platform.create_rbf)
|
2015-11-07 06:22:32 -05:00
|
|
|
|
2015-11-30 07:33:39 -05:00
|
|
|
os.chdir(cwd)
|
2015-11-07 06:22:32 -05:00
|
|
|
|
|
|
|
return v_output.ns
|
|
|
|
|
|
|
|
def add_period_constraint(self, platform, clk, period):
|
2019-04-16 10:57:23 -04:00
|
|
|
if clk in self.clocks:
|
|
|
|
raise ValueError("A period constraint already exists")
|
|
|
|
period = math.floor(period*1e3)/1e3 # round to lowest picosecond
|
|
|
|
self.clocks[clk] = period
|
|
|
|
|
|
|
|
def add_false_path_constraint(self, platform, from_, to):
|
|
|
|
if (to, from_) not in self.false_paths:
|
|
|
|
self.false_paths.add((from_, to))
|