
#include <systemc>
#include <sys/time.h>
#include <iostream>
#include <cstdlib>
#include <cstdarg>

#include "mapping_table.h"
#include "mips32.h"
#include "iss2_simhelper.h"
#include "vci_simple_ram.h"
#include "vci_multi_tty.h"
#include "vci_vgmn.h"
#include "vci_mem_cache_v1.h"
#include "vci_cc_xcache_wrapper_multi.h"

#ifdef USE_GDB_SERVER
#include "iss/gdbserver.h"
#endif

#include "segmentation.h"

int _main(int argc, char *argv[])
{
	using namespace sc_core;
	// Avoid repeating these everywhere
	using soclib::common::IntTab;
	using soclib::common::Segment;

	// Define VCI parameters
	typedef soclib::caba::VciParams<4,8,64,1,1,1,14,4,4,1> vci_param;
	typedef soclib::common::Iss2Simhelper<soclib::common::Mips32ElIss> proc_iss;
	// Mapping table

	soclib::common::MappingTable maptabp(40, IntTab(16), IntTab(8), 0x00300000);

	maptabp.add(Segment("reset", RESET_BASE, RESET_SIZE, IntTab(0), true));
	maptabp.add(Segment("excep", EXCEP_BASE, EXCEP_SIZE, IntTab(0), true));
	maptabp.add(Segment("text" , TEXT_BASE , TEXT_SIZE , IntTab(0), true));
	maptabp.add(Segment("tty"  , TTY_BASE  , TTY_SIZE  , IntTab(1), false));
	maptabp.add(Segment("mc_m" , MC_M_BASE , MC_M_SIZE , IntTab(0), true ));
 
	std::cout << maptabp << std::endl;

        soclib::common::MappingTable maptabc(40, IntTab(16), IntTab(8), 0x00300000);
	maptabc.add(Segment("c_proc0" , C_PROC0_BASE , C_PROC0_SIZE , IntTab(0), false, true, IntTab(0)));
	maptabc.add(Segment("mc_m" , MC_M_BASE , MC_M_SIZE , IntTab(1), false, false));
	maptabc.add(Segment("reset", RESET_BASE, RESET_SIZE, IntTab(1), false, false));
	maptabc.add(Segment("excep", EXCEP_BASE, EXCEP_SIZE, IntTab(1), false, false));
	maptabc.add(Segment("text" , TEXT_BASE , TEXT_SIZE , IntTab(1), false, false));

	std::cout << maptabc << std::endl;
	
	soclib::common::MappingTable maptabx(40, IntTab(16), IntTab(8), 0x00300000);
	maptabx.add(Segment("xram" , MC_M_BASE , MC_M_SIZE , IntTab(0), false));
	maptabx.add(Segment("reset", RESET_BASE, RESET_SIZE, IntTab(0), false));
	maptabx.add(Segment("excep", EXCEP_BASE, EXCEP_SIZE, IntTab(0), false));
	maptabx.add(Segment("text" , TEXT_BASE , TEXT_SIZE , IntTab(0), false));
	
	std::cout << maptabx << std::endl;

	// Signals
	sc_clock	signal_clk("clk");
	sc_signal<bool> signal_resetn("resetn");
   
	sc_signal<bool> signal_proc0_it0("proc0_it0"); 
	sc_signal<bool> signal_proc0_it1("proc0_it1"); 
	sc_signal<bool> signal_proc0_it2("proc0_it2"); 
	sc_signal<bool> signal_proc0_it3("proc0_it3"); 
	sc_signal<bool> signal_proc0_it4("proc0_it4"); 
	sc_signal<bool> signal_proc0_it5("proc0_it5");
/*
	sc_signal<bool> signal_proc1_it0("proc1_it0"); 
	sc_signal<bool> signal_proc1_it1("proc1_it1"); 
	sc_signal<bool> signal_proc1_it2("proc1_it2"); 
	sc_signal<bool> signal_proc1_it3("proc1_it3"); 
	sc_signal<bool> signal_proc1_it4("proc1_it4"); 
	sc_signal<bool> signal_proc1_it5("proc1_it5");

	sc_signal<bool> signal_proc2_it0("proc2_it0"); 
	sc_signal<bool> signal_proc2_it1("proc2_it1"); 
	sc_signal<bool> signal_proc2_it2("proc2_it2"); 
	sc_signal<bool> signal_proc2_it3("proc2_it3"); 
	sc_signal<bool> signal_proc2_it4("proc2_it4"); 
	sc_signal<bool> signal_proc2_it5("proc2_it5");

	sc_signal<bool> signal_proc3_it0("proc3_it0"); 
	sc_signal<bool> signal_proc3_it1("proc3_it1"); 
	sc_signal<bool> signal_proc3_it2("proc3_it2"); 
	sc_signal<bool> signal_proc3_it3("proc3_it3"); 
	sc_signal<bool> signal_proc3_it4("proc3_it4"); 
	sc_signal<bool> signal_proc3_it5("proc3_it5");
*/
	soclib::caba::VciSignals<vci_param> signal_vci_ini_d_proc0("vci_ini_d_proc0");
	soclib::caba::VciSignals<vci_param> signal_vci_ini_c_proc0("vci_ini_c_proc0");
	soclib::caba::VciSignals<vci_param> signal_vci_tgt_c_proc0("vci_tgt_proc0");
/*
	soclib::caba::VciSignals<vci_param> signal_vci_ini_d_proc1("vci_ini_d_proc1");
	soclib::caba::VciSignals<vci_param> signal_vci_ini_c_proc1("vci_ini_c_proc1");
	soclib::caba::VciSignals<vci_param> signal_vci_tgt_c_proc1("vci_tgt_proc1");

	soclib::caba::VciSignals<vci_param> signal_vci_ini_d_proc2("vci_ini_d_proc2");
	soclib::caba::VciSignals<vci_param> signal_vci_ini_c_proc2("vci_ini_c_proc2");
	soclib::caba::VciSignals<vci_param> signal_vci_tgt_c_proc2("vci_tgt_proc2");

	soclib::caba::VciSignals<vci_param> signal_vci_ini_d_proc3("vci_ini_d_proc3");
	soclib::caba::VciSignals<vci_param> signal_vci_ini_c_proc3("vci_ini_c_proc3");
	soclib::caba::VciSignals<vci_param> signal_vci_tgt_c_proc3("vci_tgt_proc3");
*/
	soclib::caba::VciSignals<vci_param> signal_vci_tgt_tty("vci_tgt_tty");

	soclib::caba::VciSignals<vci_param> signal_vci_tgt_xram("vci_tgt_xram");

	soclib::caba::VciSignals<vci_param> signal_vci_ixr_memc("vci_ixr_memc");
	soclib::caba::VciSignals<vci_param> signal_vci_ini_memc("vci_ini_memc");
	soclib::caba::VciSignals<vci_param> signal_vci_tgt_d_memc("vci_tgt_memc");
	soclib::caba::VciSignals<vci_param> signal_vci_tgt_c_memc("vci_tgt_cleanup_memc");

	sc_signal<bool> signal_tty_irq0("signal_tty_irq0");
/* 
	sc_signal<bool> signal_tty_irq1("signal_tty_irq1"); 
	sc_signal<bool> signal_tty_irq2("signal_tty_irq2"); 
	sc_signal<bool> signal_tty_irq3("signal_tty_irq3"); 
*/
        sc_signal<bool> signal_stuck0("signal_stuck0");

	soclib::common::Loader loader("soft/bin.soft");

        // Components
	soclib::caba::VciCcXCacheWrapperMulti<vci_param, proc_iss > 
	proc0("proc0", 0, maptabp, maptabc, IntTab(0),IntTab(0),IntTab(0),1,8,16,1,8,16,4,8,16);
/*
	soclib::caba::VciCcXCacheWrapperMulti<vci_param, proc_iss > 
	proc1("proc1", 1, maptabp, maptabc, IntTab(1),IntTab(1),IntTab(1),1,8,16,1,8,16,4,8,16);

	soclib::caba::VciCcXCacheWrapperMulti<vci_param, proc_iss > 
	proc2("proc2", 2, maptabp, maptabc, IntTab(2),IntTab(2),IntTab(2),1,8,16,1,8,16,4,8,16);

	soclib::caba::VciCcXCacheWrapperMulti<vci_param, proc_iss > 
	proc3("proc3", 3, maptabp, maptabc, IntTab(3),IntTab(3),IntTab(3),1,8,16,1,8,16,4,8,16);
*/

	soclib::caba::VciSimpleRam<vci_param> 
	xram("xram", IntTab(0), maptabx, loader);

	soclib::caba::VciMemCacheV1<vci_param> 
	memc("memc",maptabp,maptabc,maptabx,IntTab(0),IntTab(1),IntTab(0), IntTab(1),4,16,16);
	
	soclib::caba::VciMultiTty<vci_param> 
	tty("tty",IntTab(1),maptabp,"tty0", NULL);

  	soclib::caba::VciVgmn<vci_param> 
	ringd("ringd",maptabp, 1, 2, 2, 2);

  	soclib::caba::VciVgmn<vci_param> 
	ringc("ringc",maptabc, 2, 2, 2, 2);

  	soclib::caba::VciVgmn<vci_param> 
	ringx("ringx",maptabx, 1, 1, 2, 2);

	// Net-List
 
	proc0.p_clk(signal_clk);  
	proc0.p_resetn(signal_resetn);  
	proc0.p_irq[0](signal_proc0_it0); 
	proc0.p_irq[1](signal_proc0_it1); 
	proc0.p_irq[2](signal_proc0_it2); 
	proc0.p_irq[3](signal_proc0_it3); 
	proc0.p_irq[4](signal_proc0_it4); 
	proc0.p_irq[5](signal_proc0_it5); 
	proc0.p_vci_ini_d(signal_vci_ini_d_proc0);
	proc0.p_vci_ini_c(signal_vci_ini_c_proc0);
	proc0.p_vci_tgt_c(signal_vci_tgt_c_proc0);
/*
	proc1.p_clk(signal_clk);  
	proc1.p_resetn(signal_resetn);  
	proc1.p_irq[0](signal_proc1_it0); 
	proc1.p_irq[1](signal_proc1_it1); 
	proc1.p_irq[2](signal_proc1_it2); 
	proc1.p_irq[3](signal_proc1_it3); 
	proc1.p_irq[4](signal_proc1_it4); 
	proc1.p_irq[5](signal_proc1_it5); 
	proc1.p_vci_ini_d(signal_vci_ini_d_proc1);
	proc1.p_vci_ini_c(signal_vci_ini_c_proc1);
	proc1.p_vci_tgt_c(signal_vci_tgt_c_proc1);

	proc2.p_clk(signal_clk);  
	proc2.p_resetn(signal_resetn);  
	proc2.p_irq[0](signal_proc2_it0); 
	proc2.p_irq[1](signal_proc2_it1); 
	proc2.p_irq[2](signal_proc2_it2); 
	proc2.p_irq[3](signal_proc2_it3); 
	proc2.p_irq[4](signal_proc2_it4); 
	proc2.p_irq[5](signal_proc2_it5); 
	proc2.p_vci_ini_d(signal_vci_ini_d_proc2);
	proc2.p_vci_ini_c(signal_vci_ini_c_proc2);
	proc2.p_vci_tgt_c(signal_vci_tgt_c_proc2);

	proc3.p_clk(signal_clk);  
	proc3.p_resetn(signal_resetn);  
	proc3.p_irq[0](signal_proc3_it0); 
	proc3.p_irq[1](signal_proc3_it1); 
	proc3.p_irq[2](signal_proc3_it2); 
	proc3.p_irq[3](signal_proc3_it3); 
	proc3.p_irq[4](signal_proc3_it4); 
	proc3.p_irq[5](signal_proc3_it5); 
	proc3.p_vci_ini_d(signal_vci_ini_d_proc3);
	proc3.p_vci_ini_c(signal_vci_ini_c_proc3);
	proc3.p_vci_tgt_c(signal_vci_tgt_c_proc3);
*/

	tty.p_clk(signal_clk);
	tty.p_resetn(signal_resetn);
	tty.p_vci(signal_vci_tgt_tty);
	tty.p_irq[0](signal_tty_irq0);
/* 
	tty.p_irq[1](signal_tty_irq1); 
	tty.p_irq[2](signal_tty_irq2); 
	tty.p_irq[3](signal_tty_irq3); 
*/
	memc.p_clk(signal_clk);
	memc.p_resetn(signal_resetn);
	memc.p_vci_tgt(signal_vci_tgt_d_memc);
	memc.p_vci_tgt_cleanup(signal_vci_tgt_c_memc);
	memc.p_vci_ini(signal_vci_ini_memc);
	memc.p_vci_ixr(signal_vci_ixr_memc);

	xram.p_clk(signal_clk);
        xram.p_resetn(signal_resetn);
	xram.p_vci(signal_vci_tgt_xram);
	
	ringd.p_clk(signal_clk);
	ringd.p_resetn(signal_resetn);
	ringd.p_to_initiator[0](signal_vci_ini_d_proc0);
/*
	ringd.p_to_initiator[1](signal_vci_ini_d_proc1);
	ringd.p_to_initiator[2](signal_vci_ini_d_proc2);
	ringd.p_to_initiator[3](signal_vci_ini_d_proc3);
*/
	ringd.p_to_target[1](signal_vci_tgt_tty);
	ringd.p_to_target[0](signal_vci_tgt_d_memc);

	ringc.p_clk(signal_clk);
	ringc.p_resetn(signal_resetn);
	ringc.p_to_initiator[0](signal_vci_ini_c_proc0);
/*
	ringc.p_to_initiator[1](signal_vci_ini_c_proc1);
	ringc.p_to_initiator[2](signal_vci_ini_c_proc2);
	ringc.p_to_initiator[3](signal_vci_ini_c_proc3);
*/
	ringc.p_to_initiator[1](signal_vci_ini_memc);
	ringc.p_to_target[0](signal_vci_tgt_c_proc0);
/*
	ringc.p_to_target[1](signal_vci_tgt_c_proc1);
	ringc.p_to_target[2](signal_vci_tgt_c_proc2);
	ringc.p_to_target[3](signal_vci_tgt_c_proc3);
*/
	ringc.p_to_target[1](signal_vci_tgt_c_memc);

	ringx.p_clk(signal_clk);
	ringx.p_resetn(signal_resetn);
	ringx.p_to_initiator[0](signal_vci_ixr_memc);
	ringx.p_to_target[0](signal_vci_tgt_xram);

	int ncycles = 1000000000;

	if (argc == 2) {
		ncycles = std::atoi(argv[1]);
	}

	sc_start(sc_core::sc_time(0, SC_NS));
	signal_resetn = false;
        signal_stuck0 = false;

	sc_start(sc_core::sc_time(1, SC_NS));
	signal_resetn = true;

	for (int i = 0; i < ncycles ; i++) 
        {
                if ( i%10000 == 0 ) 
                {
			proc0.printStatistics();
			proc1.printStatistics();
			proc2.printStatistics();
			proc3.printStatistics();
			memc.print_stats();
		}
/*

		std::cout << "***************************************** cycle = " << std::dec << i 
                          << " *************************************************" << std::endl;  
		proc0.printTrace(0);
		proc1.printTrace(0);
		proc2.printTrace(0);
		proc3.printTrace(0);
            std::cout << " proc0.cmdval  = " << signal_vci_ini_c_proc0.cmdval << std::endl;
            std::cout << " proc0.cmdack  = " << signal_vci_ini_c_proc0.cmdack << std::endl;
            std::cout << " proc0.address = " << signal_vci_ini_c_proc0.address << std::endl;
            std::cout << " proc0.wdata   = " << signal_vci_ini_c_proc0.wdata << std::endl;
            std::cout << " proc0.eop     = " << signal_vci_ini_c_proc0.eop << std::endl;
            std::cout << " proc0.len     = " << signal_vci_ini_c_proc0.plen << std::endl;
            std::cout << " proc0.rspval  = " << signal_vci_ini_c_proc0.rspval << std::endl;
            std::cout << " proc0.rspack  = " << signal_vci_ini_c_proc0.rspack << std::endl;
            std::cout << " proc0.reop    = " << signal_vci_ini_c_proc0.reop << std::endl;
            std::cout << " proc0.rdata   = " << signal_vci_ini_c_proc0.rdata << std::endl;
            std::cout << " proc0.rerror  = " << signal_vci_ini_c_proc0.rerror << std::endl;
            std::cout << " memc.cmdval   = " << signal_vci_tgt_c_memc.cmdval << std::endl;
            std::cout << " memc.cmdack   = " << signal_vci_tgt_c_memc.cmdack << std::endl;
            std::cout << " memc.address  = " << signal_vci_tgt_c_memc.address << std::endl;
            std::cout << " memc.wdata    = " << signal_vci_tgt_c_memc.wdata << std::endl;
            std::cout << " memc.eop      = " << signal_vci_tgt_c_memc.eop << std::endl;
            std::cout << " memc.len      = " << signal_vci_tgt_c_memc.plen << std::endl;
            std::cout << " memc.rspval   = " << signal_vci_tgt_c_memc.rspval << std::endl;
            std::cout << " memc.rspack   = " << signal_vci_tgt_c_memc.rspack << std::endl;
            std::cout << " memc.reop     = " << signal_vci_tgt_c_memc.reop << std::endl;
            std::cout << " memc.rdata    = " << signal_vci_tgt_c_memc.rdata << std::endl;
            std::cout << " memc.rerror   = " << signal_vci_tgt_c_memc.rerror << std::endl;
*/

		sc_start(sc_core::sc_time(1, SC_NS));
	}

	return EXIT_SUCCESS;
}

int sc_main (int argc, char *argv[])
{
	try {
		return _main(argc, argv);
	} catch (std::exception &e) {
		std::cout << e.what() << std::endl;
	} catch (...) {
		std::cout << "Unknown exception occured" << std::endl;
		throw;
	}
	return 1;
}
