00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "TVmeModule.hh"
00017
00018 TVmeModule::TVmeModule( Tint nch, Toff_t offset, Tint mapsize, TvmeTransfer_t mode )
00019 : TModule( nch ),
00020 theMappedSize( mapsize ), theTransferMode( mode ),
00021 theFileDescriptor( -1 ), theBaseAddress( 0 ),
00022 theOffsetAddress( offset )
00023 {
00024 theFileDescriptor = open( ( TvmeDevices[ theTransferMode ] ).c_str(), O_RDWR );
00025 if ( theFileDescriptor == -1 ) {
00026 perror( "TVmeModule::TVmeModule" );
00027 exit( EXIT_FAILURE );
00028 }
00029 theStatus = -errno;
00030
00031
00032 Tvoid* base = 0;
00033 if ( ( base = mmap( 0, theMappedSize, PROT_READ|PROT_WRITE, MAP_SHARED, theFileDescriptor, theOffsetAddress ) ) == MAP_FAILED ) {
00034 perror( "TVmeModule::TVmeModule" );
00035 exit( EXIT_FAILURE );
00036 }
00037 theBaseAddress = (Tcaddr_t)base;
00038 theStatus = -errno;
00039 }
00040
00041 TVmeModule::TVmeModule( const TVmeModule& right )
00042 : TModule( right ),
00043 theMappedSize( right.theMappedSize ),
00044 theTransferMode( right.theTransferMode ),
00045 theFileDescriptor( -1 ), theBaseAddress( 0 ),
00046 theOffsetAddress( right.theOffsetAddress )
00047 {
00048 theFileDescriptor = open( ( TvmeDevices[ theTransferMode ] ).c_str(), O_RDWR );
00049 if ( theFileDescriptor == -1 ) {
00050 perror( "TVmeModule::TVmeModule" );
00051 exit( EXIT_FAILURE );
00052 }
00053 theStatus = -errno;
00054
00055 Tvoid* base = 0;
00056 if ( ( base = mmap( 0, theMappedSize, PROT_READ|PROT_WRITE, MAP_SHARED, theFileDescriptor, theOffsetAddress ) ) == MAP_FAILED ) {
00057 perror( "TVmeModule::TVmeModule" );
00058 exit( EXIT_FAILURE );
00059 }
00060 theBaseAddress = (Tcaddr_t)base;
00061 theStatus = -errno;
00062 }
00063
00064 TVmeModule::~TVmeModule()
00065 {
00066 munmap( theBaseAddress, theMappedSize );
00067 theStatus = -errno;
00068 close( theFileDescriptor );
00069 theStatus = -errno;
00070 }
00071
00072 const TVmeModule& TVmeModule::operator=( const TVmeModule& right )
00073 {
00074 *( (TModule*)this ) = *( (TModule*)(&right) );
00075 theMappedSize = right.theMappedSize;
00076 theTransferMode = right.theTransferMode;
00077 theFileDescriptor = -1;
00078 theBaseAddress = 0;
00079 theOffsetAddress = right.theOffsetAddress;
00080
00081 theFileDescriptor = open( ( TvmeDevices[ theTransferMode ] ).c_str(), O_RDWR );
00082 if ( theFileDescriptor == -1 ) {
00083 perror( "TVmeModule::operator=" );
00084 exit( EXIT_FAILURE );
00085 }
00086 theStatus = -errno;
00087
00088 Tvoid* base = 0;
00089 if ( ( base = mmap( 0, theMappedSize, PROT_READ|PROT_WRITE, MAP_SHARED, theFileDescriptor, theOffsetAddress ) ) == MAP_FAILED ) {
00090 perror( "TVmeModule::operator=" );
00091 exit( EXIT_FAILURE );
00092 }
00093 theBaseAddress = (Tcaddr_t)base;
00094 theStatus = errno;
00095 return *this;
00096 }
00097
00098 Tbool TVmeModule::operator==( const TVmeModule& right ) const
00099 {
00100 Tbool ret = Ttrue;
00101 ret &= ( *( (TModule*)this ) == *( (TModule*)(&right) ) );
00102 ret &= ( theMappedSize == right.theMappedSize );
00103 ret &= ( theTransferMode == right.theTransferMode );
00104 ret &= ( theOffsetAddress == right.theOffsetAddress );
00105 return ret;
00106 }
00107
00108 Tbool TVmeModule::operator!=( const TVmeModule& right ) const
00109 {
00110 Tbool ret = Tfalse;
00111 ret |= ( *( (TModule*)this ) != *( (TModule*)(&right) ) );
00112 ret |= ( theMappedSize != right.theMappedSize );
00113 ret |= ( theTransferMode != right.theTransferMode );
00114 ret |= ( theOffsetAddress != right.theOffsetAddress );
00115 return ret;
00116 }
00117
00118 Tvoid TVmeModule::showBit( TUshort data, const Tstring comment ) const
00119 {
00120 TUshort bit;
00121
00122 Tcout << "0x";
00123 Tcout.setf( ios::left );
00124 Tcout.width( 10 );
00125 Tcout << hex << data << ": ";
00126 static const Tint nbits = 16;
00127
00128 for ( int i = 0; i < nbits; i ++ ) {
00129 bit = data & 0x8000;
00130 bit = bit >> ( nbits - 1 );
00131 Tcout << bit;
00132 data = data << 1;
00133 }
00134
00135 if ( comment.empty() )
00136 Tcout << Tendl;
00137 else
00138 Tcout << " <--- " << comment << Tendl;
00139
00140 return;
00141 }
00142
00143 Tvoid TVmeModule::setBit( TUshort* ptr, Tint nbit, Tbit bit )
00144 {
00145 if ( nbit < 0 || nbit > 15 ) {
00146 Tcerr << "TVmeModule::setBit: invalid channel" << Tendl;
00147 return;
00148 }
00149
00150 TUshort mask = 0x0001;
00151 mask = mask << nbit;
00152
00153 if ( bit == 1 ) {
00154 *ptr |= mask;
00155 } else if ( bit == 0 ) {
00156 mask = ~mask;
00157 *ptr &= mask;
00158 } else {
00159 Tcerr << "TVmeModule::setBit: set 0 or 1" << Tendl;
00160 return;
00161 }
00162
00163 return;
00164 }
00165
00166 Tbit TVmeModule::getBit( TUshort* ptr, Tint nbit ) const
00167 {
00168 if ( nbit < 0 || nbit > 15 ) {
00169 Tcerr << "TVmeModule::getBit: invalid channel" << Tendl;
00170 return 0;
00171 }
00172
00173 TUshort data = *ptr;
00174 data = data >> nbit;
00175 data &= 0x00001;
00176
00177 return data;
00178 }
00179
00180 #ifdef __CLDAQ_ROOT_DLL
00181 ClassImp(TVmeModule)
00182 #endif