1 /* mem.c --- memory for RX simulator.
3 Copyright (C) 2005, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4 Contributed by Red Hat, Inc.
6 This file is part of the GNU simulators.
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.
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.
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/>. */
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
42 #define L1_LEN (1 << L1_BITS)
43 #define L2_LEN (1 << L2_BITS)
44 #define OFF_LEN (1 << OFF_BITS)
46 static unsigned char **pt[L1_LEN];
47 static unsigned char **ptr[L1_LEN];
49 /* [ get=0/put=1 ][ byte size ] */
50 static unsigned int mem_counters[2][5];
52 #define COUNT(isput,bytes) \
53 if (verbose && enable_counting) mem_counters[isput][bytes]++
60 for (i = 0; i < L1_LEN; i++)
63 for (j = 0; j < L2_LEN; j++)
68 memset (pt, 0, sizeof (pt));
69 memset (ptr, 0, sizeof (ptr));
70 memset (mem_counters, 0, sizeof (mem_counters));
80 static unsigned char *
81 mem_ptr (unsigned long address, enum mem_ptr_action action)
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);
88 execution_error (SIM_ERR_NULL_POINTER_DEREFERENCE, 0);
92 pt[pt1] = (unsigned char **) calloc (L2_LEN, sizeof (char **));
93 ptr[pt1] = (unsigned char **) calloc (L2_LEN, sizeof (char **));
95 if (pt[pt1][pt2] == 0)
97 if (action == MPA_READING)
98 execution_error (SIM_ERR_READ_UNWRITTEN_PAGES, address);
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);
105 else if (action == MPA_READING
106 && ptr[pt1][pt2][pto] == MC_UNINIT)
107 execution_error (SIM_ERR_READ_UNWRITTEN_BYTES, address);
109 if (action == MPA_WRITING)
111 if (ptr[pt1][pt2][pto] == MC_PUSHED_PC)
112 execution_error (SIM_ERR_CORRUPT_STACK, address);
113 ptr[pt1][pt2][pto] = MC_DATA;
116 if (action == MPA_CONTENT_TYPE)
117 return ptr[pt1][pt2] + pto;
119 return pt[pt1][pt2] + pto;
123 is_reserved_address (unsigned int address)
125 return (address >= 0x00020000 && address < 0x00080000)
126 || (address >= 0x00100000 && address < 0x01000000)
127 || (address >= 0x08000000 && address < 0xff000000);
131 used (int rstart, int i, int j)
133 int rend = i << (L2_BITS + OFF_BITS);
134 rend += j << OFF_BITS;
135 if (rstart == 0xe0000 && rend == 0xe1000)
137 printf ("mem: %08x - %08x (%dk bytes)\n", rstart, rend - 1,
138 (rend - rstart) / 1024);
142 mcs (int isput, int bytes)
144 return comma (mem_counters[isput][bytes]);
154 for (i = 0; i < L1_LEN; i++)
157 for (j = 0; j < L2_LEN; j++)
163 rstart = (i << (L2_BITS + OFF_BITS)) + (j << OFF_BITS);
180 /* mem foo: 123456789012 123456789012 123456789012 123456789012
182 printf (" byte short 3byte long"
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));
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],
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]);
206 mem_usage_cycles (void)
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;
222 s (int address, char *dir)
225 printf ("MEM[%08x] %s", address, dir);
229 #define S(d) if (trace) s(address, d)
243 unsigned char *cp = mem_ptr (address, MPA_CONTENT_TYPE);
247 #define E() if (trace) e()
250 mem_put_byte (unsigned int address, unsigned char value)
256 tc = mtypec (address);
257 m = mem_ptr (address, MPA_WRITING);
259 printf (" %02x%c", value, tc);
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 · " };
270 if (old_led != value)
273 for (i = 0; i < 3; i++)
274 if (value & (1 << i))
275 fputs (led_off[i], stdout);
277 fputs (led_on[i], stdout);
278 fputs ("\033[0m\r", stdout);
285 case 0x3aa: /* uart1tx */
287 static int pending_exit = 0;
292 step_result = RX_MAKE_EXITED(value);
303 if (is_reserved_address (address))
304 generate_access_exception ();
309 mem_put_qi (int address, unsigned char value)
312 mem_put_byte (address, value & 0xff);
318 mem_put_hi (int address, unsigned short value)
323 mem_put_byte (address, value >> 8);
324 mem_put_byte (address + 1, value & 0xff);
328 mem_put_byte (address, value & 0xff);
329 mem_put_byte (address + 1, value >> 8);
336 mem_put_psi (int address, unsigned long value)
341 mem_put_byte (address, value >> 16);
342 mem_put_byte (address + 1, (value >> 8) & 0xff);
343 mem_put_byte (address + 2, value & 0xff);
347 mem_put_byte (address, value & 0xff);
348 mem_put_byte (address + 1, (value >> 8) & 0xff);
349 mem_put_byte (address + 2, value >> 16);
356 mem_put_si (int address, unsigned long value)
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);
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);
378 mem_put_blk (int address, void *bufptr, int nbytes)
382 mem_counters[1][1] += nbytes;
384 mem_put_byte (address++, *(unsigned char *) bufptr++);
389 mem_get_pc (int address)
391 unsigned char *m = mem_ptr (address, MPA_READING);
397 mem_get_byte (unsigned int address)
402 m = mem_ptr (address, MPA_READING);
405 case 0x3ad: /* uart1c1 */
407 return 2; /* transmitter empty */
411 printf (" %02x%c", *m, mtypec (address));
412 if (is_reserved_address (address))
413 generate_access_exception ();
421 mem_get_qi (int address)
425 rv = mem_get_byte (address);
432 mem_get_hi (int address)
438 rv = mem_get_byte (address) << 8;
439 rv |= mem_get_byte (address + 1);
443 rv = mem_get_byte (address);
444 rv |= mem_get_byte (address + 1) << 8;
452 mem_get_psi (int address)
458 rv = mem_get_byte (address + 2);
459 rv |= mem_get_byte (address + 1) << 8;
460 rv |= mem_get_byte (address) << 16;
464 rv = mem_get_byte (address);
465 rv |= mem_get_byte (address + 1) << 8;
466 rv |= mem_get_byte (address + 2) << 16;
474 mem_get_si (int address)
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;
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;
498 mem_get_blk (int address, void *bufptr, int nbytes)
502 mem_counters[0][1] += nbytes;
504 *(char *) bufptr++ = mem_get_byte (address++);
509 sign_ext (int v, int bits)
513 v &= (1 << bits) - 1;
514 if (v & (1 << (bits - 1)))
521 mem_set_content_type (int address, enum mem_content_type type)
523 unsigned char *mt = mem_ptr (address, MPA_CONTENT_TYPE);
528 mem_set_content_range (int start_address, int end_address, enum mem_content_type type)
530 while (start_address < end_address)
535 sz = end_address - start_address;
536 ofs = start_address % L1_LEN;
537 if (sz + ofs > L1_LEN)
540 mt = mem_ptr (start_address, MPA_CONTENT_TYPE);
541 memset (mt, type, sz);
547 enum mem_content_type
548 mem_get_content_type (int address)
550 unsigned char *mt = mem_ptr (address, MPA_CONTENT_TYPE);