sim_cycle_timer.h Source File

yasimavr: sim_cycle_timer.h Source File
yasimavr
Loading...
Searching...
No Matches
sim_cycle_timer.h
Go to the documentation of this file.
1/*
2 * sim_cycle_timer.h
3 *
4 * Copyright 2021-2024 Clement Savergne <csavergne@yahoo.com>
5
6 This file is part of yasim-avr.
7
8 yasim-avr 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 yasim-avr 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 yasim-avr. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22//=======================================================================================
23
24#ifndef __YASIMAVR_CYCLE_TIMER_H__
25#define __YASIMAVR_CYCLE_TIMER_H__
26
27#include "sim_types.h"
28#include <deque>
29
31
32
33//=======================================================================================
34
35class CycleManager;
36
42
43public:
44
45 CycleTimer();
46 CycleTimer(const CycleTimer& other);
47 virtual ~CycleTimer();
48
50 inline bool scheduled() const { return !!m_manager; }
51
52 bool paused() const;
53
54 cycle_count_t remaining_delay() const;
55
70 virtual cycle_count_t next(cycle_count_t when) = 0;
71
72 CycleTimer& operator=(const CycleTimer& other);
73
74private:
75
76 friend class CycleManager;
77
79 CycleManager* m_manager;
80
81};
82
83
84template<class C>
86
87public:
88
90 using bound_noret_fct_t = void(C::*)(cycle_count_t);
91 using bound_noarg_fct_t = void(C::*)(void);
92
93 constexpr BoundFunctionCycleTimer(C& _c, bound_full_fct_t _f) : CycleTimer(), c(_c), m(Full), f_full(_f) {}
94 constexpr BoundFunctionCycleTimer(C& _c, bound_noret_fct_t _f) : CycleTimer(), c(_c), m(NoRet), f_noret(_f) {}
95 constexpr BoundFunctionCycleTimer(C& _c, bound_noarg_fct_t _f) : CycleTimer(), c(_c), m(NoArg), f_noarg(_f) {}
96
97 virtual cycle_count_t next(cycle_count_t when) override final
98 {
99 if (m == Full) {
100 return (c.*f_full)(when);
101 } else if (m == NoRet) {
102 (c.*f_noret)(when);
103 return 0;
104 } else {
105 (c.*f_noarg)();
106 return 0;
107 }
108 }
109
110private:
111
112 enum Mode { Full, NoRet, NoArg };
113
114 C& c;
115 const Mode m;
116
117 union {
121 };
122
123};
124
125
126//=======================================================================================
135
136public:
137
138 CycleManager();
140
141 cycle_count_t cycle() const;
142 void increment_cycle(cycle_count_t count);
143
144 void schedule(CycleTimer& timer, cycle_count_t when);
145
146 void delay(CycleTimer& timer, cycle_count_t d);
147
148 void cancel(CycleTimer& timer);
149
150 void pause(CycleTimer& timer);
151
152 void resume(CycleTimer& timer);
153
154 void process_timers();
155
156 cycle_count_t next_when() const;
157
158 CycleManager(const CycleManager&) = delete;
160
161private:
162
163 friend class CycleTimer;
164
165 //Structure holding information on a cycle timer when it's in the cycle queue
166 struct TimerSlot;
167 std::deque<TimerSlot*> m_timer_slots;
168 cycle_count_t m_cycle;
169
170 //Utility method to add a timer in the cycle queue, conserving the order or 'when'
171 //and paused timers last
172 void add_to_queue(TimerSlot* slot);
173
174 //Utility to remove a timer from the queue.
175 TimerSlot* pop_from_queue(CycleTimer& timer);
176
177 void copy_slot(const CycleTimer& src, CycleTimer& dst);
178
179 TimerSlot* get_slot(const CycleTimer& timer) const;
180
181};
182
185{
186 return m_cycle;
187}
188
189
191
192#endif //__YASIMAVR_CYCLE_TIMER_H__
Definition sim_cycle_timer.h:85
constexpr BoundFunctionCycleTimer(C &_c, bound_full_fct_t _f)
Definition sim_cycle_timer.h:93
virtual cycle_count_t next(cycle_count_t when) override final
Callback from the cycle loop.
Definition sim_cycle_timer.h:97
const bound_noret_fct_t f_noret
Definition sim_cycle_timer.h:119
cycle_count_t(C::*)(cycle_count_t) bound_full_fct_t
Definition sim_cycle_timer.h:89
constexpr BoundFunctionCycleTimer(C &_c, bound_noarg_fct_t _f)
Definition sim_cycle_timer.h:95
void(C::*)(void) bound_noarg_fct_t
Definition sim_cycle_timer.h:91
constexpr BoundFunctionCycleTimer(C &_c, bound_noret_fct_t _f)
Definition sim_cycle_timer.h:94
const bound_noarg_fct_t f_noarg
Definition sim_cycle_timer.h:120
const bound_full_fct_t f_full
Definition sim_cycle_timer.h:118
void(C::*)(cycle_count_t) bound_noret_fct_t
Definition sim_cycle_timer.h:90
Definition sim_cycle_timer.h:134
CycleManager & operator=(const CycleManager &)=delete
CycleManager(const CycleManager &)=delete
cycle_count_t cycle() const
Returns the current cycle.
Definition sim_cycle_timer.h:184
Definition sim_cycle_timer.h:41
bool scheduled() const
Returns true if this timer is scheduled with a manager.
Definition sim_cycle_timer.h:50
virtual cycle_count_t next(cycle_count_t when)=0
Callback from the cycle loop.
#define YASIMAVR_BEGIN_NAMESPACE
Definition sim_globals.h:58
#define AVR_CORE_PUBLIC_API
Definition sim_globals.h:46
#define YASIMAVR_END_NAMESPACE
Definition sim_globals.h:59
YASIMAVR_BEGIN_NAMESPACE typedef long long cycle_count_t
Definition sim_types.h:40
Definition sim_cycle_timer.cpp:31