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

📄 tools.cpp

📁 PC燒錄BIOS的源程序
💻 CPP
字号:
#include "Tools.h"
#include <Dos.h>

//return the High Byte of one word
BYTE Hi(WORD Value)
{
	BYTE RetValue;
	RetValue = (Value & 0xFF00) >> 8;
	return RetValue;
}

//return the low Byte of one word
BYTE Lo(WORD Value)
{
	BYTE RetValue;
	RetValue = (Value & 0x00FF);
	return RetValue;
}

void FOMemByte(LONGINT Addr, BYTE Data)
{
    _asm{
		XOR AX,AX
		MOV ES,AX
		DB 66H
		MOV SI, WORD PTR Addr  //MOV ESI,Addr
		MOV AL,Data
		DB 26H,67H,88H,06H     //MOV ES:[ESI],AL
    }
}

BYTE FIMemByte(LONGINT Addr)
{
    BYTE RetVal;

    _asm{
		XOR AX,AX
		MOV ES,AX
		DB 66H
		MOV SI, WORD PTR Addr  //MOV ESI,Addr
		DB 26H,67H,8AH,06H     //MOV AL,ES:[ESI]
		MOV RetVal, AL
    }
    return RetVal;
}

//Write one WORD to the Specified Memory Location 
void FOMemWord(LONGINT Addr, WORD Data)
{
	_asm{
		XOR    AX,AX
		MOV    ES,AX
		DB     66H
		MOV    SI,WORD PTR Addr    ;//{MOV ESI,Addr}
		MOV    AX,Data
		DB     26H,67H,89H,06H     ;//{MOV ES:[ESI],AX}
	}
}

//Get one WORD from the Specified Memory Location 
WORD FIMemWord(LONGINT Addr)
{
	WORD RetVal;
	_asm{
		XOR    AX,AX
		MOV    ES,AX
		DB     66H
		MOV    SI,WORD PTR Addr    ;//{MOV ESI,Addr}
		DB     26H,67H,8BH,06H     ;//{MOV AX,ES:[ESI]}
		MOV    RetVal , AX
	}
	return RetVal;
}

//{Allocate block of memory, either conventional or XMS, }
//{return linear address error will return 0                                }
//if allocate in the XMS , then we will update the XMS Block Left and XMS Block Position
//if allocate in the DOS 
LONGINT AllocLinearBlock(LONGINT  BlockSize , LONGINT* pXMSBlockLeft , LONGINT* pXMSBlockPos)
{
	WORD Block;
	LONGINT ActualAddress = 0;
	
	//align the Size with DWORD  {DWORD ALIGN}
	BlockSize = ( BlockSize + 0x03 ) & 0xFFFFFFFC;
	
	//allocate Dos Mem and Get the Largest Free Block
	_asm{
		MOV   AH,48H 
		MOV   BX,0FFFFH 
		INT   21H
		MOV   Block,BX
	}

	if (BlockSize <= ( LONGINT(Block ) * 16 ))
	{
		Block = WORD (( BlockSize + 15 ) >> 4);
		// 
		_asm{
			MOV      AH,48H
			MOV      BX,Block
			INT      21H
			MOV      Block,AX
		}
		ActualAddress = LONGINT( Block ) * 16;
	}
	else //we allocate the Block in the XMS
	{
		if (BlockSize <= *pXMSBlockLeft )
		{
			*pXMSBlockLeft = *pXMSBlockLeft - BlockSize;
			ActualAddress  = *pXMSBlockPos;
			*pXMSBlockPos  = *pXMSBlockPos + BlockSize;
		}
	}
	
	return ActualAddress;
}
/*
//move linear Block  
void MoveLinBlockD(LONGINT Src, LONGINT Dest, LONGINT Size)
{
	_asm{
		XOR    AX,AX
		MOV    ES,AX
		DB     66H
		MOV    SI, WORD PTR (Src)
		DB     66H
		MOV    DI, WORD PTR (Dest)
		DB     66H
		MOV    CX, WORD PTR (Size)
		MOV    AL,CL
		DB     66H
		SHR    CX,2
		AND    AL,3
		CLD
		DB     26H
		DW     6667H
		REP    MOVSW        ;// {REP MOVSD ES:[EDI],ES:[ESI]}
		MOV    CL,AL
		DW     2667H
		REP    MOVSB        ;// {REP MOVSB ES:[EDI],ES:[ESI]}
	}
}
*/
//move linear Block
void MoveLinBlockB(LONGINT Src, LONGINT Dest, LONGINT Size)
{
	_asm{
		XOR    AX,AX
		MOV    ES,AX
		DB     66H
		MOV    SI,WORD(Src)
		DB     66H
		MOV    DI,WORD(Dest)
		DB     66H
		MOV    CX,WORD(Size)
		CLD
		DW     2667H
		REP    MOVSB        ;// {REP MOVSB ES:[EDI],ES:[ESI]}
	}
}


//move linear Block  
void MoveLinBlockW(LONGINT Src, LONGINT Dest, LONGINT Size)
{
	_asm{
		XOR    AX,AX
		MOV    ES,AX
		DB     66H
		MOV    SI,WORD(Src)
		DB     66H
		MOV    DI,WORD(Dest)
		DB     66H
		MOV    CX,WORD(Size)
		DB     66H
		SHR    CX,1
		CLD
		DW     2667H
		REP    MOVSW        ;// {REP MOVSW ES:[EDI],ES:[ESI]}
		ADC    CL,0
		DW     2667H
		REP    MOVSB        ;// {REP MOVSB ES:[EDI],ES:[ESI]}
	}
}

void FillLinBlockD(LONGINT Dest, LONGINT Size, BYTE Data)
{
	_asm{
		XOR    AX,AX
		MOV    ES,AX
		DB     66H
		MOV    DI,WORD(Dest)
		DB     66H
		MOV    CX,WORD(Size)
		MOV    BL,CL
		DB     66H
		SHR    CX,2
		AND    BL,3
		MOV    AL,Data
		MOV    AH,AL
		MOV    SI,AX
		DB     66H
		SHL    AX,16
		MOV    AX,SI
		CLD
		DW     6667H
		REP    STOSW        ;// {REP STOSD ES:[EDI]}
		MOV    CL,BL
		DB     67H
		REP    STOSB        ;// {REP STOSB ES:[EDI]}
	}
}

/*BOOL CompLinBlocks( LONGINT Block1, LONGINT Block2, LONGINT Size)
{
	BYTE RetVal;
	_asm{
		XOR    AX,AX
		MOV    ES,AX
		DB     66H
		MOV    SI,WORD(Block1)
		DB     66H
		MOV    DI,WORD(Block2)
		DB     66H
		MOV    CX,WORD(Size)
		MOV    AL,CL
		DB     66H
		SHR    CX,2
		AND    AL,3
		CLD
		DB     26H
		DW     6667H
		REPZ   CMPSW        ;//{REP CMPSD ES:[EDI],ES:[ESI]}
		JNZ    NOT_EQUAL
		MOV    CL,AL
		DW     2667H
		REPZ   CMPSB        ;//{REP CMPSB ES:[EDI],ES:[ESI]}
		JNZ    NOT_EQUAL
		MOV    AL,1
		JMP    EQUAL
NOT_EQUAL:
		MOV    AL,0         ;//{not equal-> return false}
EUQAL:
		MOV    RetVal , AL
	}
	return ( (RetVal == 0x00) ? FALSE : TRUE);
}
*/
void Beep(unsigned long milliseconds)
{
	static union REGS ourregs;

	// first set up Timer 2 for 1 KHz signal

	outportb(0x43, 0xB6);   // timer 2 mode set
				// for 1kHz: 1190/1 = 0x04A6
	outportb(0x42, 0xA6);   // set LSB of counter
	outportb(0x42, 0x04);   // set MSB of counter

	//   Activate Speaker and connect timer to speaker by
	//   only setting bits 0 and 1 of port 0x61.  Other
	//   bits are left unchanged by reading bits first.
	outportb(0x61, (inportb(0x61) | 0x03));

	//change to mocroseconds
	unsigned long Interval = milliseconds * 1000 ;

	// Wait for Some  seconds, using interrupt 15, function 86

	ourregs.h.ah = 0x86;
	ourregs.x.cx = (Interval >> 16) ;   // 0x1E8480 = 2,000,000 uS
	ourregs.x.dx = (Interval & 0xFFFF); // 0x8480;
	int86(0x15, &ourregs, &ourregs);    // issue interrupt 15

	// turn off speaker

	outportb(0x61, (inportb(0x61) & 0xFC));
}


//end of Implementation of Tools.cpp

⌨️ 快捷键说明

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