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
41 #define OFF_BITS PAGE_BITS
43 #define L1_LEN (1 << L1_BITS)
44 #define L2_LEN (1 << L2_BITS)
45 #define OFF_LEN (1 << OFF_BITS)
47 static unsigned char **pt[L1_LEN];
48 static unsigned char **ptr[L1_LEN];
50 /* [ get=0/put=1 ][ byte size ] */
51 static unsigned int mem_counters[2][5];
53 #define COUNT(isput,bytes) \
54 if (verbose && enable_counting) mem_counters[isput][bytes]++
61 for (i = 0; i < L1_LEN; i++)
64 for (j = 0; j < L2_LEN; j++)
69 memset (pt, 0, sizeof (pt));
70 memset (ptr, 0, sizeof (ptr));
71 memset (mem_counters, 0, sizeof (mem_counters));
75 rx_mem_ptr (unsigned long address, enum mem_ptr_action action)
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);
82 execution_error (SIM_ERR_NULL_POINTER_DEREFERENCE, 0);
86 pt[pt1] = (unsigned char **) calloc (L2_LEN, sizeof (char **));
87 ptr[pt1] = (unsigned char **) calloc (L2_LEN, sizeof (char **));
89 if (pt[pt1][pt2] == 0)
91 if (action == MPA_READING)
92 execution_error (SIM_ERR_READ_UNWRITTEN_PAGES, address);
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);
99 else if (action == MPA_READING
100 && ptr[pt1][pt2][pto] == MC_UNINIT)
101 execution_error (SIM_ERR_READ_UNWRITTEN_BYTES, address);
103 if (action == MPA_WRITING)
105 if (ptr[pt1][pt2][pto] == MC_PUSHED_PC)
106 execution_error (SIM_ERR_CORRUPT_STACK, address);
107 ptr[pt1][pt2][pto] = MC_DATA;
110 if (action == MPA_CONTENT_TYPE)
111 return ptr[pt1][pt2] + pto;
113 return pt[pt1][pt2] + pto;
117 is_reserved_address (unsigned int address)
119 return (address >= 0x00020000 && address < 0x00080000)
120 || (address >= 0x00100000 && address < 0x01000000)
121 || (address >= 0x08000000 && address < 0xff000000);
125 used (int rstart, int i, int j)
127 int rend = i << (L2_BITS + OFF_BITS);
128 rend += j << OFF_BITS;
129 if (rstart == 0xe0000 && rend == 0xe1000)
131 printf ("mem: %08x - %08x (%dk bytes)\n", rstart, rend - 1,
132 (rend - rstart) / 1024);
136 mcs (int isput, int bytes)
138 return comma (mem_counters[isput][bytes]);
148 for (i = 0; i < L1_LEN; i++)
151 for (j = 0; j < L2_LEN; j++)
157 rstart = (i << (L2_BITS + OFF_BITS)) + (j << OFF_BITS);
174 /* mem foo: 123456789012 123456789012 123456789012 123456789012
176 printf (" byte short 3byte long"
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));
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],
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]);
200 mem_usage_cycles (void)
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;
216 s (int address, char *dir)
219 printf ("MEM[%08x] %s", address, dir);
223 #define S(d) if (trace) s(address, d)
237 unsigned char *cp = rx_mem_ptr (address, MPA_CONTENT_TYPE);
241 #define E() if (trace) e()
244 mem_put_byte (unsigned int address, unsigned char value)
250 tc = mtypec (address);
251 m = rx_mem_ptr (address, MPA_WRITING);
253 printf (" %02x%c", value, tc);
257 case 0x0008c02a: /* PA.DR */
259 static int old_led = -1;
263 if (old_led != value)
266 for (i = 0; i < 8; i++)
267 if (value & (1 << i))
271 fputs ("\033[31m", stdout);
274 fputs (" @", stdout);
280 fputs ("\033[0m", stdout);
283 fputs (" *", stdout);
287 fputs ("\033[0m", stdout);
289 fputs ("\r", stdout);
297 case 0x0008c02b: /* PB.DR */
300 halt_pipeline_stats ();
302 reset_pipeline_stats ();
306 case 0x00088263: /* SCI4.TDR */
308 static int pending_exit = 0;
309 if (pending_exit == 2)
311 step_result = RX_MAKE_EXITED(value);
312 longjmp (decode_jmp_buf, 1);
324 if (is_reserved_address (address))
325 generate_access_exception ();
330 mem_put_qi (int address, unsigned char value)
333 mem_put_byte (address, value & 0xff);
341 mem_put_hi (int address, unsigned short value)
346 #ifdef CYCLE_ACCURATE
347 case 0x00088126: /* TPU1.TCNT */
348 tpu_base = regs.cycle_count;
350 case 0x00088136: /* TPU2.TCNT */
351 tpu_base = regs.cycle_count;
357 mem_put_byte (address, value >> 8);
358 mem_put_byte (address + 1, value & 0xff);
362 mem_put_byte (address, value & 0xff);
363 mem_put_byte (address + 1, value >> 8);
371 mem_put_psi (int address, unsigned long value)
376 mem_put_byte (address, value >> 16);
377 mem_put_byte (address + 1, (value >> 8) & 0xff);
378 mem_put_byte (address + 2, value & 0xff);
382 mem_put_byte (address, value & 0xff);
383 mem_put_byte (address + 1, (value >> 8) & 0xff);
384 mem_put_byte (address + 2, value >> 16);
391 mem_put_si (int address, unsigned long value)
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);
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);
413 mem_put_blk (int address, void *bufptr, int nbytes)
417 mem_counters[1][1] += nbytes;
419 mem_put_byte (address++, *(unsigned char *) bufptr++);
424 mem_get_pc (int address)
426 unsigned char *m = rx_mem_ptr (address, MPA_READING);
432 mem_get_byte (unsigned int address)
437 m = rx_mem_ptr (address, MPA_READING);
440 case 0x00088264: /* SCI4.SSR */
442 return 0x04; /* transmitter empty */
446 printf (" %02x%c", *m, mtypec (address));
447 if (is_reserved_address (address))
448 generate_access_exception ();
456 mem_get_qi (int address)
460 rv = mem_get_byte (address);
467 mem_get_hi (int address)
473 #ifdef CYCLE_ACCURATE
474 case 0x00088126: /* TPU1.TCNT */
475 rv = (regs.cycle_count - tpu_base) >> 16;
477 case 0x00088136: /* TPU2.TCNT */
478 rv = (regs.cycle_count - tpu_base) >> 0;
485 rv = mem_get_byte (address) << 8;
486 rv |= mem_get_byte (address + 1);
490 rv = mem_get_byte (address);
491 rv |= mem_get_byte (address + 1) << 8;
500 mem_get_psi (int address)
506 rv = mem_get_byte (address + 2);
507 rv |= mem_get_byte (address + 1) << 8;
508 rv |= mem_get_byte (address) << 16;
512 rv = mem_get_byte (address);
513 rv |= mem_get_byte (address + 1) << 8;
514 rv |= mem_get_byte (address + 2) << 16;
522 mem_get_si (int address)
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;
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;
546 mem_get_blk (int address, void *bufptr, int nbytes)
550 mem_counters[0][1] += nbytes;
552 *(char *) bufptr++ = mem_get_byte (address++);
557 sign_ext (int v, int bits)
561 v &= (1 << bits) - 1;
562 if (v & (1 << (bits - 1)))
569 mem_set_content_type (int address, enum mem_content_type type)
571 unsigned char *mt = rx_mem_ptr (address, MPA_CONTENT_TYPE);
576 mem_set_content_range (int start_address, int end_address, enum mem_content_type type)
578 while (start_address < end_address)
583 sz = end_address - start_address;
584 ofs = start_address % L1_LEN;
585 if (sz + ofs > L1_LEN)
588 mt = rx_mem_ptr (start_address, MPA_CONTENT_TYPE);
589 memset (mt, type, sz);
595 enum mem_content_type
596 mem_get_content_type (int address)
598 unsigned char *mt = rx_mem_ptr (address, MPA_CONTENT_TYPE);