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

Subversion Repositories openrisc

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

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
//-----------------------------------------------------------------------------
33
//!Utility to give the value of a hex char
34
 
35
//! @param[in] ch  A character representing a hexadecimal digit. Done as -1,
36
//!                for consistency with other character routines, which can
37
//!                use -1 as EOF.
38
 
39
//! @return  The value of the hex character, or -1 if the character is
40
//!          invalid.
41
//-----------------------------------------------------------------------------
42
uint8_t
43
Utils::char2Hex (int  c)
44
{
45
  return  ((c >= 'a') && (c <= 'f')) ? c - 'a' + 10 :
46
          ((c >= '0') && (c <= '9')) ? c - '0' :
47
          ((c >= 'A') && (c <= 'F')) ? c - 'A' + 10 : -1;
48
 
49
}       // char2Hex ()
50
 
51
 
52
//-----------------------------------------------------------------------------
53
//! Utility mapping a value to hex character
54
 
55
//! @param[in] d  A hexadecimal digit. Any non-hex digit returns a NULL char
56
//-----------------------------------------------------------------------------
57
const char
58
Utils::hex2Char (uint8_t  d)
59
{
60
  static const char map [] = "0123456789abcdef"
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
                             "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
73
                             "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
74
                             "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
75
                             "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
76
 
77
  return  map[d];
78
 
79
}       // hex2Char ()
80
 
81
 
82
//-----------------------------------------------------------------------------
83
//! Convert a register to a hex digit string
84
 
85
//! The supplied 32-bit value is converted to an 8 digit hex string. The
86
//! string is null terminated for convenience. The hex string follows the
87
//! universal printing convention: most significant digit on the left.
88
 
89
//! @param[in]  val  The value to convert
90
//! @param[out] buf  The buffer for the text string
91
//-----------------------------------------------------------------------------
92
void
93
Utils::reg2Hex (uint32_t  val,
94
                char     *buf)
95
{
96
  for (int  n = 7; n >= 0; n--)
97
    {
98
      buf[n] = hex2Char (val & 0xf);
99
      val /= 16;
100
    }
101
 
102
  buf[8] = 0;                    // Useful to terminate as string
103
 
104
}       // reg2hex ()
105
 
106
 
107
//-----------------------------------------------------------------------------
108
//! Convert a hex digit string to a register value
109
 
110
//! The supplied 8 digit hex string follows the universal printing convention:
111
//! most significant digit on the left. It is converted to a 32-bit value.
112
 
113
//! @param[in] buf  The buffer with the hex string
114
 
115
//! @return  The value to convert
116
//-----------------------------------------------------------------------------
117
uint32_t
118
Utils::hex2Reg (char *buf)
119
{
120
  uint32_t  val = 0;     // The result
121
 
122
  for (int  n = 0; n < 8; n++)
123
    {
124
      val = val * 16 + char2Hex (buf[n]);
125
    }
126
 
127
  return val;
128
 
129
}       // hex2reg ()
130
 
131
 
132
//-----------------------------------------------------------------------------
133
//! Convert an ASCII character string to pairs of hex digits
134
 
135
//! Both source and destination are null terminated.
136
 
137
//! @param[out] dest  Buffer to store the hex digit pairs (null terminated)
138
//! @param[in]  src   The ASCII string (null terminated)                      */
139
//-----------------------------------------------------------------------------
140
void
141
Utils::ascii2Hex (char *dest,
142
                  char *src)
143
{
144
  int  i;
145
 
146
  // Step through converting the source string
147
  for (i = 0; src[i] != '\0'; i++)
148
    {
149
      char  ch = src[i];
150
 
151
      dest[i * 2]     = hex2Char(ch >> 4 & 0xf);
152
      dest[i * 2 + 1] = hex2Char(ch      & 0xf);
153
    }
154
 
155
  dest[i * 2] = '\0';
156
 
157
}       // ascii2hex ()
158
 
159
 
160
//-----------------------------------------------------------------------------
161
//! Convert pairs of hex digits to an ASCII character string
162
 
163
//! Both source and destination are null terminated.
164
 
165
//! @param[out] dest  The ASCII string (null terminated)
166
//! @param[in]  src   Buffer holding the hex digit pairs (null terminated)
167
//-----------------------------------------------------------------------------
168
void
169
Utils::hex2Ascii (char *dest,
170
                  char *src)
171
{
172
  int  i;
173
 
174
  // Step through convering the source hex digit pairs
175
  for (i = 0; src[i * 2] != '\0' && src[i * 2 + 1] != '\0'; i++)
176
    {
177
      dest[i] = ((char2Hex (src[i * 2]) & 0xf) << 4) |
178
                 (char2Hex (src[i * 2 + 1]) & 0xf);
179
    }
180
 
181
  dest[i] = '\0';
182
 
183
}       // hex2ascii ()
184
 
185
 
186
//-----------------------------------------------------------------------------
187
//! "Unescape" RSP binary data
188
 
189
//! '#', '$' and '}' are escaped by preceding them by '}' and oring with 0x20.
190
 
191
//! This function reverses that, modifying the data in place.
192
 
193
//! @param[in] buf  The array of bytes to convert
194
//! @para[in]  len   The number of bytes to be converted
195
 
196
//! @return  The number of bytes AFTER conversion
197
//-----------------------------------------------------------------------------
198
int
199
Utils::rspUnescape (char *buf,
200
                    int   len)
201
{
202
  int  fromOffset = 0;           // Offset to source char
203
  int  toOffset   = 0;           // Offset to dest char
204
 
205
  while (fromOffset < len)
206
    {
207
      // Is it escaped
208
      if ( '}' == buf[fromOffset])
209
        {
210
          fromOffset++;
211
          buf[toOffset] = buf[fromOffset] ^ 0x20;
212
        }
213
      else
214
        {
215
          buf[toOffset] = buf[fromOffset];
216
        }
217
 
218
      fromOffset++;
219
      toOffset++;
220
    }
221
 
222
  return  toOffset;
223
 
224
}       // rspUnescape () */
225
 
226
 
227
//-----------------------------------------------------------------------------
228
//! Convert 32-bit value from host to target endianness
229
 
230
//! The target endianness is determined by the #define of TARGET_BIG_ENDIAN and
231
//! TARGET_LITTLE_ENDIAN. Not definining either is a compile time error.
232
 
233
//! @param[in] hostVal  The value in host endianness
234
 
235
//! @return  The value in target endianness
236
//-----------------------------------------------------------------------------
237
uint32_t
238
Utils::htotl (uint32_t hostVal)
239
{
240
  uint8_t  targetBytes[4];
241
 
242
#ifdef TARGET_BIG_ENDIAN
243
  targetBytes[0] = hostVal / 256 / 256 / 256;
244
  targetBytes[1] = hostVal / 256 / 256;
245
  targetBytes[2] = hostVal / 256;
246
  targetBytes[3] = hostVal;
247
#elif defined TARGET_LITTLE_ENDIAN
248
  targetBytes[0] = hostVal;
249
  targetBytes[1] = hostVal / 256;
250
  targetBytes[2] = hostVal / 256 / 256;
251
  targetBytes[3] = hostVal / 256 / 256 / 256;
252
#else
253
#error Must specify TARGET_BIG_ENDIAN or TARGET_LITTLE_ENDIAN
254
#endif
255
 
256
  return *((uint32_t *)targetBytes);
257
 
258
}       // htotl ()
259
 
260
 
261
//-----------------------------------------------------------------------------
262
//! Convert 32-bit value from target to host endianness
263
 
264
//! The target endianness is determined by the #define of TARGET_BIG_ENDIAN and
265
//! TARGET_LITTLE_ENDIAN. Not definining either is a compile time error.
266
 
267
//! @param[in] targetVal  The value in target endianness
268
 
269
//! @return  The value in target endianness
270
//-----------------------------------------------------------------------------
271
uint32_t
272
Utils::ttohl (uint32_t targetVal)
273
{
274
  uint8_t *targetBytes = (uint8_t *)(&targetVal);
275
  uint32_t hostVal;
276
 
277
#ifdef TARGET_BIG_ENDIAN
278
  hostVal =                 targetBytes[0];
279
  hostVal = hostVal * 256 + targetBytes[1];
280
  hostVal = hostVal * 256 + targetBytes[2];
281
  hostVal = hostVal * 256 + targetBytes[3];
282
#elif defined TARGET_LITTLE_ENDIAN
283
  hostVal =                 targetBytes[3];
284
  hostVal = hostVal * 256 + targetBytes[2];
285
  hostVal = hostVal * 256 + targetBytes[1];
286
  hostVal = hostVal * 256 + targetBytes[0];
287
#else
288
  #error Must specify TARGET_BIG_ENDIAN or TARGET_LITTLE_ENDIAN
289
#endif
290
 
291
  return  hostVal;
292
 
293
}       // ttohl ()

powered by: WebSVN 2.1.0

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