forked from m-labs/migen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
diamond.py
186 lines (147 loc) · 6.43 KB
/
diamond.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# This file is Copyright (c) 2015 Florent Kermarrec <florent@enjoy-digital.fr>
# License: BSD
import os
import sys
import subprocess
import shutil
from migen.fhdl.structure import _Fragment
from migen.fhdl.verilog import DummyAttrTranslate
from migen.build.generic_platform import *
from migen.build import tools
from migen.build.lattice import common
def _get_bindir(path, ver=None):
if ver is None:
# _x64 suffix means StrictVersion will fail.
vers = list(tools.versions(path, strict=False))
if not vers:
raise OSError("no version directory for Diamond tools found in "
+ path)
ver = max(vers)
# Lattice does not provide 32-bit binaries anymore.
if sys.platform in ("win32", "cygwin"):
bin_suffix = "nt64"
else:
bin_suffix = "lin64"
return os.path.join(path, str(ver), "bin", bin_suffix)
def _produces_jedec(device):
return device.startswith("LCMX")
def _format_constraint(c):
if isinstance(c, Pins):
return ("LOCATE COMP ", " SITE " + "\"" + c.identifiers[0] + "\"")
elif isinstance(c, IOStandard):
return ("IOBUF PORT ", " IO_TYPE=" + c.name)
elif isinstance(c, Misc):
return c.misc
def _format_lpf(signame, pin, others, resname):
fmt_c = [_format_constraint(c) for c in ([Pins(pin)] + others)]
r = ""
for pre, suf in fmt_c:
r += pre + "\"" + signame + "\"" + suf + ";\n"
return r
def _build_lpf(named_sc, named_pc):
r = "BLOCK RESETPATHS;\n"
r += "BLOCK ASYNCPATHS;\n"
for sig, pins, others, resname in named_sc:
if len(pins) > 1:
for i, p in enumerate(pins):
r += _format_lpf(sig + "[" + str(i) + "]", p, others, resname)
else:
r += _format_lpf(sig, pins[0], others, resname)
if named_pc:
r += "\n" + "\n\n".join(named_pc)
return r
def _build_files(device, sources, vincpaths, build_name):
tcl = []
tcl.append("prj_project new -name \"{}\" -impl \"implementation\" -dev {} -synthesis \"synplify\"".format(build_name, device))
for path in vincpaths:
tcl.append("prj_impl option {include path} {\"" + path + "\"}")
for filename, language, library in sources:
tcl.append("prj_src add \"" + filename + "\" -work " + library)
tcl.append("prj_run Synthesis -impl implementation -forceOne")
tcl.append("prj_run Translate -impl implementation")
tcl.append("prj_run Map -impl implementation")
tcl.append("prj_run PAR -impl implementation")
tcl.append("prj_run Export -impl implementation -task Bitgen")
if _produces_jedec(device):
tcl.append("prj_run Export -impl implementation -task Jedecgen")
tools.write_to_file(build_name + ".tcl", "\n".join(tcl))
def _build_script(build_name, device, toolchain_path, source, ver=None):
if sys.platform in ("win32", "cygwin"):
script_ext = ".bat"
build_script_contents = "@echo off\nrem Autogenerated by Migen\n\n"
copy_stmt = "copy"
fail_stmt = " || exit /b"
else:
script_ext = ".sh"
build_script_contents = "# Autogenerated by Migen\nset -e\n\n"
copy_stmt = "cp"
fail_stmt = ""
bindir = _get_bindir(toolchain_path, ver)
if source:
if sys.platform in ("win32", "cygwin"):
build_script_contents += "set PATH={};%PATH%\n".format(bindir)
else:
build_script_contents += "bindir={}\n".format(bindir)
build_script_contents += ". ${{bindir}}/diamond_env{fail_stmt}\n".format(
fail_stmt=fail_stmt)
build_script_contents += "pnmainc {tcl_script}{fail_stmt}\n".format(
tcl_script=build_name + ".tcl",
fail_stmt=fail_stmt)
for ext in (".bit", ".jed"):
if ext == ".jed" and not _produces_jedec(device):
continue
diamond_product = os.path.join("implementation", build_name + "_implementation" + ext)
if sys.platform in ("win32", "cygwin"):
# The COPY command on Windows uses forward slash for arguments.
diamond_product = diamond_product.replace("/", "\\")
build_script_contents += "{copy_stmt} {diamond_product} {migen_product}" \
"{fail_stmt}\n".format(
copy_stmt=copy_stmt,
fail_stmt=fail_stmt,
diamond_product=diamond_product,
migen_product=build_name + ext)
build_script_file = "build_" + build_name + script_ext
tools.write_to_file(build_script_file, build_script_contents,
force_unix=False)
return build_script_file
def _run_script(script):
if sys.platform in ("win32", "cygwin"):
shell = ["cmd", "/c"]
else:
shell = ["bash"]
if subprocess.call(shell + [script]) != 0:
raise OSError("Subprocess failed")
class LatticeDiamondToolchain:
attr_translate = DummyAttrTranslate()
special_overrides = common.lattice_ecpx_special_overrides
def build(self, platform, fragment, build_dir="build", build_name="top",
toolchain_path=None, source=True, run=True, ver=None):
if toolchain_path is None:
if sys.platform == "win32":
toolchain_path = "C:\\lscc\\diamond"
elif sys.platform == "cygwin":
toolchain_path = "/cygdrive/c/lscc/diamond"
else:
toolchain_path = "/usr/local/diamond"
os.makedirs(build_dir, exist_ok=True)
cwd = os.getcwd()
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)
sources = platform.sources | {(v_file, "verilog", "work")}
_build_files(platform.device, sources, platform.verilog_include_paths, build_name)
tools.write_to_file(build_name + ".lpf", _build_lpf(named_sc, named_pc))
script = _build_script(build_name, platform.device, toolchain_path,
source, ver)
if run:
_run_script(script)
os.chdir(cwd)
return v_output.ns
def add_period_constraint(self, platform, clk, period):
# TODO: handle differential clk
platform.add_platform_command("""FREQUENCY PORT "{clk}" {freq} MHz;""".format(freq=str(float(1/period)*1000), clk="{clk}"), clk=clk)