/* 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

#if TKERNEL_PRID != 0x07u
#error "The kernel does not match this configuration file."
#endif

/*
 *  Include Directives (#include)
 */

#include "target_timer.h"
#include "syssvc/syslog.h"
#include "syssvc/banner.h"
#include "target_syssvc.h"
#include <target_serial.h>
#include "syssvc/serial.h"
#include "syssvc/logtask.h"
#include "sample1.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);

static STK_T _kernel_stack_LOGTASK[COUNT_STK_T(LOGTASK_STACK_SIZE)];
static STK_T _kernel_stack_TASK1[COUNT_STK_T(STACK_SIZE)];
static STK_T _kernel_stack_TASK2[COUNT_STK_T(STACK_SIZE)];
static STK_T _kernel_stack_TASK3[COUNT_STK_T(STACK_SIZE)];
static STK_T _kernel_stack_MAIN_TASK[COUNT_STK_T(STACK_SIZE)];

const TINIB _kernel_tinib_table[TNUM_TSKID] = {
	{ (TA_ACT), (intptr_t)(LOGTASK_PORTID), (logtask_main), INT_PRIORITY(LOGTASK_PRIORITY), ROUND_STK_T(LOGTASK_STACK_SIZE), (_kernel_stack_LOGTASK), (TA_NULL), (NULL) },
	{ (TA_NULL), (intptr_t)(1), (task), INT_PRIORITY(MID_PRIORITY), ROUND_STK_T(STACK_SIZE), (_kernel_stack_TASK1), (TA_NULL), (tex_routine) },
	{ (TA_NULL), (intptr_t)(2), (task), INT_PRIORITY(MID_PRIORITY), ROUND_STK_T(STACK_SIZE), (_kernel_stack_TASK2), (TA_NULL), (tex_routine) },
	{ (TA_NULL), (intptr_t)(3), (task), INT_PRIORITY(MID_PRIORITY), ROUND_STK_T(STACK_SIZE), (_kernel_stack_TASK3), (TA_NULL), (tex_routine) },
	{ (TA_ACT), (intptr_t)(0), (main_task), INT_PRIORITY(MAIN_PRIORITY), ROUND_STK_T(STACK_SIZE), (_kernel_stack_MAIN_TASK), (TA_NULL), (NULL) }
};

TCB _kernel_tcb_table[TNUM_TSKID];

const ID _kernel_torder_table[TNUM_TSKID] = {
	LOGTASK, TASK1, TASK2, TASK3, MAIN_TASK
};

/*
 *  Semaphore Functions
 */

const ID _kernel_tmax_semid = (TMIN_SEMID + TNUM_SEMID - 1);

const SEMINIB _kernel_seminib_table[TNUM_SEMID] = {
	{ (TA_TPRI), (0), (1) },
	{ (TA_TPRI), (1), (1) },
	{ (TA_TPRI), (0), (1) },
	{ (TA_TPRI), (1), (1) },
	{ (TA_TPRI), (0), (1) },
	{ (TA_TPRI), (1), (1) }
};

SEMCB _kernel_semcb_table[TNUM_SEMID];

/*
 *  Eventflag Functions
 */

const ID _kernel_tmax_flgid = (TMIN_FLGID + TNUM_FLGID - 1);

TOPPERS_EMPTY_LABEL(const FLGINIB, _kernel_flginib_table);
TOPPERS_EMPTY_LABEL(FLGCB, _kernel_flgcb_table);

/*
 *  Dataqueue Functions
 */

const ID _kernel_tmax_dtqid = (TMIN_DTQID + TNUM_DTQID - 1);

TOPPERS_EMPTY_LABEL(const DTQINIB, _kernel_dtqinib_table);
TOPPERS_EMPTY_LABEL(DTQCB, _kernel_dtqcb_table);

/*
 *  Priority Dataqueue Functions
 */

const ID _kernel_tmax_pdqid = (TMIN_PDQID + TNUM_PDQID - 1);

TOPPERS_EMPTY_LABEL(const PDQINIB, _kernel_pdqinib_table);
TOPPERS_EMPTY_LABEL(PDQCB, _kernel_pdqcb_table);

/*
 *  Mailbox Functions
 */

const ID _kernel_tmax_mbxid = (TMIN_MBXID + TNUM_MBXID - 1);

TOPPERS_EMPTY_LABEL(const MBXINIB, _kernel_mbxinib_table);
TOPPERS_EMPTY_LABEL(MBXCB, _kernel_mbxcb_table);

/*
 *  Fixed-sized Memorypool Functions
 */

const ID _kernel_tmax_mpfid = (TMIN_MPFID + TNUM_MPFID - 1);

TOPPERS_EMPTY_LABEL(const MPFINIB, _kernel_mpfinib_table);
TOPPERS_EMPTY_LABEL(MPFCB, _kernel_mpfcb_table);

/*
 *  Cyclic Handler Functions
 */

const ID _kernel_tmax_cycid = (TMIN_CYCID + TNUM_CYCID - 1);

const CYCINIB _kernel_cycinib_table[TNUM_CYCID] = {
	{ (TA_NULL), (intptr_t)(0), (cyclic_handler), (2000), (0) }
};

CYCCB _kernel_cyccb_table[TNUM_CYCID];

/*
 *  Alarm Handler Functions
 */

const ID _kernel_tmax_almid = (TMIN_ALMID + TNUM_ALMID - 1);

const ALMINIB _kernel_alminib_table[TNUM_ALMID] = {
	{ (TA_NULL), (intptr_t)(0), (alarm_handler) }
};

ALMCB _kernel_almcb_table[TNUM_ALMID];

/*
 *  Interrupt Management Functions
 */

void
_kernel_inthdr_15(void)
{
	i_begin_int(15);
	LOG_ISR_ENTER(15);
	((ISR)(sio_tx_isr))((intptr_t)(SIO_PORTID));
	LOG_ISR_LEAVE(15);
	i_end_int(15);
}
void
_kernel_inthdr_16(void)
{
	i_begin_int(16);
	LOG_ISR_ENTER(16);
	((ISR)(sio_rx_isr))((intptr_t)(SIO_PORTID));
	LOG_ISR_LEAVE(16);
	i_end_int(16);
}

#define TNUM_INHNO	3
const uint_t _kernel_tnum_inhno = TNUM_INHNO;

INTHDR_ENTRY(INHNO_TIMER, 21, target_timer_handler)
INTHDR_ENTRY(15, 15, _kernel_inthdr_15)
INTHDR_ENTRY(16, 16, _kernel_inthdr_16)

const INHINIB _kernel_inhinib_table[TNUM_INHNO] = {
	{ (INHNO_TIMER), (TA_NULL), (FP)(INT_ENTRY(INHNO_TIMER, target_timer_handler)) },
	{ (15), (TA_NULL), (FP)(INT_ENTRY(15, _kernel_inthdr_15)) },
	{ (16), (TA_NULL), (FP)(INT_ENTRY(16, _kernel_inthdr_16)) }
};

#define TNUM_INTNO	3
const uint_t _kernel_tnum_intno = TNUM_INTNO;

const INTINIB _kernel_intinib_table[TNUM_INTNO] = {
	{ (INTNO_TIMER), (TA_ENAINT | INTATR_TIMER), (INTPRI_TIMER) },
	{ (INTNO_SIO_TX), (INTATR_SIO), (INTPRI_SIO) },
	{ (INTNO_SIO_RX), (INTATR_SIO), (INTPRI_SIO) }
};

/*
 *  CPU Exception Handler
 */

#define TNUM_EXCNO	1
const uint_t _kernel_tnum_excno = TNUM_EXCNO;

EXCHDR_ENTRY(CPUEXC1, 1, cpuexc_handler)

const EXCINIB _kernel_excinib_table[TNUM_EXCNO] = {
	{ (CPUEXC1), (TA_NULL), (FP)(EXC_ENTRY(CPUEXC1, cpuexc_handler)) }
};

/*
 *  Stack Area for Non-task Context
 */

#ifdef DEFAULT_ISTK

#define TOPPERS_ISTKSZ		DEFAULT_ISTKSZ
#define TOPPERS_ISTK		DEFAULT_ISTK

#else /* DEAULT_ISTK */

static STK_T				_kernel_istack[COUNT_STK_T(DEFAULT_ISTKSZ)];
#define TOPPERS_ISTKSZ		ROUND_STK_T(DEFAULT_ISTKSZ)
#define TOPPERS_ISTK		_kernel_istack

#endif /* DEAULT_ISTK */

const SIZE		_kernel_istksz = TOPPERS_ISTKSZ;
STK_T *const	_kernel_istk = TOPPERS_ISTK;

#ifdef TOPPERS_ISTKPT
STK_T *const	_kernel_istkpt = TOPPERS_ISTKPT(TOPPERS_ISTK, TOPPERS_ISTKSZ);
#endif /* TOPPERS_ISTKPT */

/*
 *  Time Event Management
 */

TMEVTN   _kernel_tmevt_heap[TNUM_TSKID + TNUM_CYCID + TNUM_ALMID];

/*
 *  Module Initialization Function
 */

void
_kernel_initialize_object(void)
{
	_kernel_initialize_task();
	_kernel_initialize_semaphore();
	_kernel_initialize_cyclic();
	_kernel_initialize_alarm();
	_kernel_initialize_interrupt();
	_kernel_initialize_exception();
}

/*
 *  Initialization Routine
 */

void
_kernel_call_inirtn(void)
{
	((INIRTN)(target_timer_initialize))((intptr_t)(0));
	((INIRTN)(syslog_initialize))((intptr_t)(0));
	((INIRTN)(print_banner))((intptr_t)(0));
	((INIRTN)(sio_initialize))((intptr_t)(0));
	((INIRTN)(serial_initialize))((intptr_t)(0));
}

/*
 *  Termination Routine
 */

void
_kernel_call_terrtn(void)
{
	((TERRTN)(logtask_terminate))((intptr_t)(0));
	((TERRTN)(target_timer_terminate))((intptr_t)(0));
}

const PRI 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( 0), /* 12 */
	INT8_C( 0), /* 13 */
	INT8_C( 0), /* 14 */
	INT8_C(INTPRI_SIO), /* 15 */
	INT8_C(INTPRI_SIO), /* 16 */
	INT8_C( 0), /* 17 */
	INT8_C( 0), /* 18 */
	INT8_C( 0), /* 19 */
	INT8_C( 0), /* 20 */
	INT8_C(INTPRI_TIMER), /* 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 */
};


