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

📄 variabledlg.cpp

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

#include "stdafx.h"
#include <iostream>

#include <fstream>
using namespace std;
#include "DFYSimulator.h"
#include "VariableDlg.h"
#include "MainFrm.h"
#include "OutputWindow.h"
#include "DFY/MemorySegment.h"
#include "DFY/CPU.h"
#include "fileheader.h"



#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CVariableDlg dialog


CVariableDlg::CVariableDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CVariableDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CVariableDlg)
	m_strVarName = _T("");
	m_iVarValue = 0;
	m_uScrRange=7;
	m_wVarNum=0;
	m_varInfo=NULL;
	//}}AFX_DATA_INIT
}


void CVariableDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CVariableDlg)
	DDX_Control(pDX, IDC_SCR_VAR, m_scrVar);
	DDX_Control(pDX, IDC_LIST_VARIABLE, m_listVar);
	DDX_Text(pDX, IDC_EDIT_VARIABLENAME, m_strVarName);
	DDX_Text(pDX, IDC_EDIT_VARIABLEVALUE, m_iVarValue);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CVariableDlg, CDialog)
	//{{AFX_MSG_MAP(CVariableDlg)
	ON_WM_CLOSE()
	ON_WM_MOVE()
	ON_WM_PAINT()
	ON_WM_VSCROLL()
	ON_LBN_SELCHANGE(IDC_LIST_VARIABLE, OnSelchangeListVariable)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CVariableDlg message handlers


void CVariableDlg::OnOK()
{
	UpdateData(TRUE);
	int nSize=0;
	//get the variable name
	for(int i=0;i<m_strVarName.GetLength();i++)
	{
		if(m_strVarName[i]!=' ')
			nSize++;
		else
			break;
	}
	if(nSize<=0)return;//if not input any variable name
	char* varName=new char[nSize+1];
	for(int j=0;j<nSize;j++)
		varName[j]=m_strVarName[j];
	varName[nSize]='\0';
	//find the variable in the m_varInfo ,and get the pos
	for(int z=0;z<m_wVarNum;z++)
	{
		if(strcmp(m_varInfo[z].varName,varName)==0)
			break;
	}
	if(z>=m_wVarNum)//if can't get the variable
	{
		CString str;
		str.Format("can't find the variable \'%s\' ,or this variable is not defined",varName);
		ReportState(str);
	}
	else//get the variable pos z
	{
		//get the variable Address 
		UNSHORT uAddr=m_varInfo[z].uVarAddr;
		//get the memory
		CDFYSimulatorApp* pApp=(CDFYSimulatorApp*)AfxGetApp();
		Memory* pMemory=pApp->GetSimulatorMemory();
		pMemory->WriteMemory(uAddr,m_iVarValue);
		FlushVariable();
	}//end:get the variable pos
}
void CVariableDlg::OnCancel()
{
}

void CVariableDlg::OnClose() 
{
	CMainFrame* pFrame=(CMainFrame*)GetParent();
	pFrame->SetVariableDlg(NULL);
	DestroyWindow();
	CDialog::OnClose();
}

void CVariableDlg::OnMove(int x, int y) 
{
	CDialog::OnMove(x, y);
	GetParent()->SetActiveWindow();
	// TODO: Add your message handler code here
}

void CVariableDlg::FlushVariable()
{
	m_listVar.ResetContent();
	int temp=m_uFirstVisualPos+7;
	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%20s%20s",strVarName,strVarAddr,strVarValue);
			m_listVar.AddString(str);
		}
		else//have not get the memory
		{
				ReportState("have not get the memory ,the memory is not exist");
		}//end: if get the memory
	}//end: for
}

BOOL CVariableDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	m_uFirstVisualPos=0;
	LoadVarInfo();
	m_scrVar.SetScrollRange(0,m_uScrRange);
	//load the .pdf file 
	//add the variable information to the variable dialog
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CVariableDlg::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	FlushVariable();	
	// Do not call CDialog::OnPaint() for painting messages
}

void CVariableDlg::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;
		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;
			m_wVarNum=header.wRecordNumber;
			m_varInfo=new VarInfo[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;
				strcpy(m_varInfo[i].varName,strVarName);
				m_varInfo[i].uVarAddr=uVarAddr;
			}//load the body
			m_uScrRange=header.wRecordNumber;
			m_wVarNum=header.wRecordNumber;
		}//end: can open the pdf file
		pdfile.close();
	}//end:if open an DFYexe file

}

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

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


void CVariableDlg::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	m_uFirstVisualPos=pScrollBar->GetScrollPos();
	switch(nSBCode)
	{
	case SB_LINEDOWN:
		m_uFirstVisualPos+=1;
		break;
	case SB_LINEUP:
		m_uFirstVisualPos-=1;
		break;
	case SB_PAGEDOWN:
		m_uFirstVisualPos+=7;
	case SB_PAGEUP:
		m_uFirstVisualPos-=7;
		break;
	case SB_THUMBPOSITION:
		m_uFirstVisualPos=nPos;
		break;
	case SB_THUMBTRACK:
		m_uFirstVisualPos=nPos;
		break;
	default:
		break;
	}
	if(m_uFirstVisualPos>=m_uScrRange)
		m_uFirstVisualPos=m_uScrRange;
	if(m_uFirstVisualPos<0)
		m_uFirstVisualPos=0;
	pScrollBar->SetScrollPos(m_uFirstVisualPos);
	FlushVariable();
	CDialog::OnVScroll(nSBCode, nPos, pScrollBar);
}

void CVariableDlg::OnSelchangeListVariable() 
{
	//get the index of the selected item
	int index=m_listVar.GetCurSel();
	if(index==LB_ERR)
	{
		ReportState("the select action failed");
	}
	else
	{
		//get the variable position in the m_varInfo
		UINT uPos=index+m_uFirstVisualPos;
		//get the memory
		CDFYSimulatorApp* pApp=(CDFYSimulatorApp*)AfxGetApp();
		Memory* pMemory=pApp->GetSimulatorMemory();
		if(pMemory)
		{
			//get the value
			UNSHORT uAddr=m_varInfo[uPos].uVarAddr;
			WORD wVarValue=0;
			pMemory->ReadMemory(uAddr,wVarValue);
			CString str;
			m_listVar.GetText(index,str);
			int nSize=0;
			for(int i=0;i<(int)strlen(str);i++)
			{
				if(str[i]!=' ')
					nSize++;
				else
					break;
			}
			char* lpStr=new char[nSize+1];
			for(int j=0;j<nSize;j++)
				lpStr[j]=str[j];
			lpStr[nSize]='\0';
			m_strVarName=lpStr;
			m_iVarValue=(int)wVarValue;
			UpdateData(FALSE);
		}//end if get the memory
		else//have not get the memory
		{
			ReportState("can't get the memory ,or the memory is not exist");
			return;
		}
	}//end:if select is valid
}

⌨️ 快捷键说明

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