Generic hardware access library
|
00001 #include "hal/AddressTableItem.hh" 00002 #include <sstream> 00003 #include <iomanip> 00004 00005 HAL::AddressTableItem::AddressTableItem( std::string key, 00006 std::string description, 00007 HAL::GeneralHardwareAddress& hardwareAddress, 00008 uint32_t mask, 00009 bool writableFlag, 00010 bool readableFlag) 00011 : hardwareAddress(hardwareAddress), 00012 key(key), 00013 description(description), 00014 mask(mask), 00015 writableFlag(writableFlag), 00016 readableFlag(readableFlag) { 00017 00018 if ( countBits(mask) == 1 ) { 00019 bitFlag = true; 00020 } else { 00021 bitFlag = false; 00022 } 00023 } 00024 00025 HAL::AddressTableItem::AddressTableItem( const AddressTableItem& original ) 00026 :hardwareAddress( *(original.hardwareAddress.clone()) ) { 00027 key = original.key; 00028 description = original.description; 00029 mask = original.mask; 00030 writableFlag = original.writableFlag; 00031 readableFlag = original.readableFlag; 00032 bitFlag = original.bitFlag; 00033 } 00034 00035 HAL::AddressTableItem::~AddressTableItem() { 00036 delete &hardwareAddress; // it is on the heap ! 00037 } 00038 00039 HAL::GeneralHardwareAddress& 00040 HAL::AddressTableItem::getGeneralHardwareAddress() const { 00041 return hardwareAddress; 00042 } 00043 00044 uint32_t HAL::AddressTableItem::getMask() const { 00045 return mask; 00046 } 00047 00048 std::string HAL::AddressTableItem::getKey() const { 00049 return key; 00050 } 00051 00052 std::string HAL::AddressTableItem::getDescription() const { 00053 return description; 00054 } 00055 00056 bool HAL::AddressTableItem::isReadable() const { 00057 return readableFlag; 00058 } 00059 00060 bool HAL::AddressTableItem::isWritable() const { 00061 return writableFlag; 00062 } 00063 00064 void HAL::AddressTableItem::checkReadable() const 00065 throw (HAL::IllegalOperationException) { 00066 if ( ! readableFlag ) { 00067 std::stringstream text; 00068 text << "Try to read from non readable item!" 00069 << "\n Item : " << getKey() 00070 << "\n (HAL::AddressTableItem::checkReadable)" << std::ends; 00071 std::string textStr = text.str(); 00072 throw (HAL::IllegalOperationException( textStr, __FILE__, __LINE__, __FUNCTION__ )); 00073 } 00074 } 00075 00076 void HAL::AddressTableItem::checkWritable() const 00077 throw (HAL::IllegalOperationException) { 00078 if ( ! writableFlag ) { 00079 std::stringstream text; 00080 text << "Try to write to non writable item!" 00081 << "\n Item : " << getKey() 00082 << "\n (HAL::AddressTableItem::checkWritable)" << std::ends; 00083 std::string textStr = text.str(); 00084 throw (HAL::IllegalOperationException( textStr, __FILE__, __LINE__, __FUNCTION__ )); 00085 } 00086 } 00087 00088 void HAL::AddressTableItem::checkBit() const 00089 throw (HAL::IllegalOperationException) { 00090 if ( ! bitFlag ) { 00091 std::stringstream text; 00092 text << "Try to apply bit-operation to non-bit item!" 00093 << "\n Item : " << getKey() 00094 << "\n (HAL::AddressTableItem::checkBit)" << std::ends; 00095 std::string textStr = text.str(); 00096 throw (HAL::IllegalOperationException( textStr, __FILE__, __LINE__, __FUNCTION__ )); 00097 } 00098 } 00099 00100 bool HAL::AddressTableItem::isBit() const { 00101 return bitFlag; 00102 } 00103 00104 void HAL::AddressTableItem::print( std::ostream& os ) const { 00105 os << std::setfill(' ') << std::setw(30) << key.c_str() << " "; 00106 hardwareAddress.print(); 00107 os << std::setw(8) << std::hex << std::setfill('0') << std::setw(8) << mask << " " << std::setfill(' ') 00108 << std::setw(1) << readableFlag << " " 00109 << std::setw(1) << writableFlag << " " 00110 << description 00111 << std::endl; 00112 } 00113 00114 uint32_t HAL::AddressTableItem::countBits( uint32_t word ) const { 00115 uint32_t result = 0; 00116 for( int ic=0; ic<32; ic++ ) { 00117 if ( (word & 0x00000001) == 1 ) result++; 00118 word >>= 1; 00119 } 00120 return result; 00121 } 00122 00123 00124 uint32_t HAL::AddressTableItem::applyToMask( uint32_t data, 00125 bool MaskBoundaryCheck ) const 00126 throw( HAL::MaskBoundaryException ) { 00127 if ( mask == 0 ) { 00128 data = 0; 00129 } else { 00130 bool maskOk; 00131 if ( MaskBoundaryCheck ) { 00132 uint32_t help = mask; 00133 while ( (help & 0x00000001) == 0 ) help >>= 1; 00134 maskOk = ( (~ help & data) == 0 ); 00135 if ( ! maskOk ) { 00136 std::stringstream text; 00137 text << "Data bits set outside of mask.\n" 00138 << " Item : " << key << "\n" 00139 << " Data : " << std::hex << data << "\n" 00140 << " Mask : " << mask 00141 << " (HAL::AddressTableItem::applyToMask)" 00142 << std::ends; 00143 std::string textStr = text.str(); 00144 throw( HAL::MaskBoundaryException( textStr, __FILE__, __LINE__, __FUNCTION__ ) ); 00145 } 00146 } 00147 uint32_t help = mask; 00148 while ( (help & 0x00000001) == 0 ) { 00149 help >>= 1; 00150 data <<= 1; 00151 } 00152 } 00153 return data; 00154 } 00155 00156 uint32_t HAL::AddressTableItem::applyFromMask( uint32_t data, 00157 bool MaskBoundaryCheck ) const 00158 throw( HAL::MaskBoundaryException ) { 00159 if ( mask == 0 ) { 00160 data = 0; 00161 } else { 00162 bool maskOk; 00163 if ( MaskBoundaryCheck ) { 00164 maskOk = ( (data & (~ mask)) == 0 ); 00165 if ( ! maskOk ) { 00166 std::stringstream text; 00167 text << "Data bits set outside of mask.\n" 00168 << " Item : " << key << "\n" 00169 << " Data : " << std::hex << data << "\n" 00170 << " Mask : " << mask 00171 << " (HAL::AddressTableItem::applyFromMask)" 00172 << std::ends; 00173 std::string textStr = text.str(); 00174 throw( HAL::MaskBoundaryException( textStr, __FILE__, __LINE__, __FUNCTION__ ) ); 00175 } 00176 } 00177 uint32_t help = mask; 00178 while ( (help & 0x00000001) == 0 ) { 00179 help >>= 1; 00180 data >>= 1; 00181 } 00182 } 00183 return data; 00184 }