/**************************************************************************//**
 * @item     CosyOS-III Port
 * @file     port_x51.c
 * @brief    80x51 Core Port File
 * @author   迟凯峰
 * @version  v3.0.0
 * @date     2026.07.21
 ******************************************************************************/

#include "os_var.h"
#ifdef __PORT_X51_H


/*
 * 全局临界区
 */
static bit m_oldirq;
static s_u8_t volatile _SYS_MEM_ m_glocri_counter/**/sZeroInstall;

void mx_disable_irq(void)
{
	if(_testbit_(EA)){
		m_oldirq = 1;
	}
	else if(!m_glocri_counter){
		m_oldirq = 0;
	}
	m_glocri_counter++;
}

void mx_resume_irq(void)
{
	m_glocri_counter--;
	if(!m_glocri_counter){
		EA = m_oldirq;
	}
}


/*
 * 中断FIFO服务
 */
#if (SYSCFG_PENDSVFIFO_DEPTH > 0)

/**
 \brief    FIFO互斥访问锁
 \details  入FIFO与出FIFO之间的互斥
 \note     if(锁) { 中断FIFO服务装载器入FIFO0，中断FIFO服务处理器出FIFO1 } <br>
           else   { 中断FIFO服务装载器入FIFO1，中断FIFO服务处理器出FIFO0 }
 */
bit m_fifo_lock = true;

#if (SYSCFG_PENDSVFIFO_MONITOR == __ENABLED__)
s_u8_t  mPendSV_FIFO_DepthMAX/**/sZeroInstall;
s_u16_t mPendSV_FIFO_CountMAX/**/sZeroInstall;
#endif


/*
 * [JBC]指令
 */
#if (MCUCFG_PENDSVFIFO_MUTEX > 0xFF)

/* FIFO计数器 */
s_u8_t data m_fifo_count[2] = {SYSCFG_PENDSVFIFO_DEPTH};

static void _fifo_x_(void _STATIC_MEM_ *sv) MCUCFG_OSIT_ATTRIBUTE
{
	(*sPendSV_FIFOHandler[*(s_u8_t const _STATIC_MEM_ *)sv])(sv);
}

#define mWriteCode(n) \
	bit m_sign_fifo_0_##n = true; \
	bit m_sign_fifo_1_##n = true

mWriteCode(0);
mWriteCode(1);
mWriteCode(2);
mWriteCode(3);
mWriteCode(4);
mWriteCode(5);
mWriteCode(6);
mWriteCode(7);
#if (SYSCFG_PENDSVFIFO_DEPTH > 8)
mWriteCode(8);
mWriteCode(9);
mWriteCode(10);
mWriteCode(11);
mWriteCode(12);
mWriteCode(13);
mWriteCode(14);
mWriteCode(15);
#if (SYSCFG_PENDSVFIFO_DEPTH > 16)
mWriteCode(16);
mWriteCode(17);
mWriteCode(18);
mWriteCode(19);
mWriteCode(20);
mWriteCode(21);
mWriteCode(22);
mWriteCode(23);
#if (SYSCFG_PENDSVFIFO_DEPTH > 24)
mWriteCode(24);
mWriteCode(25);
mWriteCode(26);
mWriteCode(27);
mWriteCode(28);
mWriteCode(29);
mWriteCode(30);
mWriteCode(31);
#if (SYSCFG_PENDSVFIFO_DEPTH > 32)
mWriteCode(32);
mWriteCode(33);
mWriteCode(34);
mWriteCode(35);
mWriteCode(36);
mWriteCode(37);
mWriteCode(38);
mWriteCode(39);
#endif
#endif
#endif
#endif
#undef mWriteCode

#define mWriteCode(m, n) \
if(m_sign_fifo_##m##_##n){ i = n; goto __FIFO_##m##_END; } \
   m_sign_fifo_##m##_##n = true; \
    _fifo_x_(*(void _STATIC_MEM_ * _STATIC_MEM_ *)(mPendSV_FIFO_##m + n))

void mPendSV_FIFOHandler(void) MCUCFG_OSIT_ATTRIBUTE
{
	s_u8_t  _SYS_REG_ i;
	s_u16_t _SYS_REG_ j = 0;
	
__FIFO_0:
	m_fifo_lock = false;
	mWriteCode(0, 0);
	mWriteCode(0, 1);
	mWriteCode(0, 2);
	mWriteCode(0, 3);
	mWriteCode(0, 4);
	mWriteCode(0, 5);
	mWriteCode(0, 6);
	mWriteCode(0, 7);
	#if (SYSCFG_PENDSVFIFO_DEPTH > 8)
	mWriteCode(0, 8);
	mWriteCode(0, 9);
	mWriteCode(0, 10);
	mWriteCode(0, 11);
	mWriteCode(0, 12);
	mWriteCode(0, 13);
	mWriteCode(0, 14);
	mWriteCode(0, 15);
	#if (SYSCFG_PENDSVFIFO_DEPTH > 16)
	mWriteCode(0, 16);
	mWriteCode(0, 17);
	mWriteCode(0, 18);
	mWriteCode(0, 19);
	mWriteCode(0, 20);
	mWriteCode(0, 21);
	mWriteCode(0, 22);
	mWriteCode(0, 23);
	#if (SYSCFG_PENDSVFIFO_DEPTH > 24)
	mWriteCode(0, 24);
	mWriteCode(0, 25);
	mWriteCode(0, 26);
	mWriteCode(0, 27);
	mWriteCode(0, 28);
	mWriteCode(0, 29);
	mWriteCode(0, 30);
	mWriteCode(0, 31);
	#if (SYSCFG_PENDSVFIFO_DEPTH > 32)
	mWriteCode(0, 32);
	mWriteCode(0, 33);
	mWriteCode(0, 34);
	mWriteCode(0, 35);
	mWriteCode(0, 36);
	mWriteCode(0, 37);
	mWriteCode(0, 38);
	mWriteCode(0, 39);
	#endif
	#endif
	#endif
	#endif
	
	j += SYSCFG_PENDSVFIFO_DEPTH;
	#if (SYSCFG_PENDSVFIFO_MONITOR == __ENABLED__)
	if(m_fifo_count[0] > mPendSV_FIFO_DepthMAX){
		mPendSV_FIFO_DepthMAX = m_fifo_count[0];
	}
	m_fifo_count[0] = SYSCFG_PENDSVFIFO_DEPTH;
	#endif
	goto __FIFO_1;
	
__FIFO_0_END:
	j += i;
	#if (SYSCFG_PENDSVFIFO_MONITOR == __ENABLED__)
	if(i > mPendSV_FIFO_DepthMAX){
		mPendSV_FIFO_DepthMAX = i;
	}
	#endif
	
__FIFO_1:
	m_fifo_lock = true;
	mWriteCode(1, 0);
	mWriteCode(1, 1);
	mWriteCode(1, 2);
	mWriteCode(1, 3);
	mWriteCode(1, 4);
	mWriteCode(1, 5);
	mWriteCode(1, 6);
	mWriteCode(1, 7);
	#if (SYSCFG_PENDSVFIFO_DEPTH > 8)
	mWriteCode(1, 8);
	mWriteCode(1, 9);
	mWriteCode(1, 10);
	mWriteCode(1, 11);
	mWriteCode(1, 12);
	mWriteCode(1, 13);
	mWriteCode(1, 14);
	mWriteCode(1, 15);
	#if (SYSCFG_PENDSVFIFO_DEPTH > 16)
	mWriteCode(1, 16);
	mWriteCode(1, 17);
	mWriteCode(1, 18);
	mWriteCode(1, 19);
	mWriteCode(1, 20);
	mWriteCode(1, 21);
	mWriteCode(1, 22);
	mWriteCode(1, 23);
	#if (SYSCFG_PENDSVFIFO_DEPTH > 24)
	mWriteCode(1, 24);
	mWriteCode(1, 25);
	mWriteCode(1, 26);
	mWriteCode(1, 27);
	mWriteCode(1, 28);
	mWriteCode(1, 29);
	mWriteCode(1, 30);
	mWriteCode(1, 31);
	#if (SYSCFG_PENDSVFIFO_DEPTH > 32)
	mWriteCode(1, 32);
	mWriteCode(1, 33);
	mWriteCode(1, 34);
	mWriteCode(1, 35);
	mWriteCode(1, 36);
	mWriteCode(1, 37);
	mWriteCode(1, 38);
	mWriteCode(1, 39);
	#endif
	#endif
	#endif
	#endif
	
	j += SYSCFG_PENDSVFIFO_DEPTH;
	#if (SYSCFG_PENDSVFIFO_MONITOR == __ENABLED__)
	if(m_fifo_count[1] > mPendSV_FIFO_DepthMAX){
		mPendSV_FIFO_DepthMAX = m_fifo_count[1];
	}
	m_fifo_count[1] = SYSCFG_PENDSVFIFO_DEPTH;
	#endif
	goto __END;
	
__FIFO_1_END:
	j += i;
	#if (SYSCFG_PENDSVFIFO_MONITOR == __ENABLED__)
	if(i > mPendSV_FIFO_DepthMAX){
		mPendSV_FIFO_DepthMAX = i;
	}
	#endif
	
__END:
	if(!m_sign_fifo_0_0 && j < SYSCFG_PENDSVFIFO_COUNT) goto __FIFO_0;
	#if (SYSCFG_PENDSVFIFO_MONITOR == __ENABLED__)
	if(mPendSV_FIFO_CountMAX < j) mPendSV_FIFO_CountMAX = j;
	#endif
}
#undef mWriteCode

#endif
#endif


#endif
