Zane Shelley | 52cb1a9 | 2019-08-21 14:38:31 -0500 | [diff] [blame] | 1 | #include <hei_includes.hpp> |
Zane Shelley | 61565dc | 2019-09-18 21:57:10 -0500 | [diff] [blame] | 2 | #include <hei_user_interface.hpp> |
Zane Shelley | 52cb1a9 | 2019-08-21 14:38:31 -0500 | [diff] [blame] | 3 | #include <register/hei_hardware_register.hpp> |
| 4 | #include <util/hei_bit_string.hpp> |
| 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 | |
| 15 | #if 0 |
Paul Greenwood | 6574f6e | 2019-09-17 09:43:22 -0500 | [diff] [blame^] | 16 | void HardwareRegister::setBitString( const BitString *bs ) |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 17 | { |
Paul Greenwood | 6574f6e | 2019-09-17 09:43:22 -0500 | [diff] [blame^] | 18 | BitString & l_string = accessBitString(); |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 19 | l_string.setString(*bs); |
| 20 | } |
Paul Greenwood | 6574f6e | 2019-09-17 09:43:22 -0500 | [diff] [blame^] | 21 | #endif |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 22 | |
| 23 | //------------------------------------------------------------------------------ |
| 24 | |
Paul Greenwood | 6574f6e | 2019-09-17 09:43:22 -0500 | [diff] [blame^] | 25 | const BitString * HardwareRegister::getBitString() const |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 26 | { |
Paul Greenwood | 6574f6e | 2019-09-17 09:43:22 -0500 | [diff] [blame^] | 27 | // 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] | 28 | // entry has at been synched with hardware at least once. Note that we |
| 29 | // cannot read hardware for write-only registers. In this case, an entry |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 30 | // will be created in the cache, if it does not exist, when the cache is |
| 31 | // read below. |
| 32 | |
Paul Greenwood | 6574f6e | 2019-09-17 09:43:22 -0500 | [diff] [blame^] | 33 | if ( ( REG_ACCESS_NONE != getAccessLevel() ) && |
| 34 | ( REG_ACCESS_WO != getAccessLevel() ) ) |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 35 | { |
Paul Greenwood | 6574f6e | 2019-09-17 09:43:22 -0500 | [diff] [blame^] | 36 | read(); |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 37 | } |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 38 | |
| 39 | return &( accessCache() ); |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | //------------------------------------------------------------------------------ |
| 43 | |
Paul Greenwood | 6574f6e | 2019-09-17 09:43:22 -0500 | [diff] [blame^] | 44 | #if 0 |
| 45 | BitString & HardwareRegister::accessBitString() |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 46 | { |
Paul Greenwood | 6574f6e | 2019-09-17 09:43:22 -0500 | [diff] [blame^] | 47 | // 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] | 48 | // entry has at been synched with hardware at least once. Note that we |
| 49 | // cannot read hardware for write-only registers. In this case, an entry |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 50 | // will be created in the cache, if it does not exist, when the cache is |
| 51 | // read below. |
| 52 | |
Paul Greenwood | 6574f6e | 2019-09-17 09:43:22 -0500 | [diff] [blame^] | 53 | if ( ( REG_ACCESS_NONE != getAccessLevel() ) && |
| 54 | ( REG_ACCESS_WO != getAccessLevel() ) ) |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 55 | { |
Paul Greenwood | 6574f6e | 2019-09-17 09:43:22 -0500 | [diff] [blame^] | 56 | read(); |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 57 | } |
| 58 | |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 59 | return accessCache(); |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 60 | } |
Zane Shelley | 61565dc | 2019-09-18 21:57:10 -0500 | [diff] [blame] | 61 | #endif |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 62 | |
| 63 | //------------------------------------------------------------------------------ |
| 64 | |
Zane Shelley | 61565dc | 2019-09-18 21:57:10 -0500 | [diff] [blame] | 65 | ReturnCode HardwareRegister::read( bool i_force ) const |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 66 | { |
Zane Shelley | 61565dc | 2019-09-18 21:57:10 -0500 | [diff] [blame] | 67 | ReturnCode rc; |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 68 | |
Zane Shelley | 61565dc | 2019-09-18 21:57:10 -0500 | [diff] [blame] | 69 | // Read from hardware only if the read is forced or the entry for this |
| 70 | // instance does not exist in the cache. |
| 71 | if ( i_force || !queryCache() ) |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 72 | { |
Zane Shelley | 61565dc | 2019-09-18 21:57:10 -0500 | [diff] [blame] | 73 | // This register must be readable. |
Paul Greenwood | 6574f6e | 2019-09-17 09:43:22 -0500 | [diff] [blame^] | 74 | HEI_ASSERT( ( REG_ACCESS_NONE != getAccessLevel() ) && |
| 75 | ( REG_ACCESS_WO != getAccessLevel() ) ); |
Zane Shelley | 61565dc | 2019-09-18 21:57:10 -0500 | [diff] [blame] | 76 | |
| 77 | // Get the buffer from the register cache. |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 78 | BitString & bs = accessCache(); |
Zane Shelley | 61565dc | 2019-09-18 21:57:10 -0500 | [diff] [blame] | 79 | |
| 80 | // Get the byte size of the buffer. |
| 81 | size_t sz_buffer = BitString::getMinBytes( bs.getBitLen() ); |
| 82 | |
| 83 | // Read this register from hardware. |
| 84 | rc = registerRead( getAccessorChip().getChip(), bs.getBufAddr(), |
| 85 | sz_buffer, getRegisterType(), getAddress() ); |
| 86 | if ( RC_SUCCESS != rc ) |
| 87 | { |
| 88 | // The read failed and we can't trust what was put in the register |
| 89 | // cache. So remove this instance's entry from the cache. |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 90 | cv_cache.flush( getAccessorChip(), this ); |
Zane Shelley | 61565dc | 2019-09-18 21:57:10 -0500 | [diff] [blame] | 91 | } |
| 92 | else |
| 93 | { |
| 94 | // Sanity check. The returned size of the data written to the buffer |
| 95 | // should match the register size. |
| 96 | HEI_ASSERT( getSize() == sz_buffer ); |
| 97 | } |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 98 | } |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 99 | |
Zane Shelley | 61565dc | 2019-09-18 21:57:10 -0500 | [diff] [blame] | 100 | return rc; |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | //------------------------------------------------------------------------------ |
| 104 | |
Zane Shelley | 61565dc | 2019-09-18 21:57:10 -0500 | [diff] [blame] | 105 | #ifndef __HEI_READ_ONLY |
| 106 | |
| 107 | ReturnCode HardwareRegister::write() const |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 108 | { |
Zane Shelley | 61565dc | 2019-09-18 21:57:10 -0500 | [diff] [blame] | 109 | ReturnCode rc; |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 110 | |
Zane Shelley | 61565dc | 2019-09-18 21:57:10 -0500 | [diff] [blame] | 111 | // This register must be writable. |
Paul Greenwood | 6574f6e | 2019-09-17 09:43:22 -0500 | [diff] [blame^] | 112 | HEI_ASSERT( ( REG_ACCESS_NONE != getAccessLevel() ) && |
| 113 | ( REG_ACCESS_RO != getAccessLevel() ) ); |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 114 | |
Zane Shelley | 61565dc | 2019-09-18 21:57:10 -0500 | [diff] [blame] | 115 | // An entry for this register must exist in the cache. |
| 116 | HEI_ASSERT( queryCache() ); |
| 117 | |
| 118 | // Get the buffer from the register cache. |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 119 | BitString & bs = accessCache(); |
Zane Shelley | 61565dc | 2019-09-18 21:57:10 -0500 | [diff] [blame] | 120 | |
| 121 | // Get the byte size of the buffer. |
| 122 | size_t sz_buffer = BitString::getMinBytes( bs.getBitLen() ); |
| 123 | |
| 124 | // Write to this register to hardware. |
| 125 | rc = registerWrite( getAccessorChip().getChip(), bs.getBufAddr(), |
| 126 | sz_buffer, getRegisterType(), getAddress() ); |
| 127 | |
| 128 | if ( RC_SUCCESS == rc ) |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 129 | { |
Zane Shelley | 61565dc | 2019-09-18 21:57:10 -0500 | [diff] [blame] | 130 | // Sanity check. The returned size of the data written to the buffer |
| 131 | // should match the register size. |
| 132 | HEI_ASSERT( getSize() == sz_buffer ); |
| 133 | } |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 134 | |
Zane Shelley | 61565dc | 2019-09-18 21:57:10 -0500 | [diff] [blame] | 135 | return rc; |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 136 | } |
| 137 | |
Zane Shelley | 61565dc | 2019-09-18 21:57:10 -0500 | [diff] [blame] | 138 | #endif // __HEI_READ_ONLY |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 139 | |
Zane Shelley | fd3f9cc | 2019-07-29 15:02:24 -0500 | [diff] [blame] | 140 | //------------------------------------------------------------------------------ |
| 141 | |
Zane Shelley | 61565dc | 2019-09-18 21:57:10 -0500 | [diff] [blame] | 142 | HardwareRegister::Accessor * HardwareRegister::cv_accessor = nullptr; |
| 143 | |
| 144 | //------------------------------------------------------------------------------ |
| 145 | |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 146 | HardwareRegister::Cache HardwareRegister::cv_cache {}; |
| 147 | |
| 148 | //------------------------------------------------------------------------------ |
| 149 | |
| 150 | bool HardwareRegister::Cache::query( const Chip & i_chip, |
| 151 | const HardwareRegister * i_hwReg ) const |
| 152 | { |
| 153 | // Does i_chip exist in the cache? |
| 154 | auto chipPairItr = iv_cache.find( i_chip ); |
| 155 | if ( iv_cache.end() != chipPairItr ) |
| 156 | { |
| 157 | auto & hwRegMap = chipPairItr->second; // for ease of use |
| 158 | |
| 159 | // Does i_hwReg exist in the cache? |
| 160 | auto hwRegPairItr = hwRegMap.find( i_hwReg ); |
| 161 | if ( hwRegMap.end() != hwRegPairItr ) |
| 162 | { |
| 163 | return true; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | return false; |
| 168 | } |
| 169 | |
| 170 | //------------------------------------------------------------------------------ |
| 171 | |
| 172 | BitString & HardwareRegister::Cache::access( const Chip & i_chip, |
| 173 | const HardwareRegister * i_hwReg ) |
| 174 | { |
| 175 | // If the entry does not exist, create a new entry. |
| 176 | if ( !query(i_chip, i_hwReg) ) |
| 177 | { |
Paul Greenwood | 6574f6e | 2019-09-17 09:43:22 -0500 | [diff] [blame^] | 178 | BitString * bs = new BitStringBuffer { i_hwReg->getSize() * 8 }; |
Zane Shelley | d0af358 | 2019-09-19 10:48:59 -0500 | [diff] [blame] | 179 | iv_cache[i_chip][i_hwReg] = bs; |
| 180 | } |
| 181 | |
| 182 | // Return a reference to the target entry. |
| 183 | return *(iv_cache[i_chip][i_hwReg]); |
| 184 | } |
| 185 | |
| 186 | //------------------------------------------------------------------------------ |
| 187 | |
| 188 | void HardwareRegister::Cache::flush() |
| 189 | { |
| 190 | // Delete all of the BitStrings. |
| 191 | for ( auto & chipPair : iv_cache ) |
| 192 | { |
| 193 | for ( auto & hwRegPair : chipPair.second ) |
| 194 | { |
| 195 | delete hwRegPair.second; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | // !!! Do not delete the HardwareRegisters !!! |
| 200 | // Those are deleted when the main uninitialize() API is called. |
| 201 | |
| 202 | // Flush the rest of the cache. |
| 203 | iv_cache.clear(); |
| 204 | } |
| 205 | |
| 206 | //------------------------------------------------------------------------------ |
| 207 | |
| 208 | void HardwareRegister::Cache::flush( const Chip & i_chip, |
| 209 | const HardwareRegister * i_hwReg ) |
| 210 | { |
| 211 | // Does i_chip exist in the cache? |
| 212 | auto chipPairItr = iv_cache.find( i_chip ); |
| 213 | if ( iv_cache.end() != chipPairItr ) |
| 214 | { |
| 215 | auto & hwRegMap = chipPairItr->second; // for ease of use |
| 216 | |
| 217 | // Does i_hwReg exist in the cache? |
| 218 | auto hwRegPairItr = hwRegMap.find( i_hwReg ); |
| 219 | if ( hwRegMap.end() != hwRegPairItr ) |
| 220 | { |
| 221 | delete hwRegPairItr->second; // delete the BitString |
| 222 | hwRegMap.erase(i_hwReg); // remove the entry for this register |
| 223 | } |
| 224 | |
| 225 | // If i_hwReg was the only entry for i_chip, we can remove i_chip from |
| 226 | // the cache. |
| 227 | if ( hwRegMap.empty() ) |
| 228 | { |
| 229 | iv_cache.erase(i_chip); |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | //------------------------------------------------------------------------------ |
| 235 | |
Zane Shelley | 871adec | 2019-07-30 11:01:39 -0500 | [diff] [blame] | 236 | } // end namespace libhei |
| 237 | |