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

📄 ide.c

📁 小型操作系统,以VC为开发环境,需要boachs调试
💻 C
📖 第 1 页 / 共 2 页
字号:
				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:			IdeReadWriteBlock
**      Author:                 x.cheng
**
**      Comment:
**			从ide设备读写一块数据。
**
**      List of parameters:
**			iType - IDE_READ_BLK / IDE_WRITE_BLK
**			pstBlock - address for system buffer
**
**      Return value:   
**          none
**
**      Revisions:
**
*************************************************************
*************************************************************/
void IdeReadWriteBlock( int iCmd, ts_BufferBlock* pstBlock)
{

	//POSITION();

	IdeMakeRequest(iCmd, pstBlock);
	#ifdef _DEBUG__
	#ifdef _DEBUG_IDE__
	POSITION();
	#endif
	#endif
}


//----------------------------------------------------------------------------------
//==============================local function======================================
//----------------------------------------------------------------------------------

/************************************************************
*************************************************************
**      Function Name:			IdeLockBuffer
**      Author:                 x.cheng
**
**      Comment:
**			独占缓冲区块。
**
**      List of parameters:
**			pstBlock - address for system buffer block
**
**      Return value:   
**          none
**
**      Revisions:
**
*************************************************************
*************************************************************/
static inline void IdeLockBuffer(ts_BufferBlock* pstBlock)
{
	vCLI();

	while ( pstBlock->ucLocked )
		vMtsTaskSleep ( &pstBlock->pstWaitTask );
	pstBlock->ucLocked = 1;

	vSTI();
}

/************************************************************
*************************************************************
**      Function Name:			IdeUnLockBuffer
**      Author:                 x.cheng
**
**      Comment:
**			取消独占。
**
**      List of parameters:
**			pstBlock - address for system buffer block
**
**      Return value:   
**          none
**
**      Revisions:
**
*************************************************************
*************************************************************/
static inline void IdeUnlockBuffer( ts_BufferBlock* pstBlock)
{
	if ( !pstBlock->ucLocked )
		kprintf("\n%s:%s:%d line, buffer not locked...\n", __FILE__,__FUNCTION__,__LINE__);

	pstBlock->ucLocked = 0;
	vMtsTaskWakeup( pstBlock->pstWaitTask );
}

/************************************************************
*************************************************************
**      Function Name:			IdeMakeRequest
**      Author:                 x.cheng
**
**      Comment:
**			为ide设备的读写建立一个请求结构,并加入处理队列。
**
**      List of parameters:
**			iType - IDE_READ_BLK / IDE_WRITE_BLK
**			pstBlock - address for system buffer
**
**      Return value:   
**          none
**
**      Revisions:
**
*************************************************************
*************************************************************/
static void IdeMakeRequest( int iCmd, ts_BufferBlock* pstBlock)
{
	ts_IdeRequest* pstRequest;

	if (iCmd != IDE_READ_BLK && iCmd != IDE_WRITE_BLK)
		panic("\n%s:%s:%d line, Bad ide command...\n", __FILE__,__FUNCTION__,__LINE__);

	IdeLockBuffer( pstBlock );

	if ( (iCmd==IDE_WRITE_BLK && !pstBlock->ucDirty) || 
		 (iCmd==IDE_READ_BLK && pstBlock->ucUpToDate) ) {
		IdeUnlockBuffer( pstBlock );

		return;
	}

lbl_Repeat:
//------------------------------------------------------------------------------
//			we don't allow the write-requests to fill up the queue completely:
//		we want some room for reads: they take precedence. The last third
//		of the requests are only for reads.
//------------------------------------------------------------------------------
	if ( iCmd==IDE_READ_BLK )
		pstRequest = g_astRequest+MAX_REQUEST_NR;
	else
		pstRequest = g_astRequest+( (MAX_REQUEST_NR*2)/3 );

	//find empty request.
	while ( --pstRequest ) {
		if (pstRequest->iDevice < 0)
			break;
	}
	//if none found, sleep on new requests
	if (pstRequest < g_astRequest) {
		g_pstWaitForRequest = pstMtsGetCurrentTask();
		vMtsTaskSleep( &g_pstWaitForRequest );
		goto lbl_Repeat;
	}
	//fill up the request-info, and add it to the queue
	pstRequest->iDevice = (int)pstBlock->uiDevice;
	pstRequest->ucCmd = (unsigned char)iCmd;
	pstRequest->ucError	= 0;
	if ( pstBlock->uiDevice == IDE_DEVICE_HD ) {
		//一个block 是2046byte=512*4, 所以,相应的block转换成LBA扇区号就是
		pstRequest->ulStartOfSector = pstBlock->ulBlockNr<<2;	
		pstRequest->ulNumOfSectors = 4;
	} else if ( pstBlock->uiDevice == IDE_DEVICE_CD ) {
		//一个block 是2046byte=1*2048
		pstRequest->ulStartOfSector = pstBlock->ulBlockNr;	
		pstRequest->ulNumOfSectors = 1;
	}
	pstRequest->pucBuffer = pstBlock->pucBlock;
	pstRequest->pstBlock = pstBlock;
	pstRequest->pstNext = NULL;

#ifdef _DEBUG__
#ifdef _DEBUG_IDE__
	kprintf("new request: %d, %d, %ld, %ld, %p, %p\n", 
			pstRequest->iDevice, pstRequest->ucCmd, 
		    pstRequest->ulStartOfSector, pstRequest->ulNumOfSectors, 
			pstRequest->pucBuffer, pstRequest->pstBlock);
#endif
#endif
	//POSITION();

	IdeAddRequest( &g_astDevice[pstRequest->iDevice], pstRequest );
	#ifdef _DEBUG__
	#ifdef _DEBUG_IDE__
	POSITION();
	#endif
	#endif
}

/************************************************************
*************************************************************
**      Function Name:			IdeAddRequest
**      Author:                 x.cheng
**
**      Comment:
**			adds a request to the linked list.
**		It disables interrupts so that it can muck with the
**		request-lists in peace.
**
**      List of parameters:
**			pstDevice - 
**			pstRequest - 
**
**      Return value:   
**          none
**
**      Revisions:
**
*************************************************************
*************************************************************/
static void IdeAddRequest(ts_IdeDevice* pstDevice, ts_IdeRequest* pstRequest)
{
	ts_IdeRequest *pstTmp;
	unsigned long ulFlags;

	//POSITION();

	pstRequest->pstNext = NULL;
	SaveEflagsAndCli(ulFlags);

	if ( pstRequest->pstBlock ) 
		pstRequest->pstBlock->ucDirty = 0;
	if ( !(pstTmp=pstDevice->pstCurrentRequest) ) {
		pstDevice->pstCurrentRequest = pstRequest;
		RestoreEflags(ulFlags);
		(pstDevice->fpRequestFct)();
		return;
	}

	//POSITION();

	for( ; pstTmp->pstNext; pstTmp=pstTmp->pstNext) {
		if (	( ELEVATOR_IN_ORDER(pstTmp, pstRequest) || 
			      ELEVATOR_IN_ORDER(pstTmp, pstTmp->pstNext) ) && 
			 ELEVATOR_IN_ORDER(pstRequest, pstTmp->pstNext)   ) {
		  break;
		}
	}
	pstRequest->pstNext = pstTmp->pstNext;
	pstTmp->pstNext = pstRequest;

	RestoreEflags(ulFlags);
	return;
}

/************************************************************
*************************************************************
**      Function Name:			IdeEndRequest
**      Author:                 x.cheng
**
**      Comment:
**
**      List of parameters:
**			pstDevice - 
**			pstRequest - 
**
**      Return value:   
**          none
**
**      Revisions:
**
*************************************************************
*************************************************************/
inline void IdeEndRequest(int iDevice, unsigned char ucUpToDate)
{
	ts_IdeRequest* pstCurrent;

#ifdef _DEBUG__
#ifdef _DEBUG_IDE__
			POSITION();
#endif
#endif
	if ( !(pstCurrent=g_astDevice[iDevice].pstCurrentRequest) ) {
		panic("\n%s:%s:%d line, no request...\n", __FILE__,__FUNCTION__,__LINE__);
		return;
	}

//	kprintf("iDevice =%d, pstCurrent= %p\n", iDevice, pstCurrent);

	if (pstCurrent->pstBlock) {
		pstCurrent->pstBlock->ucUpToDate = ucUpToDate;
		IdeUnlockBuffer(pstCurrent->pstBlock);
	}

	if ( !ucUpToDate ) {
		kprintf(" I/O error\n\rdev %04x, block %d\n\r", pstCurrent->iDevice, pstCurrent->pstBlock->ulBlockNr);
	}

	vMtsTaskWakeup(pstCurrent->pstWaitTask);
	vMtsTaskWakeup(g_pstWaitForRequest);

//	pstCurrent->iDevice = -1;
	g_astDevice[iDevice].pstCurrentRequest = pstCurrent->pstNext;
}

⌨️ 快捷键说明

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