]> git.cryptolib.org Git - avr-crypto-lib.git/blob - performance_test.c
44b5b7be26b4db9183ea5f0962d0a8e81c74184d
[avr-crypto-lib.git] / performance_test.c
1 /*
2  * author: Daniel Otte
3  * email:  daniel.otte@rub.de
4  * license: GPLv3
5  * 
6  * 
7  **/
8  
9 #include "config.h"
10 #include <string.h>
11 #include <stdint.h>
12 #include <stdlib.h>
13 #include <avr/io.h>
14 #include <avr/interrupt.h>
15 #include <avr/pgmspace.h>
16 #include "uart.h"
17 #include "performance_test.h"
18
19
20 #ifdef ATMEGA644
21         #define TIMSK TIMSK1
22 #endif
23
24
25
26 uint32_t ovfcounter;
27
28 uint16_t const_overhead=0;
29 uint16_t int_overhead=0;
30
31 ISR(TIMER1_OVF_vect){
32         ovfcounter++;
33 }
34
35 void calibrateTimer(void){
36         startTimer(1);
37         stopTimer();
38         const_overhead = TCNT1;
39         startTimer(1);
40         TCNT1=0xFFFE;
41         ; ; ; ;
42 //      asm volatile("NOP\n"::); asm volatile("NOP\n"::);
43         stopTimer();
44         int_overhead = TCNT1;
45 }
46
47 void startTimer(uint8_t granularity){
48         TCCR1B = 0; /* stop timer */
49         TCNT1  = 0;
50         ovfcounter = 0;
51         TCCR1A = 0x00;
52         TIMSK &= 0xC3;
53         TIMSK |= _BV(TOIE1); /* enable TOIE1 */
54         TCCR1B = granularity & 0x7;     /* start timer */
55 }
56
57 uint64_t stopTimer(void){
58         TCCR1B = 0; /* stop timer */
59         uint64_t ret;
60         ret = (ovfcounter<<16) | TCNT1;
61         ret -= const_overhead;
62         ret -= ovfcounter * int_overhead;
63         return ret;
64 }
65
66 void getOverhead(uint16_t* constoh, uint16_t* intoh){
67         *constoh = const_overhead;
68         *intoh   = int_overhead; 
69 }
70
71 void print_time_P(PGM_P s, uint64_t t){
72         char sv[16];
73         uint8_t c;
74         uart_putstr_P(PSTR("\r\n"));
75         uart_putstr_P(s);
76         ultoa((unsigned long)t, sv, 10);
77         for(c=strlen(sv); c<11; ++c){
78                 uart_putc(' ');
79         }
80         uart_putstr(sv);
81 }
82
83 void print_overhead(void){
84         char str[16];
85         uart_putstr_P(PSTR("\r\n\r\n=== benchmark ==="));
86         utoa(const_overhead, str, 10);
87         uart_putstr_P(PSTR("\r\n\tconst overhead:     "));
88         uart_putstr(str);
89         utoa(int_overhead, str, 10);
90         uart_putstr_P(PSTR("\r\n\tinterrupt overhead: "));
91         uart_putstr(str);
92 }
93
94