Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members  

SIG_Register.cpp

00001 #include "SIGEL_Simulation/SIG_Register.h"
00002 #include <cmath> // needed for computing the power of a number
00003 
00004 SIGEL_Simulation::SIG_Register::SIG_Register(int size)
00005   throw(SIGEL_Simulation::SIG_RegisterWrongSizeException)
00006   : value(0)
00007 {
00008   if ( (size>0) && (size<100) )
00009     this->size = size;
00010   else 
00011     throw SIG_RegisterWrongSizeException(__FILE__,__LINE__,"Size of Register must be between 1 and 99");
00012 };
00013 
00014 void SIGEL_Simulation::SIG_Register::makeValid()
00015 {
00016   int max= static_cast<int>(std::pow(2,size-1));
00017 
00018   if ( max - 1 < value )
00019     {
00020       value = -max + ( ( value + max ) % (2*max) );
00021     };
00022 
00023   if ( value < -max )
00024     value = max - 1 + ( ( value - ( max - 1) ) % (2*max) );
00025 };
00026 
00027 void SIGEL_Simulation::SIG_Register::copyReg(SIG_Register const& otherRegister)
00028 { 
00029   value=otherRegister.getValue();
00030   // makeValid(); superflous
00031 };
00032 
00033 void SIGEL_Simulation::SIG_Register::addReg(SIG_Register const& otherRegister)
00034 {
00035   value=value+otherRegister.getValue();
00036   makeValid();
00037 };
00038 
00039 void SIGEL_Simulation::SIG_Register::subReg(SIG_Register const& otherRegister)
00040 { 
00041   value=value-otherRegister.getValue();
00042   makeValid();
00043 };
00044 
00045 void SIGEL_Simulation::SIG_Register::loadValue(int newValue)
00046 {
00047   value=newValue;
00048   makeValid();
00049 };
00050 
00051 void SIGEL_Simulation::SIG_Register::mulReg(SIG_Register const& otherRegister)
00052 {
00053   value=value*otherRegister.getValue();
00054   makeValid();
00055 };
00056 
00057 void SIGEL_Simulation::SIG_Register::divReg(SIG_Register const& otherRegister)
00058 {
00059   if( otherRegister.getValue() == 0 )
00060     value = 0;
00061   else
00062     value=value/otherRegister.getValue();
00063   makeValid();
00064 };
00065 
00066 void SIGEL_Simulation::SIG_Register::minReg(SIG_Register const& otherRegister)
00067 {
00068   if (otherRegister.getValue()<value)
00069     value=otherRegister.getValue();
00070   // makeValid(); superflous
00071 };
00072 
00073 void SIGEL_Simulation::SIG_Register::maxReg(SIG_Register const& otherRegister)
00074 {
00075   if (otherRegister.getValue()>value)
00076     value=otherRegister.getValue();
00077   // makeValid(); superflous
00078 };
00079 
00080 void SIGEL_Simulation::SIG_Register::modReg(SIG_Register const& otherRegister)
00081 {
00082   if( otherRegister.getValue() == 0 )
00083     value = 0;
00084   else
00085   {  
00086     int modnumber = otherRegister.getValue();
00087     if ( modnumber < 0 )
00088       {
00089         modnumber = -modnumber;
00090       }
00091     if ( value < 0 )
00092       value = modnumber + ( value % modnumber );
00093     else
00094       value = value % modnumber;
00095   };
00096   makeValid();
00097 };
00098 
00099 int SIGEL_Simulation::SIG_Register::getValue() const
00100 {
00101   return value;
00102 };
00103 
00104 int SIGEL_Simulation::SIG_Register::getSize() const
00105 {
00106   return size;
00107 };
00108 
00109 int SIGEL_Simulation::SIG_Register::getMaxValue() const
00110 {
00111   return static_cast<int>( std::pow(2,size-1) - 1);
00112 };
00113 
00114 int SIGEL_Simulation::SIG_Register::getMinValue() const
00115 {
00116   return static_cast<int>( - std::pow(2,size-1));
00117 };

Generated at Mon Sep 3 01:32:29 2001 for PG 368 - SIGEL by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000