blob: 066f6db212901ebe058075708071b7849286ca9c [file] [log] [blame]
Zane Shelley871adec2019-07-30 11:01:39 -05001#pragma once
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -05002
3/**
4 * @brief Models register.It does not contain target.
5 *
6 * This class stores the hash id and bit length of scom registers It models
7 * registers without maintaining target information. Instances of this class
8 * are shared across rule chip objects of same type.Once prd object model is
9 * built, instances of this register are saved in flyweight.These instances
10 * persist as long as prd object model survives.
11 */
12
Zane Shelley52cb1a92019-08-21 14:38:31 -050013#include <hei_includes.hpp>
14#include <register/hei_register.hpp>
15#include <util/hei_bit_string.hpp>
16
Zane Shelley871adec2019-07-30 11:01:39 -050017namespace libhei
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050018{
19
Zane Shelleyb77b5732019-08-30 22:01:06 -050020#if 0
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050021// Forward References
22class CHIP_CLASS;
23class MopsRegisterAccess;
24class ExtensibleChip;
Zane Shelleyb77b5732019-08-30 22:01:06 -050025#endif
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050026
Zane Shelley61565dc2019-09-18 21:57:10 -050027/**
28 * @brief Stores information (e.g. address, type, length, etc.) for an actual
29 * hardware register.
30 *
31 * Hardware access:
32 *
33 * Actual hardware access is defined by the user application via the user
34 * interface APIs. In order to tell the user application which chip to target,
35 * the user application gives the isolator pointers to its chip objects. As
36 * each chip needs to be accessed, the isolator must store the chip in a
37 * static variable defined in this class. The intended use is:
38 *
39 * - Call HardwareRegister::setAccessor() with the target chip.
40 * - Perform all necessary hardware accesses to that chip.
41 * - Call HardwareRegister::clearAccessor() to remove the chip access. This
42 * helps ensure we don't try to access the wrong chip.
43 */
Zane Shelleycd36f432019-08-30 21:22:07 -050044class HardwareRegister : public Register
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050045{
46 public:
47
Zane Shelley61565dc2019-09-18 21:57:10 -050048#if 0
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050049 /**
50 * @brief constructor
51 * @param i_address address of the register
52 * @param i_bitLength bit length of register
53 * @param i_targetType target type associated with register
54 */
Zane Shelleycd36f432019-08-30 21:22:07 -050055 HardwareRegister( uint64_t i_address, uint32_t i_bitLength,
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050056 TARGETING::TYPE i_targetType, AccessLevel i_access ) :
Zane Shelley23244cb2019-08-30 21:12:12 -050057 Register(),
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050058 iv_bitLength( i_bitLength ),
59 iv_chipType( i_targetType ),
60 iv_scomAddress( i_address ),
61 iv_operationType( i_access )
62 {}
63
64 /**
65 * @brief constructor .Added this because we save object of this type in
66 * @ FlyweightS
67 */
Zane Shelleycd36f432019-08-30 21:22:07 -050068 HardwareRegister():
Zane Shelley23244cb2019-08-30 21:12:12 -050069 Register(),
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050070 iv_bitLength( 0 ),
71 iv_chipType( TARGETING::TYPE_NA ),
72 iv_scomAddress( 0 ),
73 iv_operationType( ACCESS_NONE )
74 {}
75
76 /**
77 * @brief Returns the pointer to bit string
78 * @param i_type attention type
79 * @return BitString * pointer to bit string
80 */
81
82 virtual const BitString * GetBitString(ATTENTION_TYPE i_type =
83 INVALID_ATTENTION_TYPE) const;
84 /**
85 * @brief Updates bit string contents associated with register
86 * @param i_bs poiner to bit string
87 * @return Nil
88 */
89
90 virtual void SetBitString(const BitString * i_bs) ;
91
92 /**
93 * @brief Returns length of the bits string associated with register
94 * @return length of bit string
95 */
96 uint32_t GetBitLength(void) const { return iv_bitLength ;}
Zane Shelley61565dc2019-09-18 21:57:10 -050097#endif
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050098
99 /**
Zane Shelley61565dc2019-09-18 21:57:10 -0500100 * @brief Reads a register from hardware via the user interface APIs.
101 * @param i_force When false, this function will only read from hardware if
102 * an entry for this instance does not already exist in the
103 * register cache. When true, the entry in the register
104 * cache is flushed, if it exists. Then this function will
105 * read from hardware and update the cache.
106 * @return See the return code from the registerRead() user interface API.
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500107 */
Zane Shelley61565dc2019-09-18 21:57:10 -0500108 ReturnCode read( bool i_force = false ) const;
109
110 #ifndef __HEI_READ_ONLY
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500111
112 /**
Zane Shelley61565dc2019-09-18 21:57:10 -0500113 * @brief Writes the value stored in the register cache to hardware via the
114 * user interface APIs.
115 * @return See the return code from the registerWrite() user interface API.
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500116 */
Zane Shelley61565dc2019-09-18 21:57:10 -0500117 ReturnCode write() const;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500118
Zane Shelley61565dc2019-09-18 21:57:10 -0500119 #endif // __HEI_READ_ONLY
120
121#if 0
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500122 /**
123 * @brief Returns the hash id of register
124 * @return returns hash id of register
125 * @pre None
126 * @post None
127 * @note
128 */
129 virtual uint16_t GetId(void) const { return iv_shortId; };
130
131 /**
132 * @brief Sets the hash id of register
133 * @param i_id hash id of register
134 * @return Nil
135 */
136 virtual void SetId(uint16_t i_id) { iv_shortId = i_id; };
137
138 /**
139 * @brief Returns type of Target associated with register.
140 * @return Refer to function description
141 */
142 TARGETING::TYPE getChipType()const{ return iv_chipType ;} ;
143 /**
144 * @brief Returns scom address of register
145 * @return Refer to function description
146 */
147 uint64_t GetAddress( ) const {return iv_scomAddress ;};
148 /**
149 * @brief compares two ScomRegisterAccess register for equality
150 * @param i_rightRegister register to be compared against
151 * @return Returns true if registers are equal false otherwise
152 */
Zane Shelleycd36f432019-08-30 21:22:07 -0500153 bool operator == ( const HardwareRegister & i_rightRegister ) const ;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500154 /**
155 * @brief defines < operation for ScomRegisterAccess
156 * @param i_rightRegister register to be compared against
157 * @return Returns false if i_rightRegisters is less and true otherwise
158 */
Zane Shelleycd36f432019-08-30 21:22:07 -0500159 bool operator < ( const HardwareRegister & i_rightRegister ) const ;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500160 /**
161 * @brief defines >= operation for ScomRegisterAccess
162 * @param i_rightRegister register to be compared against
163 * @return Returns true if registers is >= i_rightRegister false
164 * otherwise
165 */
Zane Shelleycd36f432019-08-30 21:22:07 -0500166 bool operator >= ( const HardwareRegister & i_rightRegister ) const;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500167
168 /** @return The register access level (see enum AccessLevel). */
169 virtual AccessLevel getAccessLevel() const { return iv_operationType; }
170
171 /** @brief Sets the register access level (see enum AccessLevel). */
172 virtual void setAccessLevel( AccessLevel i_op ) { iv_operationType = i_op; }
173
174 protected: // Functions
175
176 /**
177 * @brief copy constructor
178 * @param i_scomRegister scomRegister instance to be copied
179 */
Zane Shelleycd36f432019-08-30 21:22:07 -0500180 HardwareRegister( const Register & i_scomRegister ):
Zane Shelley23244cb2019-08-30 21:12:12 -0500181 Register(),
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500182 iv_bitLength( i_scomRegister.GetBitLength() ),
183 iv_shortId( i_scomRegister.GetId() ),
184 iv_chipType( i_scomRegister.getChipType() ),
185 iv_scomAddress( i_scomRegister.GetAddress() ),
186 iv_operationType( i_scomRegister.getAccessLevel() )
187 {}
188
189 /**
190 * @brief Returns reference to bit string associated with register
191 * @return Refer to function description
192 */
193 virtual BitString & AccessBitString( );
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500194
195private: // functions
196
197 friend class CaptureData;
198
199 /** @return TRUE if entry for this register exist in this cache. */
200 bool queryCache() const;
201
202 /**
203 * @brief Reads register contents from cache.
204 * @return Reference to bit string buffer maintained in cache.
205 */
206 BitString & readCache() const;
207
208 /**
209 * @brief Deletes one or all entry in the cache
210 * @param RuleChip pointer associated with register
211 * @return Nil
212 */
Zane Shelley05bac982019-09-02 20:57:42 -0500213 void flushCache( ExtensibleChip *i_pChip = nullptr ) const;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500214
215 private: // Data
216
217 uint32_t iv_bitLength; // bit length of scom
218 uint16_t iv_shortId; // unique hash id of register
219 TARGETING::TYPE iv_chipType; // type of target associated with register
220 uint64_t iv_scomAddress; // scom address associated with regiser
221 AccessLevel iv_operationType; // Operation supported (RO, WO, or RW)
222
Zane Shelleyb77b5732019-08-30 22:01:06 -0500223#endif
Zane Shelley61565dc2019-09-18 21:57:10 -0500224
225 private: // Hardware accessor class variable
226
227 /** @brief A simple class that stores the chip used to access hardware. */
228 class Accessor
229 {
230 public:
231
232 /**
233 * @brief Constructor.
234 * @param i_chip The chip used to access hardware.
235 */
236 explicit Accessor( const Chip & i_chip ) :
237 iv_chip( i_chip )
238 {}
239
240 /** @brief Destructor. */
241 ~Accessor() = default;
242
243 /** @brief Copy constructor. */
244 Accessor( const Accessor & ) = delete;
245
246 /** @brief Assignment operator. */
247 Accessor & operator=( const Accessor & ) = delete;
248
249 /** @return The chip used to access hardware. */
250 const Chip & getChip() const { return iv_chip; }
251
252 private:
253
254 /**
255 * A Chip object provided by the user application. The isolator does not
256 * know anything about this object nor how to use it. Its only purpose
257 * is to get passed back to the user application for hardware access
258 * operations.
259 */
260 const Chip iv_chip;
261
262 }; // end class Accessor
263
264 /**
265 * This allows all HardwareRegister objects access to a chip via the user
266 * interface APIs. It is intentially defined as a pointer. It can be set to
267 * nullptr to signify that access is restricted at this time. This is useful
268 * to prevent users from accidentally accessing registers on the wrong chip.
269 * It is recommended to use setAccessor() and clearAccessor() to manage this
270 * variable.
271 */
272 static Accessor * cv_accessor;
273
274 public: // Hardware accessor management functions.
275
276 /**
277 * @brief Initializes a new hardware accessor.
278 * @param i_chip The chip used to access hardware.
279 */
280 static void setAccessor( const Chip & i_chip )
281 {
282 clearAccessor();
283 cv_accessor = new Accessor( i_chip );
284 }
285
286 /** @brief Deletes the current hardware accessor. */
287 static void clearAccessor()
288 {
289 delete cv_accessor;
290 cv_accessor = nullptr;
291 }
292
293 private: // Hardware accessor management functions.
294
295 /** @return The chip stored in cv_accessor. */
296 const Chip & getAccessorChip() const
297 {
298 HEI_ASSERT( nullptr != cv_accessor );
299
300#if 0
301 // Extra sanity check to verify this register belongs on the target
302 // accessor chip.
303 HEI_ASSERT( getChipType() != cv_accessor->getChip().getChipType() );
304#endif
305
306 return cv_accessor->getChip();
307 }
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500308};
309
Zane Shelley871adec2019-07-30 11:01:39 -0500310} // end namespace libhei
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500311