/* 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 "target_syssvc.h"
#include "target_serial.h"
#include "syssvc/serial.h"
#include "line_trace.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_INI_TASK[COUNT_STK_T(STACK_SIZE)];
static STK_T _kernel_stack_MAIN_TASK[COUNT_STK_T(STACK_SIZE)];
static STK_T _kernel_stack_BRIGHT_HIGH_TASK[COUNT_STK_T(STACK_SIZE)];
static STK_T _kernel_stack_BRIGHT_LOW_TASK[COUNT_STK_T(STACK_SIZE)];
static STK_T _kernel_stack_SW_WAIT_TASK[COUNT_STK_T(STACK_SIZE)];

const TINIB _kernel_tinib_table[TNUM_TSKID] = {
	{ (TA_ACT), (intptr_t)(1), (init_task), INT_PRIORITY(INI_TASK_PRIORITY), ROUND_STK_T(STACK_SIZE), (_kernel_stack_INI_TASK), (TA_NULL), (NULL) },
	{ (TA_ACT), (intptr_t)(1), (main_task), INT_PRIORITY(MAIN_PRIORITY), ROUND_STK_T(STACK_SIZE), (_kernel_stack_MAIN_TASK), (TA_NULL), (NULL) },
	{ (TA_NULL), (intptr_t)(1), (bright_high_task), INT_PRIORITY(BRIGHT_HIGH_PRIORITY), ROUND_STK_T(STACK_SIZE), (_kernel_stack_BRIGHT_HIGH_TASK), (TA_NULL), (NULL) },
	{ (TA_NULL), (intptr_t)(1), (bright_low_task), INT_PRIORITY(BRIGHT_LOW_PRIORITY), ROUND_STK_T(STACK_SIZE), (_kernel_stack_BRIGHT_LOW_TASK), (TA_NULL), (NULL) },
	{ (TA_NULL), (intptr_t)(1), (sw_wait_task), INT_PRIORITY(SW_WAIT_PRIORITY), ROUND_STK_T(STACK_SIZE), (_kernel_stack_SW_WAIT_TASK), (TA_NULL), (NULL) }
};

TCB _kernel_tcb_table[TNUM_TSKID];

const ID _kernel_torder_table[TNUM_TSKID] = {
	INI_TASK, MAIN_TASK, BRIGHT_HIGH_TASK, BRIGHT_LOW_TASK, SW_WAIT_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) }
};

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);

TOPPERS_EMPTY_LABEL(const CYCINIB, _kernel_cycinib_table);
TOPPERS_EMPTY_LABEL(CYCCB, _kernel_cyccb_table);

/*
 *  Alarm Handler Functions
 */

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

TOPPERS_EMPTY_LABEL(const ALMINIB, _kernel_alminib_table);
TOPPERS_EMPTY_LABEL(ALMCB, _kernel_almcb_table);

/*
 *  Interrupt Management Functions
 */

void
_kernel_inthdr_238(void)
{
	i_begin_int(238);
	LOG_ISR_ENTER(238);
	sio_rx_isr((intptr_t)(SIO_PORTID));
	LOG_ISR_LEAVE(238);
	i_end_int(238);
}
void
_kernel_inthdr_239(void)
{
	i_begin_int(239);
	LOG_ISR_ENTER(239);
	sio_tx_isr((intptr_t)(SIO_PORTID));
	LOG_ISR_LEAVE(239);
	i_end_int(239);
}

#define TNUM_INHNO	3
const uint_t _kernel_tnum_inhno = TNUM_INHNO;

INTHDR_ENTRY(INHNO_TIMER, 175, target_timer_handler)
INTHDR_ENTRY(238, 238, _kernel_inthdr_238)
INTHDR_ENTRY(239, 239, _kernel_inthdr_239)

const INHINIB _kernel_inhinib_table[TNUM_INHNO] = {
	{ (INHNO_TIMER), (TA_NULL), (FP)(INT_ENTRY(INHNO_TIMER, target_timer_handler)) },
	{ (238), (TA_NULL), (FP)(INT_ENTRY(238, _kernel_inthdr_238)) },
	{ (239), (TA_NULL), (FP)(INT_ENTRY(239, _kernel_inthdr_239)) }
};

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

/*
 *  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[DEFAULT_ISTKSZ];
#define TOPPERS_ISTKSZ		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_interrupt();
	_kernel_initialize_exception();
}

/*
 *  Initialization Routine
 */

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

/*
 *  Termination Routine
 */

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

/*												
 *  Target-dependent Definitions (SH12A)		
 */												

const uint8_t _kernel_int_iipm_tbl[TNUM_INT] = {				
	UINT8_C(0x80), /* 0x040 */
	UINT8_C(0x80), /* 0x041 */
	UINT8_C(0x80), /* 0x042 */
	UINT8_C(0x80), /* 0x043 */
	UINT8_C(0x80), /* 0x044 */
	UINT8_C(0x80), /* 0x045 */
	UINT8_C(0x80), /* 0x046 */
	UINT8_C(0x80), /* 0x047 */
	UINT8_C(0x80), /* 0x048 */
	UINT8_C(0x80), /* 0x049 */
	UINT8_C(0x80), /* 0x04a */
	UINT8_C(0x80), /* 0x04b */
	UINT8_C(0x80), /* 0x04c */
	UINT8_C(0x80), /* 0x04d */
	UINT8_C(0x80), /* 0x04e */
	UINT8_C(0x80), /* 0x04f */
	UINT8_C(0x80), /* 0x050 */
	UINT8_C(0x80), /* 0x051 */
	UINT8_C(0x80), /* 0x052 */
	UINT8_C(0x80), /* 0x053 */
	UINT8_C(0x80), /* 0x054 */
	UINT8_C(0x80), /* 0x055 */
	UINT8_C(0x80), /* 0x056 */
	UINT8_C(0x80), /* 0x057 */
	UINT8_C(0x80), /* 0x058 */
	UINT8_C(0x80), /* 0x059 */
	UINT8_C(0x80), /* 0x05a */
	UINT8_C(0x80), /* 0x05b */
	UINT8_C(0x80), /* 0x05c */
	UINT8_C(0x80), /* 0x05d */
	UINT8_C(0x80), /* 0x05e */
	UINT8_C(0x80), /* 0x05f */
	UINT8_C(0x80), /* 0x060 */
	UINT8_C(0x80), /* 0x061 */
	UINT8_C(0x80), /* 0x062 */
	UINT8_C(0x80), /* 0x063 */
	UINT8_C(0x80), /* 0x064 */
	UINT8_C(0x80), /* 0x065 */
	UINT8_C(0x80), /* 0x066 */
	UINT8_C(0x80), /* 0x067 */
	UINT8_C(0x80), /* 0x068 */
	UINT8_C(0x80), /* 0x069 */
	UINT8_C(0x80), /* 0x06a */
	UINT8_C(0x80), /* 0x06b */
	UINT8_C(0x80), /* 0x06c */
	UINT8_C(0x80), /* 0x06d */
	UINT8_C(0x80), /* 0x06e */
	UINT8_C(0x80), /* 0x06f */
	UINT8_C(0x80), /* 0x070 */
	UINT8_C(0x80), /* 0x071 */
	UINT8_C(0x80), /* 0x072 */
	UINT8_C(0x80), /* 0x073 */
	UINT8_C(0x80), /* 0x074 */
	UINT8_C(0x80), /* 0x075 */
	UINT8_C(0x80), /* 0x076 */
	UINT8_C(0x80), /* 0x077 */
	UINT8_C(0x80), /* 0x078 */
	UINT8_C(0x80), /* 0x079 */
	UINT8_C(0x80), /* 0x07a */
	UINT8_C(0x80), /* 0x07b */
	UINT8_C(0x80), /* 0x07c */
	UINT8_C(0x80), /* 0x07d */
	UINT8_C(0x80), /* 0x07e */
	UINT8_C(0x80), /* 0x07f */
	UINT8_C(0x80), /* 0x080 */
	UINT8_C(0x80), /* 0x081 */
	UINT8_C(0x80), /* 0x082 */
	UINT8_C(0x80), /* 0x083 */
	UINT8_C(0x80), /* 0x084 */
	UINT8_C(0x80), /* 0x085 */
	UINT8_C(0x80), /* 0x086 */
	UINT8_C(0x80), /* 0x087 */
	UINT8_C(0x80), /* 0x088 */
	UINT8_C(0x80), /* 0x089 */
	UINT8_C(0x80), /* 0x08a */
	UINT8_C(0x80), /* 0x08b */
	UINT8_C(0x80), /* 0x08c */
	UINT8_C(0x80), /* 0x08d */
	UINT8_C(0x80), /* 0x08e */
	UINT8_C(0x80), /* 0x08f */
	UINT8_C(0x80), /* 0x090 */
	UINT8_C(0x80), /* 0x091 */
	UINT8_C(0x80), /* 0x092 */
	UINT8_C(0x80), /* 0x093 */
	UINT8_C(0x80), /* 0x094 */
	UINT8_C(0x80), /* 0x095 */
	UINT8_C(0x80), /* 0x096 */
	UINT8_C(0x80), /* 0x097 */
	UINT8_C(0x80), /* 0x098 */
	UINT8_C(0x80), /* 0x099 */
	UINT8_C(0x80), /* 0x09a */
	UINT8_C(0x80), /* 0x09b */
	UINT8_C(0x80), /* 0x09c */
	UINT8_C(0x80), /* 0x09d */
	UINT8_C(0x80), /* 0x09e */
	UINT8_C(0x80), /* 0x09f */
	UINT8_C(0x80), /* 0x0a0 */
	UINT8_C(0x80), /* 0x0a1 */
	UINT8_C(0x80), /* 0x0a2 */
	UINT8_C(0x80), /* 0x0a3 */
	UINT8_C(0x80), /* 0x0a4 */
	UINT8_C(0x80), /* 0x0a5 */
	UINT8_C(0x80), /* 0x0a6 */
	UINT8_C(0x80), /* 0x0a7 */
	UINT8_C(0x80), /* 0x0a8 */
	UINT8_C(0x80), /* 0x0a9 */
	UINT8_C(0x80), /* 0x0aa */
	UINT8_C(0x80), /* 0x0ab */
	UINT8_C(0x80), /* 0x0ac */
	UINT8_C(0x80), /* 0x0ad */
	UINT8_C(0x80), /* 0x0ae */
	UINT8_C(0x06), /* 0x0af */
	UINT8_C(0x80), /* 0x0b0 */
	UINT8_C(0x80), /* 0x0b1 */
	UINT8_C(0x80), /* 0x0b2 */
	UINT8_C(0x80), /* 0x0b3 */
	UINT8_C(0x80), /* 0x0b4 */
	UINT8_C(0x80), /* 0x0b5 */
	UINT8_C(0x80), /* 0x0b6 */
	UINT8_C(0x80), /* 0x0b7 */
	UINT8_C(0x80), /* 0x0b8 */
	UINT8_C(0x80), /* 0x0b9 */
	UINT8_C(0x80), /* 0x0ba */
	UINT8_C(0x80), /* 0x0bb */
	UINT8_C(0x80), /* 0x0bc */
	UINT8_C(0x80), /* 0x0bd */
	UINT8_C(0x80), /* 0x0be */
	UINT8_C(0x80), /* 0x0bf */
	UINT8_C(0x80), /* 0x0c0 */
	UINT8_C(0x80), /* 0x0c1 */
	UINT8_C(0x80), /* 0x0c2 */
	UINT8_C(0x80), /* 0x0c3 */
	UINT8_C(0x80), /* 0x0c4 */
	UINT8_C(0x80), /* 0x0c5 */
	UINT8_C(0x80), /* 0x0c6 */
	UINT8_C(0x80), /* 0x0c7 */
	UINT8_C(0x80), /* 0x0c8 */
	UINT8_C(0x80), /* 0x0c9 */
	UINT8_C(0x80), /* 0x0ca */
	UINT8_C(0x80), /* 0x0cb */
	UINT8_C(0x80), /* 0x0cc */
	UINT8_C(0x80), /* 0x0cd */
	UINT8_C(0x80), /* 0x0ce */
	UINT8_C(0x80), /* 0x0cf */
	UINT8_C(0x80), /* 0x0d0 */
	UINT8_C(0x80), /* 0x0d1 */
	UINT8_C(0x80), /* 0x0d2 */
	UINT8_C(0x80), /* 0x0d3 */
	UINT8_C(0x80), /* 0x0d4 */
	UINT8_C(0x80), /* 0x0d5 */
	UINT8_C(0x80), /* 0x0d6 */
	UINT8_C(0x80), /* 0x0d7 */
	UINT8_C(0x80), /* 0x0d8 */
	UINT8_C(0x80), /* 0x0d9 */
	UINT8_C(0x80), /* 0x0da */
	UINT8_C(0x80), /* 0x0db */
	UINT8_C(0x80), /* 0x0dc */
	UINT8_C(0x80), /* 0x0dd */
	UINT8_C(0x80), /* 0x0de */
	UINT8_C(0x80), /* 0x0df */
	UINT8_C(0x80), /* 0x0e0 */
	UINT8_C(0x80), /* 0x0e1 */
	UINT8_C(0x80), /* 0x0e2 */
	UINT8_C(0x80), /* 0x0e3 */
	UINT8_C(0x80), /* 0x0e4 */
	UINT8_C(0x80), /* 0x0e5 */
	UINT8_C(0x80), /* 0x0e6 */
	UINT8_C(0x80), /* 0x0e7 */
	UINT8_C(0x80), /* 0x0e8 */
	UINT8_C(0x80), /* 0x0e9 */
	UINT8_C(0x80), /* 0x0ea */
	UINT8_C(0x80), /* 0x0eb */
	UINT8_C(0x80), /* 0x0ec */
	UINT8_C(0x80), /* 0x0ed */
	UINT8_C(0x04), /* 0x0ee */
	UINT8_C(0x04), /* 0x0ef */
	UINT8_C(0x80), /* 0x0f0 */
	UINT8_C(0x80), /* 0x0f1 */
	UINT8_C(0x80), /* 0x0f2 */
	UINT8_C(0x80), /* 0x0f3 */
	UINT8_C(0x80), /* 0x0f4 */
	UINT8_C(0x80), /* 0x0f5 */
	UINT8_C(0x80), /* 0x0f6 */
	UINT8_C(0x80), /* 0x0f7 */
	UINT8_C(0x80), /* 0x0f8 */
	UINT8_C(0x80), /* 0x0f9 */
	UINT8_C(0x80), /* 0x0fa */
	UINT8_C(0x80), /* 0x0fb */
	UINT8_C(0x80), /* 0x0fc */
	UINT8_C(0x80), /* 0x0fd */
	UINT8_C(0x80), /* 0x0fe */
	UINT8_C(0x80), /* 0x0ff */
	UINT8_C(0x80), /* 0x100 */
	UINT8_C(0x80), /* 0x101 */
	UINT8_C(0x80), /* 0x102 */
	UINT8_C(0x80), /* 0x103 */
	UINT8_C(0x80), /* 0x104 */
	UINT8_C(0x80), /* 0x105 */
	UINT8_C(0x80), /* 0x106 */
	UINT8_C(0x80), /* 0x107 */
	UINT8_C(0x80), /* 0x108 */
	UINT8_C(0x80), /* 0x109 */
	UINT8_C(0x80), /* 0x10a */
	UINT8_C(0x80), /* 0x10b */
	UINT8_C(0x80), /* 0x10c */
	UINT8_C(0x80), /* 0x10d */
	UINT8_C(0x80), /* 0x10e */
	UINT8_C(0x80), /* 0x10f */
	UINT8_C(0x80), /* 0x110 */
	UINT8_C(0x80), /* 0x111 */
	UINT8_C(0x80), /* 0x112 */
	UINT8_C(0x80), /* 0x113 */
	UINT8_C(0x80), /* 0x114 */
	UINT8_C(0x80), /* 0x115 */
	UINT8_C(0x80), /* 0x116 */
	UINT8_C(0x80), /* 0x117 */
	UINT8_C(0x80), /* 0x118 */
	UINT8_C(0x80), /* 0x119 */
	UINT8_C(0x80), /* 0x11a */
	UINT8_C(0x80), /* 0x11b */
	UINT8_C(0x80), /* 0x11c */
	UINT8_C(0x80), /* 0x11d */
	UINT8_C(0x80), /* 0x11e */
	UINT8_C(0x80), /* 0x11f */
	UINT8_C(0x80), /* 0x120 */
	UINT8_C(0x80), /* 0x121 */
	UINT8_C(0x80), /* 0x122 */
	UINT8_C(0x80), /* 0x123 */
	UINT8_C(0x80), /* 0x124 */
	UINT8_C(0x80), /* 0x125 */
	UINT8_C(0x80), /* 0x126 */
	UINT8_C(0x80), /* 0x127 */
	UINT8_C(0x80), /* 0x128 */
	UINT8_C(0x80), /* 0x129 */
	UINT8_C(0x80), /* 0x12a */
	UINT8_C(0x80), /* 0x12b */
	UINT8_C(0x80), /* 0x12c */
	UINT8_C(0x80), /* 0x12d */
	UINT8_C(0x80), /* 0x12e */
	UINT8_C(0x80), /* 0x12f */
	UINT8_C(0x80), /* 0x130 */
	UINT8_C(0x80), /* 0x131 */
	UINT8_C(0x80), /* 0x132 */
	UINT8_C(0x80), /* 0x133 */
	UINT8_C(0x80), /* 0x134 */
	UINT8_C(0x80), /* 0x135 */
	UINT8_C(0x80), /* 0x136 */
	UINT8_C(0x80), /* 0x137 */
	UINT8_C(0x80), /* 0x138 */

};																


