scc  2022.4.0
SystemC components library
ahb_tlm.h
1 /*******************************************************************************
2  * Copyright 2019-2023 MINRES Technologies GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *******************************************************************************/
16 
17 #ifndef _AHB_TLM_H_
18 #define _AHB_TLM_H_
19 
20 #include <array>
21 #include <cstdint>
22 #include <tlm>
23 
25 namespace ahb {
26 
30 template <typename Enum> struct enable_for_enum { static const bool value = false; };
36 template <typename E> inline E into(typename std::underlying_type<E>::type t);
42 template <typename E, typename ULT = typename std::underlying_type<E>::type,
43  typename X = typename std::enable_if<std::is_enum<E>::value && !std::is_convertible<E, ULT>::value, bool>::type>
44 inline constexpr ULT to_int(E t) {
45  return static_cast<typename std::underlying_type<E>::type>(t);
46 }
52 template <typename E> const char* to_char(E t);
59 template <typename E, typename std::enable_if<enable_for_enum<E>::value, bool>::type>
60 inline std::ostream& operator<<(std::ostream& os, E e) {
61  os << to_char(e);
62  return os;
63 }
64 
65 std::ostream& operator<<(std::ostream& os, tlm::tlm_generic_payload const& t);
66 
67 enum class lock_e : uint8_t { NORMAL = 0x0, EXLUSIVE = 0x1, LOCKED = 0x2 };
68 
69 enum class resp_e : uint8_t { OKAY = 0x0, EXOKAY = 0x1, SLVERR = 0x2, DECERR = 0x3 };
70 
71 enum class trans_e : uint8_t { IDLE = 0x0, BUSY = 0x1, NONSEQ = 0x2, SEQ = 0x3 };
72 
73 enum class burst_e : uint8_t { SINGLE = 0x0, INCR = 0x1, WRAP4 = 0x2, INCR4 = 0x3, WRAP8 = 0x4, INCR8 = 0x5, WRAP16 = 0x6, INCR16 = 0x7 };
74 
75 struct ahb_extension : public tlm::tlm_extension<ahb_extension> {
76  // HPROT[0]
77  bool is_instruction() const;
78  void set_instruction(bool = true);
79  // HPROT[1]
80  bool is_privileged() const;
81  void set_privileged(bool = true);
82  // HPROT[2]
83  void set_bufferable(bool = true);
84  bool is_bufferable() const;
85  // HPROT[3]
86  void set_cacheable(bool = true);
87  bool is_cacheable() const;
88  // HLOCK
89  bool is_locked() const;
90  void set_locked(bool = true);
91  // HTRANS[1]
92  bool is_seq() const;
93  void set_seq(bool = true);
94 
95  uint8_t get_protection() const;
96  void set_protection(uint8_t);
97 
98  burst_e get_burst() const;
99  void set_burst(burst_e);
100 
101  resp_e get_resp() const;
102  void set_resp(resp_e);
103 
104  ahb_extension() = default;
105 
106  ahb_extension(const ahb_extension& o) = default;
111  tlm::tlm_extension_base* clone() const override;
116  void copy_from(tlm::tlm_extension_base const& ext) override;
117 
118 private:
119  enum { INSTR = 1, PRIV = 2, BUFFERABLE = 4, CACHABLE = 8 };
120  uint8_t prot{0};
121  lock_e lock{lock_e::NORMAL};
122  resp_e resp{resp_e::OKAY};
123  burst_e burst{burst_e::SINGLE};
124  bool seq{false};
125 };
126 
127 /*****************************************************************************
128  * Implementation details
129  *****************************************************************************/
130 inline bool ahb_extension::is_instruction() const { return prot & INSTR; }
131 
132 inline void ahb_extension::set_instruction(bool instr) {
133  if(instr)
134  prot |= INSTR;
135  else
136  prot &= ~INSTR;
137 }
138 
139 inline bool ahb_extension::is_privileged() const { return prot & PRIV; }
140 
141 inline void ahb_extension::set_privileged(bool priv) {
142  if(priv)
143  prot |= PRIV;
144  else
145  prot &= ~PRIV;
146 }
147 
148 inline bool ahb_extension::is_bufferable() const { return prot & BUFFERABLE; }
149 
150 inline void ahb_extension::set_bufferable(bool bufferable) {
151  if(bufferable)
152  prot |= BUFFERABLE;
153  else
154  prot &= ~BUFFERABLE;
155 }
156 
157 inline bool ahb_extension::is_cacheable() const { return prot & CACHABLE; }
158 
159 inline void ahb_extension::set_cacheable(bool cacheable) {
160  if(cacheable)
161  prot |= CACHABLE;
162  else
163  prot &= ~CACHABLE;
164 }
165 
166 inline uint8_t ahb_extension::get_protection() const { return prot; }
167 
168 inline void ahb_extension::set_protection(uint8_t protection) { prot = protection; }
169 
170 inline bool ahb_extension::is_locked() const { return lock == lock_e::LOCKED; }
171 
172 inline void ahb_extension::set_locked(bool locked) { lock = locked ? lock_e::LOCKED : lock_e::NORMAL; }
173 
174 inline burst_e ahb_extension::get_burst() const { return burst; }
175 
176 inline void ahb_extension::set_burst(burst_e b) { burst = b; }
177 
178 inline bool ahb_extension::is_seq() const { return seq; }
179 
180 inline void ahb_extension::set_seq(bool s) { seq = s; }
181 
182 inline tlm::tlm_extension_base* ahb_extension::clone() const { return new ahb_extension(*this); }
183 
184 inline resp_e ahb_extension::get_resp() const { return resp; }
185 
186 inline void ahb_extension::set_resp(resp_e r) { resp = r; }
187 
188 inline void ahb_extension::copy_from(const tlm::tlm_extension_base& ext) {
189  auto const* ahb_ext = dynamic_cast<const ahb_extension*>(&ext);
190  assert(ahb_ext);
191  (*this) = *ahb_ext;
192 }
193 } // namespace ahb
194 
195 #endif /* _AHB_TLM_H_ */
TLM2.0 components modeling AHB.
Definition: ahb_tlm.cpp:19
constexpr ULT to_int(E t)
Definition: ahb_tlm.h:44
const char * to_char(E t)
E into(typename std::underlying_type< E >::type t)
void copy_from(tlm::tlm_extension_base const &ext) override
deep copy all values from ext
Definition: ahb_tlm.h:188
tlm::tlm_extension_base * clone() const override
the clone function to create deep copies of
Definition: ahb_tlm.h:182