blob: bf9a55aa06f3bbbb0727d683c77c83c146fba898 [file] [log] [blame]
Zane Shelley52cb1a92019-08-21 14:38:31 -05001#include <hei_includes.hpp>
Zane Shelley61565dc2019-09-18 21:57:10 -05002#include <hei_user_interface.hpp>
Zane Shelley52cb1a92019-08-21 14:38:31 -05003#include <register/hei_hardware_register.hpp>
4#include <util/hei_bit_string.hpp>
5
Zane Shelley871adec2019-07-30 11:01:39 -05006namespace libhei
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -05007{
8
Zane Shelley8deb0902019-10-14 15:52:27 -05009//------------------------------------------------------------------------------
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050010
Zane Shelley8deb0902019-10-14 15:52:27 -050011HardwareRegister::~HardwareRegister() {}
12
13//------------------------------------------------------------------------------
14
Zane Shelleyfe27b652019-10-28 11:33:07 -050015const BitString* HardwareRegister::getBitString(const Chip& i_chip) const
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050016{
Zane Shelley53efc352019-10-03 21:46:39 -050017 // Verify this register belongs on i_chip.
Zane Shelley83da2452019-10-25 15:45:34 -050018 verifyAccessorChip(i_chip);
Zane Shelley53efc352019-10-03 21:46:39 -050019
Paul Greenwood6574f6e2019-09-17 09:43:22 -050020 // Calling read() will ensure that an entry exists in the cache and the
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050021 // entry has at been synched with hardware at least once. Note that we
22 // cannot read hardware for write-only registers. In this case, an entry
Zane Shelleyd0af3582019-09-19 10:48:59 -050023 // will be created in the cache, if it does not exist, when the cache is
Zane Shelley53efc352019-10-03 21:46:39 -050024 // accessed below.
Zane Shelleyd0af3582019-09-19 10:48:59 -050025
Zane Shelley7667b712020-05-11 20:45:40 -050026 if (queryAttrFlag(REG_ATTR_ACCESS_READ))
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050027 {
Zane Shelley83da2452019-10-25 15:45:34 -050028 read(i_chip);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050029 }
Zane Shelleyd0af3582019-09-19 10:48:59 -050030
Zane Shelley83da2452019-10-25 15:45:34 -050031 return &(accessCache(i_chip));
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050032}
33
34//------------------------------------------------------------------------------
35
Zane Shelleyfe27b652019-10-28 11:33:07 -050036BitString& HardwareRegister::accessBitString(const Chip& i_chip)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050037{
Zane Shelley53efc352019-10-03 21:46:39 -050038 // Verify this register belongs on i_chip.
Zane Shelley83da2452019-10-25 15:45:34 -050039 verifyAccessorChip(i_chip);
Zane Shelley53efc352019-10-03 21:46:39 -050040
Paul Greenwood6574f6e2019-09-17 09:43:22 -050041 // Calling read() will ensure that an entry exists in the cache and the
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050042 // entry has at been synched with hardware at least once. Note that we
43 // cannot read hardware for write-only registers. In this case, an entry
Zane Shelleyd0af3582019-09-19 10:48:59 -050044 // will be created in the cache, if it does not exist, when the cache is
Zane Shelley53efc352019-10-03 21:46:39 -050045 // accessed below.
Zane Shelleyd0af3582019-09-19 10:48:59 -050046
Zane Shelley7667b712020-05-11 20:45:40 -050047 if (queryAttrFlag(REG_ATTR_ACCESS_READ))
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050048 {
Zane Shelley83da2452019-10-25 15:45:34 -050049 read(i_chip);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050050 }
51
Zane Shelley83da2452019-10-25 15:45:34 -050052 return accessCache(i_chip);
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050053}
54
55//------------------------------------------------------------------------------
56
Zane Shelley2f4aa912020-05-08 14:28:18 -050057bool HardwareRegister::read(const Chip& i_chip, bool i_force) const
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050058{
Zane Shelley2f4aa912020-05-08 14:28:18 -050059 bool accessFailure = false;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050060
Zane Shelley53efc352019-10-03 21:46:39 -050061 // Verify this register belongs on i_chip.
Zane Shelley83da2452019-10-25 15:45:34 -050062 verifyAccessorChip(i_chip);
Zane Shelley53efc352019-10-03 21:46:39 -050063
Zane Shelley61565dc2019-09-18 21:57:10 -050064 // Read from hardware only if the read is forced or the entry for this
65 // instance does not exist in the cache.
Zane Shelley83da2452019-10-25 15:45:34 -050066 if (i_force || !queryCache(i_chip))
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050067 {
Zane Shelley61565dc2019-09-18 21:57:10 -050068 // This register must be readable.
Zane Shelley7667b712020-05-11 20:45:40 -050069 HEI_ASSERT(queryAttrFlag(REG_ATTR_ACCESS_READ));
Zane Shelley61565dc2019-09-18 21:57:10 -050070
71 // Get the buffer from the register cache.
Zane Shelleyfe27b652019-10-28 11:33:07 -050072 BitString& bs = accessCache(i_chip);
Zane Shelley61565dc2019-09-18 21:57:10 -050073
74 // Get the byte size of the buffer.
Zane Shelley83da2452019-10-25 15:45:34 -050075 size_t sz_buffer = BitString::getMinBytes(bs.getBitLen());
Zane Shelley61565dc2019-09-18 21:57:10 -050076
77 // Read this register from hardware.
Zane Shelley2f4aa912020-05-08 14:28:18 -050078 accessFailure = registerRead(i_chip, bs.getBufAddr(), sz_buffer,
79 getRegisterType(), getAddress());
80 if (accessFailure)
Zane Shelley61565dc2019-09-18 21:57:10 -050081 {
82 // The read failed and we can't trust what was put in the register
83 // cache. So remove this instance's entry from the cache.
Zane Shelley83da2452019-10-25 15:45:34 -050084 flush(i_chip);
Zane Shelley61565dc2019-09-18 21:57:10 -050085 }
86 else
87 {
88 // Sanity check. The returned size of the data written to the buffer
89 // should match the register size.
Zane Shelley83da2452019-10-25 15:45:34 -050090 HEI_ASSERT(getSize() == sz_buffer);
Zane Shelley61565dc2019-09-18 21:57:10 -050091 }
Zane Shelleyd0af3582019-09-19 10:48:59 -050092 }
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050093
Zane Shelley2f4aa912020-05-08 14:28:18 -050094 return accessFailure;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -050095}
96
97//------------------------------------------------------------------------------
98
Ben Tyner7b3420b2020-05-11 10:52:07 -050099#ifdef __HEI_ENABLE_HW_WRITE
Zane Shelley61565dc2019-09-18 21:57:10 -0500100
Zane Shelley2f4aa912020-05-08 14:28:18 -0500101bool HardwareRegister::write(const Chip& i_chip) const
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500102{
Zane Shelley2f4aa912020-05-08 14:28:18 -0500103 bool accessFailure = false;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500104
Zane Shelley53efc352019-10-03 21:46:39 -0500105 // Verify this register belongs on i_chip.
Zane Shelley83da2452019-10-25 15:45:34 -0500106 verifyAccessorChip(i_chip);
Zane Shelley53efc352019-10-03 21:46:39 -0500107
Zane Shelley61565dc2019-09-18 21:57:10 -0500108 // This register must be writable.
Zane Shelley7667b712020-05-11 20:45:40 -0500109 HEI_ASSERT(queryAttrFlag(REG_ATTR_ACCESS_WRITE));
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500110
Zane Shelley61565dc2019-09-18 21:57:10 -0500111 // An entry for this register must exist in the cache.
Zane Shelley83da2452019-10-25 15:45:34 -0500112 HEI_ASSERT(queryCache(i_chip));
Zane Shelley61565dc2019-09-18 21:57:10 -0500113
114 // Get the buffer from the register cache.
Zane Shelleyfe27b652019-10-28 11:33:07 -0500115 BitString& bs = accessCache(i_chip);
Zane Shelley61565dc2019-09-18 21:57:10 -0500116
117 // Get the byte size of the buffer.
Zane Shelley83da2452019-10-25 15:45:34 -0500118 size_t sz_buffer = BitString::getMinBytes(bs.getBitLen());
Zane Shelley61565dc2019-09-18 21:57:10 -0500119
120 // Write to this register to hardware.
Zane Shelley2f4aa912020-05-08 14:28:18 -0500121 accessFailure = registerWrite(i_chip, bs.getBufAddr(), sz_buffer,
122 getRegisterType(), getAddress());
Zane Shelley61565dc2019-09-18 21:57:10 -0500123
Zane Shelley2f4aa912020-05-08 14:28:18 -0500124 if (accessFailure)
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500125 {
Zane Shelley61565dc2019-09-18 21:57:10 -0500126 // Sanity check. The returned size of the data written to the buffer
127 // should match the register size.
Zane Shelley83da2452019-10-25 15:45:34 -0500128 HEI_ASSERT(getSize() == sz_buffer);
Zane Shelley61565dc2019-09-18 21:57:10 -0500129 }
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500130
Zane Shelley2f4aa912020-05-08 14:28:18 -0500131 return accessFailure;
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500132}
133
Ben Tyner7b3420b2020-05-11 10:52:07 -0500134#endif // __HEI_ENABLE_HW_WRITE
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500135
Zane Shelleyfd3f9cc2019-07-29 15:02:24 -0500136//------------------------------------------------------------------------------
137
Zane Shelleyc4771992019-10-28 22:01:49 -0500138HardwareRegister::Cache HardwareRegister::cv_cache{};
Zane Shelleyd0af3582019-09-19 10:48:59 -0500139
140//------------------------------------------------------------------------------
141
Zane Shelleyfe27b652019-10-28 11:33:07 -0500142bool HardwareRegister::Cache::query(const Chip& i_chip,
143 const HardwareRegister* i_hwReg) const
Zane Shelleyd0af3582019-09-19 10:48:59 -0500144{
145 // Does i_chip exist in the cache?
Zane Shelley83da2452019-10-25 15:45:34 -0500146 auto chipPairItr = iv_cache.find(i_chip);
147 if (iv_cache.end() != chipPairItr)
Zane Shelleyd0af3582019-09-19 10:48:59 -0500148 {
Zane Shelleyfe27b652019-10-28 11:33:07 -0500149 auto& hwRegMap = chipPairItr->second; // for ease of use
Zane Shelleyd0af3582019-09-19 10:48:59 -0500150
151 // Does i_hwReg exist in the cache?
Zane Shelley83da2452019-10-25 15:45:34 -0500152 auto hwRegPairItr = hwRegMap.find(i_hwReg);
153 if (hwRegMap.end() != hwRegPairItr)
Zane Shelleyd0af3582019-09-19 10:48:59 -0500154 {
155 return true;
156 }
157 }
158
159 return false;
160}
161
162//------------------------------------------------------------------------------
163
Zane Shelleyfe27b652019-10-28 11:33:07 -0500164BitString& HardwareRegister::Cache::access(const Chip& i_chip,
Zane Shelleyc4771992019-10-28 22:01:49 -0500165 const HardwareRegister* i_hwReg)
Zane Shelleyd0af3582019-09-19 10:48:59 -0500166{
167 // If the entry does not exist, create a new entry.
Zane Shelley83da2452019-10-25 15:45:34 -0500168 if (!query(i_chip, i_hwReg))
Zane Shelleyd0af3582019-09-19 10:48:59 -0500169 {
Zane Shelleyc4771992019-10-28 22:01:49 -0500170 BitString* bs = new BitStringBuffer{i_hwReg->getSize() * 8};
Zane Shelley7c8faa12019-10-28 22:26:28 -0500171
Zane Shelleyd0af3582019-09-19 10:48:59 -0500172 iv_cache[i_chip][i_hwReg] = bs;
173 }
174
175 // Return a reference to the target entry.
176 return *(iv_cache[i_chip][i_hwReg]);
177}
178
179//------------------------------------------------------------------------------
180
181void HardwareRegister::Cache::flush()
182{
183 // Delete all of the BitStrings.
Zane Shelleyfe27b652019-10-28 11:33:07 -0500184 for (auto& chipPair : iv_cache)
Zane Shelleyd0af3582019-09-19 10:48:59 -0500185 {
Zane Shelleyfe27b652019-10-28 11:33:07 -0500186 for (auto& hwRegPair : chipPair.second)
Zane Shelleyd0af3582019-09-19 10:48:59 -0500187 {
188 delete hwRegPair.second;
189 }
190 }
191
192 // !!! Do not delete the HardwareRegisters !!!
193 // Those are deleted when the main uninitialize() API is called.
194
195 // Flush the rest of the cache.
196 iv_cache.clear();
197}
198
199//------------------------------------------------------------------------------
200
Zane Shelleyfe27b652019-10-28 11:33:07 -0500201void HardwareRegister::Cache::flush(const Chip& i_chip,
202 const HardwareRegister* i_hwReg)
Zane Shelleyd0af3582019-09-19 10:48:59 -0500203{
204 // Does i_chip exist in the cache?
Zane Shelley83da2452019-10-25 15:45:34 -0500205 auto chipPairItr = iv_cache.find(i_chip);
206 if (iv_cache.end() != chipPairItr)
Zane Shelleyd0af3582019-09-19 10:48:59 -0500207 {
Zane Shelleyfe27b652019-10-28 11:33:07 -0500208 auto& hwRegMap = chipPairItr->second; // for ease of use
Zane Shelleyd0af3582019-09-19 10:48:59 -0500209
210 // Does i_hwReg exist in the cache?
Zane Shelley83da2452019-10-25 15:45:34 -0500211 auto hwRegPairItr = hwRegMap.find(i_hwReg);
212 if (hwRegMap.end() != hwRegPairItr)
Zane Shelleyd0af3582019-09-19 10:48:59 -0500213 {
214 delete hwRegPairItr->second; // delete the BitString
215 hwRegMap.erase(i_hwReg); // remove the entry for this register
216 }
217
218 // If i_hwReg was the only entry for i_chip, we can remove i_chip from
219 // the cache.
Zane Shelley83da2452019-10-25 15:45:34 -0500220 if (hwRegMap.empty())
Zane Shelleyd0af3582019-09-19 10:48:59 -0500221 {
222 iv_cache.erase(i_chip);
223 }
224 }
225}
226
227//------------------------------------------------------------------------------
228
Zane Shelley871adec2019-07-30 11:01:39 -0500229} // end namespace libhei