Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
can any body tell me how to make a simulator for 8 bit in C++???
thnx in advance
Posted
Comments
BillW33 30-Aug-12 13:58pm    
First you need to specify the CPU being emulated and the format of the .asm
files.
Second, this sounds like a homework assignment.
enhzflep 30-Aug-12 14:22pm    
A simulator for an 8-bit what?
A DAC, ADC, CPU, MUX etc, etc. If CPU, what type? CISC, RISC?

Very broad question with no way of providing a meaningful answer (as the question is right now)
Legor 31-Aug-12 3:20am    
You need to provide more details if you want people to possibly help you.

1 solution

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:

C++
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
 
Share this answer
 
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900