1 /* mem.c --- memory for RX simulator.
3 Copyright (C) 2005, 2007-2012 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
33 #include "opcode/rx.h"
42 #define OFF_BITS PAGE_BITS
44 #define L1_LEN (1 << L1_BITS)
45 #define L2_LEN (1 << L2_BITS)
46 #define OFF_LEN (1 << OFF_BITS)
48 static unsigned char **pt[L1_LEN];
49 static unsigned char **ptr[L1_LEN];
50 static RX_Opcode_Decoded ***ptdc[L1_LEN];
52 /* [ get=0/put=1 ][ byte size ] */
53 static unsigned int mem_counters[2][5];
55 #define COUNT(isput,bytes) \
56 if (verbose && enable_counting) mem_counters[isput][bytes]++
63 for (i = 0; i < L1_LEN; i++)
66 for (j = 0; j < L2_LEN; j++)
71 memset (pt, 0, sizeof (pt));
72 memset (ptr, 0, sizeof (ptr));
73 memset (mem_counters, 0, sizeof (mem_counters));
77 rx_mem_ptr (unsigned long address, enum mem_ptr_action action)
79 int pt1 = (address >> (L2_BITS + OFF_BITS)) & ((1 << L1_BITS) - 1);
80 int pt2 = (address >> OFF_BITS) & ((1 << L2_BITS) - 1);
81 int pto = address & ((1 << OFF_BITS) - 1);
84 execution_error (SIM_ERR_NULL_POINTER_DEREFERENCE, 0);
88 pt[pt1] = (unsigned char **) calloc (L2_LEN, sizeof (char **));
89 ptr[pt1] = (unsigned char **) calloc (L2_LEN, sizeof (char **));
90 ptdc[pt1] = (RX_Opcode_Decoded ***) calloc (L2_LEN, sizeof (RX_Opcode_Decoded ***));
92 if (pt[pt1][pt2] == 0)
94 if (action == MPA_READING)
95 execution_error (SIM_ERR_READ_UNWRITTEN_PAGES, address);
97 pt[pt1][pt2] = (unsigned char *) calloc (OFF_LEN, 1);
98 ptr[pt1][pt2] = (unsigned char *) calloc (OFF_LEN, 1);
99 ptdc[pt1][pt2] = (RX_Opcode_Decoded **) calloc (OFF_LEN, sizeof(RX_Opcode_Decoded *));
101 else if (action == MPA_READING
102 && ptr[pt1][pt2][pto] == MC_UNINIT)
103 execution_error (SIM_ERR_READ_UNWRITTEN_BYTES, address);
105 if (action == MPA_WRITING)
108 if (ptr[pt1][pt2][pto] == MC_PUSHED_PC)
109 execution_error (SIM_ERR_CORRUPT_STACK, address);
110 ptr[pt1][pt2][pto] = MC_DATA;
112 /* The instruction decoder doesn't store it's decoded instructions
113 at word swapped addresses. Therefore, when clearing the decode
114 cache, we have to account for that here. */
115 pto_dc = pto ^ (rx_big_endian ? 3 : 0);
116 if (ptdc[pt1][pt2][pto_dc])
118 free (ptdc[pt1][pt2][pto_dc]);
119 ptdc[pt1][pt2][pto_dc] = NULL;
123 if (action == MPA_CONTENT_TYPE)
124 return (unsigned char *) (ptr[pt1][pt2] + pto);
126 if (action == MPA_DECODE_CACHE)
127 return (unsigned char *) (ptdc[pt1][pt2] + pto);
129 return pt[pt1][pt2] + pto;
133 rx_mem_decode_cache (unsigned long address)
135 return (RX_Opcode_Decoded **) rx_mem_ptr (address, MPA_DECODE_CACHE);
139 is_reserved_address (unsigned int address)
141 return (address >= 0x00020000 && address < 0x00080000)
142 || (address >= 0x00100000 && address < 0x01000000)
143 || (address >= 0x08000000 && address < 0xff000000);
147 used (int rstart, int i, int j)
149 int rend = i << (L2_BITS + OFF_BITS);
150 rend += j << OFF_BITS;
151 if (rstart == 0xe0000 && rend == 0xe1000)
153 printf ("mem: %08x - %08x (%dk bytes)\n", rstart, rend - 1,
154 (rend - rstart) / 1024);
158 mcs (int isput, int bytes)
160 return comma (mem_counters[isput][bytes]);
170 for (i = 0; i < L1_LEN; i++)
173 for (j = 0; j < L2_LEN; j++)
179 rstart = (i << (L2_BITS + OFF_BITS)) + (j << OFF_BITS);
196 /* mem foo: 123456789012 123456789012 123456789012 123456789012
198 printf (" byte short 3byte long"
202 /* Only use comma separated numbers when being very verbose.
203 Comma separated numbers are hard to parse in awk scripts. */
204 printf ("mem get: %12s %12s %12s %12s %12s\n", mcs (0, 1), mcs (0, 2),
205 mcs (0, 3), mcs (0, 4), mcs (0, 0));
206 printf ("mem put: %12s %12s %12s %12s\n", mcs (1, 1), mcs (1, 2),
207 mcs (1, 3), mcs (1, 4));
211 printf ("mem get: %12u %12u %12u %12u %12u\n",
212 mem_counters[0][1], mem_counters[0][2],
213 mem_counters[0][3], mem_counters[0][4],
215 printf ("mem put: %12u %12u %12u %12u\n",
216 mem_counters [1][1], mem_counters [1][2],
217 mem_counters [1][3], mem_counters [1][4]);
222 mem_usage_cycles (void)
224 unsigned long rv = mem_counters[0][0];
225 rv += mem_counters[0][1] * 1;
226 rv += mem_counters[0][2] * 2;
227 rv += mem_counters[0][3] * 3;
228 rv += mem_counters[0][4] * 4;
229 rv += mem_counters[1][1] * 1;
230 rv += mem_counters[1][2] * 2;
231 rv += mem_counters[1][3] * 3;
232 rv += mem_counters[1][4] * 4;
238 s (int address, char *dir)
241 printf ("MEM[%08x] %s", address, dir);
245 #define S(d) if (trace) s(address, d)
259 unsigned char *cp = rx_mem_ptr (address, MPA_CONTENT_TYPE);
263 #define E() if (trace) e()
266 mem_put_byte (unsigned int address, unsigned char value)
272 tc = mtypec (address);
273 m = rx_mem_ptr (address, MPA_WRITING);
275 printf (" %02x%c", value, tc);
279 case 0x0008c02a: /* PA.DR */
281 static int old_led = -1;
285 if (old_led != value)
288 for (i = 0; i < 8; i++)
289 if (value & (1 << i))
293 fputs ("\033[31m", stdout);
296 fputs (" @", stdout);
302 fputs ("\033[0m", stdout);
305 fputs (" *", stdout);
309 fputs ("\033[0m", stdout);
311 fputs ("\r", stdout);
319 case 0x0008c02b: /* PB.DR */
322 halt_pipeline_stats ();
324 reset_pipeline_stats ();
328 case 0x00088263: /* SCI4.TDR */
330 static int pending_exit = 0;
331 if (pending_exit == 2)
333 step_result = RX_MAKE_EXITED(value);
334 longjmp (decode_jmp_buf, 1);
346 if (is_reserved_address (address))
347 generate_access_exception ();
352 mem_put_qi (int address, unsigned char value)
355 mem_put_byte (address, value & 0xff);
360 #ifdef CYCLE_ACCURATE
365 mem_put_hi (int address, unsigned short value)
370 #ifdef CYCLE_ACCURATE
371 case 0x00088126: /* TPU1.TCNT */
372 tpu_base = regs.cycle_count;
374 case 0x00088136: /* TPU2.TCNT */
375 tpu_base = regs.cycle_count;
381 mem_put_byte (address, value >> 8);
382 mem_put_byte (address + 1, value & 0xff);
386 mem_put_byte (address, value & 0xff);
387 mem_put_byte (address + 1, value >> 8);
395 mem_put_psi (int address, unsigned long value)
400 mem_put_byte (address, value >> 16);
401 mem_put_byte (address + 1, (value >> 8) & 0xff);
402 mem_put_byte (address + 2, value & 0xff);
406 mem_put_byte (address, value & 0xff);
407 mem_put_byte (address + 1, (value >> 8) & 0xff);
408 mem_put_byte (address + 2, value >> 16);
415 mem_put_si (int address, unsigned long value)
420 mem_put_byte (address + 0, (value >> 24) & 0xff);
421 mem_put_byte (address + 1, (value >> 16) & 0xff);
422 mem_put_byte (address + 2, (value >> 8) & 0xff);
423 mem_put_byte (address + 3, value & 0xff);
427 mem_put_byte (address + 0, value & 0xff);
428 mem_put_byte (address + 1, (value >> 8) & 0xff);
429 mem_put_byte (address + 2, (value >> 16) & 0xff);
430 mem_put_byte (address + 3, (value >> 24) & 0xff);
437 mem_put_blk (int address, void *bufptr, int nbytes)
441 mem_counters[1][1] += nbytes;
443 mem_put_byte (address++, *(unsigned char *) bufptr++);
448 mem_get_pc (int address)
450 unsigned char *m = rx_mem_ptr (address, MPA_READING);
456 mem_get_byte (unsigned int address)
461 m = rx_mem_ptr (address, MPA_READING);
464 case 0x00088264: /* SCI4.SSR */
466 return 0x04; /* transmitter empty */
470 printf (" %02x%c", *m, mtypec (address));
471 if (is_reserved_address (address))
472 generate_access_exception ();
480 mem_get_qi (int address)
484 rv = mem_get_byte (address);
491 mem_get_hi (int address)
497 #ifdef CYCLE_ACCURATE
498 case 0x00088126: /* TPU1.TCNT */
499 rv = (regs.cycle_count - tpu_base) >> 16;
501 case 0x00088136: /* TPU2.TCNT */
502 rv = (regs.cycle_count - tpu_base) >> 0;
509 rv = mem_get_byte (address) << 8;
510 rv |= mem_get_byte (address + 1);
514 rv = mem_get_byte (address);
515 rv |= mem_get_byte (address + 1) << 8;
524 mem_get_psi (int address)
530 rv = mem_get_byte (address + 2);
531 rv |= mem_get_byte (address + 1) << 8;
532 rv |= mem_get_byte (address) << 16;
536 rv = mem_get_byte (address);
537 rv |= mem_get_byte (address + 1) << 8;
538 rv |= mem_get_byte (address + 2) << 16;
546 mem_get_si (int address)
552 rv = mem_get_byte (address + 3);
553 rv |= mem_get_byte (address + 2) << 8;
554 rv |= mem_get_byte (address + 1) << 16;
555 rv |= mem_get_byte (address) << 24;
559 rv = mem_get_byte (address);
560 rv |= mem_get_byte (address + 1) << 8;
561 rv |= mem_get_byte (address + 2) << 16;
562 rv |= mem_get_byte (address + 3) << 24;
570 mem_get_blk (int address, void *bufptr, int nbytes)
574 mem_counters[0][1] += nbytes;
576 *(char *) bufptr++ = mem_get_byte (address++);
581 sign_ext (int v, int bits)
585 v &= (1 << bits) - 1;
586 if (v & (1 << (bits - 1)))
593 mem_set_content_type (int address, enum mem_content_type type)
595 unsigned char *mt = rx_mem_ptr (address, MPA_CONTENT_TYPE);
600 mem_set_content_range (int start_address, int end_address, enum mem_content_type type)
602 while (start_address < end_address)
607 sz = end_address - start_address;
608 ofs = start_address % L1_LEN;
609 if (sz + ofs > L1_LEN)
612 mt = rx_mem_ptr (start_address, MPA_CONTENT_TYPE);
613 memset (mt, type, sz);
619 enum mem_content_type
620 mem_get_content_type (int address)
622 unsigned char *mt = rx_mem_ptr (address, MPA_CONTENT_TYPE);