NSVD Reader  0.0.1
Register.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_REGISTER_HPP__
10 #define __NODAMUSHI_SVD_NORMALIZED_REGISTER_HPP__
11 
12 # include <sstream>
15 
16 namespace nodamushi{
17 namespace svd{
18 namespace normalized{
19 
20 
21 /*
22  * Normalized register class.
23  * if STRREF is string_view or something like it,
24  * the reference source of string is the value of nodamushi::svd::Register instance member.
25  *
26  * @see http://www.keil.com/pack/doc/CMSIS/SVD/html/elem_registers.html#elem_register
27  */
28 
29 template<typename STRREF>struct Register
30 {
37 private:
38  p_ptr parent;
39  p2_ptr parent2;
40 
41 public:
42 
49  std::string name;
51  std::string displayName;
55  STRREF description;
69  uint64_t addressOffset;
115 
116  //-----------------------------------------------------------
120  std::string get_name_with_appendix()const noexcept{
121  auto p = get_peripheral();
122  if(p && p->has_register_name_appendix()){
123  std::stringstream s;
124  if(! p->prependToName.empty())
125  s << p->prependToName;
126  if(p->appendToName.empty())
127  s << name;
128  else{
129  bool arr=dim.is_array() && name.length() > 3;
130  if(arr){
131  size_t l = name.length()-3;
132  while(l != 1){
133  if(name[l] == '[')break;
134  l--;
135  }
136  if(name[l] == '['){
137  s.write(name.data(),l);
138  }else{
139  s << name;
140  arr = false;
141  }
142  }else
143  s << name;
144  s << p->appendToName;
145  if(arr)
146  s <<'['<<dim.index << ']';
147  }
148  return s.str();
149  }
150  return name;
151  }
152 
153 
154 
160  node_ptr<Peripheral> get_parent() noexcept{return parent.lock();}
162  node_ptr<Peripheral const> get_parent()const noexcept{return parent.lock();}
163 
169  node_ptr<Cluster> get_parent2() noexcept{return parent2.lock();}
171  node_ptr<Cluster const> get_parent2()const noexcept{return parent2.lock();}
177  uint64_t get_address()const{ return calc_address(*this);}
179  uint64_t get_address_offset()const{ return calc_offset(*this);}
182  {
184  path_helper(*this,p);
185  return p;
186  }
191  DataType get_dataType()const{
192  return dataType.get<this_t,get_default_dataType<this_t>>(this);
193  }
195  uint32_t get_size()const{
196  return size.get<this_t,get_default_size<this_t>>(this);
197  }
199  uint64_t get_resetValue()const{
200  return resetValue.get<this_t,get_default_resetValue<this_t>>(this);
201  }
203  uint64_t get_resetMask()const{
204  return resetMask.get<this_t,get_default_resetMask<this_t>>(this);
205  }
207  Access get_access()const{
208  return access.get<this_t,get_default_access<this_t>>(this);
209  }
211  Protection get_protection()const{
212  return protection.get<this_t,get_default_protection<this_t>>(this);
213  }
216  ReadAction get_readAction()const{return readAction;}
217 
218  //-------------------------------------------
225  template<typename STR>
226  node_ptr<void> find(const ::nodamushi::svd::path<STR>& p,size_t pindex=0)
227  {
228  const size_t ps = p.size();
229  if(ps > pindex){
230  string_ref n = p[pindex];
231  if(auto c = __find_helper(fields,n)){
232  if(ps == pindex + 1)return c;
233  else return c->find(p,pindex+1);
234  }
235  }
236  return {};
237  }
244  template<typename STR>
246  {
247  const size_t ps = p.size();
248  if(ps == pindex+1){
249  string_ref n = p[pindex];
250  return __find_helper(fields,n);
251  }
252  return {};
253  }
254 
255  //-------------------------------------------
256 
257 
258  template<typename T>
259  Register(const T& n)://don't change name
260  parent(),
261  parent2(),
262  derivedFrom(),
263  name(n.name),
265  dim(n.dim),
279  fields(n.fields.size())
280  {
281  if(n.svd.derivedFrom)
282  derivedFrom = n.svd.derivedFrom.get();
283 
284  // add dim offset
285  if(n.dim && n.dim.index!=0){
286  __NORMALIZED_DERIVED_FROM_HELPER(dimIncrement);
287  size_t inc = 1;
288  if(dimIncrement)
289  inc = *dimIncrement;
290  addressOffset += inc * n.dim.index;
291  }
292 
293  for(const auto& f:n.fields)
294  fields.emplace_back(f);
295  // sort by lsb
296  fields.sort([](const Field& a,const Field& b)->bool
297  {return a.lsb() < b.lsb();});
298 
299  }
300  void update_parent(p_ptr& new_parent,node_ptr<this_t>& me)
301  {
302  parent = new_parent;
303  parent2= {};
305  }
306  void update_parent(p2_ptr& new_parent,node_ptr<this_t>& me)
307  {
308  parent2= new_parent;
309  parent = {};
311  }
312 
313 };
314 //---------- Visitor --------------------
316 {
317  namespace nv = ::nodamushi::visitor;
318  using r = nv::result;
319  r ret;
320 
321  // visit register / cluster
322  ret = CONTROLLER::apply(t.fields,v);
323  if(ret == r::BREAK)return ret;
324  if(ret == r::SKIP)return r::CONTINUE;
325  return r::CONTINUE;
326 }};
327 //--------------------------------------------
328 
329 
330 }}} // end namespace svd
331 
332 #endif // __NODAMUSHI_SVD_NORMALIZED_REGISTER_HPP__
normalized <writeConstraint> element
uint64_t calc_address(const O &o, uint64_t offset=0)
auto make_dim_displayName_helper(const NODE &n) -> dim_name_helper< typename decltype(n.displayName)::type >
Definition: dim_helper.hpp:122
std::shared_ptr< T > node_ptr
node_ptr< Peripheral const > get_peripheral() const noexcept
find the Peripheral to which this register belongs.
Definition: Register.hpp:173
#define __NORMALIZED_DERIVED_FROM(name)
__NORMALIZED_DERIVED_FROM(name)
__NX_NORM_HANDLE_VISIT(Enumeration)
Definition: Enumeration.hpp:90
size_t size() const noexcept
Definition: path.hpp:218
dim_info dim
dimemsion information object.
Definition: Register.hpp:53
uint64_t get_address_offset() const
a relative address from the baseAddress of the peripheral
Definition: Register.hpp:179
STRREF description
<description>
Definition: Register.hpp:55
uint32_t get_size() const
resolve the value of size and return it.
Definition: Register.hpp:194
nullable< uint64_t > resetMask
<resetMask>
Definition: Register.hpp:94
Protection get_protection() const
resolve the value of protection and return it.
Definition: Register.hpp:210
nullable< DataType > dataType
<dataType>
Definition: Register.hpp:99
WriteConstraint writeConstraint
<writeConstraint>
Definition: Register.hpp:105
nullable< uint32_t > size
<size>
Definition: Register.hpp:74
const std::string & string_ref
Definition: string_type.hpp:44
STRREF alternateGroup
<alternateGroup>
Definition: Register.hpp:57
parent_ptr< Cluster > p2_ptr
Definition: Register.hpp:36
path< STRREF > derivedFrom
derivedFrom attribute
Definition: Register.hpp:44
node_ptr< void > find(const ::nodamushi::svd::path< STR > &p, size_t pindex=0)
find path element
Definition: Register.hpp:225
constexpr bool is_array() const noexcept
is the dimension type array
Definition: dim_info.hpp:43
node_ptr< Peripheral > get_peripheral() noexcept
find the Peripheral to which this register belongs.
Definition: Register.hpp:175
::nodamushi::svd::path get_path() const
get path
Definition: Register.hpp:180
uint64_t get_resetValue() const
resolve the value of resetValue and return it.
Definition: Register.hpp:198
ReadAction readAction
<readAction>
Definition: Register.hpp:103
STRREF alternateRegister
<alternateRegister>
Definition: Register.hpp:59
container class decralation
node_ptr< Cluster const > get_parent2() const noexcept
parent cluster pointer or nullptr.
Definition: Register.hpp:171
ModifiedWriteValues get_modifiedWriteValues() const
resolve the value of modifiedWriteValues and return it.
Definition: Register.hpp:214
Access get_access() const
resolve the value of access and return it.
Definition: Register.hpp:206
node_ptr< Cluster > get_parent2() noexcept
get parent cluster pointer
Definition: Register.hpp:169
void update_parent_of_children(list< T > &l, node_ptr< You > &you)
std::weak_ptr< Parent > parent_ptr
list< Field > fields
<field> elements list. This list is sorted by lsb of field. Field class does not prohibit copying,...
Definition: Register.hpp:114
DataType get_dataType() const
resolve the dataType. The default type is determined by the size.(size=8 : uint8_t,...
Definition: Register.hpp:190
ModifiedWriteValues modifiedWriteValues
<modifiedWriteValues>
Definition: Register.hpp:101
normalized <writeConstraint>.<range> element
nullable< uint64_t > resetValue
<resetValue>
Definition: Register.hpp:89
T get(T defaultValue) const noexcept
Definition: nullable.hpp:70
unsigned int lsb() const
get lsb bit index.
Definition: Field.hpp:84
result apply(T &t)
Definition: imple.hpp:197
uint64_t get_address() const
calculate an absolute address
Definition: Register.hpp:177
nullable< Protection > protection
<protection>
Definition: Register.hpp:84
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
std::string name
<name> of this register without peripheral.prependToName and peripheral.appendToName
Definition: Register.hpp:49
void path_helper(const O &o,::nodamushi::svd::path< S > &path)
parent_ptr< Peripheral > p_ptr
Definition: Register.hpp:35
normalized peripheral element
vector<pointer<T>>.
Definition: boxvec.hpp:75
DataType
register data type
Definition: DataType.hpp:28
#define __NORMALIZED_DERIVED_FROM_MEMBER(name)
Protection
<protection> enum
Definition: Protection.hpp:23
node_ptr< Peripheral const > get_parent() const noexcept
Definition: Register.hpp:162
std::string displayName
<displayName>
Definition: Register.hpp:51
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)
uint64_t get_resetMask() const
resolve the value of resetMask and return it.
Definition: Register.hpp:202
std::string get_name_with_appendix() const noexcept
get name with peripheral.prependToName and peripheral.appendToName
Definition: Register.hpp:120
node_ptr< Peripheral< STRREF > > find_parent_peripheral(O< STRREF > &o)
node_ptr< Peripheral > get_parent() noexcept
get parent cluster pointer
Definition: Register.hpp:160
ReadAction
<register>.<readAction> enum
Definition: ReadAction.hpp:26
uint64_t addressOffset
<addressOffset>
Definition: Register.hpp:69
#define __NORMALIZED_DERIVED_FROM_HELPER(name)
node_ptr< Field > find_field(::nodamushi::svd::path< STR > &p, size_t pindex=0)
find field element
Definition: Register.hpp:244
void update_parent(p_ptr &new_parent, node_ptr< this_t > &me)
Definition: Register.hpp:299
nullable< Access > access
<access>
Definition: Register.hpp:79