]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/performance_test.c
fixing E-Mail-Address & Copyright
[avr-crypto-lib.git] / test_src / performance_test.c
1 /* performance_test.c */
2 /*
3     This file is part of the AVR-Crypto-Lib.
4     Copyright (C) 2006-2015 Daniel Otte (bg@nerilex.org)
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:  bg@nerilex.org
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 <stdio.h>
35 #include "performance_test.h"
36
37
38 #ifdef ATMEGA644
39         #define TIMSK TIMSK1
40 #endif
41
42
43
44 static volatile uint32_t ovfcounter;
45
46 static uint16_t const_overhead = 0;
47 static 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         asm("nop");
60         stopTimer();
61         int_overhead = TCNT1;
62 }
63
64 void startTimer(uint8_t granularity){
65         TCCR1B = 0; /* stop timer */
66         TCNT1  = 0;
67         ovfcounter = 0;
68         TCCR1A = 0x00;
69         TIMSK  = _BV(TOIE1); /* enable TOIE1 */
70         TCCR1B = granularity & 0x7;     /* start timer */
71 }
72
73 uint64_t stopTimer(void){
74         TCCR1B = 0; /* stop timer */
75         uint64_t ret;
76         ret = (((uint64_t)ovfcounter)<<16) | TCNT1;
77         ret -= const_overhead;
78         ret -= ovfcounter * int_overhead;
79         return ret;
80 }
81
82 void getOverhead(uint16_t *constoh, uint16_t *intoh){
83         *constoh = const_overhead;
84         *intoh   = int_overhead;
85 }
86
87 void print_time_P(PGM_P s, uint32_t t){
88         printf_P(PSTR("%S%11"PRIu32), t);
89 }
90
91 void print_overhead(void){
92         printf_P(PSTR("\n=== benchmark ===\n\tconst overhead:     %7"PRIu16"\n\tinterrupt overhead: %7"PRIu16"\n"), const_overhead, int_overhead);
93 }
94
95