Corona_with_TLM_FIFO/main.cpp

83 lines
2.8 KiB
C++
Raw Normal View History

#include <systemc.h>
#include <tlm.h>
#include <tlm_utils/simple_initiator_socket.h>
#include <tlm_utils/simple_target_socket.h>
#include <queue>
#include <iostream>
#include <vector>
#include <string>
#include <iostream>
#include <cstdlib> // For rand()
#include <ctime> // For time()
#include "configuration.h"
#include "router.h"
#include "core.h"
#include "semaphore_manager.h"
int sc_main(int argc, char* argv[]) {
// Create a semaphore manager instance
semaphore_manager sem_mgr("SemaphoreManager");
// Instantiate ROUTER_NO routers
router* routers[ROUTER_NO];
std::vector<sc_core::sc_semaphore*> router_sems;
std::vector<sc_core::sc_fifo<my_payload*>*> router_out_fifos;
for (int i = 0; i < ROUTER_NO; ++i) {
std::string router_name = "router" + std::to_string(i);
routers[i] = new router(router_name.c_str());
// Assign the router ID
routers[i]-> router_id = i;
// Store semaphores and out_fifos in vectors
router_sems.push_back(&routers[i]->sem);
router_out_fifos.push_back(&routers[i]->out_fifo);
routers[i]->set_semaphore_manager(&sem_mgr); // Pass semaphore manager to each router
}
// Set up pointers to other routers' semaphores and out_fifos for each router
for (int i = 0; i < ROUTER_NO; i++) {
routers[i]->other_sems = router_sems;
routers[i]->other_out_fifos = router_out_fifos;
}
// Instantiate ROUTER_NO*CORE_NO cores (CORE_NO per router)
core* cores[ROUTER_NO][CORE_NO]; // ROUTER_NO routers, each with CORE_NO cores
for (int i = 0; i < ROUTER_NO; ++i) { // Router index
for (int j = 0; j < CORE_NO; ++j) { // Core index within each router
std::string core_name = "core" + std::to_string(i) + "_" + std::to_string(j);
cores[i][j] = new core(core_name.c_str());
cores[i][j]->source_router_id = i; // Set source router ID
cores[i][j]->source_core_id = j; // Set source router ID
}
}
// Bind cores to their respective routers
for (int i = 0; i < ROUTER_NO; ++i) { // Router index
for (int j = 0; j < CORE_NO; ++j) { // Core index within each router
cores[i][j]->socket.bind(routers[i]->sockets[j]); // Core's socket to router's target socket
routers[i]->core_sockets[j].bind(cores[i][j]->target_socket); // Router's initiator socket to core's target socket
routers[i]->current_router_id= i;
}
}
// Start the simulation
sc_core::sc_start(400, SC_NS);
//sc_start();
// Clean up memory to avoid leaks
for (int i = 0; i < ROUTER_NO; ++i) {
for (int j = 0; j < CORE_NO; ++j) {
delete cores[i][j]; // Delete each core
}
delete routers[i]; // Delete each router
}
return 0;
}