⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ide.c

📁 小型操作系统,以VC为开发环境,需要boachs调试
💻 C
📖 第 1 页 / 共 2 页
字号:
/***************************************************************************
**     File name   : ide.c
**     Author      : x.cheng
**     Create date :
**
**	   Comment:
**        file used for something
**
**     Revisions: 
**     $Log: ide.c,v $
**     Revision 1.6  2005/08/11 14:42:34  x.cheng
**     ide channel 1 supurted, attach cdrom to channel 1, master
**     install channel 1 ISR to irq
**
**     Revision 1.5  2005/08/09 16:10:19  x.cheng
**     add IDE0,device 1(cdrom) irq handler
**
**     Revision 1.4  2005/08/04 08:41:23  x.cheng
**     new method for interrupt ISR install
**
**     Revision 1.3  2005/08/02 15:29:14  x.cheng
**     在函数vDoIdePriIRQ直接处理中断,目前调试完成的是IDE_READ_BLK。
**
**     Revision 1.2  2005/07/27 15:59:28  x.cheng
**     IDE request managment function added
**
**     Revision 1.1.1.1  2005/07/27 06:53:15  x.cheng
**     add into repositories
**
**
***************************************************************************/
#include "const.h"
#include "type.h"
#include "stdarg.h"

#define __IDE_HD_SRC__ 
#include "..\..\Inc\i386\io.h"
#include "..\..\Inc\i386\system.h"
#include "..\..\Inc\i386\x86.h"
#include "..\..\Inc\i386\interrupt.h"
#include "..\..\Inc\mts.h"
#include "..\..\Inc\tui.h"
#include "..\..\Inc\ide.h"
#include "..\..\Inc\debug.h"

#include "..\inc\def_hd.h"
#include "..\inc\def_ata.h"
#include "..\inc\def_ide.h"

/* debug preprocessor instrument
	#ifdef _DEBUG__
	#ifdef _DEBUG_IDE__
				
	#endif
	#endif	
***************************/

/*************取setup.asm设置的硬盘数据**************/
#define HD_INFO (ts_HD_HWinfo *)0x90080

/*************global variable******************/
	//-------------------------------------------
	// The request-struct contains all necessary data
	// to load a nr of sectors into memory
ts_IdeRequest g_astRequest[MAX_REQUEST_NR];

	//-------------------------------------------
	// used to wait on when there are no free requests
ts_Task* g_pstWaitForRequest = NULL;

	//-------------------------------------------
ts_IdeDevice g_astDevice[IDE_DEVICE_NR] = {
	{NULL, NULL},	//no device
	{NULL, NULL},	//fd
	{NULL, NULL},	//hd
	{NULL, NULL},	//cd
	{NULL, NULL}	//pr
};


/******ISR defined in hwintr.asm***********/
void vIdePriISR(void);


/************local function prototype**************/


static inline void IdeLockBuffer(ts_BufferBlock* pstBlock);
static inline void IdeUnlockBuffer( ts_BufferBlock* pstBlock);
static void IdeMakeRequest(int iCmd, ts_BufferBlock* pstBlock);
static void IdeAddRequest(ts_IdeDevice* pstDevice, ts_IdeRequest* pstRequest);


/************************************************************
*************************************************************
**      Function Name:			IdeInitialize
**      Author:                 x.cheng
**
**      Comment:
**			初始化ide设备,中断号是
**
**      List of parameters:
**			
**
**      Return value:   
**          return code, or information
**
**      Revisions:
**
*************************************************************
*************************************************************/
//void vHdInit(void)
void IdeInitialize(void)
{
	int i;

	#ifdef _DEBUG__
	#ifdef _DEBUG_IDE__
		kprintf("ide init....\n");
	#endif
	#endif	

	g_astDevice[IDE_DEVICE_HD].fpRequestFct = vDoHdRequest;
	g_astDevice[IDE_DEVICE_HD].pstCurrentRequest = NULL;
	g_astDevice[IDE_DEVICE_CD].fpRequestFct = DoCdRequest;
	g_astDevice[IDE_DEVICE_CD].pstCurrentRequest = NULL;

	for (i=0; i<MAX_REQUEST_NR; i++) {
		g_astRequest[i].iDevice = -1;
		g_astRequest[i].pstNext = NULL;
	}

	IsrInstallHandler(HWM_IDE_CHN0_IRQe, &vDoIdePriIRQ);
	IsrInstallHandler(HWM_IDE_CHN1_IRQf, &DoIdeSecIRQ);
} 

/************************************************************
*************************************************************
**      Function Name:			vDoIdePriIRQ
**      Author:                 x.cheng
**
**      Comment:
**			每次ide控制器中断会调用这个函数,本函数判定ide接口上
**		设备的种类,转到相应设备的处理函数上运行。
**
**      List of parameters:
**			no
**
**      Return value:   
**          no
**
**      Revisions:
**
*************************************************************
*************************************************************/
void vDoIdePriIRQ(ts_IrqContext *pstContext)
{
	ts_IdeRequest* pstCurrent=NULL;
	
#ifdef _DEBUG__
#ifdef _DEBUG_IDE__
	kprintf("Here is a ide interrupt occuring !\n");

#endif 
#endif
//	kprintf("device is= %x, ", ucId5InterruptDevice());
	switch (ucId5InterruptDevice(IDE_CHANNEL_0)) {
	case DEVICE_0:
//		kprintf("device 0\n");
		pstCurrent=g_astDevice[IDE_DEVICE_HD].pstCurrentRequest;
		break;
	case DEVICE_1:
//		kprintf("device 1\n");
		pstCurrent=g_astDevice[IDE_DEVICE_CD].pstCurrentRequest;
		break;
	}
	if ( !pstCurrent ) {
		POSITION1("no request...\n");
		return;
	}

	if ( !(g_astDevice[pstCurrent->iDevice].fpRequestFct) ) {
		panic("\n%s:%s:%d line, no request function...\n", __FILE__,__FUNCTION__,__LINE__);
		return;
	}

	if (pstCurrent->ucCmd == IDE_READ_BLK) {
		if ( ucId5CmdResult(IDE_CHANNEL_0) ) {
			if ( ++pstCurrent->ucError >= MAX_ERRORS ) {
#ifdef _DEBUG__
#ifdef _DEBUG_IDE__
				POSITION1("read request max error encounter...\n");
#endif
#endif
				IdeEndRequest(pstCurrent->iDevice, 0);
				(g_astDevice[pstCurrent->iDevice].fpRequestFct)();
			}
			return;
		}
		
		if ( pstCurrent->iDevice == IDE_DEVICE_HD ) {
			if ( !iId2ReadHdData(pstCurrent) )
				return;
		}
		else if ( pstCurrent->iDevice == IDE_DEVICE_CD ) {
			if ( !iId3ReadCdData(pstCurrent) )
				return;
		}

		IdeEndRequest(pstCurrent->iDevice, 1);
		(g_astDevice[pstCurrent->iDevice].fpRequestFct)();
		pstCurrent->iDevice = -1;	//free..
	} else if (pstCurrent->ucCmd == IDE_WRITE_BLK) {
		if ( ucId5CmdResult(IDE_CHANNEL_0) != DRQ_STAT) {
			if ( ++pstCurrent->ucError >= MAX_ERRORS ) {
#ifdef _DEBUG__
#ifdef _DEBUG_IDE__
				POSITION1("write request max error encounter...\n");
#endif
#endif
				IdeEndRequest(pstCurrent->iDevice, 0);
				(g_astDevice[IDE_DEVICE_HD].fpRequestFct)();
			}
			return;
		}
		//由中断调用,注意,一次中断只有一个扇区的数据(512bytes)
		if (-- pstCurrent->ulNumOfSectors) {
			pstCurrent->pucBuffer += 512;
			pstCurrent->ulStartOfSector ++;
			//WritePortString(HD_DATA, pstCurrent->pucBuffer, SECTOR_SIZE/2);	//same to code
			WritePortString(HD_DATA, pstCurrent->pucBuffer, SECTOR_SIZE);
			return;
		}
		IdeEndRequest(pstCurrent->iDevice, 1);
		(g_astDevice[IDE_DEVICE_HD].fpRequestFct)();
	} else {
#ifdef _DEBUG__
#ifdef _DEBUG_IDE__
		POSITION1("unexpected request....\n");
#endif
#endif
	}

	return;
}

/************************************************************
*************************************************************
**      Function Name:			vDoIdeSecIRQ
**      Author:                 x.cheng
**
**      Comment:
**			每次ide控制器中断(secondary)会调用这个函数,
**	本函数判定ide接口上设备的种类,转到相应设备的处理函数上运行。
**
**      List of parameters:
**			no
**
**      Return value:   
**          no
**
**      Revisions:
**
*************************************************************
*************************************************************/
void DoIdeSecIRQ(ts_IrqContext *pstContext)
{
	ts_IdeRequest* pstCurrent;
	
#ifdef _DEBUG__
#ifdef _DEBUG_IDE__
	kprintf("Here is a channel 1 ide interrupt occuring !\n");

#endif 
#endif
	if ( !(pstCurrent=g_astDevice[IDE_DEVICE_CD].pstCurrentRequest) ) {
		POSITION1("no request...\n");
		return;
	}

	if ( !(g_astDevice[pstCurrent->iDevice].fpRequestFct) ) {
		panic("\n%s:%s:%d line, no request function...\n", __FILE__,__FUNCTION__,__LINE__);
		return;
	}

	if (pstCurrent->ucCmd == IDE_READ_BLK) {
		if ( ucId5CmdResult(IDE_CHANNEL_1) ) {
			if ( ++pstCurrent->ucError >= MAX_ERRORS ) {
#ifdef _DEBUG__
#ifdef _DEBUG_IDE__ 
				POSITION1("read request max error encounter...\n");
#endif
#endif
				IdeEndRequest(pstCurrent->iDevice, 0);
				(g_astDevice[pstCurrent->iDevice].fpRequestFct)();
			}
			return;
		}
		
		if ( pstCurrent->iDevice == IDE_DEVICE_HD ) {
			if ( !iId2ReadHdData(pstCurrent) )
				return;
		}
		else if ( pstCurrent->iDevice == IDE_DEVICE_CD ) {
			if ( !iId3ReadCdData(pstCurrent) )
				return;
		}

		IdeEndRequest(pstCurrent->iDevice, 1);
		(g_astDevice[pstCurrent->iDevice].fpRequestFct)();
		pstCurrent->iDevice = -1;	//free..
	} else if (pstCurrent->ucCmd == IDE_WRITE_BLK) {
		if ( ucId5CmdResult(IDE_CHANNEL_1) != DRQ_STAT) {
			if ( ++pstCurrent->ucError >= MAX_ERRORS ) {
#ifdef _DEBUG__
#ifdef _DEBUG_IDE__
				POSITION1("write request max error encounter...\n");
#endif
#endif

⌨️ 快捷键说明

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