SimAVR
AVR Simulator
sim_time.h
Go to the documentation of this file.
1 /*
2  * sim_time.h
3  *
4  * Copyright 2008-2012 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 it under the terms of the GNU
9  * General Public License as published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * simavr is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
13  * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
14  * Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along with simavr. If not, see
17  * <http://www.gnu.org/licenses/>.
18  */
19 
28 #ifndef __SIM_TIME_H___
29 #define __SIM_TIME_H___
30 
31 #ifdef __cplusplus
32 extern "C"
33 {
34 #endif
35 
36 #include "sim_avr.h"
37 
39  static inline avr_cycle_count_t avr_usec_to_cycles (struct avr_t *avr, uint32_t usec)
40  {
41  return avr->frequency * (avr_cycle_count_t) usec / 1000000;
42  }
43 
45  static inline uint32_t avr_cycles_to_usec (struct avr_t *avr, avr_cycle_count_t cycles)
46  {
47  return 1000000L * cycles / avr->frequency;
48  }
49 
51  static inline uint64_t avr_cycles_to_nsec (struct avr_t *avr, avr_cycle_count_t cycles)
52  {
53  return (uint64_t) 1E6 * (uint64_t) cycles / (avr->frequency / 1000);
54  }
55 
57  static inline avr_cycle_count_t avr_hz_to_cycles (avr_t * avr, uint32_t hz)
58  {
59  return avr->frequency / hz; // #cycle = T / Tavr
60  }
61 
62 #ifdef __cplusplus
63 };
64 #endif
65 
66 #endif /* __SIM_TIME_H___ */
67 
uint32_t frequency
frequency we are running at
Definition: sim_avr.h:159
static uint64_t avr_cycles_to_nsec(struct avr_t *avr, avr_cycle_count_t cycles)
Converts back a number of cycles to nsecs.
Definition: sim_time.h:51
static avr_cycle_count_t avr_hz_to_cycles(avr_t *avr, uint32_t hz)
Converts a number of hz (to megahertz etc) to a number of cycle.
Definition: sim_time.h:57
static avr_cycle_count_t avr_usec_to_cycles(struct avr_t *avr, uint32_t usec)
Converts a number of usec to a number of machine cycles, at current speed.
Definition: sim_time.h:39
avr_t * avr
Definition: run_avr.c:54
static uint32_t avr_cycles_to_usec(struct avr_t *avr, avr_cycle_count_t cycles)
Converts back a number of cycles to usecs (for usleep)
Definition: sim_time.h:45
uint64_t avr_cycle_count_t
Definition: sim_avr_types.h:36
Main AVR instance.
Definition: sim_avr.h:142