📄 dfysimulatorview.cpp
字号:
// DFYSimulatorView.cpp : implementation of the CDFYSimulatorView class
//
#include "stdafx.h"
#include "DFYSimulator.h"
#include "DFYSimulatorDoc.h"
#include "DFYSimulatorView.h"
#include "MainFrm.h"
#include "OutputWindow.h"
#include "DebugChildFrame.h"
#include "DebugView.h"
#include "ExecuteView.h"
#include "ExecuteChildFrame.h"
#include "LoadDlg.h"
#include "MemoryDlg.h"
#include "Compiler.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDFYSimulatorView
IMPLEMENT_DYNCREATE(CDFYSimulatorView, CEditView)
BEGIN_MESSAGE_MAP(CDFYSimulatorView, CEditView)
//{{AFX_MSG_MAP(CDFYSimulatorView)
ON_WM_KEYDOWN()
ON_WM_CHAR()
ON_COMMAND(ID_FILE_SAVE, OnFileSave)
ON_COMMAND(ID_FILE_SAVE_AS, OnFileSaveAs)
ON_COMMAND(ID_COMPILE_ASSEMBLE, OnAssemble)
ON_COMMAND(ID_ACTION_GO, OnActionGo)
ON_COMMAND(ID_COMPILE_DEBUG, OnDebug)
ON_COMMAND(ID_COMPILE_EXCUTE, OnCompileExcute)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CEditView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CEditView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CEditView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDFYSimulatorView construction/destruction
CDFYSimulatorView::CDFYSimulatorView()
{
m_bAssembleSucceed=FALSE;
}
CDFYSimulatorView::~CDFYSimulatorView()
{
}
BOOL CDFYSimulatorView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CEditView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CDFYSimulatorView drawing
void CDFYSimulatorView::OnDraw(CDC* pDC)
{
CDFYSimulatorDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
}
void CDFYSimulatorView::OnInitialUpdate()
{
CEditView::OnInitialUpdate();
}
/////////////////////////////////////////////////////////////////////////////
// CDFYSimulatorView printing
BOOL CDFYSimulatorView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CDFYSimulatorView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CDFYSimulatorView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CDFYSimulatorView diagnostics
#ifdef _DEBUG
void CDFYSimulatorView::AssertValid() const
{
CEditView::AssertValid();
}
void CDFYSimulatorView::Dump(CDumpContext& dc) const
{
CEditView::Dump(dc);
}
CDFYSimulatorDoc* CDFYSimulatorView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDFYSimulatorDoc)));
return (CDFYSimulatorDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CDFYSimulatorView message handlers
void CDFYSimulatorView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default]
CEditView::OnKeyDown(nChar, nRepCnt, nFlags);
}
void CDFYSimulatorView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
CEditView::OnChar(nChar, nRepCnt, nFlags);
}
void CDFYSimulatorView::OnFileSave()
{
CDFYSimulatorDoc* pDFYSimulatorDoc=GetDocument();
CString strFilePathName;
strFilePathName=pDFYSimulatorDoc->GetPathName();
if(strFilePathName.IsEmpty())//this file had never been saved
{
LPCTSTR lpszDefExt="asm";
LPCTSTR lpszFileName=NULL;
DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
LPCTSTR lpszFilter="Assemble file (.asm)|*.asm|";
CWnd* pParentWnd=NULL;
CFileDialog fileDlg(FALSE,lpszDefExt,lpszFileName,dwFlags,lpszFilter,pParentWnd);
fileDlg.DoModal();
strFilePathName=fileDlg.GetPathName();
}
if(!strFilePathName.IsEmpty())
{
CFile file;
file.Open(strFilePathName,CFile::modeCreate | CFile::modeWrite);
file.Write(LockBuffer(),GetBufferLength());
UnlockBuffer();
file.Close();
pDFYSimulatorDoc->SetPathName(strFilePathName);//save the file
//path name to the document
pDFYSimulatorDoc->SetModifiedFlag(FALSE);//The modifies already save
}
}
void CDFYSimulatorView::OnFileSaveAs()
{
LPCTSTR lpszDefExt="asm";
LPCTSTR lpszFileName=NULL;
DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
LPCTSTR lpszFilter="Assemble file (.asm)|*.asm|";
CWnd* pParentWnd=NULL;
CFileDialog fileDlg(FALSE,lpszDefExt,lpszFileName,dwFlags,lpszFilter,pParentWnd);
fileDlg.DoModal();
CString strFilePathName;
strFilePathName=fileDlg.GetPathName();
if(!strFilePathName.IsEmpty())
{
CFile file;
file.Open(strFilePathName,CFile::modeCreate | CFile::modeWrite);
file.Write(LockBuffer(),GetBufferLength());
UnlockBuffer();
file.Close();
CDFYSimulatorDoc* pDFYSimulatorDoc=GetDocument();
pDFYSimulatorDoc->SetPathName(strFilePathName);//save the file
//path name to the document
pDFYSimulatorDoc->SetModifiedFlag(FALSE);//The modifies already save
}
}
void CDFYSimulatorView::OnAssemble()
{
OnFileSave();
CDFYSimulatorDoc* pDFYSimulatorDoc=GetDocument();
CString strPath=pDFYSimulatorDoc->GetPathName();
if(!strPath.IsEmpty())
{
int nSize=strPath.GetLength();
//get the asm source file name in char*
char* strAsmSource=new char[nSize+1];
for(int i=0;i<nSize;i++)
strAsmSource[i]=strPath.GetAt(i);
strAsmSource[nSize]='\0';
//get the pdfFile name in char*
char* pdfFile=new char[nSize+1];
for(int j=0;j<nSize-3;j++)
pdfFile[j]=strPath.GetAt(j);
pdfFile[nSize-3]='p';
pdfFile[nSize-2]='d';
pdfFile[nSize-1]='f';
pdfFile[nSize]='\0';
//get the DFYexeFileName
char* DFYexeFile=new char[nSize+4];
for(int z=0;z<nSize-3;z++)
DFYexeFile[z]=strPath.GetAt(z);
DFYexeFile[nSize-3]='D';
DFYexeFile[nSize-2]='F';
DFYexeFile[nSize-1]='Y';
DFYexeFile[nSize]='e';
DFYexeFile[nSize+1]='x';
DFYexeFile[nSize+2]='e';
DFYexeFile[nSize+3]='\0';
//compiling...
CCompiler compiler;
if(compiler.Compile(strAsmSource,pdfFile,DFYexeFile))
m_bAssembleSucceed=TRUE;
else
m_bAssembleSucceed=FALSE;
}
}
void CDFYSimulatorView::OnActionGo()
{
//assemble the file first
OnAssemble();
if(m_bAssembleSucceed)
{ CDFYSimulatorDoc* pDFYSimulatorDoc=GetDocument();
CString strFilePathName;
strFilePathName=pDFYSimulatorDoc->GetPathName();
//get the exe filename
int nSize=strFilePathName.GetLength();
char* DFYexeFile=new char[nSize+4];
for(int i=0;i<nSize-3;i++)
DFYexeFile[i]=strFilePathName[i];
DFYexeFile[nSize-3]='D';
DFYexeFile[nSize-2]='F';
DFYexeFile[nSize-1]='Y';
DFYexeFile[nSize]='e';
DFYexeFile[nSize+1]='x';
DFYexeFile[nSize+2]='e';
DFYexeFile[nSize+3]='\0';
//into the debug mode
CDFYSimulatorApp* pApp=(CDFYSimulatorApp*)AfxGetApp();
pApp->OnModeDebug();
CPU* pCPU=pApp->GetSimulatorCPU();
if(pCPU)
{
CLoadDlg loadDlg(this);
int i=loadDlg.DoModal();
if(i==IDOK)
{
//get DebgChildFrame
POSITION templatePos;
templatePos=pApp->GetFirstDocTemplatePosition();
pApp->GetNextDocTemplate(templatePos);
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();
CDebugView* pView=(CDebugView*)pDoc->GetNextView(viewPos);
CDebugChildFrame* pFrame=(CDebugChildFrame*)pView->GetParent();
pFrame->SetIP(loadDlg.GetIP());
if(!pFrame->LoadFileToMemory(DFYexeFile))
{
CString str;
str.Format("load file: %s failed",DFYexeFile);
ReportState(str);
}
}
}//end:the cpu is valid
else
{
ReportState("can't get cpu");
}
}
}
void CDFYSimulatorView::ReportState(CString strState)
{
CMainFrame* pFrame=(CMainFrame*)AfxGetMainWnd();
COutputWindow* pOutput=pFrame->GetOutputWindow();
if(pOutput)
{
pOutput->ShowState(strState);
}
}
void CDFYSimulatorView::OnDebug()
{
//assemble the file first
OnAssemble();
if(m_bAssembleSucceed){
CDFYSimulatorDoc* pDFYSimulatorDoc=GetDocument();
CString strFilePathName;
strFilePathName=pDFYSimulatorDoc->GetPathName();
//get the exe filename
int nSize=strFilePathName.GetLength();
char* DFYexeFile=new char[nSize+4];
for(int i=0;i<nSize-3;i++)
DFYexeFile[i]=strFilePathName[i];
DFYexeFile[nSize-3]='D';
DFYexeFile[nSize-2]='F';
DFYexeFile[nSize-1]='Y';
DFYexeFile[nSize]='e';
DFYexeFile[nSize+1]='x';
DFYexeFile[nSize+2]='e';
DFYexeFile[nSize+3]='\0';
//into the debug mode
CDFYSimulatorApp* pApp=(CDFYSimulatorApp*)AfxGetApp();
pApp->OnModeDebug();
CPU* pCPU=pApp->GetSimulatorCPU();
if(pCPU)
{
CLoadDlg loadDlg(this);
int i=loadDlg.DoModal();
if(i==IDOK)
{
//get DebgChildFrame
POSITION templatePos;
templatePos=pApp->GetFirstDocTemplatePosition();
pApp->GetNextDocTemplate(templatePos);
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();
CDebugView* pView=(CDebugView*)pDoc->GetNextView(viewPos);
CDebugChildFrame* pFrame=(CDebugChildFrame*)pView->GetParent();
pFrame->SetIP(loadDlg.GetIP());
if(!pFrame->LoadFileToMemory(DFYexeFile))
{
CString str;
str.Format("load file: %s failed",DFYexeFile);
ReportState(str);
}
}
}//end:the cpu is valid
else
{
ReportState("can't get cpu");
}
}
}
void CDFYSimulatorView::OnCompileExcute()
{
//assemble the file first
OnAssemble();
if(m_bAssembleSucceed){
CDFYSimulatorDoc* pDFYSimulatorDoc=GetDocument();
CString strFilePathName;
strFilePathName=pDFYSimulatorDoc->GetPathName();
//get the exe filename
int nSize=strFilePathName.GetLength();
char* DFYexeFile=new char[nSize+4];
for(int i=0;i<nSize-3;i++)
DFYexeFile[i]=strFilePathName[i];
DFYexeFile[nSize-3]='D';
DFYexeFile[nSize-2]='F';
DFYexeFile[nSize-1]='Y';
DFYexeFile[nSize]='e';
DFYexeFile[nSize+1]='x';
DFYexeFile[nSize+2]='e';
DFYexeFile[nSize+3]='\0';
//into the debug mode
CDFYSimulatorApp* pApp=(CDFYSimulatorApp*)AfxGetApp();
pApp->OnModeExecute();
CPU* pCPU=pApp->GetSimulatorCPU();
if(pCPU)
{
CLoadDlg loadDlg(this);
int i=loadDlg.DoModal();
if(i==IDOK)
{
//get DebgChildFrame
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);
CExecuteChildFrame* pFrame=(CExecuteChildFrame*)pView->GetParent();
pFrame->SetIP(loadDlg.GetIP());
if(!pFrame->LoadFileToMemory(DFYexeFile))
{
CString str;
str.Format("load file: %s failed",DFYexeFile);
ReportState(str);
}
}
}//end:the cpu is valid
else
{
ReportState("can't get cpu");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -