/*****************************************************************************
 *
 * 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 "MAP.h"


/*
behavioral model of a multiplexer
depending on selected opcode, the corresponding out port is switched through
*/
void MAPModule::mux_out(){
	
	if(opcode->read()==_OPCODE_ADD_){
		out->write(mod_add_out_sig);
	}
	else if(opcode->read()==_OPCODE_SUB_){
		out->write(mod_sub_out_sig);
	}
	else if(opcode->read()==_OPCODE_MUL_){
		out->write(mod_mul_out_sig);
	}
	else if(opcode->read()==_OPCODE_DIV_){
		out->write(mod_div_out_sig);
	}
	else if(opcode->read()==_OPCODE_NOOP_) {
		out->write(0);
	}
}

/*
behavioral model of a multiplexer
depending on selected opcode, the corresponding done port is switched through
*/
void MAPModule::mux_done(){

	if(opcode->read()==_OPCODE_ADD_){
		done->write(mod_add_done_sig.read());
	}
	else if(opcode->read()==_OPCODE_SUB_){
		done->write(mod_sub_done_sig.read());
	}
	else if(opcode->read()==_OPCODE_MUL_){
		done->write(mod_mul_done_sig.read());
	}
	else if(opcode->read()==_OPCODE_DIV_){
		done->write(mod_div_done_sig.read());
	}
	else if(opcode->read()==_OPCODE_NOOP_){
		done->write(false);
	}
}

/*
behavioral model of a multiplexer
depending on selected opcode, the corresponding en port is switched through
*/
void MAPModule::enable_decoder(){

	while(true){
		if(opcode->read()==_OPCODE_ADD_){
			mod_add_en_sig.write(true);
			wait();
			while(mod_add_done_sig.read()==false){
				wait();
			}
			mod_add_en_sig.write(false);
			while(mod_add_done_sig.read()==true){
				wait();
			}	
		}

		else if(opcode->read()==_OPCODE_SUB_){
			mod_sub_en_sig.write(true);
			wait();
			while(mod_sub_done_sig.read()==false){
				wait();
			}
			mod_sub_en_sig.write(false);
			while(mod_sub_done_sig.read()==true){
				wait();
			}
		}

		else if(opcode->read()==_OPCODE_MUL_){
			mod_mul_en_sig.write(true);
			wait();
			while(mod_mul_done_sig.read()==false){
				wait();
			}
			mod_mul_en_sig.write(false);
			while(mod_mul_done_sig.read()==true){
				wait();
			}
		}

		else if(opcode->read()==_OPCODE_DIV_){
			mod_div_en_sig.write(true);
			wait();
			while(mod_div_done_sig.read()==false){
				wait();
			}
			mod_div_en_sig.write(false);
			while(mod_div_done_sig.read()==true){
				wait();
			}		
		}

		else {
			mod_add_en_sig.write(false);
			while(mod_add_done_sig.read()==true){
				wait();
			}	
			mod_sub_en_sig.write(false);
			while(mod_sub_done_sig.read()==true){
				wait();
			}	
			mod_mul_en_sig.write(false);
			while(mod_mul_done_sig.read()==true){
				wait();
			}	
			mod_div_en_sig.write(false);
			while(mod_div_done_sig.read()==true){
				wait();
			}	
			wait();
		}
	}
}

