1 // Copyright 2017-2019 VMware, Inc.
2 // SPDX-License-Identifier: BSD-2-Clause
3 //
4 // The BSD-2 license (the License) set forth below applies to all parts of the
5 // Cascade project.  You may not use this file except in compliance with the
6 // License.
7 //
8 // BSD-2 License
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are met:
12 //
13 // 1. Redistributions of source code must retain the above copyright notice, this
14 // list of conditions and the following disclaimer.
15 //
16 // 2. Redistributions in binary form must reproduce the above copyright notice,
17 // this list of conditions and the following disclaimer in the documentation
18 // and/or other materials provided with the distribution.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND
21 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 #ifndef CASCADE_SRC_TARGET_CORE_SW_SW_RESET_H
32 #define CASCADE_SRC_TARGET_CORE_SW_SW_RESET_H
33 
34 #include <cassert>
35 #include <mutex>
36 #include "common/bits.h"
37 #include "target/core.h"
38 
39 namespace cascade::sw {
40 
41 class SwReset : public Reset {
42   public:
43     SwReset(Interface* interface, VId out, Bits* val, std::mutex* lock);
44     ~SwReset() override = default;
45 
46     State* get_state() override;
47     void set_state(const State* s) override;
48     Input* get_input() override;
49     void set_input(const Input* i) override;
50 
51     bool overrides_done_step() const override;
52     void done_step() override;
53 
54     void read(VId id, const Bits* b) override;
55     void evaluate() override;
56     bool there_are_updates() const override;
57     void update() override;
58 
59   private:
60     VId out_;
61     Bits* val_;
62     std::mutex* lock_;
63 
64     Bits latch_;
65     bool there_are_updates_;
66 };
67 
SwReset(Interface * interface,VId out,Bits * val,std::mutex * lock)68 inline SwReset::SwReset(Interface* interface, VId out, Bits* val, std::mutex* lock) : Reset(interface) {
69   out_ = out;
70   val_ = val;
71   lock_ = lock;
72 
73   there_are_updates_ = true;
74 }
75 
get_state()76 inline State* SwReset::get_state() {
77   return new State();
78 }
79 
set_state(const State * s)80 inline void SwReset::set_state(const State* s) {
81   // Stateless; does nothing.
82   (void) s;
83 }
84 
get_input()85 inline Input* SwReset::get_input() {
86   // Outputs only; does nothing
87   return new Input();
88 }
89 
set_input(const Input * i)90 inline void SwReset::set_input(const Input* i) {
91   // Outputs only; does nothing
92   (void) i;
93 }
94 
overrides_done_step()95 inline bool SwReset::overrides_done_step() const {
96   return true;
97 }
98 
done_step()99 inline void SwReset::done_step() {
100   there_are_updates_ = true;
101 }
102 
read(VId id,const Bits * b)103 inline void SwReset::read(VId id, const Bits* b) {
104   // Invoking this method doesn't make sense
105   (void) id;
106   (void) b;
107   assert(false);
108 }
109 
evaluate()110 inline void SwReset::evaluate() {
111   interface()->write(out_, &latch_);
112 }
113 
there_are_updates()114 inline bool SwReset::there_are_updates() const {
115   return there_are_updates_;
116 }
117 
update()118 inline void SwReset::update() {
119   there_are_updates_ = false;
120   {
121     std::lock_guard<std::mutex> lg(*lock_);
122     latch_.assign(0, *val_);
123   }
124   evaluate();
125 }
126 
127 } // namespace cascade::sw
128 
129 #endif
130