Permalink
Browse files

Clifford Wolf:

	Further improved examples/musicplayer
	Added an AVR speed estimation to README file



git-svn-id: http://svn.clifford.at/embedvm/trunk@39 9d21f824-f436-4d2e-9c88-bb158a3a422f
  • Loading branch information...
1 parent 95ec4fa commit 74aa3c0ff478ee6dfb6079ea3be83691444824e2 clifford committed Apr 5, 2011
Showing with 34 additions and 9 deletions.
  1. +3 −1 README
  2. +31 −8 examples/musicplayer/firmware.cc
View
4 README
@@ -18,7 +18,9 @@ are no additional memory requirements for the VM. Especially the VM does not
depend on any dymaic memory management.
EmbedVM is optimized for size and simplicity, not execution speed. The VM
-itself takes up about 3kB of program memory on an AVR microcontroller.
+itself takes up about 3kB of program memory on an AVR microcontroller. On an
+AVR ATmega168 running at 16MHz the VM can execute about 75 VM instructions
+per millisecond.
All memory accesses done by the VM are parformed using user callback functions.
So it is possible to have some or all of the VM memory on external memory
@@ -8,6 +8,7 @@
struct embedvm_s vm = { };
uint8_t vm_mem[256] = { EMBEDVM_SECT_SRAM_DATA };
+uint16_t vm_counter, vm_millis;
struct tone_s {
uint16_t freq, on, off;
@@ -18,20 +19,28 @@ uint8_t tone_buf_out, tone_buf_fill;
uint16_t last_freq, last_time;
bool tone_buf_empty_silent;
-void buzzer(uint16_t freq)
+void buzzer_setup()
{
- if (freq == last_freq)
- return;
-
- TCCR1A = (freq > 0 ? _BV(COM1A0) : 0);
+ TCCR1A = 0;
TCCR1B = _BV(WGM12) | _BV(CS11) | _BV(CS10);
TCCR1C = 0;
- OCR1A = freq > 0 ? 0xffff / (freq >> 1) : 0;
+ OCR1A = 0;
OCR1B = 0;
ICR1 = 0;
TIMSK1 = 0;
TIFR1 = 0;
+ TCNT1 = 0;
+}
+
+void buzzer(uint16_t freq)
+{
+ if (freq == last_freq)
+ return;
+
+ TCCR1A = (freq > 0 ? _BV(COM1A0) : 0);
+ OCR1A = freq > 0 ? 0xffff / (freq >> 1) : 0;
+ TCNT1 = 0;
last_freq = freq;
}
@@ -63,7 +72,7 @@ int16_t vm_call_user(uint8_t funcid, uint8_t argc, int16_t *argv, void *ctx)
uint8_t idx = (tone_buf_out+tone_buf_fill) % 4;
tone_buf[idx].freq = argv[0];
tone_buf[idx].on = argv[1];
- tone_buf[idx].off = argv[1] + argv[2];
+ tone_buf[idx].off = argv[2];
tone_buf_fill++;
}
}
@@ -81,8 +90,12 @@ void setup()
vm.call_user = &vm_call_user;
embedvm_interrupt(&vm, EMBEDVM_SYM_main);
+ vm_counter = 0;
+ vm_millis = 0;
+
pinMode(9, OUTPUT);
digitalWrite(9, LOW);
+ buzzer_setup();
buzzer(0);
tone_buf_empty_silent = true;
@@ -116,10 +129,20 @@ void setup()
}
tone_buf_empty_silent = false;
}
- if (vm.ip != 0xffff && tone_buf_fill < 4)
+ if (vm.ip != 0xffff && tone_buf_fill < 4) {
+ uint16_t time_start = millis();
embedvm_exec(&vm);
+ vm_millis += millis() - time_start;
+ vm_counter++;
+ }
}
+ Serial.print("Executed ");
+ Serial.print(vm_counter, DEC);
+ Serial.print(" VM instructions in ");
+ Serial.print(vm_millis, DEC);
+ Serial.println("ms CPU time.");
+
Serial.println("Have a nice day!");
}

0 comments on commit 74aa3c0

Please sign in to comment.