Greg Hewgill ([info]ghewgill) wrote,
@ 2009-05-16 12:56:00
Previous Entry  Add to memories!  Tell a Friend  Next Entry
emulino: arduino cpu emulator

About a week ago I got an Arduino board for some project ideas. Naturally, one of the things I looked for was a free, cross-platform emulator that could run AVR code on a desktop machine. I only found one on Sourceforge which was started a few months ago but development seems to have stalled. So I decided to write my own. How hard could it be?

The AVR CPU is a pretty simple 8-bit microcontroller. Instruction words are all 16 bits wide, and only a handful of instructions have an additional 16 bit data word following the instruction. Given the instruction set reference, I wrote a script to help create a big 64k table of function pointers, one for each possible instruction. The main loop looks something like this:

for (;;) {
    u16 instr = Program[PC++];
    Instr[instr](instr);
}

This loads the next instruction word into instr. Then that value is used as an index into the Instr function pointer table, and calls the function at that index with the instruction word as an argument. The instruction implementation function can then pull apart the instruction word if it needs to. For example, this is the implementation of the LDI instruction, which loads a constant value into a register:

static void do_LDI(u16 instr)
{
    // ----KKKKddddKKKK
    u16 K = (instr & 0xf) | ((instr >> 4) & 0xf0);
    u16 d = 16 + ((instr >> 4) & 0xf);
    Data.Reg[d] = K;
    Cycle++;
}

It turns out there are only 93 distinct instruction words that need to be implemented. I've got 80 of the important ones working, plus some basic peripherals like the USART for serial communications, EEPROM storage, and the simplest timer that could possibly work. With that, many of the sample programs that come with the Arduino IDE run perfectly, plus it can run simple Java code using NanoVM!

I have a long list of ideas to build around this emulator core, including:

  • More useful built-in peripheral implementations
  • Cycle-correct emulation including interrupt handling
  • Interactive AVR code debugger
  • Integration with the Arduino IDE as a separate run target (instead of avrdude)
  • A GUI framework around the emulator with a full-noise visual debugger
  • Drag and drop circuit components (LEDs, switches, displays, etc)

I called the emulator emulino, which in addition to being a combination of emulator and Arduino, also happens to be an obscure conjugation (third person plural present subjunctive) of the Italian verb emulare, "to emulate".

You can find the current code on Github in the emulino project.



(6 comments) - (Post a new comment)


[info]goulo
2009-05-16 05:55 am UTC (link)
What? No mention of the Esperanto meaning of "emulino"? I'd go loosely with "a willing woman". :)

(Reply to this) (Thread)


[info]ghewgill
2009-05-16 07:15 am UTC (link)
Good point, the thought had occurred to me but I'd sort of forgotten that you can form more-or-less complete words using only suffixes. Dankon por tio, tio povas esti la "enŝerco". :)

(Reply to this) (Parent)


[info]pne
2009-05-16 04:51 pm UTC (link)
I once saw "emigisto" glossed as "animateur" (or whatever you call the chap on cruise ships or holiday camps who tries to make sure people are having fun, or are at least occupied).

(Reply to this) (Parent)


[info]paradox0220
2009-05-18 03:31 pm UTC (link)
I've worked with Xilinx FPGAs at work and LOVE those things. They have everything from tiny FPGAs to almost whole computer systems in a chip depending on what you want.

(Reply to this)

Nice work, dude!
(Anonymous)
2009-06-12 06:43 pm UTC (link)
Nice work! I too wrote a simple simulator for the 8051 core and that was way back in 2002 when I was still learning to program, It can be downloaded from http://sf.net/gsim51.

(Reply to this)

Error while compiling
[info]andreacollet.wordpress.com
2009-12-20 02:25 pm UTC (link)
Hi Greg and thank you very much for sharing your work.
I found Emulino while looking for an Arduino emulator to make some safe exercises. I've got just a little knowloedge of programming, probably not enough to make the code work since I'm unable to compile it correctly under Windows XP (32bit).
After messing a while with gcc + Cygwin (which returned a lot of errors) I tried loading and compiling the "emulino.pro" file with the Nokia QT SDK (http://qt.nokia.com/downloads) which returns 5 errors totally, the first is: "cpu.c:1169: error: avr.inc: No such file or directory".
Hoping to compile the project with the right tool, can you tell where I can retrieve the avr.inc file, please? Or else, would you suggest a better tool to compile the project?
Many thanks

Andrea

(Reply to this)


(6 comments) - (Post a new comment)

Create an Account
Forgot your login or password?
Login w/ OpenID
English • Español • Deutsch • Русский…