scc  2024.06
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 template <> struct enable_for_enum<lock_e> { static const bool enable = true; };
131 template <> struct enable_for_enum<resp_e> { static const bool enable = true; };
132 template <> struct enable_for_enum<trans_e> { static const bool enable = true; };
133 template <> struct enable_for_enum<burst_e> { static const bool enable = true; };
134 
135 inline bool ahb_extension::is_instruction() const { return prot & INSTR; }
136 
137 inline void ahb_extension::set_instruction(bool instr) {
138  if(instr)
139  prot |= INSTR;
140  else
141  prot &= ~INSTR;
142 }
143 
144 inline bool ahb_extension::is_privileged() const { return prot & PRIV; }
145 
146 inline void ahb_extension::set_privileged(bool priv) {
147  if(priv)
148  prot |= PRIV;
149  else
150  prot &= ~PRIV;
151 }
152 
153 inline bool ahb_extension::is_bufferable() const { return prot & BUFFERABLE; }
154 
155 inline void ahb_extension::set_bufferable(bool bufferable) {
156  if(bufferable)
157  prot |= BUFFERABLE;
158  else
159  prot &= ~BUFFERABLE;
160 }
161 
162 inline bool ahb_extension::is_cacheable() const { return prot & CACHABLE; }
163 
164 inline void ahb_extension::set_cacheable(bool cacheable) {
165  if(cacheable)
166  prot |= CACHABLE;
167  else
168  prot &= ~CACHABLE;
169 }
170 
171 inline uint8_t ahb_extension::get_protection() const { return prot; }
172 
173 inline void ahb_extension::set_protection(uint8_t protection) { prot = protection; }
174 
175 inline bool ahb_extension::is_locked() const { return lock == lock_e::LOCKED; }
176 
177 inline void ahb_extension::set_locked(bool locked) { lock = locked ? lock_e::LOCKED : lock_e::NORMAL; }
178 
179 inline burst_e ahb_extension::get_burst() const { return burst; }
180 
181 inline void ahb_extension::set_burst(burst_e b) { burst = b; }
182 
183 inline bool ahb_extension::is_seq() const { return seq; }
184 
185 inline void ahb_extension::set_seq(bool s) { seq = s; }
186 
187 inline tlm::tlm_extension_base* ahb_extension::clone() const { return new ahb_extension(*this); }
188 
189 inline resp_e ahb_extension::get_resp() const { return resp; }
190 
191 inline void ahb_extension::set_resp(resp_e r) { resp = r; }
192 
193 inline void ahb_extension::copy_from(const tlm::tlm_extension_base& ext) {
194  auto const* ahb_ext = dynamic_cast<const ahb_extension*>(&ext);
195  assert(ahb_ext);
196  (*this) = *ahb_ext;
197 }
198 } // namespace ahb
199 
200 #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:193
tlm::tlm_extension_base * clone() const override
the clone function to create deep copies of
Definition: ahb_tlm.h:187