xref: /freebsd-src/contrib/opencsd/decoder/source/trc_printable_elem.cpp (revision 46e6e290975f19ea62d03f90ac3e523af4dae557)
1*c120c564SAndrew Turner /*
2*c120c564SAndrew Turner  * \file       trc_printable_elem.cpp
3*c120c564SAndrew Turner  * \brief      OpenCSD :
4*c120c564SAndrew Turner  *
5*c120c564SAndrew Turner  * \copyright  Copyright (c) 2015, ARM Limited. All Rights Reserved.
6*c120c564SAndrew Turner  */
7*c120c564SAndrew Turner 
8*c120c564SAndrew Turner /*
9*c120c564SAndrew Turner  * Redistribution and use in source and binary forms, with or without modification,
10*c120c564SAndrew Turner  * are permitted provided that the following conditions are met:
11*c120c564SAndrew Turner  *
12*c120c564SAndrew Turner  * 1. Redistributions of source code must retain the above copyright notice,
13*c120c564SAndrew Turner  * this list of conditions and the following disclaimer.
14*c120c564SAndrew Turner  *
15*c120c564SAndrew Turner  * 2. Redistributions in binary form must reproduce the above copyright notice,
16*c120c564SAndrew Turner  * this list of conditions and the following disclaimer in the documentation
17*c120c564SAndrew Turner  * and/or other materials provided with the distribution.
18*c120c564SAndrew Turner  *
19*c120c564SAndrew Turner  * 3. Neither the name of the copyright holder nor the names of its contributors
20*c120c564SAndrew Turner  * may be used to endorse or promote products derived from this software without
21*c120c564SAndrew Turner  * specific prior written permission.
22*c120c564SAndrew Turner  *
23*c120c564SAndrew Turner  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND
24*c120c564SAndrew Turner  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25*c120c564SAndrew Turner  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26*c120c564SAndrew Turner  * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27*c120c564SAndrew Turner  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28*c120c564SAndrew Turner  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29*c120c564SAndrew Turner  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30*c120c564SAndrew Turner  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31*c120c564SAndrew Turner  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32*c120c564SAndrew Turner  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33*c120c564SAndrew Turner  */
34*c120c564SAndrew Turner 
35*c120c564SAndrew Turner #include "common/trc_printable_elem.h"
36*c120c564SAndrew Turner #include <cassert>
37*c120c564SAndrew Turner #include <cstring>
38*c120c564SAndrew Turner #if defined(_MSC_VER) && (_MSC_VER < 1900)
39*c120c564SAndrew Turner  /** VS2010 does not support inttypes - remove when VS2010 support is dropped */
40*c120c564SAndrew Turner #define __PRI64_PREFIX "ll"
41*c120c564SAndrew Turner #define PRIX64 __PRI64_PREFIX "X"
42*c120c564SAndrew Turner #define PRIu64 __PRI64_PREFIX "u"
43*c120c564SAndrew Turner #define PRIu32 "u"
44*c120c564SAndrew Turner #else
45*c120c564SAndrew Turner #include <cinttypes>
46*c120c564SAndrew Turner #endif
47*c120c564SAndrew Turner 
getValStr(std::string & valStr,const int valTotalBitSize,const int valValidBits,const uint64_t value,const bool asHex,const int updateBits)48*c120c564SAndrew Turner void trcPrintableElem::getValStr(std::string &valStr, const int valTotalBitSize, const int valValidBits, const uint64_t value, const bool asHex /* = true*/, const int updateBits /* = 0*/)
49*c120c564SAndrew Turner {
50*c120c564SAndrew Turner     static char szStrBuffer[128];
51*c120c564SAndrew Turner     static char szFormatBuffer[32];
52*c120c564SAndrew Turner 
53*c120c564SAndrew Turner     assert((valTotalBitSize >= 4) && (valTotalBitSize <= 64));
54*c120c564SAndrew Turner 
55*c120c564SAndrew Turner     valStr = "0x";
56*c120c564SAndrew Turner 
57*c120c564SAndrew Turner     if(asHex)
58*c120c564SAndrew Turner     {
59*c120c564SAndrew Turner         int numHexChars = valTotalBitSize / 4;
60*c120c564SAndrew Turner         numHexChars += ((valTotalBitSize % 4) > 0) ? 1 : 0;
61*c120c564SAndrew Turner 
62*c120c564SAndrew Turner         int validChars = valValidBits / 4;
63*c120c564SAndrew Turner         if((valValidBits % 4) > 0) validChars++;
64*c120c564SAndrew Turner 		if (validChars < numHexChars)
65*c120c564SAndrew Turner 		{
66*c120c564SAndrew Turner 			int QM = numHexChars - validChars;
67*c120c564SAndrew Turner 			while (QM)
68*c120c564SAndrew Turner 			{
69*c120c564SAndrew Turner 				QM--;
70*c120c564SAndrew Turner 				valStr += "?";
71*c120c564SAndrew Turner 			}
72*c120c564SAndrew Turner 		}
73*c120c564SAndrew Turner         if(valValidBits > 32)
74*c120c564SAndrew Turner         {
75*c120c564SAndrew Turner             sprintf(szFormatBuffer,"%%0%dllX",validChars);  // create the format
76*c120c564SAndrew Turner             sprintf(szStrBuffer,szFormatBuffer,value); // fill the buffer
77*c120c564SAndrew Turner         }
78*c120c564SAndrew Turner         else
79*c120c564SAndrew Turner         {
80*c120c564SAndrew Turner             sprintf(szFormatBuffer,"%%0%dlX",validChars);  // create the format
81*c120c564SAndrew Turner             sprintf(szStrBuffer,szFormatBuffer,(uint32_t)value); // fill the buffer
82*c120c564SAndrew Turner         }
83*c120c564SAndrew Turner         valStr+=szStrBuffer;
84*c120c564SAndrew Turner         if(valValidBits < valTotalBitSize)
85*c120c564SAndrew Turner         {
86*c120c564SAndrew Turner             sprintf(szStrBuffer," (%d:0)", valValidBits-1);
87*c120c564SAndrew Turner             valStr+=szStrBuffer;
88*c120c564SAndrew Turner         }
89*c120c564SAndrew Turner 
90*c120c564SAndrew Turner         if(updateBits)
91*c120c564SAndrew Turner         {
92*c120c564SAndrew Turner             uint64_t updateMask = ~0ULL;
93*c120c564SAndrew Turner             updateMask >>= 64-updateBits;
94*c120c564SAndrew Turner             sprintf(szStrBuffer," ~[0x%" PRIX64 "]",value & updateMask);
95*c120c564SAndrew Turner             valStr+=szStrBuffer;
96*c120c564SAndrew Turner         }
97*c120c564SAndrew Turner     }
98*c120c564SAndrew Turner     else
99*c120c564SAndrew Turner     {
100*c120c564SAndrew Turner         valStr = "";
101*c120c564SAndrew Turner         if(valValidBits < valTotalBitSize)
102*c120c564SAndrew Turner             valStr += "??";
103*c120c564SAndrew Turner         if(valValidBits > 32)
104*c120c564SAndrew Turner         {
105*c120c564SAndrew Turner             sprintf(szStrBuffer,"%" PRIu64 ,value);
106*c120c564SAndrew Turner         }
107*c120c564SAndrew Turner         else
108*c120c564SAndrew Turner         {
109*c120c564SAndrew Turner             sprintf(szStrBuffer,"%" PRIu32 ,(uint32_t)value);
110*c120c564SAndrew Turner         }
111*c120c564SAndrew Turner         valStr +=  szStrBuffer;
112*c120c564SAndrew Turner         if(valValidBits < valTotalBitSize)
113*c120c564SAndrew Turner         {
114*c120c564SAndrew Turner             sprintf(szStrBuffer," (%d:0)", valValidBits-1);
115*c120c564SAndrew Turner             valStr+=szStrBuffer;
116*c120c564SAndrew Turner         }
117*c120c564SAndrew Turner     }
118*c120c564SAndrew Turner }
119*c120c564SAndrew Turner 
120*c120c564SAndrew Turner 
121*c120c564SAndrew Turner /* End of File trc_printable_elem.cpp */
122