views:

531

answers:

2

Hi SO Friends,

I am working on ARM 9 processor with 266 Mhz with fpu support and 32 MB RAM, I run linux on it.I want to emulate it on pc ( I have both linux and windows availabe on pc ). I want to profile my cycle counts, run my cross-compiled executables directly in emulator. Is there any opensource project available to create emulator easily, How much change/code/effort does I need to write to make custom emulator with it ? It would be great if you provide me tutorials ot other reference to get kick-start.

Thanks & Regards,

Sunny.

+4  A: 

Do you want to emulate just the processor or an entire machine?

Emulate a CPU is very easy, just define a structure containing all CPU registers, create an array to simulate RAM and then just emulate like this:

    cpu_ticks = 0;  // counter for cpu cycles 

    while (true) {

      opcode = RAM[CPU.PC++]; // Fetch opcode and increment program counter

      switch (opcode) {

        case 0x12: // invented opcode for "MOV A,B"
          CPU.A = CPU.B;
          cpu_ticks += 4; // imagine you need 4 ticks for this operation
          set_cpu_flags_mov();
          break;

        case 0x23: // invented opcode for "ADD A, #"
          CPU.A += RAM[CPU. PC++]; // get operand from memory
          cpu_ticks += 8;
          set_cpu_flags_add();
          break;

        case 0x45: // invented opcode for "JP Z, #"
          if (CPU.FLAGS.Z) CPU.PC=RAM[CPU.PC++]; // jump
          else CPU.PC++; // continue
          cpu_ticks += 12;
          set_cpu_flags_jump();
          break;
        ...
      }

      handle_interrupts();

    }

Emulate an entire machine is much much harder... you need to emulate LCD controllers, memory mapped registers, memory banks controllers, DMAs, input devices, sound, I/O stuff... also probably you need a dump from the bios and operative system... I don't know the ARM processor but if it has pipelines, caches and such things, things get more complicated for timing.

If you have all hardware parts fully documented, there's no problem but if you need to reverse engineer or guess how the emulated machine works... you will have a hard time.

Start here: http://infocenter.arm.com/help/index.jsp and download the "Technical Reference Manual" for your processor.

And for general emulation questions: http://www.google.es/search?q=how+to+write+an+emulator

Pedro Ladaria
Well, i want to emulate entire machine. Thanks for detailed answer.
Sunny
I've done a Nintendo Gameboy emulator in javascript, code is very simple and easy, perhaps it helps you understanding how a emulator works. http://www.codebase.es That version is a little outdated, in a few days I'm going to upload new version with lots of bugfixes and improved compatibility.
Pedro Ladaria
While this approach is essentially correct, it's important to note that this is an inherently inaccurate emulation. It serializes things that would not be serialized in a real processor. For example opcodes are not atomic in ARM 9, they're done over it's 5 stage pipeline, thus an interrupt could occur at any stage in the pipeline.Also writing an emulator for a modern CPU is definitely a non-trivial task, so I'd advice against it for production code (but it could be a good learning experience).
Falaina
Additionally if you are set on writing your own ARM emulator, check to see if any of the Nintendo DS emulators out now are open source. If I remember correct the DS has an ARM9 in it.
Falaina
Yes, ARM9 and ARM7.
Joren
+1  A: 

You should give a look at QEMU. I don't understand however, why do you need a complete emulator ?

You can already a lot of profiling without emulator. What are the gains you expect from having a system emulator ?

shodanex
my embedded device does not have any connectivity except serial connector, It is big pain to transfer code to it everytime. It takes 10 to 15 minutes to transfer code to device.
Sunny
can you please tell me that, how much effort does it require to make emulator with qemu?
Sunny