
unit OS
{
	interface[1] void REMO_SetInterrupt( int32 index, bool open ){}
	interface[2] void REMO_WriteByte( uint8 d ){}
	interface[3] void REMO_Debug( int32 d ){}
	
	interface[4] void REMO_DataChannelWrite( int32 d, int32 d1 ){}
	interface[5] int32 REMO_DataChannelRead( int32 d ){}
	
	interface[6] void REMO_DataChannelInit(){}
	
	interface[7] void REMO_ModuleWrite( int32 d, int32 d1 ){}
	interface[8] int32 REMO_ModuleRead( int32 d ){}
	
	//---------------------------------------------------
	public void OS_init()
	{
		DriverInit();
		
		//OS内核初始化
		KernelInit();
		
		#.OS.REMO_SetInterrupt( 0x11, true );
	}
	
	//=================================================================================
	[uint8*RUN_NUMBER] TickList;
	[uint8*RUN_NUMBER] MaxTickList;
	[uint32*RUN_NUMBER] DriverHandleList;
	
	//..........................
	//注意 DriverNumber 由操作系统使用,用于调度边界控制,
	//而 RUN_NUMBER 仅仅用于定义足够大的存储区,不参与代码逻辑
	
	//内核驱动程序总数
	uint8 DriverNumber;
	//所需的最小存储边界
	public const uint8 RUN_NUMBER = 0;
	
	[uint32*RUN100us_NUMBER] Driver100usHandleList;
	
	//..........................
	//注意 Driver100usNumber 由操作系统使用,用于调度边界控制,
	//而 RUN100us_NUMBER 仅仅用于定义足够大的存储区,不参与代码逻辑
	
	//快速内核驱动程序总数
	uint8 Driver100usNumber;
	//所需的最小存储边界
	public const uint8 RUN100us_NUMBER = 0;
	//..........................
	
	public uint8 tick;
	
	//=================================================================================
	
	//---------------------------------------------------
	//中断服务函数
	interrupt 200 [0x11]
	void OS_run()
	{
		//运行快速内核驱动列表
		RunDriver100usList();
		
		RunDriverList();
		
		tick + 1;
	}
	//---------------------------------------------------
	//初始化内核驱动时间表
	void DriverInit()
	{
		DriverNumber = 0;
		for( uint8 i = 0; i < RUN_NUMBER; i + 1 ) {
			TickList[i] = 0;
			MaxTickList[i] = 0;
			DriverHandleList[i] = 0;
		}
		Driver100usNumber = 0;
	}
	//---------------------------------------------------
	//注入组件驱动程序到内核进程
	public void CreateDriver100us( uint32 Handle )
	{
		Driver100usHandleList[Driver100usNumber] = Handle;
		Driver100usNumber + 1;
	}
	//---------------------------------------------------
	//注入组件驱动程序到内核进程
	public void CreateDriver( uint32 Handle, uint8 MaxTick )
	{
		DriverHandleList[DriverNumber] = Handle;
		MaxTickList[DriverNumber] = MaxTick;
		DriverNumber + 1;
	}
	//---------------------------------------------------
	//遍历组件驱动列表,逐个运行
	void RunDriverList()
	{
		for( uint8 i = 0; i < DriverNumber; i + 1 ) {
			TickList[i] + 1;
			if( TickList[i] == MaxTickList[i] ) {
				TickList[i] = 0;
				D_CALL( DriverHandleList[i] );
			}
		}
	}
	//---------------------------------------------------
	//遍历快速组件驱动列表,逐个运行
	void RunDriver100usList()
	{
		for( uint8 i = 0; i < Driver100usNumber; i + 1 ) {
			D_CALL( Driver100usHandleList[i] );
		}
	}
	//---------------------------------------------------
	//动态调用封装器
	void D_CALL( uint32 Handle )
	{
		#asm "加载_PC_local_uint24 0"
	}
	
	//=================================================================================
	//任务控制块
	struct TCB
	{
		//任务使用的存储空间,用于堆栈基址,局部变量和调用函数地址的保护
		//堆栈数据格式(初始状态):
		//位置索引: 0    1    2   ~~   L-8  L-7  L-6  L-5  L-4  L-3  L-2  L-1
		//单元参数: *    *    *   ~~   R29  R28  PCE  PCH  PCL  SDE  SDH  SDL
		//堆栈指针: *    *    *   ~~   /^\   *    *    *    *    *    *    * 
		[uint8*STACK_LENGTH] Stack;
		
		//进程占用的资源(事件代码)计数器, 当计数器不等于0时表示对应的事件正在被占用
		->uint8 e_res_count;
		
		//进程是否被使用
		bool open;
		
		//当前进程所绑定的事件指针
		uint32 EventProc;
		
		//任务的当前SP指针
		uint16 SP;
	}
	[struct TCB * TASK_NUMBER] AppTCB;
	
	//每个任务占据的存储空间大小
	const uint16 STACK_LENGTH = 200;
	
	//当前任务号
	uint16 CurrentTaskIndex;
	
	//所需的最小存储边界
	const uint16 TASK_NUMBER = 5;
	//...................
	
	//堆栈指针
	//uint16 SP = #addr 0x5D;
	
	//---------------------------------------------------
	//调度器内核初始化
	void KernelInit()
	{
		CurrentTaskIndex = 0;
		for( uint8 i = 0; i < TASK_NUMBER; i + 1 ) {
			AppTCB[i].open = false;
		}
	}
	//---------------------------------------------------
	//开始运行
	public void Start()
	{
		uint16 BSP = AppTCB[0].SP;
		#asm "加载_SP_local_uint16 &BSP"
		//#asm "POP R29"
		//#asm "POP R28"
		#asm "出栈 DP"
		#asm "中断返回"
	}
	//---------------------------------------------------
	//进行一次调度, 把当前局部变量基指针保存到当前任务的堆栈区,
	//然后切换到下一个任务,弹出对应的局部变量基指针和PC,运行下一个任务
	public void Schedule()
	{
		//#asm "PUSH R28"
		//#asm "PUSH R29"
		#asm "压栈 DP"
		
		uint16 BSP;
		#asm "保存_SP_local_uint16 &BSP"
		AppTCB[CurrentTaskIndex].SP = BSP;
		
		//搜索下一个就绪的进程
		forever {
			CurrentTaskIndex + 1;
			CurrentTaskIndex % TASK_NUMBER;
			if( AppTCB[CurrentTaskIndex].open ) {
				break;
			}
		}
		//准备切换到继续的进程
		BSP = AppTCB[CurrentTaskIndex].SP;
		
		//一定要关中断,因为 SP 是一个16位变量,不允许访问期间被中断
		//#asm "CLI"
		#asm "加载_SP_local_uint16 &BSP"
		//#asm "SEI"
		
		//#asm "POP R29"
		//#asm "POP R28"
		#asm "出栈 DP"
		
		#asm "返回"
	}
	//---------------------------------------------------
	//手工触发执行一次指定编号的事件
	public void TrigEvent( uint8 i )
	{
		//#asm "PUSH R28"
		//#asm "PUSH R29"
		#asm "压栈 DP"
		
		uint16 BSP;
		#asm "保存_SP_local_uint16 &BSP"
		AppTCB[CurrentTaskIndex].SP = BSP;
		
		CurrentTaskIndex = i;
		
		//准备切换到继续的进程
		BSP = AppTCB[CurrentTaskIndex].SP;
		
		//一定要关中断,因为 SP 是一个16位变量,不允许访问期间被中断
		//#asm "CLI"
		#asm "加载_SP_local_uint16 &BSP"
		//#asm "SEI"
		
		//#asm "POP R29"
		//#asm "POP R28"
		#asm "出栈 DP"
		
		#asm "返回"
	}
	//---------------------------------------------------
	//注入一个事件到调度器事件列表
	public uint8 CreateTask( uint32 Proc )
	{
		//临时辅助变量, 用于引用类型数据的底层访问
		uint16 NP;
		->uint8 P;
		
		//搜索一个空闲的进程, 如果线程池满了的话, 就自动调度一次进行释放
		uint8 i = 0;
		forever {
			if( !AppTCB[i].open ) {
				break;
			}
			i + 1;
			//i % TASK_NUMBER;
			if( i == TASK_NUMBER ) {
				i = 0;
				Schedule();
			}
		}
		//绑定当前进程的事件指针
		AppTCB[i].EventProc = Proc;
		
		//设置事件的运行起点
		AppTCB[i].Stack[STACK_LENGTH - 4] = Proc.0(uint8);
		AppTCB[i].Stack[STACK_LENGTH - 5] = Proc.8(uint8);
		AppTCB[i].Stack[STACK_LENGTH - 6] = Proc.16(uint8);
		
		//设置事件的沙箱,保证事件安全退出
		uint32 SandboxHandle = #addr EventSandbox;
		AppTCB[i].Stack[STACK_LENGTH - 1] = SandboxHandle.0(uint8);
		AppTCB[i].Stack[STACK_LENGTH - 2] = SandboxHandle.8(uint8);
		AppTCB[i].Stack[STACK_LENGTH - 3] = SandboxHandle.16(uint8);
		
		//设置事件变量空间
		P -> AppTCB[i].Stack[0];
		
		//#asm "LDD r30,y+&P"
		//#asm "LDD r31,y+&P+1"
		//#asm "STD y+&NP,r30"
		//#asm "STD y+&NP+1,r31"
		#asm "加载_0_local_uint16 &P"
		#asm "保存_0_local_uint16 &NP"
		
		AppTCB[i].Stack[STACK_LENGTH - 7] = NP.0(uint8);
		AppTCB[i].Stack[STACK_LENGTH - 8] = NP.8(uint8);
		
		//设置进程的事件资源计数器
		if( i != 0 ) {
			AppTCB[i].e_res_count -> AppTCB[CurrentTaskIndex].e_res_count;
			AppTCB[i].e_res_count + 1;
		}
		
		//设置堆栈指针起点,先指向堆栈结尾,再移动6个PC指针(保护沙箱 和 事件起始点) 和 2个软件堆栈指针的空间
		P -> AppTCB[i].Stack[STACK_LENGTH - 1 - 8];
		
		//#asm "LDD r30,y+&P"
		//#asm "LDD r31,y+&P+1"
		//#asm "STD y+&NP,r30"
		//#asm "STD y+&NP+1,r31"
		#asm "加载_0_local_uint16 &P"
		#asm "保存_0_local_uint16 &NP"
		
		AppTCB[i].SP = NP;
		
		AppTCB[i].open = true;

		return i;
	}
	//---------------------------------------------------
	//立即结束指定的事件
	public void DeleteTask( uint32 Proc )
	{
		//遍历事件进程池
		uint8 i = 0;
		loop( TASK_NUMBER ) {
			if( AppTCB[i].open && AppTCB[i].EventProc == Proc ) {
				AppTCB[i].e_res_count = 0;
				AppTCB[i].open = false;
			}
			i + 1;
		}
	}
	//---------------------------------------------------
	//事件运行沙箱, 确保事件安全退出
	void EventSandbox()
	{
		AppTCB[CurrentTaskIndex].e_res_count - 1;
		
		//占用用户事件空间,跳过6个存储单元(两个PC指针),防止在下边的代码中干扰事件堆栈初始化
		uint16 BSP;
		//#asm "CLI"
		#asm "保存_SP_local_uint16 &BSP"
		BSP - 6;
		#asm "加载_SP_local_uint16 &BSP"
		//#asm "SEI"
		
		AppTCB[CurrentTaskIndex].open = false;
		Schedule();
	}
}







