A SystemC productivity library for virtual platform development utilizing SCV and TLM2.0 https://www.minres.com/#opensource

configurer.h 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*******************************************************************************
  2. * Copyright 2017, 2018 MINRES Technologies GmbH
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *******************************************************************************/
  16. #ifndef _SYSC_CONFIGURER_H_
  17. #define _SYSC_CONFIGURER_H_
  18. #include "report.h"
  19. #include "utilities.h"
  20. #include <cci_configuration>
  21. #include <json/json.h>
  22. namespace scc {
  23. /**
  24. * a class to configure a design hierarchy using a JSON input file
  25. */
  26. class configurer : public sc_core::sc_object {
  27. public:
  28. using base_type = sc_core::sc_object;
  29. /**
  30. * create a configurer using a JSON input file
  31. * @param filename
  32. */
  33. configurer(const std::string &filename);
  34. /**
  35. * no default constructor
  36. */
  37. configurer() = delete;
  38. /**
  39. * no copy constructor
  40. *
  41. * @param
  42. */
  43. configurer(const configurer &) = delete;
  44. /**
  45. * no move constructor
  46. *
  47. * @param
  48. */
  49. configurer(configurer &&) = delete;
  50. /**
  51. * no copy assignment
  52. *
  53. * @param
  54. * @return
  55. */
  56. configurer &operator=(const configurer &) = delete;
  57. /**
  58. * no move assignment
  59. *
  60. * @param
  61. * @return
  62. */
  63. configurer &operator=(configurer &&) = delete;
  64. /**
  65. * configure the design hierarchy using the input file
  66. */
  67. void configure();
  68. /**
  69. * dump the design hierarchy as text
  70. *
  71. * @param os the output stream, std::cout by default
  72. * @param obj if not null specifies the root object of the dump
  73. */
  74. void dump_hierarchy(std::ostream &os = std::cout, sc_core::sc_object *obj = nullptr);
  75. /**
  76. * dump the parameters of a design hierarchy to output stream
  77. *
  78. * @param os the output stream, std::cout by default
  79. * @param obj if not null specifies the root object of the dump
  80. */
  81. void dump_configuration(std::ostream &os = std::cout, sc_core::sc_object *obj = nullptr);
  82. /**
  83. * set a value a some attribute (sc_attribute or cci_param)
  84. *
  85. * @param hier_name the hierarchical name of the attribute
  86. * @param value the value to put
  87. */
  88. template <typename T> void set_value(const std::string &hier_name, T value) {
  89. cci::cci_param_handle param_handle = cci_broker.get_param_handle(hier_name);
  90. if (param_handle.is_valid()) {
  91. param_handle.set_cci_value(cci::cci_value(value));
  92. } else {
  93. size_t pos = hier_name.find_last_of('.');
  94. sc_core::sc_module *mod =
  95. dynamic_cast<sc_core::sc_module *>(sc_core::sc_find_object(hier_name.substr(0, pos).c_str()));
  96. if (mod != nullptr) {
  97. sc_core::sc_attribute<T> *attr =
  98. dynamic_cast<sc_core::sc_attribute<T> *>(mod->get_attribute(hier_name.substr(pos + 1)));
  99. if (attr != nullptr)
  100. attr->value = value;
  101. else
  102. LOG(ERROR) << "Could not set attribute value " << hier_name;
  103. }
  104. }
  105. }
  106. /**
  107. * set a value of an sc_attribute from given configuration
  108. *
  109. * @param attr_base
  110. * @param owner
  111. */
  112. void set_configuration_value(sc_core::sc_attr_base *attr_base, sc_core::sc_object *owner);
  113. /**
  114. * find the configurer in the design hierarchy
  115. *
  116. * @return
  117. */
  118. static configurer &instance() {
  119. configurer *inst = dynamic_cast<configurer *>(sc_core::sc_find_object("configurer"));
  120. sc_assert("No configurer instantiated when using it" && inst != nullptr);
  121. return *inst;
  122. }
  123. protected:
  124. void dump_configuration(std::ostream &os, sc_core::sc_object *obj, Json::Value &node);
  125. void configure_sc_attribute_hierarchical(sc_core::sc_object *obj, Json::Value &hier_val);
  126. void set_value(sc_core::sc_attr_base *attr_base, Json::Value &hier_val);
  127. Json::Value &get_value_from_hierarchy(const std::string &hier_name);
  128. Json::Value &get_value_from_hierarchy(const std::string &hier_name, Json::Value &val);
  129. void configure_cci_hierarchical(const Json::Value &root, std::string prefix);
  130. Json::Value root;
  131. cci::cci_originator cci_originator;
  132. cci::cci_broker_handle cci_broker;
  133. };
  134. }
  135. #endif /* _SYSC_CONFIGURER_H_ */