1 /* mem.c --- memory for RX simulator.
3 Copyright (C) 2005, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
5 Contributed by Red Hat, Inc.
7 This file is part of the GNU simulators.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 /* This slows down the simulator and we get some false negatives from
23 gcc, like when it uses a long-sized hole to hold a byte-sized
24 variable, knowing that it doesn't care about the other bits. But,
25 if you need to track down a read-from-unitialized bug, set this to
34 #include "opcode/rx.h"
43 #define OFF_BITS PAGE_BITS
45 #define L1_LEN (1 << L1_BITS)
46 #define L2_LEN (1 << L2_BITS)
47 #define OFF_LEN (1 << OFF_BITS)
49 static unsigned char **pt[L1_LEN];
50 static unsigned char **ptr[L1_LEN];
51 static RX_Opcode_Decoded ***ptdc[L1_LEN];
53 /* [ get=0/put=1 ][ byte size ] */
54 static unsigned int mem_counters[2][5];
56 #define COUNT(isput,bytes) \
57 if (verbose && enable_counting) mem_counters[isput][bytes]++
64 for (i = 0; i < L1_LEN; i++)
67 for (j = 0; j < L2_LEN; j++)
72 memset (pt, 0, sizeof (pt));
73 memset (ptr, 0, sizeof (ptr));
74 memset (mem_counters, 0, sizeof (mem_counters));
78 rx_mem_ptr (unsigned long address, enum mem_ptr_action action)
80 int pt1 = (address >> (L2_BITS + OFF_BITS)) & ((1 << L1_BITS) - 1);
81 int pt2 = (address >> OFF_BITS) & ((1 << L2_BITS) - 1);
82 int pto = address & ((1 << OFF_BITS) - 1);
85 execution_error (SIM_ERR_NULL_POINTER_DEREFERENCE, 0);
89 pt[pt1] = (unsigned char **) calloc (L2_LEN, sizeof (char **));
90 ptr[pt1] = (unsigned char **) calloc (L2_LEN, sizeof (char **));
91 ptdc[pt1] = (RX_Opcode_Decoded ***) calloc (L2_LEN, sizeof (RX_Opcode_Decoded ***));
93 if (pt[pt1][pt2] == 0)
95 if (action == MPA_READING)
96 execution_error (SIM_ERR_READ_UNWRITTEN_PAGES, address);
98 pt[pt1][pt2] = (unsigned char *) calloc (OFF_LEN, 1);
99 ptr[pt1][pt2] = (unsigned char *) calloc (OFF_LEN, 1);
100 ptdc[pt1][pt2] = (RX_Opcode_Decoded **) calloc (OFF_LEN, sizeof(RX_Opcode_Decoded *));
102 else if (action == MPA_READING
103 && ptr[pt1][pt2][pto] == MC_UNINIT)
104 execution_error (SIM_ERR_READ_UNWRITTEN_BYTES, address);
106 if (action == MPA_WRITING)
109 if (ptr[pt1][pt2][pto] == MC_PUSHED_PC)
110 execution_error (SIM_ERR_CORRUPT_STACK, address);
111 ptr[pt1][pt2][pto] = MC_DATA;
113 /* The instruction decoder doesn't store it's decoded instructions
114 at word swapped addresses. Therefore, when clearing the decode
115 cache, we have to account for that here. */
116 pto_dc = pto ^ (rx_big_endian ? 3 : 0);
117 if (ptdc[pt1][pt2][pto_dc])
119 free (ptdc[pt1][pt2][pto_dc]);
120 ptdc[pt1][pt2][pto_dc] = NULL;
124 if (action == MPA_CONTENT_TYPE)
125 return (unsigned char *) (ptr[pt1][pt2] + pto);
127 if (action == MPA_DECODE_CACHE)
128 return (unsigned char *) (ptdc[pt1][pt2] + pto);
130 return pt[pt1][pt2] + pto;
134 rx_mem_decode_cache (unsigned long address)
136 return (RX_Opcode_Decoded **) rx_mem_ptr (address, MPA_DECODE_CACHE);
140 is_reserved_address (unsigned int address)
142 return (address >= 0x00020000 && address < 0x00080000)
143 || (address >= 0x00100000 && address < 0x01000000)
144 || (address >= 0x08000000 && address < 0xff000000);
148 used (int rstart, int i, int j)
150 int rend = i << (L2_BITS + OFF_BITS);
151 rend += j << OFF_BITS;
152 if (rstart == 0xe0000 && rend == 0xe1000)
154 printf ("mem: %08x - %08x (%dk bytes)\n", rstart, rend - 1,
155 (rend - rstart) / 1024);
159 mcs (int isput, int bytes)
161 return comma (mem_counters[isput][bytes]);
171 for (i = 0; i < L1_LEN; i++)
174 for (j = 0; j < L2_LEN; j++)
180 rstart = (i << (L2_BITS + OFF_BITS)) + (j << OFF_BITS);
197 /* mem foo: 123456789012 123456789012 123456789012 123456789012
199 printf (" byte short 3byte long"
203 /* Only use comma separated numbers when being very verbose.
204 Comma separated numbers are hard to parse in awk scripts. */
205 printf ("mem get: %12s %12s %12s %12s %12s\n", mcs (0, 1), mcs (0, 2),
206 mcs (0, 3), mcs (0, 4), mcs (0, 0));
207 printf ("mem put: %12s %12s %12s %12s\n", mcs (1, 1), mcs (1, 2),
208 mcs (1, 3), mcs (1, 4));
212 printf ("mem get: %12u %12u %12u %12u %12u\n",
213 mem_counters[0][1], mem_counters[0][2],
214 mem_counters[0][3], mem_counters[0][4],
216 printf ("mem put: %12u %12u %12u %12u\n",
217 mem_counters [1][1], mem_counters [1][2],
218 mem_counters [1][3], mem_counters [1][4]);
223 mem_usage_cycles (void)
225 unsigned long rv = mem_counters[0][0];
226 rv += mem_counters[0][1] * 1;
227 rv += mem_counters[0][2] * 2;
228 rv += mem_counters[0][3] * 3;
229 rv += mem_counters[0][4] * 4;
230 rv += mem_counters[1][1] * 1;
231 rv += mem_counters[1][2] * 2;
232 rv += mem_counters[1][3] * 3;
233 rv += mem_counters[1][4] * 4;
239 s (int address, char *dir)
242 printf ("MEM[%08x] %s", address, dir);
246 #define S(d) if (trace) s(address, d)
260 unsigned char *cp = rx_mem_ptr (address, MPA_CONTENT_TYPE);
264 #define E() if (trace) e()
267 mem_put_byte (unsigned int address, unsigned char value)
273 tc = mtypec (address);
274 m = rx_mem_ptr (address, MPA_WRITING);
276 printf (" %02x%c", value, tc);
280 case 0x0008c02a: /* PA.DR */
282 static int old_led = -1;
286 if (old_led != value)
289 for (i = 0; i < 8; i++)
290 if (value & (1 << i))
294 fputs ("\033[31m", stdout);
297 fputs (" @", stdout);
303 fputs ("\033[0m", stdout);
306 fputs (" *", stdout);
310 fputs ("\033[0m", stdout);
312 fputs ("\r", stdout);
320 case 0x0008c02b: /* PB.DR */
323 halt_pipeline_stats ();
325 reset_pipeline_stats ();
329 case 0x00088263: /* SCI4.TDR */
331 static int pending_exit = 0;
332 if (pending_exit == 2)
334 step_result = RX_MAKE_EXITED(value);
335 longjmp (decode_jmp_buf, 1);
347 if (is_reserved_address (address))
348 generate_access_exception ();
353 mem_put_qi (int address, unsigned char value)
356 mem_put_byte (address, value & 0xff);
361 #ifdef CYCLE_ACCURATE
366 mem_put_hi (int address, unsigned short value)
371 #ifdef CYCLE_ACCURATE
372 case 0x00088126: /* TPU1.TCNT */
373 tpu_base = regs.cycle_count;
375 case 0x00088136: /* TPU2.TCNT */
376 tpu_base = regs.cycle_count;
382 mem_put_byte (address, value >> 8);
383 mem_put_byte (address + 1, value & 0xff);
387 mem_put_byte (address, value & 0xff);
388 mem_put_byte (address + 1, value >> 8);
396 mem_put_psi (int address, unsigned long value)
401 mem_put_byte (address, value >> 16);
402 mem_put_byte (address + 1, (value >> 8) & 0xff);
403 mem_put_byte (address + 2, value & 0xff);
407 mem_put_byte (address, value & 0xff);
408 mem_put_byte (address + 1, (value >> 8) & 0xff);
409 mem_put_byte (address + 2, value >> 16);
416 mem_put_si (int address, unsigned long value)
421 mem_put_byte (address + 0, (value >> 24) & 0xff);
422 mem_put_byte (address + 1, (value >> 16) & 0xff);
423 mem_put_byte (address + 2, (value >> 8) & 0xff);
424 mem_put_byte (address + 3, value & 0xff);
428 mem_put_byte (address + 0, value & 0xff);
429 mem_put_byte (address + 1, (value >> 8) & 0xff);
430 mem_put_byte (address + 2, (value >> 16) & 0xff);
431 mem_put_byte (address + 3, (value >> 24) & 0xff);
438 mem_put_blk (int address, void *bufptr, int nbytes)
442 mem_counters[1][1] += nbytes;
444 mem_put_byte (address++, *(unsigned char *) bufptr++);
449 mem_get_pc (int address)
451 unsigned char *m = rx_mem_ptr (address, MPA_READING);
457 mem_get_byte (unsigned int address)
462 m = rx_mem_ptr (address, MPA_READING);
465 case 0x00088264: /* SCI4.SSR */
467 return 0x04; /* transmitter empty */
471 printf (" %02x%c", *m, mtypec (address));
472 if (is_reserved_address (address))
473 generate_access_exception ();
481 mem_get_qi (int address)
485 rv = mem_get_byte (address);
492 mem_get_hi (int address)
498 #ifdef CYCLE_ACCURATE
499 case 0x00088126: /* TPU1.TCNT */
500 rv = (regs.cycle_count - tpu_base) >> 16;
502 case 0x00088136: /* TPU2.TCNT */
503 rv = (regs.cycle_count - tpu_base) >> 0;
510 rv = mem_get_byte (address) << 8;
511 rv |= mem_get_byte (address + 1);
515 rv = mem_get_byte (address);
516 rv |= mem_get_byte (address + 1) << 8;
525 mem_get_psi (int address)
531 rv = mem_get_byte (address + 2);
532 rv |= mem_get_byte (address + 1) << 8;
533 rv |= mem_get_byte (address) << 16;
537 rv = mem_get_byte (address);
538 rv |= mem_get_byte (address + 1) << 8;
539 rv |= mem_get_byte (address + 2) << 16;
547 mem_get_si (int address)
553 rv = mem_get_byte (address + 3);
554 rv |= mem_get_byte (address + 2) << 8;
555 rv |= mem_get_byte (address + 1) << 16;
556 rv |= mem_get_byte (address) << 24;
560 rv = mem_get_byte (address);
561 rv |= mem_get_byte (address + 1) << 8;
562 rv |= mem_get_byte (address + 2) << 16;
563 rv |= mem_get_byte (address + 3) << 24;
571 mem_get_blk (int address, void *bufptr, int nbytes)
575 mem_counters[0][1] += nbytes;
577 *(char *) bufptr++ = mem_get_byte (address++);
582 sign_ext (int v, int bits)
586 v &= (1 << bits) - 1;
587 if (v & (1 << (bits - 1)))
594 mem_set_content_type (int address, enum mem_content_type type)
596 unsigned char *mt = rx_mem_ptr (address, MPA_CONTENT_TYPE);
601 mem_set_content_range (int start_address, int end_address, enum mem_content_type type)
603 while (start_address < end_address)
608 sz = end_address - start_address;
609 ofs = start_address % L1_LEN;
610 if (sz + ofs > L1_LEN)
613 mt = rx_mem_ptr (start_address, MPA_CONTENT_TYPE);
614 memset (mt, type, sz);
620 enum mem_content_type
621 mem_get_content_type (int address)
623 unsigned char *mt = rx_mem_ptr (address, MPA_CONTENT_TYPE);