1 //
2 // Copyright (c) 2010-2018 Antmicro
3 //
4 // This file is licensed under the MIT License.
5 // Full license text is available in 'licenses/MIT.txt'.
6 //
7 using System;
8 using System.Collections.Generic;
9 using System.Linq;
10 
11 namespace Antmicro.Renode.Core
12 {
13     public class GPIOGate
14     {
GPIOGate(GPIO destinationIrq)15         public GPIOGate(GPIO destinationIrq)
16         {
17             destination = destinationIrq;
18             nodes = new List<IrqNode>();
19         }
20 
GetGPIO()21         public IGPIO GetGPIO()
22         {
23             var result = new IrqNode(destination, HandleChangeState);
24             nodes.Add(result);
25             return result;
26         }
27 
HandleChangeState()28         private void HandleChangeState()
29         {
30             for(var i = 0; i < nodes.Count; ++i)
31             {
32                 if(nodes[i].LocalState)
33                 {
34                     destination.Set(true);
35                     return;
36                 }
37             }
38             destination.Set(false);
39         }
40 
41         private readonly GPIO destination;
42 
43         private readonly List<IrqNode> nodes;
44 
45         private class IrqNode : IGPIO
46         {
IrqNode(IGPIO destination, Action changeCallback)47             public IrqNode(IGPIO destination, Action changeCallback)
48             {
49                 this.destination = destination;
50                 this.changeCallback = changeCallback;
51             }
52 
Connect(IGPIOReceiver destination, int destinationNumber)53             public void Connect(IGPIOReceiver destination, int destinationNumber)
54             {
55                 throw new NotImplementedException();
56             }
57 
Disconnect()58             public void Disconnect()
59             {
60                 throw new NotImplementedException();
61             }
62 
Disconnect(GPIOEndpoint endpoint)63             public void Disconnect(GPIOEndpoint endpoint)
64             {
65                 throw new NotImplementedException();
66             }
67 
Set(bool value)68             public void Set(bool value)
69             {
70                 LocalState = value;
71                 changeCallback();
72             }
73 
Toggle()74             public void Toggle()
75             {
76                 Set(!LocalState);
77             }
78 
79             public bool LocalState { get; private set; }
80 
81             public bool IsSet => destination.IsSet;
82 
83             public bool IsConnected => destination.IsConnected;
84 
85             public IList<GPIOEndpoint> Endpoints => destination.Endpoints;
86 
87             private readonly IGPIO destination;
88             private readonly Action changeCallback;
89         }
90     }
91 }
92