Zane Shelley | 61565dc | 2019-09-18 21:57:10 -0500 | [diff] [blame] | 1 | #include <hei_user_interface.hpp> |
Zane Shelley | 52cb1a9 | 2019-08-21 14:38:31 -0500 | [diff] [blame] | 2 | #include <register/hei_hardware_register.hpp> |
Zane Shelley | 995be6c | 2021-02-24 15:48:55 -0600 | [diff] [blame] | 3 | #include <util/hei_bit_string.hpp> |
Zane Shelley | d507351 | 2021-01-14 12:51:18 -0600 | [diff] [blame] | 4 | #include <util/hei_includes.hpp> |
Zane Shelley | 52cb1a9 | 2019-08-21 14:38:31 -0500 | [diff] [blame] | 5 | |
Zane Shelley | 871adec | 2019-07-30 11:01:39 -0500 | [diff] [blame] | 6 | namespace libhei |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 7 | { |
| 8 | |
Zane Shelley | 8deb090 | 2019-10-14 15:52:27 -0500 | [diff] [blame] | 9 | //------------------------------------------------------------------------------ |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 10 | |
Zane Shelley | 8deb090 | 2019-10-14 15:52:27 -0500 | [diff] [blame] | 11 | HardwareRegister::~HardwareRegister() {} |
| 12 | |
| 13 | //------------------------------------------------------------------------------ |
| 14 | |
Zane Shelley | fe27b65 | 2019-10-28 11:33:07 -0500 | [diff] [blame] | 15 | const BitString* HardwareRegister::getBitString(const Chip& i_chip) const |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 16 | { |
Paul Greenwood | 6574f6e | 2019-09-17 09:43:22 -0500 | [diff] [blame] | 17 | // Calling read() will ensure that an entry exists in the cache and the |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 18 | // entry has at been synched with hardware at least once. Note that we |
| 19 | // cannot read hardware for write-only registers. In this case, an entry |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 20 | // will be created in the cache, if it does not exist, when the cache is |
Zane Shelley | 53efc35 | 2019-10-03 21:46:39 -0500 | [diff] [blame] | 21 | // accessed below. |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 22 | |
Zane Shelley | 7667b71 | 2020-05-11 20:45:40 -0500 | [diff] [blame] | 23 | if (queryAttrFlag(REG_ATTR_ACCESS_READ)) |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 24 | { |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 25 | read(i_chip); |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 26 | } |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 27 | |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 28 | return &(accessCache(i_chip)); |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | //------------------------------------------------------------------------------ |
| 32 | |
Zane Shelley | fe27b65 | 2019-10-28 11:33:07 -0500 | [diff] [blame] | 33 | BitString& HardwareRegister::accessBitString(const Chip& i_chip) |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 34 | { |
Paul Greenwood | 6574f6e | 2019-09-17 09:43:22 -0500 | [diff] [blame] | 35 | // Calling read() will ensure that an entry exists in the cache and the |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 36 | // entry has at been synched with hardware at least once. Note that we |
| 37 | // cannot read hardware for write-only registers. In this case, an entry |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 38 | // will be created in the cache, if it does not exist, when the cache is |
Zane Shelley | 53efc35 | 2019-10-03 21:46:39 -0500 | [diff] [blame] | 39 | // accessed below. |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 40 | |
Zane Shelley | 7667b71 | 2020-05-11 20:45:40 -0500 | [diff] [blame] | 41 | if (queryAttrFlag(REG_ATTR_ACCESS_READ)) |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 42 | { |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 43 | read(i_chip); |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 44 | } |
| 45 | |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 46 | return accessCache(i_chip); |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | //------------------------------------------------------------------------------ |
| 50 | |
Zane Shelley | 2f4aa91 | 2020-05-08 14:28:18 -0500 | [diff] [blame] | 51 | bool HardwareRegister::read(const Chip& i_chip, bool i_force) const |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 52 | { |
Zane Shelley | 2f4aa91 | 2020-05-08 14:28:18 -0500 | [diff] [blame] | 53 | bool accessFailure = false; |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 54 | |
Zane Shelley | 2d4981a | 2020-06-04 14:32:45 -0500 | [diff] [blame] | 55 | // This register must be readable. |
| 56 | HEI_ASSERT(queryAttrFlag(REG_ATTR_ACCESS_READ)); |
| 57 | |
Zane Shelley | 61565dc | 2019-09-18 21:57:10 -0500 | [diff] [blame] | 58 | // Read from hardware only if the read is forced or the entry for this |
| 59 | // instance does not exist in the cache. |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 60 | if (i_force || !queryCache(i_chip)) |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 61 | { |
Zane Shelley | 61565dc | 2019-09-18 21:57:10 -0500 | [diff] [blame] | 62 | // Read this register from hardware. |
Patrick Williams | 2f7537d | 2023-05-10 07:51:39 -0500 | [diff] [blame] | 63 | uint64_t val = 0; |
Zane Shelley | 2d4981a | 2020-06-04 14:32:45 -0500 | [diff] [blame] | 64 | accessFailure = registerRead(i_chip, getType(), getAddress(), val); |
| 65 | if (!accessFailure) |
Zane Shelley | 61565dc | 2019-09-18 21:57:10 -0500 | [diff] [blame] | 66 | { |
Zane Shelley | 2d4981a | 2020-06-04 14:32:45 -0500 | [diff] [blame] | 67 | // Get the buffer from the register cache. |
| 68 | BitString& bs = accessCache(i_chip); |
| 69 | |
| 70 | // Set this value in the bit string buffer. |
| 71 | bs.setFieldRight(0, bs.getBitLen(), val); |
Zane Shelley | 61565dc | 2019-09-18 21:57:10 -0500 | [diff] [blame] | 72 | } |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 73 | } |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 74 | |
Zane Shelley | 2f4aa91 | 2020-05-08 14:28:18 -0500 | [diff] [blame] | 75 | return accessFailure; |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | //------------------------------------------------------------------------------ |
| 79 | |
Ben Tyner | 7b3420b | 2020-05-11 10:52:07 -0500 | [diff] [blame] | 80 | #ifdef __HEI_ENABLE_HW_WRITE |
Zane Shelley | 61565dc | 2019-09-18 21:57:10 -0500 | [diff] [blame] | 81 | |
Zane Shelley | 2f4aa91 | 2020-05-08 14:28:18 -0500 | [diff] [blame] | 82 | bool HardwareRegister::write(const Chip& i_chip) const |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 83 | { |
Zane Shelley | 61565dc | 2019-09-18 21:57:10 -0500 | [diff] [blame] | 84 | // This register must be writable. |
Zane Shelley | 7667b71 | 2020-05-11 20:45:40 -0500 | [diff] [blame] | 85 | HEI_ASSERT(queryAttrFlag(REG_ATTR_ACCESS_WRITE)); |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 86 | |
Zane Shelley | 61565dc | 2019-09-18 21:57:10 -0500 | [diff] [blame] | 87 | // An entry for this register must exist in the cache. |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 88 | HEI_ASSERT(queryCache(i_chip)); |
Zane Shelley | 61565dc | 2019-09-18 21:57:10 -0500 | [diff] [blame] | 89 | |
| 90 | // Get the buffer from the register cache. |
Zane Shelley | fe27b65 | 2019-10-28 11:33:07 -0500 | [diff] [blame] | 91 | BitString& bs = accessCache(i_chip); |
Zane Shelley | 61565dc | 2019-09-18 21:57:10 -0500 | [diff] [blame] | 92 | |
Zane Shelley | 2d4981a | 2020-06-04 14:32:45 -0500 | [diff] [blame] | 93 | // Set this value from the bit string buffer. |
| 94 | uint64_t val = bs.getFieldRight(0, bs.getBitLen()); |
Zane Shelley | 61565dc | 2019-09-18 21:57:10 -0500 | [diff] [blame] | 95 | |
| 96 | // Write to this register to hardware. |
Zane Shelley | 2d4981a | 2020-06-04 14:32:45 -0500 | [diff] [blame] | 97 | return registerWrite(i_chip, getType(), getAddress(), val); |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 98 | } |
| 99 | |
Ben Tyner | 7b3420b | 2020-05-11 10:52:07 -0500 | [diff] [blame] | 100 | #endif // __HEI_ENABLE_HW_WRITE |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 101 | |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 102 | //------------------------------------------------------------------------------ |
| 103 | |
Caleb Palmer | e4ad4e3 | 2024-08-07 09:48:14 -0500 | [diff] [blame^] | 104 | void HardwareRegister::setBit(const Chip& i_chip, uint64_t i_pos) const |
| 105 | { |
| 106 | // Get the buffer from the register cache. |
| 107 | BitString& bs = accessCache(i_chip); |
| 108 | bs.setBit(i_pos); |
| 109 | } |
| 110 | |
| 111 | //------------------------------------------------------------------------------ |
| 112 | |
| 113 | void HardwareRegister::setAllBits(const Chip& i_chip) const |
| 114 | { |
| 115 | // Get the buffer from the register cache. |
| 116 | BitString& bs = accessCache(i_chip); |
| 117 | bs.setAll(); |
| 118 | } |
| 119 | |
| 120 | //------------------------------------------------------------------------------ |
| 121 | |
| 122 | void HardwareRegister::clearBit(const Chip& i_chip, uint64_t i_pos) const |
| 123 | { |
| 124 | // Get the buffer from the register cache. |
| 125 | BitString& bs = accessCache(i_chip); |
| 126 | bs.clearBit(i_pos); |
| 127 | } |
| 128 | |
| 129 | //------------------------------------------------------------------------------ |
| 130 | |
| 131 | void HardwareRegister::clearAllBits(const Chip& i_chip) const |
| 132 | { |
| 133 | // Get the buffer from the register cache. |
| 134 | BitString& bs = accessCache(i_chip); |
| 135 | bs.clearAll(); |
| 136 | } |
| 137 | |
| 138 | //------------------------------------------------------------------------------ |
| 139 | |
Zane Shelley | c477199 | 2019-10-28 22:01:49 -0500 | [diff] [blame] | 140 | HardwareRegister::Cache HardwareRegister::cv_cache{}; |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 141 | |
| 142 | //------------------------------------------------------------------------------ |
| 143 | |
Zane Shelley | fe27b65 | 2019-10-28 11:33:07 -0500 | [diff] [blame] | 144 | bool HardwareRegister::Cache::query(const Chip& i_chip, |
| 145 | const HardwareRegister* i_hwReg) const |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 146 | { |
| 147 | // Does i_chip exist in the cache? |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 148 | auto chipPairItr = iv_cache.find(i_chip); |
| 149 | if (iv_cache.end() != chipPairItr) |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 150 | { |
Zane Shelley | fe27b65 | 2019-10-28 11:33:07 -0500 | [diff] [blame] | 151 | auto& hwRegMap = chipPairItr->second; // for ease of use |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 152 | |
| 153 | // Does i_hwReg exist in the cache? |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 154 | auto hwRegPairItr = hwRegMap.find(i_hwReg); |
| 155 | if (hwRegMap.end() != hwRegPairItr) |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 156 | { |
| 157 | return true; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | return false; |
| 162 | } |
| 163 | |
| 164 | //------------------------------------------------------------------------------ |
| 165 | |
Zane Shelley | fe27b65 | 2019-10-28 11:33:07 -0500 | [diff] [blame] | 166 | BitString& HardwareRegister::Cache::access(const Chip& i_chip, |
Zane Shelley | c477199 | 2019-10-28 22:01:49 -0500 | [diff] [blame] | 167 | const HardwareRegister* i_hwReg) |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 168 | { |
| 169 | // If the entry does not exist, create a new entry. |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 170 | if (!query(i_chip, i_hwReg)) |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 171 | { |
Zane Shelley | c477199 | 2019-10-28 22:01:49 -0500 | [diff] [blame] | 172 | BitString* bs = new BitStringBuffer{i_hwReg->getSize() * 8}; |
Zane Shelley | 7c8faa1 | 2019-10-28 22:26:28 -0500 | [diff] [blame] | 173 | |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 174 | iv_cache[i_chip][i_hwReg] = bs; |
| 175 | } |
| 176 | |
| 177 | // Return a reference to the target entry. |
| 178 | return *(iv_cache[i_chip][i_hwReg]); |
| 179 | } |
| 180 | |
| 181 | //------------------------------------------------------------------------------ |
| 182 | |
| 183 | void HardwareRegister::Cache::flush() |
| 184 | { |
| 185 | // Delete all of the BitStrings. |
Zane Shelley | 5a78fa8 | 2022-09-16 16:49:58 -0500 | [diff] [blame] | 186 | for (const auto& chipPair : iv_cache) |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 187 | { |
Zane Shelley | 5a78fa8 | 2022-09-16 16:49:58 -0500 | [diff] [blame] | 188 | for (const auto& hwRegPair : chipPair.second) |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 189 | { |
| 190 | delete hwRegPair.second; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | // !!! Do not delete the HardwareRegisters !!! |
| 195 | // Those are deleted when the main uninitialize() API is called. |
| 196 | |
| 197 | // Flush the rest of the cache. |
| 198 | iv_cache.clear(); |
| 199 | } |
| 200 | |
| 201 | //------------------------------------------------------------------------------ |
| 202 | |
Zane Shelley | fe27b65 | 2019-10-28 11:33:07 -0500 | [diff] [blame] | 203 | void HardwareRegister::Cache::flush(const Chip& i_chip, |
| 204 | const HardwareRegister* i_hwReg) |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 205 | { |
| 206 | // Does i_chip exist in the cache? |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 207 | auto chipPairItr = iv_cache.find(i_chip); |
| 208 | if (iv_cache.end() != chipPairItr) |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 209 | { |
Zane Shelley | fe27b65 | 2019-10-28 11:33:07 -0500 | [diff] [blame] | 210 | auto& hwRegMap = chipPairItr->second; // for ease of use |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 211 | |
| 212 | // Does i_hwReg exist in the cache? |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 213 | auto hwRegPairItr = hwRegMap.find(i_hwReg); |
| 214 | if (hwRegMap.end() != hwRegPairItr) |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 215 | { |
| 216 | delete hwRegPairItr->second; // delete the BitString |
| 217 | hwRegMap.erase(i_hwReg); // remove the entry for this register |
| 218 | } |
| 219 | |
| 220 | // If i_hwReg was the only entry for i_chip, we can remove i_chip from |
| 221 | // the cache. |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 222 | if (hwRegMap.empty()) |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 223 | { |
| 224 | iv_cache.erase(i_chip); |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | //------------------------------------------------------------------------------ |
| 230 | |
Zane Shelley | 871adec | 2019-07-30 11:01:39 -0500 | [diff] [blame] | 231 | } // end namespace libhei |