/// <summary>
        /// Converts the members of the bit field to an integer value.
        /// </summary>
        /// <param name="obj">An instance of a struct that implements the interface IBitField.</param>
        /// <returns>An integer representation of the bit field.</returns>
        public static ulong ToUInt64(this IBitField obj)
        {
            ulong result = 0;

            // Loop through all the properties
            foreach (PropertyInfo pi in obj.GetType().GetProperties())
            {
                // Check if the property has an attribute of type BitFieldLengthAttribute
                BitFieldInfoAttribute bitField = (pi.GetCustomAttribute(typeof(BitFieldInfoAttribute)) as BitFieldInfoAttribute);
                if (bitField != null)
                {
                    // Calculate a bitmask using the length of the bit field
                    ulong mask = 0;
                    for (byte i = 0; i < bitField.Length; i++)
                    {
                        mask |= 1UL << i;
                    }

                    // This conversion makes it possible to use different types in the bit field
                    ulong value = Convert.ToUInt64(pi.GetValue(obj));

                    result |= (value & mask) << bitField.Offset;
                }
            }

            return(result);
        }
Example #2
0
        /// <summary>
        /// Construct a register with the specified register attributes
        /// </summary>
        /// <param name="name">register name</param>
        /// <param name="offset">offset (BAR offset for memory mapped register)</param>
        /// <param name="bfType">enumerated type of BitFields contained by this register.</param>
        /// <param name="regType">register type/attribute (one or more bits from RegType, e.g. Cmd|RO).</param>
        /// <param name="driver">The default driver the register uses to read/write.</param>
        protected Reg(string name,
                      Int32 offset,
                      Type bfType,
                      IRegDriver driver,
                      RegType regType)
            : base(name, offset, bfType, regType, driver)
        {
            if (bfType != null)
            {
                // mFields = new TRegField[Enum.GetNames(BFenumType).Length];

                // PREPARING FOR UNUSED COMMON BITS
                Array values = Enum.GetValues(bfType);
                mFirstBFvalue = (byte)((int)values.GetValue(0));
                mNumBFs       = (byte)values.Length;

                //int maxValue = 0;
                //foreach (int value in values)
                //{
                //   if (value > maxValue)
                //      maxValue = value;
                //   if (value < mFirstField)
                //      mFirstField = (byte)value;
                //}
                // we create an array for enum values 0-LastBFvalue
                mFields = new IBitField[LastBF + 1];
                // mNumBFs = (byte) (maxValue+1);
            }
            else
            {
                mFields = null;
            }
            Reset(); // reset IEnumerable index.
        }
Example #3
0
        public PtrRecord()
        {
            testFlags       = new TestFlagsField(this);
            parametricFlags = new ParametricFlagsField(this);
            optionalFlags   = new OptionalFlagsField(this);

            AddField(FieldName.TEST_NUM.ToString(), testNumber);
            AddField(FieldName.HEAD_NUM.ToString(), headNumber);
            AddField(FieldName.SITE_NUM.ToString(), siteNumber);
            AddField(FieldName.TEST_FLG.ToString(), testFlags);
            AddField(FieldName.PARM_FLG.ToString(), parametricFlags);
            AddField(FieldName.RESULT.ToString(), result);
            AddField(FieldName.TEST_TXT.ToString(), testLabel);
            AddField(FieldName.ALARM_ID.ToString(), alarmName);
            AddField(FieldName.OPT_FLAG.ToString(), optionalFlags);
            AddField(FieldName.RES_SCAL.ToString(), resultScale);
            AddField(FieldName.LLM_SCAL.ToString(), lowLimitScale);
            AddField(FieldName.HLM_SCAL.ToString(), highLimitScale);
            AddField(FieldName.LO_LIMIT.ToString(), lowLimit);
            AddField(FieldName.HI_LIMIT.ToString(), highLimit);
            AddField(FieldName.UNITS.ToString(), units);
            AddField(FieldName.C_RESFMT.ToString(), resultFormat);
            AddField(FieldName.C_LLMFMT.ToString(), lowLimitFormat);
            AddField(FieldName.C_HLMFMT.ToString(), highLimitFormat);
            AddField(FieldName.LO_SPEC.ToString(), lowSpecificationLimit);
            AddField(FieldName.HI_SPEC.ToString(), highSpecificationLimit);
        }
Example #4
0
 /// <summary>
 /// Construct a "device register" -- the read and write operations are delegated to BitFields
 /// (which could be part of memory mapped registers or other more complicated objects).
 /// Although an IRegDriver is specified, it isn't directly used by AddrDataReg32 but it is
 /// "forwarded" to the BitFields (addrField, dataField, rwField) which may be required for
 /// "control stream operations".
 ///
 /// If rwField is non-null, it will be set to 1 for writes and 0 for reads
 ///
 /// *** THIS IMPLEMENTATION ASSUMES ALL IBitField OBJECTS BELONG TO THE SAME REGISTER ***
 /// </summary>
 /// <param name="name">register name</param>
 /// <param name="offset">offset (internal "device address" of the register ... written to 'addrField')</param>
 /// <param name="bfType">enumerated type of BitFields contained by this register.</param>
 /// <param name="driver">the default driver, not directly used by AddrDataReg32 but may be used by the addrField, dataField, rwField</param>
 /// <param name="addrField">BitField for the device's internal register address (offset will be written to this)</param>
 /// <param name="dataField">BitField for the device's internal register data</param>
 /// <param name="rwField">Optional BitField for indicating a read or write operation</param>
 public AddrDataReg32(string name,
                      Int32 offset,
                      Type bfType,
                      IRegDriver driver,
                      IBitField addrField,
                      IBitField dataField,
                      IBitField rwField)
     : this(name, offset, bfType, driver, RegType.RW, addrField, dataField, rwField, 0, 1)
 {
 }
Example #5
0
        public static Byte GetBitCount(IBitField BitField, bool State)
        {
            Byte count = 0;

            for (Byte X = 0; X <= BitField.MaxBitNumber; X++)
            {
                if (BitField[(Byte)X] == State)
                {
                    count++;
                }
            }
            return(count);
        }
Example #6
0
 /// <summary>
 /// Construct a "device register" -- the read and write operations are delegated to BitFields
 /// (which could be part of memory mapped registers or other more complicated objects).
 /// Although an IRegDriver is specified, it isn't directly used by AddrDataReg32 but it is
 /// "forwarded" to the BitFields (addrField, dataField, rwField) which may be required for
 /// "control stream operations".
 ///
 /// If rwField is non-null, it will be set to writeValue for writes and readValue for reads
 ///
 /// *** THIS IMPLEMENTATION ASSUMES ALL IBitField OBJECTS BELONG TO THE SAME REGISTER ***
 /// </summary>
 /// <param name="name">register name</param>
 /// <param name="offset">offset (internal "device address" of the register ... written to 'addrField')</param>
 /// <param name="bfType">enumerated type of BitFields contained by this register.</param>
 /// <param name="driver">the default driver, not directly used by AddrDataReg32 but may be used by the addrField, dataField, rwField</param>
 /// <param name="regType">register type/attribute (one or more bits from RegType, e.g. Cmd|RO).</param>
 /// <param name="addrField">BitField for the device's internal register address (offset will be written to this)</param>
 /// <param name="dataField">BitField for the device's internal register data</param>
 /// <param name="rwField">Optional BitField for indicating a read or write operation</param>
 /// <param name="readValue">The value written to rwField for read operations</param>
 /// <param name="writeValue">The value written to rwField for write operations</param>
 public AddrDataReg32(string name,
                      Int32 offset,
                      Type bfType,
                      IRegDriver driver,
                      RegType regType,
                      IBitField addrField,
                      IBitField dataField,
                      IBitField rwField,
                      int readValue,
                      int writeValue)
     : base(name, offset, bfType, driver, regType)
 {
     mAddrField  = addrField;
     mDataField  = dataField;
     mRwField    = rwField;
     mReadValue  = readValue;
     mWriteValue = writeValue;
 }
        /// <summary>
        /// This method converts the struct into a string of binary values.
        /// The length of the string will be equal to the number of bits in the struct
        /// The least significant bit will be on the right in the string.
        /// </summary>
        /// <param name="obj">An instance of a struct that implements the interface IBitField.</param>
        /// <returns>A string representing the binary value of tbe bit field.</returns>
        public static string ToBinaryString(this IBitField obj)
        {
            BitFieldNumberOfBitsAttribute bitField = (obj.GetType().GetCustomAttribute(typeof(BitFieldNumberOfBitsAttribute)) as BitFieldNumberOfBitsAttribute);

            if (bitField == null)
            {
                throw new Exception(string.Format("The attribute 'BitFieldNumberOfBitsAttribute' has to be added to the struct '{0}'.", obj.GetType().Name));
            }

            StringBuilder sb = new StringBuilder(bitField.BitCount);

            ulong bitFieldValue = obj.ToUInt64();

            for (int i = bitField.BitCount - 1; i >= 0; i--)
            {
                sb.Append(((bitFieldValue & (1UL << i)) > 0) ? "1" : "0");
            }

            return(sb.ToString());
        }
Example #8
0
        /// <summary>
        /// Adds a Bit Field to a Register
        /// </summary>
        /// <param name="bf">The BitField being added to the register</param>
        /// <remarks>The BitField passed in must be an implementation of the Type.BFType
        /// passed in on construction of the register.  If not, an exception is thrown.</remarks>
        public void AddField(IBitField bf)
        {
            if (mBFType == null)
            {
                throw new Exception("Can't add bit fields to a register that does not contain bitfields.");
            }

            // remove all characters up to and including the "_" (the RegName) in order to
            //  get just the BitField portion of the name.
            string bfName = bf.Name;
            //int underScoreLocation = bfName.LastIndexOf("_");
            int underScoreLocation = bfName.LastIndexOf(":", StringComparison.InvariantCultureIgnoreCase);

            int firstCharOfFieldName = underScoreLocation + 1;

            if (firstCharOfFieldName == 0)
            {
                // no "_" was found, so we must strip out any " "s instead
                firstCharOfFieldName = bfName.LastIndexOf(" ", StringComparison.InvariantCultureIgnoreCase) +
                                       " ".Length;
            }

            string bfNameOnly = bfName.Substring(firstCharOfFieldName, bfName.Length - firstCharOfFieldName);
            int    index;

            // see if the BFname exists in the enum list or not.
            try
            {
                // If it exists we will have it's index position into the containing array.
                index = (int)Enum.Parse(mBFType, bfNameOnly);
            }
            catch
            {
                // the BitField passed in was not implementing one of the valid BitFields
                // for this register.
                throw new ArgumentException("No enum match found for BitField name '" +
                                            bfName + "'");
            }

            SetField(index, bf);
        }
Example #9
0
        public TsrRecord()
        {
            optionalDataFlag = new OptionalDataFlagField(this);

            AddField(FieldName.HEAD_NUM.ToString(), headNumber);
            AddField(FieldName.SITE_NUM.ToString(), siteNumber);
            AddField(FieldName.TEST_TYP.ToString(), testType);
            AddField(FieldName.TEST_NUM.ToString(), testNumber);
            AddField(FieldName.EXEC_CNT.ToString(), executionCount);
            AddField(FieldName.FAIL_CNT.ToString(), failCount);
            AddField(FieldName.ALRM_CNT.ToString(), alarmCount);
            AddField(FieldName.TEST_NAM.ToString(), testName);
            AddField(FieldName.SEQ_NAME.ToString(), sequencerName);
            AddField(FieldName.TEST_LBL.ToString(), testLabel);
            AddField(FieldName.OPT_FLAG.ToString(), optionalDataFlag);
            AddField(FieldName.TEST_TIM.ToString(), executionTime);
            AddField(FieldName.TEST_MIN.ToString(), lowestResultValue);
            AddField(FieldName.TEST_MAX.ToString(), highestResultValue);
            AddField(FieldName.TST_SUMS.ToString(), resultValuesSum);
            AddField(FieldName.TST_SQRS.ToString(), resultValuesSquareSum);
        }
Example #10
0
 /// <summary>
 /// Adds a Bit Field to a Register
 /// </summary>
 /// <param name="bf">The BitField being added to the register</param>
 /// <remarks>The BitField passed in must be an implementation of the Type.BFType
 /// passed in on construction of the register.  If not, an exception is thrown.</remarks>
 public void AddField(IBitField bf)
 {
     throw new NotImplementedException("Not supported for Buffer32");
 }
Example #11
0
        /// <summary>
        /// Utility function to compare two sets of registers with the intent of ensuring
        /// replacing the "old style" of register creation with RegisterSets results in
        /// the same register and bit field collections
        /// </summary>
        /// <param name="name">name of register collection</param>
        /// <param name="referenceSet">register collection to be matched</param>
        /// <param name="candidateSet">register collection that should match 'referenceSet'</param>
        /// <returns>Description of the differences</returns>
        public static string Compare(string name, IRegister[] referenceSet, IRegister[] candidateSet)
        {
            StringBuilder buffer = new StringBuilder();

            buffer.AppendFormat("------ Compare( '{0}', IRegister[], IRegister[] ) ------\n", name);
            if (referenceSet == null && candidateSet == null)
            {
                buffer.AppendFormat("Info: both register sets are null\n");
                return(buffer.ToString());
            }
            if (referenceSet == null)
            {
                buffer.AppendFormat("Extra: referenceSet is null, candidateSet[{0}]\n", candidateSet.Length);
                return(buffer.ToString());
            }
            if (candidateSet == null)
            {
                buffer.AppendFormat("Missing: referenceSet[{0}], candidateSet is null\n", referenceSet.Length);
                return(buffer.ToString());
            }
            for (int index = 0; index < referenceSet.Length; index++)
            {
                try
                {
                    IRegister reference = referenceSet[index];
                    IRegister candidate = (index < candidateSet.Length) ? candidateSet[index] : null;
                    if (reference == null)
                    {
                        if (candidate != null)
                        {
                            buffer.AppendFormat("Extra:   referenceSet does not contain {0}\n", candidate.Name);
                        }
                        continue;
                    }
                    if (candidate == null)
                    {
                        buffer.AppendFormat("Missing: candidateSet does not contain {0}\n", reference.Name);
                        continue;
                    }
                    if (string.Compare(reference.Name, candidate.Name, StringComparison.InvariantCultureIgnoreCase) !=
                        0)
                    {
                        buffer.AppendFormat("Naming:  {0} != {1}\n", reference.Name, candidate.Name);
                        continue;
                    }
                    if (reference.RegType != candidate.RegType)
                    {
                        buffer.AppendFormat("RegType:  {0}.{1} != {2}.{3}\n",
                                            reference.NameBase,
                                            reference.RegType,
                                            candidate.NameBase,
                                            candidate.RegType);
                    }
                    if (reference.Offset != candidate.Offset)
                    {
                        buffer.AppendFormat("Offset:  {0}.0x{1:x} != {2}.0x{3:x}\n",
                                            reference.NameBase,
                                            reference.Offset,
                                            candidate.NameBase,
                                            candidate.Offset);
                    }
                    if (reference.NumBitFields != candidate.NumBitFields ||
                        reference.FirstBF != candidate.FirstBF ||
                        reference.LastBF != candidate.LastBF)
                    {
                        buffer.AppendFormat("Bitfield: {6} # fields ({0},{1},{2}) vs. ({3},{4},{5})\n",
                                            reference.NumBitFields,
                                            reference.FirstBF,
                                            reference.LastBF,
                                            candidate.NumBitFields,
                                            candidate.FirstBF,
                                            candidate.LastBF,
                                            reference.Name);
                        continue;
                    }
                    if (reference.NumBitFields > 0)
                    {
                        for (int j = reference.FirstBF; j < reference.LastBF; j++)
                        {
                            IBitField referenceBF = reference.GetField((uint)j);
                            IBitField candidateBF = candidate.GetField((uint)j);
                            if (referenceBF == null)
                            {
                                if (candidateBF != null)
                                {
                                    buffer.AppendFormat("ExtraBF: reference does not contain {0}.{1}\n",
                                                        reference.Name,
                                                        candidateBF.Name);
                                }
                                continue;
                            }
                            if (candidateBF == null)
                            {
                                buffer.AppendFormat("MissingBF: candidate does not contain {0}.{1}\n",
                                                    reference.Name,
                                                    referenceBF.Name);
                                continue;
                            }
                            if (string.Compare(referenceBF.Name,
                                               candidateBF.Name,
                                               StringComparison.InvariantCultureIgnoreCase) != 0)
                            {
                                buffer.AppendFormat("NameBF: {0} != {1}\n", referenceBF.Name, candidateBF.Name);
                                continue;
                            }
                            if (referenceBF.StartBit != candidateBF.StartBit ||
                                referenceBF.EndBit != candidateBF.EndBit ||
                                referenceBF.Mask != candidateBF.Mask)
                            {
                                buffer.AppendFormat("MismatchBF: {6}.{7} ({0},{1},0x{2:x}) vs ({3},{4},0x{5:x})\n",
                                                    referenceBF.StartBit,
                                                    referenceBF.EndBit,
                                                    referenceBF.Mask,
                                                    candidateBF.StartBit,
                                                    candidateBF.EndBit,
                                                    candidateBF.Mask,
                                                    reference.Name,
                                                    referenceBF.Name);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    buffer.AppendFormat("Error at index {0}: {1}\n", index, ex.Message);
                }
            }
            for (int index = referenceSet.Length; index < candidateSet.Length; index++)
            {
                IRegister candidate = candidateSet[index];
                if (candidate != null)
                {
                    buffer.AppendFormat("Extra: referenceSet does not contain {0}\n", candidate.Name);
                }
            }
            return(buffer.ToString());
        }
Example #12
0
 public IBitField From(IBitField value)
 {
     _bitfield = _bitfield.From(((HdknBitField)value)._bitfield);
     return this;
 }
Example #13
0
 public IBitField And(IBitField value)
 {
     _bitfield = _bitfield.And(((HdknBitField)value)._bitfield);
     return this;
 }
Example #14
0
 public IBitField Xor(IBitField value)
 {
     _bitfield = _bitfield.Xor(((HdknBitField)value)._bitfield);
     return this;
 }
Example #15
0
 /// <summary>
 /// Replace the BitField at index 'i' of the containing array with 'bf'
 /// </summary>
 /// <param name="i">index into containing array</param>
 /// <param name="bf">BitField being inserted</param>
 protected void SetField(int i, IBitField bf)
 {
     mFields[i] = bf;
 }