/**************************************************************************//**
 * @item     CosyOS-III Port
 * @file     port_80251.h
 * @brief    80251 Core Port File
 * @author   迟凯峰
 * @version  v3.0.0
 * @date     2026.07.21
 ******************************************************************************/

#ifndef __PORT_80251_H
#define __PORT_80251_H

/* INCLUDE */
#include "port_x51.h"
#include "mcucfg_80251.h"
#include SYSCFG_STANDARDHEAD
#include "os_cmsis.h"
#include "os_stack.h"


/* Memory */
#define _SYS_MEM_    data
#define _CODE_MEM_
#define _CONST_MEM_
#define _STACK_MEM_  near
#define _XDATA_MEM_  xdata
  #if (MCUCFG_MEMORYMODEL == 0)
#define _STATIC_MEM_ near
#define _MALLOC_MEM_ near
#define _OBJ_MEM_    near
#elif (MCUCFG_MEMORYMODEL == 1)
#define _STATIC_MEM_ near
#define _MALLOC_MEM_ near
#define _OBJ_MEM_    near
#elif (MCUCFG_MEMORYMODEL == 2)
#define _STATIC_MEM_ near
#define _MALLOC_MEM_ xdata
#define _OBJ_MEM_
#elif (MCUCFG_MEMORYMODEL == 3)
#define _STATIC_MEM_ xdata
#define _MALLOC_MEM_ near
#define _OBJ_MEM_
#elif (MCUCFG_MEMORYMODEL == 4)
#define _STATIC_MEM_ xdata
#define _MALLOC_MEM_ xdata
#define _OBJ_MEM_    xdata
#endif
/*
 * 用户自定义内存
 * 用户可自定义下方的内存，以寻求在性能和资源之间取得平衡。默认为未定义，由内存MODEL决定。
 * 在 os_var.c、os_var.h 中，用户也可逐个查看及调整对应定义内存的变量，以实现极致的优化。
 */
#define _RTC_MEM_    /* 软件RTC相关系统变量的内存 */
#define _DEBUG_MEM_  /* DEBUG相关系统变量的内存 */


/* Register */
#define _SYS_REG_


/* Typedef */
#define m_boolvoid_tf *(s_boolvoid_tfp)
typedef s_u16_t  m_sp_t;
typedef s_u32_t  m_pc_t;
typedef s_u32_t  m_tick_t;
typedef s_u16_t  m_fetion_t;
typedef s_u16_t  m_tqcount_t;
typedef s_u16_t  m_stacklen_t;
typedef s_u16_t  m_stacksize_t;


/* Extern */
#if (SYSCFG_PENDSVFIFO_DEPTH > 0)
extern  void _STATIC_MEM_ *mPendSV_FIFO_0[SYSCFG_PENDSVFIFO_DEPTH];
extern  void _STATIC_MEM_ *mPendSV_FIFO_1[SYSCFG_PENDSVFIFO_DEPTH];
#pragma SAVE
#pragma functions(static)
extern  void mPendSV_FIFOLoader(void _STATIC_MEM_ *sv);
#pragma RESTORE
#endif


/* CONST & ATTRIBUTE */
#define MCUCFG_PCLEN        4
#define MCUCFG_TASK_ATTRIBUTE
#define MCUCFG_OSIT_ATTRIBUTE
#define MCUCFG_TERNARY_MASK
/** \PUSH {4 Byte Interrupt Frame,DR28-DR0,DR56,PSW1,PSW,[USERREG(ASM)]} */
#define MCUCFG_BASICSTACKSIZE     (42 + MCUCFG_USERREGSIZE)


/* TaskCtrl */
#define mTaskCtrl_Tail_           mUserReg_DEFc_


/* SysTick */
#if (MCUCFG_SYSTICK_COUNT1CYC > (MCUCFG_SYSTICK_LIMITVALUE - 1))
#error 系统滴答定时器溢出。
#endif

////////////////////////////////////////////////////////////////////////////////

/* 用户定义 */// 默认用定时器0做为系统滴答

#define mSysTick_INIT() /*!< 系统滴答初始化 */\
do{ \
	IPH &=~PT0H; PT0 = 0; /*!< 系统滴答中断优先级，必须为最低级 */\
	AUXR = /*!< 定时器频率源 */\
	( \
		(MCUCFG_SYSTICK_CLKDIV == 1) /* 这一行不要动 */\
	  ? (AUXR | 0x80) /*!< 定时器频率源自系统时钟 */\
	  : (AUXR &~0x80) /*!< 定时器频率源自系统时钟/12 */\
	); \
	TMOD &= 0xF0; /*!< 用做定时器，16位自动重装载模式 */\
	TL0 = (s_u8_t)(MCUCFG_SYSTICK_LOADVALUE);      /*!< 重装载值低8位 */\
	TH0 = (s_u8_t)(MCUCFG_SYSTICK_LOADVALUE >> 8); /*!< 重装载值高8位 */\
	TR0 = 1;      /*!< 开启定时器 */\
}while(false)

#define MCUCFG_SYSTICK_CURRVALUE  ((TH0 << 8) | TL0) // 获取系统滴答当前值
#define MCUCFG_SYSTICK_ATTRIBUTE  interrupt 1 // 系统滴答中断属性
#define mSysTick_Disable()  ET0 = 0 // 关闭系统滴答中断
#define mSysTick_Enable()   ET0 = 1 // 开启系统滴答中断
#define mSysTick_Clear()            // 清滴答中断标志位

////////////////////////////////////////////////////////////////////////////////

/* 中断FIFO服务-装载 */
#define mPendSV_FIFOLoad() \
do{ \
	mPendSV_FIFOLoader(&u_psv); \
	mPendSV_Set(); \
}while(false)

/* 中断FIFO服务-处理 */
#if (MCUCFG_PENDSVFIFO_MUTEX < 0x100)
#define mPendSV_FIFOHandle() if( m_fifo_count[0]) mPendSV_FIFOHandler()
#else
#define mPendSV_FIFOHandle() if(!m_sign_fifo_0_0) mPendSV_FIFOHandler()
#endif

/* 事件标志组-中断中写多标志位 */
#define miWriteFlagBits() \
	if(!u_psv.value){ \
		do{}while(false)

/*----------------------------------------------------------------------------*/

/* 任务栈 */
#define mTaskStack_Reset0() \
do{ \
	*(s_u32_t *)node_news->bsp = ( \
	   ((s_u16_t)node_news->entry << 8) \
	 | ((s_u16_t)node_news->entry >> 8) \
	 | ((s_u32_t)node_news->entry & 0xFFFF0000) \
	); \
	*(s_u8_t *)(node_news->bsp + 42 - 5) = DPXL; \
	*(s_u8_t *)(node_news->bsp + 42 - 1) = 0; \
	mUserReg_INITa(); \
	mUserReg_INITc(); \
}while(false)

/* 栈监控 */
#if (SYSCFG_DEBUGGING == __ENABLED__)
extern s_u16_t data m_taskstacklen;
#endif

/* MSP模式 */
#if (MCUCFG_TASKSTACK_MODE == __MSP__)

/* 任务栈 */
#define MCUCFG_TASKSTACK_REALLOC __ENABLED__
#define mTaskCtrl_Head_  m_stacksize_t stacklen;

#define mTaskStack_Reset() \
do{ \
	mTaskStack_Reset0(); \
	node_news->stacklen = MCUCFG_BASICSTACKSIZE; \
}while(false)

#define mTaskStack_Init() \
do{ \
	mTaskStack_Reset(); \
}while(false)

/* 栈监控 */
#if (SYSCFG_DEBUGGING == __ENABLED__)
extern s_u16_t m_msp;
extern s_u16_t m_stacksize;
extern s_u16_t m_stacklen_max;
#define mMainStack_Check()     s_stack_check((s_u8_t edata *)m_msp, m_stacksize)
#define mTaskStack_Check0()    m_taskstacklen

#define mMainStack_CheckInit(size) \
do{ \
	m_msp = (SPH << 8) | SP; \
	m_stacksize = size; \
	s_stack_check_init((s_u8_t edata *)(m_msp + 16), size - 16); \
}while(false)
#endif

/* PSP模式 */
#elif (MCUCFG_TASKSTACK_MODE == __PSP__)

/* 任务栈 */
#define MCUCFG_TASKSTACK_REALLOC __DISABLED__
#define mTaskCtrl_Head_  m_sp_t psp;

#define mTaskStack_Reset() \
do{ \
	mTaskStack_Reset0(); \
	node_news->psp = (m_sp_t)node_news->bsp + MCUCFG_BASICSTACKSIZE - 1; \
}while(false)

#define mTaskStack_Init() \
do{ \
	mTaskStack_CheckInit(); \
	mTaskStack_Reset(); \
}while(false)

/* 栈监控 */
#if (SYSCFG_DEBUGGING == __ENABLED__)
#define mTaskStack_Check0()    m_taskstacklen
#define mTaskStack_Check()     s_stack_check(s_task_current->bsp, s_task_current->stacksize)
#define mTaskStack_CheckInit() s_stack_check_init(node_news->bsp + MCUCFG_BASICSTACKSIZE, node_news->stacksize - MCUCFG_BASICSTACKSIZE)
#else
#define mTaskStack_CheckInit()
#endif
#endif

/*----------------------------------------------------------------------------*/

#if (MCUCFG_PENDSVFIFO_MUTEX > 0xFF)
#if ((SYSCFG_PENDSVFIFO_DEPTH % 8) || (SYSCFG_PENDSVFIFO_DEPTH > 40))
#error 参数非法 of 中断挂起服务FIFO队列深度！
#endif
#else
#if (SYSCFG_PENDSVFIFO_DEPTH > 127)
#error 参数非法 of 中断挂起服务FIFO队列深度！
#endif
#endif


#endif
