/// <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()); }