OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [orpsocv2/] [bench/] [sysc/] [src/] [Utils.cpp] - Blame information for rev 740

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 63 julius
// ----------------------------------------------------------------------------
2
 
3
// GDB Server Utilties: implementation
4
 
5
// Copyright (C) 2008  Embecosm Limited <info@embecosm.com>
6
 
7
// Contributor Jeremy Bennett <jeremy.bennett@embecosm.com>
8
 
9
// This file is part of the cycle accurate model of the OpenRISC 1000 based
10
// system-on-chip, ORPSoC, built using Verilator.
11
 
12
// This program is free software: you can redistribute it and/or modify it
13
// under the terms of the GNU Lesser General Public License as published by
14
// the Free Software Foundation, either version 3 of the License, or (at your
15
// option) any later version.
16
 
17
// This program is distributed in the hope that it will be useful, but WITHOUT
18
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
20
// License for more details.
21
 
22
// You should have received a copy of the GNU Lesser General Public License
23
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 
25
// ----------------------------------------------------------------------------
26
 
27
// $Id: Utils.cpp 324 2009-03-07 09:42:52Z jeremy $
28
 
29
#include "Utils.h"
30
 
31
//-----------------------------------------------------------------------------
32
//!Utility to give the value of a hex char
33
 
34
//! @param[in] ch  A character representing a hexadecimal digit. Done as -1,
35
//!                for consistency with other character routines, which can
36
//!                use -1 as EOF.
37
 
38
//! @return  The value of the hex character, or -1 if the character is
39
//!          invalid.
40
//-----------------------------------------------------------------------------
41 462 julius
uint8_t Utils::char2Hex(int c)
42 63 julius
{
43 462 julius
        return ((c >= 'a') && (c <= 'f')) ? c - 'a' + 10 :
44
            ((c >= '0') && (c <= '9')) ? c - '0' :
45
            ((c >= 'A') && (c <= 'F')) ? c - 'A' + 10 : -1;
46 63 julius
 
47 462 julius
}                               // char2Hex ()
48 63 julius
 
49
//-----------------------------------------------------------------------------
50
//! Utility mapping a value to hex character
51
 
52
//! @param[in] d  A hexadecimal digit. Any non-hex digit returns a NULL char
53
//-----------------------------------------------------------------------------
54 462 julius
const char Utils::hex2Char(uint8_t d)
55 63 julius
{
56 462 julius
        static const char map[] = "0123456789abcdef"
57
            "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
58
            "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
59
            "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
60
            "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
61
            "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
62
            "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
63
            "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
64
            "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
65
            "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
66
            "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
67
            "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
68
            "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
69
            "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
70
            "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
71
            "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
72 63 julius
 
73 462 julius
        return map[d];
74 63 julius
 
75 462 julius
}                               // hex2Char ()
76 63 julius
 
77
//-----------------------------------------------------------------------------
78
//! Convert a register to a hex digit string
79
 
80
//! The supplied 32-bit value is converted to an 8 digit hex string. The
81
//! string is null terminated for convenience. The hex string follows the
82
//! universal printing convention: most significant digit on the left.
83
 
84
//! @param[in]  val  The value to convert
85
//! @param[out] buf  The buffer for the text string
86
//-----------------------------------------------------------------------------
87 462 julius
void Utils::reg2Hex(uint32_t val, char *buf)
88 63 julius
{
89 462 julius
        for (int n = 7; n >= 0; n--) {
90
                buf[n] = hex2Char(val & 0xf);
91
                val /= 16;
92
        }
93 63 julius
 
94 462 julius
        buf[8] = 0;              // Useful to terminate as string
95 63 julius
 
96 462 julius
}                               // reg2hex ()
97 63 julius
 
98
//-----------------------------------------------------------------------------
99
//! Convert a hex digit string to a register value
100
 
101
//! The supplied 8 digit hex string follows the universal printing convention:
102
//! most significant digit on the left. It is converted to a 32-bit value.
103
 
104
//! @param[in] buf  The buffer with the hex string
105
 
106
//! @return  The value to convert
107
//-----------------------------------------------------------------------------
108 462 julius
uint32_t Utils::hex2Reg(char *buf)
109 63 julius
{
110 462 julius
        uint32_t val = 0;        // The result
111 63 julius
 
112 462 julius
        for (int n = 0; n < 8; n++) {
113
                val = val * 16 + char2Hex(buf[n]);
114
        }
115 63 julius
 
116 462 julius
        return val;
117 63 julius
 
118 462 julius
}                               // hex2reg ()
119 63 julius
 
120
//-----------------------------------------------------------------------------
121
//! Convert an ASCII character string to pairs of hex digits
122
 
123
//! Both source and destination are null terminated.
124
 
125
//! @param[out] dest  Buffer to store the hex digit pairs (null terminated)
126
//! @param[in]  src   The ASCII string (null terminated)                      */
127
//-----------------------------------------------------------------------------
128 462 julius
void Utils::ascii2Hex(char *dest, char *src)
129 63 julius
{
130 462 julius
        int i;
131 63 julius
 
132 462 julius
        // Step through converting the source string
133
        for (i = 0; src[i] != '\0'; i++) {
134
                char ch = src[i];
135 63 julius
 
136 462 julius
                dest[i * 2] = hex2Char(ch >> 4 & 0xf);
137
                dest[i * 2 + 1] = hex2Char(ch & 0xf);
138
        }
139 63 julius
 
140 462 julius
        dest[i * 2] = '\0';
141 63 julius
 
142 462 julius
}                               // ascii2hex ()
143 63 julius
 
144
//-----------------------------------------------------------------------------
145
//! Convert pairs of hex digits to an ASCII character string
146
 
147
//! Both source and destination are null terminated.
148
 
149
//! @param[out] dest  The ASCII string (null terminated)
150
//! @param[in]  src   Buffer holding the hex digit pairs (null terminated)
151
//-----------------------------------------------------------------------------
152 462 julius
void Utils::hex2Ascii(char *dest, char *src)
153 63 julius
{
154 462 julius
        int i;
155 63 julius
 
156 462 julius
        // Step through convering the source hex digit pairs
157
        for (i = 0; src[i * 2] != '\0' && src[i * 2 + 1] != '\0'; i++) {
158
                dest[i] = ((char2Hex(src[i * 2]) & 0xf) << 4) |
159
                    (char2Hex(src[i * 2 + 1]) & 0xf);
160
        }
161 63 julius
 
162 462 julius
        dest[i] = '\0';
163 63 julius
 
164 462 julius
}                               // hex2ascii ()
165 63 julius
 
166
//-----------------------------------------------------------------------------
167
//! "Unescape" RSP binary data
168
 
169
//! '#', '$' and '}' are escaped by preceding them by '}' and oring with 0x20.
170
 
171
//! This function reverses that, modifying the data in place.
172
 
173
//! @param[in] buf  The array of bytes to convert
174
//! @para[in]  len   The number of bytes to be converted
175
 
176
//! @return  The number of bytes AFTER conversion
177
//-----------------------------------------------------------------------------
178 462 julius
int Utils::rspUnescape(char *buf, int len)
179 63 julius
{
180 462 julius
        int fromOffset = 0;      // Offset to source char
181
        int toOffset = 0;        // Offset to dest char
182 63 julius
 
183 462 julius
        while (fromOffset < len) {
184
                // Is it escaped
185
                if ('}' == buf[fromOffset]) {
186
                        fromOffset++;
187
                        buf[toOffset] = buf[fromOffset] ^ 0x20;
188
                } else {
189
                        buf[toOffset] = buf[fromOffset];
190
                }
191
 
192
                fromOffset++;
193
                toOffset++;
194 63 julius
        }
195
 
196 462 julius
        return toOffset;
197 63 julius
 
198 462 julius
}                               // rspUnescape () */
199 63 julius
 
200
//-----------------------------------------------------------------------------
201
//! Convert 32-bit value from host to target endianness
202
 
203
//! The target endianness is determined by the #define of TARGET_BIG_ENDIAN and
204
//! TARGET_LITTLE_ENDIAN. Not definining either is a compile time error.
205
 
206
//! @param[in] hostVal  The value in host endianness
207
 
208
//! @return  The value in target endianness
209
//-----------------------------------------------------------------------------
210 462 julius
uint32_t Utils::htotl(uint32_t hostVal)
211 63 julius
{
212 462 julius
        uint8_t targetBytes[4];
213 63 julius
 
214
#ifdef TARGET_BIG_ENDIAN
215 462 julius
        targetBytes[0] = hostVal / 256 / 256 / 256;
216
        targetBytes[1] = hostVal / 256 / 256;
217
        targetBytes[2] = hostVal / 256;
218
        targetBytes[3] = hostVal;
219 63 julius
#elif defined TARGET_LITTLE_ENDIAN
220 462 julius
        targetBytes[0] = hostVal;
221
        targetBytes[1] = hostVal / 256;
222
        targetBytes[2] = hostVal / 256 / 256;
223
        targetBytes[3] = hostVal / 256 / 256 / 256;
224 63 julius
#else
225
#error Must specify TARGET_BIG_ENDIAN or TARGET_LITTLE_ENDIAN
226
#endif
227
 
228 462 julius
        return *((uint32_t *) targetBytes);
229 63 julius
 
230 462 julius
}                               // htotl ()
231 63 julius
 
232
//-----------------------------------------------------------------------------
233
//! Convert 32-bit value from target to host endianness
234
 
235
//! The target endianness is determined by the #define of TARGET_BIG_ENDIAN and
236
//! TARGET_LITTLE_ENDIAN. Not definining either is a compile time error.
237
 
238
//! @param[in] targetVal  The value in target endianness
239
 
240
//! @return  The value in target endianness
241
//-----------------------------------------------------------------------------
242 462 julius
uint32_t Utils::ttohl(uint32_t targetVal)
243 63 julius
{
244 462 julius
        uint8_t *targetBytes = (uint8_t *) (&targetVal);
245
        uint32_t hostVal;
246 63 julius
 
247
#ifdef TARGET_BIG_ENDIAN
248 462 julius
        hostVal = targetBytes[0];
249
        hostVal = hostVal * 256 + targetBytes[1];
250
        hostVal = hostVal * 256 + targetBytes[2];
251
        hostVal = hostVal * 256 + targetBytes[3];
252 63 julius
#elif defined TARGET_LITTLE_ENDIAN
253 462 julius
        hostVal = targetBytes[3];
254
        hostVal = hostVal * 256 + targetBytes[2];
255
        hostVal = hostVal * 256 + targetBytes[1];
256
        hostVal = hostVal * 256 + targetBytes[0];
257 63 julius
#else
258 462 julius
#error Must specify TARGET_BIG_ENDIAN or TARGET_LITTLE_ENDIAN
259 63 julius
#endif
260
 
261 462 julius
        return hostVal;
262 63 julius
 
263 462 julius
}                               // ttohl ()

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.