如何在C ++中制作模拟器 [英] how to make simulator in C++

查看:247
本文介绍了如何在C ++中制作模拟器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能告诉我如何用C ++制作8位模拟器吗??
预先thnx

can any body tell me how to make a simulator for 8 bit in C++???
thnx in advance

推荐答案

假定您的CPU的指令集具有以下形式(其中A,B,D是各种形式的内存,相对于PC的内容,寄存器) ,持续访问等):
-Op
-作品A
-操作-> D
-Op A-> D
-Op A,B
-Op A,B-> D

伪代码:

Assuming you have a CPU with instruction set of the following form (where A, B, D are various forms of memory, pc-relative, register, constant access, etc.):
- Op
- Op A
- Op -> D
- Op A -> D
- Op A,B
- Op A,B -> D

Pseudo Code:

class CPUModel
{
   typedef unsigned long Register;
   typedef unsigned long Value;
   class Registers
   {
      Register PC;
      ...
   }
   class Operation
   {
      enum OpCode { ... }
      enum Kind { Reg, Rel, Abs, Const, ... }
      class Source
      {
         Value value;
         Kind kind;
         ...
         Value Get()
         {
             switch(kind)
             {
                case Const:
                   return value;
                case Reg:
                   return Cpu.Registers[value];
                case Rel:
                   return Sys.Memory[Cpu.Registers.PC + value];
                case Abs:
                   return Sys.Memory[value];
                ...
             }
         }
      }
      class Destination
      {
         Value value;
         Kind kind;
         ...
         void Set(Value v)
         {
             switch(kind)
             {
                case Reg:
                   Cpu.Registers[value] = v;
                   break;
                case Rel:
                   Sys.Memory[Cpu.Registers.PC + value] = v;
                   break;
                case Abs:
                   Sys.Memory[value] = v;
                   break;
                ...
             }
         }
      }
      ...
      Operation(Word w)
      {
          code = DecodeOp(w);
          src1 = Source.DecodeFirst(w);
          src2 = Source.DecodeSecond(w);
          dest = Destination.Decode(w);
      }
   }
   void Run(Address loc)
   {
       Registers.PC = loc;
       Halt = false;
       while (!Halt)
       {
           FetchDecodeAndExecute();
           sys.DumpSystemState();
       }
   }
   void FetchDecodeAndExecute()
   {
       Word w = Sys.Memory[Registers.PC];
       Operation op = new Operation(w);
       Execute(op);
   }
   void Execute(Operation op)
   {
       switch(op.code)
       {
          case Halt:
              Halt = true;
              break;
          case Add:
              op.dest.Set(op.src1.Get() + op.src2.Get());
              Registers.PC += op.Length();
              break;
          ...
          case Jump:
              Registers.PC = op.src1.Get();
              break;
          ...
       }
   }
}
class MemoryModel
{
   ...
}
class DeviceXModel
{
   ...
}
class System
{
    ...
    System()
    {
    }
    void Init()
    {
       Cpu = new CPUModel(this);
       Mem = new MemoryModel(this);
       DevX = new DeviceXModel(this);
       ...
    }
    void Run(Address loc)
    {
        Cpu.Run(loc);
    }
}
void Simulate(File image file, Addrsss loc)
{
    System sys = new System();
    sys.Init();
    sys.LoadImageIntoMemory(loc, imageFile);
    sys.Run(loc);
}



玩得开心!

干杯
安迪



Have Fun!

Cheers
Andi


这篇关于如何在C ++中制作模拟器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆