36 SCP_LOGGER((),
"json_zip_archive");
38 std::set<std::string> m_loaded;
39 std::vector<uint8_t> m_bin_file_data;
41 bool m_base_address_captured;
42 uint64_t m_base_address;
43 uint64_t m_parent_model_base_addr;
44 bool m_is_platform_top;
45 bool m_is_platform_model;
47 std::shared_ptr<gs::router<>> m_router;
48 std::shared_ptr<gs::gs_memory<>> m_memory;
49 std::string m_archive_name;
52 json_zip_archive(zip_t* archive);
54 zip_t* get_zip_archive_ptr();
56 std::string get_archive_name();
58 uint64_t getNumber(rapidjson::Value& v)
64 const char* p = v.GetString();
65 return std::strtoull(p,
nullptr, 0);
67 SCP_FATAL(())(
"JSON Number neither number, nor string!");
71 std::vector<std::string> get_file_names_in_zip_archive();
73 std::string get_json_topname();
75 std::string get_bin_file_name();
77 uint64_t get_bin_file_size();
79 rapidjson::Document& get_json(std::string file, rapidjson::Document& d);
83 void cci_set(cci::cci_broker_handle broker, std::string n, T value)
85 if (!broker.has_preset_value(n)) {
86 SCP_DEBUG(())(
"set {} to {}", n, value);
87 broker.set_preset_cci_value(n, cci::cci_value(value));
89 SCP_FATAL(())(
"Trying to re-set a value? {}", n);
94 bool json_read_cci(cci::cci_broker_handle broker, std::string sc_name,
bool is_platform_model =
false,
96 std::shared_ptr<
gs::gs_memory<>> _memory =
nullptr,
bool is_platform =
false,
97 std::string _path =
"", rapidjson::Value* _node =
nullptr,
bool top =
true)
99 m_is_platform_model = is_platform_model;
100 m_is_platform_top = is_platform;
101 std::string module_path = _path.empty() ? get_json_topname() : _path;
103 rapidjson::Document json_doc;
104 rapidjson::Value& node = _node ? *_node : get_json(module_path +
".json", json_doc);
106 if (node.IsNull())
return false;
108 sc_name.erase(remove_if(sc_name.begin(), sc_name.end(), isspace), sc_name.end());
109 if (m_loaded.count(sc_name))
return true;
110 m_loaded.insert(sc_name);
113 (
"JSON read into sc object {} from JSON node {}", sc_name, node[
"NAME"].GetString());
115 if (node.HasMember(
"ADDRESS") && !m_base_address_captured) {
118 uint64_t bin_file_size = get_bin_file_size();
120 if (node.HasMember(
"SIZE")) size = getNumber(node[
"SIZE"]);
121 m_max_size = std::max(size, bin_file_size);
122 SCP_TRACE(())(
"Setting {}.reg_memory", sc_name);
123 m_base_address = getNumber(node[
"ADDRESS"]);
124 m_parent_model_base_addr = m_base_address;
126 std::string parent_mod_name = sc_name.substr(0, sc_name.find_last_of(
'.'));
127 if (broker.has_preset_value(parent_mod_name +
".target_socket.address")) {
128 broker.get_preset_cci_value(parent_mod_name +
".target_socket.address")
129 .template try_get<uint64_t>(m_parent_model_base_addr);
132 (
"{} is not connected to a router, or not configured properly", parent_mod_name);
135 m_base_address_captured =
true;
137 if (node.HasMember(
"MODULES")) {
138 auto mods = node[
"MODULES"].GetArray();
139 for (
auto& mod : mods) {
140 if (mod.IsObject()) {
141 if (!mod.HasMember(
"INST_HIER")) {
143 (
"Missing INST_HIER entry while parsing JSON config file in archive: {}", get_archive_name());
145 std::string mod_hier_name = mod[
"INST_HIER"].GetString();
146 std::string mod_name = mod[
"NAME"].GetString();
147 std::string sn = sc_name +
"." +
148 (is_platform ? mod_hier_name.substr(mod_hier_name.find_last_of(
".") + 1)
150 json_read_cci(broker, sn, is_platform_model, _router, _memory, is_platform, module_path, &mod,
152 if (mod.HasMember(
"OFFSET")) {
153 cci_set(broker, sn +
".target_socket.chained",
false);
158 if (node.HasMember(
"MEMORY") && !is_platform_model) {
159 uint64_t address = getNumber(node[
"ADDRESS"]);
160 auto mems = node[
"MEMORY"].GetArray();
161 for (
auto& m : mems) {
162 std::string sn = sc_name +
"." + m[
"NAME"].GetString();
163 uint64_t mem_address = 0;
164 if (m.HasMember(
"OFFSET"))
165 mem_address = getNumber(m[
"OFFSET"]) + (is_platform ? address : 0);
167 mem_address = getNumber(m[
"ADDRESS"]) + (is_platform ? address : 0);
168 uint64_t mem_size = (m.HasMember(
"SIZE") ? getNumber(m[
"SIZE"]) : 0);
169 m_max_size = std::max(m_max_size, (mem_address + mem_size - (is_platform ? m_base_address : 0)));
170 cci_set(broker, sn +
".target_socket.address", mem_address);
171 cci_set(broker, sn +
".target_socket.size", mem_size);
172 cci_set(broker, sn +
".target_socket.relative_addresses",
true);
173 cci_set(broker, sn +
".target_socket.priority", 0);
176 if (node.HasMember(
"ADDRESS")) {
177 uint64_t mod_size = 0;
178 uint64_t mod_address = getNumber(node[
"ADDRESS"]);
179 if (node.HasMember(
"SIZE")) mod_size = getNumber(node[
"SIZE"]);
180 m_max_size = std::max(m_max_size, (mod_address + mod_size - m_base_address));
181 cci_set(broker, sc_name +
".target_socket.address", mod_address);
182 cci_set(broker, sc_name +
".target_socket.size", mod_size);
183 cci_set(broker, sc_name +
".target_socket.relative_addresses",
true);
184 if (!is_platform) cci_set(broker, sc_name +
".router.lazy_init",
true);
187 if (!is_platform_model && !is_platform) {
188 cci_set(broker, sc_name +
".reg_memory.target_socket.address", 0);
189 cci_set(broker, sc_name +
".reg_memory.target_socket.size", m_max_size);
190 cci_set(broker, sc_name +
".reg_memory.target_socket.relative_addresses",
true);
192 cci_set(broker, sc_name +
".reg_router.target_socket.address",
193 (is_platform_model || is_platform) ? m_parent_model_base_addr : 0);
194 cci_set(broker, sc_name +
".reg_router.target_socket.size", m_max_size);
195 cci_set(broker, sc_name +
".reg_router.target_socket.priority", 1024);
196 cci_set(broker, sc_name +
".reg_router.target_socket.relative_addresses",
true);
197 cci_set(broker, sc_name +
".reg_router.lazy_init",
true);
202 uint64_t get_base_address()
const {
return m_base_address; }
203 uint64_t get_parent_module_address() {
return m_parent_model_base_addr; }
204 bool is_platform_top() {
return m_is_platform_top; }
205 bool is_platform_model() {
return m_is_platform_model; }
206 void set_memory(std::shared_ptr<
gs::gs_memory<>> _memory) { m_memory = _memory; }
207 void set_router(std::shared_ptr<
gs::router<>> _router) { m_router = _router; }
208 std::shared_ptr<gs::gs_memory<>> get_memory() {
return m_memory; }
209 std::shared_ptr<gs::router<>> get_router() {
return m_router; }
214 SCP_LOGGER((),
"json_module_base_components");
217 base_components(
const std::string& name,
json_zip_archive& jza, cci::cci_broker_handle broker,
220 , m_reg_memory(
nullptr)
221 , m_reg_router(
nullptr)
223 , m_is_platform_mod(_router && _reg_memory)
224 , m_is_platform_top(jza.is_platform_top())
226 SCP_LOGGER_NAME().features[0] =
"json_module_base_components";
227 SCP_TRACE(())(
"base_components Constructor");
228 if (!m_is_platform_mod) {
230 if (!m_is_platform_top) {
231 m_reg_memory = std::make_shared<gs::gs_memory<>>(
"reg_memory");
232 m_router = std::make_shared<gs::router<>>(
"router");
233 m_reset.bind(m_reg_memory->reset);
234 bin_file_name = jza.get_bin_file_name();
235 zip_archive_name = jza.get_archive_name();
236 zip_archive_ptr = jza.get_zip_archive_ptr();
237 m_reg_memory->load.zip_file_load(zip_archive_ptr, zip_archive_name, 0, bin_file_name);
239 m_router = jza.get_router();
240 m_reg_memory = jza.get_memory();
243 std::string mod_name = std::string(_router->name());
244 std::string container_name = mod_name.substr(0, mod_name.find_last_of(
"."));
249 (
"parent module: {} is not a container)", container_name);
251 m_router = std::dynamic_pointer_cast<gs::router<>>(
252 container->find_module_by_name(std::string(_router->name())));
253 m_reg_memory = std::dynamic_pointer_cast<gs::gs_memory<>>(
254 container->find_module_by_name(std::string(_reg_memory->name())));
257 sc_assert(m_reg_memory);
259 void insert_mod_addr_name_pair(
const std::pair<uint64_t, std::pair<uint64_t, std::string>>& item)
261 m_mod_addr_name_map.insert(item);
263 void init_reg_router()
265 if (m_reg_memory && !m_reg_router) {
266 m_reg_router = std::make_unique<gs::reg_router<>>(
"reg_router", m_mod_addr_name_map);
267 m_reg_router->initiator_socket(m_reg_memory->socket);
268 if (!m_is_platform_mod) m_router->initiator_socket(m_reg_router->target_socket);
271 ~base_components() {}
272 void reset(
bool value)
274 if (m_reg_memory && !m_is_platform_mod && !m_is_platform_top) {
275 m_reset->write(value);
277 m_reg_memory->load.zip_file_load(zip_archive_ptr, zip_archive_name, 0, bin_file_name);
282 std::shared_ptr<gs::router<>> m_router;
283 std::shared_ptr<gs::gs_memory<>> m_reg_memory;
284 std::unique_ptr<gs::reg_router<>> m_reg_router;
287 InitiatorSignalSocket<bool> m_reset;
288 std::map<uint64_t, std::pair<uint64_t, std::string>> m_mod_addr_name_map;
289 bool m_is_platform_mod;
290 bool m_is_platform_top;
291 std::string bin_file_name;
292 std::string zip_archive_name;
293 zip_t* zip_archive_ptr;
296class json_module_base :
public virtual sc_core::sc_module
302 cci::cci_broker_handle m_broker;
303 std::shared_ptr<base_components> m_sh_comp;
304 sc_core::sc_vector<gs::gs_memory<>> memories;
305 sc_core::sc_vector<json_module_base> sub_modules;
306 uint32_t m_bound_regs_num;
307 bool m_is_platform_mod;
311 void cci_set(std::string n, T value)
313 auto handle = m_broker.get_param_handle(n);
314 if (handle.is_valid())
315 handle.set_cci_value(cci::cci_value(value));
316 else if (!m_broker.has_preset_value(n)) {
317 m_broker.set_preset_cci_value(n, cci::cci_value(value));
319 SCP_FATAL(())(
"Trying to re-set a value? {}", n);
323 template <
typename T>
324 T cci_get(std::string n)
327 if (!m_broker.get_preset_cci_value(n).template try_get<T>(ret)) {
328 SCP_FATAL(()) <<
"Unable to get parameter " << n <<
"\nIs your .lua file up-to-date?";
330 m_broker.lock_preset_value(n);
335 ~json_module_base() =
default;
338 gs::gs_memory<>* _reg_memory =
nullptr,
const std::shared_ptr<base_components>& sh_comp =
nullptr,
339 rapidjson::Value* _node =
nullptr, std::string _path =
"",
bool top =
true)
340 : sc_core::sc_module(_name)
342 , m_broker(cci::cci_get_broker())
344 , m_bound_regs_num(0)
345 , m_is_platform_mod(
false)
347 SCP_TRACE(())(
"Constructor of {}", sc_core::sc_module::name());
348 std::string n = std::string(sc_core::sc_module::name());
349 std::string module_path = _path.empty() ? jza.get_json_topname() : _path;
350 rapidjson::Document json_doc;
351 rapidjson::Value& node = _node ? *_node : jza.get_json(module_path +
".json", json_doc);
354 m_is_platform_mod = jza.is_platform_model();
355 if (m_is_platform_mod && !_router && !_reg_memory) {
357 (
"Platform model is instantiated without router and reg_memory arguments, is your conf.lua updated?");
359 m_sh_comp = std::make_shared<base_components>(std::string(sc_core::sc_module::name()), jza, m_broker,
360 _router, _reg_memory);
362 if (m_sh_comp && jza.is_platform_top() && node.HasMember(
"ADDRESS") && node.HasMember(
"SIZE") &&
363 node.HasMember(
"INST_HIER")) {
364 std::string mod_hier_name = node[
"INST_HIER"].GetString();
365 std::string mod_name = mod_hier_name.substr(mod_hier_name.find_last_of(
".") + 1);
366 m_sh_comp->insert_mod_addr_name_pair(std::make_pair<uint64_t, std::pair<uint64_t, std::string>>(
367 (jza.getNumber(node[
"ADDRESS"]) - jza.get_base_address()) +
368 (m_is_platform_mod ? jza.get_parent_module_address() : 0),
369 std::make_pair<uint64_t, std::string>(jza.getNumber(node[
"SIZE"]), std::move(mod_name))));
371 if (!m_is_platform_mod && node.HasMember(
"MEMORY")) {
372 auto mems = node[
"MEMORY"].GetArray();
373 memories.init(mems.Size(), [&, mems](
const char* name,
size_t n) ->
gs::gs_memory<>* {
374 return new gs::gs_memory<>(mems[n][
"NAME"].GetString());
377 for (
auto& m : memories) {
378 m_sh_comp->m_router->initiator_socket(m.socket);
380 if (node.HasMember(
"MODULES")) {
381 auto mods = node[
"MODULES"].GetArray();
382 sub_modules.init(mods.Size(), [&, mods](
const char* name,
size_t n) -> json_module_base* {
383 if (!mods[n].IsObject()) {
384 SCP_FATAL(())(
"JSON file for json_module_base must be fully self contained");
386 std::string mod_hier_name = mods[n][
"INST_HIER"].GetString();
387 std::string mod_name = mods[n][
"NAME"].GetString();
388 mod_hier_name.erase(remove_if(mod_hier_name.begin(), mod_hier_name.end(), isspace),
389 mod_hier_name.end());
390 mod_name.erase(remove_if(mod_name.begin(), mod_name.end(), isspace), mod_name.end());
391 std::string used_mod_name = (jza.is_platform_top()
392 ? mod_hier_name.substr(mod_hier_name.find_last_of(
".") + 1)
394 return new json_module_base(used_mod_name.c_str(), jza, _router, _reg_memory, m_sh_comp, &mods[n],
399 m_sh_comp->init_reg_router();
403 template <
class TYPE>
406 std::string path = reg.get_path();
407 std::string self_full_name = std::string(sc_core::sc_module::name());
408 std::string self_name = self_full_name.substr(self_full_name.find_last_of(
".") + 1);
409 std::string parent = path.substr(self_name.length() + 1);
410 if (parent == reg.get_regname()) {
413 parent = parent.substr(0, parent.find_last_of(
"."));
416 (
"Binding {} in {}", reg.get_regname(),
417 std::string(sc_core::sc_module::name()) + ((!parent.empty()) ?
"." + parent :
""));
418 reg.initiator_socket.bind(m_sh_comp->m_reg_memory->socket);
419 std::string mod_address_str = std::string(sc_core::sc_module::name()) +
"." +
420 ((!parent.empty()) ? parent +
"." :
"") +
"target_socket.address";
421 std::string reg_target_socket_name = std::string(this->name()) +
"." + ((!parent.empty()) ? parent +
"." :
"") +
422 reg.get_regname() +
".target_socket";
423 uint64_t parent_address = m_jza.get_parent_module_address();
424 uint64_t mod_address = cci_get<uint64_t>(mod_address_str) - m_jza.get_base_address();
425 std::string reg_address_str = reg_target_socket_name +
".address";
426 uint64_t new_reg_offset = reg.get_offset() + mod_address + (m_jza.is_platform_model() ? parent_address : 0);
427 cci_set<uint64_t>(reg_address_str, new_reg_offset);
428 if constexpr (gs::is_gs_bank<TYPE>::value) {
429 auto encode_dims = [](
const std::vector<uint64_t>& values) {
431 for (
size_t i = 0; i < values.size(); ++i) {
432 if (i != 0) result +=
',';
433 result += std::to_string(values[i]);
437 cci_set<std::string>(reg_target_socket_name +
".dim_strides", encode_dims(reg.get_dim_strides()));
438 cci_set<std::string>(reg_target_socket_name +
".dim_counts", encode_dims(reg.get_dim_counts()));
440 if (priority != 0) cci_set<uint32_t>(reg_target_socket_name +
".priority", priority);
441 m_sh_comp->m_reg_router->initiator_socket.bind(reg);
442 m_sh_comp->m_reg_router->rename_last(reg_target_socket_name);
446 uint32_t get_num_of_bound_regs()
const {
return m_bound_regs_num; }
448 void log_end_of_binding_msg(
const std::string& gen_name)
450 SCP_DEBUG(()) << gen_name <<
": Constructed and bound " << m_bound_regs_num <<
" registers.";
453 void reset(
bool value) { m_sh_comp->reset(value); }
476class json_module_top :
public json_module_base
479 bool is_json_loaded_ok(
json_zip_archive& jza, cci::cci_broker_handle broker, std::string sc_name,
481 std::string _path, rapidjson::Value* _node)
483 bool loaded_ok = jza.json_read_cci(broker, sc_name,
false, _router, _memory,
true, _path, _node,
true);
484 sc_assert(loaded_ok);
489 ~json_module_top() =
default;
491 json_module_top(sc_core::sc_module_name _name,
json_zip_archive& jza, cci::cci_broker_handle broker,
492 std::string sc_name, std::shared_ptr<
gs::router<>> _router =
nullptr,
493 std::shared_ptr<
gs::gs_memory<>> _memory =
nullptr, std::string _path =
"",
494 rapidjson::Value* _node =
nullptr)
495 : json_module_base(_name, jza,
nullptr,
nullptr,
nullptr,
nullptr,
"",
496 is_json_loaded_ok(jza, broker, sc_name, _router, _memory, _path, _node))