Coverage for apio / scons / plugin_ice40.py: 100%

48 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-08 02:47 +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 

10 

11"""Apio scons plugin for the ice40 architecture.""" 

12 

13# pylint: disable=duplicate-code 

14 

15from pathlib import Path 

16from SCons.Script import Builder 

17from SCons.Builder import BuilderBase 

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) 

31 

32 

33class PluginIce40(PluginBase): 

34 """Apio scons plugin for the ice40 architecture.""" 

35 

36 def __init__(self, apio_env: ApioEnv): 

37 # -- Call parent constructor. 

38 super().__init__(apio_env) 

39 

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 

45 

46 def plugin_info(self) -> ArchPluginInfo: 

47 """Return plugin specific parameters.""" 

48 return ArchPluginInfo( 

49 constrains_file_ext=".pcf", 

50 bin_file_suffix=".bin", 

51 clk_name_index=0, 

52 ) 

53 

54 # @overrides 

55 def synth_builder(self) -> BuilderBase: 

56 """Creates and returns the synth builder.""" 

57 

58 # -- Keep short references. 

59 apio_env = self.apio_env 

60 params = apio_env.params 

61 

62 # -- The yosys synth builder. 

63 return Builder( 

64 action=( 

65 'yosys -p "synth_ice40 -top {0} {1} -json $TARGET" ' 

66 "{2} -DSYNTHESIZE {3} $SOURCES" 

67 ).format( 

68 params.apio_env_params.top_module, 

69 " ".join(params.apio_env_params.yosys_synth_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 ) 

77 

78 # @overrides 

79 def pnr_builder(self) -> BuilderBase: 

80 """Creates and returns the pnr builder.""" 

81 

82 # -- Keep short references. 

83 apio_env = self.apio_env 

84 params = apio_env.params 

85 

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 

91 

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}" 

97 ).format( 

98 params.fpga_info.ice40.type, 

99 params.fpga_info.ice40.pack, 

100 apio_env.target + ".pnr", 

101 self.constrain_file(), 

102 "" if params.verbosity.all or params.verbosity.pnr else "-q", 

103 " ".join(params.apio_env_params.nextpnr_extra_options), 

104 ), 

105 suffix=".asc", 

106 src_suffix=".json", 

107 emitter=emitter, 

108 ) 

109 

110 # @overrides 

111 def bitstream_builder(self) -> BuilderBase: 

112 """Creates and returns the bitstream builder.""" 

113 return Builder( 

114 action="icepack $SOURCE $TARGET", 

115 suffix=".bin", 

116 src_suffix=".asc", 

117 ) 

118 

119 # @overrides 

120 def testbench_compile_builder(self) -> BuilderBase: 

121 """Creates and returns the testbench compile builder.""" 

122 

123 # -- Keep short references. 

124 apio_env = self.apio_env 

125 params = apio_env.params 

126 

127 # -- Sanity checks 

128 assert apio_env.targeting_one_of("sim", "test") 

129 assert params.target.HasField("sim") or params.target.HasField("test") 

130 

131 # -- We use a generator because we need a different action 

132 # -- string for sim and test. 

133 def action_generator(target, source, env, for_signature): 

134 _ = (source, env, for_signature) # Unused 

135 # Extract testbench name from target file name. 

136 testbench_file = str(target[0]) 

137 assert has_testbench_name(testbench_file), testbench_file 

138 testbench_name = basename(testbench_file) 

139 

140 # Construct the actions list. 

141 action = [ 

142 # -- Print a testbench title. 

143 announce_testbench_action(), 

144 # -- Scan source files for issues. 

145 source_files_issue_scanner_action(), 

146 # -- Perform the actual test or sim compilation. 

147 iverilog_action( 

148 apio_env, 

149 verbose=params.verbosity.all, 

150 vcd_output_name=testbench_name, 

151 is_interactive=apio_env.targeting_one_of("sim"), 

152 extra_params=["-DNO_ICE40_DEFAULT_ASSIGNMENTS"], 

153 lib_dirs=[self.yosys_lib_dir], 

154 lib_files=self.sim_lib_files, 

155 ), 

156 ] 

157 return action 

158 

159 # -- The testbench compiler builder. 

160 return Builder( 

161 # -- Dynamic action string generator. 

162 generator=action_generator, 

163 suffix=".out", 

164 src_suffix=SRC_SUFFIXES, 

165 source_scanner=self.verilog_src_scanner, 

166 ) 

167 

168 # @overrides 

169 def lint_config_builder(self) -> BuilderBase: 

170 """Creates and returns the lint config builder.""" 

171 

172 # -- Sanity checks 

173 assert self.apio_env.targeting_one_of("lint") 

174 

175 # -- Make the builder. 

176 return make_verilator_config_builder( 

177 self.yosys_lib_dir, 

178 rules_to_supress=[], 

179 ) 

180 

181 # @overrides 

182 def lint_builder(self) -> BuilderBase: 

183 """Creates and returns the lint builder.""" 

184 

185 return Builder( 

186 action=verilator_lint_action( 

187 self.apio_env, 

188 extra_params=["-DNO_ICE40_DEFAULT_ASSIGNMENTS"], 

189 lib_dirs=[self.yosys_lib_dir], 

190 lib_files=self.lint_lib_files, 

191 ), 

192 src_suffix=SRC_SUFFIXES, 

193 source_scanner=self.verilog_src_scanner, 

194 )