/* kernel_cfg.c */
#include "kernel/kernel_int.h"
#include "kernel_cfg.h"

#ifndef TOPPERS_EMPTY_LABEL
#define TOPPERS_EMPTY_LABEL(x,y) x y[0]
#endif

/*
 *  Include Directives (#include)
 */

#include "sample1.h"
#include "target_timer.h"
#include "target_syssvc.h"
#include "target_syssvc.h"
#include "target_serial.h"
#include "syssvc/serial.h"
#include "syssvc/banner.h"
#include "syssvc/syslog.h"
#include "syssvc/logtask.h"

/*
 *  Default Definitions of Trace Log Macros
 */

#ifndef LOG_ISR_ENTER
#define LOG_ISR_ENTER(intno)
#endif /* LOG_ISR_ENTER */

#ifndef LOG_ISR_LEAVE
#define LOG_ISR_LEAVE(intno)
#endif /* LOG_ISR_LEAVE */

/*
 *  Task Management Functions
 */

const ID _kernel_tmax_tskid = (TMIN_TSKID + TNUM_TSKID - 1);

const ATR     	_kernel_tinib_tskatr[TNUM_TSKID]    = {(TA_ACT),(TA_ACT),(TA_NULL),(TA_NULL),(TA_NULL),(TA_NULL)};
const uint_t	_kernel_init_rdypmap = 3U;
const intptr_t	_kernel_tinib_exinf[TNUM_TSKID]     = {(intptr_t)(0),(intptr_t)(LOGTASK_PORTID),(intptr_t)(0),(intptr_t)(1),(intptr_t)(2),(intptr_t)(3)};
const TASK    	_kernel_tinib_task[TNUM_TSKID]      = {(init_task),(logtask_main),(main_task),(task),(task),(task)};
const uint_t  	_kernel_tinib_epriority[TNUM_TSKID] = {INT_PRIORITY(1),INT_PRIORITY(2),INT_PRIORITY(3),INT_PRIORITY(4),INT_PRIORITY(5),INT_PRIORITY(5)};

/*
 * Task priority configuration result:
 *  	INIT_TASK:	 IPRI = 1, EXEPRI = 1
 *  	LOGTASK:	 IPRI = 2, EXEPRI = 2
 *  	MAIN_TASK:	 IPRI = 3, EXEPRI = 3
 *  	TASK1:	 IPRI = 4, EXEPRI = 4
 *  	TASK2:	 IPRI = 5, EXEPRI = 5
 *  	TASK3:	 IPRI = 6, EXEPRI = 5
 */

/* 
 * Task Stack Size Estimation: 
 * 
 * Calculation start (Task ID = TASK3, StackSize[TASK3]=STACK_SIZE)
 * 	Task list with higher priority than TASK3 = TASK1,MAIN_TASK,LOGTASK,INIT_TASK
 * 	Calculation start (Task ID = TASK1, StackSize[TASK1]=STACK_SIZE)
 * 		Task list with higher priority than TASK1 = MAIN_TASK,LOGTASK,INIT_TASK
 * 		Calculation start (Task ID = MAIN_TASK, StackSize[MAIN_TASK]=STACK_SIZE)
 * 			Task list with higher priority than MAIN_TASK = LOGTASK,INIT_TASK
 * 			Calculation start (Task ID = LOGTASK, StackSize[LOGTASK]=LOGTASK_STACK_SIZE)
 * 				Task list with higher priority than LOGTASK = INIT_TASK
 * 				Calculation start (Task ID = INIT_TASK, StackSize[INIT_TASK]=STACK_SIZE)
 * 					Task list with higher priority than INIT_TASK = 
 * 					higher_pri_maxstksz[INIT_TASK] = 0
 * 					DONE(max_stksz[INIT_TASK] = 128)
 * 				higher_pri_maxstksz[LOGTASK] = 128
 * 				DONE(max_stksz[LOGTASK] = 384)
 * 			Calculation start (Task ID = INIT_TASK, StackSize[INIT_TASK]=STACK_SIZE)
 * 				SKIP(max_stksz[INIT_TASK] = 128)
 * 			higher_pri_maxstksz[MAIN_TASK] = 384
 * 			DONE(max_stksz[MAIN_TASK] = 512)
 * 		Calculation start (Task ID = LOGTASK, StackSize[LOGTASK]=LOGTASK_STACK_SIZE)
 * 			SKIP(max_stksz[LOGTASK] = 384)
 * 		Calculation start (Task ID = INIT_TASK, StackSize[INIT_TASK]=STACK_SIZE)
 * 			SKIP(max_stksz[INIT_TASK] = 128)
 * 		higher_pri_maxstksz[TASK1] = 512
 * 		DONE(max_stksz[TASK1] = 640)
 * 	Calculation start (Task ID = MAIN_TASK, StackSize[MAIN_TASK]=STACK_SIZE)
 * 		SKIP(max_stksz[MAIN_TASK] = 512)
 * 	Calculation start (Task ID = LOGTASK, StackSize[LOGTASK]=LOGTASK_STACK_SIZE)
 * 		SKIP(max_stksz[LOGTASK] = 384)
 * 	Calculation start (Task ID = INIT_TASK, StackSize[INIT_TASK]=STACK_SIZE)
 * 		SKIP(max_stksz[INIT_TASK] = 128)
 * 	higher_pri_maxstksz[TASK3] = 640
 * 	DONE(max_stksz[TASK3] = 768)
 * Calculation start (Task ID = TASK2, StackSize[TASK2]=STACK_SIZE)
 * 	Task list with higher priority than TASK2 = TASK1,MAIN_TASK,LOGTASK,INIT_TASK
 * 	Calculation start (Task ID = TASK1, StackSize[TASK1]=STACK_SIZE)
 * 		SKIP(max_stksz[TASK1] = 640)
 * 	Calculation start (Task ID = MAIN_TASK, StackSize[MAIN_TASK]=STACK_SIZE)
 * 		SKIP(max_stksz[MAIN_TASK] = 512)
 * 	Calculation start (Task ID = LOGTASK, StackSize[LOGTASK]=LOGTASK_STACK_SIZE)
 * 		SKIP(max_stksz[LOGTASK] = 384)
 * 	Calculation start (Task ID = INIT_TASK, StackSize[INIT_TASK]=STACK_SIZE)
 * 		SKIP(max_stksz[INIT_TASK] = 128)
 * 	higher_pri_maxstksz[TASK2] = 640
 * 	DONE(max_stksz[TASK2] = 768)
 * List of Estimated Total Stack Sizes of Tasks = 768,768
 * Estimated Maximum Total Stack Size of Tasks = 768
 */ 
#define TOPPERS_TSTKSZ		(768)

/*
 *  Eventflag Functions
 */

const ID _kernel_tmax_flgid = (TMIN_FLGID + TNUM_FLGID - 1);

const ATR _kernel_flginib_atr[TNUM_FLGID] = {(TA_NULL)};
const FLGPTN _kernel_flginib_iflgptn[TNUM_FLGID] = {(0x00000000)};
FLGPTN _kernel_flgcb_flgptn[TNUM_FLGID];

/*
 *  Dataqueue Functions
 */

const ID _kernel_tmax_dtqid = (TMIN_DTQID + TNUM_DTQID - 1);

static intptr_t _kernel_dtqmb_DTQ1[3];
const ATR _kernel_dtqinib_atr[TNUM_DTQID] = { (TA_NULL)};

const uint8_t _kernel_dtqinib_size[TNUM_DTQID] = { (3)};

intptr_t * const _kernel_dtqinib_data[TNUM_DTQID] = {(_kernel_dtqmb_DTQ1)};

uint8_t _kernel_dtqcb_count[TNUM_DTQID];
uint8_t _kernel_dtqcb_head[TNUM_DTQID];
uint8_t _kernel_dtqcb_tail[TNUM_DTQID];

/*
 *  Interrupt Management Functions
 */

void
_kernel_inthdr_17(void)
{
	i_begin_int(17);
	LOG_ISR_ENTER(17);
	((ISR)(sio_isr_snd))((intptr_t)(LOGTASK_PORTID));
	LOG_ISR_LEAVE(17);
	i_end_int(17);
}
void
_kernel_inthdr_18(void)
{
	i_begin_int(18);
	LOG_ISR_ENTER(18);
	((ISR)(sio_isr_rcv))((intptr_t)(LOGTASK_PORTID));
	LOG_ISR_LEAVE(18);
	i_end_int(18);
}

#define TNUM_INHNO	3
const uint_t _kernel_tnum_inhno = TNUM_INHNO;

INTHDR_ENTRY(INHNO_TIMER, 12, target_timer_handler)

INTHDR_ENTRY(17, 17, _kernel_inthdr_17)

INTHDR_ENTRY(18, 18, _kernel_inthdr_18)

const INHNO _kernel_inhinib_inhno[TNUM_INHNO] = {(INHNO_TIMER),(17),(18)};
const ATR _kernel_inhinib_inhatr[TNUM_INHNO] = {(TA_NULL),(TA_NULL),(TA_NULL)};
const FP _kernel_inhinib_entry[TNUM_INHNO] = {(FP)(INT_ENTRY(INHNO_TIMER, target_timer_handler)),(FP)(INT_ENTRY(17, _kernel_inthdr_17)),(FP)(INT_ENTRY(18, _kernel_inthdr_18))};

 #define TNUM_INTNO	3
const uint_t _kernel_tnum_intno = TNUM_INTNO;

const INTNO _kernel_intinib_intno[TNUM_INTNO] = {(INTNO_TIMER),(INTNO_SIOP1_RCV),(INTNO_SIOP1_SND)};
const ATR _kernel_intinib_intatr[TNUM_INTNO] = {(TA_ENAINT),(INTATR_SIOP1_RCV),(INTATR_SIOP1_SND)};
const PRI _kernel_intinib_intpri[TNUM_INTNO] = {(INTPRI_TIMER),(INTPRI_SIOP1_RCV),(INTPRI_SIOP1_SND)};

/*
 *  CPU Exception Handler
 */

#define TNUM_EXCNO	1
const uint_t _kernel_tnum_excno = TNUM_EXCNO;

EXCHDR_ENTRY(CPUEXC1, 1, exc_handler)

const EXCNO _kernel_excinib_excno[TNUM_EXCNO] = {(CPUEXC1)};
const ATR _kernel_excinib_excatr[TNUM_EXCNO] = {(TA_NULL)};
const FP _kernel_excinib_entry[TNUM_EXCNO] = {(FP)(EXC_ENTRY(CPUEXC1, exc_handler))};

/*
 *  Cyclic Handler Functions
 */

const ID _kernel_tmax_cycid = (TMIN_CYCID + TNUM_CYCID - 1);
const uint_t _kernel_cycevtid_offset = 0;

const uint16_t _kernel_cycinib_cycact = 1;
const intptr_t _kernel_cycinib_exinf[TNUM_CYCID] = {(intptr_t)(LOGTASK),(intptr_t)(MAIN_TASK),(intptr_t)(0)};
const CYCHDR _kernel_cycinib_cychdr[TNUM_CYCID] = {(cychdr_act_logtsk),(main_task_cychdr),(cyclic_handler)};
const RELTIM _kernel_cycinib_cyctim[TNUM_CYCID] = {(LOGTASK_INTERVAL),(100),(2000)};
const RELTIM _kernel_cycinib_cycphs[TNUM_CYCID] = {(LOGTASK_INTERVAL),(100),(2000)};

EVTTIM _kernel_cyccb_evttim[TNUM_CYCID];
uint16_t _kernel_cyccb_cycact;


/*
 *  Alarm Handler Functions
 */

const ID _kernel_tmax_almid = (TMIN_ALMID + TNUM_ALMID - 1);
const uint_t _kernel_almevtid_offset = 3;

const ALMHDR _kernel_alminib_almhdr[TNUM_ALMID] = {(alarm_handler)};
const intptr_t _kernel_alminib_exinf[TNUM_ALMID] = {(intptr_t)(TASK2)};

uint16_t _kernel_almcb_almact;

/*
 *  Time Event Management
 */

#define TNUM_TMEVT 4

const uint_t _kernel_tnum_tmevt_queue = TNUM_TMEVT;

QUEUE _kernel_tmevt_queue[TNUM_TMEVT+1];
EVTTIM _kernel_tmevt_time[TNUM_TMEVT];
CBACK _kernel_tmevt_callback[TNUM_TMEVT];
uintptr_t _kernel_tmevt_arg[TNUM_TMEVT];


/*
 *  Interrupt Context Stack Size
 */

#define TOPPERS_ISTKSZ		(INTERRUPT_STACKSIZE)


/*
 *  Stack Area for System
 */

static STK_T         		_kernel_stack[COUNT_STK_T(ALL_STACKSIZE)];
#define TOPPERS_STK  		_kernel_stack
#define TOPPERS_STKSZ		ROUND_STK_T(ALL_STACKSIZE)

const SIZE		_kernel_stksz = TOPPERS_STKSZ;
STK_T *const	_kernel_stk = TOPPERS_STK;

#ifdef TOPPERS_ISTKPT
STK_T *const	_kernel_istkpt = TOPPERS_ISTKPT(TOPPERS_STK, TOPPERS_STKSZ);
#endif /* TOPPERS_ISTKPT */


/*
 *  Module Initialization Function
 */

void
_kernel_initialize_object(void)
{
	_kernel_initialize_time_event();
	_kernel_initialize_task();
	_kernel_initialize_interrupt();
	_kernel_initialize_exception();
	_kernel_initialize_cyclic();
	_kernel_initialize_alarm();
	_kernel_initialize_eventflag();
	_kernel_initialize_dataqueue();
}

/*
 *  Initialization Routine
 */

void
_kernel_call_inirtn(void)
{
	((INIRTN)(target_timer_initialize))((intptr_t)(0));
	((INIRTN)(sio_initialize))((intptr_t)(0));
	((INIRTN)(serial_initialize))((intptr_t)(0));
	((INIRTN)(print_banner))((intptr_t)(0));
	((INIRTN)(syslog_initialize))((intptr_t)(0));
}

/*
 *  Termination Routine
 */

void
_kernel_call_terrtn(void)
{
	((TERRTN)(logtask_terminate))((intptr_t)(0));
	((TERRTN)(serial_terminate))((intptr_t)(0));
	((TERRTN)(target_timer_terminate))((intptr_t)(0));
}

const PRI _kernel_intpri_table[64] = 
{	INT8_C( 0), /* 00 */
	INT8_C( 0), /* 01 */
	INT8_C( 0), /* 02 */
	INT8_C( 0), /* 03 */
	INT8_C( 0), /* 04 */
	INT8_C( 0), /* 05 */
	INT8_C( 0), /* 06 */
	INT8_C( 0), /* 07 */
	INT8_C( 0), /* 08 */
	INT8_C( 0), /* 09 */
	INT8_C( 0), /* 10 */
	INT8_C( 0), /* 11 */
	INT8_C(INTPRI_TIMER), /* 12 */
	INT8_C( 0), /* 13 */
	INT8_C( 0), /* 14 */
	INT8_C( 0), /* 15 */
	INT8_C( 0), /* 16 */
	INT8_C(INTPRI_SIOP1_SND), /* 17 */
	INT8_C(INTPRI_SIOP1_RCV), /* 18 */
	INT8_C( 0), /* 19 */
	INT8_C( 0), /* 20 */
	INT8_C( 0), /* 21 */
	INT8_C( 0), /* 22 */
	INT8_C( 0), /* 23 */
	INT8_C( 0), /* 24 */
	INT8_C( 0), /* 25 */
	INT8_C( 0), /* 26 */
	INT8_C( 0), /* 27 */
	INT8_C( 0), /* 28 */
	INT8_C( 0), /* 29 */
	INT8_C( 0), /* 30 */
	INT8_C( 0), /* 31 */
	INT8_C( 0), /* 32 */
	INT8_C( 0), /* 33 */
	INT8_C( 0), /* 34 */
	INT8_C( 0), /* 35 */
	INT8_C( 0), /* 36 */
	INT8_C( 0), /* 37 */
	INT8_C( 0), /* 38 */
	INT8_C( 0), /* 39 */
	INT8_C( 0), /* 40 */
	INT8_C( 0), /* 41 */
	INT8_C( 0), /* 42 */
	INT8_C( 0), /* 43 */
	INT8_C( 0), /* 44 */
	INT8_C( 0), /* 45 */
	INT8_C( 0), /* 46 */
	INT8_C( 0), /* 47 */
	INT8_C( 0), /* 48 */
	INT8_C( 0), /* 49 */
	INT8_C( 0), /* 50 */
	INT8_C( 0), /* 51 */
	INT8_C( 0), /* 52 */
	INT8_C( 0), /* 53 */
	INT8_C( 0), /* 54 */
	INT8_C( 0), /* 55 */
	INT8_C( 0), /* 56 */
	INT8_C( 0), /* 57 */
	INT8_C( 0), /* 58 */
	INT8_C( 0), /* 59 */
	INT8_C( 0), /* 60 */
	INT8_C( 0), /* 61 */
	INT8_C( 0), /* 62 */
	INT8_C( 0), /* 63 */
};




#ifndef TARGET_COPYRIGHT
#define TARGET_COPYRIGHT
#endif /* TARGET_COPYRIGHT */


#ifndef TARGET_NAME
#define TARGET_NAME
#endif /* TARGET_NAME */


const char banner[] = "\n" "TOPPERS/SSP Kernel " "1.2.1 " 	\
		" for " TARGET_NAME " (" __DATE__ " , " __TIME__ ")\n"	\
		"Copyright (C) 2010 by Meika Sugimoto\n"	\
		"Copyright (C) 2010 by Naoki Saito\n"	\
		"            Nagoya Municipal Industrial Research Institute, JAPAN\n"	\
		TARGET_COPYRIGHT "\n";
const int32_t banner_size = sizeof(banner) / sizeof(banner[0]);

