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

📄 instruction.cpp

📁 这个也是我们的毕业设计课题
💻 CPP
📖 第 1 页 / 共 3 页
字号:
		{
			wFlags &=0XFFFC;
			wFlags |=0X0002;
			_cpu->SetFLAGS(wFlags);
		}
	}
	else
	{
		ReportState("the 'CPA' instruction can't handle this code");
		return;
	}
	UNSHORT uAddr=_cpu->GetPC();
	_cpu->SetPC(uAddr+2);
}
void CPL::Excute(DWORD dwCode)
{
	WORD uIP=_cpu->GetIP();
	DWORD dw=dwCode & 0X01C00000;//specify operand number
	WORD wFlags=_cpu->GetFLAGS();//flag register
	if(dw==0x00800000)//2 operand (CPL GR,ADR)
	{
		DWORD dwTemp1=dwCode & 0X0000FFFF;//get the memory address
		UNSHORT uAddr1=UNSHORT(dwTemp1+uIP);
		_cpu->RequestBUS();
		_cpu->WriteAddrToAddrBUS(uAddr1);
		WORD wData1=_cpu->ReadDataFromMemory();
		_cpu->ReleaseBUS();
		//register operation
		BYTE bIndex=BYTE((dwCode & 0X00070000)>>16);
		wData1=UNSHORT(_cpu->GetGR(bIndex))-UNSHORT(wData1);
				//change the flags register
		if(wData1>0)
		{
			wFlags &=0XFFFC;
			_cpu->SetFLAGS(wFlags);
		}
		else if(wData1=0)
		{
			wFlags &=0XFFFC;
			wFlags |=0X0001;
			_cpu->SetFLAGS(wFlags);
		}
		else if(wData1<0)
		{
			wFlags &=0XFFFC;
			wFlags |=0X0002;
			_cpu->SetFLAGS(wFlags);
		}
	}
	else if(dw==0X00C00000)//3 operand (CPL GRd,imm,GRs)
	{
		DWORD dwTemp2=dwCode & 0X0000FFFF;
		WORD wData2=WORD(dwTemp2+uIP);
		BYTE bIndexSrc=BYTE((dwCode & 0X00070000)>>16);
		BYTE bIndexDst=BYTE((dwCode & 0X00380000)>>19);
		UNSHORT uAddr2=UNSHORT(wData2+_cpu->GetGR(bIndexSrc));
		_cpu->RequestBUS();
		_cpu->WriteAddrToAddrBUS(uAddr2);
		wData2=_cpu->ReadDataFromMemory();
		_cpu->ReleaseBUS();
		wData2=UNSHORT(_cpu->GetGR(bIndexDst))-UNSHORT(wData2);
		//change the flags
		if(wData2>0)
		{
			wFlags &=0XFFFC;
			_cpu->SetFLAGS(wFlags);
		}
		else if(wData2=0)
		{
			wFlags &=0XFFFC;
			wFlags |=0X0001;
			_cpu->SetFLAGS(wFlags);
		}
		else if(wData2<0)
		{
			wFlags &=0XFFFC;
			wFlags |=0X0002;
			_cpu->SetFLAGS(wFlags);
		}
	}
	else
	{
		ReportState("the 'CPL' instruction can't handle this code");
		return;
	}
	UNSHORT uAddr=_cpu->GetPC();
	_cpu->SetPC(uAddr+2);
}
void JMP::Excute(DWORD dwCode)
{
	WORD uIP=_cpu->GetIP();
	DWORD dw=dwCode & 0X01C00000;//specify the operand number
	if(dw==0X00400000)//1 operand (JMP BH)
	{
		DWORD dwTemp1=dwCode & 0X0000FFFF;//get the immediate data
		UNSHORT uAddr1=UNSHORT(dwTemp1+uIP);//convert the address into 16 bits formats
		_cpu->SetPC(uAddr1);

	}
	else if(dw==0X00800000)//3 operand (JMP BH,ADR)
	{
		BYTE bIndex=BYTE((dwCode & 0X00070000)>>16);//specofy the register index
		//the index of register is stored in 18-16 bit of dwCode
		WORD wData2=_cpu->GetGR(bIndex);//read the data holdes in the register
		DWORD dwTemp2=dwCode & 0X0000FFFF;//get the immediate data
		UNSHORT uAddr2=UNSHORT(dwTemp2+uIP);
		uAddr2+=wData2;
		_cpu->SetPC(uAddr2);
	}
	else
	{
		ReportState("the 'JMP' instruction can't handle this code");
		return;
	}
	
}
void JPZ::Excute(DWORD dwCode)
{
	WORD uIP=_cpu->GetIP();
	WORD wFlags=_cpu->GetFLAGS();//flag register
	wFlags=wFlags&0X0003;
	if(wFlags==0X0000||wFlags==0X0001)
	{
	
		DWORD dw=dwCode & 0X01C00000;//specify the operand number
		if(dw==0X00400000)//1 operand (JPZ BH) 
		{
			DWORD dwTemp1=dwCode & 0X0000FFFF;//get the immediate data
			UNSHORT uAddr1=UNSHORT(dwTemp1+uIP);//convert the address into 16 bits formats
			_cpu->SetPC(uAddr1);

		}
		else if(dw==0X00800000)//3 operand (JPZ BH,ADR),GRd->(imm+(GRs))
		{
			BYTE bIndex=BYTE((dwCode & 0X00070000)>>16);//specofy the register index
			//the index of register is stored in 18-16 bit of dwCode
			WORD wData2=_cpu->GetGR(bIndex);//read the data holdes in the register
			DWORD dwTemp2=dwCode & 0X0000FFFF;//get the immediate data
			UNSHORT uAddr2=UNSHORT(dwTemp2+uIP);
			uAddr2+=wData2;
			_cpu->SetPC(uAddr2);
		}
		else
		{
			ReportState("the 'JPZ' instruction can't handle this code");
			return;
		}
	}
	else
	{
		UNSHORT uAddr=_cpu->GetPC();
		_cpu->SetPC(uAddr+2);
	}
}
void JMI::Excute(DWORD dwCode)
{
	WORD uIP=_cpu->GetIP();
	WORD wFlags=_cpu->GetFLAGS();//flag register
	wFlags=wFlags&0X0003;
	if(wFlags==0X0002)
	{
	
		DWORD dw=dwCode & 0X01C00000;//specify the operand number
		if(dw==0X00400000)//1 operand (JMI BH) 
		{
			DWORD dwTemp1=dwCode & 0X0000FFFF;//get the immediate data
			UNSHORT uAddr1=UNSHORT(dwTemp1+uIP);//convert the address into 16 bits formats
			_cpu->SetPC(uAddr1);

		}
		else if(dw==0X00800000)//3 operand (JMI BH,ADR),GRd->(imm+(GRs))
		{
			BYTE bIndex=BYTE((dwCode & 0X00070000)>>16);//specofy the register index
			//the index of register is stored in 18-16 bit of dwCode
			WORD wData2=_cpu->GetGR(bIndex);//read the data holdes in the register
			DWORD dwTemp2=dwCode & 0X0000FFFF;//get the immediate data
			UNSHORT uAddr2=UNSHORT(dwTemp2+uIP);
			uAddr2+=wData2;
			_cpu->SetPC(uAddr2);
		}
		else
		{
			ReportState("the 'JMI' instruction can't handle this code");
			return;
		}
	}
	else
	{
		UNSHORT uAddr=_cpu->GetPC();
		_cpu->SetPC(uAddr+2);
	}
}
void JNZ::Excute(DWORD dwCode)
{
	WORD uIP=_cpu->GetIP();
	WORD wFlags=_cpu->GetFLAGS();//flag register
	wFlags=wFlags&0X0003;
	if(wFlags==0X0000||wFlags==0X0002)
	{
	
		DWORD dw=dwCode & 0X01C00000;//specify the operand number
		if(dw==0X00400000)//1 operand (JNZ BH) 
		{
			DWORD dwTemp1=dwCode & 0X0000FFFF;//get the immediate data
			UNSHORT uAddr1=UNSHORT(dwTemp1+uIP);//convert the address into 16 bits formats
			_cpu->SetPC(uAddr1);

		}
		else if(dw==0X00800000)//3 operand (JNZ BH,ADR),GRd->(imm+(GRs))
		{
			BYTE bIndex=BYTE((dwCode & 0X00070000)>>16);//specofy the register index
			//the index of register is stored in 18-16 bit of dwCode
			WORD wData2=_cpu->GetGR(bIndex);//read the data holdes in the register
			DWORD dwTemp2=dwCode & 0X0000FFFF;//get the immediate data
			UNSHORT uAddr2=UNSHORT(dwTemp2+uIP);
			uAddr2+=wData2;
			_cpu->SetPC(uAddr2);
		}
		else
		{
			ReportState("the 'JNZ' instruction can't handle this code");
			return;
		}
	}
	else
	{
		UNSHORT uAddr=_cpu->GetPC();
		_cpu->SetPC(uAddr+2);
	}
}
void JZE::Excute(DWORD dwCode)
{
	WORD uIP=_cpu->GetIP();
	WORD wFlags=_cpu->GetFLAGS();//flag register
	wFlags=wFlags&0X0003;
	if(wFlags==0X0001)
	{
	
		DWORD dw=dwCode & 0X01C00000;//specify the operand number
		if(dw==0X00400000)//1 operand (JZE BH) 
		{
			DWORD dwTemp1=dwCode & 0X0000FFFF;//get the immediate data
			UNSHORT uAddr1=UNSHORT(dwTemp1+uIP);//convert the address into 16 bits formats
			_cpu->SetPC(uAddr1);

		}
		else if(dw==0X00800000)//3 operand (JZE BH,ADR),GRd->(imm+(GRs))
		{
			BYTE bIndex=BYTE((dwCode & 0X00070000)>>16);//specofy the register index
			//the index of register is stored in 18-16 bit of dwCode
			WORD wData2=_cpu->GetGR(bIndex);//read the data holdes in the register
			DWORD dwTemp2=dwCode & 0X0000FFFF;//get the immediate data
			UNSHORT uAddr2=UNSHORT(dwTemp2+uIP);
			uAddr2+=wData2;
			_cpu->SetPC(uAddr2);
		}
		else
		{
			ReportState("the 'JZE' instruction can't handle thsi code");
			return;
		}
	}
	else
	{
		UNSHORT uAddr=_cpu->GetPC();
		_cpu->SetPC(uAddr+2);
	}
}
void PUSH::Excute(DWORD dwCode)
{
	WORD uIP=_cpu->GetIP();
	DWORD dw=dwCode & 0X01C00000;//specify the operand number
	if(dw=0X00400000)//1 operand (PUSH ADR),(GR4)<-(ADR)
	{		//but in this instruction the implemention will be
		   //((SS)+(SP))<-(ADR),(SP)=(GR4)
		DWORD dwTemp1=dwCode & 0X0000FFFF;//Get the memory address 
		UNSHORT uAddr1=UNSHORT(dwTemp1+uIP);
		_cpu->RequestBUS();
		_cpu->WriteAddrToAddrBUS(uAddr1);
		WORD wData1=_cpu->ReadDataFromMemory();//read the source data
		_cpu->ReleaseBUS();
		uAddr1=UNSHORT(_cpu->GetGR(4)+1);//load the stack top
								//point
		_cpu->RequestBUS();
		_cpu->WriteAddrToAddrBUS(uAddr1);
		_cpu->WriteDataToMemory(wData1);
		_cpu->ReleaseBUS();
		_cpu->SetGR(uAddr1,4);//restore the stack top point to GR4
	}
	else if(dw=0X00800000)//3 operand (PUSH imm,GR)
	{
		BYTE bIndex=BYTE((dwCode & 0X00070000)>>16);//specify the register
		DWORD dwTemp2=dwCode & 0X0000FFFF;//get the immediate data
		UNSHORT uAddr2=UNSHORT(dwTemp2+uIP);
		uAddr2+=_cpu->GetGR(bIndex);
		_cpu->RequestBUS();
		_cpu->WriteAddrToAddrBUS(uAddr2);
		WORD wData2=_cpu->ReadDataFromMemory();
		_cpu->ReleaseBUS();
		uAddr2=UNSHORT(_cpu->GetGR(4)+1);//get the stack top point
		_cpu->RequestBUS();
		_cpu->WriteAddrToAddrBUS(uAddr2);
		_cpu->WriteDataToMemory(wData2);
		_cpu->ReleaseBUS();
		_cpu->SetGR(uAddr2,4);
		_cpu->SetSP(uAddr2);
	}
	else 
	{
		ReportState("the 'PUSH' instruction can't handle this code");
		return;
	}
	UNSHORT uAddr=_cpu->GetPC();//set the PC to the next instruction
								//in the memory.
	_cpu->SetPC(uAddr+2);
}
void POP::Excute(DWORD dwCode)
{
	WORD uIP=_cpu->GetIP();
	BYTE bIndex=BYTE((dwCode & 0X00070000)>>16);//specify the register
	UNSHORT uAddr=UNSHORT(_cpu->GetGR(4));//get the stack top pointer
	_cpu->RequestBUS();
	_cpu->WriteAddrToAddrBUS(uAddr);
	WORD wData=_cpu->ReadDataFromMemory();
	_cpu->ReleaseBUS();
	_cpu->SetGR(wData,bIndex);
	uAddr-=1;
	_cpu->SetGR(uAddr,4);
	uAddr=_cpu->GetPC();//set the PC to the next instruction
								//in the memory.
	_cpu->SetPC(uAddr+2);
}
void CALL::Excute(DWORD dwCode)
{
	WORD uIP=_cpu->GetIP();
	DWORD dw=dwCode & 0X01C00000;//specify the operand number
	if(dw=0X00400000)//1 operand CALL ADR
	{
		DWORD dwTemp1=dwCode & 0X0000FFFF;//get the routine address
		
		//the following operations are used for store the current 
		//instruction address into the stack
		UNSHORT uAddr1=UNSHORT(_cpu->GetGR(4));
		uAddr1+=1;
		_cpu->SetGR(uAddr1,4);//store the stack top pointer into GR4
		_cpu->SetSP(uAddr1);
		_cpu->RequestBUS();
		_cpu->WriteAddrToAddrBUS(uAddr1);
		uAddr1=_cpu->GetPC();//get the current PC
		_cpu->WriteDataToMemory(uAddr1);//store the current PC to stack
		_cpu->ReleaseBUS();
		uAddr1= UNSHORT(dwTemp1+uIP);//convert the routine address into 16 bits format
		_cpu->SetPC(uAddr1);//jump to the routine
	}
	else if(dw=0X00800000)//2 operand CALL imm,GR
	{
		DWORD dwTemp2=dw & 0X0000FFFF;//get the immediate data
		BYTE bIndex=BYTE((dwCode & 0X00070000)>>16);//specify the register
		UNSHORT uAddr2=UNSHORT(_cpu->GetGR(4));
		uAddr2+=1;
		_cpu->SetGR(uAddr2,4);
		_cpu->SetSP(uAddr2);
		_cpu->RequestBUS();
		_cpu->WriteAddrToAddrBUS(uAddr2);
		uAddr2=_cpu->GetPC();//get the current PC
		_cpu->WriteDataToMemory(uAddr2);
		_cpu->ReleaseBUS();
		uAddr2=UNSHORT(dwTemp2+uIP);
		uAddr2=UNSHORT(uAddr2+(_cpu->GetGR(bIndex)));//specify the routine address
		_cpu->SetPC(uAddr2);
	}
	else
	{
		ReportState("the 'POP' instructio can't handle this code");
		return;
	}
}
void RET::Excute(DWORD dwCode)
{
	WORD uIP=_cpu->GetIP();
	//the following code get the address that store in the stack
	//and excute jump to the address to continue
	UNSHORT uAddr=UNSHORT(_cpu->GetGR(4));
	_cpu->RequestBUS();
	_cpu->WriteAddrToAddrBUS(uAddr);
	uAddr=(UNSHORT)_cpu->ReadDataFromMemory();
	_cpu->ReleaseBUS();
	uAddr+=2;//return to the caller and point to the next instruction
	_cpu->SetPC(uAddr);
	//the following code restore the stack top pointer
	uAddr=UNSHORT(_cpu->GetGR(4));
	uAddr-=1;
	_cpu->SetGR(uAddr,4);
	_cpu->SetSP(uAddr);
}
void INPORT::Excute(DWORD dwCode)
{
	WORD uIP=_cpu->GetIP();
	ReportState("the 'INPORT' instruction haven't implemented");
	UNSHORT uAddr=_cpu->GetPC();
	_cpu->SetPC(uAddr+2);
	return;
}
void OUTPORT::Excute(DWORD dwCode)
{
	WORD uIP=_cpu->GetIP();
	ReportState("the 'OUTPUT' instruction haven't implemented");
	UNSHORT uAddr=_cpu->GetPC();
	_cpu->SetPC(uAddr+2);
	return;
}
void EXIT::Excute(DWORD dwCode)
{
	WORD uIP=_cpu->GetIP();
	//halt the _cpu
	_cpu->SetContinue(FALSE);
	
}
void INTR::Excute(DWORD dwCode)
{
	WORD uIP=_cpu->GetIP();
	DWORD dwTemp=dwCode & 0X0000FFFF;
	BYTE bCode=BYTE(dwTemp);
	switch(bCode)
	{
	case 3:
		_cpu->SetContinue(FALSE); 
		break;
	default:
		ReportState("the 'INTR' instruction haven't define any operation for this code");
		break;
	}
}
void IRET::Excute(DWORD dwCode)
{
	WORD uIP=_cpu->GetIP();
	//the following code get the address that store in the stack
	//and excute jump to the address to continue
	UNSHORT uAddr=UNSHORT(_cpu->GetGR(4));
	_cpu->RequestBUS();
	_cpu->WriteAddrToAddrBUS(uAddr);
	uAddr=(UNSHORT)_cpu->ReadDataFromMemory();
	_cpu->ReleaseBUS();
	uAddr+=2;//return to the caller and point to the next instruction
	_cpu->SetPC(uAddr);
	//the following code restore the stack top pointer
	uAddr=UNSHORT(_cpu->GetGR(4));
	uAddr-=1;
	_cpu->SetGR(uAddr,4);
	_cpu->SetSP(uAddr);
	
}
void STI::Excute(DWORD dwCode)
{
	WORD uIP=_cpu->GetIP();
	WORD wFlags=_cpu->GetFLAGS()|0X0008;//set the IF flag
	_cpu->SetFLAGS(wFlags);
	UNSHORT uAddr=_cpu->GetPC();
	_cpu->SetPC(uAddr+2);
}
void CLI::Excute(DWORD dwCode)
{
	WORD uIP=_cpu->GetIP();
	WORD wFlags=_cpu->GetFLAGS()&0XFFF7;
	_cpu->SetFLAGS(wFlags);
	UNSHORT uAddr=_cpu->GetPC();
	_cpu->SetPC(uAddr+2);
}

void Instruction::ReportState(CString strState)
{
	CMainFrame* pMainFrame=(CMainFrame*)AfxGetMainWnd();
	COutputWindow* pOutputWindow=pMainFrame->GetOutputWindow();
	if(pOutputWindow)
	{
		pOutputWindow->ShowState(strState);
	}

}

⌨️ 快捷键说明

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