/*****************************************************************************
 *
 * Copyright (c) 2018-19, Eduard Gerlits, Paula Herber, Josef Treus
 * Westfälische Wilhelms-Universität Münster, AG Embedded Systems 
 * All rights reserved.
 *
 * This file is part of SysCSec
 *
 * SysCSec is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 *
 * SysCSec is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with SysCSec.  If not, see <http://www.gnu.org/licenses/>.
 *
 *
 *  Please report any problems or bugs to: sysc.sec@uni-muenster.de
 *
 ****************************************************************************/

#include <iostream>
#include <systemc.h>
#include "commons.h"

SC_MODULE(InputIOModule){

	// clock
	sc_port<sc_clock> clk;

	sc_fifo_out<sc_uint<3> > io_control_in;
	sc_fifo_out<sc_biguint<160> > data_in;
	sc_fifo_out<sc_biguint<960> > p_data_in;

	sc_fifo_in<sc_uint<3> > io_status;
	sc_fifo_in<sc_biguint<160> > data_out;
	sc_fifo_in<sc_biguint<960> > p_data_out;  

	sc_biguint<960> curr_p_data_out_val;

	void input_test(sc_uint<3> io_control_in_param, sc_biguint<160> data_in_param, sc_biguint<960> p_data_in_param){
		std::cout << "[" << sc_time_stamp() << "]: " << "Write io_control_in: " << io_control_in_param << std::endl;
		io_control_in->write(io_control_in_param);		
		std::cout << "[" << sc_time_stamp() << "]: " << "Write data_in: " << data_in_param << std::endl;
		data_in->write(data_in_param);
		std::cout << "[" << sc_time_stamp() << "]: " << "Write p_data_in: " << p_data_in_param << std::endl;
		p_data_in->write(p_data_in_param);
		wait();

		sc_uint<3> io_status_val=_IO_PROCESSING_;
		sc_biguint<160> data_out_val;
		sc_biguint<960> p_data_out_val;


		while(io_status_val!=_IO_FINISHED_){
			io_status_val=io_status->read();
			std::cout << "[" << sc_time_stamp() << "]: " << "Computing ..." << std::endl;
			wait();
		}
		//std::cout << "Num_available in data_out: " << data_out->num_available() << std::endl;
		//std::cout << "Num_available in p_data_out: " << p_data_out->num_available() << std::endl;

		data_out_val = data_out->read();
		p_data_out_val = p_data_out->read();
		
		std::cout << "[" << sc_time_stamp() << "]: " << "io_status: " << io_status_val << std::endl;
		std::cout << "[" << sc_time_stamp() << "]: " << "data_out: " << data_out_val << std::endl;
		std::cout << "[" << sc_time_stamp() << "]: " << "p_data_out: " << p_data_out_val  << std::endl;
		if(io_control_in_param==_IO_GET_DATA_){
			write_p_data_out_val(curr_p_data_out_val, p_data_out_val);
			// std::cout << "curr_p_data_out_val: " << curr_p_data_out_val << std::endl;
		}
		std::cout << std::endl;
	}

	void write_p_data_out_val(sc_biguint<960> &val, sc_biguint<960> p_data_out_val){
		val = p_data_out_val;
	}

	void exe(){
	  //		sc_time t1(200, "SC_MS");
		//wait(t1);
		input_test(_IO_PACK_DATA_, 81134,0);
		//wait(t1);
		input_test(_IO_PACK_DATA_, 26632,0);
		//wait(t1);
		input_test(_IO_PACK_DATA_, 88432,0);
		//wait(t1);
		input_test(_IO_PACK_DATA_, 99007,0);
		//wait(t1);
		input_test(_IO_PACK_DATA_, 87639,0);
		//wait(t1);
		input_test(_IO_PACK_DATA_, 22865,0);
		//wait(t1);
		input_test(_IO_GET_DATA_, 0,0);
		//wait(t1);
		//std::cout << "curr_p_data_out_val: " << curr_p_data_out_val << std::endl;
		input_test(_IO_SET_DATA_, 0, curr_p_data_out_val);
		//wait(t1);
		input_test(_IO_UNPACK_DATA_, 0,0);
		//wait(t1);
		input_test(_IO_UNPACK_DATA_, 0,0);
		//wait(t1);
		input_test(_IO_UNPACK_DATA_, 0,0);
		//wait(t1);
		input_test(_IO_UNPACK_DATA_, 0,0);
		//wait(t1);
		input_test(_IO_UNPACK_DATA_, 0,0);
		//wait(t1);
		input_test(_IO_UNPACK_DATA_, 0,0);
		//wait(t1);
		sc_stop();
	}

	SC_CTOR(InputIOModule){
		SC_THREAD(exe);
		sensitive << clk;
	}

};
