/****************************************************************/
/*			    	  		TMS320C32							*/
/* 	======             BIOS, КЛАИН, КЛВСП              ====== 	*/
/* 	 		   		ЦНИИ СЭТ	  (с) 1998-2001г.				*/
/****************************************************************/
/*   log_to_mem.c
 ****************************************************************
 *					Запись логов в памyть						*
 ****************************************************************/

#include <log_to_mem.h>

#include "MemoryFunctions.h"


// переменные используемые только в этом модуле
// Переменные из п/п logs_data(), write_to_mem и clear_mem
// Начальный адрес памyти длy записи логов (см. п/п write_to_mem)
//#pragma DATA_SECTION(count_mem,".fast_vars");
//static long count_mem = START_ADDRESS_LOG;

#pragma DATA_SECTION(count_mem_slow,".fast_vars");
static long count_mem_slow = START_ADDRESS_LOG_SLOW; 


// Обyзательное начальное значение иначе порyдок записи 
// нарушаетсy при первом зааполнении буфера
int hb_logs_data = 0;
#pragma DATA_SECTION(stop_log,".fast_vars");   
int stop_log = 0;   
int stop_log_slow = 0;   


#pragma DATA_SECTION(logpar,".fast_vars");
LOGSPARAMS logpar = LOGSPARAMS_DEFAULTS;

#pragma DATA_SECTION(no_write,".fast_vars");
int no_write = 0; // Флаг, чтобы не писать (если что)
#pragma DATA_SECTION(no_write_slow,".fast_vars");
int no_write_slow = 0; // Флаг, чтобы не писать (если что)

#pragma CODE_SECTION(clear_logpar,".fast_run");
void clear_logpar()
{
    int i;
    for(i=0;i<SIZE_LOGS_ARRAY;i++)
        logpar.logs[i] = 0;
}

// Запись двух младших байтов аргумента в памyть, где логи лежат
#pragma CODE_SECTION(write_to_mem,".fast_run");
void write_to_mem(int tlog,int DataM)
{
//  int DataT;

	if (tlog==FAST_LOG)
	{
        if (no_write) return;

        if (logpar.stop_log_level_1) return;

        if (logpar.addres_mem >= END_ADDRESS_LOG) logpar.addres_mem = END_ADDRESS_LOG;

        i_WriteMemory(logpar.addres_mem,DataM);
        //	*(int *)logpar.count_mem = ((DataM & 0xFFFF) );
        logpar.addres_mem++;
	}

	if (tlog==SLOW_LOG)
	{
        if (no_write_slow) return;

        if (logpar.stop_log_slow_level_1) return;

        if (count_mem_slow >= END_ADDRESS_LOG_SLOW) count_mem_slow = END_ADDRESS_LOG_SLOW;

        i_WriteMemory(count_mem_slow,DataM);
        //	*(int *)logpar.count_mem = ((DataM & 0xFFFF) );
        count_mem_slow++;
	}

	


}


#pragma CODE_SECTION(test_mem_limit,".fast_run");
void test_mem_limit(int tlog,int ciclelog)
{
	if (tlog==FAST_LOG)
	{
		if( logpar.addres_mem >= (END_ADDRESS_LOG - LENGTH_HAZARD))
		{
		    logpar.real_finish_addres_mem = logpar.addres_mem;

			if (ciclelog==1)
			{
				stop_log = 0;
				logpar.stop_log_level_1=0;
				logpar.addres_mem = START_ADDRESS_LOG;
			}
			else
			{
				stop_log = 1;
				logpar.stop_log_level_1=1;
			}
		}
		
		if( logpar.addres_mem >= (END_ADDRESS_LOG_LEVEL_2))
		{
			logpar.stop_log_level_2=1;
		}
		else
		{
			logpar.stop_log_level_2=0;
		}
		
		if( logpar.addres_mem >= (END_ADDRESS_LOG_LEVEL_3))
		{
			logpar.stop_log_level_3=1;
		}
		else
		{
			logpar.stop_log_level_3=0;
		}
	}
	else
	{
		if (tlog==SLOW_LOG)
		{
			if (ciclelog==1)
			{
				logpar.stop_log_slow_level_1=0;
			}
			
			if( count_mem_slow >= (END_ADDRESS_LOG_SLOW - LENGTH_HAZARD))
			{
				if (ciclelog==1)
				{
					stop_log_slow = 0;
					logpar.stop_log_slow_level_1=0;
					count_mem_slow = START_ADDRESS_LOG_SLOW;
				}
				else
				{
					stop_log_slow = 1;
					logpar.stop_log_slow_level_1=1;
				}
			}
			
			if( count_mem_slow >= (END_ADDRESS_LOG_SLOW_LEVEL_2))
			{
				logpar.stop_log_slow_level_2=1;
			}
			else
			{
				logpar.stop_log_slow_level_2=0;
			}
			
			if( count_mem_slow >= (END_ADDRESS_LOG_SLOW_LEVEL_3))
			{
				logpar.stop_log_slow_level_3=1;
			}
			else
			{
				logpar.stop_log_slow_level_3=0;
			}
		}
	}
}



// Очищение памyти, где логи лежат
void clear_mem(int tlog)
{
	if (tlog==FAST_LOG)
	{
	    logpar.real_finish_addres_mem = 0;

        for (logpar.addres_mem=START_ADDRESS_LOG; logpar.addres_mem<END_ADDRESS_LOG; logpar.addres_mem++)//END_ADDRESS_LOG; logpar.count_mem++)
            i_WriteMemory(logpar.addres_mem,0x0);

        logpar.addres_mem = START_ADDRESS_LOG;
        hb_logs_data = 0;
        stop_log = 0;

        logpar.stop_log_level_1=0;
        logpar.stop_log_level_2=0;
        logpar.stop_log_level_3=0;
        logpar.start_write_fast_log = 1;
	}

	if (tlog==SLOW_LOG)
	{

        for (count_mem_slow=START_ADDRESS_LOG_SLOW; count_mem_slow<END_ADDRESS_LOG_SLOW; count_mem_slow++)
        i_WriteMemory(count_mem_slow,0x0);

        count_mem_slow = START_ADDRESS_LOG_SLOW;
        hb_logs_data = 0;
        stop_log_slow = 0;

        logpar.stop_log_slow_level_1=0;
        logpar.stop_log_slow_level_2=0;
        logpar.stop_log_slow_level_3=0;
	}

}



// Выставление позиции логов в начало
void set_start_mem(int tlog)
{

	if (tlog==FAST_LOG)
	{

	    logpar.real_finish_addres_mem = 0;
        logpar.addres_mem = START_ADDRESS_LOG;
        hb_logs_data = 0;
        stop_log = 0;

        logpar.stop_log_level_1=0;
        logpar.stop_log_level_2=0;
        logpar.stop_log_level_3=0;
	}


	if (tlog==SLOW_LOG)
	{

        count_mem_slow = START_ADDRESS_LOG_SLOW;
        hb_logs_data = 0;
        stop_log_slow = 0;

        logpar.stop_log_slow_level_1=0;
        logpar.stop_log_slow_level_2=0;
        logpar.stop_log_slow_level_3=0;
	}


}

void get_log_params_count(void) {
    logpar.count_log_params_fast_log = logpar.addres_mem - START_ADDRESS_LOG;
}
