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 System.Linq;
10 using NUnit.Framework;
11 using Antmicro.Renode.Core;
12 using Moq;
13 using Antmicro.Renode.Peripherals;
14 using Antmicro.Renode.Peripherals.Bus;
15 using Antmicro.Renode.Core.Structure;
16 using System.Threading;
17 using Antmicro.Renode.Exceptions;
18 using Antmicro.Renode.UserInterface;
19 
20 namespace Antmicro.Renode.UnitTests
21 {
22     public class MachineTests
23     {
24         [Test]
ShouldThrowOnRegisteringAnotherPeripheralWithTheSameName()25         public void ShouldThrowOnRegisteringAnotherPeripheralWithTheSameName()
26         {
27             var machine = new Machine();
28             var peripheral1 = new Mock<IDoubleWordPeripheral>().Object;
29             var peripheral2 = new Mock<IDoubleWordPeripheral>().Object;
30             machine.SystemBus.Register(peripheral1,  0.By(10));
31             machine.SystemBus.Register(peripheral2, 10.By(10));
32             machine.SetLocalName(peripheral1, "name");
33 
34             Assert.Throws(typeof(RecoverableException), () => machine.SetLocalName(peripheral2, "name"));
35         }
36 
37         [Test]
ShouldFindPeripheralByPath()38         public void ShouldFindPeripheralByPath()
39         {
40             var machine = new Machine();
41             var peripheral1 = new Mock<IDoubleWordPeripheral>().Object;
42             machine.SystemBus.Register(peripheral1, 0.By(10));
43             machine.SetLocalName(peripheral1, "name");
44 
45             Assert.AreEqual(peripheral1, machine["sysbus.name"]);
46         }
47 
48         [Test]
ShouldFindPeripheralByPathWhenThereAreTwo()49         public void ShouldFindPeripheralByPathWhenThereAreTwo()
50         {
51             var machine = new Machine();
52             var peripheral1 = new Mock<IDoubleWordPeripheral>().Object;
53             var peripheral2 = new Mock<IDoubleWordPeripheral>().Object;
54             machine.SystemBus.Register(peripheral1,  0.By(10));
55             machine.SystemBus.Register(peripheral2, 10.By(10));
56             machine.SetLocalName(peripheral1, "first");
57             machine.SetLocalName(peripheral2, "second");
58 
59             Assert.AreEqual(peripheral1, machine["sysbus.first"]);
60             Assert.AreEqual(peripheral2, machine["sysbus.second"]);
61         }
62 
63         [Test]
ShouldThrowOnNullOrEmptyPeripheralName()64         public void ShouldThrowOnNullOrEmptyPeripheralName()
65         {
66             var machine = new Machine();
67             var peripheral1 = new Mock<IDoubleWordPeripheral>().Object;
68             machine.SystemBus.Register(peripheral1, 0.By(10));
69 
70             Assert.Throws(typeof(RecoverableException), () => machine.SetLocalName(peripheral1, ""));
71             Assert.Throws(typeof(RecoverableException), () => machine.SetLocalName(peripheral1, null));
72         }
73 
74         [Test]
ShouldHandleManagedThreads()75         public void ShouldHandleManagedThreads()
76         {
77             var counter = 0;
78             var a = (Action)(() => counter++);
79 
80             using(var emulation = new Emulation())
81             {
82                 var machine = new Machine();
83                 emulation.AddMachine(machine);
84 
85                 emulation.SetGlobalQuantum(Time.TimeInterval.FromMilliseconds(1000));
86                 emulation.SetGlobalAdvanceImmediately(true);
87 
88                 var mt = machine.ObtainManagedThread(a, 1);
89 
90                 emulation.RunToNearestSyncPoint();
91                 emulation.RunToNearestSyncPoint();
92                 emulation.RunToNearestSyncPoint();
93                 Assert.AreEqual(0, counter);
94 
95                 mt.Start();
96 
97                 emulation.RunToNearestSyncPoint();
98                 Assert.AreEqual(1, counter);
99 
100                 emulation.RunToNearestSyncPoint();
101                 Assert.AreEqual(2, counter);
102 
103                 emulation.RunToNearestSyncPoint();
104                 Assert.AreEqual(3, counter);
105 
106                 mt.Stop();
107 
108                 emulation.RunToNearestSyncPoint();
109                 emulation.RunToNearestSyncPoint();
110                 emulation.RunToNearestSyncPoint();
111                 Assert.AreEqual(3, counter);
112             }
113         }
114 
115         public sealed class Mother : IPeripheralRegister<IPeripheral, NullRegistrationPoint>, IDoubleWordPeripheral
116         {
Mother(IMachine machine)117             public Mother(IMachine machine)
118             {
119                 this.machine = machine;
120             }
121 
Register(IPeripheral peripheral, NullRegistrationPoint registrationPoint)122             public void Register(IPeripheral peripheral, NullRegistrationPoint registrationPoint)
123             {
124                 machine.RegisterAsAChildOf(this, peripheral, registrationPoint);
125             }
126 
Unregister(IPeripheral peripheral)127             public void Unregister(IPeripheral peripheral)
128             {
129                 machine.UnregisterAsAChildOf(this, peripheral);
130             }
131 
ReadDoubleWord(long offset)132             public uint ReadDoubleWord(long offset)
133             {
134                 return 0;
135             }
136 
WriteDoubleWord(long offset, uint value)137             public void WriteDoubleWord(long offset, uint value)
138             {
139 
140             }
141 
Reset()142             public void Reset()
143             {
144 
145             }
146 
147             private readonly IMachine machine;
148         }
149     }
150 }
151 
152