/
Cadence_TTC.cs
580 lines (522 loc) · 21.1 KB
/
Cadence_TTC.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
//
// Copyright (c) 2010-2023 Antmicro
//
// This file is licensed under the MIT License.
// Full license text is available in 'licenses/MIT.txt'.
//
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Antmicro.Renode.Peripherals.Bus;
using Antmicro.Renode.Core;
using Antmicro.Renode.Core.Structure.Registers;
using Antmicro.Renode.Exceptions;
using Antmicro.Renode.Time;
namespace Antmicro.Renode.Peripherals.Timers
{
public class Cadence_TTC : IDoubleWordPeripheral, INumberedGPIOOutput, IKnownSize
{
public Cadence_TTC(IMachine machine, long frequency = DefaultFrequency)
{
var irqs = new Dictionary<int, IGPIO>(TimerUnitsCount);
var registersMap = new Dictionary<long, DoubleWordRegister>();
for(var index = 0; index < timerUnits.Length; index++)
{
var timer = new TimerUnit(machine.ClockSource, this, frequency, $"Timer{index + 1}");
foreach(var register in BuildTimerUnitRegisters(timer))
{
registersMap[register.Key + index * RegisterSize] = register.Value;
}
timerUnits[index] = timer;
irqs[index] = timer.irq;
}
Connections = new ReadOnlyDictionary<int, IGPIO>(irqs);
registers = new DoubleWordRegisterCollection(this, registersMap);
}
public void WriteDoubleWord(long offset, uint value)
{
registers.Write(offset, value);
}
public uint ReadDoubleWord(long offset)
{
return registers.Read(offset);
}
public void Reset()
{
registers.Reset();
// Registers values depend only on a timer object (not on registers reset)
foreach(var timer in timerUnits)
{
timer.Reset();
}
}
public void SetCounterValue(int timerIndex, uint value)
{
if(timerIndex < 0 || timerIndex >= TimerUnitsCount)
{
throw new RecoverableException($"Invalid timer index: TTC contains {TimerUnitsCount} timers.");
}
timerUnits[timerIndex].Value = value;
}
public long Size => 0x100;
public IReadOnlyDictionary<int, IGPIO> Connections { get; }
public long Frequency
{
get => timerUnits[0].Frequency;
set
{
foreach(var timer in timerUnits)
{
timer.Frequency = value;
}
}
}
private Dictionary<long, DoubleWordRegister> BuildTimerUnitRegisters(TimerUnit timer)
{
return new Dictionary<long, DoubleWordRegister>
{
{(long)Registers.ClockControl1, new DoubleWordRegister(this)
.WithReservedBits(7, 25)
.WithTaggedFlag("ExternalClockEdge", 6)
.WithTaggedFlag("ClockSource", 5)
.WithValueField(1, 4, name: "PrescalerValue",
writeCallback: (_, val) => timer.Prescaler = (int)val,
valueProviderCallback: (_) => (uint)timer.Prescaler
)
.WithFlag(0, name: "PrescalerEnable",
writeCallback: (_, val) => timer.PrescalerEnabled = val,
valueProviderCallback: (_) => timer.PrescalerEnabled
)
},
{(long)Registers.CounterControl1, new DoubleWordRegister(this)
.WithReservedBits(7, 25)
.WithTaggedFlag("WaveformPolarity", 6)
.WithTaggedFlag("WaveformOutputDisable", 5)
.WithFlag(4, name: "Reset",
writeCallback: (_, val) => { if(val) timer.ResetValue(); },
valueProviderCallback: (_) => false
)
.WithFlag(3, name: "MatchEnable",
writeCallback: (_, val) => timer.MatchEnabled = val,
valueProviderCallback: (_) => timer.MatchEnabled
)
.WithFlag(2, name: "CounterDecrement",
writeCallback: (_, val) => timer.Direction = (val ? Direction.Descending : Direction.Ascending),
valueProviderCallback: (_) => timer.Direction == Direction.Descending
)
.WithEnumField<DoubleWordRegister, CounterMode>(1, 1, name: "CounterMode",
writeCallback: (_, val) => timer.Mode = val,
valueProviderCallback: (_) => timer.Mode
)
.WithFlag(0, name: "Disable",
writeCallback: (_, val) => timer.Enabled = !val,
valueProviderCallback: (_) => !timer.Enabled
)
},
{(long)Registers.CounterValue1, new DoubleWordRegister(this)
.WithValueField(0, 32, FieldMode.Read, name: "CounterValue",
valueProviderCallback: (_) => (uint)timer.Value
)
},
{(long)Registers.CounterInterval1, new DoubleWordRegister(this)
.WithValueField(0, 32, name: "IntervalCounter",
writeCallback: (_, val) => timer.Interval = (uint)val,
valueProviderCallback: (_) => timer.Interval
)
},
{(long)Registers.Match1Counter1, new DoubleWordRegister(this)
.WithValueField(0, 32, name: "Match1Value",
writeCallback: (_, val) => timer.Match[0].MatchValue = (uint)val,
valueProviderCallback: (_) => timer.Match[0].MatchValue
)
},
{(long)Registers.Match2Counter1, new DoubleWordRegister(this)
.WithValueField(0, 32, name: "Match1Value",
writeCallback: (_, val) => timer.Match[1].MatchValue = (uint)val,
valueProviderCallback: (_) => timer.Match[1].MatchValue
)
},
{(long)Registers.Match3Counter1, new DoubleWordRegister(this)
.WithValueField(0, 32, name: "Match1Value",
writeCallback: (_, val) => timer.Match[2].MatchValue = (uint)val,
valueProviderCallback: (_) => timer.Match[2].MatchValue
)
},
{(long)Registers.InterruptStatus1, new DoubleWordRegister(this)
.WithReservedBits(6, 26)
.WithTaggedFlag("EventTimerOverflowInterrupt", 5)
.WithFlag(4, FieldMode.ReadToClear, name: "CounterInterrupt",
readCallback: (_, __) => timer.OverflowInterruptFlag = false,
valueProviderCallback: (_) => timer.OverflowInterruptFlag
)
.WithFlag(3, FieldMode.ReadToClear, name: "Match3Interrupt",
readCallback: (_, __) => timer.Match[2].Interrupt = false,
valueProviderCallback: (_) => timer.Match[2].Interrupt
)
.WithFlag(2, FieldMode.ReadToClear, name: "Match2Interrupt",
readCallback: (_, __) => timer.Match[1].Interrupt = false,
valueProviderCallback: (_) => timer.Match[1].Interrupt
)
.WithFlag(1, FieldMode.ReadToClear, name: "Match1Interrupt",
readCallback: (_, __) => timer.Match[0].Interrupt = false,
valueProviderCallback: (_) => timer.Match[0].Interrupt
)
.WithFlag(0, FieldMode.ReadToClear, name: "IntervalInterrupt",
readCallback: (_, __) => timer.IntervalInterruptFlag = false,
valueProviderCallback: (_) => timer.IntervalInterruptFlag
)
.WithReadCallback((_, __) => timer.UpdateInterrupts())
},
{(long)Registers.InterruptEnable1, new DoubleWordRegister(this)
.WithReservedBits(6, 26)
.WithTaggedFlag("EventTimerOverflowInterruptEnable", 5)
.WithFlag(4, name: "CounterInterruptEnable",
writeCallback: (_, val) => timer.OverflowInterruptEnabled = val,
valueProviderCallback: (_) => timer.OverflowInterruptEnabled
)
.WithFlag(3, name: "Match3InterruptEnable",
writeCallback: (_, val) => timer.Match[2].InterruptEnable = val,
valueProviderCallback: (_) => timer.Match[2].InterruptEnable
)
.WithFlag(2, name: "Match2InterruptEnable",
writeCallback: (_, val) => timer.Match[1].InterruptEnable = val,
valueProviderCallback: (_) => timer.Match[1].InterruptEnable
)
.WithFlag(1, name: "Match1InterruptEnable",
writeCallback: (_, val) => timer.Match[0].InterruptEnable = val,
valueProviderCallback: (_) => timer.Match[0].InterruptEnable
)
.WithFlag(0, name: "IntervalInterruptEnable",
writeCallback: (_, val) => timer.IntervalInterruptEnabled = val,
valueProviderCallback: (_) => timer.IntervalInterruptEnabled
)
.WithWriteCallback((_, __) => timer.UpdateInterrupts())
}
};
}
private readonly TimerUnit[] timerUnits = new TimerUnit[TimerUnitsCount];
private readonly DoubleWordRegisterCollection registers;
private const long DefaultFrequency = 33330000;
private const int RegisterSize = 4;
private const int TimerUnitsCount = 3;
private const int MatchTimerUnitsCount = 3;
private class TimerUnit : ITimer
{
public TimerUnit(IClockSource clockSource, IPeripheral parent, long frequency, string localName)
{
timer = new LimitTimer(clockSource, frequency, parent, localName, limit: OverflowLimit, direction: Direction.Ascending, eventEnabled: true);
timer.LimitReached += OnLimitReached;
Match = new MatchTimerUnit[MatchTimerUnitsCount];
for(var i = 0; i < MatchTimerUnitsCount; i++)
{
Match[i] = new MatchTimerUnit(clockSource, parent, this, frequency, $"{localName}-match{i}");
}
}
public void Reset()
{
timer.Reset();
MatchEnabled = false;
Mode = CounterMode.Overflow;
Interval = 0;
PrescalerEnabled = false;
Prescaler = 0;
Array.ForEach(Match, m => m.Reset());
OverflowInterruptEnabled = false;
IntervalInterruptEnabled = false;
ResetFlags();
}
public void ResetFlags()
{
OverflowInterruptFlag = false;
IntervalInterruptFlag = false;
UpdateInterrupts();
}
public void ResetValue()
{
timer.ResetValue();
Array.ForEach(Match, m => m.Update());
}
public void UpdateInterrupts()
{
irq.Set((OverflowInterruptFlag && OverflowInterruptEnabled)
|| (IntervalInterruptFlag && IntervalInterruptEnabled)
|| Match.Any(m => m.IRQ));
}
public bool Enabled
{
get => timer.Enabled;
set
{
timer.Enabled = value;
Array.ForEach(Match, m => m.Update());
}
}
public bool MatchEnabled
{
get => matchEnabled;
set
{
matchEnabled = value;
Array.ForEach(Match, m => m.Enabled = value);
}
}
public CounterMode Mode
{
get => mode;
set
{
mode = value;
UpdateLimit();
}
}
public uint Interval
{
get => interval;
set
{
interval = value;
UpdateLimit();
}
}
public bool PrescalerEnabled
{
get => prescalerEnabled;
set
{
prescalerEnabled = value;
UpdateDivider();
}
}
public int Prescaler
{
get => prescaler;
set
{
prescaler = value;
UpdateDivider();
}
}
public Direction Direction
{
get => timer.Direction;
set
{
timer.Direction = value;
Array.ForEach(Match, m => m.Update());
}
}
public ulong Value
{
get => timer.Value;
set
{
timer.Value = value;
Array.ForEach(Match, m => m.Update());
}
}
public long Frequency
{
get => timer.Frequency;
set
{
timer.Frequency = value;
Array.ForEach(Match, m => m.Frequency = value);
}
}
public bool OverflowInterruptFlag { get; set; }
public bool OverflowInterruptEnabled { get; set; }
public bool IntervalInterruptFlag { get; set; }
public bool IntervalInterruptEnabled { get; set; }
public MatchTimerUnit[] Match { get; }
public readonly IGPIO irq = new GPIO();
private void OnLimitReached()
{
if(Mode == CounterMode.Interval)
{
IntervalInterruptFlag = true;
}
else
{
OverflowInterruptFlag = true;
}
Array.ForEach(Match, m => m.Update());
UpdateInterrupts();
}
private void UpdateDivider()
{
if(PrescalerEnabled)
{
timer.Divider = 1 << (Prescaler + 1);
}
else
{
timer.Divider = 1;
}
Array.ForEach(Match, m => m.Divider = timer.Divider);
}
private void UpdateLimit()
{
if(Mode == CounterMode.Interval)
{
timer.Limit = Interval;
}
else
{
timer.Limit = OverflowLimit;
}
Array.ForEach(Match, m => m.Limit = timer.Limit);
}
private CounterMode mode;
private uint interval;
private bool prescalerEnabled;
private bool matchEnabled;
private int prescaler;
private readonly LimitTimer timer;
private const uint OverflowLimit = UInt32.MaxValue;
public class MatchTimerUnit
{
public MatchTimerUnit(IClockSource clockSource, IPeripheral parent, TimerUnit owner, long frequency, string localName)
{
this.owner = owner;
timer = new LimitTimer(clockSource, frequency, parent, localName, limit: OverflowLimit, direction: Direction.Ascending, workMode: WorkMode.OneShot);
timer.LimitReached += owner.UpdateInterrupts;
limit = OverflowLimit;
}
public void Reset()
{
timer.Reset();
matchValue = 0;
// `limit` and `matchEnabled` are reset by `owner`
}
public void Update()
{
if(!enabled || matchValue > limit || !owner.Enabled || (IsAscending ? matchValue < owner.Value : owner.Value < matchValue))
{
timer.Enabled = false;
return;
}
TimerMatchValue = matchValue;
TimerValue = owner.Value;
timer.Enabled = true;
}
public uint MatchValue
{
get => matchValue;
set
{
matchValue = value;
Update();
}
}
public bool Enabled
{
get => enabled;
set
{
enabled = value;
Update();
}
}
public bool Interrupt
{
get => timer.RawInterrupt;
set
{
if(!value)
{
timer.ClearInterrupt();
}
}
}
public bool IRQ => timer.Interrupt;
public bool InterruptEnable
{
get => timer.EventEnabled;
set => timer.EventEnabled = value;
}
public int Divider
{
set => timer.Divider = value;
}
public ulong Limit
{
set
{
limit = value;
Update();
}
}
public long Frequency
{
set => timer.Frequency = value;
}
private ulong TranslateValueForInternalTimer(ulong value)
{
// For descending this class flips direction, thus counting in ascending
// direction, changing sign and using values congruent modulo `limit`
// NOTE: ComparingTimer doesn't support descending direction so this
// translation and usage of LimitTimer is a workaround
return IsAscending ? value : limit - value;
}
private ulong TimerMatchValue
{
get => TranslateValueForInternalTimer(timer.Limit);
set => timer.Limit = TranslateValueForInternalTimer(value);
}
private ulong TimerValue
{
get => TranslateValueForInternalTimer(timer.Value);
set => timer.Value = TranslateValueForInternalTimer(value);
}
private bool IsAscending => owner.Direction == Direction.Ascending;
private bool enabled;
private ulong limit;
private uint matchValue;
private readonly TimerUnit owner;
private readonly LimitTimer timer;
}
}
private enum CounterMode
{
Overflow = 0x0,
Interval = 0x1,
}
private enum Registers : long
{
ClockControl1 = 0x00,
ClockControl2 = 0x04,
ClockControl3 = 0x08,
CounterControl1 = 0x0C,
CounterControl2 = 0x10,
CounterControl3 = 0x14,
CounterValue1 = 0x18,
CounterValue2 = 0x1C,
CounterValue3 = 0x20,
CounterInterval1 = 0x24,
CounterInterval2 = 0x28,
CounterInterval3 = 0x2C,
Match1Counter1 = 0x30,
Match1Counter2 = 0x34,
Match1Counter3 = 0x38,
Match2Counter1 = 0x3C,
Match2Counter2 = 0x40,
Match2Counter3 = 0x44,
Match3Counter1 = 0x48,
Match3Counter2 = 0x4C,
Match3Counter3 = 0x50,
InterruptStatus1 = 0x54,
InterruptStatus2 = 0x58,
InterruptStatus3 = 0x5C,
InterruptEnable1 = 0x60,
InterruptEnable2 = 0x64,
InterruptEnable3 = 0x68,
EventControlTimer1 = 0x6C,
EventControlTimer2 = 0x70,
EventControlTimer3 = 0x74,
EventRegister1 = 0x78,
EventRegister2 = 0x7C,
EventRegister3 = 0x80
}
}
}