NSVD Reader  0.0.1
Peripheral.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_PERIPHERAL_HPP__
10 #define __NODAMUSHI_SVD_NORMALIZED_PERIPHERAL_HPP__
11 
12 # include <vector>
13 # include <memory>
14 # include <string>
15 
16 # include "nodamushi/svd/path.hpp"
22 
24 
25 
26 namespace nodamushi{
27 namespace svd{
28 namespace normalized{
29 
35 template<typename STRREF>struct Peripheral
36 {
43 
44  private:
45  p_ptr parent;
46  public:
50  std::string name;
54  STRREF version;
56  STRREF description;
60  STRREF groupName;
69  STRREF prependToName;
78  STRREF appendToName;
84  uint64_t baseAddress;
111  std::vector<AddressBlock> addressBlock;
116  std::vector<Interrupt> interrupt;
117 
138  //-------------------------------------------
145  template<typename STR>
147  {
148  const size_t ps = p.size();
149  if(ps > pindex){
150  string_ref n = p[pindex];
151  if(auto c = __find_helper(clusters,n)){
152  if(ps == pindex +1)return c;
153  else return c->find(p,pindex+1);
154  }
155  if(auto c = __find_helper(registers,n)){
156  if(ps == pindex +1)return c;
157  else return c->find(p,pindex+1);
158  }
159  }
160  return {};
161  }
168  template<typename STR>
170  {
171  const size_t ps = p.size();
172  if(ps > pindex){
173  string_ref n = p[pindex];
174  if(auto c = __find_helper(clusters,n)){
175  if(ps == pindex +1)return c;
176  else return c->find_cluster(p,pindex+1);
177  }
178  }
179  return {};
180  }
187  template<typename STR>
189  {
190  const size_t ps = p.size();
191  if(ps > pindex){
192  string_ref n = p[pindex];
193  if(ps > pindex + 1){
194  if(auto c = __find_helper(clusters,n))
195  return c->find_register(p,pindex+1);
196  }else if(ps == pindex + 1){
197  return __find_helper(registers,n);
198  }
199  }
200  return {};
201  }
208  template<typename STR>
210  {
211  const size_t ps = p.size();
212  if(ps > pindex+1){
213  string_ref n = p[pindex];
214  if(ps > pindex + 2){
215  if(auto c = __find_helper(clusters,n))
216  return c->find_field(p,pindex+1);
217  }else if(ps == pindex + 2){
218  if(auto c = __find_helper(registers,n))
219  return c->find_field(p,pindex+1);
220  }
221  }
222  return {};
223  }
224 
225  //-------------------------------------------
226 
230  bool has_register_name_appendix()const noexcept
231  {
232  return !prependToName.empty() || !appendToName.empty();
233  }
235  auto get_parent()const noexcept{return parent.lock();}
237  auto get_parent()noexcept{return parent.logk();}
239  uint64_t get_address()const{return baseAddress;}
241  uint32_t get_size()const{
242  return size.get<this_t,get_default_size<this_t>>(this);
243  }
245  uint64_t get_resetValue()const{
246  return resetValue.get<this_t,get_default_resetValue<this_t>>(this);
247  }
249  uint64_t get_resetMask()const{
250  return resetMask.get<this_t,get_default_resetMask<this_t>>(this);
251  }
254  return access.get<this_t,get_default_access<this_t>>(this);
255  }
258  return protection.get<this_t,get_default_protection<this_t>>(this);
259  }
262  {
264  p.add_name(name);
265  return p;
266  }
267 
268 
269  template<typename T>
270  Peripheral(const T& n): // don't change name
271  parent(),
272  derivedFrom(),
273  name(n.name),
274  dim(n.dim),
275  __NORMALIZED_DERIVED_FROM(version), // see derived_from_helper.hpp
289  addressBlock{},
290  interrupt{},
291  clusters(n.clusters.size()),
292  registers(n.registers.size())
293  {
294  if(n.svd.derivedFrom)
295  derivedFrom = *n.svd.derivedFrom;
296 
297  // add dim offset
298  if(n.dim&& n.dim.index!=0){
299  __NORMALIZED_DERIVED_FROM_HELPER(dimIncrement);
300  size_t inc = 1;
301  if(dimIncrement)
302  inc = *dimIncrement;
303  baseAddress += inc * n.dim.index;
304  }
305 
306  //----------------------------------------
307  // addressBlock
308  //----------------------------------------
309  size_t addrBlockSize = 0;
310 
311  if(n.svd.addressBlock)
312  addrBlockSize = n.svd.addressBlock.get().size();
313 
314  if(n.derivedFrom){
315  const auto* ptr = n.derivedFrom;
316  while(ptr){
317  const auto& v = ptr->svd.addressBlock;
318  if(v) addrBlockSize += v.get().size();
319  ptr = ptr->derivedFrom;
320  }
321  }
322 
323  addressBlock.reserve(addrBlockSize);
324 
325  if(n.svd.addressBlock){
326  for(const auto& s:*n.svd.addressBlock)
327  addressBlock.emplace_back(s);
328  }
329 
330  if(n.derivedFrom){
331  const auto* ptr = n.derivedFrom;
332  while(ptr){
333  if(ptr->svd.addressBlock){
334  for(const auto& s:ptr->svd.addressBlock.get())
335  addressBlock.emplace_back(s);
336  }
337  ptr = ptr->derivedFrom;
338  }
339  }
340 
341  //----------------------------------------
342  // interrupt
343  // derivedFrom property is ignored
344  //----------------------------------------
345  if(n.svd.interrupt){
346  auto& d = interrupt;
347  const auto& v = *n.svd.interrupt;
348  d.reserve(v.size());
349  for(const auto& s:v)
350  d.emplace_back(s);
351  }
352  //----------------------------------------
353  // children
354  for(const auto& c:n.clusters){
355  clusters.emplace_back(c);
356  }
357  for(const auto& c:n.registers)
358  registers.emplace_back(c);
359  // sort by address
360  clusters.sort([](const Cluster& a,const Cluster& b)->bool
361  {return a.addressOffset < b.addressOffset;});
362  registers.sort([](const Register& a,const Register& b)->bool
363  {return a.addressOffset < b.addressOffset;});
364  }
365 
367  constexpr void* get_parent2()const noexcept{return nullptr;}
369  void update_parent(p_ptr& new_parent,node_ptr<this_t>& me)
370  {
371  parent = new_parent;
374  }
375 
376 };
377 
378 
379 //---------- Visitor --------------------
381 {
382  namespace nv = ::nodamushi::visitor;
383  using r = nv::result;
384  r ret;
385  ret = CONTROLLER::apply(t.interrupt,v);
386  if(ret == r::BREAK)return ret;
387  if(ret == r::SKIP)return r::CONTINUE;
388 
389  ret = CONTROLLER::apply(t.addressBlock,v);
390  if(ret == r::BREAK)return ret;
391  if(ret == r::SKIP)return r::CONTINUE;
392 
393  // visit register / cluster
394  ret = CONTROLLER::apply(t.clusters,v);
395  if(ret == r::BREAK)return ret;
396  if(ret == r::SKIP)return r::CONTINUE;
397 
398  ret = CONTROLLER::apply(t.registers,v);
399  if(ret == r::BREAK)return ret;
400  return r::CONTINUE;
401 }};
402 //--------------------------------------------
403 
404 
405 }}} // end namespace svd
406 
407 #endif // __NODAMUSHI_SVD_NORMALIZED_PERIPHERAL_HPP__
std::vector< Interrupt > interrupt
<interrupt> elements.
Definition: Peripheral.hpp:116
list< Register > registers
<register> elements list.This list is sorted by the addressOffset.
Definition: Peripheral.hpp:137
Access get_access() const
resolve the value of access and return it.
Definition: Peripheral.hpp:253
::nodamushi::svd::normalized::Cluster< STRREF > Cluster
Definition: Peripheral.hpp:40
std::shared_ptr< T > node_ptr
helper for copy derivedFrom element
uint64_t get_address() const
get baseAddress
Definition: Peripheral.hpp:239
nullable< uint64_t > resetMask
<resetMask>
Definition: Peripheral.hpp:109
constexpr void * get_parent2() const noexcept
always return nullptr
Definition: Peripheral.hpp:366
#define __NORMALIZED_DERIVED_FROM(name)
__NORMALIZED_DERIVED_FROM(name)
nullable< uint32_t > size
<size>
Definition: Peripheral.hpp:89
__NX_NORM_HANDLE_VISIT(Enumeration)
Definition: Enumeration.hpp:90
size_t size() const noexcept
Definition: path.hpp:218
uint64_t get_resetValue() const
resolve the value of resetValue and return it.
Definition: Peripheral.hpp:245
path< STRREF > derivedFrom
derivedFrom attribute
Definition: Peripheral.hpp:48
auto get_parent() const noexcept
get parent Device pointer
Definition: Peripheral.hpp:235
list< Cluster > clusters
<cluster> elements list.This list is sorted by the addressOffset.
Definition: Peripheral.hpp:127
optional like container
STRREF alternatePeripheral
<alternatePeripheral>
Definition: Peripheral.hpp:58
node_ptr< Register > find_register(::nodamushi::svd::path< STR > &p, size_t pindex=0)
find register element
Definition: Peripheral.hpp:188
nullable< uint64_t > resetValue
<resetValue>
Definition: Peripheral.hpp:104
path< STR > & add_name(string_ref p)
Definition: path.hpp:206
const std::string & string_ref
Definition: string_type.hpp:44
Protection get_protection() const
resolve the value of protection and return it.
Definition: Peripheral.hpp:257
uint32_t get_size() const
resolve the value of size and return it.
Definition: Peripheral.hpp:241
STRREF headerStructName
<headerStructName>
Definition: Peripheral.hpp:80
uint64_t get_resetMask() const
resolve the value of resetMask and return it.
Definition: Peripheral.hpp:249
::nodamushi::svd::normalized::Register< STRREF > Register
Definition: Peripheral.hpp:39
node_ptr< Cluster > find_cluster(::nodamushi::svd::path< STR > &p, size_t pindex=0)
find cluster element
Definition: Peripheral.hpp:169
container class decralation
void update_parent_of_children(list< T > &l, node_ptr< You > &you)
svd element path
std::weak_ptr< Parent > parent_ptr
normalized AddressBlock
T get(T defaultValue) const noexcept
Definition: nullable.hpp:70
bool has_register_name_appendix() const noexcept
prependToName or appendToName is not empty
Definition: Peripheral.hpp:230
result apply(T &t)
Definition: imple.hpp:197
void update_parent(p_ptr &new_parent, node_ptr< this_t > &me)
Definition: Peripheral.hpp:368
std::vector< AddressBlock > addressBlock
<addressBlock> emenents
Definition: Peripheral.hpp:111
Access
Read/Write Access type enum.<access> Read-Write/Read-Only/Write-Only/Write-Once/Read- Write Once.
Definition: Access.hpp:26
dim,dimIndex,array information object.
Definition: dim_info.hpp:27
parent_ptr< Device< STRREF > > p_ptr
Definition: Peripheral.hpp:38
dim_info dim
dimemsion information object.
Definition: Peripheral.hpp:52
normalized interrupt element
normalized peripheral element
auto get_parent() noexcept
get parent Device pointer
Definition: Peripheral.hpp:237
normalized interrupt element
Definition: Interrupt.hpp:23
vector<pointer<T>>.
Definition: boxvec.hpp:75
STRREF prependToName
<prependToName>
Definition: Peripheral.hpp:69
::nodamushi::svd::path get_path() const
get path
Definition: Peripheral.hpp:260
Protection
<protection> enum
Definition: Protection.hpp:23
node_ptr< void > find(::nodamushi::svd::path< STR > &p, size_t pindex=0)
find path element
Definition: Peripheral.hpp:146
normalized field element
Definition: Field.hpp:32
node_ptr< R > __find_helper(list< R > &v, string_ref n)
nullable< Protection > protection
<protection>
Definition: Peripheral.hpp:99
STRREF disableCondition
<disableCondition>
Definition: Peripheral.hpp:82
dimension information
#define __NORMALIZED_DERIVED_FROM_HELPER(name)
node_ptr< Field > find_field(::nodamushi::svd::path< STR > &p, size_t pindex=0)
find field element
Definition: Peripheral.hpp:209
nullable< Access > access
<access>
Definition: Peripheral.hpp:94