1 from abc
import abstractproperty
7 __all__
= ["LatticeECP5Platform"]
10 class LatticeECP5Platform(TemplatedPlatform
):
20 The environment is populated by running the script specified in the environment variable
21 ``NMIGEN_ENV_Trellis``, if present.
24 * ``verbose``: enables logging of informational messages to standard error.
25 * ``read_verilog_opts``: adds options for ``read_verilog`` Yosys command.
26 * ``synth_opts``: adds options for ``synth_ecp5`` Yosys command.
27 * ``script_after_read``: inserts commands after ``read_ilang`` in Yosys script.
28 * ``script_after_synth``: inserts commands after ``synth_ecp5`` in Yosys script.
29 * ``yosys_opts``: adds extra options for ``yosys``.
30 * ``nextpnr_opts``: adds extra options for ``nextpnr-ecp5``.
31 * ``ecppack_opts``: adds extra options for ``ecppack``.
32 * ``add_preferences``: inserts commands at the end of the LPF file.
35 * ``{{name}}.rpt``: Yosys log.
36 * ``{{name}}.json``: synthesized RTL.
37 * ``{{name}}.tim``: nextpnr log.
38 * ``{{name}}.config``: ASCII bitstream.
39 * ``{{name}}.bit``: binary bitstream.
40 * ``{{name}}.svf``: JTAG programming vector.
49 The environment is populated by running the script specified in the environment variable
50 ``NMIGEN_ENV_Diamond``, if present.
53 * ``script_project``: inserts commands before ``prj_project save`` in Tcl script.
54 * ``script_after_export``: inserts commands after ``prj_run Export`` in Tcl script.
55 * ``add_preferences``: inserts commands at the end of the LPF file.
56 * ``add_constraints``: inserts commands at the end of the XDC file.
59 * ``{{name}}_impl/{{name}}_impl.htm``: consolidated log.
60 * ``{{name}}.bit``: binary bitstream.
61 * ``{{name}}.svf``: JTAG programming vector.
64 toolchain
= None # selected when creating platform
66 device
= abstractproperty()
67 package
= abstractproperty()
68 speed
= abstractproperty()
69 grade
= "C" # [C]ommercial, [I]ndustrial
73 _nextpnr_device_options
= {
78 "LFE5UM-12F": "--um-25k",
79 "LFE5UM-25F": "--um-25k",
80 "LFE5UM-45F": "--um-45k",
81 "LFE5UM-85F": "--um-85k",
82 "LFE5UM5G-12F": "--um5g-25k",
83 "LFE5UM5G-25F": "--um5g-25k",
84 "LFE5UM5G-45F": "--um5g-45k",
85 "LFE5UM5G-85F": "--um5g-85k",
87 _nextpnr_package_options
= {
95 _trellis_required_tools
= [
100 _trellis_file_templates
= {
101 **TemplatedPlatform
.build_script_templates
,
104 {{emit_design("rtlil")}}
108 {% for file in platform.iter_extra_files(".v") -%}
109 read_verilog {{get_override("read_opts")|options}} {{file}}
111 {% for file in platform.iter_extra_files(".sv") -%}
112 read_verilog -sv {{get_override("read_opts")|options}} {{file}}
114 read_ilang {{name}}.il
115 {{get_override("script_after_read")|default("# (script_after_read placeholder)")}}
116 synth_ecp5 {{get_override("synth_opts")|options}} -top {{name}}
117 {{get_override("script_after_synth")|default("# (script_after_synth placeholder)")}}
118 write_json {{name}}.json
124 {% for port_name, pin_name, extras in platform.iter_port_constraints_bits() -%}
125 LOCATE COMP "{{port_name}}" SITE "{{pin_name}}";
126 IOBUF PORT "{{port_name}}"
127 {%- for key, value in extras.items() %} {{key}}={{value}}{% endfor %};
129 {% for signal, frequency in platform.iter_clock_constraints() -%}
130 FREQUENCY NET "{{signal|hierarchy(".")}}" {{frequency}} HZ;
132 {{get_override("add_preferences")|default("# (add_preferences placeholder)")}}
135 _trellis_command_templates
= [
137 {{get_tool("yosys")}}
139 {{get_override("yosys_opts")|options}}
144 {{get_tool("nextpnr-ecp5")}}
146 {{get_override("nextpnr_opts")|options}}
148 {{platform._nextpnr_device_options[platform.device]}}
149 --package {{platform._nextpnr_package_options[platform.package]|upper}}
150 --speed {{platform.speed}}
153 --textcfg {{name}}.config
156 {{get_tool("ecppack")}}
157 {{verbose("--verbose")}}
158 {{get_override("ecppack_opts")|options}}
159 --input {{name}}.config
167 _diamond_required_tools
= [
171 _diamond_file_templates
= {
172 **TemplatedPlatform
.build_script_templates
,
173 "build_{{name}}.sh": r
"""
175 set -e{{verbose("x")}}
176 if [ -z "$BASH" ] ; then exec /bin/bash "$0" "$@"; fi
177 if [ -n "${{platform._toolchain_env_var}}" ]; then
178 bindir=$(dirname "${{platform._toolchain_env_var}}")
179 . "${{platform._toolchain_env_var}}"
181 {{emit_commands("sh")}}
184 /* {{autogenerated}} */
185 {{emit_design("verilog")}}
188 prj_project new -name {{name}} -impl impl -impl_dir top_impl \
189 -dev {{platform.device}}-{{platform.speed}}{{platform.package}}{{platform.grade}} \
192 {% for file in platform.iter_extra_files(".v", ".sv", ".vhd", ".vhdl") -%}
193 prj_src add "{{file}}"
195 prj_src add {{name}}.v
196 prj_impl option top {{name}}
197 prj_src add {{name}}.sdc
198 {{get_override("script_project")|default("# (script_project placeholder)")}}
200 prj_run Synthesis -impl impl -forceAll
201 prj_run Translate -impl impl -forceAll
202 prj_run Map -impl impl -forceAll
203 prj_run PAR -impl impl -forceAll
204 prj_run Export -impl "impl" -forceAll -task Bitgen
205 {{get_override("script_after_export")|default("# (script_after_export placeholder)")}}
211 {% for port_name, pin_name, extras in platform.iter_port_constraints_bits() -%}
212 LOCATE COMP "{{port_name}}" SITE "{{pin_name}}";
213 IOBUF PORT "{{port_name}}"
214 {%- for key, value in extras.items() %} {{key}}={{value}}{% endfor %};
216 {% for signal, frequency in platform.iter_clock_constraints() -%}
217 FREQUENCY NET "{{signal|hierarchy("/")}}" {{frequency/1000000}} MHZ;
219 {{get_override("add_preferences")|default("# (add_preferences placeholder)")}}
222 {% for signal, frequency in platform.iter_clock_constraints() -%}
223 create_clock -period {{1000000000/frequency}} [get_nets {{signal|hierarchy("/")}}]
225 {{get_override("add_constraints")|default("# (add_constraints placeholder)")}}
228 _diamond_command_templates
= [
229 # These don't have any usable command-line option overrides.
231 {{get_tool("pnmainc")}}
235 {{get_tool("ddtcmd")}}
237 -if {{name}}_impl/{{name}}_impl.bit -of {{name}}.bit
240 {{get_tool("ddtcmd")}}
241 -oft -svfsingle -revd -op "Fast Program"
242 -if {{name}}_impl/{{name}}_impl.bit -of {{name}}.svf
248 def __init__(self
, *, toolchain
="Trellis"):
251 assert toolchain
in ("Trellis", "Diamond")
252 self
.toolchain
= toolchain
255 def required_tools(self
):
256 if self
.toolchain
== "Trellis":
257 return self
._trellis
_required
_tools
258 if self
.toolchain
== "Diamond":
259 return self
._diamond
_required
_tools
263 def file_templates(self
):
264 if self
.toolchain
== "Trellis":
265 return self
._trellis
_file
_templates
266 if self
.toolchain
== "Diamond":
267 return self
._diamond
_file
_templates
271 def command_templates(self
):
272 if self
.toolchain
== "Trellis":
273 return self
._trellis
_command
_templates
274 if self
.toolchain
== "Diamond":
275 return self
._diamond
_command
_templates
278 def create_missing_domain(self
, name
):
279 # Lattice ECP devices have two global set/reset signals: PUR, which is driven at startup
280 # by the configuration logic and unconditionally resets every storage element, and GSR,
281 # which is driven by user logic and each storage element may be configured as affected or
282 # unaffected by GSR. PUR is purely asynchronous, so even though it is a low-skew global
283 # network, its deassertion may violate a setup/hold constraint with relation to a user
284 # clock. To avoid this, a GSR/SGSR instance should be driven synchronized to user clock.
285 if name
== "sync" and self
.default_clk
is not None:
286 clk_i
= self
.request(self
.default_clk
).i
287 if self
.default_rst
is not None:
288 rst_i
= self
.request(self
.default_rst
).i
295 # There is no end-of-startup signal on ECP5, but PUR is released after IOB enable, so
296 # a simple reset synchronizer (with PUR as the asynchronous reset) does the job.
298 Instance("FD1S3AX", p_GSR
="DISABLED", i_CK
=clk_i
, i_D
=~rst_i
, o_Q
=gsr0
),
299 Instance("FD1S3AX", p_GSR
="DISABLED", i_CK
=clk_i
, i_D
=gsr0
, o_Q
=gsr1
),
300 # Although we already synchronize the reset input to user clock, SGSR has dedicated
301 # clock routing to the center of the FPGA; use that just in case it turns out to be
302 # more reliable. (None of this is documented.)
303 Instance("SGSR", i_CLK
=clk_i
, i_GSR
=gsr1
),
305 # GSR implicitly connects to every appropriate storage element. As such, the sync
306 # domain is reset-less; domains driven by other clocks would need to have dedicated
307 # reset circuitry or otherwise meet setup/hold constraints on their own.
308 m
.domains
+= ClockDomain("sync", reset_less
=True)
309 m
.d
.comb
+= ClockSignal("sync").eq(clk_i
)
312 _single_ended_io_types
= [
313 "HSUL12", "LVCMOS12", "LVCMOS15", "LVCMOS18", "LVCMOS25", "LVCMOS33", "LVTTL33",
314 "SSTL135_I", "SSTL135_II", "SSTL15_I", "SSTL15_II", "SSTL18_I", "SSTL18_II",
316 _differential_io_types
= [
317 "BLVDS25", "BLVDS25E", "HSUL12D", "LVCMOS18D", "LVCMOS25D", "LVCMOS33D",
318 "LVDS", "LVDS25E", "LVPECL33", "LVPECL33E", "LVTTL33D", "MLVDS", "MLVDS25E",
319 "SLVS", "SSTL135D_II", "SSTL15D_II", "SSTL18D_II", "SUBLVDS",
322 def should_skip_port_component(self
, port
, attrs
, component
):
323 # On ECP5, a differential IO is placed by only instantiating an IO buffer primitive at
324 # the PIOA or PIOC location, which is always the non-inverting pin.
325 if attrs
.get("IO_TYPE", "LVCMOS25") in self
._differential
_io
_types
and component
== "n":
329 def _get_xdr_buffer(self
, m
, pin
, *, i_invert
=False, o_invert
=False):
330 def get_ireg(clk
, d
, q
):
331 for bit
in range(len(q
)):
332 m
.submodules
+= Instance("IFS1P3DX",
340 def get_oreg(clk
, d
, q
):
341 for bit
in range(len(q
)):
342 m
.submodules
+= Instance("OFS1P3DX",
350 def get_iddr(sclk
, d
, q0
, q1
):
351 for bit
in range(len(d
)):
352 m
.submodules
+= Instance("IDDRX1F",
356 o_Q0
=q0
[bit
], o_Q1
=q1
[bit
]
359 def get_oddr(sclk
, d0
, d1
, q
):
360 for bit
in range(len(q
)):
361 m
.submodules
+= Instance("ODDRX1F",
364 i_D0
=d0
[bit
], i_D1
=d1
[bit
],
368 def get_ineg(z
, invert
):
370 a
= Signal
.like(z
, name_suffix
="_n")
376 def get_oneg(a
, invert
):
378 z
= Signal
.like(a
, name_suffix
="_n")
386 pin_i
= get_ineg(pin
.i
, i_invert
)
388 pin_i0
= get_ineg(pin
.i0
, i_invert
)
389 pin_i1
= get_ineg(pin
.i1
, i_invert
)
392 pin_o
= get_oneg(pin
.o
, o_invert
)
394 pin_o0
= get_oneg(pin
.o0
, o_invert
)
395 pin_o1
= get_oneg(pin
.o1
, o_invert
)
399 i
= Signal(pin
.width
, name
="{}_xdr_i".format(pin
.name
))
401 o
= Signal(pin
.width
, name
="{}_xdr_o".format(pin
.name
))
402 if pin
.dir in ("oe", "io"):
403 t
= Signal(1, name
="{}_xdr_t".format(pin
.name
))
410 if pin
.dir in ("oe", "io"):
413 # Note that currently nextpnr will not pack an FF (*FS1P3DX) into the PIO.
415 get_ireg(pin
.i_clk
, i
, pin_i
)
417 get_oreg(pin
.o_clk
, pin_o
, o
)
418 if pin
.dir in ("oe", "io"):
419 get_oreg(pin
.o_clk
, ~pin
.oe
, t
)
422 get_iddr(pin
.i_clk
, i
, pin_i0
, pin_i1
)
424 get_oddr(pin
.o_clk
, pin_o0
, pin_o1
, o
)
425 if pin
.dir in ("oe", "io"):
426 # It looks like Diamond will not pack an OREG as a tristate register in a DDR PIO.
427 # It is not clear what is the recommended set of primitives for this task.
428 # Similarly, nextpnr will not pack anything as a tristate register in a DDR PIO.
429 get_oreg(pin
.o_clk
, ~pin
.oe
, t
)
435 def get_input(self
, pin
, port
, attrs
, invert
):
436 self
._check
_feature
("single-ended input", pin
, attrs
,
437 valid_xdrs
=(0, 1, 2), valid_attrs
=True)
439 i
, o
, t
= self
._get
_xdr
_buffer
(m
, pin
, i_invert
=invert
)
440 for bit
in range(len(port
)):
441 m
.submodules
["{}_{}".format(pin
.name
, bit
)] = Instance("IB",
447 def get_output(self
, pin
, port
, attrs
, invert
):
448 self
._check
_feature
("single-ended output", pin
, attrs
,
449 valid_xdrs
=(0, 1, 2), valid_attrs
=True)
451 i
, o
, t
= self
._get
_xdr
_buffer
(m
, pin
, o_invert
=invert
)
452 for bit
in range(len(port
)):
453 m
.submodules
["{}_{}".format(pin
.name
, bit
)] = Instance("OB",
459 def get_tristate(self
, pin
, port
, attrs
, invert
):
460 self
._check
_feature
("single-ended tristate", pin
, attrs
,
461 valid_xdrs
=(0, 1, 2), valid_attrs
=True)
463 i
, o
, t
= self
._get
_xdr
_buffer
(m
, pin
, o_invert
=invert
)
464 for bit
in range(len(port
)):
465 m
.submodules
["{}_{}".format(pin
.name
, bit
)] = Instance("OBZ",
472 def get_input_output(self
, pin
, port
, attrs
, invert
):
473 self
._check
_feature
("single-ended input/output", pin
, attrs
,
474 valid_xdrs
=(0, 1, 2), valid_attrs
=True)
476 i
, o
, t
= self
._get
_xdr
_buffer
(m
, pin
, i_invert
=invert
, o_invert
=invert
)
477 for bit
in range(len(port
)):
478 m
.submodules
["{}_{}".format(pin
.name
, bit
)] = Instance("BB",
486 def get_diff_input(self
, pin
, p_port
, n_port
, attrs
, invert
):
487 self
._check
_feature
("differential input", pin
, attrs
,
488 valid_xdrs
=(0, 1, 2), valid_attrs
=True)
490 i
, o
, t
= self
._get
_xdr
_buffer
(m
, pin
, i_invert
=invert
)
491 for bit
in range(len(p_port
)):
492 m
.submodules
["{}_{}".format(pin
.name
, bit
)] = Instance("IB",
498 def get_diff_output(self
, pin
, p_port
, n_port
, attrs
, invert
):
499 self
._check
_feature
("differential output", pin
, attrs
,
500 valid_xdrs
=(0, 1, 2), valid_attrs
=True)
502 i
, o
, t
= self
._get
_xdr
_buffer
(m
, pin
, o_invert
=invert
)
503 for bit
in range(len(p_port
)):
504 m
.submodules
["{}_{}".format(pin
.name
, bit
)] = Instance("OB",
510 def get_diff_tristate(self
, pin
, p_port
, n_port
, attrs
, invert
):
511 self
._check
_feature
("differential tristate", pin
, attrs
,
512 valid_xdrs
=(0, 1, 2), valid_attrs
=True)
514 i
, o
, t
= self
._get
_xdr
_buffer
(m
, pin
, o_invert
=invert
)
515 for bit
in range(len(p_port
)):
516 m
.submodules
["{}_{}".format(pin
.name
, bit
)] = Instance("OBZ",
523 def get_diff_input_output(self
, pin
, p_port
, n_port
, attrs
, invert
):
524 self
._check
_feature
("differential input/output", pin
, attrs
,
525 valid_xdrs
=(0, 1, 2), valid_attrs
=True)
527 i
, o
, t
= self
._get
_xdr
_buffer
(m
, pin
, i_invert
=invert
, o_invert
=invert
)
528 for bit
in range(len(p_port
)):
529 m
.submodules
["{}_{}".format(pin
.name
, bit
)] = Instance("BB",
537 # CDC primitives are not currently specialized for ECP5. While Diamond supports the necessary
538 # attributes (TBD); nextpnr-ecp5 does not.