sim_timer.h Source File

yasimavr: sim_timer.h Source File
yasimavr
Loading...
Searching...
No Matches
sim_timer.h
Go to the documentation of this file.
1/*
2 * sim_timer.h
3 *
4 * Copyright 2021-2026 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_IO_TIMER_H__
25#define __YASIMAVR_IO_TIMER_H__
26
27#include "../core/sim_cycle_timer.h"
28#include "../core/sim_signal.h"
29#include "../core/sim_device.h"
30
32
33
34//=======================================================================================
35
37
64
65public:
66
68 virtual ~PrescaledTimer();
69
70 void init(CycleManager& cycle_manager, Logger& logger);
71
72 void reset();
73
74 void set_prescaler(unsigned long ps_max, unsigned long ps_factor);
75
76 unsigned long prescaler_factor() const;
77
78 void set_timer_delay(cycle_count_t delay);
79
80 cycle_count_t timer_delay() const;
81
82 void set_paused(bool paused);
83
84 void update();
85
86 virtual cycle_count_t next(cycle_count_t when) override;
87
88 Signal& signal();
89
90 void register_chained_timer(PrescaledTimer& timer);
91 void unregister_chained_timer(PrescaledTimer& timer);
92
93 static cycle_count_t ticks_to_event(cycle_count_t counter, cycle_count_t event, cycle_count_t wrap);
94
95 //Disable copy semantics
98
99private:
100
101 CycleManager* m_cycle_manager;
102 Logger* m_logger;
103
104 //***** Prescaler management *****
105 unsigned long m_ps_max; //Max value of the prescaler
106 unsigned long m_ps_factor; //Prescaler division factor (Tick period / Clock period)
107 cycle_count_t m_ps_counter; //Prescaler counter
108
109 //***** Delay management *****
110 cycle_count_t m_delay; //Delay until the next timeout in ticks
111
112 //***** Update management *****
113 bool m_paused; //Boolean indicating if the timer is paused
114 bool m_updating; //Boolean used to avoid infinite updating reentrance
115 cycle_count_t m_update_cycle; //Cycle number of the last update
116 Signal m_signal; //Signal raised for processing ticks
117
118 //***** Timer chain management *****
119 std::vector<PrescaledTimer*> m_chained_timers;
120 PrescaledTimer* m_parent_timer;
121
122 void reschedule();
123 void update(cycle_count_t when);
124 void update_timer(cycle_count_t when);
125 void process_cycles(cycle_count_t cycles);
126
127 cycle_count_t calculate_when(cycle_count_t when);
128 cycle_count_t calculate_delay();
129 cycle_count_t convert_ticks_to_cycles(cycle_count_t ticks);
130
131};
132
134inline unsigned long PrescaledTimer::prescaler_factor() const
135{
136 return m_ps_factor;
137}
138
141{
142 return m_delay;
143}
144
147{
148 return m_signal;
149}
150
151
152//=======================================================================================
165
166public:
167
171 Tick_Stopped = 0,
176 };
177
181 Slope_Up = 0,
186 };
187
191 Event_Max = 0x01,
193 Event_Top = 0x02,
195 Event_Bottom = 0x04,
197 Event_Compare = 0x08
198 };
199
213
214 TimerCounter(long wrap, size_t comp_count);
215
216 void init(CycleManager& cycle_manager, Logger& logger);
217
218 void reset();
219 void reschedule();
220 void update();
221
222 long wrap() const;
223
224 void set_tick_source(TickSource src);
225 TickSource tick_source() const;
226
227 void tick();
228
229 void set_top(long top);
230 long top() const;
231
232 void set_slope_mode(SlopeMode mode);
233 SlopeMode slope_mode() const;
234
235 void set_counter(long value);
236 long counter() const;
237
238 void set_comp_value(size_t index, long value);
239 long comp_value(size_t index) const;
240
241 void set_comp_enabled(size_t index, bool enable);
242 bool comp_enabled(size_t index) const;
243
244 bool countdown() const;
245 void set_countdown(bool down);
246
247 Signal& signal();
248 SignalHook& ext_tick_hook();
249
250 PrescaledTimer& prescaler();
251
252 //no copy semantics
253 //TimerCounter(const TimerCounter&) = delete;
254 //TimerCounter& operator=(const TimerCounter&) = delete;
255
256private:
257
258 struct CompareUnit {
259 long value = 0;
260 bool enabled = false;
261 bool is_next_event = false;
262 };
263
264 //Selected tick source
265 TickSource m_source;
266 //Counter wrap value
267 long m_wrap;
268 //Current counter value
269 long m_counter;
270 //Top value
271 long m_top;
272 //Slope mode
273 SlopeMode m_slope;
274 //Indicates if the counter is counting up (false) or down (true)
275 bool m_countdown;
276 //List of compare units
277 std::vector<CompareUnit> m_cmp;
278 //Event timer engine
279 PrescaledTimer m_timer;
280 //Flag variable storing the next event type(s)
281 uint8_t m_next_event_type;
282 //Signal management
283 DataSignal m_signal;
286 //Logging
287 Logger* m_logger;
288
289 long delay_to_event();
290 void timer_raised(const signal_data_t& sigdata, int);
291 void extclock_raised(const signal_data_t&, int);
292 void add_tick();
293 long ticks_to_event(long event);
294 void process_ticks(long ticks, bool event_reached);
295
296};
297
299inline long TimerCounter::wrap() const
300{
301 return m_wrap;
302}
303
304
307{
308 m_timer.update();
309}
310
311
314{
315 return m_source;
316}
317
319inline long TimerCounter::top() const
320{
321 return m_top;
322}
323
326{
327 return m_slope;
328}
329
331inline long TimerCounter::counter() const
332{
333 return m_counter;
334}
335
337inline long TimerCounter::comp_value(size_t index) const
338{
339 return m_cmp[index].value;
340}
341
343inline bool TimerCounter::comp_enabled(size_t index) const
344{
345 return m_cmp[index].enabled;
346}
347
349inline bool TimerCounter::countdown() const
350{
351 return m_countdown;
352}
353
356{
357 return m_signal;
358}
359
362{
363 return m_ext_hook;
364}
365
368{
369 return m_timer;
370}
371
372
374
375#endif //__YASIMAVR_IO_TIMER_H__
Definition sim_signal.h:227
Definition sim_cycle_timer.h:134
Definition sim_cycle_timer.h:41
virtual cycle_count_t next(cycle_count_t when)=0
Callback from the cycle loop.
Definition sim_signal.h:137
Definition sim_logger.h:91
Generic model of a Timer with prescaling.
Definition sim_timer.h:63
void update()
Definition sim_timer.cpp:152
unsigned long prescaler_factor() const
Getter for ps_factor.
Definition sim_timer.h:134
PrescaledTimer(const PrescaledTimer &)=delete
PrescaledTimer & operator=(const PrescaledTimer &)=delete
Signal & signal()
Getter for the signal raised with counter updates.
Definition sim_timer.h:146
cycle_count_t timer_delay() const
Getter for timer_delay.
Definition sim_timer.h:140
Definition sim_signal.h:55
Signalling framework class.
Definition sim_signal.h:97
Generic model of a Counter.
Definition sim_timer.h:164
long counter() const
Getter for the current counter value.
Definition sim_timer.h:331
TickSource
Tick source mode.
Definition sim_timer.h:169
@ Tick_External
External signal hook used as tick source.
Definition sim_timer.h:175
@ Tick_Timer
Internal prescaled timer used as tick source.
Definition sim_timer.h:173
Signal & signal()
Getter for the counting signal.
Definition sim_timer.h:355
bool countdown() const
Getter for the current counting direction.
Definition sim_timer.h:349
SignalHook & ext_tick_hook()
Getter for the external signal hook used for tick source.
Definition sim_timer.h:361
PrescaledTimer & prescaler()
Getter for the internal prescaler.
Definition sim_timer.h:367
SignalId
Signal Ids raised by this object.
Definition sim_timer.h:201
@ Signal_CompMatch
Definition sim_timer.h:211
@ Signal_Event
Definition sim_timer.h:206
void update()
Force update of the internal prescaler.
Definition sim_timer.h:306
long comp_value(size_t index) const
Getter for a compare value.
Definition sim_timer.h:337
long wrap() const
Getter for the wrapping value.
Definition sim_timer.h:299
long top() const
Getter for the TOP value.
Definition sim_timer.h:319
TickSource tick_source() const
Getter for the tick source mode.
Definition sim_timer.h:313
bool comp_enabled(size_t index) const
Getter for a compare value enable.
Definition sim_timer.h:343
SlopeMode slope_mode() const
Getter for the slope mode.
Definition sim_timer.h:325
SlopeMode
Counter direction mode.
Definition sim_timer.h:179
@ Slope_Down
Down-counting.
Definition sim_timer.h:183
@ Slope_Double
Dual-slope counting.
Definition sim_timer.h:185
EventType
Event type flags used when signaling.
Definition sim_timer.h:189
#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_signal.h:39