]> git.cryptolib.org Git - avr-crypto-lib.git/blob - performance_test.c
insereated GPLv3 stub
[avr-crypto-lib.git] / performance_test.c
1 /* performance_test.c */
2 /*
3     This file is part of the Crypto-avr-lib/microcrypt-lib.
4     Copyright (C) 2008  Daniel Otte (daniel.otte@rub.de)
5
6     This program is free software: you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation, either version 3 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 /*
20  * author: Daniel Otte
21  * email:  daniel.otte@rub.de
22  * license: GPLv3
23  * 
24  * 
25  **/
26  
27 #include "config.h"
28 #include <string.h>
29 #include <stdint.h>
30 #include <stdlib.h>
31 #include <avr/io.h>
32 #include <avr/interrupt.h>
33 #include <avr/pgmspace.h>
34 #include "uart.h"
35 #include "performance_test.h"
36
37
38 #ifdef ATMEGA644
39         #define TIMSK TIMSK1
40 #endif
41
42
43
44 uint32_t ovfcounter;
45
46 uint16_t const_overhead=0;
47 uint16_t int_overhead=0;
48
49 ISR(TIMER1_OVF_vect){
50         ovfcounter++;
51 }
52
53 void calibrateTimer(void){
54         startTimer(1);
55         stopTimer();
56         const_overhead = TCNT1;
57         startTimer(1);
58         TCNT1=0xFFFE;
59         ; ; ; ;
60 //      asm volatile("NOP\n"::); asm volatile("NOP\n"::);
61         stopTimer();
62         int_overhead = TCNT1;
63 }
64
65 void startTimer(uint8_t granularity){
66         TCCR1B = 0; /* stop timer */
67         TCNT1  = 0;
68         ovfcounter = 0;
69         TCCR1A = 0x00;
70         TIMSK &= 0xC3;
71         TIMSK |= _BV(TOIE1); /* enable TOIE1 */
72         TCCR1B = granularity & 0x7;     /* start timer */
73 }
74
75 uint64_t stopTimer(void){
76         TCCR1B = 0; /* stop timer */
77         uint64_t ret;
78         ret = (ovfcounter<<16) | TCNT1;
79         ret -= const_overhead;
80         ret -= ovfcounter * int_overhead;
81         return ret;
82 }
83
84 void getOverhead(uint16_t* constoh, uint16_t* intoh){
85         *constoh = const_overhead;
86         *intoh   = int_overhead; 
87 }
88
89 void print_time_P(PGM_P s, uint64_t t){
90         char sv[16];
91         uint8_t c;
92         uart_putstr_P(PSTR("\r\n"));
93         uart_putstr_P(s);
94         ultoa((unsigned long)t, sv, 10);
95         for(c=strlen(sv); c<11; ++c){
96                 uart_putc(' ');
97         }
98         uart_putstr(sv);
99 }
100
101 void print_overhead(void){
102         char str[16];
103         uart_putstr_P(PSTR("\r\n\r\n=== benchmark ==="));
104         utoa(const_overhead, str, 10);
105         uart_putstr_P(PSTR("\r\n\tconst overhead:     "));
106         uart_putstr(str);
107         utoa(int_overhead, str, 10);
108         uart_putstr_P(PSTR("\r\n\tinterrupt overhead: "));
109         uart_putstr(str);
110 }
111
112