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

📄 executechildframe.cpp

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

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

#include "DFY/MemorySegment.h"
#include "DFY/CPU.h"

#include "fstream"
using namespace std;
#include "fileheader.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CExecuteChildFrame

IMPLEMENT_DYNCREATE(CExecuteChildFrame, CMDIChildWnd)

CExecuteChildFrame::CExecuteChildFrame()
{
	m_strCurOpenFile=NULL;
	m_uFirstVisualPos=0;
	m_uIP=0;
	m_uScrollRange=0;
}

CExecuteChildFrame::~CExecuteChildFrame()
{
}


BEGIN_MESSAGE_MAP(CExecuteChildFrame, CMDIChildWnd)
	//{{AFX_MSG_MAP(CExecuteChildFrame)
	ON_COMMAND(ID_PROGRAM_EXIT, OnProgramExit)
	ON_WM_CLOSE()
	ON_WM_PAINT()
	ON_WM_VSCROLL()
	ON_COMMAND(ID_PROGRAM_LOAD, OnProgramLoad)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CExecuteChildFrame message handlers

void CExecuteChildFrame::OnProgramExit() 
{
	CMainFrame* pMainFrame=(CMainFrame*)AfxGetMainWnd();
	COutputWindow* pOutputWindow=pMainFrame->GetOutputWindow();
	pOutputWindow->ResetContent();
	CExecuteChildFrame::OnClose();
}

void CExecuteChildFrame::OnClose() 
{

	CMenu* pMenu=new CMenu();
	if(pMenu)
	{
		pMenu->LoadMenu(IDR_MAINFRAME);
		GetMDIFrame()->SetMenu(pMenu);
	}
	CDFYSimulatorApp* pApp=(CDFYSimulatorApp*)AfxGetApp();
	pApp->SetExeFrameState(FALSE);
	//set the current file name to NULL
	pApp->SetCurFileName(NULL);
	//reset the memory
	Memory* pMemory=pApp->GetSimulatorMemory();
	for(UNSHORT i=0;i<=65534;i++)
		pMemory->WriteMemory(i,0);
	//reset the register
	CPU* pCPU=pApp->GetSimulatorCPU();
	pCPU->SetGR(0,0);
	pCPU->SetGR(0,1);
	pCPU->SetGR(0,2);
	pCPU->SetGR(0,3);
	pCPU->SetGR(0,4);
	pCPU->SetIP(0);
	pCPU->SetSP(0);
	pCPU->SetFLAGS(0);
	pCPU->SetPC(0);

	//get the doctemplate
	POSITION templatePos;
	templatePos=pApp->GetFirstDocTemplatePosition();
	pApp->GetNextDocTemplate(templatePos);
			//get the cocument
	CMultiDocTemplate* pTemplate=(CMultiDocTemplate*)pApp->GetNextDocTemplate(templatePos);
	POSITION docPos=pTemplate->GetFirstDocPosition();
	CDocument* pDoc=pTemplate->GetNextDoc(docPos);
			//get the view
	POSITION viewPos=pDoc->GetFirstViewPosition();
	CExecuteView* pView=(CExecuteView*)pDoc->GetNextView(viewPos);
	pView->SetProExistState(FALSE);
	//set the open file name to NULL		
	m_strCurOpenFile=NULL;
	//clear the output
	CMainFrame* pMainFrame=(CMainFrame*)AfxGetMainWnd();
	COutputWindow* pOutputWindow=pMainFrame->GetOutputWindow();
	pOutputWindow->ResetContent();
	CMDIChildWnd::OnClose();

}

void CExecuteChildFrame::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	if(m_strCurOpenFile)
		SetWindowText(m_strCurOpenFile);
	// TODO: Add your message handler code here
	// Do not call CMDIChildWnd::OnPaint() for painting messages
}

BOOL CExecuteChildFrame::PreCreateWindow(CREATESTRUCT& cs) 
{	
	return CMDIChildWnd::PreCreateWindow(cs);
}



void CExecuteChildFrame::ReportState(CString strState)
{
	CMainFrame* pMainFrame=(CMainFrame*)AfxGetMainWnd();
	COutputWindow* pOutputWindow=(COutputWindow*)pMainFrame->GetOutputWindow();
	if(pOutputWindow)
		pOutputWindow->ShowState(strState);
	
}
BOOL CExecuteChildFrame::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CMDIFrameWnd* pParentWnd, CCreateContext* pContext) 
{
	dwStyle=dwStyle|WS_VSCROLL;	
	return CMDIChildWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, pContext);
}


BOOL CExecuteChildFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext) 
{	
	return CMDIChildWnd::OnCreateClient(lpcs, pContext);
}
void CExecuteChildFrame::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	 m_uFirstVisualPos=GetScrollPos(SB_VERT);
	switch(nSBCode)
	{
	case SB_LINEDOWN:
		m_uFirstVisualPos+=1;
		break;
	case SB_LINEUP:
		m_uFirstVisualPos-=1;
		break;
	case SB_PAGEDOWN:
		m_uFirstVisualPos+=28;
		break;
	case SB_PAGEUP:
		m_uFirstVisualPos-=28;
		break;
	case SB_THUMBPOSITION:
		m_uFirstVisualPos=nPos;
		break;
	case SB_THUMBTRACK:
		m_uFirstVisualPos=nPos;
		break;
	default:
		break;
	}
	SetScrollPos(SB_VERT,m_uFirstVisualPos);
	//get the doctemplate
	POSITION templatePos;
	CDFYSimulatorApp* pApp=(CDFYSimulatorApp*)AfxGetApp();
	templatePos=pApp->GetFirstDocTemplatePosition();
	pApp->GetNextDocTemplate(templatePos);
	//get the cocument
	CMultiDocTemplate* pTemplate=(CMultiDocTemplate*)pApp->GetNextDocTemplate(templatePos);
	POSITION docPos=pTemplate->GetFirstDocPosition();
	CDocument* pDoc=pTemplate->GetNextDoc(docPos);
	//get the view
	POSITION viewPos=pDoc->GetFirstViewPosition();
	CExecuteView* pView=(CExecuteView*)pDoc->GetNextView(viewPos);
	//set the view's first visual pos
	pView->SetFirstVisualPos(m_uFirstVisualPos);
	pView->FlushDisplay();
	CMDIChildWnd::OnVScroll(nSBCode, nPos, pScrollBar);
}
void CExecuteChildFrame::ShowExeInfo(CString strExeInfo)
{
//forward to the show command to 
						//CExecuteView
}

CString CExecuteChildFrame::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;
}

BOOL CExecuteChildFrame::LoadFileToMemory(char *DFYexeFile)
{
	CString strState;
	CMainFrame* pMainFrame=(CMainFrame*)AfxGetMainWnd();
	CDFYSimulatorApp* pApp=(CDFYSimulatorApp*)AfxGetApp();
	Memory* pMemory=pApp->GetSimulatorMemory();
	if(!pMemory)
	{
		ReportState("cant't get the simulator's _memory");
	}
	else//if get the memory
	{
		DFYexeFileHeader header;
		ifstream file;
		file.open(DFYexeFile,ios::in);
		if(!file)
		{
			strState.Format("fatal error:can't open file \'%s\' ,or the file is not exist",DFYexeFile);
			ReportState(strState);
			return FALSE;
		}
		else//can open the file
		{
			//load the file header
			ReportState("load DFYexe file header");
			file>>hex>>header.wFileVersion>>header.wFileSize>>header.wCodeSegSize
				>>header.wDataSegSize>>header.wDS>>header.wES>>
				header.wSP>>header.wReserved;
			//reset the memory
			ReportState("reset the memory");
			for(UNSHORT x=0;x<65534;x++)
				pMemory->WriteMemory(x,0);
			//load the file body to memory
			ReportState("load DFYexe file body to memory");
			WORD wData;
			for(int i=0;i<header.wFileSize;i++)
			{
				file>>hex>>wData;
				pMemory->WriteMemory(m_uIP+i,wData);
				//write the instructions to CExecuteView
				//show it can display in the view
			}
			file.close();
			CPU* pCPU=pApp->GetSimulatorCPU();
			if(pCPU)
			{
				ReportState("initlizing the cpu...");
				pCPU->SetSP(header.wSP+m_uIP);
				pCPU->SetIP(m_uIP);
				pCPU->SetDS(header.wDS);
				pCPU->SetES(header.wES);
				pCPU->SetGR(header.wSP+m_uIP,4);
				pCPU->SetPC(m_uIP);
				pCPU->SetGR(0,0);
				pCPU->SetGR(0,1);
				pCPU->SetGR(0,2);
				pCPU->SetGR(0,3);
				pCPU->SetFLAGS(0);
				pCPU->SetContinue(TRUE);
			}
			else
			{
				ReportState("fatal error:can't find the cpu");
				return FALSE;
			}
			//store the file name to CDFYSimulatorApp
			int nSize=strlen(DFYexeFile)-7;
			char* strFile=new char[nSize+1];
			for(int z=0;z<nSize;z++)
				strFile[z]=DFYexeFile[z];
			strFile[nSize]='\0';
			CDFYSimulatorApp* pApp=(CDFYSimulatorApp*)AfxGetApp();
			pApp->SetCurFileName(strFile);
			//fill the content to CExecuteView
			CString strAddr;
			CString strData;
			CString strData1;
			WORD wData1;
			//get the doctemplate
			POSITION templatePos;
			templatePos=pApp->GetFirstDocTemplatePosition();
			pApp->GetNextDocTemplate(templatePos);
			//get the cocument
			CMultiDocTemplate* pTemplate=(CMultiDocTemplate*)pApp->GetNextDocTemplate(templatePos);
			POSITION docPos=pTemplate->GetFirstDocPosition();
			CDocument* pDoc=pTemplate->GetNextDoc(docPos);
			//get the view
			POSITION viewPos=pDoc->GetFirstViewPosition();
			CExecuteView* pView=(CExecuteView*)pDoc->GetNextView(viewPos);
			pView->SetIP(m_uIP);
			pView->SetProExistState(TRUE);
			pView->SetCodeSegSize(header.wCodeSegSize);
			pView->SetActiveWindow();
			pView->ResetContent();
			for(int j=0;j<header.wCodeSegSize;j+=2)
			{
				if(j>=4096)
					break;//support the max size of 2048 instrucitons
				strAddr=ChangeToHexStr(m_uIP+j);
				pMemory->ReadMemory(m_uIP+j,wData1);
				strData=ChangeToHexStr((UNSHORT)wData1);
				pMemory->ReadMemory(m_uIP+1+j,wData1);
				strData1=ChangeToHexStr((UNSHORT)wData1);
				CString str;
				str.Format("%16s%10s%10s",strAddr,strData,strData1);
				pView->AddContent(str);
			};
			LoadVarInfo();
			pView->FlushDisplay();
	
		}//end:can open the file
		
	}//end:if get the memory
	strState.Format("file: %s has been loaded succeeded",DFYexeFile);
	ReportState(strState);
	return TRUE;

}


void CExecuteChildFrame::OnProgramLoad() 
{

	CMainFrame* pMainFrame=(CMainFrame*)AfxGetMainWnd();
 	COutputWindow* pOutputWindow=pMainFrame->GetOutputWindow();
 	if(pOutputWindow)
 		pOutputWindow->ResetContent();
	ReportState("load DFYexe file to memory");
 	LPCTSTR lpszDefExt="DFYexe";
 	LPCTSTR lpszFileName=NULL;
 	DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
 	LPCTSTR lpszFilter="Excutable file(.DFYexe)|*.DFYexe|";
 	CWnd* pParentWnd=NULL;
 	CFileDialog fileDlg(TRUE,lpszDefExt,lpszFileName,dwFlags\
 						,lpszFilter,pParentWnd);
 	fileDlg.DoModal();
 	CString strFilePathName;
 	strFilePathName=fileDlg.GetPathName();
 	if(strFilePathName.IsEmpty())
 	{
 		ReportState("file load have been canceled");
 		return;
 	}
 	else
 	{
 		int nSize=strFilePathName.GetLength();
 		char* DFYexeFile=new char[nSize+1];
 		for(int j=0;j<nSize;j++)
 			DFYexeFile[j]=strFilePathName[j];
 		DFYexeFile[nSize]='\0';
 		CLoadDlg loadDlg(this);
 		int i=loadDlg.DoModal();
 		if(i==IDOK)
 		{
 			m_uIP=loadDlg.GetIP();
 			if(LoadFileToMemory(DFYexeFile))
			{
				m_strCurOpenFile=new char[nSize+1];
				strcpy(m_strCurOpenFile,DFYexeFile);
				SetWindowText(m_strCurOpenFile);
				
			}
 		}
 		else
 		{
 			ReportState("Load have been canceled");
			return;
 		}
 	}
	
}

void CExecuteChildFrame::LoadVarInfo()
{
	CDFYSimulatorApp* pApp=(CDFYSimulatorApp*)AfxGetApp();
	char* fileName=pApp->GetCurFileName();
	if(!fileName)//if not have open any DFYexe file
	{
		ReportState("have't open any DFYexe file ,so I don't know to load witch .pdf file");
		return;
	}//end:	not have open any DFYexe file
	else//if open an DFYexe file
	{
		//get the .pdf file name
		UINT uSize=strlen(fileName);
		char* pdfFileName=new char[uSize+5];
		strcpy(pdfFileName,fileName);
		strcat(pdfFileName,".pdf");
		CString str;
		POSITION templatePos;
		templatePos=pApp->GetFirstDocTemplatePosition();
		pApp->GetNextDocTemplate(templatePos);
		//get the cocument
		CMultiDocTemplate* pTemplate=(CMultiDocTemplate*)pApp->GetNextDocTemplate(templatePos);
		POSITION docPos=pTemplate->GetFirstDocPosition();
		CDocument* pDoc=pTemplate->GetNextDoc(docPos);
		//get the view
		POSITION viewPos=pDoc->GetFirstViewPosition();
		CExecuteView* pView=(CExecuteView*)pDoc->GetNextView(viewPos);
		pView->ResetVarInfo();
		ifstream pdfile;
		pdfile.open(pdfFileName,ios::in);
		if(!pdfile)
		{
			str.Format("can't open the file: %s ,or the file is not exist",pdfFileName);
			ReportState(str);
		}
		else//can open the pdf file
		{
			PdfFileHeader header;
			pdfile>>header.wFileVersion>>header.wRecordNumber;
			for(int i=0;i<header.wRecordNumber;i++)
			{
				char* strVarName=new char[20];
				UNSHORT uVarAddr=0;
				WORD wVarValue=0;
				//get the variable name and address
				pdfile>>strVarName>>uVarAddr>>wVarValue;
				CPU* pCPU=pApp->GetSimulatorCPU();
				UNSHORT uIP=pCPU->GetIP();
				uVarAddr+=uIP;
				pView->AddVarInfo(strVarName,uVarAddr);
			}//load the body
		}//end: can open the pdf file
		pdfile.close();
	}//end:if open an DFYexe file
}

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

⌨️ 快捷键说明

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