Online AVR simulator

Recently the micro-controller course at my university started. We learn programming assembler for the ATMega 1280 and use a commercial development-board. Because I wanted to work from home, I came up with an idea.

What if we compile an AVR simulator with Emscripten so everybody can easily access it from the web?

Emscripten is a C to JavaScript compiler that can be used to turn PC applications into Web applications.

Turns out that this was quiet doable and so simavr.js was born.

simavr.js can be found here: http://wq.lc/avr

It provides access to all IO ports of the AVR with simulated LEDs and buttons and should support most of the features of the chip.

Currently only .hex file loading is implemented for the flash memory. The EEPROM is simulated internally and can be used but data preloading support is missing.

In the near future there will be support for emulating other peripherals such as text LCDs and the UART. I’m also planning to implement an online logic-analyzer which can be used to inspect signals without having to actually build a board and probe it. Support for multiple MCU models will also be added soon.

My source code is on Github.

https://github.com/donothingloop/simavr_emscripten
https://github.com/donothingloop/simavr.js

I have provided some test files. These were generated by compiling some small c programs.

This example uses the timer1 to blink all avaliable LEDs:

#include <avr/interrupt.h>
#include <avr/io.h>

int main(void) {
 volatile uint8_t *ddr = &DDRA;
 int i;

 for(i = 0; i < 10; i++) {
 *(ddr) = 0xFF;
 ddr += 0x03;
 }

 TCCR1A |= (1<<WGM11);
 TCNT1 = 0;
 TIMSK1 |= (1<<TOIE1);
 sei();
 OCR1AH = 0x04;
 OCR1AL = 0xE2;
 TCCR1B |= (1<<CS12) | (1<<CS10);

 for(;;) {}
}

ISR(TIMER1_OVF_vect) {
 volatile uint8_t *port = &PORTA;
 int i;
 for(i = 0; i < 10; i++) {
 *(port) = ~(*port);
 port += 0x03;
 }
}

Save this to blink.c

To compile it first install the required tools to cross-compile the code. On Ubuntu you can enter the following command to install them:

sudo apt-get install avr-libc gcc-avr binutils-avr

After the installation is completed, the code can be compiled:
avr-gcc -mmcu=atmega1280 blink.c -o blink.elf -O3

Now the resulting elf file has to be converted to an hex file:

avr-objcopy -j .text -j .data -O ihex blink.elf blink.hex

Open http://wq.lc/avr/ (link opens in new tab) and click on “Open .hex file”. Select the blink.hex from your PC.

The firmware should now be running and you should see the LEDs of PORTA to PORTG blinking.

 

If you are somehow unable to compile this or just want to test the simulator, a compiled copy is available at http://wq.lc/avr/blink.hex