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