vcd: change VCD time base to 1 ns to handle higher AVR speeds (e.g. 20 MHz / max...
[simavr] / simavr / sim / sim_cycle_timers.h
1 /*
2         sim_cycle_timers.h
3
4         Copyright 2008, 2009 Michel Pollet <buserror@gmail.com>
5
6         This file is part of simavr.
7
8         simavr is free software: you can redistribute it and/or modify
9         it under the terms of the GNU General Public License as published by
10         the Free Software Foundation, either version 3 of the License, or
11         (at your option) any later version.
12
13         simavr is distributed in the hope that it will be useful,
14         but WITHOUT ANY WARRANTY; without even the implied warranty of
15         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16         GNU General Public License for more details.
17
18         You should have received a copy of the GNU General Public License
19         along with simavr.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22
23 #ifndef __SIM_CYCLE_TIMERS_H___
24 #define __SIM_CYCLE_TIMERS_H___
25
26 #include "sim_avr.h"
27
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31
32 // converts a number of usec to a number of machine cycles, at current speed
33 static inline avr_cycle_count_t avr_usec_to_cycles(avr_t * avr, uint32_t usec)
34 {
35         return avr->frequency * (avr_cycle_count_t)usec / 1000000;
36 }
37
38 // converts back a number of cycles to usecs (for usleep)
39 static inline uint32_t avr_cycles_to_usec(avr_t * avr, avr_cycle_count_t cycles)
40 {
41         return 1000000L * cycles / avr->frequency;
42 }
43
44 // converts back a number of cycles to nsecs
45 static inline uint64_t avr_cycles_to_nsec(avr_t * avr, avr_cycle_count_t cycles)
46 {
47         return (uint64_t)1E6 * (uint64_t)cycles / (avr->frequency/1000);
48 }
49
50 // converts a number of hz (to megahertz etc) to a number of cycle
51 static inline avr_cycle_count_t avr_hz_to_cycles(avr_t * avr, uint32_t hz)
52 {
53         return avr->frequency / hz;
54 }
55
56 // register for calling 'timer' in 'when' cycles
57 void avr_cycle_timer_register(avr_t * avr, avr_cycle_count_t when, avr_cycle_timer_t timer, void * param);
58 // register a timer to call in 'when' usec
59 void avr_cycle_timer_register_usec(avr_t * avr, uint32_t when, avr_cycle_timer_t timer, void * param);
60 // cancel a previously set timer
61 void avr_cycle_timer_cancel(avr_t * avr, avr_cycle_timer_t timer, void * param);
62 /*
63  * Check to see if a timer is present, if so, return the number (+1) of
64  * cycles left for it to fire, and if not present, return zero
65  */
66 avr_cycle_count_t
67 avr_cycle_timer_status(avr_t * avr, avr_cycle_timer_t timer, void * param);
68
69
70 //
71 // Private, called from the core
72 //
73 avr_cycle_count_t avr_cycle_timer_process(avr_t * avr);
74
75 #ifdef __cplusplus
76 };
77 #endif
78
79 #endif /* __SIM_CYCLE_TIMERS_H___ */