cpu_defs.c

来自「嵌入式系统开发 TOPPERS and JSP Kernel Release 」· C语言 代码 · 共 350 行

C
350
字号
/* *  TOPPERS/JSP Kernel *      Toyohashi Open Platform for Embedded Real-Time Systems/ *      Just Standard Profile Kernel *  *  Copyright (C) 2000-2002 by Embedded and Real-Time Systems Laboratory *                              Toyohashi Univ. of Technology, JAPAN *  *  惧淡螟侯涪荚は·Free Software Foundation によって给山されている  *  GNU General Public License の Version 2 に淡揭されている掘凤か·笆 *  布の(1)×(4)の掘凤を塔たす眷圭に嘎り·塑ソフトウェア∈塑ソフトウェ *  アを猖恃したものを崔むˉ笆布票じ∷を蝗脱ˇ剩澜ˇ猖恃ˇ浩芹邵∈笆布· *  网脱と钙ぶ∷することを痰浸で钓满するˉ *  (1) 塑ソフトウェアをソ〖スコ〖ドの妨で网脱する眷圭には·惧淡の螟侯 *      涪山绩·この网脱掘凤および布淡の痰瘦沮惮年が·そのままの妨でソ〖 *      スコ〖ド面に崔まれていることˉ *  (2) 塑ソフトウェアを浩网脱材墙なバイナリコ〖ド∈リロケ〖タブルオブ *      ジェクトファイルやライブラリなど∷の妨で网脱する眷圭には·网脱 *      に燃うドキュメント∈网脱荚マニュアルなど∷に·惧淡の螟侯涪山绩· *      この网脱掘凤および布淡の痰瘦沮惮年を非很することˉ *  (3) 塑ソフトウェアを浩网脱稍材墙なバイナリコ〖ドの妨または怠达に寥 *      み哈んだ妨で网脱する眷圭には·肌のいずれかの掘凤を塔たすことˉ *    (a) 网脱に燃うドキュメント∈网脱荚マニュアルなど∷に·惧淡の螟侯 *        涪山绩·この网脱掘凤および布淡の痰瘦沮惮年を非很することˉ *    (b) 网脱の妨轮を·侍に年める数恕によって·惧淡螟侯涪荚に鼠桂する *        ことˉ *  (4) 塑ソフトウェアの网脱により木儡弄または粗儡弄に栏じるいかなる禄 *      巢からも·惧淡螟侯涪荚を倘勒することˉ *  *  塑ソフトウェアは·痰瘦沮で捏丁されているものであるˉ惧淡螟侯涪荚は· *  塑ソフトウェアに簇して·その努脱材墙拉も崔めて·いかなる瘦沮も乖わ *  ないˉまた·塑ソフトウェアの网脱により木儡弄または粗儡弄に栏じたい *  かなる禄巢に簇しても·その勒扦を砷わないˉ *  *  @(#) $Id: cpu_defs.c,v 1.4 2002/04/10 11:20:09 takayuki Exp $ */#include "hal_msg.h"#include "cpu_defs.h"#include "cpu_config.h"/* * m68kっぽい充哈みエミュレ〖タ (Windows HAL) */struct tagInterruptLevel InterruptLevel[INT_NUMINTERRUPTS];CRITICAL_SECTION InterruptCriticalSection;unsigned int CurrentInterruptLevel;#define CHECK_IMS(x) ( (x) != 0 && (x) <= INT_NUMINTERRUPTS )struct tagExceptionLevel ExceptionLevel[EXC_MAXITEMS];static LPTOP_LEVEL_EXCEPTION_FILTER AnotherExceptionFilter;static unsigned intisns_int( unsigned int ipl ){	unsigned int result = INT_NUMINTERRUPTS+1;	while( result > ipl)	{		result --;		if( (InterruptLevel[result].Status & (INT_STAT_PENDING|INT_STAT_RUNNING)) != 0)			return result+1;	}	while( result > 0)	{		result --;		if( (InterruptLevel[result].Status & INT_STAT_RUNNING) != 0)			return result+1;	}	return 0;}DWORD WINAPIInterruptHandlerWrapper(LPVOID param){	unsigned int i;	unsigned int PrevLevel;	struct tagInterruptLevel * intlv = (struct tagInterruptLevel *)param;	while(1==1)	{			/* 充哈み涟借妄 */		EnterCriticalSection(&InterruptCriticalSection);		PrevLevel = CurrentInterruptLevel;		CurrentInterruptLevel = (unsigned int)(intlv - InterruptLevel)+1;		intlv->Status &= ~INT_STAT_PENDING;		intlv->Status |= INT_STAT_RUNNING;		LeaveCriticalSection(&InterruptCriticalSection);			/* 充哈みハンドラの弹瓢 */		( (void (*)(void) )(intlv->Routine))();			/* 充哈み稿借妄 */		EnterCriticalSection(&InterruptCriticalSection);		intlv->Status &= ~INT_STAT_RUNNING;		i = isns_int(PrevLevel);		CurrentInterruptLevel = PrevLevel;		LeaveCriticalSection(&InterruptCriticalSection);					/* ディスパッチル〖チンの弹瓢 */		if(i != 0)			HALInterruptRequestAndWait(0);		else			HALDispatchRequest(NULL);	}	ExitThread(0);	return 0;}BOOLdef_int(unsigned int ims, void * rtn){	if(!CHECK_IMS(ims))		return FALSE;	ims--;		EnterCriticalSection(&InterruptCriticalSection);	if(InterruptLevel[ims].ThreadHandle != INVALID_HANDLE_VALUE)	{		TerminateThread(InterruptLevel[ims].ThreadHandle,0);		CloseHandle(InterruptLevel[ims].ThreadHandle);	}	InterruptLevel[ims].Routine = rtn;	InterruptLevel[ims].ThreadHandle = CreateThread(NULL,0,InterruptHandlerWrapper,(LPVOID)&InterruptLevel[ims],CREATE_SUSPENDED,&InterruptLevel[ims].ThreadID);	LeaveCriticalSection(&InterruptCriticalSection);	return TRUE;}BOOLini_int(void){	int i;	InitializeCriticalSection(&InterruptCriticalSection);	EnterCriticalSection(&InterruptCriticalSection);	for(i=0;i<INT_NUMINTERRUPTS;i++)	{		InterruptLevel[i].Routine = (void *)0l;		InterruptLevel[i].ThreadHandle = INVALID_HANDLE_VALUE;		InterruptLevel[i].ThreadID = 0;		InterruptLevel[i].Status = 0;	}	CurrentInterruptLevel = 0;	LeaveCriticalSection(&InterruptCriticalSection);	return TRUE;}voidfin_int(void){	int i;	EnterCriticalSection(&InterruptCriticalSection);	for(i=0;i<INT_NUMINTERRUPTS;i++)	{		if(InterruptLevel[i].ThreadHandle != INVALID_HANDLE_VALUE)		{			TerminateThread(InterruptLevel[i].ThreadHandle,0);			CloseHandle(InterruptLevel[i].ThreadHandle);		}		InterruptLevel[i].Routine = (void *)0l;		InterruptLevel[i].ThreadHandle = INVALID_HANDLE_VALUE;		InterruptLevel[i].ThreadID = 0;		InterruptLevel[i].Status = 0;	}	CurrentInterruptLevel = 0;	LeaveCriticalSection(&InterruptCriticalSection);	DeleteCriticalSection(&InterruptCriticalSection);}BOOLras_int(unsigned int ims){	BOOL result;	if(!CHECK_IMS(ims))		return FALSE;	ims --;	EnterCriticalSection(&InterruptCriticalSection);	if(InterruptLevel[ims].ThreadHandle == INVALID_HANDLE_VALUE)	{		result = FALSE;	}else		InterruptLevel[ims].Status = INT_STAT_PENDING;	LeaveCriticalSection(&InterruptCriticalSection);	return result;}unsigned intsns_int( void ){	int result;	EnterCriticalSection(&InterruptCriticalSection);	result = isns_int(CurrentInterruptLevel);	LeaveCriticalSection(&InterruptCriticalSection);	return result;}HANDLEsch_int( void ){	HANDLE result;	unsigned int level;	EnterCriticalSection(&InterruptCriticalSection);	level = isns_int(CurrentInterruptLevel);	if(level != 0)		result = InterruptLevel[level-1].ThreadHandle;	else		result = INVALID_HANDLE_VALUE;	LeaveCriticalSection(&InterruptCriticalSection);	return result;}ERena_int(unsigned int ims){	return chg_ims(0);}ERdis_int(unsigned int ims){	return chg_ims(INT_NUMINTERRUPTS+1);}ERchg_ims(unsigned int ims){	int i;	if(!CHECK_IMS(ims))		return -17 /*E_PAR*/;	EnterCriticalSection(&InterruptCriticalSection);	CurrentInterruptLevel = ims;	i = isns_int(ims);	LeaveCriticalSection(&InterruptCriticalSection);	if(i != 0)		HALInterruptRequest(0);	return 0 /*E_OK*/;}ERget_ims(unsigned int *p_ims){	if(p_ims == (void *)0l)		return -17 /*E_PAR*/;	EnterCriticalSection(&InterruptCriticalSection);	*p_ims = CurrentInterruptLevel;	LeaveCriticalSection(&InterruptCriticalSection);	return 0 /*E_OK*/;}/* * 呵惧疤レベルWindows菇陇步毋嘲ハンドラ */LONG WINAPIHALExceptionHandler( EXCEPTION_POINTERS * exc ){	int i;	if((CPUStatus & CPU_STAT_EXC) == 0)	{		CPUStatus |= CPU_STAT_EXC;		for(i=0;i<EXC_MAXITEMS;i++)		{			if(ExceptionLevel[i].ExceptionCode == exc->ExceptionRecord->ExceptionCode)			{				i = EXCEPTION_CONTINUE_SEARCH;				( * ((void (*)(void *,int *))ExceptionLevel[i].Routine)) (exc,&i);				CPUStatus &= ~CPU_STAT_EXC;				return i;			}		}		CPUStatus &= ~CPU_STAT_EXC;	}	return EXCEPTION_CONTINUE_SEARCH;}BOOLini_exc(void){	int i;	for(i=0;i<EXC_MAXITEMS;i++)	{		ExceptionLevel[i].ExceptionCode = 0;		ExceptionLevel[i].Routine = 0l;	}	AnotherExceptionFilter = SetUnhandledExceptionFilter(HALExceptionHandler);		return TRUE;}voidfin_exc(void){}BOOLdef_exc(DWORD exc, void * routine){	int j;	int i;	if(routine == 0l)	{		for(i=0;i<EXC_MAXITEMS;i++)			if(ExceptionLevel[i].ExceptionCode == exc)			{				ExceptionLevel[i].ExceptionCode = 0;				ExceptionLevel[i].Routine = 0;				return TRUE;			}		return FALSE;	}	j = EXC_MAXITEMS;	for(i=0;i<EXC_MAXITEMS;i++)	{		if(ExceptionLevel[i].ExceptionCode != 0)		{			if(ExceptionLevel[i].ExceptionCode == exc)				return FALSE;		}else			if(j > i)				j = i;	}		//This sequence will never change	ExceptionLevel[j].Routine = routine;	ExceptionLevel[j].ExceptionCode = exc;	return TRUE;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?