📄 virtualmachine.h
字号:
// VirtualMachine.h: interface for the CVirtualMachine class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_VIRTUALMACHINE_H__9DB59720_032D_11D3_BAEF_005004230120__INCLUDED_)
#define AFX_VIRTUALMACHINE_H__9DB59720_032D_11D3_BAEF_005004230120__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "defines.h"
// defining the stack entry stuff
// and the operators needed
struct StackEntry
{
BYTE m_nType;
INFOTYPE m_Info;
// default constructor
StackEntry()
{
m_nType = SE_UNDEFINED;
m_Info = 0;
}
// copy constructor
StackEntry(StackEntry& entry)
{
*this = entry;
}
// The operators
StackEntry &operator=(StackEntry& entry)
{
m_nType = entry.m_nType ;
m_Info = entry.m_Info ;
return *this;
}
BOOLEAN operator < (StackEntry& entry)
{
switch (m_nType)
{
case SE_REAL:
return CV_REAL(m_Info) < CV_REAL(entry.m_Info) ;
default:
return m_Info < entry.m_Info;
}
}
BOOLEAN operator > (StackEntry& entry)
{
switch (m_nType)
{
case SE_REAL:
return CV_REAL(m_Info) > CV_REAL(entry.m_Info) ;
default:
return m_Info > entry.m_Info;
}
}
BOOLEAN operator <= (StackEntry& entry)
{
switch (m_nType)
{
case SE_REAL:
return CV_REAL(m_Info) <= CV_REAL(entry.m_Info) ;
default:
return m_Info <= entry.m_Info;
}
}
BOOLEAN operator >= (StackEntry& entry)
{
switch (m_nType)
{
case SE_REAL:
return CV_REAL(m_Info) >= CV_REAL(entry.m_Info) ;
default:
return m_Info >= entry.m_Info;
}
}
BOOLEAN operator == (StackEntry& entry)
{
switch (m_nType)
{
case SE_INTEGER:
return (CV_INTEGER(m_Info) == CV_INTEGER(entry.m_Info));
case SE_REAL:
return (CV_REAL(m_Info) == CV_REAL(entry.m_Info));
case SE_CHAR:
return (CV_CHAR(m_Info) == CV_CHAR(entry.m_Info));
}
return FALSE;
}
BOOLEAN operator != (StackEntry& entry)
{
return m_Info != entry.m_Info;
}
StackEntry operator + (StackEntry & entry)
{
StackEntry result;
result.m_nType = max(m_nType, entry.m_nType);
switch (result.m_nType)
{
case SE_REAL:
CV_REAL(result.m_Info) = CV_REAL(m_Info) + CV_REAL(entry.m_Info) ;
break;
case SE_CHAR:
CV_CHAR(result.m_Info) = CV_CHAR(m_Info) + CV_CHAR(entry.m_Info) ;
break;
case SE_INTEGER:
CV_INTEGER(result.m_Info) = CV_INTEGER(m_Info) + CV_INTEGER(entry.m_Info) ;
break;
default:
result.m_Info = m_Info + entry.m_Info;
}
return result;
}
StackEntry operator - (StackEntry & entry)
{
StackEntry result;
int ires;
result.m_nType = max(m_nType, entry.m_nType);
switch (result.m_nType)
{
case SE_REAL:
CV_REAL(result.m_Info) = CV_REAL(m_Info) - CV_REAL(entry.m_Info) ;
break;
case SE_CHAR:
CV_CHAR(result.m_Info) = CV_CHAR(m_Info) - CV_CHAR(entry.m_Info) ;
break;
case SE_INTEGER:
ires = CV_INTEGER(m_Info) - CV_INTEGER(entry.m_Info) ;
CV_INTEGER(result.m_Info) = ires;
break;
default:
result.m_Info = m_Info - entry.m_Info;
}
return result;
}
StackEntry operator * (StackEntry & entry)
{
StackEntry result;
result.m_nType = max(m_nType, entry.m_nType);
switch (result.m_nType)
{
case SE_REAL:
CV_REAL(result.m_Info) = CV_REAL(m_Info) * CV_REAL(entry.m_Info) ;
break;
case SE_CHAR:
CV_CHAR(result.m_Info) = CV_CHAR(m_Info) * CV_CHAR(entry.m_Info) ;
break;
case SE_INTEGER:
CV_INTEGER(result.m_Info) = CV_INTEGER(m_Info) * CV_INTEGER(entry.m_Info) ;
break;
default:
result.m_Info = m_Info * entry.m_Info;
}
return result;
}
StackEntry operator / (StackEntry & entry)
{
StackEntry result;
result.m_nType = max(m_nType, entry.m_nType);
switch (result.m_nType)
{
case SE_REAL:
CV_REAL(result.m_Info) = CV_REAL(m_Info) / CV_REAL(entry.m_Info) ;
break;
case SE_CHAR:
CV_CHAR(result.m_Info) = CV_CHAR(m_Info) / CV_CHAR(entry.m_Info) ;
break;
case SE_INTEGER:
CV_INTEGER(result.m_Info) = CV_INTEGER(m_Info) / CV_INTEGER(entry.m_Info) ;
break;
default:
result.m_Info = m_Info - entry.m_Info;
}
return result;
}
StackEntry operator % (StackEntry & entry)
{
StackEntry result;
result.m_nType = max(m_nType, entry.m_nType);
switch (m_nType)
{
case SE_CHAR:
CV_CHAR(result.m_Info) = CV_CHAR(m_Info) % CV_CHAR(entry.m_Info) ;
break;
case SE_INTEGER:
CV_INTEGER(result.m_Info) = CV_INTEGER(m_Info) % CV_INTEGER(entry.m_Info) ;
break;
default:
result.m_Info = m_Info % entry.m_Info;
}
return result;
}
};
class CVirtualMachine;
// the instruction type function
typedef void (CVirtualMachine::*MachineInstruction)(void);
///////////////////////////////////////////////////////////////
// Programmer : Zoly Farkas
// Creation Date : 5/25/99 1:11:38 PM
// Class name : CVirtualMachine
// Parents : none
// Description : This class is a representation of the
// : virtual machine
///////////////////////////////////////////////////////////////
class CVirtualMachine
{
public:
void LoadMemoryContent(REGISTER addrTo, BYTE* from,int count);
CVirtualMachine(int memSize = 16000);
virtual ~CVirtualMachine();
REGISTER Start(REGISTER addr, REGISTER sp);
private:
void NFJP();
void USTO(void);
void POP();
void PUSH();
void LODEA();
void LODAX();
void LODA();
void RSTSFP();
void CHGSFP(void);
void LEAVE(void);
void ENTER(void);
// Instructions
void NOP();
void OR();
void AND();
void OUTP();
void INP();
void ERR();
void STOP();
void RET();
void CALL();
void MOD();
void DIV();
void MUL();
void SUB();
void ADD(void);
void NEQ(void);
void EQU(void);
void GEQ(void);
void GRT(void);
void LEQ(void);
void LES(void);
void FJP(void);
void UJP(void);
void RED(void);
void MVRX(void);
void STO(void);
void COPY(void);
void LODIX(void);
void LODI(void);
void LOD();
// Central unit's components
REGISTER m_SP; // Stack Pointer
REGISTER m_FP; // Frame Pointer, for addressing Frames
REGISTER m_CFP; // Curent Frame pointer
REGISTER m_NI; // Program counter
REGISTER m_ST; // Status Register
REGISTER m_RX; // Index Register
REGISTER m_IE; // Error Register
// Storage Registers, for stack entryes, use push&pop the modify registers
StackEntry m_SREG[10];
// The Memory
BYTE *m_pbMem;
int m_nMemSize;
// Trap Flag
BOOL m_bTrap;
// The execution part of the programm
MachineInstruction m_Execute[INSTR_NUMBER];
public:
int LoadMemoryContent(REGISTER addrTo, CFile *from, int count);
BYTE * GetMem();
REGISTER Continue();
BOOL SetTrap(BOOL value)
{ return m_bTrap = value;}
static CString m_stInstNames[INSTR_NUMBER];
};
#endif // !defined(AFX_VIRTUALMACHINE_H__9DB59720_032D_11D3_BAEF_005004230120__INCLUDED_)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -