qualcomm/qbox
Loading...
Searching...
No Matches
module_factory_container.h
1/*
2 * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause-Clear
5 */
6
7#ifndef GREENSOCS_MODULE_FACTORY_H
8#define GREENSOCS_MODULE_FACTORY_H
9
10#include "cciutils.h"
11#include <cci_configuration>
12#include <systemc>
13#include <tlm>
14#include <tlm_utils/multi_passthrough_target_socket.h>
15#include <tlm_utils/simple_initiator_socket.h>
16#include <tlm_utils/simple_target_socket.h>
17
18#include <dynlib_loader.h>
19#include <scp/report.h>
20#include <scp/helpers.h>
21
22#include <libgsutils.h>
23#include <libgssync.h>
24
25#include <map>
26
27#include <ports/target-signal-socket.h>
28#include <ports/initiator-signal-socket.h>
29
30#ifndef WITHOUT_QEMU
31#include <ports/target.h>
32#include <ports/initiator.h>
33#include <ports/qemu-target-signal-socket.h>
34#include <ports/qemu-initiator-signal-socket.h>
35#endif
36
37#include <ports/biflow-socket.h>
38#include <module_factory_registery.h>
39#include <transaction_forwarder_if.h>
40#include <tlm_sockets_buswidth.h>
41#include <algorithm>
42#include <vector>
43#include <iterator>
44#include <regex>
45#include <cctype>
46
47namespace gs {
48namespace ModuleFactory {
49
50class ContainerBase : public virtual sc_core::sc_module,
51 public transaction_forwarder_if<CONTAINER>,
52 public container_module
53{
63
64private:
65 // Performance optimization caches (lazy initialization)
66 mutable std::map<std::string, cci::cci_value_list> m_module_args_cache;
67 mutable std::map<std::string, bool> m_is_absolute_path_cache;
68 mutable std::map<std::string, std::map<std::string, std::set<std::string>>>
69 m_bind_refs_cache; // [module_name][target] -> referrers
70 mutable std::map<std::string, std::vector<std::pair<std::string, cci::cci_value>>>
71 m_moduletype_params_cache; // container_full_name -> params
72 mutable std::map<std::string, std::set<std::string>>
73 m_per_pass_nested_deps_cache; // Per-pass cache: container_name -> deps (cleared each pass)
74 mutable std::map<std::string, bool> m_is_container_type_cache; // Cache for is_container_type() results
75 mutable std::map<std::string, std::set<std::string>> m_cci_children_cache;
76 mutable std::map<std::string, std::vector<std::pair<std::string, cci::cci_value>>> m_cci_bind_params_cache;
77 mutable bool m_cci_children_cache_initialized = false;
78
79public:
80 SCP_LOGGER(());
81
82 sc_core::sc_module* construct_module(std::string moduletype, sc_core::sc_module_name name, cci::cci_value_list args)
83 {
85 cci::cci_value v = cci::cci_value::from_json("\"" + moduletype + "\"");
86 if (!v.try_get<gs::cci_constructor_vl>(m_fac)) {
87 register_module_from_dylib(name);
88 }
89
90 if (v.try_get<gs::cci_constructor_vl>(m_fac)) {
95 sc_core::sc_module* mod = (m_fac)(name, args);
96 if (mod) {
97 m_constructedModules.emplace_back(mod);
98 return mod;
99 } else {
100 SC_REPORT_ERROR("ModuleFactory", ("Null module type: " + moduletype).c_str());
101 }
102 } else {
103 SC_REPORT_ERROR("ModuleFactory", ("Can't find module type: " + moduletype).c_str());
104 }
105 __builtin_unreachable();
106 }
107
112 sc_core::sc_module* construct_module(std::string moduletype, sc_core::sc_module_name name)
113 {
114 cci::cci_value_list emptyargs;
115 return construct_module(moduletype, name, emptyargs);
116 }
117
118 std::string get_parent_name(const std::string& module_name)
119 {
120 return module_name.substr(0, module_name.find_last_of("."));
121 }
122
129 bool is_absolute_path(const std::string& name)
130 {
131 // Check cache first
132 auto it = m_is_absolute_path_cache.find(name);
133 if (it != m_is_absolute_path_cache.end()) {
134 return it->second;
135 }
136
137 bool result = false;
138 std::string container_name = std::string(this->name());
139
140 // A path rooted at this container is already a fully qualified
141 // SystemC object name and must not be prefixed again.
142 if (name.size() > container_name.size() && name.substr(0, container_name.size()) == container_name &&
143 name[container_name.size()] == '.') {
144 result = true;
145 } else if (name.find('.') != std::string::npos) {
146 // If the name contains dots, it's a hierarchical path
147 // Get the parent container name (e.g., "parent" from "parent.child_container")
148 std::string parent_name = get_parent_name(container_name);
149 if (parent_name.empty()) {
150 // We're at the top level, everything with dots is relative to us
151 result = false;
152 } else if (name.find(parent_name + ".") == 0) {
153 // Check if the name starts with the parent or any ancestor
154 // If it starts with parent name, it's an absolute path from parent's perspective
155 result = true;
156 } else {
157 // Check if it exists in the global hierarchy
158 sc_core::sc_object* obj = find_sc_obj(nullptr, name, true);
159 if (obj != nullptr) {
160 // Object exists, check if it's inside this container
161 std::string obj_name = std::string(obj->name());
162 result = obj_name.find(container_name + ".") != 0;
163 } else {
164 result = false;
165 }
166 }
167 } else {
168 // For simple names (no dots), check if they exist in global hierarchy
169 result = find_sc_obj(nullptr, name, true) != nullptr;
170 }
171
172 // Cache the result
173 m_is_absolute_path_cache[name] = result;
174 return result;
175 }
176
177 bool is_container_arg_mod_name(const std::string& name)
178 {
179 if (!this->container_mod_arg) return false;
180 std::string arg_mod_full_name_str = std::string(this->container_mod_arg->name());
181 std::string arg_mod_name_str = arg_mod_full_name_str.substr(get_parent_name(arg_mod_full_name_str).size() + 1);
182 return name == arg_mod_name_str;
183 }
184
191 cci::cci_value_list get_module_args(std::string modulename)
192 {
193 // Check cache first
194 auto cache_it = m_module_args_cache.find(modulename);
195 if (cache_it != m_module_args_cache.end()) {
196 return cache_it->second;
197 }
198
199 cci::cci_value_list args;
200 for (int i = 0; cci::cci_get_broker().has_preset_value(modulename + ".args." + std::to_string(i)); i++) {
201 SCP_TRACE(())("Looking for : {}.args.{}", modulename, std::to_string(i));
202
203 gs::cci_clear_unused(m_broker, modulename + ".args." + std::to_string(i));
204 auto arg = cci::cci_get_broker().get_preset_cci_value(modulename + ".args." + std::to_string(i));
205 if (arg.is_string() && std::string(arg.get_string())[0] == '&') {
206 std::string arg_path = std::string(arg.get_string()).substr(1); // Remove leading '&'
207
208 // Check if it's an absolute path (exists in global hierarchy)
209 if (!is_absolute_path(arg_path)) {
210 // It's a relative path, prepend the parent
211 std::string parent = is_container_arg_mod_name(arg_path)
212 ? get_parent_name(std::string(this->container_mod_arg->name()))
213 : get_parent_name(modulename);
214 // Only prepend if parent is not already in the path
215 if (arg_path.find(parent) == std::string::npos) {
216 arg.set_string("&" + parent + "." + arg_path);
217 } else {
218 arg.set_string("&" + arg_path);
219 }
220 } else {
221 // It's an absolute path, use as-is
222 arg.set_string("&" + arg_path);
223 }
224 }
225 args.push_back(arg);
226 }
227
228 // Cache the result
229 m_module_args_cache[modulename] = args;
230 return args;
231 }
232
233 template <typename I, typename T>
234 bool try_bind(sc_core::sc_object* i_obj, sc_core::sc_object* t_obj)
235 {
236 auto i = dynamic_cast<I*>(i_obj);
237 auto t = dynamic_cast<T*>(t_obj);
238 if (i && t) {
239 i->bind(*t);
240 SCP_INFO(())
241 ("Binding initiator socket: {}({}) with the target socket: {}({})", i_obj->name(), typeid(i).name(),
242 t_obj->name(), typeid(t).name());
243 return true;
244 }
245 t = dynamic_cast<T*>(i_obj);
246 i = dynamic_cast<I*>(t_obj);
247 if (i && t) {
248 i->bind(*t);
249 SCP_INFO(())
250 ("Binding initiator socket: {}({}) with the target socket: {}({})", t_obj->name(), typeid(i).name(),
251 i_obj->name(), typeid(t).name());
252 return true;
253 }
254 return false;
255 }
256
257 /* bind all know port types */
265 void name_bind(sc_core::sc_module* m)
266 {
267 for (const auto& param : get_cci_children(m->name())) {
268 /* We should have a param like
269 * foo = &a.baa
270 * foo should relate to an object in this model. a.b.bar should relate to an other
271 * object Note that the names are relative to the current module (this).
272 */
273 std::string targetname = std::string(m->name()) + "." + param;
274 gs::cci_clear_unused(m_broker, targetname + ".0");
275 if (m_broker.get_preset_cci_value(targetname + ".0").is_string()) {
276 // deal with an alias
277 std::string src = m_broker.get_preset_cci_value(targetname + ".0").get_string();
278 m_broker.set_preset_cci_value(targetname + ".address", m_broker.get_preset_cci_value(src + ".address"));
279 m_broker.set_preset_cci_value(targetname + ".size", m_broker.get_preset_cci_value(src + ".size"));
280 }
281 std::string bindname = targetname + ".bind";
282 gs::cci_clear_unused(m_broker, bindname);
283 if (!m_broker.get_preset_cci_value(bindname).is_string()) {
284 continue;
285 }
286 std::string initname = m_broker.get_preset_cci_value(bindname).get_string();
287
288 if (initname.at(0) != '&') continue; // all port binds are donated with a '&', so not for us
289
290 if (initname.find(';') != std::string::npos) {
291 order_bind(m, initname, targetname);
292 } else {
293 initname.erase(0, 1); // remove leading '&'
294 // Check if it's an absolute path (exists in global hierarchy)
295 if (!is_absolute_path(initname)) {
296 // It's a relative path, prepend the container name
297 initname = std::string(name()) + "." + initname;
298 }
299 SCP_TRACE(())("Bind {} to {}", targetname, initname);
300 do_binding(m, initname, targetname);
301 }
302 }
303 }
304
305private:
306 void populate_cci_children_cache() const
307 {
308 m_cci_children_cache.clear();
309 m_cci_bind_params_cache.clear();
310 const std::string container_prefix = std::string(name()) + ".";
311 for (const auto& value : m_broker.get_unconsumed_preset_values()) {
312 if (value.first.find(container_prefix) != 0) {
313 continue;
314 }
315
316 const std::string relative_name = value.first.substr(container_prefix.size());
317 const size_t module_end = relative_name.find('.');
318 if (module_end == std::string::npos) {
319 continue;
320 }
321 const size_t child_end = relative_name.find('.', module_end + 1);
322 const std::string module_name = container_prefix + relative_name.substr(0, module_end);
323 const std::string child_name = relative_name.substr(module_end + 1, child_end - module_end - 1);
324 m_cci_children_cache[module_name].insert(child_name);
325 if (relative_name.size() > 5 && relative_name.compare(relative_name.size() - 5, 5, ".bind") == 0) {
326 m_cci_bind_params_cache[module_name].emplace_back(value);
327 }
328 }
329 m_cci_children_cache_initialized = true;
330 }
331
332 const std::set<std::string>& get_cci_children(const std::string& object_name) const
333 {
334 if (!m_cci_children_cache_initialized) {
335 populate_cci_children_cache();
336 }
337
338 static const std::set<std::string> no_children;
339 const auto cache_it = m_cci_children_cache.find(object_name);
340 return cache_it == m_cci_children_cache.end() ? no_children : cache_it->second;
341 }
342
343 const std::vector<std::pair<std::string, cci::cci_value>>& get_cci_bind_params(const std::string& object_name) const
344 {
345 if (!m_cci_children_cache_initialized) {
346 populate_cci_children_cache();
347 }
348
349 static const std::vector<std::pair<std::string, cci::cci_value>> no_bind_params;
350 const auto cache_it = m_cci_bind_params_cache.find(object_name);
351 return cache_it == m_cci_bind_params_cache.end() ? no_bind_params : cache_it->second;
352 }
353
354public:
360 template <unsigned int BIND_BUSWIDTH>
361 bool try_bind_all(sc_core::sc_object* i_obj, sc_core::sc_object* t_obj)
362 {
363 if ((try_bind<tlm_utils::multi_init_base<BIND_BUSWIDTH>, tlm::tlm_base_target_socket<BIND_BUSWIDTH>>(i_obj,
364 t_obj)) ||
365 (try_bind<tlm_utils::multi_init_base<BIND_BUSWIDTH>, tlm_utils::multi_target_base<BIND_BUSWIDTH>>(i_obj,
366 t_obj)) ||
367 (try_bind<tlm::tlm_base_initiator_socket<BIND_BUSWIDTH>, tlm::tlm_base_target_socket<BIND_BUSWIDTH>>(
368 i_obj, t_obj)) ||
369 (try_bind<tlm::tlm_base_initiator_socket<BIND_BUSWIDTH>, tlm_utils::multi_target_base<BIND_BUSWIDTH>>(
370 i_obj, t_obj)) ||
371 (try_bind<tlm::tlm_base_initiator_socket<BIND_BUSWIDTH, tlm::tlm_fw_transport_if<>,
372 tlm::tlm_bw_transport_if<>, 1, sc_core::SC_ZERO_OR_MORE_BOUND>,
373 tlm_utils::multi_target_base<BIND_BUSWIDTH>>(i_obj, t_obj)) ||
374 (try_bind<gs::biflow_bindable, gs::biflow_bindable>(i_obj, t_obj)) ||
375 (try_bind<tlm::tlm_base_initiator_socket<BIND_BUSWIDTH, tlm::tlm_fw_transport_if<>,
376 tlm::tlm_bw_transport_if<>, 1, sc_core::SC_ZERO_OR_MORE_BOUND>,
377 tlm::tlm_base_target_socket<BIND_BUSWIDTH>>(i_obj, t_obj)) ||
378 (try_bind<
379 tlm::tlm_initiator_socket<BIND_BUSWIDTH, tlm::tlm_base_protocol_types, 1,
380 sc_core::SC_ONE_OR_MORE_BOUND>,
381 tlm::tlm_target_socket<BIND_BUSWIDTH, tlm::tlm_base_protocol_types, 1, sc_core::SC_ZERO_OR_MORE_BOUND>>(
382 i_obj, t_obj)) ||
383 (try_bind<
384 tlm_utils::multi_init_base<BIND_BUSWIDTH>,
385 tlm::tlm_target_socket<BIND_BUSWIDTH, tlm::tlm_base_protocol_types, 1, sc_core::SC_ZERO_OR_MORE_BOUND>>(
386 i_obj, t_obj)) ||
387 (try_bind<
388 tlm::tlm_base_initiator_socket<BIND_BUSWIDTH, tlm::tlm_fw_transport_if<>, tlm::tlm_bw_transport_if<>, 1,
389 sc_core::SC_ZERO_OR_MORE_BOUND>,
390 tlm::tlm_target_socket<BIND_BUSWIDTH, tlm::tlm_base_protocol_types, 1, sc_core::SC_ZERO_OR_MORE_BOUND>>(
391 i_obj, t_obj)) ||
392 (try_bind<sc_core::sc_port<sc_core::sc_signal_inout_if<bool>, 0, sc_core::SC_ZERO_OR_MORE_BOUND>,
393 TargetSignalSocket<bool>>(i_obj, t_obj)) ||
394 (try_bind<InitiatorSignalSocket<bool>, TargetSignalSocket<bool>>(i_obj, t_obj)) ||
395 (try_bind<InitiatorSignalSocket<uint32_t>, TargetSignalSocket<uint32_t>>(i_obj, t_obj))
396#ifndef WITHOUT_QEMU
397 ||
398 (try_bind<QemuInitiatorSocket<BIND_BUSWIDTH>, tlm::tlm_base_target_socket<BIND_BUSWIDTH>>(i_obj, t_obj)) ||
399 (try_bind<QemuInitiatorSocket<BIND_BUSWIDTH>, tlm_utils::multi_target_base<BIND_BUSWIDTH>>(i_obj, t_obj)) ||
400 (try_bind<tlm_utils::multi_init_base<BIND_BUSWIDTH>, QemuTargetSocket<BIND_BUSWIDTH>>(i_obj, t_obj)) ||
401 (try_bind<tlm::tlm_base_initiator_socket<BIND_BUSWIDTH>, QemuTargetSocket<BIND_BUSWIDTH>>(i_obj, t_obj)) ||
403 (try_bind<QemuInitiatorSignalSocket, QemuTargetSignalSocket>(i_obj, t_obj)) ||
404 (try_bind<QemuInitiatorSignalSocket, TargetSignalSocket<bool>>(i_obj, t_obj)) ||
405 (try_bind<InitiatorSignalSocket<bool>, QemuTargetSignalSocket>(i_obj, t_obj))
406#endif
407 ) {
408 return true;
409 } else {
410 return false;
411 }
412 }
413
414private:
415 bool is_container_config(const std::string& config_name)
416 {
417 if (m_broker.has_preset_value(config_name + ".dont_construct")) {
418 return dynamic_cast<container_module*>(sc_core::sc_find_object(config_name.c_str())) != nullptr;
419 }
420
421 const cci::cci_value module_type = m_broker.get_preset_cci_value(config_name + ".moduletype");
422 if (!module_type.is_string()) {
423 return false;
424 }
425
426 const std::string factory_name = CCI_GS_MF_NAME + std::string(module_type.get_string());
427 // Dynamic factories must be registered before planning nested dependencies.
428 if (!m_broker.get_param_handle(factory_name).is_valid()) {
429 register_module_config_from_dylib(config_name);
430 }
431 cci::cci_param_typed_handle<gs::cci_constructor_vl> factory(m_broker.get_param_handle(factory_name));
432 return factory.is_valid() && (*factory).is_container();
433 }
434
435public:
442 bool is_container_type(const std::string& name)
443 {
444 // Check cache first
445 auto cache_it = m_is_container_type_cache.find(name);
446 if (cache_it != m_is_container_type_cache.end()) {
447 return cache_it->second;
448 }
449
450 const bool result = is_container_config(std::string(sc_module::name()) + "." + name);
451
452 // Cache the result
453 m_is_container_type_cache[name] = result;
454 return result;
455 }
456
466 void get_nested_dependencies(const std::string& container_name, std::set<std::string>& dependencies)
467 {
468 auto cache_it = m_per_pass_nested_deps_cache.find(container_name);
469 if (cache_it != m_per_pass_nested_deps_cache.end()) {
470 dependencies.insert(cache_it->second.begin(), cache_it->second.end());
471 return;
472 }
473
474 std::string module_name = std::string(sc_module::name());
475 std::string container_full_name = module_name + "." + container_name;
476 std::set<std::string> computed_deps;
477
478 std::vector<std::pair<std::string, cci::cci_value>> all_params;
479 auto params_cache_it = m_moduletype_params_cache.find(container_full_name);
480 if (params_cache_it != m_moduletype_params_cache.end()) {
481 all_params = params_cache_it->second;
482 } else {
483 auto params_range = m_broker.get_unconsumed_preset_values(
484 [&container_full_name](const std::pair<std::string, cci::cci_value>& iv) {
485 return iv.first.find(container_full_name + ".") == 0 &&
486 iv.first.find(".moduletype") != std::string::npos &&
487 iv.first.find(".moduletype") == iv.first.length() - 11;
488 });
489 for (const auto& param : params_range) {
490 all_params.push_back(param);
491 }
492 m_moduletype_params_cache[container_full_name] = all_params;
493 }
494
495 std::set<std::string> child_modules;
496 bool config_prefix_used = false;
497 std::string config_prefix = container_full_name + ".config.";
498
499 for (const auto& param : all_params) {
500 std::string param_name = param.first;
501 if (param_name == container_full_name + ".moduletype") continue;
502
503 if (param_name.find(config_prefix) == 0 && param_name.find(".moduletype") == param_name.length() - 11) {
504 std::string child_path = param_name.substr(config_prefix.length());
505 child_path = child_path.substr(0, child_path.length() - 11);
506 size_t dot_pos = child_path.find('.');
507 if (dot_pos != std::string::npos) {
508 child_path = child_path.substr(0, dot_pos);
509 }
510 child_modules.insert(child_path);
511 config_prefix_used = true;
512 } else if (param_name.length() > container_full_name.length() + 12) {
513 std::string child_path = param_name.substr(container_full_name.length() + 1);
514 if (child_path.find("config.") == 0) continue;
515
516 child_path = child_path.substr(0, child_path.length() - 11);
517 size_t dot_pos = child_path.find('.');
518 if (dot_pos != std::string::npos) {
519 child_path = child_path.substr(0, dot_pos);
520 }
521 child_modules.insert(child_path);
522 }
523 }
524
525 for (const auto& child : child_modules) {
526 std::string child_config_name;
527 if (config_prefix_used) {
528 child_config_name = config_prefix + child;
529 } else {
530 child_config_name = container_full_name + "." + child;
531 }
532
533 cci::cci_value_list child_args;
534 auto args_cache_it = m_module_args_cache.find(child_config_name);
535 if (args_cache_it != m_module_args_cache.end()) {
536 child_args = args_cache_it->second;
537 } else {
538 for (int i = 0; m_broker.has_preset_value(child_config_name + ".args." + std::to_string(i)); i++) {
539 child_args.push_back(
540 m_broker.get_preset_cci_value(child_config_name + ".args." + std::to_string(i)));
541 }
542 m_module_args_cache[child_config_name] = child_args;
543 }
544
545 for (auto arg : child_args) {
546 if (arg.is_string()) {
547 std::string a = arg.get_string();
548 if (a.length() > 0 && a[0] == '&') {
549 std::string ref_path = a.substr(1);
550
551 if (ref_path.find(module_name) != 0) {
552 ref_path = container_full_name + "." + ref_path;
553 }
554
555 if (!(ref_path.length() >= container_full_name.size() &&
556 ref_path.substr(0, container_full_name.size()) == container_full_name &&
557 (ref_path.length() == container_full_name.size() ||
558 ref_path[container_full_name.size()] == '.'))) {
559 computed_deps.insert(ref_path);
560 }
561 }
562 }
563 }
564
565 // Check bind parameters only if this child is a container type
566 bool child_is_container = false;
567 auto container_cache_it = m_is_container_type_cache.find(child_config_name);
568 if (container_cache_it != m_is_container_type_cache.end()) {
569 child_is_container = container_cache_it->second;
570 } else {
571 child_is_container = is_container_config(child_config_name);
572 m_is_container_type_cache[child_config_name] = child_is_container;
573 }
574
575 if (child_is_container) {
576 // Get bind parameters for this container child
577 auto child_params = m_broker.get_unconsumed_preset_values(
578 [&child_config_name](const std::pair<std::string, cci::cci_value>& iv) {
579 return iv.first.find(child_config_name + ".") == 0 &&
580 iv.first.find(".bind") != std::string::npos;
581 });
582
583 for (const auto& param : child_params) {
584 if (param.second.is_string()) {
585 std::string bind_value = param.second.get_string();
586 // Handle semicolon-separated bind strings
587 size_t pos = 0;
588 while (pos < bind_value.length()) {
589 size_t end = bind_value.find(';', pos);
590 if (end == std::string::npos) end = bind_value.length();
591
592 std::string single_bind = bind_value.substr(pos, end - pos);
593 // Trim whitespace
594 size_t start = single_bind.find_first_not_of(" \t\n\r");
595 if (start != std::string::npos) {
596 size_t finish = single_bind.find_last_not_of(" \t\n\r");
597 single_bind = single_bind.substr(start, finish - start + 1);
598 }
599
600 if (single_bind.length() > 0 && single_bind[0] == '&') {
601 std::string ref_path = single_bind.substr(1);
602
603 // Make absolute if relative
604 if (ref_path.find(module_name) != 0) {
605 ref_path = container_full_name + "." + ref_path;
606 }
607
608 // Only add if it's external to this container
609 if (!(ref_path.length() >= container_full_name.size() &&
610 ref_path.substr(0, container_full_name.size()) == container_full_name &&
611 (ref_path.length() == container_full_name.size() ||
612 ref_path[container_full_name.size()] == '.'))) {
613 computed_deps.insert(ref_path);
614 }
615 }
616
617 pos = (end == bind_value.length()) ? end : end + 1;
618 }
619 }
620 }
621 }
622 }
623
624 // Check the container's own bind parameters (outside config table)
625 auto container_params = m_broker.get_unconsumed_preset_values(
626 [&container_full_name](const std::pair<std::string, cci::cci_value>& iv) {
627 std::string param_name = iv.first;
628 // Match pattern: container_full_name.<param>.bind but not container_full_name.config.*
629 return param_name.find(container_full_name + ".") == 0 &&
630 param_name.find(".config.") == std::string::npos &&
631 param_name.find(".bind") != std::string::npos;
632 });
633
634 for (const auto& param : container_params) {
635 if (param.second.is_string()) {
636 std::string bind_value = param.second.get_string();
637 // Handle semicolon-separated bind strings
638 size_t pos = 0;
639 while (pos < bind_value.length()) {
640 size_t end = bind_value.find(';', pos);
641 if (end == std::string::npos) end = bind_value.length();
642
643 std::string single_bind = bind_value.substr(pos, end - pos);
644 // Trim whitespace
645 size_t start = single_bind.find_first_not_of(" \t\n\r");
646 if (start != std::string::npos) {
647 size_t finish = single_bind.find_last_not_of(" \t\n\r");
648 single_bind = single_bind.substr(start, finish - start + 1);
649 }
650
651 if (single_bind.length() > 0 && single_bind[0] == '&') {
652 std::string ref_path = single_bind.substr(1);
653
654 // Make absolute if relative
655 if (ref_path.find(module_name) != 0) {
656 ref_path = container_full_name + "." + ref_path;
657 }
658
659 // Only add if it's external to this container
660 if (!(ref_path.length() >= container_full_name.size() &&
661 ref_path.substr(0, container_full_name.size()) == container_full_name &&
662 (ref_path.length() == container_full_name.size() ||
663 ref_path[container_full_name.size()] == '.'))) {
664 computed_deps.insert(ref_path);
665 }
666 }
667
668 pos = (end == bind_value.length()) ? end : end + 1;
669 }
670 }
671 }
672
673 m_per_pass_nested_deps_cache[container_name] = computed_deps;
674 dependencies.insert(computed_deps.begin(), computed_deps.end());
675 }
676
677 bool are_dependencies_available(const std::string& name, const std::set<std::string>& done)
678 {
679 std::string module_name = std::string(sc_module::name());
680 std::set<std::string> all_dependencies;
681
682 cci::cci_value_list mod_args = get_module_args(module_name + "." + name);
683 for (auto arg : mod_args) {
684 if (arg.is_string()) {
685 std::string a = arg.get_string();
686 if (a.length() > 0 && a[0] == '&') {
687 std::string ref_path = a.substr(1);
688 if (!ref_path.empty()) {
689 all_dependencies.insert(ref_path);
690 }
691 }
692 }
693 }
694
695 if (is_container_type(name)) {
696 get_nested_dependencies(name, all_dependencies);
697 }
698
699 for (const auto& ref_path : all_dependencies) {
700 if (ref_path.substr(0, module_name.size()) == module_name && ref_path.length() > module_name.size() &&
701 ref_path[module_name.size()] == '.') {
702 std::string referenced_module = ref_path.substr(module_name.size() + 1);
703 size_t dot_pos = referenced_module.find('.');
704 if (dot_pos != std::string::npos) {
705 referenced_module = referenced_module.substr(0, dot_pos);
706 }
707 if (done.count(referenced_module) == 0) {
708 return false;
709 }
710 } else {
711 sc_core::sc_object* dep_obj = find_sc_obj(nullptr, ref_path, true);
712 if (dep_obj == nullptr) {
713 return false;
714 }
715 }
716 }
717 return true;
718 }
719
720 // Find modules with bind parameters pointing to target_module
721 std::set<std::string> get_bind_references_to_module(const std::string& module_name,
722 const std::string& target_module_name,
723 const std::list<std::string>& all_modules)
724 {
725 // Check cache first
726 auto& module_cache = m_bind_refs_cache[module_name];
727 auto cache_it = module_cache.find(target_module_name);
728 if (cache_it != module_cache.end()) {
729 return cache_it->second;
730 }
731
732 std::set<std::string> modules_that_reference_target;
733 std::string target_full_path = module_name + "." + target_module_name;
734
735 for (const auto& mod : all_modules) {
736 if (mod == target_module_name) continue;
737
738 std::string mod_full_path = module_name + "." + mod;
739
740 // Query broker for this module's parameters
741 auto mod_params = m_broker.get_unconsumed_preset_values(
742 [&mod_full_path](const std::pair<std::string, cci::cci_value>& iv) {
743 return iv.first.find(mod_full_path + ".") == 0;
744 });
745
746 for (const auto& param : mod_params) {
747 const std::string& param_name = param.first;
748 const cci::cci_value& param_value = param.second;
749
750 if (param_name.length() > 5 && param_name.substr(param_name.length() - 5) == ".bind") {
751 if (param_value.is_string()) {
752 std::string bind_target = param_value.get_string();
753 if (bind_target.length() > 0 && bind_target[0] == '&') {
754 std::string ref_path = bind_target.substr(1);
755 if (ref_path == target_full_path ||
756 (ref_path.length() > target_full_path.length() &&
757 ref_path.substr(0, target_full_path.length() + 1) == target_full_path + ".")) {
758 modules_that_reference_target.insert(mod);
759 break;
760 }
761 }
762 }
763 }
764 }
765 }
766
767 // Cache the result
768 module_cache[target_module_name] = modules_that_reference_target;
769 return modules_that_reference_target;
770 }
771
772 int get_construction_priority(const std::string& name)
773 {
774 std::string module_name = std::string(sc_module::name());
775 std::string priority_param = module_name + "." + name + ".construction_priority";
776
777 if (m_broker.has_preset_value(priority_param)) {
778 cci::cci_value cci_priority = m_broker.get_preset_cci_value(priority_param);
779 if (cci_priority.is_int()) {
780 return cci_priority.get_int();
781 } else if (cci_priority.is_int64()) {
782 return static_cast<int>(cci_priority.get_int64());
783 }
784 }
785 return 0; // Default priority
786 }
787
788 std::list<std::string> PriorityConstruct(void)
789 {
790 std::list<std::string> args;
791 std::set<std::string> done;
792 std::list<std::string> all_modules;
793 std::string module_name = std::string(sc_module::name());
794
795 all_modules = gs::sc_cci_children(name());
796
797 for (auto it = all_modules.begin(); it != all_modules.end();) {
798 std::string mod_type_name = module_name + "." + *it + ".moduletype";
799 if (!m_broker.has_preset_value(mod_type_name)) {
800 it = all_modules.erase(it);
801 } else {
802 ++it;
803 }
804 }
805
806 std::map<int, std::list<std::string>> priority_groups;
807 for (const auto& mod : all_modules) {
808 int priority = get_construction_priority(mod);
809 priority_groups[priority].push_back(mod);
810 }
811
812 for (auto& priority_pair : priority_groups) {
813 std::list<std::string> todo = priority_pair.second;
814 int max_passes = todo.size() + 1;
815 int pass = 0;
816
817 bool enforce_bind_deps = true;
818 while (todo.size() > 0 && pass < max_passes) {
819 pass++;
820 int constructed_this_pass = 0;
821
822 m_per_pass_nested_deps_cache.clear();
823
824 std::map<std::string, std::set<std::string>> depends_on_me;
825 for (const auto& source_mod : todo) {
826 if (is_container_type(source_mod)) {
827 continue;
828 }
829
830 const std::string source_full_path = module_name + "." + source_mod;
831 const cci::cci_value source_type = m_broker.get_preset_cci_value(source_full_path + ".moduletype");
832 if (!source_type.is_string()) {
833 continue;
834 }
835
836 const std::string source_modtype = source_type.get_string();
837 if (source_modtype.find("router") == std::string::npos &&
838 source_modtype.find("Router") == std::string::npos) {
839 continue;
840 }
841
842 for (const auto& param : get_cci_bind_params(source_full_path)) {
843 const cci::cci_value& bind = param.second;
844 if (!bind.is_string()) {
845 continue;
846 }
847
848 std::string target = bind.get_string();
849 if (target.empty() || target.front() != '&') {
850 continue;
851 }
852
853 target.erase(0, 1);
854 const std::string module_prefix = module_name + ".";
855 if (target.find(module_prefix) == 0) {
856 target.erase(0, module_prefix.size());
857 }
858 const size_t target_end = target.find('.');
859 if (target_end != std::string::npos) {
860 target.erase(target_end);
861 }
862
863 if (std::find(todo.begin(), todo.end(), target) != todo.end()) {
864 depends_on_me[target].insert(source_mod);
865 }
866 }
867 }
868
869 for (const auto& mod : todo) {
870 std::set<std::string> all_deps;
871 cci::cci_value_list mod_args = get_module_args(module_name + "." + mod);
872 for (auto arg : mod_args) {
873 if (arg.is_string()) {
874 std::string a = arg.get_string();
875 if (a.length() > 0 && a[0] == '&') {
876 std::string ref_path = a.substr(1);
877 if (!ref_path.empty()) {
878 all_deps.insert(ref_path);
879 }
880 }
881 }
882 }
883
884 if (is_container_type(mod)) {
885 get_nested_dependencies(mod, all_deps);
886 }
887
888 for (const auto& ref_path : all_deps) {
889 if (ref_path.substr(0, module_name.size()) == module_name &&
890 ref_path.length() > module_name.size() && ref_path[module_name.size()] == '.') {
891 std::string sibling_name = ref_path.substr(module_name.size() + 1);
892 size_t dot_pos = sibling_name.find('.');
893 if (dot_pos != std::string::npos) {
894 sibling_name = sibling_name.substr(0, dot_pos);
895 }
896 if (std::find(todo.begin(), todo.end(), sibling_name) != todo.end()) {
897 depends_on_me[sibling_name].insert(mod);
898 }
899 }
900 }
901 }
902
903 for (auto it = todo.begin(); it != todo.end();) {
904 std::string name = *it;
905
906 bool deps_available = are_dependencies_available(name, done);
907
908 bool bind_deps_ready = true;
909 if (deps_available && enforce_bind_deps) {
910 for (const auto& dep_pair : depends_on_me) {
911 const std::string& required_module = dep_pair.first;
912 const std::set<std::string>& modules_depending_on_it = dep_pair.second;
913
914 if (modules_depending_on_it.count(name) > 0) {
915 if (done.count(required_module) == 0) {
916 bind_deps_ready = false;
917 break;
918 }
919 }
920 }
921 }
922
923 if (deps_available && bind_deps_ready) {
924 done.insert(name);
925 it = todo.erase(it);
926 constructed_this_pass++;
927 args.push_back(name);
928 } else {
929 ++it;
930 }
931 }
932
933 if (constructed_this_pass == 0 && todo.size() > 0) {
934 if (enforce_bind_deps) {
935 enforce_bind_deps = false;
936 pass--;
937 continue;
938 }
939 SCP_WARN(())("No progress in dependency resolution, {} modules remaining", todo.size());
940 for (auto t : todo) {
941 SCP_WARN(())("Module with unresolved dependencies: {}", t);
942 std::set<std::string> all_deps;
943 cci::cci_value_list mod_args = get_module_args(module_name + "." + t);
944 for (auto arg : mod_args) {
945 if (arg.is_string()) {
946 std::string a = arg.get_string();
947 if (a.length() > 0 && a[0] == '&') {
948 std::string ref_path = a.substr(1);
949 if (!ref_path.empty()) {
950 all_deps.insert(ref_path);
951 }
952 }
953 }
954 }
955 if (is_container_type(t)) {
956 get_nested_dependencies(t, all_deps);
957 }
958 for (const auto& ref_path : all_deps) {
959 sc_core::sc_object* dep_obj = find_sc_obj(nullptr, ref_path, true);
960 if (dep_obj == nullptr) {
961 SCP_WARN(())(" Missing dependency: {}", ref_path);
962 }
963 }
964 }
965 SCP_FATAL(())("Unresolvable module dependencies");
966 }
967 }
968 }
969
970 return args;
971 }
972
973 void register_module_from_dylib(sc_core::sc_module_name name)
974 {
975 register_module_config_from_dylib(std::string(sc_module::name()) + "." + std::string(name));
976 }
977
978private:
979 void register_module_config_from_dylib(const std::string& config_name)
980 {
981 std::string libname;
982 std::vector<std::string> lib_types = { ".moduletype", ".dylib_path" };
983
984 for (const auto& type : lib_types) {
985 const std::string base_name = config_name + type;
986 if (m_broker.has_preset_value(base_name)) {
987 libname = std::string(m_broker.get_preset_cci_value(base_name).get_string()) + "." +
988 std::string(m_library_loader.get_lib_ext());
989 }
990 }
991
992 qemu::LibraryLoaderIface::LibraryIfacePtr libraryHandle = m_library_loader.load_library(libname);
993 if (!libraryHandle) {
994 SCP_FATAL(())("Failed to load library {}: {}", libname, m_library_loader.get_last_error());
995 }
996
997 m_dls.insert(libraryHandle);
998
999 void (*module_register)() = nullptr;
1000 if (libraryHandle->symbol_exists("module_register")) {
1001 module_register = reinterpret_cast<void (*)()>(libraryHandle->get_symbol("module_register"));
1002 module_register();
1003 } else {
1004 SCP_WARN(()) << "module_register not found in library " << libname;
1005 }
1006 }
1007
1008public:
1009 void ModulesConstruct(void)
1010 {
1011 // Snapshot the configuration used for construction planning.
1012 populate_cci_children_cache();
1013 for (auto name : PriorityConstruct()) {
1014 auto mod_type_name = std::string(sc_module::name()) + "." + name + ".moduletype";
1015 if (m_broker.has_preset_value(mod_type_name)) {
1016 if (m_broker.has_preset_value(std::string(sc_module::name()) + "." + name + ".dont_construct")) {
1017 sc_core::sc_object* no_construct_mod_obj = nullptr;
1018 std::string no_construct_mod_name = std::string(sc_module::name()) + "." + name;
1019 no_construct_mod_obj = gs::find_sc_obj(nullptr, no_construct_mod_name);
1020 auto no_construct_mod = dynamic_cast<sc_core::sc_module*>(no_construct_mod_obj);
1021 if (!no_construct_mod) {
1022 SCP_FATAL(()) << "The object " << std::string(sc_module::name()) + "." + name
1023 << " is not yet constructed";
1024 }
1025 m_allModules.push_back(no_construct_mod);
1026 } else {
1027 gs::cci_clear_unused(m_broker, mod_type_name);
1028 cci::cci_value cci_type = gs::cci_get(m_broker, mod_type_name);
1029 if (cci_type.is_string()) {
1030 std::string mod_type = cci_type.get_string();
1031 cci::cci_value_list mod_args = get_module_args(std::string(sc_module::name()) + "." + name);
1032 sc_core::sc_module* m = construct_module(mod_type, name.c_str(), mod_args);
1033 if (!m) {
1034 SC_REPORT_ERROR(
1035 "ModuleFactory",
1036 ("Can't automatically handle argument list for module: " + mod_type).c_str());
1037 }
1038
1039 m_allModules.push_back(m);
1040 if (!m_local_pass) {
1041 m_local_pass = dynamic_cast<transaction_forwarder_if<PASS>*>(m);
1042 }
1043 } else {
1044 SCP_WARN(()) << "moduletype parameter is not a string for " << name;
1045 }
1046 }
1047 }
1048 }
1049
1050 // Child constructors can create bindings and hide redirected aliases.
1051 populate_cci_children_cache();
1052 for (auto mod : m_allModules) {
1053 name_bind(mod);
1054 }
1055 SCP_DEBUG(())("ModulesConstruct complete");
1056 }
1057
1058 using tlm_initiator_socket_type = tlm_utils::simple_initiator_socket_b<
1059 ContainerBase, DEFAULT_TLM_BUSWIDTH, tlm::tlm_base_protocol_types, sc_core::SC_ZERO_OR_MORE_BOUND>;
1060 using tlm_target_socket_type = tlm_utils::simple_target_socket_tagged_b<
1061 ContainerBase, DEFAULT_TLM_BUSWIDTH, tlm::tlm_base_protocol_types, sc_core::SC_ZERO_OR_MORE_BOUND>;
1062
1063private:
1064 bool m_defer_modules_construct;
1065 std::list<sc_core::sc_module*> m_allModules;
1066 std::list<std::shared_ptr<sc_core::sc_module>> m_constructedModules;
1067
1068 std::set<qemu::LibraryLoaderIface::LibraryIfacePtr> m_dls;
1069 qemu::LibraryLoaderIface& m_library_loader = qemu::get_default_lib_loader();
1070
1071public:
1072 cci::cci_broker_handle m_broker;
1073 cci::cci_param<std::string> moduletype; // for consistency
1074 cci::cci_param<uint32_t> p_tlm_initiator_ports_num;
1075 cci::cci_param<uint32_t> p_tlm_target_ports_num;
1076 cci::cci_param<uint32_t> p_initiator_signals_num;
1077 cci::cci_param<uint32_t> p_target_signals_num;
1078 sc_core::sc_vector<tlm_initiator_socket_type> initiator_sockets;
1079 sc_core::sc_vector<tlm_target_socket_type> target_sockets;
1080 sc_core::sc_vector<InitiatorSignalSocket<bool>> initiator_signal_sockets;
1081 sc_core::sc_vector<TargetSignalSocket<bool>> target_signal_sockets;
1082 TargetSignalSocket<bool> container_self_reset;
1083 transaction_forwarder_if<PASS>* m_local_pass;
1084
1085 std::vector<cci::cci_param<gs::cci_constructor_vl>*> registered_mods;
1086 sc_core::sc_object* container_mod_arg;
1087
1088 virtual ~ContainerBase()
1089 {
1090 m_constructedModules.reverse();
1091 m_constructedModules.clear();
1092 }
1093
1094 std::shared_ptr<sc_core::sc_module> find_module_by_name(const std::string& mod_name)
1095 {
1096 auto ret = std::find_if(m_constructedModules.begin(), m_constructedModules.end(),
1097 [&](auto mod) { return (mod && std::string(mod->name()) == mod_name); });
1098 if (ret == m_constructedModules.end()) {
1099 return nullptr;
1100 }
1101 return *ret;
1102 }
1103
1104 template <typename T>
1105 std::shared_ptr<sc_core::sc_module> find_module_by_cci_param(const std::string& cci_param_name, const T& cmp_value)
1106 {
1107 T ret_val;
1108 auto ret = std::find_if(m_constructedModules.begin(), m_constructedModules.end(), [&](auto mod) {
1109 return (mod && m_broker.has_preset_value(std::string(mod->name()) + "." + cci_param_name) &&
1110 m_broker.get_preset_cci_value(std::string(mod->name()) + "." + cci_param_name)
1111 .template try_get<T>(ret_val) &&
1112 (ret_val == cmp_value));
1113 });
1114 if (ret == m_constructedModules.end()) {
1115 return nullptr;
1116 }
1117 return *ret;
1118 }
1119
1125
1126 ContainerBase(const sc_core::sc_module_name _n, bool defer_modules_construct,
1127 sc_core::sc_object* p_container_mod_arg = nullptr)
1128 : m_defer_modules_construct(defer_modules_construct)
1129 , m_broker(cci::cci_get_broker())
1130 , moduletype("moduletype", "", "Module type for the TLM container, must be \"Container\"")
1131 , p_tlm_initiator_ports_num("tlm_initiator_ports_num", 0, "number of tlm initiator ports")
1132 , p_tlm_target_ports_num("tlm_target_ports_num", 0, "number of tlm target ports")
1133 , p_initiator_signals_num("initiator_signals_num", 0, "number of initiator signals")
1134 , p_target_signals_num("target_signals_num", 0, "number of target signals")
1135 , initiator_sockets("initiator_socket")
1136 , target_sockets("target_socket")
1137 , initiator_signal_sockets("initiator_signal_socket")
1138 , target_signal_sockets("target_signal_socket")
1139 , container_self_reset("container_self_reset")
1140 , m_local_pass(nullptr)
1141 , container_mod_arg(p_container_mod_arg)
1142 {
1143 SCP_DEBUG(()) << "ContainerBase Constructor";
1144
1145 initiator_sockets.init(p_tlm_initiator_ports_num.get_value(),
1146 [this](const char* n, int i) { return new tlm_initiator_socket_type(n); });
1147 target_sockets.init(p_tlm_target_ports_num.get_value(),
1148 [this](const char* n, int i) { return new tlm_target_socket_type(n); });
1149 initiator_signal_sockets.init(p_initiator_signals_num.get_value(),
1150 [this](const char* n, int i) { return new InitiatorSignalSocket<bool>(n); });
1151 target_signal_sockets.init(p_target_signals_num.get_value(),
1152 [this](const char* n, int i) { return new TargetSignalSocket<bool>(n); });
1153
1154 for (int i = 0; i < p_tlm_target_ports_num.get_value(); i++) {
1155 target_sockets[i].register_b_transport(this, &ContainerBase::b_transport, i);
1156 target_sockets[i].register_transport_dbg(this, &ContainerBase::transport_dbg, i);
1157 target_sockets[i].register_get_direct_mem_ptr(this, &ContainerBase::get_direct_mem_ptr, i);
1158 }
1159
1160 for (int i = 0; i < p_tlm_initiator_ports_num.get_value(); i++) {
1161 initiator_sockets[i].register_invalidate_direct_mem_ptr(this, &ContainerBase::invalidate_direct_mem_ptr);
1162 }
1163
1164 for (int i = 0; i < p_target_signals_num.get_value(); i++) {
1165 target_signal_sockets[i].register_value_changed_cb([&, i](bool value) {
1166 if (m_local_pass) m_local_pass->fw_handle_signal(i, value);
1167 });
1168 }
1169
1170 container_self_reset.register_value_changed_cb([this](bool value) { do_reset(value); });
1171
1172 auto mods = gs::ModuleFactory::GetAvailableModuleList();
1173
1174 while (mods->size()) {
1175 registered_mods.push_back((mods->back())());
1176 mods->pop_back();
1177 }
1178
1179 if (!m_defer_modules_construct) ModulesConstruct();
1180 }
1181
1182 void fw_b_transport(int id, tlm::tlm_generic_payload& trans, sc_core::sc_time& delay) override
1183 {
1184 SCP_DEBUG(()) << "calling b_transport on initiator_socket_" << id << " " << scp::scp_txn_tostring(trans);
1185 initiator_sockets[id]->b_transport(trans, delay);
1186 SCP_DEBUG(()) << "return from b_transport on initiator_socket_" << id << " " << scp::scp_txn_tostring(trans);
1187 }
1188
1189 unsigned int fw_transport_dbg(int id, tlm::tlm_generic_payload& trans) override
1190 {
1191 SCP_DEBUG(()) << "calling transport_dbg on initiator_socket_" << id << " " << scp::scp_txn_tostring(trans);
1192 unsigned int ret = initiator_sockets[id]->transport_dbg(trans);
1193 SCP_DEBUG(()) << "return from transport_dbg on initiator_socket_" << id << " " << scp::scp_txn_tostring(trans);
1194 return ret;
1195 }
1196
1197 bool fw_get_direct_mem_ptr(int id, tlm::tlm_generic_payload& trans, tlm::tlm_dmi& dmi_data) override
1198 {
1199 SCP_DEBUG(()) << "calling get_direct_mem_ptr on initiator_socket_" << id << " " << scp::scp_txn_tostring(trans);
1200 bool ret = initiator_sockets[id]->get_direct_mem_ptr(trans, dmi_data);
1201 SCP_DEBUG(()) << "return from get_direct_mem_ptr on initiator_socket_" << id << " "
1202 << " RET: " << std::boolalpha << ret << " " << scp::scp_txn_tostring(trans)
1203 << " IS_READ_ALLOWED: " << std::boolalpha << dmi_data.is_read_allowed() << " "
1204 << " IS_WRITE_ALLOWED: " << std::boolalpha << dmi_data.is_write_allowed();
1205 return ret;
1206 }
1207
1208 void fw_invalidate_direct_mem_ptr(sc_dt::uint64 start, sc_dt::uint64 end) override
1209 {
1210 SCP_DEBUG(()) << " " << name() << " invalidate_direct_mem_ptr " << " start address 0x" << std::hex << start
1211 << " end address 0x" << std::hex << end;
1212 for (int i = 0; i < target_sockets.size(); i++) {
1213 target_sockets[i]->invalidate_direct_mem_ptr(start, end);
1214 }
1215 }
1216
1217 void fw_handle_signal(int id, bool value) override
1218 {
1219 SCP_DEBUG(()) << "calling handle_signal on initiator_signal_socket_" << id << " value: " << std::boolalpha
1220 << value;
1221 initiator_signal_sockets[id]->write(value);
1222 }
1223
1224private:
1225 void b_transport(int id, tlm::tlm_generic_payload& trans, sc_core::sc_time& delay)
1226 {
1227 if (m_local_pass) {
1228 m_local_pass->fw_b_transport(id, trans, delay);
1229 }
1230 }
1231
1232 unsigned int transport_dbg(int id, tlm::tlm_generic_payload& trans)
1233 {
1234 if (m_local_pass) {
1235 return m_local_pass->fw_transport_dbg(id, trans);
1236 }
1237 return 0;
1238 }
1239
1240 bool get_direct_mem_ptr(int id, tlm::tlm_generic_payload& trans, tlm::tlm_dmi& dmi_data)
1241 {
1242 if (m_local_pass) {
1243 return m_local_pass->fw_get_direct_mem_ptr(id, trans, dmi_data);
1244 }
1245 return false;
1246 }
1247
1248 void invalidate_direct_mem_ptr(sc_dt::uint64 start, sc_dt::uint64 end)
1249 {
1250 if (m_local_pass) {
1251 m_local_pass->fw_invalidate_direct_mem_ptr(start, end);
1252 }
1253 }
1254
1255 std::vector<std::string> parse_order_str(const std::string& reset_order)
1256 {
1257 std::vector<std::string> ret;
1258 if (reset_order.empty()) {
1259 return ret;
1260 }
1261 std::string reset_order_tr = reset_order;
1262 reset_order_tr.erase(
1263 std::remove_if(reset_order_tr.begin(), reset_order_tr.end(), [](char c) { return std::isspace(c); }),
1264 reset_order_tr.end());
1265 size_t sig_count = std::count_if(reset_order_tr.begin(), reset_order_tr.end(), [](char c) { return c == ';'; });
1266 ret.resize(sig_count + 1);
1267 const std::regex separator(R"(;)");
1268 std::copy(std::sregex_token_iterator(reset_order_tr.begin(), reset_order_tr.end(), separator, -1),
1269 std::sregex_token_iterator(), ret.begin());
1270 return ret;
1271 }
1272
1273 void do_binding(sc_core::sc_module* module_obj, const std::string& initiator_name, const std::string& target_name)
1274 {
1275 sc_core::sc_object* i_obj = nullptr;
1276 sc_core::sc_object* t_obj = nullptr;
1277 /* find the initiator object */
1278
1279 i_obj = find_sc_obj(nullptr, initiator_name);
1280
1281 /* find the target object which must be inside this object */
1282 t_obj = find_sc_obj(module_obj, target_name);
1283
1284 // actually you could probably just cast everything to (tlm::tlm_target_socket<>*), but
1285 // we will dynamic cast to be sure.
1286 if (!try_bind_all<32>(i_obj, t_obj) && !try_bind_all<64>(i_obj, t_obj)) {
1287 SCP_FATAL(())("No bind found for: {} to {}", i_obj->name(), t_obj->name());
1288 }
1289 }
1290
1291 void order_bind(sc_core::sc_module* module_obj, const std::string& target_order_str,
1292 const std::string& initiator_name)
1293 {
1294 if (!target_order_str.empty()) {
1295 std::vector<std::string> target_vec = parse_order_str(target_order_str);
1296 for (const auto& target : target_vec) {
1297 std::string target_name = target;
1298 if (target_name.at(0) != '&') continue;
1299 target_name.erase(0, 1);
1300 if (!is_absolute_path(target_name)) {
1301 target_name = std::string(name()) + "." + target_name;
1302 }
1303 SCP_TRACE(())("Bind {} to {}", target_name, initiator_name);
1304 do_binding(module_obj, target_name, initiator_name);
1305 }
1306 }
1307 }
1308
1309 virtual void do_reset(bool value)
1310 {
1311 if (value) SCP_WARN(()) << "Reset";
1312 }
1313};
1314
1318class Container : public ContainerBase
1319{
1320public:
1321 SCP_LOGGER(());
1322 Container(const sc_core::sc_module_name& n): ContainerBase(n, false, nullptr)
1323 {
1324 SCP_DEBUG(()) << "Container constructor";
1325 std::string moduletype_param = std::string(name()) + ".moduletype";
1326 if (!m_broker.has_preset_value(moduletype_param)) {
1327 m_broker.set_preset_cci_value(moduletype_param, cci::cci_value("Container"));
1328 }
1329 }
1330
1331 virtual ~Container() = default;
1332};
1333
1337class ContainerWithArgs : public ContainerBase
1338{
1339public:
1340 SCP_LOGGER(());
1341 ContainerWithArgs(const sc_core::sc_module_name& n, sc_core::sc_object* p_container_mod_arg)
1342 : ContainerBase(n, false, p_container_mod_arg)
1343 {
1344 SCP_DEBUG(()) << "ContainerWithArgs constructor";
1345 std::string moduletype_param = std::string(name()) + ".moduletype";
1346 if (!m_broker.has_preset_value(moduletype_param)) {
1347 m_broker.set_preset_cci_value(moduletype_param, cci::cci_value("ContainerWithArgs"));
1348 }
1349 }
1350
1351 virtual ~ContainerWithArgs() = default;
1352};
1353
1357class ContainerDeferModulesConstruct : public ContainerBase
1358{
1359public:
1360 SCP_LOGGER(());
1361 ContainerDeferModulesConstruct(const sc_core::sc_module_name& n): ContainerBase(n, true, nullptr)
1362 {
1363 SCP_DEBUG(()) << "ContainerDeferModulesConstruct constructor";
1364 std::string moduletype_param = std::string(name()) + ".moduletype";
1365 if (!m_broker.has_preset_value(moduletype_param)) {
1366 m_broker.set_preset_cci_value(moduletype_param, cci::cci_value("ContainerDeferModulesConstruct"));
1367 }
1368 }
1369
1370 virtual ~ContainerDeferModulesConstruct() = default;
1371};
1372
1373} // namespace ModuleFactory
1374} // namespace gs
1375
1376typedef gs::ModuleFactory::Container Container;
1377typedef gs::ModuleFactory::ContainerDeferModulesConstruct ContainerDeferModulesConstruct;
1378typedef gs::ModuleFactory::ContainerWithArgs ContainerWithArgs;
1379#endif
A QEMU output GPIO exposed as a InitiatorSignalSocket<bool>.
Definition qemu-initiator-signal-socket.h:40
TLM-2.0 initiator socket specialisation for QEMU AddressSpace mapping.
Definition initiator.h:63
A QEMU input GPIO exposed as a TargetSignalSocket<bool>.
Definition qemu-target-signal-socket.h:29
Definition target.h:194
Definition target-signal-socket.h:163
bool is_absolute_path(const std::string &name)
Check if a name refers to an absolute path (outside this container).
Definition module_factory_container.h:129
sc_core::sc_module * construct_module(std::string moduletype, sc_core::sc_module_name name)
Helper to construct a module that takes no arguments.
Definition module_factory_container.h:112
void get_nested_dependencies(const std::string &container_name, std::set< std::string > &dependencies)
Get all EXTERNAL nested dependencies from a container's children (excludes dependencies that are inte...
Definition module_factory_container.h:466
void name_bind(sc_core::sc_module *m)
Bind all known port types.
Definition module_factory_container.h:265
sc_core::sc_module * construct_module(std::string moduletype, sc_core::sc_module_name name, cci::cci_value_list args)
Definition module_factory_container.h:82
ContainerBase(const sc_core::sc_module_name _n, bool defer_modules_construct, sc_core::sc_object *p_container_mod_arg=nullptr)
construct a Container, and all modules within it, and perform binding
Definition module_factory_container.h:1126
cci::cci_value_list get_module_args(std::string modulename)
Get the module args helper to find argument list for a module constructor.
Definition module_factory_container.h:191
bool is_container_type(const std::string &name)
Check if a module is a container type.
Definition module_factory_container.h:442
bool try_bind_all(sc_core::sc_object *i_obj, sc_core::sc_object *t_obj)
Definition module_factory_container.h:361
Definition module_factory_container.h:1358
Definition module_factory_container.h:1338
Definition module_factory_container.h:1319
sc_module constructor container for CCI This uses the CCI Function container, and the FactoryMaker in...
Definition cciutils.h:69
Definition cciutils.h:36
Definition transaction_forwarder_if.h:23
Tool which reads a Lua configuration file and sets parameters.
Definition biflow.cc:10
std::list< std::string > sc_cci_children(sc_core::sc_module_name name)
return a list of 'unconsumed' children from the given module name, can be used inside or outside the ...
Definition cciutils.cc:63
sc_core::sc_object * find_sc_obj(sc_core::sc_object *m, std::string name, bool test=false)
Helper function to find a sc_object by fully qualified name throws SC_ERROR if nothing found.
Definition cciutils.cc:18