Coverage for apio/scons/plugin_ice40.py: 100%
48 statements
« prev ^ index » next coverage.py v7.15.4, created at 2026-08-09 01:55 +0000
« prev ^ index » next coverage.py v7.15.4, created at 2026-08-09 01:55 +0000
1# -*- coding: utf-8 -*-
2# -- This file is part of the Apio project
3# -- (C) 2016-2018 FPGAwars
4# -- Author Jesús Arroyo
5# -- License GPLv2
6# -- Derived from:
7# ---- Platformio project
8# ---- (C) 2014-2016 Ivan Kravets <me@ikravets.com>
9# ---- License Apache v2
11"""Apio scons plugin for the ice40 architecture."""
13# pylint: disable=duplicate-code
15from pathlib import Path
16from SCons.Script import Builder
17from SCons.Builder import BuilderBase, CompositeBuilder
18from apio.common.common_util import SRC_SUFFIXES
19from apio.scons.apio_env import ApioEnv
20from apio.scons.plugin_base import PluginBase, ArchPluginInfo
21from apio.scons.plugin_util import (
22 verilator_lint_action,
23 has_testbench_name,
24 announce_testbench_action,
25 source_files_issue_scanner_action,
26 iverilog_action,
27 basename,
28 make_verilator_config_builder,
29 get_define_flags,
30)
33class PluginIce40(PluginBase):
34 """Apio scons plugin for the ice40 architecture."""
36 def __init__(self, apio_env: ApioEnv):
37 # -- Call parent constructor.
38 super().__init__(apio_env)
40 # -- Cache values.
41 yosys_path = Path(apio_env.params.environment.yosys_path)
42 self.yosys_lib_dir = yosys_path / "ice40"
43 self.sim_lib_files = [yosys_path / "ice40" / "cells_sim.v"]
44 self.lint_lib_files = self.sim_lib_files
46 def plugin_info(self) -> ArchPluginInfo:
47 """Return plugin specific parameters."""
48 return ArchPluginInfo(
49 constrains_file_suffix=".pcf",
50 pnr_file_suffix=".asc",
51 bitstream_file_suffix=".bin",
52 )
54 # @overrides
55 def synth_builder(self) -> BuilderBase | CompositeBuilder:
56 """Creates and returns the synth builder."""
58 # -- Keep short references.
59 apio_env = self.apio_env
60 params = apio_env.params
62 # -- The yosys synth builder.
63 return Builder(
64 action=(
65 'yosys -p "synth_ice40 -top {0} -json $TARGET {1}" '
66 "{2} -DSYNTHESIZE {3} $SOURCES"
67 ).format(
68 params.apio_env_params.top_module,
69 " ".join(params.apio_env_params.yosys_extra_options),
70 "" if params.verbosity.all or params.verbosity.synth else "-q",
71 get_define_flags(apio_env),
72 ),
73 suffix=".json",
74 src_suffix=SRC_SUFFIXES,
75 source_scanner=self.verilog_src_scanner,
76 )
78 # @overrides
79 def pnr_builder(self) -> BuilderBase | CompositeBuilder:
80 """Creates and returns the pnr builder."""
82 # -- Keep short references.
83 apio_env = self.apio_env
84 params = apio_env.params
86 # -- We use an emmiter to add to the builder a second output file.
87 def emitter(target, source, env):
88 _ = env # Unused
89 target.append(apio_env.target + ".pnr")
90 return target, source
92 # -- Create the builder.
93 return Builder(
94 action=(
95 "nextpnr-ice40 --{0} --package {1} --json $SOURCE "
96 "--asc $TARGET --report {2} --pcf {3} {4} {5} {6}"
97 ).format(
98 params.fpga_info.ice40_params.type,
99 params.fpga_info.ice40_params.package,
100 apio_env.target + ".pnr",
101 self.constrain_file(),
102 "" if params.verbosity.all or params.verbosity.pnr else "-q",
103 "--gui" if params.nextpnr_gui else "",
104 " ".join(params.apio_env_params.nextpnr_extra_options),
105 ),
106 suffix=".asc",
107 src_suffix=".json",
108 emitter=emitter,
109 )
111 # @overrides
112 def bitstream_builder(self) -> BuilderBase | CompositeBuilder:
113 """Creates and returns the bitstream builder."""
114 return Builder(
115 action="icepack $SOURCE $TARGET",
116 suffix=".bin",
117 src_suffix=".asc",
118 )
120 # @overrides
121 def testbench_compile_builder(self) -> BuilderBase | CompositeBuilder:
122 """Creates and returns the testbench compile builder."""
124 # -- Keep short references.
125 apio_env = self.apio_env
126 params = apio_env.params
128 # -- Sanity checks
129 assert apio_env.targeting_one_of("sim", "test")
130 assert params.target.HasField("sim") or params.target.HasField("test")
132 # -- We use a generator because we need a different action
133 # -- string for sim and test.
134 def action_generator(target, source, env, for_signature):
135 _ = (source, env, for_signature) # Unused
136 # Extract testbench name from target file name.
137 testbench_file = str(target[0])
138 assert has_testbench_name(testbench_file), testbench_file
139 testbench_name = basename(testbench_file)
141 # Construct the actions list.
142 action = [
143 # -- Print a testbench title.
144 announce_testbench_action(),
145 # -- Scan source files for issues.
146 source_files_issue_scanner_action(),
147 # -- Perform the actual test or sim compilation.
148 iverilog_action(
149 apio_env,
150 verbose=params.verbosity.all,
151 vcd_output_name=testbench_name,
152 is_interactive=apio_env.targeting_one_of("sim"),
153 extra_params=["-DNO_ICE40_DEFAULT_ASSIGNMENTS"],
154 lib_dirs=[self.yosys_lib_dir],
155 lib_files=self.sim_lib_files,
156 ),
157 ]
158 return action
160 # -- The testbench compiler builder.
161 return Builder(
162 # -- Dynamic action string generator.
163 generator=action_generator,
164 suffix=".out",
165 src_suffix=SRC_SUFFIXES,
166 source_scanner=self.verilog_src_scanner,
167 )
169 # @overrides
170 def lint_config_builder(self) -> BuilderBase:
171 """Creates and returns the lint config builder."""
173 # -- Sanity checks
174 assert self.apio_env.targeting_one_of("lint")
176 # -- Make the builder.
177 return make_verilator_config_builder(
178 self.yosys_lib_dir,
179 rules_to_supress=[],
180 )
182 # @overrides
183 def lint_builder(self) -> BuilderBase | CompositeBuilder:
184 """Creates and returns the lint builder."""
186 return Builder(
187 action=verilator_lint_action(
188 self.apio_env,
189 extra_params=["-DNO_ICE40_DEFAULT_ASSIGNMENTS"],
190 lib_dirs=[self.yosys_lib_dir],
191 lib_files=self.lint_lib_files,
192 ),
193 src_suffix=SRC_SUFFIXES,
194 source_scanner=self.verilog_src_scanner,
195 )