KM3NeT CLB  2.0
KM3NeT CLB v2 Embedded Software
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
varid.h
Go to the documentation of this file.
1 /*
2  * KM3NeT CLB v2 Firmware
3  * ----------------------
4  *
5  * Copyright 2013 KM3NeT Collaboration
6  *
7  * All Rights Reserved.
8  *
9  *
10  * File : var.h
11  * Created : 26 feb. 2014
12  * Author : Vincent van Beveren
13  */
14 
15 #ifndef VARID_H_
16 #define VARID_H_
17 
18 #include "util/macro.h"
19 #include "stdbool.h"
20 /**
21  * @file
22  *
23  * @ingroup pv
24  *
25  * Defines the variable ID format. A 32 bit variable ID actually contains meta data so
26  * introspection can be down without need for additional information.
27  *
28  * The following information is encoded:
29  *
30  * Bit(s) Name Description
31  * 30..28 SubSys Subsystem ID
32  * 0: SYS, 1: CLB, 2: NET,
33  * 3: OPT, 4: INS, 5: OPT,
34  * 6: BSE 7: reserved
35  * 27..20 VarIdx Subsystem variable index (max = 255)
36  * 19..18 BaseType Basic Type
37  * 0: unsigned int, 1: signed int,
38  * 2: floating point/bool, 3: reserved
39  * 17..16 ScalarSize Size in bytes: 2^ScalarSize, or:
40  * 0: 1 byte, 1: 2 bytes,
41  * 2: 4 bytes, 4: 8 bytes
42  * 19..16* ScalarType Combination of BaseType and ScalarSize make Scalar type.
43  * 15 Virtual Variable is virtual
44  * 14 Readable Variable can be read
45  * 13 Writable Variable can be written
46  * 12 Config Variable is a configuration variable
47  * 11..0 ArraySize When 0 - not an array,
48  * Else the actual array size ArraySize + 1
49  * maximum array size = 2 ^ 12, or 4096
50  *
51  * Total size of the variable is 2^ScalarSize * (ArraySize + 1).
52  */
53 
54 #define VID_SUBSYS_MASK 0x70000000
55 #define VID_SUBSYS_SHIFT 28
56 #define VID_VARIDX_MASK 0x0FF00000
57 #define VID_VARIDX_SHIFT 20
58 
59 #define VID_BASETYP_MASK 0x000C0000
60 #define VID_BASETYP_SHIFT 18
61 #define VID_SCLSIZE_MASK 0x00030000
62 #define VID_SCLSIZE_SHIFT 16
63 
64 #define VID_SCLTYP_MASK 0x000F0000
65 #define VID_SCLTYP_SHIFT 16
66 
67 #define VID_SCLTYP_U8 0x0 //!< Unsigned 8 bit integer
68 #define VID_SCLTYP_U16 0x1 //!< Unsigned 16 bit integer
69 #define VID_SCLTYP_U32 0x2 //!< Unsigned 32 bit integer
70 #define VID_SCLTYP_U64 0x3 //!< Unsigned 64 bit integer
71 #define VID_SCLTYP_I8 0x4 //!< Signed 8 bit integer
72 #define VID_SCLTYP_I16 0x5 //!< Signed 16 bit integer
73 #define VID_SCLTYP_I32 0x6 //!< Signed 32 bit integer
74 #define VID_SCLTYP_I64 0x7 //!< Signed 64 bit integer
75 // F8 and F16 not supported
76 #define VID_SCLTYP_BOOL 0x8 //!< Boolean, encoded as a byte (0 - false, 1 - true)
77 #define VID_SCLTYP_F32 0xA //!< 32 bit IEEE floating point
78 #define VID_SCLTYP_F64 0xB //!< 64 bit IEEE double precision floating point
79 
80 #define VID_VIRTUAL BIT(15) //!< Indicates variable is virtual, does not really exist
81 #define VID_READABLE BIT(14) //!< Variable is writable
82 #define VID_WRITABLE BIT(13) //!< Variable is readable
83 #define VID_CONFIG BIT(12) //!< Config variables may not be writable in some states
84 
85 #define VID_ARRSIZE_MASK 0x00000FFF
86 #define VID_ARRSIZE_SHIFT 0
87 
88 /**
89  * Types names.
90  */
91 extern const char * const vidTypeNames[16];
92 
93 /**
94  * Returns the number of elements in the array of this variable.
95  *
96  * @param varID Variable ID
97  * @return The number of elements in the array, or 1 if its a scalar.
98  */
99 static inline int vidArrSize(int varID)
100 {
101  return ((varID & VID_ARRSIZE_MASK) >> VID_ARRSIZE_SHIFT) + 1;
102 }
103 
104 /**
105  * Returns the size of the scalar part of variable in bytes.
106  *
107  * @param varID Variable ID
108  * @return The size of the primitive type in bytes, e.g. U32 -> 4.
109  */
110 static inline int vidSclSize(int varID)
111 {
112  return 1 << ((varID & VID_SCLSIZE_MASK) >> VID_SCLSIZE_SHIFT);
113 }
114 
115 /**
116  * Returns the type of the variable.
117  *
118  * @param varID The variable
119  * @return The type ID of the varaible.
120  */
121 static inline int vidType(int varID) {
122  return (varID & VID_SCLTYP_MASK) >> VID_SCLTYP_SHIFT;
123 }
124 
125 /**
126  * Returns the total size of the variable. So if its an array its the size of the
127  * scalar times the size of the array.
128  *
129  * @param varID Variable ID
130  * @return Size of the complete variable in bytes
131  */
132 static inline int vidVarSize(int varID)
133 {
134  return vidSclSize(varID) * vidArrSize(varID);
135 }
136 
137 /**
138  * Checks if a variable is readable.
139  *
140  * @param varID the variable ID
141  *
142  * @retval true Its readable.
143  * @retval false Its not readable
144  */
145 static inline bool vidIsReadable(int varID)
146 {
147  return ( varID & VID_READABLE ) != 0;
148 }
149 
150 /**
151  * Checks if a variable is readable.
152  *
153  * @param varID the variable ID
154  *
155  * @retval true Its writable
156  * @retval false Its not writable
157  */
158 static inline bool vidIsWritable(int varID)
159 {
160  return ( varID & VID_WRITABLE ) != 0;
161 }
162 
163 
164 #endif /* VARID_H_ */
165 
static int vidVarSize(int varID)
Returns the total size of the variable.
Definition: varid.h:132
#define VID_WRITABLE
Variable is readable.
Definition: varid.h:82
static int vidArrSize(int varID)
Returns the number of elements in the array of this variable.
Definition: varid.h:99
static int vidType(int varID)
Returns the type of the variable.
Definition: varid.h:121
#define VID_READABLE
Variable is writable.
Definition: varid.h:81
static bool vidIsReadable(int varID)
Checks if a variable is readable.
Definition: varid.h:145
const char *const vidTypeNames[16]
Types names.
Definition: varid.c:17
Provides common macros.
static int vidSclSize(int varID)
Returns the size of the scalar part of variable in bytes.
Definition: varid.h:110
static bool vidIsWritable(int varID)
Checks if a variable is readable.
Definition: varid.h:158