/*****************************************************************************
 *
 * 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(InputModule){

	// clock
	sc_in<bool> clk;

	// in
	sc_in<sc_biguint<160> > out;
	sc_in<bool> done;

	// out
	sc_out<sc_biguint<160> > x;
	sc_out<sc_biguint<160> > y;
	sc_out<sc_biguint<160> > m;
	sc_out<sc_uint<3> > opcode;

	void input_test(sc_biguint<160> param_x, sc_biguint<160> param_y, sc_biguint<160> param_m, sc_uint<3> param_opcode){

		std::cout << "[" << sc_time_stamp() << "]: " << "Write x value: " << param_x << std::endl;
		x->write(param_x);
		std::cout << "[" << sc_time_stamp() << "]: " << "Write y value: " << param_y << std::endl;
		y->write(param_y);
		std::cout << "[" << sc_time_stamp() << "]: " << "Write m value: " << param_m << std::endl;
		m->write(param_m);
		std::cout << "[" << sc_time_stamp() << "]: " << "Write opcode: " << param_opcode << std::endl;
		opcode->write(param_opcode);

		wait();

		while(done->read()==false){
			std::cout << "[" << sc_time_stamp() << "]: " << "Computing ..." << std::endl;
			wait();
		}

		
		sc_biguint<160> val = out->read();
		std::cout << "[" << sc_time_stamp() << "]: " << "Computed value: " << val << std::endl;
		opcode->write(_OPCODE_NOOP_);
		wait();	
	}

	void reset_test(){
		while(done->read()==true){
			wait();
		}
	}

	void exe(){

		input_test(13,3,17,_OPCODE_SUB_);
		reset_test();
		input_test(8,5,17,_OPCODE_SUB_);
		reset_test();
		input_test(0,9,17,_OPCODE_SUB_);
		reset_test();
		input_test(13,9,17,_OPCODE_ADD_);
		reset_test();
		input_test(2,7,17,_OPCODE_ADD_);
		sc_stop();
	}

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

};

