MFMv2.0.10
Movable Feast Machine Simulator 2.0.10
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
VD.h
Go to the documentation of this file.
1 /* -*- mode:C++ -*- */
2 /*
3  VD.h A description of a typed, sized, bounded, positioned value
4  Copyright (C) 2014 The Regents of the University of New Mexico. All rights reserved.
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either
9  version 2.1 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this library; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
19  USA
20 */
21 
29 #ifndef VD_H
30 #define VD_H
31 
32 #include "itype.h"
33 #include "Fail.h"
34 #include "Util.h"
35 
36 namespace MFM
37 {
38  template <class CC> class Atom; // FORWARD
39 
44  class VD
45  {
46  public:
51  enum Type {
52  INVALID=0,
53  U32,
54  S32,
55  BOOL,
56  UNARY,
57  BITS,
58  TYPE_COUNT
59  };
60 
61  static bool ValidType(u32 type)
62  {
63  return type > INVALID && type < TYPE_COUNT;
64  }
65 
66  static void AssertValidType(u32 type)
67  {
68  if (!ValidType(type))
69  {
70  FAIL(ILLEGAL_ARGUMENT);
71  }
72  }
73 
74  static const char *(m_typeNames[TYPE_COUNT]);
75 
76  static const char * GetTypeName(u32 t)
77  {
78  if (!ValidType(t))
79  {
80  t = INVALID;
81  }
82  return m_typeNames[t];
83  }
84 
85  const u32 m_type;
86  const u32 m_length;
87  const u32 m_start;
88 
89  const s32 m_min;
90  const s32 m_vdef;
91  const s32 m_max;
92 
93  const u64 m_longdef;
94 
95  static s32 GetMin(u32 type, u32 bits)
96  {
97  switch (type)
98  {
99  case U32: return 0;
100  case S32: return bits==0 ? 0 : -MakeMask(bits-1) - 1;
101  case BOOL: return 0;
102  case UNARY: return 0;
103  case BITS: // FALL THROUGH
104  default: FAIL(ILLEGAL_ARGUMENT);
105  }
106  }
107 
108  static s32 GetMax(u32 type, u32 bits)
109  {
110  switch (type)
111  {
112  case U32: return MakeMask(bits > 31 ? 31 : bits); // Not 32 because we use all-s32
113  case S32: return MakeMask(bits-1);
114  case BOOL: return 1;
115  case UNARY: return bits;
116  case BITS: // FALL THROUGH
117  default: FAIL(ILLEGAL_ARGUMENT);
118  }
119  }
120 
121  bool InRangeByLength(s32 val) const
122  {
123  return GetMin(m_type, m_length) <= val && GetMax(m_type, m_length) >= val;
124  }
125 
126  void AssertInRangeByLength(s32 val) const
127  {
128  if (!InRangeByLength(val))
129  {
130  FAIL(OUT_OF_BOUNDS);
131  }
132  }
133 
134  u32 GetType() const
135  {
136  return m_type;
137  }
138 
139  s32 GetMin() const
140  {
141  return m_min;
142  }
143 
144  s32 GetMax() const
145  {
146  return m_max;
147  }
148 
149  s32 GetDefault() const
150  {
151  return m_vdef;
152  }
153 
154  u64 GetLongDefault() const
155  {
156  return m_longdef;
157  }
158 
159  bool InRange(s32 val) const
160  {
161  return m_min <= val && m_max >= val;
162  }
163 
164  void AssertInRange(s32 val) const
165  {
166  if (!InRange(val))
167  {
168  FAIL(OUT_OF_BOUNDS);
169  }
170  }
171 
172  VD(u32 type, u32 len, u32 pos, s32 min, s32 vdef, s32 max)
173  : m_type(type), m_length(len), m_start(pos), m_min(min), m_vdef(vdef), m_max(max), m_longdef(0)
174  {
175  AssertValidType(m_type);
176  if (m_length > 32 || m_type == BITS)
177  {
178  FAIL(ILLEGAL_ARGUMENT);
179  }
180  AssertInRangeByLength(m_min);
181  AssertInRangeByLength(m_max);
182  AssertInRange(m_vdef);
183  }
184 
185  VD(u32 type, u32 len, u32 pos, u64 longdef)
186  : m_type(type), m_length(len), m_start(pos), m_min(0), m_vdef(0), m_max(0), m_longdef(longdef)
187  {
188  if (m_type != BITS || m_length > 64)
189  {
190  FAIL(ILLEGAL_ARGUMENT);
191  }
192  }
193 
194  void AssertIsType(u32 type) const
195  {
196  if (m_type != type)
197  {
198  FAIL(ILLEGAL_ARGUMENT);
199  }
200  }
201 
202  static u32 MakeMask(u32 length)
203  {
204  return _GetNOnes32(length);
205  }
206 
213 
216  template <class CC> static u32 GetFieldAsBits(const u32 length, const u32 start, const typename CC::ATOM_TYPE & a) ;
217  template <class CC> static void SetFieldAsBits(const u32 length, const u32 start, typename CC::ATOM_TYPE & a, const u32 val) ;
218 
219  template <class CC> static u64 GetLongFieldAsBits(const u32 length, const u32 start, const typename CC::ATOM_TYPE & a) ;
220  template <class CC> static void SetLongFieldAsBits(const u32 length, const u32 start, typename CC::ATOM_TYPE & a, const u64 val) ;
221 
227  template <class CC> s32 GetBitsAsS32(const typename CC::ATOM_TYPE & a) const ;
228 
234  template <class CC> void SetBitsAsS32(typename CC::ATOM_TYPE & a, s32 val) const ;
235 
240  template <class CC> u64 GetBitsAsU64(const typename CC::ATOM_TYPE & a) const ;
241 
246  template <class CC> void SetBitsAsU64(typename CC::ATOM_TYPE & a, u64 val) const ;
247 
250  template <class CC> static u32 GetFieldAsU32(const u32 length, const u32 start, const typename CC::ATOM_TYPE & a) ;
251  template <class CC> static void SetFieldAsU32(const u32 length, const u32 start, typename CC::ATOM_TYPE & a, const u32 val) ;
252  template <class CC> u32 GetValueU32(const typename CC::ATOM_TYPE & a) const ;
253  template <class CC> void SetValueU32(typename CC::ATOM_TYPE & a, const u32 val) const ;
254 
257  template <class CC> static s32 GetFieldAsS32(const u32 length, const u32 start, const typename CC::ATOM_TYPE & a) ;
258  template <class CC> static void SetFieldAsS32(const u32 length, const u32 start, typename CC::ATOM_TYPE & a, const s32 val) ;
259  template <class CC> s32 GetValueS32(const typename CC::ATOM_TYPE & a) const ;
260  template <class CC> void SetValueS32(typename CC::ATOM_TYPE & a, const s32 val) const ;
261 
264  template <class CC> static bool GetFieldAsBool(const u32 length, const u32 start, const typename CC::ATOM_TYPE & a) ;
265  template <class CC> static void SetFieldAsBool(const u32 length, const u32 start, typename CC::ATOM_TYPE & a, const bool val) ;
266  template <class CC> bool GetValueBool(const typename CC::ATOM_TYPE & a) const ;
267  template <class CC> void SetValueBool(typename CC::ATOM_TYPE & a, const bool val) const ;
268 
271  template <class CC> static u32 GetFieldAsUnary(const u32 length, const u32 start, const typename CC::ATOM_TYPE & a) ;
272  template <class CC> static void SetFieldAsUnary(const u32 length, const u32 start, typename CC::ATOM_TYPE & a, const u32 val) ;
273  template <class CC> u32 GetValueUnary(const typename CC::ATOM_TYPE & a) const ;
274  template <class CC> void SetValueUnary(typename CC::ATOM_TYPE & a, const u32 val) const ;
275 
276  };
277 
278  template <VD::Type VTYPE> struct VTypeToType { /* INVALID WON'T COMPILE WITHOUT TYPE */ };
279  template<> struct VTypeToType<VD::U32> { typedef u32 TYPE; };
280  template<> struct VTypeToType<VD::S32> { typedef s32 TYPE; };
281  template<> struct VTypeToType<VD::BOOL> { typedef bool TYPE; };
282  template<> struct VTypeToType<VD::UNARY> { typedef u32 TYPE; };
283  template<> struct VTypeToType<VD::BITS> { typedef u64 TYPE; };
284 }
285 
286 #include "VD.tcc"
287 
288 #endif /* VD_H */
Definition: VD.h:44
static u32 GetFieldAsU32(const u32 length, const u32 start, const typename CC::ATOM_TYPE &a)
u32 value
Definition: VD.tcc:63
static bool GetFieldAsBool(const u32 length, const u32 start, const typename CC::ATOM_TYPE &a)
bool value
Definition: VD.tcc:117
s32 GetBitsAsS32(const typename CC::ATOM_TYPE &a) const
Definition: VD.tcc:43
void SetBitsAsU64(typename CC::ATOM_TYPE &a, u64 val) const
Definition: VD.tcc:37
Definition: VD.h:278
static u32 GetFieldAsBits(const u32 length, const u32 start, const typename CC::ATOM_TYPE &a)
Raw bits value.
Definition: VD.tcc:7
void SetBitsAsS32(typename CC::ATOM_TYPE &a, s32 val) const
Definition: VD.tcc:49
u64 GetBitsAsU64(const typename CC::ATOM_TYPE &a) const
Definition: VD.tcc:31
static s32 GetFieldAsS32(const u32 length, const u32 start, const typename CC::ATOM_TYPE &a)
s32 value
Definition: VD.tcc:89
Type
Definition: VD.h:51
static u32 GetFieldAsUnary(const u32 length, const u32 start, const typename CC::ATOM_TYPE &a)
unary value
Definition: VD.tcc:144