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>>>
70 mutable std::map<std::string, std::vector<std::pair<std::string, cci::cci_value>>>
71 m_moduletype_params_cache;
72 mutable std::map<std::string, std::set<std::string>>
73 m_per_pass_nested_deps_cache;
74 mutable std::map<std::string, bool> m_is_container_type_cache;
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;
82 sc_core::sc_module*
construct_module(std::string moduletype, sc_core::sc_module_name name, cci::cci_value_list args)
85 cci::cci_value v = cci::cci_value::from_json(
"\"" + moduletype +
"\"");
87 register_module_from_dylib(name);
95 sc_core::sc_module* mod = (m_fac)(name, args);
97 m_constructedModules.emplace_back(mod);
100 SC_REPORT_ERROR(
"ModuleFactory", (
"Null module type: " + moduletype).c_str());
103 SC_REPORT_ERROR(
"ModuleFactory", (
"Can't find module type: " + moduletype).c_str());
105 __builtin_unreachable();
114 cci::cci_value_list emptyargs;
118 std::string get_parent_name(
const std::string& module_name)
120 return module_name.substr(0, module_name.find_last_of(
"."));
132 auto it = m_is_absolute_path_cache.find(name);
133 if (it != m_is_absolute_path_cache.end()) {
138 std::string container_name = std::string(this->name());
142 if (name.size() > container_name.size() && name.substr(0, container_name.size()) == container_name &&
143 name[container_name.size()] ==
'.') {
145 }
else if (name.find(
'.') != std::string::npos) {
148 std::string parent_name = get_parent_name(container_name);
149 if (parent_name.empty()) {
152 }
else if (name.find(parent_name +
".") == 0) {
158 sc_core::sc_object* obj =
find_sc_obj(
nullptr, name,
true);
159 if (obj !=
nullptr) {
161 std::string obj_name = std::string(obj->name());
162 result = obj_name.find(container_name +
".") != 0;
169 result =
find_sc_obj(
nullptr, name,
true) !=
nullptr;
173 m_is_absolute_path_cache[name] = result;
177 bool is_container_arg_mod_name(
const std::string& name)
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;
194 auto cache_it = m_module_args_cache.find(modulename);
195 if (cache_it != m_module_args_cache.end()) {
196 return cache_it->second;
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));
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);
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);
215 if (arg_path.find(parent) == std::string::npos) {
216 arg.set_string(
"&" + parent +
"." + arg_path);
218 arg.set_string(
"&" + arg_path);
222 arg.set_string(
"&" + arg_path);
229 m_module_args_cache[modulename] = args;
233 template <
typename I,
typename T>
234 bool try_bind(sc_core::sc_object* i_obj, sc_core::sc_object* t_obj)
236 auto i =
dynamic_cast<I*
>(i_obj);
237 auto t =
dynamic_cast<T*
>(t_obj);
241 (
"Binding initiator socket: {}({}) with the target socket: {}({})", i_obj->name(),
typeid(i).name(),
242 t_obj->name(),
typeid(t).name());
245 t =
dynamic_cast<T*
>(i_obj);
246 i =
dynamic_cast<I*
>(t_obj);
250 (
"Binding initiator socket: {}({}) with the target socket: {}({})", t_obj->name(),
typeid(i).name(),
251 i_obj->name(),
typeid(t).name());
267 for (
const auto& param : get_cci_children(m->name())) {
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()) {
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"));
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()) {
286 std::string initname = m_broker.get_preset_cci_value(bindname).get_string();
288 if (initname.at(0) !=
'&')
continue;
290 if (initname.find(
';') != std::string::npos) {
291 order_bind(m, initname, targetname);
293 initname.erase(0, 1);
297 initname = std::string(name()) +
"." + initname;
299 SCP_TRACE(())(
"Bind {} to {}", targetname, initname);
300 do_binding(m, initname, targetname);
306 void populate_cci_children_cache()
const
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) {
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) {
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);
329 m_cci_children_cache_initialized =
true;
332 const std::set<std::string>& get_cci_children(
const std::string& object_name)
const
334 if (!m_cci_children_cache_initialized) {
335 populate_cci_children_cache();
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;
343 const std::vector<std::pair<std::string, cci::cci_value>>& get_cci_bind_params(
const std::string& object_name)
const
345 if (!m_cci_children_cache_initialized) {
346 populate_cci_children_cache();
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;
360 template <
unsigned int BIND_BUSWIDTH>
361 bool try_bind_all(sc_core::sc_object* i_obj, sc_core::sc_object* t_obj)
363 if ((try_bind<tlm_utils::multi_init_base<BIND_BUSWIDTH>, tlm::tlm_base_target_socket<BIND_BUSWIDTH>>(i_obj,
365 (try_bind<tlm_utils::multi_init_base<BIND_BUSWIDTH>, tlm_utils::multi_target_base<BIND_BUSWIDTH>>(i_obj,
367 (try_bind<tlm::tlm_base_initiator_socket<BIND_BUSWIDTH>, tlm::tlm_base_target_socket<BIND_BUSWIDTH>>(
369 (try_bind<tlm::tlm_base_initiator_socket<BIND_BUSWIDTH>, tlm_utils::multi_target_base<BIND_BUSWIDTH>>(
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)) ||
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>>(
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>>(
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>>(
392 (try_bind<sc_core::sc_port<sc_core::sc_signal_inout_if<bool>, 0, sc_core::SC_ZERO_OR_MORE_BOUND>,
403 (try_bind<QemuInitiatorSignalSocket, QemuTargetSignalSocket>(i_obj, t_obj)) ||
415 bool is_container_config(
const std::string& config_name)
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;
421 const cci::cci_value module_type = m_broker.get_preset_cci_value(config_name +
".moduletype");
422 if (!module_type.is_string()) {
426 const std::string factory_name = CCI_GS_MF_NAME + std::string(module_type.get_string());
428 if (!m_broker.get_param_handle(factory_name).is_valid()) {
429 register_module_config_from_dylib(config_name);
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();
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;
450 const bool result = is_container_config(std::string(sc_module::name()) +
"." + name);
453 m_is_container_type_cache[name] = result;
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());
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;
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;
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;
489 for (
const auto& param : params_range) {
490 all_params.push_back(param);
492 m_moduletype_params_cache[container_full_name] = all_params;
495 std::set<std::string> child_modules;
496 bool config_prefix_used =
false;
497 std::string config_prefix = container_full_name +
".config.";
499 for (
const auto& param : all_params) {
500 std::string param_name = param.first;
501 if (param_name == container_full_name +
".moduletype")
continue;
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);
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;
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);
521 child_modules.insert(child_path);
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;
530 child_config_name = container_full_name +
"." + child;
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;
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)));
542 m_module_args_cache[child_config_name] = child_args;
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);
551 if (ref_path.find(module_name) != 0) {
552 ref_path = container_full_name +
"." + ref_path;
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);
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;
571 child_is_container = is_container_config(child_config_name);
572 m_is_container_type_cache[child_config_name] = child_is_container;
575 if (child_is_container) {
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;
583 for (
const auto& param : child_params) {
584 if (param.second.is_string()) {
585 std::string bind_value = param.second.get_string();
588 while (pos < bind_value.length()) {
589 size_t end = bind_value.find(
';', pos);
590 if (end == std::string::npos) end = bind_value.length();
592 std::string single_bind = bind_value.substr(pos, end - pos);
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);
600 if (single_bind.length() > 0 && single_bind[0] ==
'&') {
601 std::string ref_path = single_bind.substr(1);
604 if (ref_path.find(module_name) != 0) {
605 ref_path = container_full_name +
"." + ref_path;
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);
617 pos = (end == bind_value.length()) ? end : end + 1;
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;
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;
634 for (
const auto& param : container_params) {
635 if (param.second.is_string()) {
636 std::string bind_value = param.second.get_string();
639 while (pos < bind_value.length()) {
640 size_t end = bind_value.find(
';', pos);
641 if (end == std::string::npos) end = bind_value.length();
643 std::string single_bind = bind_value.substr(pos, end - pos);
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);
651 if (single_bind.length() > 0 && single_bind[0] ==
'&') {
652 std::string ref_path = single_bind.substr(1);
655 if (ref_path.find(module_name) != 0) {
656 ref_path = container_full_name +
"." + ref_path;
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);
668 pos = (end == bind_value.length()) ? end : end + 1;
673 m_per_pass_nested_deps_cache[container_name] = computed_deps;
674 dependencies.insert(computed_deps.begin(), computed_deps.end());
677 bool are_dependencies_available(
const std::string& name,
const std::set<std::string>& done)
679 std::string module_name = std::string(sc_module::name());
680 std::set<std::string> all_dependencies;
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);
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);
707 if (done.count(referenced_module) == 0) {
711 sc_core::sc_object* dep_obj =
find_sc_obj(
nullptr, ref_path,
true);
712 if (dep_obj ==
nullptr) {
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)
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;
732 std::set<std::string> modules_that_reference_target;
733 std::string target_full_path = module_name +
"." + target_module_name;
735 for (
const auto& mod : all_modules) {
736 if (mod == target_module_name)
continue;
738 std::string mod_full_path = module_name +
"." + mod;
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;
746 for (
const auto& param : mod_params) {
747 const std::string& param_name = param.first;
748 const cci::cci_value& param_value = param.second;
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);
768 module_cache[target_module_name] = modules_that_reference_target;
769 return modules_that_reference_target;
772 int get_construction_priority(
const std::string& name)
774 std::string module_name = std::string(sc_module::name());
775 std::string priority_param = module_name +
"." + name +
".construction_priority";
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());
788 std::list<std::string> PriorityConstruct(
void)
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());
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);
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);
812 for (
auto& priority_pair : priority_groups) {
813 std::list<std::string> todo = priority_pair.second;
814 int max_passes = todo.size() + 1;
817 bool enforce_bind_deps =
true;
818 while (todo.size() > 0 && pass < max_passes) {
820 int constructed_this_pass = 0;
822 m_per_pass_nested_deps_cache.clear();
824 std::map<std::string, std::set<std::string>> depends_on_me;
825 for (
const auto& source_mod : todo) {
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()) {
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) {
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()) {
848 std::string target = bind.get_string();
849 if (target.empty() || target.front() !=
'&') {
854 const std::string module_prefix = module_name +
".";
855 if (target.find(module_prefix) == 0) {
856 target.erase(0, module_prefix.size());
858 const size_t target_end = target.find(
'.');
859 if (target_end != std::string::npos) {
860 target.erase(target_end);
863 if (std::find(todo.begin(), todo.end(), target) != todo.end()) {
864 depends_on_me[target].insert(source_mod);
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);
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);
896 if (std::find(todo.begin(), todo.end(), sibling_name) != todo.end()) {
897 depends_on_me[sibling_name].insert(mod);
903 for (
auto it = todo.begin(); it != todo.end();) {
904 std::string name = *it;
906 bool deps_available = are_dependencies_available(name, done);
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;
914 if (modules_depending_on_it.count(name) > 0) {
915 if (done.count(required_module) == 0) {
916 bind_deps_ready =
false;
923 if (deps_available && bind_deps_ready) {
926 constructed_this_pass++;
927 args.push_back(name);
933 if (constructed_this_pass == 0 && todo.size() > 0) {
934 if (enforce_bind_deps) {
935 enforce_bind_deps =
false;
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;
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);
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);
965 SCP_FATAL(())(
"Unresolvable module dependencies");
973 void register_module_from_dylib(sc_core::sc_module_name name)
975 register_module_config_from_dylib(std::string(sc_module::name()) +
"." + std::string(name));
979 void register_module_config_from_dylib(
const std::string& config_name)
982 std::vector<std::string> lib_types = {
".moduletype",
".dylib_path" };
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());
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());
997 m_dls.insert(libraryHandle);
999 void (*module_register)() =
nullptr;
1000 if (libraryHandle->symbol_exists(
"module_register")) {
1001 module_register =
reinterpret_cast<void (*)()
>(libraryHandle->get_symbol(
"module_register"));
1004 SCP_WARN(()) <<
"module_register not found in library " << libname;
1009 void ModulesConstruct(
void)
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";
1025 m_allModules.push_back(no_construct_mod);
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);
1036 (
"Can't automatically handle argument list for module: " + mod_type).c_str());
1039 m_allModules.push_back(m);
1040 if (!m_local_pass) {
1041 m_local_pass =
dynamic_cast<transaction_forwarder_if<PASS>*
>(m);
1044 SCP_WARN(()) <<
"moduletype parameter is not a string for " << name;
1051 populate_cci_children_cache();
1052 for (
auto mod : m_allModules) {
1055 SCP_DEBUG(())(
"ModulesConstruct complete");
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>;
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;
1068 std::set<qemu::LibraryLoaderIface::LibraryIfacePtr> m_dls;
1069 qemu::LibraryLoaderIface& m_library_loader = qemu::get_default_lib_loader();
1072 cci::cci_broker_handle m_broker;
1073 cci::cci_param<std::string> moduletype;
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;
1085 std::vector<cci::cci_param<gs::cci_constructor_vl>*> registered_mods;
1086 sc_core::sc_object* container_mod_arg;
1090 m_constructedModules.reverse();
1091 m_constructedModules.clear();
1094 std::shared_ptr<sc_core::sc_module> find_module_by_name(
const std::string& mod_name)
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()) {
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)
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));
1114 if (ret == m_constructedModules.end()) {
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)
1143 SCP_DEBUG(()) <<
"ContainerBase Constructor";
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); });
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);
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);
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);
1170 container_self_reset.register_value_changed_cb([
this](
bool value) { do_reset(value); });
1172 auto mods = gs::ModuleFactory::GetAvailableModuleList();
1174 while (mods->size()) {
1175 registered_mods.push_back((mods->back())());
1179 if (!m_defer_modules_construct) ModulesConstruct();
1182 void fw_b_transport(
int id, tlm::tlm_generic_payload& trans, sc_core::sc_time& delay)
override
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);
1189 unsigned int fw_transport_dbg(
int id, tlm::tlm_generic_payload& trans)
override
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);
1197 bool fw_get_direct_mem_ptr(
int id, tlm::tlm_generic_payload& trans, tlm::tlm_dmi& dmi_data)
override
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();
1208 void fw_invalidate_direct_mem_ptr(sc_dt::uint64 start, sc_dt::uint64 end)
override
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);
1217 void fw_handle_signal(
int id,
bool value)
override
1219 SCP_DEBUG(()) <<
"calling handle_signal on initiator_signal_socket_" <<
id <<
" value: " << std::boolalpha
1221 initiator_signal_sockets[id]->write(value);
1225 void b_transport(
int id, tlm::tlm_generic_payload& trans, sc_core::sc_time& delay)
1228 m_local_pass->fw_b_transport(
id, trans, delay);
1232 unsigned int transport_dbg(
int id, tlm::tlm_generic_payload& trans)
1235 return m_local_pass->fw_transport_dbg(
id, trans);
1240 bool get_direct_mem_ptr(
int id, tlm::tlm_generic_payload& trans, tlm::tlm_dmi& dmi_data)
1243 return m_local_pass->fw_get_direct_mem_ptr(
id, trans, dmi_data);
1248 void invalidate_direct_mem_ptr(sc_dt::uint64 start, sc_dt::uint64 end)
1251 m_local_pass->fw_invalidate_direct_mem_ptr(start, end);
1255 std::vector<std::string> parse_order_str(
const std::string& reset_order)
1257 std::vector<std::string> ret;
1258 if (reset_order.empty()) {
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());
1273 void do_binding(sc_core::sc_module* module_obj,
const std::string& initiator_name,
const std::string& target_name)
1275 sc_core::sc_object* i_obj =
nullptr;
1276 sc_core::sc_object* t_obj =
nullptr;
1287 SCP_FATAL(())(
"No bind found for: {} to {}", i_obj->name(), t_obj->name());
1291 void order_bind(sc_core::sc_module* module_obj,
const std::string& target_order_str,
1292 const std::string& initiator_name)
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);
1301 target_name = std::string(name()) +
"." + target_name;
1303 SCP_TRACE(())(
"Bind {} to {}", target_name, initiator_name);
1304 do_binding(module_obj, target_name, initiator_name);
1309 virtual void do_reset(
bool value)
1311 if (value) SCP_WARN(()) <<
"Reset";