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

📄 debugview.cpp

📁 这个也是我们的毕业设计课题
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// DebugView.cpp : implementation file
//

#include "stdafx.h"
#include "DFYSimulator.h"
#include "DebugView.h"
#include "MainFrm.h"
#include "OutputWindow.h"
#include "DebugChildFrame.h"
#include "DFY/CPU.h"
#include "DFY/MemorySegment.H"
#include "DFY/Instruction.h"
#include "MemoryDlg.h"
#include "RegisterDlg.h"
#include "fileheader.h"
#include "StackDlg.h"
#include "fstream"
using namespace std;
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDebugView

IMPLEMENT_DYNCREATE(CDebugView, CScrollView)

CDebugView::CDebugView()
{
	m_caretPos.x=305;
	m_caretPos.y=17;
	m_caretPos1.x=7;
	m_caretPos1.y=17;
	m_caretPos2.x=3;
	m_caretPos2.y=17;
	m_point.x=1;
	m_point.y=17;
	m_point1.x=3;
	m_point1.y=17;
	m_uContentNum=0;
	m_uCodeLineNum=0;
	m_uFirstVisualPos=0;
	m_uScrollRange=28;
	m_bProExist=FALSE;
	m_wCodeSegSize=0;
	m_uSourceLine=0;
	m_uLineIndex=0;
	m_wVarNum=0;
	m_mapInfo=NULL;
	m_breakInfo=NULL;
	m_wPC=0;
	m_uIP=0;
	m_wFLAGS=0;
	m_uIP=0;
	m_dwOP=0;
	m_wPC=0;
	m_wPC1=0;
	m_wSP=0;
	m_wGR[0]=0;
	m_wGR[1]=0;
	m_wGR[2]=0;
	m_wGR[3]=0;
	m_wGR[4]=0;

}

CDebugView::~CDebugView()
{
}


BEGIN_MESSAGE_MAP(CDebugView, CScrollView)
	//{{AFX_MSG_MAP(CDebugView)
	ON_WM_PAINT()
	ON_WM_LBUTTONDOWN()
	ON_COMMAND(ID_ACTION_STEP, OnActionStep)
	ON_COMMAND(ID_ACTION_GO, OnActionGo)
	ON_WM_TIMER()
	ON_COMMAND(ID_ACTION_STEPOUT, OnActionStepout)
	ON_COMMAND(ID_ACTION_STEPOVER, OnActionStepover)
	ON_COMMAND(ID_ACTION_STOPDEBUG, OnActionStopdebug)
	ON_WM_DESTROY()
	ON_COMMAND(ID_EDIT_SETBREAKPOINT, OnEditSetbreakpoint)
	ON_COMMAND(ID_EDIT_CLEARBREAKPIONT, OnEditClearbreakpiont)
	ON_WM_RBUTTONDOWN()
	ON_COMMAND(ID_ACTION_RESET, OnActionReset)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDebugView drawing

void CDebugView::OnInitialUpdate()
{
	CScrollView::OnInitialUpdate();
}

void CDebugView::OnDraw(CDC* pDC)
{

	CDocument* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
		// TODO: add draw code here
	//clear the last display content
	CRect rect;
	GetClientRect(&rect);
	rect.left+=3;
	CBrush whiteBrush;
	pDC->FillRect(&rect,&whiteBrush);
	CDebugChildFrame* pFrame =(CDebugChildFrame*)GetParent();
	int nPos=1;
	nPos+=16;
	//set caret pos
	//show source code
	//show 20 lines
	int temp=m_uSourceLine-20;
	if(temp<0)
		temp=m_uSourceLine;
	pDC->TextOut(34,1,"LineNum  SourceCode");
	m_point1.y=17;
	for(int i=m_uFirstVisualPos;i<=temp;i++)
	{
		if(m_bProExist)
		{
			if((i<m_uCodeLineNum)&&m_breakInfo[i].bIsSet)	
				DrawBreakPoint(m_point1);
		}
		m_point1.y+=16;
		if(i==GetLineNumFromAddr(m_wPC)-1)
		{
			pDC->SetTextColor(RGB(255,0,0));
			pDC->TextOut(45,nPos,m_strSource[i]);
			pDC->SetTextColor(RGB(0,0,0));
			nPos+=16;
			continue;
		}
		pDC->TextOut(45,nPos,m_strSource[i]);
		nPos+=16;
	}
	//show 20 lines 
	//show instruction code
	CDFYSimulatorApp* pApp=(CDFYSimulatorApp*)AfxGetApp();
	nPos=17;
	temp=m_uContentNum-20;
	if(temp<0)
		temp=m_uContentNum;
	pDC->TextOut(295,1,"    MemoryAddr           InsctructionCode");
	for(int ii=m_uFirstVisualPos;ii<=temp;ii++)
	{
		if(ii==((m_wPC-m_uIP)/2)-m_uFirstVisualPos)
		{
			pDC->SetTextColor(RGB(255,0,0));
			pDC->TextOut(295,nPos,m_strInstruction[ii]);
			pDC->SetTextColor(RGB(0,0,0));
			nPos+=16;
			continue;
		}
		
		pDC->TextOut(295,nPos,m_strInstruction[ii]);
		nPos+=16;
	}
	//show variables
	ShowVariable(pDC);
	//show registers
	ShowRegisters(pDC);
	
	if(m_bProExist)
	{
		CDFYSimulatorApp* pApp=(CDFYSimulatorApp*)AfxGetApp();
		CPU* pCPU=pApp->GetSimulatorCPU();
		if(pCPU)
		{
			//draw the arrow flag that poit to the instruction code
			m_caretPos.y=((m_wPC-pCPU->GetIP())/2-m_uFirstVisualPos)*16+17;
			DrawFlag(m_caretPos);
			//draw the arrow flag that point to the source code
			m_caretPos1.y=(GetLineNumFromAddr(m_wPC)-m_uFirstVisualPos-1)*16+17;
			DrawFlag(m_caretPos1);
			return;
		}
		else
		{
			ReportState("can't get the cpu , so can't show the flag");
			return;
		}
	}
	
	// TODO: add draw code here
}

///////////////////////////////////////////////////////////////////////////
// CDebugView message handlers
/////////////////////////////////////////////////////////////////////////////
// CDebugView diagnostics

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

}

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

//

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

void CDebugView::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 CDebugView::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	
}

CString CDebugView::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 CDebugView::ReportState(CString strState)
{
	CMainFrame* pMainFrame=(CMainFrame*)AfxGetMainWnd();
	COutputWindow* pOutputWindow=pMainFrame->GetOutputWindow();
	if(pOutputWindow)
	{
		pOutputWindow->ShowState(strState);
	}
}

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

}

void CDebugView::SetCodeSegSize(WORD wSize)
{
	m_wCodeSegSize=wSize;
}

void CDebugView::SetFirstVisualPos(UNSHORT pos)
{
	m_uFirstVisualPos=pos;
}

void CDebugView::SetProExistState(BOOL bState)
{
	m_bProExist=bState;
}

void CDebugView::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(585,nPos,"the current value of register:");
		nPos+=16;
		//show the registers
		uAddr=pCPU->GetCS();
		strReg.Format("CS = %x",uAddr);
		strReg.MakeUpper();
		pDC->TextOut(585,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(585,nPos,strReg);
			pDC->SetTextColor(RGB(0,0,0));
		}
		else
		{
			pDC->TextOut(585,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(585,nPos,strReg);
			pDC->SetTextColor(RGB(0,0,0));
			m_wPC1=m_wPC;
		}
		else
		{
			pDC->TextOut(585,nPos,strReg);
		}
		nPos+=16;
	
		uAddr=pCPU->GetDS();
		strReg.Format("DS = %x",uAddr);
		strReg.MakeUpper();
		pDC->TextOut(585,nPos,strReg);
		nPos+=16;

		uAddr=pCPU->GetES();
		strReg.Format("ES = %x",uAddr);
		strReg.MakeUpper();
		pDC->TextOut(585,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(585,nPos,strReg);
			pDC->SetTextColor(RGB(0,0,0));
			m_wSP=wData;
		}
		else
		{
			pDC->TextOut(585,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(585,nPos,strReg);
			pDC->SetTextColor(RGB(0,0,0));
			m_wFLAGS=wData;
		}
		else
		{
			pDC->TextOut(585,nPos,strReg);
		}
		nPos+=16;
		nPos=17;
		dwData=pCPU->GetOP();
		strReg.Format("OP = %x",dwData);
		strReg.MakeUpper();
		if(dwData!=m_wSP)
		{
			pDC->SetTextColor(RGB(255,0,0));
			pDC->TextOut(675,nPos,strReg);
			pDC->SetTextColor(RGB(0,0,0));
			m_dwOP=dwData;
		}
		else
		{
			pDC->TextOut(675,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(675,nPos,strReg);
				pDC->SetTextColor(RGB(0,0,0));
				m_wGR[i]=wData;
			}
			else
			{
				pDC->TextOut(675,nPos,strReg);
			}
			nPos+=16;
		}
	}//end:get the cpu
}

void CDebugView::ShowVariable(CDC* pDC)
{
	//show header
	pDC->TextOut(575,150,"VarName       VarAddr   VarVal");
	int nPos=166;
	int temp=m_uFirstVisualPos+10;
	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%6s%10s",strVarName,strVarAddr,strVarValue);
			pDC->TextOut(575,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 CDebugView::AddSource(CString strCode)
{
	if(m_uSourceLine<2048)
	{
		m_strSource[m_uSourceLine]=strCode;
		m_uSourceLine++;//delete the old information
	}
	else
	{
		ReportState("can't add the source to file ,the content is full");
		m_uSourceLine=2048;
	}	
}

void CDebugView::ResetSource()
{
	//clear the content
	m_uSourceLine=0;
	m_uFirstVisualPos=0;

}

void CDebugView::DrawFlag(CPoint 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 CDebugView::FlushDisplay()
{
	CClientDC dc(this);
	OnDraw(&dc);
}

void CDebugView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	ClearCaret();
	m_uLineIndex=GetLineNum(point);//get the line number
	m_caretPos2.y=GetLineY(point);
	m_point.x=m_caretPos2.x;
	m_point.y=m_caretPos2.y;
	DrawCaret(m_caretPos2);
	CScrollView::OnLButtonDown(nFlags, point);
}

UNSHORT CDebugView::GetLineNum(CPoint point)
{
	return ((point.y-17)/16+m_uFirstVisualPos+1);
}

BOOL CDebugView::PreCreateWindow(CREATESTRUCT& cs) 
{
	m_bitmapFlag.LoadBitmap(IDB_FLAG);
		
	return CScrollView::PreCreateWindow(cs);
}

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

BOOL CDebugView::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext) 
{

	return CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
}

⌨️ 快捷键说明

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