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

📄 vga.c

📁 小型操作系统,以VC为开发环境,需要boachs调试
💻 C
字号:
/***************************************************************************
**     File name   : vga.c
**     Author      : 
**     Create date :
**
**	   Comment:
**        vga驱动程序....
**
**     Revisions:
**     $Log: vga.c,v $
**     Revision 1.4  2005/08/05 15:06:02  x.cheng
**     init at boot time using abs video address(0xb8000)
**
**     Revision 1.3  2005/07/27 15:57:00  x.cheng
**     change function prototype -vVgaSetCursorSize()
**
**     Revision 1.2  2005/07/27 07:07:29  x.cheng
**     bug fix for funciton vVgaMoveCursor();
**
**     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"
#include "string.h"

#include "..\..\Inc\i386\io.h"
#include "..\..\Inc\i386\system.h"
#include "..\..\Inc\vga.h"
#include "..\..\Inc\tui.h"

#include "..\inc\def_vga.h" 
  
/*********************************************
 * global variable declaration
 *********************************************/
int g_iCrtColumns	= 80;
int g_iCrtRows		= 25;

/************************************************************
*************************************************************
**      Function Name:			vVgaInit
**      Author:                 x.cheng
**
**      Comment:
**		 初始化vga显示器
**
**      List of parameters:
**			no
**
**      Return value:   
**			no
**
**      Revisions:
**
*************************************************************
*************************************************************/ 
void vVgaInit(void)
{
	// set the size of screen;
	g_iCrtColumns = 80;
	g_iCrtRows = 25;

	//clear the screen
	memsetw((unsigned short*)(0xB8000), BLANK, g_iCrtColumns*g_iCrtRows);
	
	//set the cursor size and position
	vVgaSetCursorSize(15, 16);
	vVgaMoveCursor( 0 );

	return;
}

/************************************************************
*************************************************************
**      Function Name:			vVgaSetCursorSize
**      Author:                 x.cheng
**
**      Comment:
**		 设置光标的尺寸
**
**      List of parameters:
**			ucWidth	
**			ucHeight
**
**      Return value:   
**			no
**
**      Revisions:
**
*************************************************************
*************************************************************/ 
void vVgaSetCursorSize(unsigned char ucStartLine, unsigned char ucEndLine)
{
	unsigned long ulFlags;

	SaveEflagsAndCli(ulFlags);
	
	vOutPort( VGA_CRTC_INDX, 0x0a);
	vOutPort( VGA_CRTC_DATA, ucStartLine);
	vOutPort( VGA_CRTC_INDX, 0x0b);
	vOutPort( VGA_CRTC_DATA, ucEndLine);

	RestoreEflags(ulFlags);
}

/************************************************************
*************************************************************
**      Function Name:			vVgaMoveCursor
**      Author:                 x.cheng
**
**      Comment:
**		 移动光标的位置
**
**      List of parameters:
**			uiPos
**
**      Return value:   
**			no
**
**      Revisions:
**
*************************************************************
*************************************************************/ 
void vVgaMoveCursor(unsigned short uiPos)
{
	unsigned long ulFlags;

	SaveEflagsAndCli(ulFlags);

	vOutPort( VGA_CRTC_INDX, 0x0E );
	vOutPort( VGA_CRTC_DATA, uiPos >> 8);
	vOutPort( VGA_CRTC_INDX, 0x0F );
	vOutPort( VGA_CRTC_DATA, uiPos & 0xff);

	RestoreEflags(ulFlags);
}
//-------------------------------------------------------------
void vVgaConsoleGotoXy(ts_Console *pstConsole, int iX, int iY)
{
	unsigned long ulFlags;

	//Round x, y to CRT bounds...
	if ( iX != -1 )	iX = iX % g_iCrtColumns;
	else	iX = pstConsole->uiCurrentPos % g_iCrtColumns;
		//----
	if ( iY != -1 )	iY = iY % g_iCrtRows;
	else	iY = pstConsole->uiCurrentPos / g_iCrtColumns;

	SaveEflagsAndCli(ulFlags);
	
	pstConsole->uiCurrentPos = iY * g_iCrtColumns + iX;

	//Update the cursor position only for current console.

	if( pstConsole==pstTuiGetConsoleAddress(0) )
		vVgaMoveCursor( pstConsole->uiCurrentPos );

	RestoreEflags(ulFlags);
}

/************************************************************
*************************************************************
**      Function Name:			iVgaConsoleSetPos
**      Author:                 x.cheng
**
**      Comment:
**		 Set the cursor position of the selected console.
**
**      List of parameters:
**			pstConsole - pstConsole The selected pstConsole address.
**			uiPos	- The cursor position (in characters from the beginning
**					of the video buffer).
**
**      Return value:   
**			TRUE on success;
**			FALSE if the position is over the buffer.
**
**      Revisions:
**
*************************************************************
*************************************************************/
int iVgaConsoleSetPos(ts_Console *pstConsole, unsigned short uiPos)
{
	unsigned long ulFlags;

	if ( uiPos < (g_iCrtColumns*g_iCrtRows) ) {
		SaveEflagsAndCli(ulFlags);
		pstConsole->uiCurrentPos = uiPos;
		// Update the cursor position only for current console.
		if ( pstConsole==pstTuiGetConsoleAddress(0) )
			vVgaMoveCursor( pstConsole->uiCurrentPos );

		RestoreEflags(ulFlags);

		return( TRUE );
	}
	return( FALSE );

}
/************************************************************
*************************************************************
**      Function Name:			vVgaConsoleClr
**      Author:                 x.cheng
**
**      Comment:
**		 Clear the screen of the selected Console.
**
**      List of parameters:
**			pstConsole - pstConsole The selected pstConsole address.
**
**      Return value:   
**			no
**
**      Revisions:
**
*************************************************************
*************************************************************/
void vVgaConsoleClr(ts_Console *pstConsole)
{
	unsigned long ulFlags;

	SaveEflagsAndCli(ulFlags);

	memsetw( (unsigned short *)(pstConsole->puiVideoBuffer),
		BLANK,
		g_iCrtColumns*g_iCrtColumns );

	pstConsole->uiCurrentPos = 0;

	// Update the cursor position only for current pstConsole.
	if( pstConsole==pstTuiGetConsoleAddress(0) )
		vVgaMoveCursor( pstConsole->uiCurrentPos );

	RestoreEflags(ulFlags);
}

/************************************************************
*************************************************************
**      Function Name:			vVgaConsolePutChar
**      Author:                 x.cheng
**
**      Comment:
**		 Clear the screen of the selected Console.
**
**      List of parameters:
**			pstConsole - pstConsole The selected pstConsole address.
**			ch	-	The ASCII code of the character to print.
**
**      Return value:   
**			no
**
**      Revisions:
**
*************************************************************
*************************************************************/
void vVgaConsolePutChar(ts_Console *pstConsole, const char ch)
{
	unsigned long ulFlags;

	SaveEflagsAndCli(ulFlags);

	switch (ch){
	case '\n':
		(pstConsole->uiCurrentPos)+=g_iCrtColumns;
		(pstConsole->uiCurrentPos)-=(pstConsole->uiCurrentPos)%g_iCrtColumns;
		break;
	case '\r':
		(pstConsole->uiCurrentPos)-=(pstConsole->uiCurrentPos)%g_iCrtColumns;
		break;
	case '\b':
		if ((pstConsole->uiCurrentPos) > 0) {
			(pstConsole->puiVideoBuffer)[--(pstConsole->uiCurrentPos)] =
			((pstConsole->ucCurrentColor) << 8 ) | ' ';
		}
		break;
	case '\t':
		(pstConsole->uiCurrentPos)+=8;
		break;
	case '\a': // CTRL+G => system beep! //
		//beep();
		RestoreEflags(ulFlags);
		return;
		break;

	default:
		(pstConsole->puiVideoBuffer)[pstConsole->uiCurrentPos] = ((pstConsole->ucCurrentColor) << 8) | ch;
		(pstConsole->uiCurrentPos)++;
		break;
	}

	// Check if cursor is at the bottom of the screen.
	if ( pstConsole->uiCurrentPos >= (g_iCrtColumns * g_iCrtRows) )
		vVgaConsoleScrollUp(pstConsole);

	// Update the cursor position only for current pstConsole.
	if( pstConsole==pstTuiGetConsoleAddress(0) )
		vVgaMoveCursor( pstConsole->uiCurrentPos );


	RestoreEflags(ulFlags);
}

/************************************************************
*************************************************************
**      Function Name:			vVgaConsolePutString
**      Author:                 x.cheng
**
**      Comment:
**		 Clear the screen of the selected Console.
**
**      List of parameters:
**			pstConsole - pstConsole The selected pstConsole address.
**			szBuffer   - string of the ASCII code character to print.
**
**      Return value:   
**			no
**
**      Revisions:
**
*************************************************************
*************************************************************/
void vVgaConsolePutString(ts_Console *pstConsole, const char* szBuffer)
{
	char ch;

	while ( ( ch = *szBuffer++ ) != '\0' ) {
		vVgaConsolePutChar(pstConsole, ch);
	}

}
/************************************************************
*************************************************************
**      Function Name:			vVgaConsoleSetColor
**      Author:                 x.cheng
**
**      Comment:
**		 Clear the screen of the selected Console.
**
**      List of parameters:
**			pstConsole - The selected console address.
**			ucColor	- The current color code (see console.h).
**
**      Return value:   
**			no
**
**      Revisions:
**
*************************************************************
*************************************************************/
void vVgaConsoleSetColor(ts_Console *pstConsole, unsigned char ucColor)
{
	pstConsole->ucCurrentColor = ucColor;
}

/************************************************************
*************************************************************
**      Function Name:			vVgaConsoleScrollUp
**      Author:                 x.cheng
**
**      Comment:
**		 Scroll up the screen of the selected console.
**
**      List of parameters:
**			pstConsole - The selected console address.
**
**      Return value:   
**			no
**
**      Revisions:
**
*************************************************************
*************************************************************/
void vVgaConsoleScrollUp(ts_Console *pstConsole)
{
	unsigned long ulFlags;

	SaveEflagsAndCli(ulFlags);

	// Scroll up.
	memcpy( (void *)(pstConsole->puiVideoBuffer),
		((void *)(pstConsole->puiVideoBuffer))+g_iCrtColumns*2,
		g_iCrtColumns*(g_iCrtRows-1)*2 );

	// Blank the bottom line of the screen.
	memsetw((void *)( ((void *)(pstConsole->puiVideoBuffer)) + g_iCrtColumns*(g_iCrtRows-1)*2),
		 BLANK, g_iCrtColumns);

	(pstConsole->uiCurrentPos) -= g_iCrtColumns;

	// Update the cursor position only for current console.
	if( pstConsole==pstTuiGetConsoleAddress(0) )
		vVgaMoveCursor( pstConsole->uiCurrentPos );

	RestoreEflags(ulFlags);
}

//----------------------------------------------------
int iVgaGetCrtColumns()
{
	return g_iCrtColumns;
}
int iVgaGetCrtRows()
{
	return g_iCrtRows;
}

⌨️ 快捷键说明

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