1 //
2 // Copyright (c) 2010-2022 Antmicro
3 //
4 //  This file is licensed under the MIT License.
5 //  Full license text is available in 'licenses/MIT.txt'.
6 //
7 
8 using Antmicro.Renode.Core;
9 using Antmicro.Renode.Core.Structure.Registers;
10 
11 namespace Antmicro.Renode.Peripherals.Miscellaneous
12 {
13     public sealed class IMX_TRNG : BasicDoubleWordPeripheral, IKnownSize
14     {
IMX_TRNG(IMachine machine)15         public IMX_TRNG(IMachine machine) : base(machine)
16         {
17             rng = EmulationManager.Instance.CurrentEmulation.RandomGenerator;
18             IRQ = new GPIO();
19             DefineRegisters();
20         }
21 
Reset()22         public override void Reset()
23         {
24             base.Reset();
25             IRQ.Unset();
26         }
27 
28         public long Size => 0x4000;
29 
30         public GPIO IRQ { get; }
31 
DefineRegisters()32         private void DefineRegisters()
33         {
34             Registers.MiscControl.Define(this)
35                 .WithTag("SAMP_MODE", 0, 2)
36                 .WithTag("OSC_DIV", 2, 2)
37                 .WithReservedBits(4, 2)
38                 .WithTaggedFlag("RST_DEF", 6)
39                 .WithTaggedFlag("FOR_SCLK", 7)
40                 .WithTaggedFlag("FCT_FAIL", 8)
41                 .WithTaggedFlag("FCT_VAL", 9)
42                 .WithFlag(10, name: "ENT_VAL", valueProviderCallback: _ => true)
43                 .WithTaggedFlag("TST_OUT", 11)
44                 .WithFlag(12, name: "ERR", valueProviderCallback: _ => false)
45                 .WithTaggedFlag("TSTOP_OK", 13)
46                 .WithTaggedFlag("LRUN_CONT", 14)
47                 .WithReservedBits(15, 1)
48                 .WithTaggedFlag("PRGM", 16)
49                 .WithReservedBits(17, 15)
50             ;
51 
52             Registers.StatCheckMisc.Define(this)
53                 .WithTag("LRUN_MAX", 0, 8)
54                 .WithReservedBits(8, 8)
55                 .WithTag("RTY_CT", 16, 4)
56             ;
57 
58             Registers.PokerRange.Define(this)
59                 .WithTag("PKR_RNG", 0, 16)
60                 .WithReservedBits(16, 16)
61             ;
62 
63             Registers.SeedControl.Define(this)
64                 .WithTag("SAMP_SIZE", 0, 16)
65                 .WithTag("ENT_DLY", 16, 16)
66             ;
67 
68             Registers.Entropy.DefineMany(this, 16, (register, idx) =>
69             {
70                 var j = idx;
71                 register
72                     .WithValueField(0, 32, name: $"ENT{j}",
73                         valueProviderCallback: _ => (uint)rng.Next())
74                 ;
75             });
76         }
77 
78         private readonly PseudorandomNumberGenerator rng;
79 
80         private enum Registers
81         {
82             MiscControl = 0x0,
83             StatCheckMisc = 0x4,
84             PokerRange = 0x8,
85             // Poker Maximum Limit / Poker Square Calculation Result
86             PKRMAX_PKRSQ = 0xC,
87             SeedControl = 0x10,
88             // Sparse Bit Limit / Total Samples
89             SBLIM_TOTSAM = 0x14,
90             FreqCountMin = 0x18,
91             // Frequency Count / Frequency Count Maximum Limit
92             FRQCNT_FRQMAX = 0x1C,
93             // Statistical Check Monobit Count / Limit
94             SCMC_SCML = 0x20,
95             // Statistical Check Run Length 1 Count / Limit
96             SCR1C_SCR1L = 0x24,
97             // Statistical Check Run Length 2 Count / Limit
98             SCR2C_SCR2L = 0x28,
99             // Statistical Check Run Length 3 Count / Limit
100             SCR3C_SCR3L = 0x2C,
101             // Statistical Check Run Length 4 Count / Limit
102             SCR4C_SCR4L = 0x30,
103             // Statistical Check Run Length 5 Count / Limit
104             SCR5C_SCR5L = 0x34,
105             // Statistical Check Run Length 6 Count / Limit
106             SCR6C_SCR6L = 0x38,
107             Status = 0x3C,
108             Entropy  = 0x40,
109             // Statistical Check Poker Count 1 and 0
110             PKRCNT10 = 0x80,
111             // Statistical Check Poker Count 3 and 2
112             PKRCNT32 = 0x84,
113             // Statistical Check Poker Count 5 and 4
114             PKRCNT54 = 0x88,
115             // Statistical Check Poker Count 7 and 6
116             PKRCNT76 = 0x8C,
117             // Statistical Check Poker Count 9 and 8
118             PKRCNT98 = 0x90,
119             // Statistical Check Poker Count B and A
120             PKRCNTBA = 0x94,
121             // Statistical Check Poker Count D and C
122             PKRCNTDC = 0x98,
123             // Statistical Check Poker Count F and E
124             PKRCNTFE = 0x9C,
125             SecurityConfig = 0xA0,
126             InterruptControl = 0xA4,
127             InterruptMask = 0xA8,
128             InterruptStatus = 0xAC,
129             VersionID1 = 0xF0,
130             VersionID2 = 0xF4,
131         }
132     }
133 }
134