Library of Bus-Adapters
/home/cschwick/hal/busAdapter/dummy/src/common/VMEDummyDeviceIdentifier.cc
Go to the documentation of this file.
00001 #include "hal/VMEDummyDeviceIdentifier.hh"
00002 #include <sstream>
00003 
00004 HAL::VMEDummyDeviceIdentifier::VMEDummyDeviceIdentifier( uint32_t deviceNumber,
00005                                                          std::vector<char *> memoryRegionPtr,
00006                                                          std::vector<uint32_t> baseaddresses,
00007                                                          std::vector<uint32_t> mapsizes,
00008                                                          bool doSwapping )
00009     
00010   : deviceNumber_(deviceNumber),
00011     memoryRegionPtr_(memoryRegionPtr),
00012     baseaddresses_( baseaddresses),
00013     mapsizes_( mapsizes ),
00014     doSwapping_( doSwapping ) {
00015 
00016   
00017 }
00018 
00019 HAL::VMEDummyDeviceIdentifier::~VMEDummyDeviceIdentifier() {
00020   // in principle we do not need the if since it is legal to delete a null 
00021   // pointer; but it is more instructive to code it like this:
00022 
00023   for ( int ic=0; ic<=NUMBER_OF_VME64XFUNCTIONS; ic++ ) {
00024 
00025     if ( memoryRegionPtr_[ic] != (char *) 0 ) {
00026       delete [] memoryRegionPtr_[ic];
00027       memoryRegionPtr_[ic] = (char *) 0;
00028     }
00029 
00030   }
00031 }
00032 
00033 // to be done here: find out in which ifunc we are
00034 //                  subtract the baseaddress offset
00035 //                  add the offset to the memory region in pc
00036 //                  return the resulting pointer for read or write operation
00037 char* HAL::VMEDummyDeviceIdentifier::remap( uint32_t address ) const 
00038   throw( BusAdapterException) {
00039 
00040   int ifunc;
00041   bool found = false;
00042   for ( ifunc=0; ifunc<NUMBER_OF_VME64XFUNCTIONS; ifunc++ ) {
00043 
00044     //std::cout << "ifunc " << ifunc 
00045     //        << " address " << std::hex << address
00046     //        << " baseadr " << baseaddresses_[ifunc]
00047     //        << " mapsizes " << mapsizes_[ifunc]
00048     //        << std::endl;
00049 
00050     if ( ( address >= baseaddresses_[ifunc] ) && ( address < (baseaddresses_[ifunc] + mapsizes_[ifunc]) ) ) {
00051       found = true;
00052       break;
00053     }
00054   }
00055 
00056   if ( ! found ) {    
00057     std::stringstream text;
00058     text << "Could not find a mapped ifunc for the requested address. Software bug in HAL.\n" << std::ends;
00059     throw( BusAdapterException( text.str(), __FILE__, __LINE__, __FUNCTION__ ) );
00060   }
00061 
00062   char* result = ((address - baseaddresses_[ifunc])) + memoryRegionPtr_[ifunc];
00063 
00064   return result;
00065 
00066 }
00067 
00068 
00069 std::string HAL::VMEDummyDeviceIdentifier::printString() const {
00070   char buf[256];
00071   sprintf( buf, "VMEDummyDeviceIdentifier : %u ", deviceNumber_ ); 
00072   return std::string(buf) ;
00073 }
00074 
00075 bool HAL::VMEDummyDeviceIdentifier::doSwapping() const {
00076     return doSwapping_;
00077 }