1 from abc
import abstractproperty
7 __all__
= ["LatticeICE40Platform"]
10 class LatticeICE40Platform(TemplatedPlatform
):
18 * ``verbose``: enables logging of informational messages to standard error.
19 * ``read_verilog_opts``: adds options for ``read_verilog`` Yosys command.
20 * ``synth_opts``: adds options for ``synth_ice40`` Yosys command.
21 * ``script_after_read``: inserts commands after ``read_ilang`` in Yosys script.
22 * ``script_after_synth``: inserts commands after ``synth_ice40`` in Yosys script.
23 * ``yosys_opts``: adds extra options for Yosys.
24 * ``nextpnr_opts``: adds extra and overrides default options (``--placer heap``)
28 * ``{{name}}.rpt``: Yosys log.
29 * ``{{name}}.json``: synthesized RTL.
30 * ``{{name}}.tim``: nextpnr log.
31 * ``{{name}}.asc``: ASCII bitstream.
32 * ``{{name}}.bin``: binary bitstream.
35 device
= abstractproperty()
36 package
= abstractproperty()
38 _nextpnr_device_options
= {
39 "iCE40LP384": "--lp384",
40 "iCE40LP1K": "--lp1k",
41 "iCE40LP4K": "--lp8k",
42 "iCE40LP8K": "--lp8k",
43 "iCE40HX1K": "--hx1k",
44 "iCE40HX4K": "--hx8k",
45 "iCE40HX8K": "--hx8k",
46 "iCE40UP5K": "--up5k",
49 _nextpnr_package_options
= {
55 **TemplatedPlatform
.build_script_templates
,
58 {{emit_design("rtlil")}}
62 {% for file in platform.iter_extra_files(".v") -%}
63 read_verilog {{get_override("read_opts")|join(" ")}} {{file}}
65 {% for file in platform.iter_extra_files(".sv") -%}
66 read_verilog -sv {{get_override("read_opts")|join(" ")}} {{file}}
68 read_ilang {{name}}.il
69 {{get_override("script_after_read")|default("# (script_after_read placeholder)")}}
70 synth_ice40 {{get_override("synth_opts")|join(" ")}} -top {{name}}
71 {{get_override("script_after_synth")|default("# (script_after_synth placeholder)")}}
72 write_json {{name}}.json
76 {% for port_name, pin_name, attrs in platform.iter_port_constraints_bits() -%}
77 set_io {{port_name}} {{pin_name}}
80 "{{name}}_pre_pack.py": r
"""
82 {% for signal, frequency in platform.iter_clock_constraints() -%}
84 ctx.addClock("{{signal.name}}", {{frequency/1000000}})
92 {{get_override("yosys_opts")|join(" ")}}
97 {{get_tool("nextpnr-ice40")}}
99 {{get_override("nextpnr_opts")|default(["--placer","heap"])|join(" ")}}
101 {{platform._nextpnr_device_options[platform.device]}}
103 {{platform.package|lower}}{{platform._nextpnr_package_options[platform.device]}}
106 --pre-pack {{name}}_pre_pack.py
110 {{get_tool("icepack")}}
117 def should_skip_port_component(self
, port
, attrs
, component
):
118 # On iCE40, a differential input is placed by only instantiating an SB_IO primitive for
119 # the pin with z=0, which is the non-inverting pin. The pinout unfortunately differs
120 # between LP/HX and UP series:
121 # * for LP/HX, z=0 is DPxxB (B is non-inverting, A is inverting)
122 # * for UP, z=0 is IOB_xxA (A is non-inverting, B is inverting)
123 if attrs
.get("IO_STANDARD", "SB_LVCMOS") == "SB_LVDS_INPUT" and component
== "n":
127 def _get_io_buffer(self
, m
, pin
, port
, attrs
, i_invert
=None, o_invert
=None):
128 def get_dff(clk
, d
, q
):
129 m
.submodules
+= Instance("$dff",
136 def get_ixor(y
, invert
):
140 a
= Signal
.like(y
, name_suffix
="_x{}".format(1 if invert
else 0))
141 for bit
in range(len(y
)):
142 m
.submodules
+= Instance("SB_LUT4",
143 p_LUT_INIT
=0b01 if invert
else 0b10,
151 def get_oxor(a
, invert
):
155 y
= Signal
.like(a
, name_suffix
="_x{}".format(1 if invert
else 0))
156 for bit
in range(len(a
)):
157 m
.submodules
+= Instance("SB_LUT4",
158 p_LUT_INIT
=0b01 if invert
else 0b10,
166 if "GLOBAL" in attrs
:
167 is_global_input
= bool(attrs
["GLOBAL"])
170 is_global_input
= False
171 assert not (is_global_input
and i_invert
)
175 pin_i
= get_ixor(pin
.i
, i_invert
)
177 pin_i0
= get_ixor(pin
.i0
, i_invert
)
178 pin_i1
= get_ixor(pin
.i1
, i_invert
)
181 pin_o
= get_oxor(pin
.o
, o_invert
)
183 pin_o0
= get_oxor(pin
.o0
, o_invert
)
184 pin_o1
= get_oxor(pin
.o1
, o_invert
)
186 if "i" in pin
.dir and pin
.xdr
== 2:
187 i0_ff
= Signal
.like(pin_i0
, name_suffix
="_ff")
188 i1_ff
= Signal
.like(pin_i1
, name_suffix
="_ff")
189 get_dff(pin
.i_clk
, i0_ff
, pin_i0
)
190 get_dff(pin
.i_clk
, i1_ff
, pin_i1
)
191 if "o" in pin
.dir and pin
.xdr
== 2:
192 o1_ff
= Signal
.like(pin_o1
, name_suffix
="_ff")
193 get_dff(pin
.o_clk
, pin_o1
, o1_ff
)
195 for bit
in range(len(port
)):
197 ("io", "PACKAGE_PIN", port
[bit
]),
198 *(("p", key
, value
) for key
, value
in attrs
.items()),
201 if "i" not in pin
.dir:
202 i_type
= 0b00 # PIN_NO_INPUT aka PIN_INPUT_REGISTERED
204 i_type
= 0b01 # PIN_INPUT
206 i_type
= 0b00 # PIN_INPUT_REGISTERED
207 if "o" not in pin
.dir:
208 o_type
= 0b0000 # PIN_NO_OUTPUT
209 elif pin
.xdr
== 0 and pin
.dir == "o":
210 o_type
= 0b0110 # PIN_OUTPUT
212 o_type
= 0b1010 # PIN_OUTPUT_TRISTATE
213 elif pin
.xdr
== 1 and pin
.dir == "o":
214 o_type
= 0b0101 # PIN_OUTPUT_REGISTERED
216 o_type
= 0b1101 # PIN_OUTPUT_REGISTERED_ENABLE_REGISTERED
217 elif pin
.xdr
== 2 and pin
.dir == "o":
218 o_type
= 0b0100 # PIN_OUTPUT_DDR
220 o_type
= 0b1100 # PIN_OUTPUT_DDR_ENABLE_REGISTERED
221 io_args
.append(("p", "PIN_TYPE", (o_type
<< 2) | i_type
))
223 if hasattr(pin
, "i_clk"):
224 io_args
.append(("i", "INPUT_CLK", pin
.i_clk
))
225 if hasattr(pin
, "o_clk"):
226 io_args
.append(("i", "OUTPUT_CLK", pin
.o_clk
))
229 if pin
.xdr
== 0 and is_global_input
:
230 io_args
.append(("o", "GLOBAL_BUFFER_OUTPUT", pin
.i
[bit
]))
232 io_args
.append(("o", "D_IN_0", pin_i
[bit
]))
234 # Re-register both inputs before they enter fabric. This increases hold time
235 # to an entire cycle, and adds one cycle of latency.
236 io_args
.append(("o", "D_IN_0", i0_ff
))
237 io_args
.append(("o", "D_IN_1", i1_ff
))
240 io_args
.append(("i", "D_OUT_0", pin_o
[bit
]))
242 # Re-register negedge output after it leaves fabric. This increases setup time
243 # to an entire cycle, and doesn't add latency.
244 io_args
.append(("i", "D_OUT_0", pin_o0
[bit
]))
245 io_args
.append(("i", "D_OUT_1", o1_ff
))
247 if pin
.dir in ("oe", "io"):
248 io_args
.append(("i", "OUTPUT_ENABLE", pin
.oe
))
251 m
.submodules
+= Instance("SB_GB_IO", *io_args
)
253 m
.submodules
+= Instance("SB_IO", *io_args
)
255 def get_input(self
, pin
, port
, attrs
, invert
):
256 self
._check
_feature
("single-ended input", pin
, attrs
,
257 valid_xdrs
=(0, 1, 2), valid_attrs
=True)
259 self
._get
_io
_buffer
(m
, pin
, port
, attrs
, i_invert
=True if invert
else None)
262 def get_output(self
, pin
, port
, attrs
, invert
):
263 self
._check
_feature
("single-ended output", pin
, attrs
,
264 valid_xdrs
=(0, 1, 2), valid_attrs
=True)
266 self
._get
_io
_buffer
(m
, pin
, port
, attrs
, o_invert
=True if invert
else None)
269 def get_tristate(self
, pin
, port
, attrs
, invert
):
270 self
._check
_feature
("single-ended tristate", pin
, attrs
,
271 valid_xdrs
=(0, 1, 2), valid_attrs
=True)
273 self
._get
_io
_buffer
(m
, pin
, port
, attrs
, o_invert
=True if invert
else None)
276 def get_input_output(self
, pin
, port
, attrs
, invert
):
277 self
._check
_feature
("single-ended input/output", pin
, attrs
,
278 valid_xdrs
=(0, 1, 2), valid_attrs
=True)
280 self
._get
_io
_buffer
(m
, pin
, port
, attrs
, i_invert
=True if invert
else None,
281 o_invert
=True if invert
else None)
284 def get_diff_input(self
, pin
, p_port
, n_port
, attrs
, invert
):
285 self
._check
_feature
("differential input", pin
, attrs
,
286 valid_xdrs
=(0, 1, 2), valid_attrs
=True)
288 # See comment in should_skip_port_component above.
289 self
._get
_io
_buffer
(m
, pin
, p_port
, attrs
, i_invert
=True if invert
else None)
292 def get_diff_output(self
, pin
, p_port
, n_port
, attrs
, invert
):
293 self
._check
_feature
("differential output", pin
, attrs
,
294 valid_xdrs
=(0, 1, 2), valid_attrs
=True)
296 # Note that the non-inverting output pin is not driven the same way as a regular
297 # output pin. The inverter introduces a delay, so for a non-inverting output pin,
298 # an identical delay is introduced by instantiating a LUT. This makes the waveform
299 # perfectly symmetric in the xdr=0 case.
300 self
._get
_io
_buffer
(m
, pin
, p_port
, attrs
, o_invert
=invert
)
301 self
._get
_io
_buffer
(m
, pin
, n_port
, attrs
, o_invert
=not invert
)
304 # Tristate and bidirectional buffers are not supported on iCE40 because it requires external
305 # termination, which is incompatible for input and output differential I/Os.