NSVD Reader  0.0.1
node_container.hpp
Go to the documentation of this file.
1 
5 /*
6  * These codes are licensed under CC0.
7  * http://creativecommons.org/publicdomain/zero/1.0/
8  */
9 #ifndef __NODAMUSHI_SVD_NORMALIZED_NODE_CONTAINER_HPP__
10 #define __NODAMUSHI_SVD_NORMALIZED_NODE_CONTAINER_HPP__
11 
12 # include <vector>
13 # include <memory>
14 # include <string>
15 
16 
20 
21 # include "nodamushi/svd/Access.hpp"
26 
27 # include "nodamushi/boxvec.hpp"
29 
30 namespace nodamushi{
31 namespace svd{
32 namespace normalized{
33 
34 // ----------------------------------------------------------------------
35 // * interface
36 // get_address() : return absolute address
37 // get_offset() : return relative address in the peripheral
38 // get_protection(): return protection
39 // get_access() : return get access
40 // get_resetValue(): return reset value
41 // get_resetMask() : return reset mask.default value is ~0
42 // get_parent() : return parent node pointer.
43 // if it has no parent , return void*.
44 // get_parent2() : return cluster parent.
45 // if it has no cluster parent, return void*.
46 // ----------------------------------------------------------------------
47 
48 template<typename STRREF> struct Device;
49 template<typename STRREF> struct Peripheral;
50 template<typename STRREF> struct Cluster;
51 template<typename STRREF> struct Register;
52 template<typename STRREF> struct Field;
53 template<typename STRREF> struct Enumeration;
54 template<typename STRREF> struct Cpu;
55 template<typename STRREF> struct SAURegionsConfig;
56 
57 template<typename T> using node_ptr = std::shared_ptr<T>;
58 template<typename Parent> using parent_ptr= std::weak_ptr<Parent>;
59 template<typename Child> using list = ::nodamushi::boxvec<Child,node_ptr<Child>>;
60 
61 template<typename T,typename... ARGS>
62 node_ptr<T> make_node_ptr(ARGS&&... args)
63 {
64  return std::make_shared<T>(std::forward<ARGS>(args)...);
65 }
66 
67 
68 template<typename PTR>
70 {
71  template<typename T>
72  static uint32_t get_size(T p){return p->get_size();}
73  template<typename T>
74  static Protection get_protection(T p){return p->get_protection();}
75  template<typename T>
76  static Access get_access(T p){return p->get_access();}
77  template<typename T>
78  static uint64_t get_resetValue(T p){return p->get_resetValue();}
79  template<typename T>
80  static uint64_t get_resetMask(T p){return p->get_resetMask();}
81  template<typename T>
82  static ModifiedWriteValues get_modifiedWriteValues(T p){return p->get_modifiedWriteValues();}
83  template<typename T>
84  static ReadAction get_readAction(T p){return p->get_readAction();}
85 };
86 
87 
88 //----------------------- helper methods ------------------------------
89 template<typename T,typename You>
91 {
92  parent_ptr<You> _you(you);
93  auto itr = l.ptr_begin();
94  auto end = l.ptr_end();
95  while(itr != end){
96  auto& ptr = *itr;
97  ptr->update_parent(_you,ptr);
98  ++itr;
99  }
100 }
101 template<typename O,typename S>
103 {
104  if(auto p = o.get_parent()){
105  path_helper(*p,path);
106  }else if(auto p=o.get_parent2()){
107  path_helper(*p,path);
108  }
109  path.add_name(o.name);
110 }
111 template<typename O,typename S>
113 {
114  path.add_name(o.name);
115 }
116 
117 
118 template<typename STRREF,template<class>class O>
120  if(auto p = o.get_parent()){
121  return find_parent_peripheral(p);
122  }else if(auto p=o.get_parent2()){
123  return find_parent_peripheral(p);
124  }
125  return {};
126 }
127 template<typename STRREF,template<class>class O>
129  if(auto p = o.get_parent()){
130  return find_parent_peripheral(p);
131  }else if(auto p=o.get_parent2()){
132  return find_parent_peripheral(p);
133  }
134  return {};
135 }
136 
137 template<typename STRREF,template<class>class O>
139  if(auto p = o->get_parent()){
140  return find_parent_peripheral(p);
141  }else if(auto p=o->get_parent2()){
142  return find_parent_peripheral(p);
143  }
144  return {};
145 }
146 
147 template<typename STRREF,template<class>class O>
149  if(auto p = o->get_parent()){
150  return find_parent_peripheral(p);
151  }else if(auto p=o->get_parent2()){
152  return find_parent_peripheral(p);
153  }
154  return {};
155 }
156 
157 template<typename STRREF>
159 template<typename STRREF>
161 template<typename STRREF>
163 template<typename STRREF>
165 
166 //--------------- Address -------------------------------------------
167 template<typename O>
168 uint64_t calc_address(const O& o,uint64_t offset = 0)
169 {
170  offset += o.addressOffset;
171  if(auto ptr = o.get_parent())
172  return calc_address(*ptr,offset);
173  else if(auto ptr = o.get_parent2())
174  return calc_address(*ptr,offset);
175  return offset;
176 }
177 template<typename X>
178 uint64_t constexpr calc_address(const Device<X>& d,uint64_t offset=0){return offset;}
179 template<typename X>
180 uint64_t constexpr calc_address(const Peripheral<X>& p,uint64_t offset=0){return p.baseAddress+offset;}
181 
182 template<typename O>
183 uint64_t calc_offset(const O& o,uint64_t offset = 0)
184 {
185  offset += o.addressOffset;
186  if(auto ptr = o.get_parent())
187  return calc_offset(*ptr,offset);
188  else if(auto ptr = o.get_parent2())
189  return calc_offset(*ptr,offset);
190  return offset;
191 }
192 template<typename X>
193 uint64_t constexpr calc_offset(const Device<X>& d,uint64_t offset=0){return offset;}
194 template<typename X>
195 uint64_t constexpr calc_offset(const Peripheral<X>& p,uint64_t offset=0){return offset;}
196 
197 //------------------------------------------------------------------
198 
199 template<typename O>uint32_t get_default_size(const O& o)noexcept
200 {
201  if(auto ptr = o.get_parent()){
202  return parent_method_call<decltype(ptr)>::get_size(ptr);
203  }else if(auto ptr = o.get_parent2()){
204  return parent_method_call<decltype(ptr)>::get_size(ptr);
205  }
206  return 32;
207 }
208 
209 template<typename O>Access get_default_access(const O& o)noexcept
210 {
211  if(auto ptr = o.get_parent()){
212  return parent_method_call<decltype(ptr)>::get_access(ptr);
213  }else if(auto ptr = o.get_parent2()){
214  return parent_method_call<decltype(ptr)>::get_access(ptr);
215  }
216  return static_cast<Access>(0);
217 }
218 
219 template<typename O>Access get_default_fieldaccess(const O& o)noexcept
220 {
221  if(auto ptr = o.get_parent()){
222  return parent_method_call<decltype(ptr)>::get_access(ptr);
223  }
224  return static_cast<Access>(0);
225 }
226 
227 template<typename O>Protection get_default_protection(const O& o)noexcept
228 {
229  if(auto ptr = o.get_parent()){
230  return parent_method_call<decltype(ptr)>::get_protection(ptr);
231  }else if(auto ptr = o.get_parent2()){
232  return parent_method_call<decltype(ptr)>::get_protection(ptr);
233  }
234  return static_cast<Protection>(0);
235 }
236 
237 template<typename O>uint64_t get_default_resetValue(const O& o)noexcept
238 {
239  if(auto ptr = o.get_parent()){
240  return parent_method_call<decltype(ptr)>::get_resetValue(ptr);
241  }else if(auto ptr = o.get_parent2()){
242  return parent_method_call<decltype(ptr)>::get_resetValue(ptr);
243  }
244  return 0;
245 }
246 
247 template<typename O>uint64_t get_default_resetMask(const O& o)
248 {
249  if(auto ptr = o.get_parent()){
250  return parent_method_call<decltype(ptr)>::get_resetMask(ptr);
251  }else if(auto ptr = o.get_parent2()){
252  return parent_method_call<decltype(ptr)>::get_resetMask(ptr);
253  }
254  //TODO error?
255  return ~(uint64_t)0;
256 }
257 
258 template<typename O> ModifiedWriteValues get_default_modifiedWriteValues(const O& o){
259  if(auto ptr = o.get_parent()){
260  return parent_method_call<decltype(ptr)>::get_modifiedWriteValues(ptr);
261  }
262  return static_cast<ModifiedWriteValues>(0);
263 }
264 
265 template<typename O> ReadAction get_default_readAction(const O& o){
266  if(auto ptr = o.get_parent()){
267  return parent_method_call<decltype(ptr)>::get_readAction(ptr);
268  }
269  return static_cast<ReadAction>(0);
270 }
271 
272 
273 
274 template<typename O>
275 DataType get_default_dataType(const O& r)noexcept
276 {
277  auto s = r.get_size();
278  if(s <= 8)return DataType::UINT8; //0 - 8
279  else if(s <= 16)return DataType::UINT16;//9 -16
280  else if(s <= 32)return DataType::UINT32;//17 - 32
281  else if(s <= 64)return DataType::UINT64;//33 - 64
282  return DataType::UNDEFINED;
283 }
284 
285 template<typename R>
287 {
288  if(v.size()!=0){
289  auto i= v.ptr_begin(),e =v.ptr_end();
290  while(i!=e){
291  auto c = *i;
292  if(c->name == n)
293  return c;
294  ++i;
295  }
296  }
297  return {};
298 }
299 
300 // for void*
301 template<>struct parent_method_call<void*>
302 {
303  static constexpr uint64_t get_address(const void*){return 0;}
304  static constexpr uint32_t get_size(const void*){return 0;}
305  static constexpr Protection get_protection(const void*){return static_cast<Protection>(0);}
306  static constexpr Access get_access(const void*){return static_cast<Access>(0);}
307  static constexpr uint64_t get_resetValue(const void*){return 0;}
308  static constexpr uint64_t get_resetMask(const void*){return ~0;}
309  static constexpr ModifiedWriteValues get_modifiedWriteValues(const void*){return static_cast<ModifiedWriteValues>(0);}
310  static constexpr ReadAction get_readAction(const void*){return static_cast<ReadAction>(0);}
311 };
312 
313 
314 
315 template<typename STRREF,typename STR,typename V>
316 struct normalizer;
317 }}}
318 
319 #endif // __NODAMUSHI_SVD_NORMALIZED_NODE_CONTAINER_HPP__
boxvec. vector<pointer<X>>
uint64_t calc_address(const O &o, uint64_t offset=0)
uint64_t get_default_resetValue(const O &o) noexcept
static ModifiedWriteValues get_modifiedWriteValues(T p)
Define <protection> enum.
static constexpr ReadAction get_readAction(const void *)
std::shared_ptr< T > node_ptr
ModifiedWriteValues get_default_modifiedWriteValues(const O &o)
uint32_t get_default_size(const O &o) noexcept
Define <readAction> enum.
vec_t::iterator ptr_begin() noexcept
Definition: boxvec.hpp:243
optional like container
size_type size() const noexcept
Definition: boxvec.hpp:137
uint64_t get_default_resetMask(const O &o)
node_ptr< T > make_node_ptr(ARGS &&... args)
Protection get_default_protection(const O &o) noexcept
path< STR > & add_name(string_ref p)
Definition: path.hpp:206
const std::string & string_ref
Definition: string_type.hpp:44
static constexpr uint64_t get_resetValue(const void *)
static constexpr uint64_t get_address(const void *)
static constexpr ModifiedWriteValues get_modifiedWriteValues(const void *)
<access> enum
Access get_default_access(const O &o) noexcept
DataType get_default_dataType(const O &r) noexcept
void update_parent_of_children(list< T > &l, node_ptr< You > &you)
ReadAction get_default_readAction(const O &o)
normalized visitor
std::weak_ptr< Parent > parent_ptr
static constexpr Protection get_protection(const void *)
Access
Read/Write Access type enum.<access> Read-Write/Read-Only/Write-Only/Write-Once/Read- Write Once.
Definition: Access.hpp:26
void path_helper(const O &o,::nodamushi::svd::path< S > &path)
vec_t::iterator ptr_end() noexcept
Definition: boxvec.hpp:245
static constexpr Access get_access(const void *)
Access get_default_fieldaccess(const O &o) noexcept
normalized peripheral element
vector<pointer<T>>.
Definition: boxvec.hpp:75
Security Attribution Unit(SAU).
Definition: Cpu.hpp:58
DataType
register data type
Definition: DataType.hpp:28
names,dispalyNames->name0,displayName1
Protection
<protection> enum
Definition: Protection.hpp:23
uint64_t calc_offset(const O &o, uint64_t offset=0)
normalized field element
Definition: Field.hpp:32
node_ptr< R > __find_helper(list< R > &v, string_ref n)
dot '.' separeted path.
Definition: path.hpp:16
Define <modifiedWriteValues> enum.
node_ptr< Peripheral< STRREF > > find_parent_peripheral(O< STRREF > &o)
dimension information
ReadAction
<register>.<readAction> enum
Definition: ReadAction.hpp:26
Define <dataType> enum.
static constexpr uint64_t get_resetMask(const void *)
normalized <enumeratedValues> element. iterable
Definition: Enumeration.hpp:27
static constexpr uint32_t get_size(const void *)