|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 using System; |
|
18 using System.Text; |
|
19 using System.Collections.Generic; |
|
20 using CrashItemLib.Crash; |
|
21 using CrashItemLib.Crash.Base; |
|
22 using CrashItemLib.Crash.Container; |
|
23 using CrashItemLib.Crash.Registers; |
|
24 using CrashItemLib.Crash.Registers.Visualization.Bits; |
|
25 |
|
26 namespace CrashItemLib.Crash.Registers.Visualization.Utilities |
|
27 { |
|
28 public static class VisUtilities |
|
29 { |
|
30 public static IEnumerable<CIRegisterVisBit> ExtractBits( CIContainer aContainer, uint aValue, string aByte3Mask, string aByte2Mask, string aByte1Mask, string aByte0Mask ) |
|
31 { |
|
32 return ExtractBits( aContainer, aValue, aByte3Mask + aByte2Mask + aByte1Mask + aByte0Mask ); |
|
33 } |
|
34 |
|
35 public static IEnumerable<CIRegisterVisBit> ExtractBits( CIContainer aContainer, uint aValue, string aMask ) |
|
36 { |
|
37 List<CIRegisterVisBit> ret = new List<CIRegisterVisBit>(); |
|
38 // |
|
39 int shift = 0; |
|
40 int sigBitCount = 0; |
|
41 uint mask = MakeMask( aMask, out shift, out sigBitCount ); |
|
42 uint maskedValue = ( aValue & mask ) >> shift; |
|
43 |
|
44 // Now make bits |
|
45 for ( int i = sigBitCount-1; i >= 0; i-- ) |
|
46 { |
|
47 mask = (uint) ( 1u << i ); |
|
48 uint value = ( maskedValue & mask ) >> i; |
|
49 |
|
50 CIRegisterVisBit bit = new CIRegisterVisBit( aContainer ); |
|
51 if ( value != 0 ) |
|
52 { |
|
53 bit.Value = TBit.EBitSet; |
|
54 } |
|
55 ret.Add( bit ); |
|
56 } |
|
57 |
|
58 return ret.ToArray(); |
|
59 } |
|
60 |
|
61 public static TBit ExtractBit( uint aValue, string aByte1Mask, string aByte0Mask ) |
|
62 { |
|
63 string b0 = EnsureByteMask( aByte0Mask ); |
|
64 string b1 = EnsureByteMask( aByte1Mask ); |
|
65 // |
|
66 int shift; |
|
67 uint mask = MakeMask( b1 + b0, out shift ); |
|
68 // |
|
69 uint value = aValue & mask; |
|
70 value >>= shift; |
|
71 // |
|
72 if ( value == 1 ) |
|
73 { |
|
74 return TBit.EBitSet; |
|
75 } |
|
76 |
|
77 return TBit.EBitClear; |
|
78 } |
|
79 |
|
80 public static TBit ExtractBit( uint aValue, string aByte3Mask, string aByte2Mask, string aByte1Mask, string aByte0Mask ) |
|
81 { |
|
82 string b0 = EnsureByteMask( aByte0Mask ); |
|
83 string b1 = EnsureByteMask( aByte1Mask ); |
|
84 string b2 = EnsureByteMask( aByte2Mask ); |
|
85 string b3 = EnsureByteMask( aByte3Mask ); |
|
86 // |
|
87 int shift; |
|
88 uint mask = MakeMask( b3 + b2 + b1 + b0, out shift ); |
|
89 // |
|
90 uint value = aValue & mask; |
|
91 value >>= shift; |
|
92 // |
|
93 if ( value == 1 ) |
|
94 { |
|
95 return TBit.EBitSet; |
|
96 } |
|
97 |
|
98 return TBit.EBitClear; |
|
99 } |
|
100 |
|
101 // [ aByte0 ] |
|
102 public static uint MakeMask( string aByte ) |
|
103 { |
|
104 return MakeMask( string.Empty, string.Empty, string.Empty, aByte ); |
|
105 } |
|
106 |
|
107 // [ aByte1 ] [ aByte0 ] |
|
108 public static uint MakeMask( string aByte1, string aByte0 ) |
|
109 { |
|
110 return MakeMask( string.Empty, string.Empty, aByte1, aByte0 ); |
|
111 } |
|
112 |
|
113 // [ aByte3 ] [ aByte2 ] [ aByte1 ] [ aByte0 ] |
|
114 public static uint MakeMask( string aByte3, string aByte2, string aByte1, string aByte0 ) |
|
115 { |
|
116 int shift = 0; |
|
117 return MakeMask( aByte3, aByte2, aByte1, aByte0, out shift ); |
|
118 } |
|
119 |
|
120 public static uint MakeMask( string aByte3, string aByte2, string aByte1, string aByte0, out int aShiftAmount ) |
|
121 { |
|
122 string byteString3 = EnsureByteMask( aByte3 ); |
|
123 string byteString2 = EnsureByteMask( aByte2 ); |
|
124 string byteString1 = EnsureByteMask( aByte1 ); |
|
125 string byteString0 = EnsureByteMask( aByte0 ); |
|
126 string byteString = byteString3 + byteString2 + byteString1 + byteString0; |
|
127 // |
|
128 uint mask = MakeMask( byteString, out aShiftAmount ); |
|
129 // |
|
130 return mask; |
|
131 } |
|
132 |
|
133 public static uint MakeMask( string aSpec, out int aShiftAmount ) |
|
134 { |
|
135 int sigBitCount = 0; |
|
136 return MakeMask( aSpec, out aShiftAmount, out sigBitCount ); |
|
137 } |
|
138 |
|
139 public static uint MakeMask( string aSpec, out int aShiftAmount, out int aSignificantBitCount ) |
|
140 { |
|
141 bool setFirstBitIndex = false; |
|
142 aShiftAmount = 0; |
|
143 aSignificantBitCount = 0; |
|
144 uint mask = 0; |
|
145 |
|
146 // Loop through all characters in the mask, starting from the RHS, working |
|
147 // towards the left hand side. |
|
148 int count = aSpec.Length; |
|
149 for ( int bit = 0; bit < count; bit++ ) |
|
150 { |
|
151 // Get a character from the string, starting at the RHS |
|
152 char c = aSpec[ count - bit - 1 ]; |
|
153 // |
|
154 if ( c == KBitIsSet ) |
|
155 { |
|
156 mask |= (uint) ( 1u << bit ); |
|
157 if ( !setFirstBitIndex ) |
|
158 { |
|
159 aShiftAmount = bit; |
|
160 setFirstBitIndex = true; |
|
161 } |
|
162 ++aSignificantBitCount; |
|
163 } |
|
164 else if ( c == KBitIsClear ) |
|
165 { |
|
166 if ( !setFirstBitIndex ) |
|
167 { |
|
168 aShiftAmount = bit; |
|
169 setFirstBitIndex = true; |
|
170 } |
|
171 ++aSignificantBitCount; |
|
172 } |
|
173 else if ( c == KBitIsNotApplicable ) |
|
174 { |
|
175 } |
|
176 } |
|
177 // |
|
178 return mask; |
|
179 } |
|
180 |
|
181 public static string EnsureByteMask( string aMask ) |
|
182 { |
|
183 string ret = aMask.PadLeft( 8, '0' ); |
|
184 return ret; |
|
185 } |
|
186 |
|
187 public static string ToBinary( uint aValue ) |
|
188 { |
|
189 StringBuilder ret = new StringBuilder(); |
|
190 // |
|
191 for ( int i = 31; i >= 0; i-- ) |
|
192 { |
|
193 uint mask = (uint) ( 1u << i ); |
|
194 uint value = ( aValue & mask ) >> i; |
|
195 if ( value != 0 ) |
|
196 { |
|
197 ret.Append( '1' ); |
|
198 } |
|
199 else |
|
200 { |
|
201 ret.Append( '0' ); |
|
202 } |
|
203 } |
|
204 // |
|
205 return ret.ToString(); |
|
206 } |
|
207 |
|
208 #region Constants |
|
209 public const char KBitIsSet = '1'; |
|
210 public const char KBitIsClear = '0'; |
|
211 public const char KBitIsNotApplicable = '#'; |
|
212 #endregion |
|
213 } |
|
214 } |