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

📄 devicemanager.cpp

📁 一款国产的8051模拟器(全部源代码) 本软件是一款8051模拟器
💻 CPP
字号:
// DeviceManager.cpp: implementation of the DeviceManager class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "MDIDemo.h"
#include "DeviceManager.h"

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

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
DeviceManager DeviceMag;
DeviceManager::DeviceManager()
{
	isrun=0;
	isrunth=0;
	pcpurunthread=NULL;
}

DeviceManager::~DeviceManager()
{

}

int DeviceManager::init()
{
#if OS==0
	memset(DTCB,0,256*sizeof(DeviceTCB));
#endif
#if OS==1 //WINDOWS
	ZeroMemory(DTCB,256*sizeof(DeviceTCB));
#endif
	return 1;
}

DeviceTCB * DeviceManager::GetDeviceListPoint(DWORD DeviceType)
{
	return &(DTCB[DeviceType]);
}

void * DeviceManager::CreateDevice(DWORD DeviceType,TCHAR * DeviceName)
{
	switch(DeviceType)
	{
	case ID_TYPE_8051CPU:
		return Create8051CPU(DeviceName);
	case ID_TYPE_SerialBus:
		return CreateSerialBus(DeviceName);
	case ID_TYPE_MEM:
		return CreateMem(DeviceName);
	default:
		return (void *)1;
	}
	return (void *)1;
}
//BOOL SwitchToThread(void);
UINT CPURUN(LPVOID pParam)//处理器运行线程
{
	
	instruction * pinst=DeviceMag.pinst;
	BYTE numinst=0;
	//BYTE * pcode=
run :	DeviceMag.isrunth=1;//运行状态
	while(DeviceMag.isrun)
	{
		pinst->RUN();
		pinst=pinst->pnextinst;

	}
	DeviceMag.isrunth=0;//暂停
	//SuspendThread(DeviceMag.pcpurunthread->m_hThread);//挂起自己,必须在其他线程中激活
	for(;!DeviceMag.isrun;)
	{
		//Sleep(100);
		Sleep(100);
		//::SwitchToThread();
	}
	goto run;
//	BYTE numinst=0;
//	for(;ptcb[numinst]!=0;)
//	{
//		pinst
	return 1;
}
void * DeviceManager::Create8051CPU(TCHAR *DeviceName)
{
	if(isrun)
	{
		STOP();
	}
	DTCB[ID_TYPE_8051CPU].DeviceType=ID_TYPE_8051CPU;
#if OS!=1
	GlobalVar * pcpu=new GlobalVar;//初始化内存类
#endif
#if OS==1
	GlobalVar * pcpu=(GlobalVar *)VirtualAlloc(NULL,sizeof(GlobalVar),MEM_RESERVE|MEM_COMMIT,PAGE_READWRITE);
	pcpu->GlobalVar::GlobalVar();
#endif
	//GlobalVar * pcpu=new GlobalVar;
	pcpu->init();
	pcpu->mem->VarNum=DTCB[ID_TYPE_8051CPU].Currentpos;//哈哈,忘了设置处理器编号,这里真是个比较严重的BUG
	var8051[DTCB[ID_TYPE_8051CPU].Currentpos]=pcpu;
	strcpy(pcpu->NAME,DeviceName);
	DTCB[ID_TYPE_8051CPU].DEVICEINFO[DTCB[ID_TYPE_8051CPU].Currentpos].DevicePoint=pcpu;
	DTCB[ID_TYPE_8051CPU].DEVICEINFO[DTCB[ID_TYPE_8051CPU].Currentpos].pDeviceName=pcpu->NAME;
	if(DTCB[ID_TYPE_8051CPU].Currentpos==0)
	{
		//AfxBeginThread(
		pcpu->inst->pnextinst=pcpu->inst;
		pinst=pcpu->inst;
		pcpurunthread=AfxBeginThread(CPURUN, this, THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);//创建处理器线程
		//ResumeThread(pthread->m_hThread);
		//SuspendThread(pthread->m_hThread);
	}
	else
	{
		GlobalVar * ppcpu=(GlobalVar *)DTCB[ID_TYPE_8051CPU].DEVICEINFO[DTCB[ID_TYPE_8051CPU].Currentpos-1].DevicePoint;
		ppcpu->inst->pnextinst=pcpu->inst;
		GlobalVar * ppcpu0=(GlobalVar *)DTCB[ID_TYPE_8051CPU].DEVICEINFO[0].DevicePoint;
		pcpu->inst->pnextinst=ppcpu0->inst;
	}
	DTCB[ID_TYPE_8051CPU].Currentpos++;
	return (void *)pcpu;
}

void * DeviceManager::CreateSerialBus(TCHAR *DeviceName)
{
	DTCB[ID_TYPE_SerialBus].DeviceType=ID_TYPE_8051CPU;
#if OS!=1
	SerialBus * psbus=new SerialBus;//初始化内存类
#endif
#if OS==1
	SerialBus * psbus=(SerialBus *)VirtualAlloc(NULL,sizeof(SerialBus),MEM_RESERVE|MEM_COMMIT,PAGE_READWRITE);
	psbus->SerialBus::SerialBus();
#endif
	//=new SerialBus;
	psbus->init(255,255);
	strcpy(psbus->NAME,DeviceName);
	DTCB[ID_TYPE_SerialBus].DEVICEINFO[DTCB[ID_TYPE_SerialBus].Currentpos].DevicePoint=psbus;
	DTCB[ID_TYPE_SerialBus].DEVICEINFO[DTCB[ID_TYPE_SerialBus].Currentpos].pDeviceName=psbus->NAME;
	DTCB[ID_TYPE_SerialBus].Currentpos++;
	return (void *)psbus;
}

void * DeviceManager::CreateMem(TCHAR *pName)
{
	DTCB[ID_TYPE_MEM].DeviceType=ID_TYPE_MEM;
#if OS!=1
	BYTE * pmem=new BYTE[65536];//初始化内存类
	TCHAR * strname=new TCHAR[512];
#endif
#if OS==1
	BYTE * pmem=(BYTE *)VirtualAlloc(NULL,sizeof(BYTE)*65536,MEM_RESERVE|MEM_COMMIT,PAGE_READWRITE);
	TCHAR * strname=(TCHAR *)VirtualAlloc(NULL,sizeof(TCHAR)*512,MEM_RESERVE|MEM_COMMIT,PAGE_READWRITE);
#endif
	//BYTE * pmem=new BYTE[65536];
	//TCHAR * strname=new TCHAR[512];
	strcpy(strname,pName);
	DTCB[ID_TYPE_MEM].DEVICEINFO[DTCB[ID_TYPE_MEM].Currentpos].DevicePoint=pmem;
	DTCB[ID_TYPE_MEM].DEVICEINFO[DTCB[ID_TYPE_MEM].Currentpos].pDeviceName=strname;
	DTCB[ID_TYPE_MEM].Currentpos++;
	return (void *)pmem;
}



int DeviceManager::RUN()
{
	if(isrun==1)
	{
		return 257;
	}
	if(pcpurunthread==NULL)
	{
		return -1;
	}
	GlobalVar * pcpu=NULL;
	for(int i=0;i<DeviceMag.DTCB[ID_TYPE_8051CPU].Currentpos;i++)
	{
		pcpu=(GlobalVar *)DeviceMag.DTCB[ID_TYPE_8051CPU].DEVICEINFO[i].DevicePoint;
		if(pcpu->isready!=0x3)
		{
			return i;//没有初始化的处理器编号
		}
	}
	isrun=1;
	ResumeThread(pcpurunthread->m_hThread);
	return 257;
}

int DeviceManager::STOP()
{
	if(isrun==0)
	{
		return 1;
	}
	isrun=0;
	for(int i=0;i<20&&isrunth==1;)//如果是运行状态,则等待线程运行
	{
		Sleep(10);
		i++;
		//::SwitchToThread();
	}
	if(i==20)
	{
		return -1;
	}
//	SuspendThread(pcpurunthread->m_hThread);
	
	return 1;
}

⌨️ 快捷键说明

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