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

📄 cpudlg.cpp

📁 一个简单的8位cpu的功能实现,从外部导入机器码,模拟cpu的运行
💻 CPP
字号:
// cpuDlg.cpp : implementation file

#include "stdafx.h"
#include "cpu.h"
#include "cpuDlg.h"
#include "math.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();
  
// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	virtual void OnOK();
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};


CString toString(int a[]){             //返回把存储int类型的长度为8数组转换成CString
	CString h="00000000";
	for(int i=0;i<8;i++)
	  h.SetAt(i,a[i]+48);
	return h;} 

CString toString1(int b[]){           //返回把存储in类型t的长度为2数组转换成CString
   CString h="00";
	for(int i=0;i<2;i++)
	  h.SetAt(i,b[i]+48);
	return h;
}

void toint(CString s,int a[]){       //把CString转换成存储int类型的长度为8数组
  for(int i=0;i<8;i++)
		a[i]=s.GetAt(i)-48;
	
}
int toshi(int a[]){                  //转换成十进制
	int x=0;
	for(int i=0;i<8;i++)
		x+=a[7-i]*int(pow(2,i));
    return x;
}

void totwo(int x,int a[]){          //转换成二进制的数组
	
	a[7]=x%2;
	for(int i=0;i<6;i++)
	{x=int(x/2);
	a[6-i]=x%2;}
}



CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCpuDlg dialog

CCpuDlg::CCpuDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCpuDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCpuDlg)
	start = _T("");
	text = _T("");
	AR = _T("");
	PC = _T("");
	DR = _T("");
	AC = _T("");
	IR = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCpuDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCpuDlg)
	DDX_Control(pDX, IDC_LIST1, memory);
	DDX_Text(pDX, IDC_EDIT6, start);
	DDX_Text(pDX, IDC_EDIT5, text);
	DDX_Text(pDX, IDC_EDIT1, AR);
	DDX_Text(pDX, IDC_EDIT2, PC);
	DDX_Text(pDX, IDC_EDIT3, DR);
	DDX_Text(pDX, IDC_EDIT4, AC);
	DDX_Text(pDX, IDC_EDIT7, IR);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCpuDlg, CDialog)
	//{{AFX_MSG_MAP(CCpuDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_EN_CHANGE(IDC_EDIT5, OnChangeEdit5)
	ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_LBN_SELCHANGE(IDC_LIST1, OnSelchangeList1)
	ON_BN_CLICKED(initialize, Oninitialize)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCpuDlg message handlers

BOOL CCpuDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CCpuDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CCpuDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CCpuDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CCpuDlg::OnButton1() 
{
 CFileDialog open(1); //定义CFileDialog类的函数
 open.DoModal();
 CString s=open.GetFileName();//定义读取文件名
  
 CFile m;      
 char* n=new char [80];
 
 for(int i=0;i<80;i++)      
	 n[i]=' ';
 m.Open(s,0); 
 m.Read(n,80); //读取文件数据
 PC=n;

 UpdateData(0);
}
int mem[64][8];
int a[8];
void CCpuDlg::Oninitialize() 
{
	
	start="00000000"; //初始化
  

	AR="00000000";
	PC="00000000";
	DR="00000000";
	AC="00000000";
	
for(int k=63;k>=0;k--)
{totwo(k,a);
for(int i=0;i<8;i++)
mem[k][i]=a[i];
memory.InsertString(0,toString(mem[k]));}

    UpdateData(0);

}

void CCpuDlg::OnChangeEdit5() 
{   
	
}
void CCpuDlg::OnOK() 
{
	// TODO: Add extra validation here
	
	CDialog::OnOK();
}
void CCpuDlg::OnButton4() 
{
	CDialog::OnOK();	
}

int c=1;     
int i=0;  
int x=0;  
int y=0;
int z=0;
int p=0;

int d[8];
int e[8];
int b[2];

void CCpuDlg::OnButton2() 
{ 
	switch(c){
	case 1:text="FETCH1:AR<-PC";
	       
		   AR=PC;
		   c++;
		   break;
	case 2:text="FETCH2:DR<-M,PC<-PC+1";
		   toint(PC,a);
		   x=toshi(a)+1;
		   totwo(x,a);
		   PC=toString(a);
		   DR=AR;
		   c++;
		   break;
    case 3:text="FETCH3:IR<-DR[7..6],AR<-DR[5..0]";
		   toint(DR,a);
		   b[1]=a[1];
		   b[0]=a[0];
		   IR=toString1(b);
	   	   if(IR=="00")c++;
		   else if(IR=="01")c+=3;
		   else if(IR=="10")c+=5;
		   else if(IR=="11")c+=6;
		   break;
	case 4:text="ADD1:DR<-M";
		   c++;
		   break;	
    case 5:text="ADD2:AC<-AC+DR";
		   toint(AC,d);
		   y=toshi(d);
		   toint(DR,e);
		   z=toshi(e);
		   z+=y;
		   totwo(z,e);
		   AC=toString(e);
		   c=1;
		   break;
    case 6:text="AND1:DR<-M;";
		   c++;
		   break;
	case 7:text="AND2:AC<-AC^DR";
		   toint(AC,d);
		   toint(DR,e);
		   
		  for(i=0;i<8;i++)
		  { e[i]=d[i]&e[i];}
		   AC=toString(e);
		   c=1;
		   break;
    case 8:text="JMP1:PC<-Dr[5..0]";
		   
		   c=1;
		   break;
	case 9:text="INC1:AC<-AC+1";
		   toint(AC,a);
		   p=toshi(a)+1;
		   totwo(p,a);
		   AC=toString(a);
		   c=1;
		   break;
}
    UpdateData(0);
  
	// TODO: Add your control notification handler code here
	}
void CCpuDlg::OnSelchangeList1() 
{
	// TODO: Add your control notification handler code here
	
}


void CAboutDlg::OnOK() 
{
	// TODO: Add extra validation here
	
	CDialog::OnOK();
}

⌨️ 快捷键说明

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