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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [cygmon/] [current/] [misc/] [utils.c] - Blame information for rev 838

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

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
2
//
3
//      utils.c
4
//
5
//      Monitor utilities.
6
//
7
//==========================================================================
8
// ####ECOSGPLCOPYRIGHTBEGIN####                                            
9
// -------------------------------------------                              
10
// This file is part of eCos, the Embedded Configurable Operating System.   
11
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
12
//
13
// eCos is free software; you can redistribute it and/or modify it under    
14
// the terms of the GNU General Public License as published by the Free     
15
// Software Foundation; either version 2 or (at your option) any later      
16
// version.                                                                 
17
//
18
// eCos is distributed in the hope that it will be useful, but WITHOUT      
19
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or    
20
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License    
21
// for more details.                                                        
22
//
23
// You should have received a copy of the GNU General Public License        
24
// along with eCos; if not, write to the Free Software Foundation, Inc.,    
25
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.            
26
//
27
// As a special exception, if other files instantiate templates or use      
28
// macros or inline functions from this file, or you compile this file      
29
// and link it with other works to produce a work based on this file,       
30
// this file does not by itself cause the resulting work to be covered by   
31
// the GNU General Public License. However the source code for this file    
32
// must still be made available in accordance with section (3) of the GNU   
33
// General Public License v2.                                               
34
//
35
// This exception does not invalidate any other reasons why a work based    
36
// on this file might be covered by the GNU General Public License.         
37
// -------------------------------------------                              
38
// ####ECOSGPLCOPYRIGHTEND####                                              
39
//==========================================================================
40
//#####DESCRIPTIONBEGIN####
41
//
42
// Author(s):    
43
// Contributors: gthomas, dmoseley
44
// Date:         1999-10-20
45
// Purpose:      Monitor utilities.
46
// Description:  
47
//               
48
//
49
//####DESCRIPTIONEND####
50
//
51
//=========================================================================
52
 
53
#include <limits.h>
54
#include <stdlib.h>
55
#include <string.h>
56
#include <ctype.h>
57
#ifdef HAVE_BSP
58
#include <bsp/bsp.h>
59
#include <bsp/cpu.h>
60
#include <bsp/hex-utils.h>
61
#endif
62
#include "monitor.h"
63
#include "tservice.h"
64
 
65
#if USE_CYGMON_PROTOTYPES
66
/* Use common prototypes */
67
/* Some of the composed board.h files compose these
68
   prototypes redundently, but if they dont,
69
   these are the common definitions */
70
#include "fmt_util.h"   /* Interface to string formatting utilities */
71
#include "generic-stub.h" /* from libstub */
72
#endif /* USE_CYGMON_PROTOTYPES */
73
 
74
volatile int switch_to_stub_flag = 0;
75
 
76
/* Input routine for the line editor. */
77
int
78
input_char (void)
79
{
80
  int i;
81
 
82
  /* We have to drop the '+' characters on the floor
83
     because gdb will send a '+' as the first character
84
     when connecting to the target. If we waste time
85
     echoing that, slow hw might get a uart overrun. */
86
  while ((i = xgetchar ()) == '+');
87
 
88
  if (i == '$')
89
    {
90
      xungetchar ('$');
91
      switch_to_stub_flag = 1;
92
      i = '\n';
93
    }
94
  return i;
95
}
96
 
97
 
98
static char tohex_array[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
99
                               '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
100
 
101
#define tohex(X) (tohex_array[(X) & 15])
102
 
103
#ifdef HAVE_BSP
104
#define fromhex __hex
105
#else
106
static int
107
fromhex (a)
108
{
109
  int number = -1;
110
 
111
  if (a >= '0' && a <= '9')
112
    number = a - '0';
113
  else if (a >= 'a' && a <= 'f')
114
    number = a - 'a' + 10;
115
  else if (a >= 'A' && a <= 'F')
116
    number = a - 'A' + 10;
117
  else
118
    xprintf ("Invalid hex digit %c", a);
119
 
120
  return number;
121
}
122
#endif
123
 
124
 
125
static unsigned long long
126
str2ull (char *str, int base)
127
{
128
  unsigned long long l = 0;
129
 
130
  if (str[0] == '0' && str[1] == 'x')
131
    {
132
      str += 2 ;
133
      base = 16 ;
134
    }
135
 
136
  while (*str != '\0')
137
    {
138
      if (*str == '.')
139
        str++;
140
      else
141
        l = (l * base) + fromhex(*str++);
142
    }
143
 
144
  return l;
145
}
146
 
147
 
148
/* Converts a string to a long, base is the assumed base of the string */
149
 
150
target_register_t
151
str2int (char *str, int base)
152
{
153
  return str2ull(str, base);
154
}
155
 
156
/* Converts a string to a double, input is a raw integer string
157
 * of the given assumed base. */
158
#if HAVE_DOUBLE_REGS
159
static double
160
str2double (char *str, int base)
161
{
162
  double d;
163
 
164
  switch (sizeof(double))
165
    {
166
      case sizeof(unsigned int):
167
        *((unsigned int *)&d) = str2ull(str, base);
168
        break;
169
#if __LONG_MAX__ != __INT_MAX__
170
      case sizeof(unsigned long):
171
        *((unsigned long *)&d) = str2ull(str, base);
172
        break;
173
#endif
174
#if __LONG_LONG_MAX__ != __LONG_MAX__
175
      case sizeof(unsigned long long):
176
        *((unsigned long long *)&d) = str2ull(str, base);
177
        break;
178
#endif
179
      default:
180
        d = 0.0;
181
        break;
182
    }
183
  return d;
184
}
185
#endif
186
 
187
target_register_t
188
str2intlen (char *str, int base, int len)
189
{
190
  target_register_t number = 0;
191
 
192
  while ((len--) > 0 && *str != '\0')
193
    number = number * base + fromhex (*(str++));
194
 
195
  return number;
196
}
197
 
198
int
199
hex2bytes (char *str, char *dest, int maxsize)
200
{
201
  int i;
202
  char *ptr;
203
 
204
  for (i = 0; i < maxsize; i++)
205
    dest[i] = 0;
206
  maxsize--;
207
 
208
  // Don't try and convert 0x prefix
209
  if ((str[0] == '0') && (str[1] == 'x'))
210
      str += 2;
211
 
212
  ptr = str + strlen(str) - 1;
213
  while (maxsize >= 0 && ptr >= str)
214
    {
215
      dest [maxsize] = fromhex(*ptr);
216
      ptr--;
217
      if (ptr >= str)
218
        {
219
          dest [maxsize--] |= fromhex(*ptr) * 16;
220
          ptr--;
221
        }
222
    }
223
  return 0;
224
}
225
 
226
 
227
/* Converts an unsigned long long to an ASCII string, adding leading
228
   zeroes to pad space up to numdigs. */
229
static int use_dots = 1;
230
 
231
#define MAX_NUM_DIGS 51
232
 
233
static char *
234
ull2str (unsigned long long number, int base, int numdigs)
235
{
236
  static char string[MAX_NUM_DIGS+1];
237
  int dots, i;
238
  char *ptr = string + MAX_NUM_DIGS;
239
 
240
  dots = (use_dots && base == 16);
241
 
242
  *(ptr--) = '\0';
243
  *(ptr--) = tohex (number % base);
244
  i = 1;
245
  number = number / base;
246
 
247
  while (number != 0)
248
    {
249
      if (dots && (i % 4) == 0)
250
        *(ptr--) = '.';
251
      *(ptr--) = tohex (number % base);
252
      i++;
253
      number = number / base;
254
    }
255
 
256
  if (numdigs == 0)
257
    {
258
      numdigs = i;
259
    }
260
  else
261
    {
262
      while(i < numdigs)
263
        {
264
          if (dots && (i % 4) == 0)
265
            *(ptr--) = '.';
266
          *(ptr--) = '0';
267
          i++;
268
        }
269
    }
270
  return ptr + 1;
271
}
272
 
273
 
274
char *
275
int2str (target_register_t number, int base, int numdigs)
276
{
277
  return ull2str((unsigned long long)number, base, numdigs);
278
}
279
 
280
#if HAVE_DOUBLE_REGS
281
static char *
282
double2str(double d)
283
{
284
  switch(sizeof(double))
285
    {
286
      case sizeof(unsigned int):
287
        return ull2str(*((unsigned int *)&d), 16, sizeof(double) * 2);
288
        break;
289
#if __LONG_MAX__ != __INT_MAX__
290
      case sizeof(unsigned long):
291
        return ull2str(*((unsigned long *)&d), 16, sizeof(double) * 2);
292
        break;
293
#endif
294
#if __LONG_LONG_MAX__ != __LONG_MAX__
295
      case sizeof(unsigned long long):
296
        return ull2str(*((unsigned long long *)&d), 16, sizeof(double) * 2);
297
        break;
298
#endif
299
    }
300
  return "....fixme...";
301
}
302
#endif
303
 
304
#ifndef NO_MALLOC
305
char *
306
strdup(const char *str)
307
{
308
  char *x = malloc (strlen (str) + 1);
309
  if (x != NULL)
310
    strcpy (x, str);
311
  return x;
312
}
313
#endif
314
 
315
 
316
target_register_t
317
get_pc(void)
318
{
319
    return get_register(REG_PC);
320
}
321
 
322
 
323
#if defined(HAVE_BSP) && !defined(__ECOS__)
324
static int
325
get_register_type(regnames_t which)
326
{
327
    int i;
328
 
329
    for (i = 0; regtab[i].registername != NULL; i++)
330
      if (regtab[i].registernumber == which)
331
        return regtab[i].registertype;
332
    return REGTYPE_INT;
333
}
334
#endif
335
 
336
char *get_register_str (regnames_t which, int detail, int valid)
337
{
338
#ifdef SPECIAL_REG_OUTPUT
339
  char *res;
340
 
341
  if ((res = SPECIAL_REG_OUTPUT (which, detail)) != NULL)
342
    {
343
      return res;
344
    }
345
#endif
346
  if (valid == 0)
347
    {
348
      switch (sizeof (target_register_t))
349
        {
350
        case 1:  return "...";
351
        case 2:  return ".....";
352
        case 4:  return ".........";
353
        case 8:  return ".................";
354
        default: return ".........";
355
        }
356
    }
357
  else
358
    {
359
      return int2str (get_register (which), 16, sizeof (target_register_t) * 2);
360
    }
361
}
362
 
363
 
364
void
365
store_register (regnames_t which, char *string)
366
{
367
#ifdef SPECIAL_REG_STORE
368
  if (SPECIAL_REG_STORE(which, string))
369
    return;
370
#endif
371
  put_register (which, str2int (string, 16));
372
}
373
 
374
 
375
 

powered by: WebSVN 2.1.0

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