/*2013-01-18T16:21:42-01:00*/

/********************************************************************
 * graf_mdl_alignement.c
 * 
 * generated by: plcgen 2.3.253
 ********************************************************************/

#include "cb_comp.h"
#include "iec_1131.h"

#include "graf_mdl_alignement.h"


/**************************** Variables *****************************/

CB_Index graf_mdl_alignement__num = 0;
CB_Mem_Bool *graf_mdl_alignement_D_align;
CB_Mem_Bool *graf_mdl_alignement_Cpt_align;
CB_Mem_Bool *graf_mdl_alignement_Verin_sorti;
CB_Mem_Bool *graf_mdl_alignement_Verin_rentre;
CB_Mem_Bool *graf_mdl_alignement_Def_verin;
CB_Mem_Bool *graf_mdl_alignement_acq;
CB_Mem_Bool *graf_mdl_alignement_AU;
CB_Mem_Int *graf_mdl_alignement_TTL;
CB_Mem_Int *graf_mdl_alignement_t1;
CB_Mem_Int *graf_mdl_alignement_t2;
CB_Mem_Bool *graf_mdl_alignement_Dem_AOS;
CB_Mem_Bool *graf_mdl_alignement_Dem_AOR;
CB_Mem_Bool *graf_mdl_alignement_Def_Align;
CB_Mem_Bool *graf_mdl_alignement_OK_align;
CB_Mem_Bool *graf_mdl_alignement__X_10;
CB_Mem_Bool *graf_mdl_alignement__X_11;
CB_Mem_Bool *graf_mdl_alignement__X_12;
CB_Mem_Bool *graf_mdl_alignement__X_20;
CB_Mem_Bool *graf_mdl_alignement__X_21;
CB_Mem_Bool *graf_mdl_alignement__X_22;
CB_Mem_Bool *graf_mdl_alignement__X_23;
CB_Mem_Bool *graf_mdl_alignement__FORCING_G2;
CB_Mem_Bool *graf_mdl_alignement__X_12_PREV;
CB_Mem_Bool *graf_mdl_alignement__X_21_PREV;
CB_Mem_Bool *graf_mdl_alignement__X_23_PREV;

/**************************** Variables *****************************/

#define D_align (graf_mdl_alignement_D_align->CB_current_value)
#define Cpt_align (graf_mdl_alignement_Cpt_align->CB_current_value)
#define Verin_sorti (graf_mdl_alignement_Verin_sorti->CB_current_value)
#define Verin_rentre (graf_mdl_alignement_Verin_rentre->CB_current_value)
#define Def_verin (graf_mdl_alignement_Def_verin->CB_current_value)
#define acq (graf_mdl_alignement_acq->CB_current_value)
#define AU (graf_mdl_alignement_AU->CB_current_value)
#define TTL(i) (graf_mdl_alignement_TTL[i+1].CB_current_value)
#define t1(i) (graf_mdl_alignement_t1[i+1].CB_current_value)
#define t2 (graf_mdl_alignement_t2->CB_current_value)
#define Dem_AOS (graf_mdl_alignement_Dem_AOS->CB_current_value)
#define Dem_AOR (graf_mdl_alignement_Dem_AOR->CB_current_value)
#define Def_Align (graf_mdl_alignement_Def_Align->CB_current_value)
#define OK_align (graf_mdl_alignement_OK_align->CB_current_value)
#define _X_10 (graf_mdl_alignement__X_10->CB_current_value)
#define _X_11 (graf_mdl_alignement__X_11->CB_current_value)
#define _X_12 (graf_mdl_alignement__X_12->CB_current_value)
#define _X_20 (graf_mdl_alignement__X_20->CB_current_value)
#define _X_21 (graf_mdl_alignement__X_21->CB_current_value)
#define _X_22 (graf_mdl_alignement__X_22->CB_current_value)
#define _X_23 (graf_mdl_alignement__X_23->CB_current_value)
#define _FORCING_G2 (graf_mdl_alignement__FORCING_G2->CB_current_value)
#define _X_12_PREV (graf_mdl_alignement__X_12_PREV->CB_current_value)
#define _X_21_PREV (graf_mdl_alignement__X_21_PREV->CB_current_value)
#define _X_23_PREV (graf_mdl_alignement__X_23_PREV->CB_current_value)


/************************ Components offsets ************************/


/********************* Initialization function **********************/

int _sfp__graf_mdl_alignement_init(void)
{
	(graf_mdl_alignement_TTL+3)->CB_current_value = 100000; /*TTL[2]*/
	(self.Bool+0)->CB_current_value = 1; /*_X_10*/
	(self.Bool+3)->CB_current_value = 1; /*_X_20*/
	self.Bool+=11;


	return 0;
}


/************************ Behavior function *************************/

int sfp__graf_mdl_alignement(void)
{
	if (exec_term_node(self_num)) {

		/*********************** Internals variables ************************/

		graf_mdl_alignement__X_10 = self.Bool+0;
		graf_mdl_alignement__X_11 = self.Bool+1;
		graf_mdl_alignement__X_12 = self.Bool+2;
		graf_mdl_alignement__X_20 = self.Bool+3;
		graf_mdl_alignement__X_21 = self.Bool+4;
		graf_mdl_alignement__X_22 = self.Bool+5;
		graf_mdl_alignement__X_23 = self.Bool+6;
		graf_mdl_alignement__FORCING_G2 = self.Bool+7;
		graf_mdl_alignement__X_12_PREV = self.Bool+8;
		graf_mdl_alignement__X_21_PREV = self.Bool+9;
		graf_mdl_alignement__X_23_PREV = self.Bool+10;

		/********************* Pre-conditions execution *********************/

		/*no Pre-conditions*/

		/************************ Behavior execution ************************/

		{
			unsigned char _Y_6;
			unsigned char _Y_5;
			unsigned char _Y_4;
			unsigned char _Y_7;
			unsigned char _Y_22;
			unsigned char _Y_21;
			unsigned char _Y_20;
			unsigned char _Y_19;
			_FORCING_G2 = 0;
			/*pre-SFC*/
			/*transitions G1*/
			_Y_6 = _X_10 && _X_20;
			_Y_5 = _X_11 && Def_Align;
			_Y_4 = _X_12 && !Def_Align;
			_Y_7 = _X_10 && (!_Y_6 && Def_Align);
			/*transitions G2*/
			_Y_22 = _X_20 && D_align;
			_Y_21 = _X_21 && Verin_sorti;
			_Y_20 = _X_22 && Cpt_align;
			_Y_19 = _X_23 && Verin_rentre;
			/*evolutions G1*/
			if (_Y_6) {
				_X_10 = 0;
			}
			if (_Y_5) {
				_X_11 = 0;
			}
			if (_Y_4) {
				_X_12 = 0;
			}
			if (_Y_7) {
				_X_10 = 0;
			}
			if (_Y_6) {
				_X_11 = 1;
			}
			if (_Y_5) {
				_X_12 = 1;
			}
			if (_Y_4) {
				_X_10 = 1;
			}
			if (_Y_7) {
				_X_12 = 1;
			}
			/*timers G1*/
			/*actions G1*/
			if (CB_edge_bool(_X_12, _X_12_PREV) < 0) {
				Dem_AOR = 0;
			}
			if (_X_10) {
				_FORCING_G2 = 1;
				_X_20 = 1;
				_X_21 = 0;
				_X_22 = 0;
				_X_23 = 0;
			}
			if (_X_12) {
				_FORCING_G2 = 1;
				_X_20 = 0;
				_X_21 = 0;
				_X_22 = 0;
				_X_23 = 0;
				Dem_AOR = 1;
			}
			/*evolutions G2*/
			if (!_FORCING_G2) {
				if (_Y_22) {
					_X_20 = 0;
				}
				if (_Y_21) {
					_X_21 = 0;
				}
				if (_Y_20) {
					_X_22 = 0;
				}
				if (_Y_19) {
					_X_23 = 0;
				}
				if (_Y_22) {
					_X_21 = 1;
				}
				if (_Y_21) {
					_X_22 = 1;
				}
				if (_Y_20) {
					_X_23 = 1;
				}
				if (_Y_19) {
					_X_20 = 1;
				}
			}
			/*timers G2*/
			/*actions G2*/
			if (CB_edge_bool(_X_21, _X_21_PREV) < 0) {
				Dem_AOS = 0;
			}
			if (CB_edge_bool(_X_23, _X_23_PREV) < 0) {
				Dem_AOR = 0;
			}
			if (_X_21) {
				Dem_AOS = 1;
			}
			if (_X_23) {
				Dem_AOR = 1;
			}
			/*post-SFC*/
			if ((AU || Def_verin) | ton_Q(_X_22, graf_mdl_alignement_TTL, graf_mdl_alignement_t1, &t2)) {
				Def_Align = 1;
			}
			if ((acq && !Def_verin) && !AU) {
				Def_Align = 0;
			}
			OK_align = _X_20 && Cpt_align;
			_X_12_PREV = _X_12;
			_X_23_PREV = _X_23;
			_X_21_PREV = _X_21;
		}

		if(nb_alloc > 0) {
			free_all_alloc();
		}

		CB_post_bool(graf_mdl_alignement_Dem_AOS);
		CB_post_bool(graf_mdl_alignement_Dem_AOR);
		CB_post_bool(graf_mdl_alignement_Def_Align);
		CB_post_bool(graf_mdl_alignement_OK_align);

		/******************** Post-conditions execution *********************/

		/*no Post-conditions*/
	}

	/*************************** self update ****************************/

	self.Bool += 11;

	return 0;
}

