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

#include "log_to_memory.h"
#include "MemoryFunctions.h"
#include "log_params.h"

#include "global_time.h"

// переменные используемые только в этом модуле
// Переменные из п/п logs_data(), write_to_mem и clear_mem
// Начальный адрес памyти длy записи логов (см. п/п write_to_mem)

//#pragma DATA_SECTION(count_mem, ".fast_vars1");
//static unsigned long count_mem = START_ADDRESS_LOG; //static
//#pragma DATA_SECTION(count_mem_slow, ".fast_vars1");
//static unsigned long count_mem_slow; // =  START_ADDRESS_LOG_SLOW;//START_ADDRESS_LOG_SLOW; //static
//#pragma DATA_SECTION(count_mem_err, ".fast_vars1");
//static unsigned long count_mem_err; // =  START_ADDRESS_ERR_LOG;//START_ADDRESS_ERR_LOG;	//static
//#pragma DATA_SECTION(count_mem_init, ".fast_vars1");
//static unsigned long count_mem_init = ADDR_SIZE_ERR_LOW;

// Обyзательное начальное значение иначе порyдок записи
// нарушаетсy при первом зааполнении буфера
int hb_logs_data = 0;
//int stop_log = 0;
//int stop_log_slow = 0;
//int block_size_counter_slow = 0;

//int block_size_counter_fast = 0;

//#pragma DATA_SECTION(LOAG, ".fast_vars1");
//int LOAG[12];

#pragma DATA_SECTION(logsdata, ".fast_vars1");
LOGSDATA logsdata = LOGSDATA_DEFAULT;

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

//int size_fast_done = 0;
//int size_slow_done = 0;


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


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

	if (tlog == FAST_LOG)
	{
		if (!log_params.size_fast_done)
		{
		    log_params.BlockSizeErr++;
			return;
		}

		if (log_params.no_write_fast)
			return;
//		if (log_params.stop_log_level_1)
//			return;
		if (log_params.addres_mem >= log_params.end_address_log)
			log_params.addres_mem = log_params.end_address_log;

		WriteMemory(log_params.addres_mem, DataM);
		//	 Fast_log_written = 1;
		//	 if (one_block) block_size_counter++;
		//	*(int *)count_mem = ((DataM & 0xFFFF) );
		log_params.addres_mem++;

		return;
	}

    if (tlog == SLOW_LOG)
    {
        if (!log_params.size_slow_done)
        {
            log_params.BlockSizeSlow++;
            return;
        }

        if (log_params.no_write_slow)
            return;
//        if (logpar.stop_log_level_1)
//            return;
        if (log_params.addres_mem_slow >= log_params.end_address_log_slow)
            log_params.addres_mem_slow = log_params.end_address_log_slow;

        WriteMemory(log_params.addres_mem_slow, DataM);
        //   Fast_log_written = 1;
        //   if (one_block) block_size_counter++;
        //  *(int *)count_mem = ((DataM & 0xFFFF) );
        log_params.addres_mem_slow++;
        return;
    }

//	if (tlog == ALARM_SAVE_MEMORY)
//	{
//
//		if (!size_slow_done)
//		{
//			block_size_counter_slow++;
//			return;
//		}
//
//		if (no_write_slow)
//			return;
//
//		if (logpar.stop_log_slow_level_1)
//			return;
//
//		if (count_mem_slow >= log_params.end_address_save_log_memory)
//			count_mem_slow = log_params.end_address_save_log_memory;
//
//		WriteMemory(count_mem_slow, DataM);
//		count_mem_slow++;
//
//		return;
//	}

	/*	if (tlog==ERR_LOG)
	{
	 if (count_mem_err >= END_ADDRESS_ERR_LOG) count_mem_err = END_ADDRESS_ERR_LOG;
	  WriteMemory(count_mem_err,DataM);
	 count_mem_err++;
	}	
*/
}

#pragma CODE_SECTION(test_mem_limit, ".fast_run");
void test_mem_limit(int tlog, int ciclelog)
{

	if (tlog == FAST_LOG)
	{
		// block_size_counter = 0;
		if (log_params.addres_mem >= log_params.end_address_log)
		{
			log_params.log_cycle_done = 1;
		    if (ciclelog == 1)
			{
		        log_params.stop_log_fast = 0;
//				log_params.stop_log_level_1 = 0;
				log_params.addres_mem = log_params.start_address_log;
			}
			else
			{
			    log_params.stop_log_fast = 1;
//				log_params.stop_log_level_1 = 1;
			}
		}

		if (log_params.addres_mem >= (log_params.end_address_log_level_1))
		{
		    log_params.stop_log_level_1 = 1;
		}
		else
		{
		    log_params.stop_log_level_1 = 0;
		}

		if (log_params.addres_mem >= (log_params.end_address_log_level_2))
		{
		    log_params.stop_log_level_2 = 1;
		}
		else
		{
		    log_params.stop_log_level_2 = 0;
		}
		return;
	}

    if (tlog == SLOW_LOG)
    {
        // block_size_counter = 0;
        if (log_params.addres_mem_slow >= log_params.end_address_log_slow)
        {
            log_params.log_cycle_done_slow = 1;
            if (ciclelog == 1)
            {
                log_params.stop_log_slow = 0;
   ///             logpar.stop_log_level_1 = 0;
                log_params.addres_mem_slow= log_params.start_address_log_slow;
            }
            else
            {
                log_params.stop_log_slow = 1;
    //            logpar.stop_log_level_1 = 1;
            }

            if (log_params.addres_mem_slow >= (log_params.end_address_log_slow_level_1))
            {
                log_params.stop_log_slow_level_1= 1;
            }
            else
            {
                log_params.stop_log_slow_level_1 = 0;
            }

            if (log_params.addres_mem_slow >= (log_params.end_address_log_slow_level_2))
            {
                log_params.stop_log_slow_level_2 = 1;
            }
            else
            {
                log_params.stop_log_slow_level_2 = 0;
            }
        }

        return;
    }
//	if (tlog == ALARM_SAVE_MEMORY)
//	{
//		if (ciclelog == 1)
//		{
//			logpar.stop_log_slow_level_1 = 0;
//		}
//
//		if (count_mem_slow >= (log_params.end_address_save_log_memory - LENGTH_HAZARD))
//		{
//			if (ciclelog == 1)
//			{
//				stop_log_slow = 0;
//				logpar.stop_log_slow_level_1 = 0;
//				count_mem_slow = log_params.start_address_save_log_memory;
//			}
//			else
//			{
//				stop_log_slow = 1;
//				logpar.stop_log_slow_level_1 = 1;
//			}
//		}
//
//		if (count_mem_slow >= (log_params.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 >= (log_params.end_address_log_slow_level_3))
//		{
//			logpar.stop_log_slow_level_3 = 1;
//		}
//		else
//		{
//			logpar.stop_log_slow_level_3 = 0;
//		}
//
//		return;
//	}
}

// Очищение памyти, где логи пишутся
void clear_mem(int tlog)
{


	if (tlog == FAST_LOG)
	{

		for (log_params.addres_mem = log_params.start_address_log; log_params.addres_mem < log_params.end_address_log; log_params.addres_mem++)
			WriteMemory(log_params.addres_mem, 0x0);

		log_params.addres_mem = log_params.start_address_log;
		hb_logs_data = 0;
		log_params.stop_log_fast = 0;

		log_params.stop_log_level_1 = 0;
		log_params.stop_log_level_2 = 0;

		return;
	}

    if (tlog == SLOW_LOG)
    {

        for (log_params.addres_mem_slow = log_params.start_address_log_slow; log_params.addres_mem_slow < log_params.end_address_log_slow; log_params.addres_mem_slow++)
            WriteMemory(log_params.addres_mem_slow, 0x0);

        log_params.addres_mem_slow = log_params.start_address_log_slow;
        hb_logs_data = 0;
        log_params.stop_log_slow = 0;
        log_params.stop_log_slow_level_1 = 0;
        log_params.stop_log_slow_level_2 = 0;



        return;
    }
//	if (tlog == ALARM_SAVE_MEMORY)
//	{
//
//		for (count_mem_slow = log_params.start_address_save_log_memory; count_mem_slow < log_params.end_address_save_log_memory; count_mem_slow++)
//			WriteMemory(count_mem_slow, 0x0);
//
//		count_mem_slow = log_params.start_address_save_log_memory;
//		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;
//
//		return;
//	}

//	if (tlog == ERR_LOG)
//	{
//
//		for (count_mem_err = log_params.start_address_err_log; count_mem_err < log_params.end_address_err_log; count_mem_err++)
//			WriteMemory(count_mem_err, 0x0);
//
//		count_mem_err = log_params.start_address_err_log;
//		return;
//	}

//	if (tlog == INIT_LOG)
//	{
//		for (count_mem_init = ADDR_SIZE_ERR_LOW; count_mem_init <= END_ADDR_TIME_ERR_WRITE; count_mem_init++)
//			WriteMemory(count_mem_init, 0x0);
//
//		count_mem_init = ADDR_SIZE_ERR_LOW;
//		return;
//	}
}

//Очищение всей памяти сохранения логов
void clear_mem_all() {
    for (log_params.addres_mem = START_ADDRESS_LOG; log_params.addres_mem < END_ADDRESS_LOGS; log_params.addres_mem++) {
        WriteMemory(log_params.addres_mem, 0x0);
    }

    log_params.addres_mem = log_params.start_address_log;
    log_params.stop_log_fast = 0;
    log_params.stop_log_level_1 = 0;
    log_params.stop_log_level_2 = 0;
    log_params.log_cycle_done = 0;

    hb_logs_data = 0;
    log_params.addres_mem_slow = log_params.start_address_log_slow;
    log_params.stop_log_slow = 0;
    log_params.stop_log_slow_level_1 = 0;
    log_params.stop_log_slow_level_2 = 0;

}

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

	if (tlog == FAST_LOG)
	{

		log_params.addres_mem = log_params.start_address_log;
		log_params.log_cycle_done = 0;
		hb_logs_data = 0;
		log_params.stop_log_fast = 0;

		log_params.stop_log_level_1 = 0;
		log_params.stop_log_level_2 = 0;
	}

	if (tlog == SLOW_LOG)
    {

        log_params.addres_mem_slow = log_params.start_address_log_slow;
        log_params.log_cycle_done = 0;
        hb_logs_data = 0;
        log_params.stop_log_slow = 0;

        log_params.stop_log_slow_level_1 = 0;
        log_params.stop_log_slow_level_2 = 0;
    }

//	if (tlog == ALARM_SAVE_MEMORY)
//	{
//
//		count_mem_slow = log_params.start_address_save_log_memory;
//		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;
//	}

//	if (tlog == ERR_LOG)
//	{
//
//		count_mem_err = log_params.start_address_err_log;
//		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;
//	}
//	if (tlog == INIT_LOG)
//	{
//		count_mem_init = ADDR_SIZE_ERR_LOW;
//	}
}


#pragma CODE_SECTION(getFastLogs, ".fast_run2");
void getFastLogs(int cicleLog)
{
    int i_log;

	if (log_params.size_fast_done)
		test_mem_limit(FAST_LOG, cicleLog);

    for (i_log=0;i_log<logsdata.block_size_fast;i_log++)
        write_to_mem(FAST_LOG, (int) logsdata.logs[i_log]);

    log_params.size_fast_done = 1;
}

#pragma CODE_SECTION(getSlowLogs, ".fast_run2");
void getSlowLogs(int cicleLog)
{

    int i_log;

    if (log_params.size_slow_done)
        test_mem_limit(SLOW_LOG, cicleLog);


    for (i_log=0;i_log<logsdata.block_size_slow;i_log++)
        write_to_mem(SLOW_LOG, (int) logsdata.logs[i_log]);

    if (log_params.cur_volume_of_slow_log>0)
        log_params.cur_volume_of_slow_log--;
    else
        log_params.cur_volume_of_slow_log = log_params.volume_of_slow_log;

    log_params.size_slow_done = 1;
}


//void copyLogsToSaveArea()
//{
//    unsigned long from = START_ADDRESS_LOG;
//    unsigned long to = START_ADDRESS_SAVE_ON_ALARM;
//
//    for (;from <= log_params.end_address_log && to < END_ADDRESS_LOGS ;++from, ++to) {
//        WriteMemory(to, ReadMemory(from));
//    }
//}

//void copyLogsToSaveAreaUnrolled()
//{
//    unsigned long from = log_params.log_cycle_done ? log_params.addres_mem : log_params.start_address_log;
//    unsigned long to = START_ADDRESS_SAVE_ON_ALARM;
//    unsigned long count = log_params.end_address_log - log_params.start_address_log;
//
//    for (;count-- > 0 && to < END_ADDRESS_LOGS ;++from, ++to) {
//        if (from >= log_params.end_address_log) {
//            from = log_params.start_address_log;
//        }
//        WriteMemory(to, ReadMemory(from));
//    }
//}

