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

47 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-11-06 10:20 +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.yosys_lib_file = yosys_path / "ice40" / "cells_sim.v" 

44 

45 def plugin_info(self) -> ArchPluginInfo: 

46 """Return plugin specific parameters.""" 

47 return ArchPluginInfo( 

48 constrains_file_ext=".pcf", 

49 bin_file_suffix=".bin", 

50 clk_name_index=0, 

51 ) 

52 

53 # @overrides 

54 def synth_builder(self) -> BuilderBase: 

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

56 

57 # -- Keep short references. 

58 apio_env = self.apio_env 

59 params = apio_env.params 

60 

61 # -- The yosys synth builder. 

62 return Builder( 

63 action=( 

64 'yosys -p "synth_ice40 -top {0} {1} -json $TARGET" {2} {3} ' 

65 "$SOURCES" 

66 ).format( 

67 params.apio_env_params.top_module, 

68 " ".join(params.apio_env_params.yosys_synth_extra_options), 

69 "" if params.verbosity.all or params.verbosity.synth else "-q", 

70 get_define_flags(apio_env), 

71 ), 

72 suffix=".json", 

73 src_suffix=SRC_SUFFIXES, 

74 source_scanner=self.verilog_src_scanner, 

75 ) 

76 

77 # @overrides 

78 def pnr_builder(self) -> BuilderBase: 

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

80 

81 # -- Keep short references. 

82 apio_env = self.apio_env 

83 params = apio_env.params 

84 

85 # -- We use an emmiter to add to the builder a second output file. 

86 def emitter(target, source, env): 

87 _ = env # Unused 

88 target.append(apio_env.target + ".pnr") 

89 return target, source 

90 

91 # -- Create the builder. 

92 return Builder( 

93 action=( 

94 "nextpnr-ice40 --{0} --package {1} --json $SOURCE " 

95 "--asc $TARGET --report {2} --pcf {3} {4} {5}" 

96 ).format( 

97 params.fpga_info.ice40.type, 

98 params.fpga_info.ice40.pack, 

99 apio_env.target + ".pnr", 

100 self.constrain_file(), 

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

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

103 ), 

104 suffix=".asc", 

105 src_suffix=".json", 

106 emitter=emitter, 

107 ) 

108 

109 # @overrides 

110 def bitstream_builder(self) -> BuilderBase: 

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

112 return Builder( 

113 action="icepack $SOURCE $TARGET", 

114 suffix=".bin", 

115 src_suffix=".asc", 

116 ) 

117 

118 # @overrides 

119 def testbench_compile_builder(self) -> BuilderBase: 

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

121 

122 # -- Keep short references. 

123 apio_env = self.apio_env 

124 params = apio_env.params 

125 

126 # -- Sanity checks 

127 assert apio_env.targeting("sim", "test") 

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

129 

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

131 # -- string for sim and test. 

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

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

134 # Extract testbench name from target file name. 

135 testbench_file = str(target[0]) 

136 assert has_testbench_name(testbench_file), testbench_file 

137 testbench_name = basename(testbench_file) 

138 

139 # Construct the actions list. 

140 action = [ 

141 # -- Print a testbench title. 

142 announce_testbench_action(), 

143 # -- Scan source files for issues. 

144 source_files_issue_scanner_action(), 

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

146 iverilog_action( 

147 apio_env, 

148 verbose=params.verbosity.all, 

149 vcd_output_name=testbench_name, 

150 is_interactive=apio_env.targeting("sim"), 

151 extra_params=["-DNO_ICE40_DEFAULT_ASSIGNMENTS"], 

152 lib_dirs=[self.yosys_lib_dir], 

153 lib_files=[self.yosys_lib_file], 

154 ), 

155 ] 

156 return action 

157 

158 # -- The testbench compiler builder. 

159 return Builder( 

160 # -- Dynamic action string generator. 

161 generator=action_generator, 

162 suffix=".out", 

163 src_suffix=SRC_SUFFIXES, 

164 source_scanner=self.verilog_src_scanner, 

165 ) 

166 

167 # @overrides 

168 def lint_config_builder(self) -> BuilderBase: 

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

170 

171 # -- Sanity checks 

172 assert self.apio_env.targeting("lint") 

173 

174 # -- Make the builder. 

175 return make_verilator_config_builder(self.yosys_lib_dir) 

176 

177 # @overrides 

178 def lint_builder(self) -> BuilderBase: 

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

180 

181 return Builder( 

182 action=verilator_lint_action( 

183 self.apio_env, 

184 extra_params=["-DNO_ICE40_DEFAULT_ASSIGNMENTS"], 

185 lib_dirs=[self.yosys_lib_dir], 

186 lib_files=[self.yosys_lib_file], 

187 ), 

188 src_suffix=SRC_SUFFIXES, 

189 source_scanner=self.verilog_src_scanner, 

190 )