📄 debugchildframe.cpp
字号:
// DebugChildFrame.cpp : implementation file
//
#include "stdafx.h"
#include "DFYSimulator.h"
#include "DebugChildFrame.h"
#include "OutputWindow.h"
#include "MainFrm.h"
#include "DebugView.h"
#include "fileheader.h"
#include "fstream"
using namespace std;
#include "DFY/MemorySegment.h"
#include "DFY/CPU.h"
#include "LoadDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDebugChildFrame
IMPLEMENT_DYNCREATE(CDebugChildFrame, CMDIChildWnd)
CDebugChildFrame::CDebugChildFrame()
{
m_uFirstVisualPos=0;
m_uIP=0;
m_uScrollRange=0;
}
CDebugChildFrame::~CDebugChildFrame()
{
}
BEGIN_MESSAGE_MAP(CDebugChildFrame, CMDIChildWnd)
//{{AFX_MSG_MAP(CDebugChildFrame)
ON_WM_CLOSE()
ON_COMMAND(ID_PROGRAM_LOAD, OnProgramLoad)
ON_COMMAND(ID_PROGRAM_EXIT, OnProgramExit)
ON_WM_PAINT()
ON_WM_VSCROLL()
ON_WM_LBUTTONDOWN()
ON_WM_CREATE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDebugChildFrame message handlers
void CDebugChildFrame::OnClose()
{
CMenu* pMenu=new CMenu();
pMenu->LoadMenu(IDR_MAINFRAME);
GetMDIFrame()->SetMenu(pMenu);
CDFYSimulatorApp* pApp=(CDFYSimulatorApp*)AfxGetApp();
pApp->SetDebugFrameState(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);
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);
pView->SetProExistState(FALSE);
//set the open file name to NULL
//clear the output
CMainFrame* pMainFrame=(CMainFrame*)AfxGetMainWnd();
COutputWindow* pOutputWindow=pMainFrame->GetOutputWindow();
pOutputWindow->ResetContent();
CMDIChildWnd::OnClose();
}
void CDebugChildFrame::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))
{
CString str;
str.Format("load file: %s failed",DFYexeFile);
ReportState(str);
}
}
else
{
ReportState("Load have been canceled");
return;
}
}
}
void CDebugChildFrame::OnProgramExit()
{
CMainFrame* pMainFrame=(CMainFrame*)AfxGetMainWnd();
COutputWindow* pOutputWindow=pMainFrame->GetOutputWindow();
pOutputWindow->ResetContent();
CDebugChildFrame::OnClose();
}
void CDebugChildFrame::OnPaint()
{
CPaintDC dc(this); // device context for painting
// Do not call CMDIChildWnd::OnPaint() for painting messages
}
BOOL CDebugChildFrame::PreCreateWindow(CREATESTRUCT& cs)
{
return CMDIChildWnd::PreCreateWindow(cs);
}
void CDebugChildFrame::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);
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);
//set the view's first visual pos
pView->SetFirstVisualPos(m_uFirstVisualPos);
pView->FlushDisplay();
CMDIChildWnd::OnVScroll(nSBCode, nPos, pScrollBar);
}
CString CDebugChildFrame::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 CDebugChildFrame::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 CDebugView
//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 CDebugView
CString strAddr;
CString strData;
CString strData1;
WORD wData1;
//get the doctemplate
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);
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%20s%10s",strAddr,strData,strData1);
pView->AddContent(str);
};
//add the source code to CDebugView
pView->ResetSource();
ReportState("add source code...");
//get the source name
nSize=strlen(DFYexeFile)-3;
char* asmFileName=new char[nSize+1];
for(int ii=0;ii<nSize-3;ii++)
asmFileName[ii]=DFYexeFile[ii];
asmFileName[nSize-3]='a';
asmFileName[nSize-2]='s';
asmFileName[nSize-1]='m';
asmFileName[nSize]='\0';
ifstream asmfile;
asmfile.open(asmFileName,ios::in);
if(!asmfile)//can't open the file
{
strState.Format("can't open the file: %s ,or the file is no exist",asmFileName);
}//can't open the file
else//can open the file
{
WORD lineNum=0;
char* pBuf=new char[41];
CString strLineNum;
CString strContent;
while(!asmfile.eof())
{
lineNum++;
strLineNum=ChangeToHexStr(lineNum);
asmfile.getline(pBuf,40);
strContent.Format("%-8s%s",strLineNum,pBuf);
pView->AddSource(strContent);
}
}//end:can open the file
asmfile.close();
LoadVarInfo();
pView->LoadMapInfo();
pView->FlushDisplay();
}//end:can open the file
}//end:if get the memory
strState.Format("file: %s has been loaded succeeded",DFYexeFile);
ReportState(strState);
SetWindowText(DFYexeFile);
return TRUE;
}
void CDebugChildFrame::ReportState(CString strState)
{
CMainFrame* pMainFrame=(CMainFrame*)AfxGetMainWnd();
COutputWindow* pOutputWindow=(COutputWindow*)pMainFrame->GetOutputWindow();
if(pOutputWindow)
pOutputWindow->ShowState(strState);
}
void CDebugChildFrame::OnLButtonDown(UINT nFlags, CPoint point)
{
CMDIChildWnd::OnLButtonDown(nFlags, point);
}
int CDebugChildFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CMDIChildWnd::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
return 0;
}
BOOL CDebugChildFrame::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CMDIFrameWnd* pParentWnd, CCreateContext* pContext)
{
// TODO: Add your specialized code here and/or call the base class
dwStyle=dwStyle|WS_VSCROLL;
return CMDIChildWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, pContext);
}
void CDebugChildFrame::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);
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);
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();
//load map information
}//end:if open an DFYexe file
}
void CDebugChildFrame::SetIP(WORD wIP)
{
m_uIP=wIP;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -