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

📄 executeview.cpp

📁 这个也是我们的毕业设计课题
💻 CPP
字号:
// ExecuteView.cpp : implementation file
//

#include "stdafx.h"
#include "DFYSimulator.h"
#include "ExecuteView.h"
#include "ExecuteChildFrame.h"
#include "MainFrm.h"
#include "OutputWindow.h"
#include "MemoryDlg.h"
#include "RegisterDlg.h"
#include "StackDlg.h"

#include "DFY\CPU.h"
#include "DFY\MemorySegment.h"
#include "DFY\instruction.h"
#include "fileheader.h"
#include "fstream"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CExecuteView

IMPLEMENT_DYNCREATE(CExecuteView, CScrollView)

CExecuteView::CExecuteView()
{
	m_caretPos.x=5;
	m_caretPos.y=17;
	m_uContentNum=0;
	m_uFirstVisualPos=0;
	m_uScrollRange=28;
	m_bProExist=FALSE;
	m_uProCount=0;
	m_wCodeSegSize=0;
	m_wVarNum=0;
	m_uIP=0;
	m_wPC=0;
	m_wPC1=0;
	m_dwOP=0;
	m_wSP=0;
	m_wFLAGS=0;
	m_wGR[0]=0;
	m_wGR[1]=0;
	m_wGR[2]=0;
	m_wGR[3]=0;
	m_wGR[4]=0;


}

CExecuteView::~CExecuteView()
{
}


BEGIN_MESSAGE_MAP(CExecuteView, CScrollView)
	//{{AFX_MSG_MAP(CExecuteView)
	ON_WM_PAINT()
	ON_WM_CREATE()
	ON_WM_LBUTTONDOWN()
	ON_WM_RBUTTONDOWN()
	ON_COMMAND(ID_COMPILE_EXCUTE, OnExcute)
	ON_WM_TIMER()
	ON_COMMAND(ID_COMPILE_STOP, OnStop)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CExecuteView drawing

void CExecuteView::OnInitialUpdate()
{

	CScrollView::OnInitialUpdate();
}

void CExecuteView::OnDraw(CDC* pDC)
{
	// TODO: add draw code here
	//clear the last display content
	CRect rect;
	GetClientRect(&rect);
	CBrush whiteBrush;
	whiteBrush.CreateSolidBrush(RGB(255,255,255));
	pDC->FillRect(&rect,&whiteBrush);
	
	CExecuteChildFrame* pFrame =(CExecuteChildFrame*)GetParent();
	int temp=m_uContentNum-20;
	if(temp<0)
		temp=m_uContentNum;
	int nPos=1;
	//show 28 lines 
	//show instruction code
	nPos+=16;
	pDC->TextOut(1,1," MemoryAddr    InsctructionCode");
	for(int i=m_uFirstVisualPos;i<=temp;i++)
	{
		if(i==((m_wPC-m_uIP)/2)-m_uFirstVisualPos)
		{
			pDC->SetTextColor(RGB(255,0,0));
			pDC->TextOut(1,nPos,m_strInstruction[i]);
			pDC->SetTextColor(RGB(0,0,0));
			nPos+=16;
			continue;
		}
		pDC->TextOut(1,nPos,m_strInstruction[i]);
		nPos+=16;
	}
	//show variable
	ShowVariable(pDC);
	//show registers
	ShowRegisters(pDC);
	CDFYSimulatorApp* pApp=(CDFYSimulatorApp*)AfxGetApp();
	CPU* pCPU=pApp->GetSimulatorCPU();
	m_caretPos.y=((m_wPC-pCPU->GetIP())/2-m_uFirstVisualPos)*16+17;
	DrawFlag(m_caretPos);
}

/////////////////////////////////////////////////////////////////////////////
// CExecuteView diagnostics

#ifdef _DEBUG
void CExecuteView::AssertValid() const
{
	CScrollView::AssertValid();
}

void CExecuteView::Dump(CDumpContext& dc) const
{
	CScrollView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CExecuteView message handlers

void CExecuteView::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	OnDraw(&dc);
	
	// Do not call CScrollView::OnPaint() for painting messages
}



void CExecuteView::FlushDisplay()
{
	CClientDC dc(this);
	OnDraw(&dc);
}



int CExecuteView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CScrollView::OnCreate(lpCreateStruct) == -1)
		return -1;

	return 0;
}





void CExecuteView::ShowRegisters(CDC* pDC)
{
	CDFYSimulatorApp* pApp=(CDFYSimulatorApp*)AfxGetApp();
	CPU* pCPU=pApp->GetSimulatorCPU();
	if(!pCPU)
	{
		ReportState("can't get the simulator's cpu");
	}
	else//get the cpu
	{
		int nPos=1;
		CString strReg;
		WORD wData;
		DWORD dwData;
		UNSHORT uAddr;
		//show the tip information
		pDC->TextOut(580,nPos,"the current value of registers:");
		nPos+=16;
		//show the registers
		uAddr=pCPU->GetCS();
		strReg.Format("CS = %x",uAddr);
		strReg.MakeUpper();
		pDC->TextOut(580,nPos,strReg);
		nPos+=16;

		uAddr=pCPU->GetIP();
		strReg.Format("IP = %x",uAddr);
		strReg.MakeUpper();
		if(uAddr!=m_uIP)
		{
			pDC->SetTextColor(RGB(255,0,0));
			pDC->TextOut(580,nPos,strReg);
			pDC->SetTextColor(RGB(0,0,0));
		}
		else
		{
			pDC->TextOut(580,nPos,strReg);
		}
		nPos+=16;
		
		strReg.Format("PC = %x",m_wPC);
		strReg.MakeUpper();
		if(m_wPC!=m_wPC1)
		{
			pDC->SetTextColor(RGB(255,0,0));
			pDC->TextOut(580,nPos,strReg);
			pDC->SetTextColor(RGB(0,0,0));
			m_wPC1=m_wPC;
		}
		else
		{
			pDC->TextOut(580,nPos,strReg);
		}
		nPos+=16;
	
		uAddr=pCPU->GetDS();
		strReg.Format("DS = %x",uAddr);
		strReg.MakeUpper();
		pDC->TextOut(580,nPos,strReg);
		nPos+=16;

		uAddr=pCPU->GetES();
		strReg.Format("ES = %x",uAddr);
		strReg.MakeUpper();
		pDC->TextOut(580,nPos,strReg);
		nPos+=16;

		wData=pCPU->GetSP();
		strReg.Format("SP = %x",wData);
		strReg.MakeUpper();
		if(wData!=m_wSP)
		{
			pDC->SetTextColor(RGB(255,0,0));
			pDC->TextOut(580,nPos,strReg);
			pDC->SetTextColor(RGB(0,0,0));
			m_wSP=wData;
		}
		else
		{
			pDC->TextOut(580,nPos,strReg);
			
		}
		nPos+=16;

		wData=pCPU->GetFLAGS();
		strReg.Format("FLAGS = %x",wData);
		strReg.MakeUpper();
		if(wData!=m_wFLAGS)
		{
			pDC->SetTextColor(RGB(255,0,0));
			pDC->TextOut(580,nPos,strReg);
			pDC->SetTextColor(RGB(0,0,0));
			m_wFLAGS=wData;
		}
		else
		{
			pDC->TextOut(580,nPos,strReg);
			
		}
		nPos+=16;

		dwData=pCPU->GetOP();
		strReg.Format("OP = %x",dwData);
		strReg.MakeUpper();
		if(dwData!=m_dwOP)
		{
			pDC->SetTextColor(RGB(255,0,0));
			pDC->TextOut(580,nPos,strReg);
			pDC->SetTextColor(RGB(0,0,0));
			m_dwOP=dwData;
		}
		else
		{
			pDC->TextOut(580,nPos,strReg);
			
		}
		nPos+=16;
		for(int i=0;i<5;i++)
		{
			wData=pCPU->GetGR(i);
			strReg.Format("GR%d = %x",i,wData);
			strReg.MakeUpper();
			if(wData!=m_wGR[i])
			{
				pDC->SetTextColor(RGB(255,0,0));
				pDC->TextOut(580,nPos,strReg);
				pDC->SetTextColor(RGB(0,0,0));
				m_wGR[i]=wData;
			}
			else
			{
				pDC->TextOut(580,nPos,strReg);
				
			}
			nPos+=16;
		}

	}//end:get the cpu
}

void CExecuteView::ShowVariable(CDC *pDC)
{
	//show header
	pDC->TextOut(290,1,"VarName          VarAddr   VarValue");
	int nPos=17;
	int temp=m_uFirstVisualPos+20;
	if(temp>m_wVarNum-1)
	temp=m_wVarNum-1;
	for(int i=m_uFirstVisualPos;i<=temp;i++)
	{
		//get the memory value and flush the display
		CDFYSimulatorApp* pApp=(CDFYSimulatorApp*)AfxGetApp();
		Memory* pMemory=pApp->GetSimulatorMemory();
		if(pMemory)//if get the memory
		{
			UNSHORT uAddr=m_varInfo[i].uVarAddr;
			WORD wVarValue=0;
			pMemory->ReadMemory(uAddr,wVarValue);
			CString strVarName;
			strVarName.Format("%s",m_varInfo[i].varName);
			CString strVarAddr=ChangeToHexStr(uAddr);
			CString strVarValue=ChangeToHexStr(UNSHORT(wVarValue));
			CString str;
			str.Format("%-20s%10s%10s",strVarName,strVarAddr,strVarValue);
			pDC->TextOut(290,nPos,str);
			nPos+=16;
		}
		else//have not get the memory
		{
				ReportState("have not get the memory ,the memory is not exist");
		}//end: if get the memory
	}//end: for
}

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





void CExecuteView::AddContent(CString str)
{
	if(m_uContentNum<2048)
	{
		m_strInstruction[m_uContentNum]=str;
		m_uContentNum++;//delete the old information
	}
	else
	{
		ReportState("can't add the content to file ,the content is full");
		m_uContentNum=2048;
	}
}

void CExecuteView::ResetContent()
{
	//clear the content
	m_uContentNum=0;
	m_uFirstVisualPos=0;
}

void CExecuteView::SetFirstVisualPos(UNSHORT uFirstVisualPos)
{
	m_uFirstVisualPos=uFirstVisualPos;
}

BOOL CExecuteView::PreCreateWindow(CREATESTRUCT& cs) 
{

	return CScrollView::PreCreateWindow(cs);
}

void CExecuteView::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView) 
{
	CScrollView::OnActivateView(bActivate, pActivateView, pDeactiveView);
}

void CExecuteView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	DrawFlag(m_caretPos);
	CScrollView::OnLButtonDown(nFlags, point);
}

void CExecuteView::OnRButtonDown(UINT nFlags, CPoint point) 
{
	
	CScrollView::OnRButtonDown(nFlags, point);
}

void CExecuteView::SetProExistState(BOOL bProExist)
{
	m_bProExist=bProExist;
}

void CExecuteView::SetCodeSegSize(WORD wCodeSegSize)
{
	m_wCodeSegSize=wCodeSegSize;
}
void CExecuteView::OnExcute() 
{
	if(!m_bProExist)
	{
		ReportState("no program exist in the memory");
	}
	else
	{
		//get the simulator speed
		CDFYSimulatorApp* pApp=(CDFYSimulatorApp*)AfxGetApp();
		int speed=pApp->GetSimulatorSpeed();
		pApp->GetSimulatorCPU()->SetContinue(TRUE);
		//excute the program
		CMainFrame* pMainFrame=(CMainFrame*)AfxGetMainWnd();
		COutputWindow* pOutputWindow=pMainFrame->GetOutputWindow();
		pOutputWindow->ResetContent();
		SetTimer(1,100*speed,NULL);
	}		
}
void CExecuteView::OnTimer(UINT nIDEvent) 
{
	CDFYSimulatorApp* pApp=(CDFYSimulatorApp*)AfxGetApp();
	CPU* pCPU=pApp->GetSimulatorCPU();
	m_wPC=pCPU->GetPC();//hold the last PC
	//change caret pos
	//specify the flags y position
	//execute
	if(pCPU)
	{
		if(pCPU->GetContinue())
		{
			pCPU->Excute();
			//flush the cscreen
			FlushDisplay();
			AnylizeCode();
		}
		else
		{
			KillTimer(1);
		}
	}
	else
	{
		ReportState("can't find the simulator cpu");
		return;
	}
	CMainFrame* pMainFrame=(CMainFrame*)AfxGetMainWnd();
	//flush the moniter windows
	CMemoryDlg* pMemoryDlg=pMainFrame->GetMemoryDlg();
	CRegisterDlg* pRegDlg=pMainFrame->GetRegisterDlg();
	CStackDlg* pStackDlg=pMainFrame->GetStackDlg();
	CVariableDlg* pVarDlg=pMainFrame->GetVariableDlg();
	if(pMemoryDlg)
		pMemoryDlg->FlushDisplay();
	if(pRegDlg)
		pRegDlg->FlushRegister();
	if(pStackDlg)
		pStackDlg->FlushStack();
	if(pVarDlg)
		pVarDlg->FlushVariable();
	//check if go to the end
	if((pCPU->GetPC()-pCPU->GetIP())>=m_wCodeSegSize)
	{
		KillTimer(1);
	}
	
	CScrollView::OnTimer(nIDEvent);
}

CString CExecuteView::ChangeToHexStr(UNSHORT wData)
{
	CString strData;
	if(wData<16)
 		strData.Format("000%xH",wData);
 	else if(wData>=16&&wData<256)
 		strData.Format("00%xH",wData);
 	else if(wData>=256&&wData<4096)
 		strData.Format("0%xH",wData);
 	else
 		strData.Format("%xH",wData);
 	strData.MakeUpper();
 	return strData;
}

void CExecuteView::OnStop() 
{
	KillTimer(1);
}

void CExecuteView::DrawFlag(POINT point)
{
	try{
		CPen pen(PS_SOLID,1,RGB(255,0,0));
		CPen* oldPen;
		CClientDC dc(this);
		oldPen=dc.SelectObject(&pen);
		dc.MoveTo(point.x,point.y+6);
		dc.LineTo(point.x+17,point.y+6);
		dc.LineTo(point.x+17,point.y);
		dc.LineTo(point.x+25,point.y+8);
		dc.LineTo(point.x+17,point.y+16);
		dc.LineTo(point.x+17,point.y+10);
		dc.LineTo(point.x-1,point.y+10);
		dc.SelectObject(oldPen);
	}
	catch(CResourceException* e)
	{
		e->ReportError();
	}
	catch(...)
	{
	}
}


void CExecuteView::AnylizeCode()
{
	CDFYSimulatorApp* pApp=(CDFYSimulatorApp*)AfxGetApp();
	CPU* pCPU=pApp->GetSimulatorCPU();
	if(pCPU)//if pCPU is legal
	{
		DWORD dwCode=pCPU->GetOP();
		CMainFrame* pFrame=(CMainFrame*)AfxGetMainWnd();
		BYTE bInstructionCode=BYTE((dwCode&0XFE000000)>>25);
		char* strInstruction=new char[5];
		strcpy(strInstruction,pCPU->GetInstruction(bInstructionCode)->GetName());
		BYTE bOperandNum=BYTE((dwCode&0X01C00000)>>22);
		BYTE bDstReg=BYTE((dwCode&0X00380000)>>19);
		BYTE bSrcReg=BYTE((dwCode&0X0007FFFF)>>16);
		WORD wImmNumber=WORD(dwCode&0X0000FFFF);
		CString str;
		str.Format("InstructionName: %5s  InstructionCode: %3x  OperandNum: %3x  DstReg: %3x SrcReg: %3x  ImmNum: %5x"\
				 ,strInstruction,bInstructionCode,bOperandNum,\
					bDstReg,bSrcReg,wImmNumber);
		ReportState(str);
	}//end:if pCPU is legal
}
void CExecuteView::ResetVarInfo()
{
	m_wVarNum=0;
}

void CExecuteView::AddVarInfo(char* strVarName, UNSHORT uAddr)
{
	if(m_wVarNum>=2048)
	{
		ReportState("the m_varInfo is full can't add varinfo to it");
		return;
	}
	strcpy(m_varInfo[m_wVarNum].varName,strVarName);
	m_varInfo[m_wVarNum].uVarAddr=uAddr;
	m_wVarNum++;
}



void CExecuteView::SetIP(WORD wIP)
{
	m_uIP=wIP;
}

void CExecuteView::OnDestroy() 
{
	CScrollView::OnDestroy();
	KillTimer(1);
	// TODO: Add your message handler code here
	
}

⌨️ 快捷键说明

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