1 //
2 // Copyright (c) 2010-2024 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.Logging;
10 using Antmicro.Renode.Peripherals;
11 using Antmicro.Renode.Utilities;
12 
13 namespace Antmicro.Renode.Time
14 {
15     public struct ClockEntry
16     {
ClockEntryAntmicro.Renode.Time.ClockEntry17         public ClockEntry(ulong period, long frequency, Action handler, IEmulationElement owner, string localName, bool enabled = true, Direction direction = Direction.Ascending, WorkMode workMode = WorkMode.Periodic, long step = 1) : this()
18         {
19             this.Value = direction == Direction.Ascending ? 0 : period;
20             this.Frequency = frequency;
21             this.Step = step;
22             this.Period = period;
23             this.Handler = handler;
24             this.Enabled = enabled;
25             this.Direction = direction;
26             this.WorkMode = workMode;
27             this.Owner = owner;
28             this.LocalName = localName;
29             this.Ratio = FrequencyToRatio(Step * Frequency);
30             this.ValueResiduum = Fraction.Zero;
31         }
32 
WithAntmicro.Renode.Time.ClockEntry33         public ClockEntry With(ulong? period = null, long? frequency = null, Action handler = null, bool? enabled = null,
34             ulong? value = null, Direction? direction = null, WorkMode? workMode = null, long? step = null)
35         {
36             var result = new ClockEntry(
37                 period ?? Period,
38                 frequency ?? Frequency,
39                 handler ?? Handler,
40                 Owner,
41                 LocalName,
42                 enabled ?? Enabled,
43                 direction ?? Direction,
44                 workMode ?? WorkMode,
45                 step ?? Step);
46 
47             result.Value = value ?? Value;
48             result.ValueResiduum = frequency != null ? Fraction.Zero : ValueResiduum;
49             return result;
50         }
51 
52         public ulong Value;
53         public Fraction ValueResiduum;
54 
55         public ulong Period { get; }
56         public Action Handler { get; }
57         public bool Enabled { get; }
58         public Direction Direction { get; }
59         public WorkMode WorkMode { get; }
60         public IEmulationElement Owner { get; }
61         public string LocalName { get; }
62         public long Step { get; }
63         public long Frequency { get; }
64         // Ratio - i.e. how many emulator ticks are needed for this clock entry tick
65         public Fraction Ratio { get; }
66 
FrequencyToRatioAntmicro.Renode.Time.ClockEntry67         private static Fraction FrequencyToRatio(long desiredFrequency)
68         {
69             var maxHz = TimeInterval.TicksPerSecond;
70             return new Fraction((ulong)desiredFrequency, maxHz);
71         }
72     }
73 }
74 
75