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