2013-03-15 07:37:25 -04:00
|
|
|
# This file is Copyright (c) 2013 Florent Kermarrec <florent@enjoy-digital.fr>
|
2013-08-01 11:46:09 -04:00
|
|
|
# License: BSD
|
2013-03-15 07:37:25 -04:00
|
|
|
|
2015-04-13 14:54:19 -04:00
|
|
|
import os
|
|
|
|
import subprocess
|
2013-03-15 06:32:12 -04:00
|
|
|
|
2013-08-26 12:08:01 -04:00
|
|
|
from migen.fhdl.structure import _Fragment
|
2015-03-16 15:44:29 -04:00
|
|
|
|
2015-09-10 13:53:15 -04:00
|
|
|
from migen.build.generic_platform import Pins, IOStandard, Misc
|
|
|
|
from migen.build import tools
|
2013-03-15 06:32:12 -04:00
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2015-04-20 04:03:08 -04:00
|
|
|
def _format_constraint(c, signame, fmt_r):
|
2015-04-13 14:07:07 -04:00
|
|
|
if isinstance(c, Pins):
|
2015-04-20 04:03:08 -04:00
|
|
|
return "set_location_assignment -comment \"{name}\" " \
|
|
|
|
"-to {signame} Pin_{pin}".format(
|
|
|
|
signame=signame,
|
|
|
|
name=fmt_r,
|
|
|
|
pin=c.identifiers[0])
|
2015-04-13 14:07:07 -04:00
|
|
|
elif isinstance(c, IOStandard):
|
2015-04-20 04:03:08 -04:00
|
|
|
return "set_instance_assignment -name io_standard " \
|
|
|
|
"-comment \"{name}\" \"{std}\" -to {signame}".format(
|
|
|
|
signame=signame,
|
|
|
|
name=fmt_r,
|
|
|
|
std=c.name)
|
2015-04-13 14:07:07 -04:00
|
|
|
elif isinstance(c, Misc):
|
2015-04-20 04:03:08 -04:00
|
|
|
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)
|
2013-03-15 06:32:12 -04:00
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2013-03-15 06:32:12 -04:00
|
|
|
def _format_qsf(signame, pin, others, resname):
|
2015-04-20 04:03:08 -04:00
|
|
|
fmt_r = "{}:{}".format(*resname[:2])
|
2015-04-13 14:07:07 -04:00
|
|
|
if resname[2] is not None:
|
|
|
|
fmt_r += "." + resname[2]
|
2015-04-20 04:03:08 -04:00
|
|
|
|
|
|
|
fmt_c = [_format_constraint(c, signame, fmt_r) for c in
|
|
|
|
([Pins(pin)] + others)]
|
|
|
|
|
|
|
|
return '\n'.join(fmt_c)
|
2013-03-15 06:32:12 -04:00
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2013-03-15 06:32:12 -04:00
|
|
|
def _build_qsf(named_sc, named_pc):
|
2015-04-20 04:03:08 -04:00
|
|
|
lines = []
|
2015-04-13 14:07:07 -04:00
|
|
|
for sig, pins, others, resname in named_sc:
|
|
|
|
if len(pins) > 1:
|
|
|
|
for i, p in enumerate(pins):
|
2015-04-20 04:03:08 -04:00
|
|
|
lines.append(
|
|
|
|
_format_qsf("{}[{}]".format(sig, i), p, others, resname))
|
2015-04-13 14:07:07 -04:00
|
|
|
else:
|
2015-04-20 04:03:08 -04:00
|
|
|
lines.append(_format_qsf(sig, pins[0], others, resname))
|
|
|
|
|
2015-04-13 14:07:07 -04:00
|
|
|
if named_pc:
|
2015-04-20 04:03:08 -04:00
|
|
|
lines.append("")
|
|
|
|
lines.append("\n\n".join(named_pc))
|
|
|
|
|
|
|
|
lines.append("set_global_assignment -name top_level_entity top")
|
|
|
|
return "\n".join(lines)
|
2013-03-15 06:32:12 -04:00
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2014-04-11 17:24:39 -04:00
|
|
|
def _build_files(device, sources, vincpaths, named_sc, named_pc, build_name):
|
2015-04-20 04:03:08 -04:00
|
|
|
lines = []
|
2015-04-16 07:07:28 -04:00
|
|
|
for filename, language, library in sources:
|
2015-04-20 04:03:08 -04:00
|
|
|
# Enforce use of SystemVerilog
|
|
|
|
# (Quartus does not support global parameters in Verilog)
|
2015-04-13 14:07:07 -04:00
|
|
|
if language == "verilog":
|
|
|
|
language = "systemverilog"
|
2015-04-20 04:03:08 -04:00
|
|
|
lines.append(
|
|
|
|
"set_global_assignment -name {lang}_FILE {path} "
|
|
|
|
"-library {lib}".format(
|
|
|
|
lang=language.upper(),
|
|
|
|
path=filename.replace("\\", "/"),
|
|
|
|
lib=library))
|
2013-03-15 06:32:12 -04:00
|
|
|
|
2015-04-13 14:07:07 -04:00
|
|
|
for path in vincpaths:
|
2015-04-20 04:03:08 -04:00
|
|
|
lines.append("set_global_assignment -name SEARCH_PATH {}".format(
|
|
|
|
path.replace("\\", "/")))
|
2014-04-11 17:24:39 -04:00
|
|
|
|
2015-04-20 04:03:08 -04:00
|
|
|
lines.append(_build_qsf(named_sc, named_pc))
|
|
|
|
lines.append("set_global_assignment -name DEVICE {}".format(device))
|
|
|
|
tools.write_to_file("{}.qsf".format(build_name), "\n".join(lines))
|
2013-03-15 06:32:12 -04:00
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2013-03-15 06:32:12 -04:00
|
|
|
def _run_quartus(build_name, quartus_path):
|
2015-09-10 13:53:15 -04:00
|
|
|
build_script_contents = """# Autogenerated by Migen
|
2013-03-15 06:32:12 -04:00
|
|
|
|
2014-05-19 05:21:42 -04:00
|
|
|
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}
|
|
|
|
quartus_sta {build_name} -c {build_name}
|
2013-03-15 06:32:12 -04:00
|
|
|
|
2015-04-20 04:03:08 -04:00
|
|
|
""".format(build_name=build_name) # noqa
|
2015-04-13 14:07:07 -04:00
|
|
|
build_script_file = "build_" + build_name + ".sh"
|
2015-04-20 04:03:08 -04:00
|
|
|
tools.write_to_file(build_script_file,
|
|
|
|
build_script_contents,
|
|
|
|
force_unix=True)
|
2013-03-15 06:32:12 -04:00
|
|
|
|
2015-04-20 04:03:08 -04:00
|
|
|
if subprocess.call(["bash", build_script_file]):
|
2015-04-13 14:07:07 -04:00
|
|
|
raise OSError("Subprocess failed")
|
2013-03-15 06:32:12 -04:00
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2015-03-16 15:44:29 -04:00
|
|
|
class AlteraQuartusToolchain:
|
2015-04-13 14:07:07 -04:00
|
|
|
def build(self, platform, fragment, build_dir="build", build_name="top",
|
2015-04-20 04:03:08 -04:00
|
|
|
quartus_path="/opt/Altera", run=True):
|
2015-04-13 14:07:07 -04:00
|
|
|
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-20 04:03:08 -04:00
|
|
|
_build_files(platform.device,
|
|
|
|
sources,
|
|
|
|
platform.verilog_include_paths,
|
|
|
|
named_sc,
|
|
|
|
named_pc,
|
|
|
|
build_name)
|
2015-04-13 14:07:07 -04:00
|
|
|
if run:
|
|
|
|
_run_quartus(build_name, quartus_path)
|
|
|
|
|
|
|
|
os.chdir("..")
|
|
|
|
|
|
|
|
return v_output.ns
|
|
|
|
|
|
|
|
def add_period_constraint(self, platform, clk, period):
|
|
|
|
# TODO: handle differential clk
|
2015-04-20 04:03:08 -04:00
|
|
|
platform.add_platform_command(
|
|
|
|
"set_global_assignment -name duty_cycle 50 -section_id {clk}",
|
|
|
|
clk=clk)
|
|
|
|
platform.add_platform_command(
|
|
|
|
"set_global_assignment -name fmax_requirement \"{freq} MHz\" "
|
|
|
|
"-section_id {clk}".format(freq=(1. / period) * 1000,
|
|
|
|
clk="{clk}"),
|
|
|
|
clk=clk)
|