/*
 * Copyright (c) 2015, Technische Universität Kaiserslautern
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Authors:
 *    Robert Gernhardt
 *    Matthias Jung
 *    Luiza Correa
 *    Lukas Steiner
 *    Derek Christ
 */

#include "simulator/TraceSetup.h"
#include "simulator/TrafficInitiator.h"

#include "DRAMSys/simulation/DRAMSys.h"
#include "DRAMSys/simulation/DRAMSysRecordable.h"
#include "DRAMSys/config/DRAMSysConfiguration.h"

#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <chrono>
#include <systemc>

using namespace sc_core;

std::string pathOfFile(const std::string &file)
{
    return file.substr(0, file.find_last_of('/'));
}

int main(int argc, char **argv)
{
    return sc_main(argc, argv);
}

int sc_main(int argc, char **argv)
{
    sc_set_time_resolution(1, SC_PS);

    std::string resources;
    std::string simulationJson;
    // Run only with default config (ddr3-example.json):
    if (argc == 1)
    {
        // Get path of resources:
        resources = pathOfFile(argv[0])
                    + std::string("/../../configs/");
        simulationJson = resources + "ddr4-example.json";
    }
    // Run with specific config but default resource folders:
    else if (argc == 2)
    {
        // Get path of resources:
        resources = pathOfFile(argv[0])
                    + std::string("/../../configs/");
        simulationJson = argv[1];
    }
    // Run with specific config and specific resource folder:
    else if (argc == 3)
    {
        simulationJson = argv[1];
        resources = argv[2];
    }

    std::vector<std::unique_ptr<TrafficInitiator>> players;

    DRAMSys::Config::Configuration configLib = DRAMSys::Config::from_path(simulationJson, resources);

    // Instantiate DRAMSys:
    std::unique_ptr<DRAMSys::DRAMSys> dramSys;

    if (configLib.simConfig.databaseRecording.value_or(false))
        dramSys = std::make_unique<DRAMSys::DRAMSysRecordable>("DRAMSys", configLib);
    else
        dramSys = std::make_unique<DRAMSys::DRAMSys>("DRAMSys", configLib);

    if (!configLib.traceSetup.has_value())
        SC_REPORT_FATAL("sc_main", "No trace setup section provided.");

    // Instantiate STL Players:
    TraceSetup setup(dramSys->getConfig(), configLib.traceSetup.value(), resources, players);

    // Bind STL Players with DRAMSys:
    for (auto& player : players)
            player->iSocket.bind(dramSys->tSocket);

    // Store the starting of the simulation in wall-clock time:
    auto start = std::chrono::high_resolution_clock::now();

    // Start SystemC Simulation:
    sc_set_stop_mode(SC_STOP_FINISH_DELTA);
    sc_start();

    if (!sc_end_of_simulation_invoked())
    {
        SC_REPORT_WARNING("sc_main", "Simulation stopped without explicit sc_stop()");
        sc_stop();
    }

    auto finish = std::chrono::high_resolution_clock::now();
    std::chrono::duration<double> elapsed = finish - start;
    std::cout << "Simulation took " + std::to_string(elapsed.count()) + " seconds." << std::endl;
    return 0;
}
