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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [gdb-7.1/] [sim/] [rx/] [mem.c] - Blame information for rev 842

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 227 jeremybenn
/* mem.c --- memory for RX simulator.
2
 
3
Copyright (C) 2005, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4
Contributed by Red Hat, Inc.
5
 
6
This file is part of the GNU simulators.
7
 
8
This program is free software; you can redistribute it and/or modify
9
it under the terms of the GNU General Public License as published by
10
the Free Software Foundation; either version 3 of the License, or
11
(at your option) any later version.
12
 
13
This program is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
GNU General Public License for more details.
17
 
18
You should have received a copy of the GNU General Public License
19
along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20
 
21
/* This slows down the simulator and we get some false negatives from
22
   gcc, like when it uses a long-sized hole to hold a byte-sized
23
   variable, knowing that it doesn't care about the other bits.  But,
24
   if you need to track down a read-from-unitialized bug, set this to
25
   1.  */
26
#define RDCHECK 0
27
 
28
#include <stdio.h>
29
#include <stdlib.h>
30
#include <string.h>
31
 
32
#include "mem.h"
33
#include "cpu.h"
34
#include "syscalls.h"
35
#include "misc.h"
36
#include "err.h"
37
 
38
#define L1_BITS  (10)
39
#define L2_BITS  (10)
40
#define OFF_BITS (12)
41
 
42
#define L1_LEN  (1 << L1_BITS)
43
#define L2_LEN  (1 << L2_BITS)
44
#define OFF_LEN (1 << OFF_BITS)
45
 
46
static unsigned char **pt[L1_LEN];
47
static unsigned char **ptr[L1_LEN];
48
 
49
/* [ get=0/put=1 ][ byte size ] */
50
static unsigned int mem_counters[2][5];
51
 
52
#define COUNT(isput,bytes)                                      \
53
  if (verbose && enable_counting) mem_counters[isput][bytes]++
54
 
55
void
56
init_mem (void)
57
{
58
  int i, j;
59
 
60
  for (i = 0; i < L1_LEN; i++)
61
    if (pt[i])
62
      {
63
        for (j = 0; j < L2_LEN; j++)
64
          if (pt[i][j])
65
            free (pt[i][j]);
66
        free (pt[i]);
67
      }
68
  memset (pt, 0, sizeof (pt));
69
  memset (ptr, 0, sizeof (ptr));
70
  memset (mem_counters, 0, sizeof (mem_counters));
71
}
72
 
73
enum mem_ptr_action
74
{
75
  MPA_WRITING,
76
  MPA_READING,
77
  MPA_CONTENT_TYPE
78
};
79
 
80
static unsigned char *
81
mem_ptr (unsigned long address, enum mem_ptr_action action)
82
{
83
  int pt1 = (address >> (L2_BITS + OFF_BITS)) & ((1 << L1_BITS) - 1);
84
  int pt2 = (address >> OFF_BITS) & ((1 << L2_BITS) - 1);
85
  int pto = address & ((1 << OFF_BITS) - 1);
86
 
87
  if (address == 0)
88
    execution_error (SIM_ERR_NULL_POINTER_DEREFERENCE, 0);
89
 
90
  if (pt[pt1] == 0)
91
    {
92
      pt[pt1] = (unsigned char **) calloc (L2_LEN, sizeof (char **));
93
      ptr[pt1] = (unsigned char **) calloc (L2_LEN, sizeof (char **));
94
    }
95
  if (pt[pt1][pt2] == 0)
96
    {
97
      if (action == MPA_READING)
98
        execution_error (SIM_ERR_READ_UNWRITTEN_PAGES, address);
99
 
100
      pt[pt1][pt2] = (unsigned char *) malloc (OFF_LEN);
101
      memset (pt[pt1][pt2], 0, OFF_LEN);
102
      ptr[pt1][pt2] = (unsigned char *) malloc (OFF_LEN);
103
      memset (ptr[pt1][pt2], MC_UNINIT, OFF_LEN);
104
    }
105
  else if (action == MPA_READING
106
           && ptr[pt1][pt2][pto] == MC_UNINIT)
107
    execution_error (SIM_ERR_READ_UNWRITTEN_BYTES, address);
108
 
109
  if (action == MPA_WRITING)
110
    {
111
      if (ptr[pt1][pt2][pto] == MC_PUSHED_PC)
112
        execution_error (SIM_ERR_CORRUPT_STACK, address);
113
      ptr[pt1][pt2][pto] = MC_DATA;
114
    }
115
 
116
  if (action == MPA_CONTENT_TYPE)
117
    return ptr[pt1][pt2] + pto;
118
 
119
  return pt[pt1][pt2] + pto;
120
}
121
 
122
static inline int
123
is_reserved_address (unsigned int address)
124
{
125
  return (address >= 0x00020000 && address < 0x00080000)
126
    ||   (address >= 0x00100000 && address < 0x01000000)
127
    ||   (address >= 0x08000000 && address < 0xff000000);
128
}
129
 
130
static void
131
used (int rstart, int i, int j)
132
{
133
  int rend = i << (L2_BITS + OFF_BITS);
134
  rend += j << OFF_BITS;
135
  if (rstart == 0xe0000 && rend == 0xe1000)
136
    return;
137
  printf ("mem:   %08x - %08x (%dk bytes)\n", rstart, rend - 1,
138
          (rend - rstart) / 1024);
139
}
140
 
141
static char *
142
mcs (int isput, int bytes)
143
{
144
  return comma (mem_counters[isput][bytes]);
145
}
146
 
147
void
148
mem_usage_stats ()
149
{
150
  int i, j;
151
  int rstart = 0;
152
  int pending = 0;
153
 
154
  for (i = 0; i < L1_LEN; i++)
155
    if (pt[i])
156
      {
157
        for (j = 0; j < L2_LEN; j++)
158
          if (pt[i][j])
159
            {
160
              if (!pending)
161
                {
162
                  pending = 1;
163
                  rstart = (i << (L2_BITS + OFF_BITS)) + (j << OFF_BITS);
164
                }
165
            }
166
          else if (pending)
167
            {
168
              pending = 0;
169
              used (rstart, i, j);
170
            }
171
      }
172
    else
173
      {
174
        if (pending)
175
          {
176
            pending = 0;
177
            used (rstart, i, 0);
178
          }
179
      }
180
  /*       mem foo: 123456789012 123456789012 123456789012 123456789012
181
            123456789012 */
182
  printf ("                 byte        short        3byte         long"
183
          "       opcode\n");
184
  if (verbose > 1)
185
    {
186
      /* Only use comma separated numbers when being very verbose.
187
         Comma separated numbers are hard to parse in awk scripts.  */
188
      printf ("mem get: %12s %12s %12s %12s %12s\n", mcs (0, 1), mcs (0, 2),
189
              mcs (0, 3), mcs (0, 4), mcs (0, 0));
190
      printf ("mem put: %12s %12s %12s %12s\n", mcs (1, 1), mcs (1, 2),
191
              mcs (1, 3), mcs (1, 4));
192
    }
193
  else
194
    {
195
      printf ("mem get: %12u %12u %12u %12u %12u\n",
196
              mem_counters[0][1], mem_counters[0][2],
197
              mem_counters[0][3], mem_counters[0][4],
198
              mem_counters[0][0]);
199
      printf ("mem put: %12u %12u %12u %12u\n",
200
              mem_counters [1][1], mem_counters [1][2],
201
              mem_counters [1][3], mem_counters [1][4]);
202
    }
203
}
204
 
205
unsigned long
206
mem_usage_cycles (void)
207
{
208
  unsigned long rv = mem_counters[0][0];
209
  rv += mem_counters[0][1] * 1;
210
  rv += mem_counters[0][2] * 2;
211
  rv += mem_counters[0][3] * 3;
212
  rv += mem_counters[0][4] * 4;
213
  rv += mem_counters[1][1] * 1;
214
  rv += mem_counters[1][2] * 2;
215
  rv += mem_counters[1][3] * 3;
216
  rv += mem_counters[1][4] * 4;
217
  return rv;
218
}
219
 
220
static int tpr = 0;
221
static void
222
s (int address, char *dir)
223
{
224
  if (tpr == 0)
225
    printf ("MEM[%08x] %s", address, dir);
226
  tpr++;
227
}
228
 
229
#define S(d) if (trace) s(address, d)
230
static void
231
e ()
232
{
233
  if (!trace)
234
    return;
235
  tpr--;
236
  if (tpr == 0)
237
    printf ("\n");
238
}
239
 
240
static char
241
mtypec (int address)
242
{
243
  unsigned char *cp = mem_ptr (address, MPA_CONTENT_TYPE);
244
  return "udp"[*cp];
245
}
246
 
247
#define E() if (trace) e()
248
 
249
void
250
mem_put_byte (unsigned int address, unsigned char value)
251
{
252
  unsigned char *m;
253
  char tc = ' ';
254
 
255
  if (trace)
256
    tc = mtypec (address);
257
  m = mem_ptr (address, MPA_WRITING);
258
  if (trace)
259
    printf (" %02x%c", value, tc);
260
  *m = value;
261
  switch (address)
262
    {
263
    case 0x00e1:
264
      {
265
        static int old_led = -1;
266
        static char *led_on[] =
267
          { "\033[31m O ", "\033[32m O ", "\033[34m O " };
268
        static char *led_off[] = { "\033[0m · ", "\033[0m · ", "\033[0m · " };
269
        int i;
270
        if (old_led != value)
271
          {
272
            fputs ("  ", stdout);
273
            for (i = 0; i < 3; i++)
274
              if (value & (1 << i))
275
                fputs (led_off[i], stdout);
276
              else
277
                fputs (led_on[i], stdout);
278
            fputs ("\033[0m\r", stdout);
279
            fflush (stdout);
280
            old_led = value;
281
          }
282
      }
283
      break;
284
 
285
    case 0x3aa: /* uart1tx */
286
      {
287
        static int pending_exit = 0;
288
        if (value == 0)
289
          {
290
            if (pending_exit)
291
              {
292
                step_result = RX_MAKE_EXITED(value);
293
                return;
294
              }
295
            pending_exit = 1;
296
          }
297
        else
298
          putchar(value);
299
      }
300
      break;
301
 
302
    default:
303
      if (is_reserved_address (address))
304
        generate_access_exception ();
305
    }
306
}
307
 
308
void
309
mem_put_qi (int address, unsigned char value)
310
{
311
  S ("<=");
312
  mem_put_byte (address, value & 0xff);
313
  E ();
314
  COUNT (1, 1);
315
}
316
 
317
void
318
mem_put_hi (int address, unsigned short value)
319
{
320
  S ("<=");
321
  if (rx_big_endian)
322
    {
323
      mem_put_byte (address, value >> 8);
324
      mem_put_byte (address + 1, value & 0xff);
325
    }
326
  else
327
    {
328
      mem_put_byte (address, value & 0xff);
329
      mem_put_byte (address + 1, value >> 8);
330
    }
331
  E ();
332
  COUNT (1, 2);
333
}
334
 
335
void
336
mem_put_psi (int address, unsigned long value)
337
{
338
  S ("<=");
339
  if (rx_big_endian)
340
    {
341
      mem_put_byte (address, value >> 16);
342
      mem_put_byte (address + 1, (value >> 8) & 0xff);
343
      mem_put_byte (address + 2, value & 0xff);
344
    }
345
  else
346
    {
347
      mem_put_byte (address, value & 0xff);
348
      mem_put_byte (address + 1, (value >> 8) & 0xff);
349
      mem_put_byte (address + 2, value >> 16);
350
    }
351
  E ();
352
  COUNT (1, 3);
353
}
354
 
355
void
356
mem_put_si (int address, unsigned long value)
357
{
358
  S ("<=");
359
  if (rx_big_endian)
360
    {
361
      mem_put_byte (address + 0, (value >> 24) & 0xff);
362
      mem_put_byte (address + 1, (value >> 16) & 0xff);
363
      mem_put_byte (address + 2, (value >> 8) & 0xff);
364
      mem_put_byte (address + 3, value & 0xff);
365
    }
366
  else
367
    {
368
      mem_put_byte (address + 0, value & 0xff);
369
      mem_put_byte (address + 1, (value >> 8) & 0xff);
370
      mem_put_byte (address + 2, (value >> 16) & 0xff);
371
      mem_put_byte (address + 3, (value >> 24) & 0xff);
372
    }
373
  E ();
374
  COUNT (1, 4);
375
}
376
 
377
void
378
mem_put_blk (int address, void *bufptr, int nbytes)
379
{
380
  S ("<=");
381
  if (enable_counting)
382
    mem_counters[1][1] += nbytes;
383
  while (nbytes--)
384
    mem_put_byte (address++, *(unsigned char *) bufptr++);
385
  E ();
386
}
387
 
388
unsigned char
389
mem_get_pc (int address)
390
{
391
  unsigned char *m = mem_ptr (address, MPA_READING);
392
  COUNT (0, 0);
393
  return *m;
394
}
395
 
396
static unsigned char
397
mem_get_byte (unsigned int address)
398
{
399
  unsigned char *m;
400
 
401
  S ("=>");
402
  m = mem_ptr (address, MPA_READING);
403
  switch (address)
404
    {
405
    case 0x3ad: /* uart1c1 */
406
      E();
407
      return 2; /* transmitter empty */
408
      break;
409
    default:
410
      if (trace)
411
        printf (" %02x%c", *m, mtypec (address));
412
      if (is_reserved_address (address))
413
        generate_access_exception ();
414
      break;
415
    }
416
  E ();
417
  return *m;
418
}
419
 
420
unsigned char
421
mem_get_qi (int address)
422
{
423
  unsigned char rv;
424
  S ("=>");
425
  rv = mem_get_byte (address);
426
  COUNT (0, 1);
427
  E ();
428
  return rv;
429
}
430
 
431
unsigned short
432
mem_get_hi (int address)
433
{
434
  unsigned short rv;
435
  S ("=>");
436
  if (rx_big_endian)
437
    {
438
      rv = mem_get_byte (address) << 8;
439
      rv |= mem_get_byte (address + 1);
440
    }
441
  else
442
    {
443
      rv = mem_get_byte (address);
444
      rv |= mem_get_byte (address + 1) << 8;
445
    }
446
  COUNT (0, 2);
447
  E ();
448
  return rv;
449
}
450
 
451
unsigned long
452
mem_get_psi (int address)
453
{
454
  unsigned long rv;
455
  S ("=>");
456
  if (rx_big_endian)
457
    {
458
      rv = mem_get_byte (address + 2);
459
      rv |= mem_get_byte (address + 1) << 8;
460
      rv |= mem_get_byte (address) << 16;
461
    }
462
  else
463
    {
464
      rv = mem_get_byte (address);
465
      rv |= mem_get_byte (address + 1) << 8;
466
      rv |= mem_get_byte (address + 2) << 16;
467
    }
468
  COUNT (0, 3);
469
  E ();
470
  return rv;
471
}
472
 
473
unsigned long
474
mem_get_si (int address)
475
{
476
  unsigned long rv;
477
  S ("=>");
478
  if (rx_big_endian)
479
    {
480
      rv = mem_get_byte (address + 3);
481
      rv |= mem_get_byte (address + 2) << 8;
482
      rv |= mem_get_byte (address + 1) << 16;
483
      rv |= mem_get_byte (address) << 24;
484
    }
485
  else
486
    {
487
      rv = mem_get_byte (address);
488
      rv |= mem_get_byte (address + 1) << 8;
489
      rv |= mem_get_byte (address + 2) << 16;
490
      rv |= mem_get_byte (address + 3) << 24;
491
    }
492
  COUNT (0, 4);
493
  E ();
494
  return rv;
495
}
496
 
497
void
498
mem_get_blk (int address, void *bufptr, int nbytes)
499
{
500
  S ("=>");
501
  if (enable_counting)
502
    mem_counters[0][1] += nbytes;
503
  while (nbytes--)
504
    *(char *) bufptr++ = mem_get_byte (address++);
505
  E ();
506
}
507
 
508
int
509
sign_ext (int v, int bits)
510
{
511
  if (bits < 32)
512
    {
513
      v &= (1 << bits) - 1;
514
      if (v & (1 << (bits - 1)))
515
        v -= (1 << bits);
516
    }
517
  return v;
518
}
519
 
520
void
521
mem_set_content_type (int address, enum mem_content_type type)
522
{
523
  unsigned char *mt = mem_ptr (address, MPA_CONTENT_TYPE);
524
  *mt = type;
525
}
526
 
527
void
528
mem_set_content_range (int start_address, int end_address, enum mem_content_type type)
529
{
530
  while (start_address < end_address)
531
    {
532
      int sz, ofs;
533
      unsigned char *mt;
534
 
535
      sz = end_address - start_address;
536
      ofs = start_address % L1_LEN;
537
      if (sz + ofs > L1_LEN)
538
        sz = L1_LEN - ofs;
539
 
540
      mt = mem_ptr (start_address, MPA_CONTENT_TYPE);
541
      memset (mt, type, sz);
542
 
543
      start_address += sz;
544
    }
545
}
546
 
547
enum mem_content_type
548
mem_get_content_type (int address)
549
{
550
  unsigned char *mt = mem_ptr (address, MPA_CONTENT_TYPE);
551
  return *mt;
552
}

powered by: WebSVN 2.1.0

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