litex/mibuild/xilinx_ise.py

183 lines
5.5 KiB
Python
Raw Normal View History

import os, subprocess, sys
2013-02-07 16:07:30 -05:00
2013-05-26 12:07:26 -04:00
from migen.fhdl.std import *
2013-08-11 17:07:07 -04:00
from migen.fhdl.structure import _Fragment
2013-02-07 16:07:30 -05:00
from mibuild.generic_platform import *
from mibuild import tools, xilinx_common
2013-02-07 16:07:30 -05:00
def _format_constraint(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(signame, pin, others, resname):
2013-03-26 14:17:35 -04:00
fmt_c = []
for c in [Pins(pin)] + others:
fc = _format_constraint(c)
if fc is not None:
fmt_c.append(fc)
2013-02-07 16:07:30 -05:00
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_ucf(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_ucf(sig + "(" + str(i) + ")", p, others, resname)
else:
r += _format_ucf(sig, pins[0], others, resname)
if named_pc:
r += "\n" + "\n\n".join(named_pc)
return r
2013-12-12 17:17:51 -05:00
def _build_xst_files(device, sources, vincpaths, build_name, xst_opt):
2013-02-07 16:07:30 -05:00
prj_contents = ""
2013-02-08 14:25:20 -05:00
for filename, language in sources:
prj_contents += language + " work " + filename + "\n"
2013-02-07 16:07:30 -05:00
tools.write_to_file(build_name + ".prj", prj_contents)
xst_contents = """run
-ifn {build_name}.prj
2013-02-07 16:07:30 -05:00
-top top
{xst_opt}
-ofn {build_name}.ngc
2013-12-12 17:17:51 -05:00
-p {device}
""".format(build_name=build_name, xst_opt=xst_opt, device=device)
for path in vincpaths:
xst_contents += "-vlgincdir " + path + "\n"
2013-02-07 16:07:30 -05:00
tools.write_to_file(build_name + ".xst", xst_contents)
2013-12-12 17:17:51 -05:00
def _run_yosys(device, sources, vincpaths, build_name):
2013-12-01 11:07:48 -05:00
ys_contents = ""
2013-12-12 17:17:51 -05:00
incflags = ""
for path in vincpaths:
incflags += " -I" + path
2013-12-01 11:07:48 -05:00
for filename, language in sources:
2013-12-12 17:17:51 -05:00
ys_contents += "read_{}{} {}\n".format(language, incflags, filename)
2014-10-17 05:08:37 -04:00
2013-12-01 11:07:48 -05:00
if device[:2] == "xc":
archcode = device[2:4]
else:
archcode = device[0:2]
arch = {
"6s": "spartan6",
"7a": "artix7",
"7k": "kintex7",
"7v": "virtex7",
"7z": "zynq7000"
}[archcode]
2014-10-17 05:08:37 -04:00
2013-12-01 11:07:48 -05:00
ys_contents += """hierarchy -check -top top
proc; memory; opt; fsm; opt
synth_xilinx -arch {arch} -top top -edif {build_name}.edif""".format(arch=arch, build_name=build_name)
2014-10-17 05:08:37 -04:00
2013-12-01 11:07:48 -05:00
ys_name = build_name + ".ys"
tools.write_to_file(ys_name, ys_contents)
r = subprocess.call(["yosys", ys_name])
if r != 0:
raise OSError("Subprocess failed")
def _run_ise(build_name, ise_path, source, mode, ngdbuild_opt,
bitgen_opt, ise_commands, map_opt, par_opt, ver=None):
if sys.platform == "win32" or sys.platform == "cygwin":
source = False
build_script_contents = "# Autogenerated by mibuild\nset -e\n"
if source:
settings = xilinx_common.settings(ise_path, ver, "ISE_DS")
build_script_contents += "source " + settings + "\n"
2013-08-02 11:10:33 -04:00
if mode == "edif":
ext = "edif"
2013-08-02 11:10:33 -04:00
else:
ext = "ngc"
2013-08-02 11:10:33 -04:00
build_script_contents += """
xst -ifn {build_name}.xst
"""
2013-08-02 11:10:33 -04:00
build_script_contents += """
ngdbuild {ngdbuild_opt} -uc {build_name}.ucf {build_name}.{ext} {build_name}.ngd
map {map_opt} -o {build_name}_map.ncd {build_name}.ngd {build_name}.pcf
par {par_opt} {build_name}_map.ncd {build_name}.ncd {build_name}.pcf
bitgen {bitgen_opt} {build_name}.ncd {build_name}.bit
2013-08-07 18:15:35 -04:00
"""
build_script_contents = build_script_contents.format(build_name=build_name,
ngdbuild_opt=ngdbuild_opt, bitgen_opt=bitgen_opt, ext=ext,
par_opt=par_opt, map_opt=map_opt)
build_script_contents += ise_commands.format(build_name=build_name)
2013-02-07 16:07:30 -05:00
build_script_file = "build_" + build_name + ".sh"
tools.write_to_file(build_script_file, build_script_contents, force_unix=True)
2013-02-07 16:07:30 -05:00
r = subprocess.call(["bash", build_script_file])
if r != 0:
raise OSError("Subprocess failed")
class XilinxISEPlatform(xilinx_common.XilinxGenericPlatform):
xst_opt = """-ifmt MIXED
-opt_mode SPEED
-register_balancing yes"""
map_opt = "-ol high -w"
par_opt = "-ol high -w"
ngdbuild_opt = ""
bitgen_opt = "-g LCK_cycle:6 -g Binary:Yes -w"
ise_commands = ""
2013-08-02 11:10:33 -04:00
2013-03-15 13:46:11 -04:00
def build(self, fragment, build_dir="build", build_name="top",
2013-12-01 11:07:48 -05:00
ise_path="/opt/Xilinx", source=True, run=True, mode="xst"):
2013-02-07 16:07:30 -05:00
tools.mkdir_noerror(build_dir)
os.chdir(build_dir)
2013-08-11 17:07:07 -04:00
if not isinstance(fragment, _Fragment):
fragment = fragment.get_fragment()
self.finalize(fragment)
ngdbuild_opt = self.ngdbuild_opt
2013-12-01 11:07:48 -05:00
vns = None
2013-12-01 11:07:48 -05:00
if mode == "xst" or mode == "yosys":
v_src, vns = self.get_verilog(fragment)
2015-02-14 06:05:07 -05:00
named_sc, named_pc = self.resolve_signals(vns)
2013-08-02 11:10:33 -04:00
v_file = build_name + ".v"
tools.write_to_file(v_file, v_src)
sources = self.sources + [(v_file, "verilog")]
2013-12-01 11:07:48 -05:00
if mode == "xst":
2013-12-12 17:17:51 -05:00
_build_xst_files(self.device, sources, self.verilog_include_paths, build_name, self.xst_opt)
2013-12-01 11:07:48 -05:00
isemode = "xst"
else:
2013-12-12 17:17:51 -05:00
_run_yosys(self.device, sources, self.verilog_include_paths, build_name)
2013-12-01 11:07:48 -05:00
isemode = "edif"
ngdbuild_opt += "-p " + self.device
2013-08-02 11:10:33 -04:00
2013-08-11 17:07:07 -04:00
if mode == "mist":
from mist import synthesize
synthesize(fragment, self.constraint_manager.get_io_signals())
if mode == "edif" or mode == "mist":
e_src, vns = self.get_edif(fragment)
2015-02-14 06:05:07 -05:00
named_sc, named_pc = self.resolve_signals(vns)
2013-08-02 11:10:33 -04:00
e_file = build_name + ".edif"
tools.write_to_file(e_file, e_src)
2013-12-01 11:07:48 -05:00
isemode = "edif"
tools.write_to_file(build_name + ".ucf", _build_ucf(named_sc, named_pc))
if run:
_run_ise(build_name, ise_path, source, isemode,
ngdbuild_opt, self.bitgen_opt, self.ise_commands,
self.map_opt, self.par_opt)
2013-08-11 17:07:07 -04:00
2013-02-07 16:07:30 -05:00
os.chdir("..")
return vns
def add_period_constraint(self, clk, period):
self.add_platform_command("""NET "{clk}" TNM_NET = "GRP{clk}";
TIMESPEC "TS{clk}" = PERIOD "GRP{clk}" """+str(period)+""" ns HIGH 50%;""", clk=clk)