1 //
2 // Copyright (c) 2010-2022 Antmicro
3 // Copyright (c) 2011-2015 Realtime Embedded
4 //
5 // This file is licensed under the MIT License.
6 // Full license text is available in 'licenses/MIT.txt'.
7 //
8 using System;
9 using Antmicro.Renode.Core;
10 using Antmicro.Renode.Peripherals.Bus;
11 using Antmicro.Renode.Time;
12 using Antmicro.Renode.Logging;
13 using System.Threading;
14 
15 namespace Antmicro.Renode.Peripherals.Timers
16 {
17     public class SimpleTicker : IDoubleWordPeripheral
18     {
SimpleTicker(ulong periodInMs, IMachine machine)19         public SimpleTicker(ulong periodInMs, IMachine machine)
20         {
21             machine.ClockSource.AddClockEntry(new ClockEntry(periodInMs, 1000, OnTick, this, String.Empty));
22         }
23 
ReadDoubleWord(long offset)24         public virtual uint ReadDoubleWord(long offset)
25         {
26             return (uint)Interlocked.CompareExchange(ref counter, 0, 0);
27         }
28 
WriteDoubleWord(long offset, uint value)29         public virtual void WriteDoubleWord(long offset, uint value)
30         {
31             this.LogUnhandledWrite(offset, value);
32         }
33 
Reset()34         public virtual void Reset()
35         {
36             Interlocked.Exchange(ref counter, 0);
37         }
38 
OnTick()39         private void OnTick()
40         {
41             Interlocked.Increment(ref counter);
42         }
43 
44         private int counter;
45     }
46 }
47 
48