1/*
2 * Copyright 2019 Google, Inc.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met: redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer;
8 * redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution;
11 * neither the name of the copyright holders nor the names of its
12 * contributors may be used to endorse or promote products derived from
13 * this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 * Authors: Gabe Black
28 */
29
30#ifndef __DEV_INTPIN_HH__
31#define __DEV_INTPIN_HH__
32
33#include "sim/port.hh"
34
35class IntSourcePinBase;
36
37class IntSinkPinBase : public Port
38{
39  protected:
40    friend IntSourcePinBase;
41
42    IntSourcePinBase *source = nullptr;
43
44    int _number = 0;
45    bool _state = false;
46
47    IntSinkPinBase(const std::string &_name, PortID _id, int num) :
48        Port(_name, _id), _number(num)
49    {}
50
51    virtual void raiseOnDevice() = 0;
52    virtual void lowerOnDevice() = 0;
53
54    void
55    raise()
56    {
57        _state = true;
58        raiseOnDevice();
59    }
60
61    void
62    lower()
63    {
64        _state = false;
65        lowerOnDevice();
66    }
67
68  public:
69    int number() { return _number; }
70    bool state() { return _state; }
71
72    void bind(Port &peer) override;
73    void unbind() override;
74};
75
76template <class Device>
77class IntSinkPin : public IntSinkPinBase
78{
79  private:
80    Device *device = nullptr;
81
82    void raiseOnDevice() override { device->raiseInterruptPin(number()); }
83    void lowerOnDevice() override { device->lowerInterruptPin(number()); }
84
85  public:
86    IntSinkPin(const std::string &_name, PortID _id, Device *dev, int num) :
87        IntSinkPinBase(_name, _id, num), device(dev) {}
88    IntSinkPin(const std::string &_name, PortID _id, Device *dev) :
89        IntSinkPin(_name, _id, dev, _id) {}
90};
91
92class IntSourcePinBase : public Port
93{
94  private:
95    IntSinkPinBase *sink = nullptr;
96    bool _state = false;
97
98  public:
99    IntSourcePinBase(const std::string &_name, PortID _id, bool def_state) :
100        Port(_name, _id), _state(def_state)
101    {}
102
103    void raise() { sink->raise(); }
104    void lower() { sink->lower(); }
105
106    void bind(Port &peer) override;
107    void unbind() override;
108};
109
110template <class Device>
111class IntSourcePin : public IntSourcePinBase
112{
113  public:
114    IntSourcePin(const std::string &_name, PortID _id, Device *owner,
115                 bool def_state=false) :
116        IntSourcePinBase(_name, _id, def_state)
117    {}
118};
119
120#endif //__DEV_INTPIN_HH__
121