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
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)
107 if (ptr[pt1][pt2][pto] == MC_PUSHED_PC)
108 execution_error (SIM_ERR_CORRUPT_STACK, address);
109 ptr[pt1][pt2][pto] = MC_DATA;
110 if (ptdc[pt1][pt2][pto])
112 free (ptdc[pt1][pt2][pto]);
113 ptdc[pt1][pt2][pto] = NULL;
117 if (action == MPA_CONTENT_TYPE)
118 return (unsigned char *) (ptr[pt1][pt2] + pto);
120 if (action == MPA_DECODE_CACHE)
121 return (unsigned char *) (ptdc[pt1][pt2] + pto);
123 return pt[pt1][pt2] + pto;
127 rx_mem_decode_cache (unsigned long address)
129 return (RX_Opcode_Decoded **) rx_mem_ptr (address, MPA_DECODE_CACHE);
133 is_reserved_address (unsigned int address)
135 return (address >= 0x00020000 && address < 0x00080000)
136 || (address >= 0x00100000 && address < 0x01000000)
137 || (address >= 0x08000000 && address < 0xff000000);
141 used (int rstart, int i, int j)
143 int rend = i << (L2_BITS + OFF_BITS);
144 rend += j << OFF_BITS;
145 if (rstart == 0xe0000 && rend == 0xe1000)
147 printf ("mem: %08x - %08x (%dk bytes)\n", rstart, rend - 1,
148 (rend - rstart) / 1024);
152 mcs (int isput, int bytes)
154 return comma (mem_counters[isput][bytes]);
164 for (i = 0; i < L1_LEN; i++)
167 for (j = 0; j < L2_LEN; j++)
173 rstart = (i << (L2_BITS + OFF_BITS)) + (j << OFF_BITS);
190 /* mem foo: 123456789012 123456789012 123456789012 123456789012
192 printf (" byte short 3byte long"
196 /* Only use comma separated numbers when being very verbose.
197 Comma separated numbers are hard to parse in awk scripts. */
198 printf ("mem get: %12s %12s %12s %12s %12s\n", mcs (0, 1), mcs (0, 2),
199 mcs (0, 3), mcs (0, 4), mcs (0, 0));
200 printf ("mem put: %12s %12s %12s %12s\n", mcs (1, 1), mcs (1, 2),
201 mcs (1, 3), mcs (1, 4));
205 printf ("mem get: %12u %12u %12u %12u %12u\n",
206 mem_counters[0][1], mem_counters[0][2],
207 mem_counters[0][3], mem_counters[0][4],
209 printf ("mem put: %12u %12u %12u %12u\n",
210 mem_counters [1][1], mem_counters [1][2],
211 mem_counters [1][3], mem_counters [1][4]);
216 mem_usage_cycles (void)
218 unsigned long rv = mem_counters[0][0];
219 rv += mem_counters[0][1] * 1;
220 rv += mem_counters[0][2] * 2;
221 rv += mem_counters[0][3] * 3;
222 rv += mem_counters[0][4] * 4;
223 rv += mem_counters[1][1] * 1;
224 rv += mem_counters[1][2] * 2;
225 rv += mem_counters[1][3] * 3;
226 rv += mem_counters[1][4] * 4;
232 s (int address, char *dir)
235 printf ("MEM[%08x] %s", address, dir);
239 #define S(d) if (trace) s(address, d)
253 unsigned char *cp = rx_mem_ptr (address, MPA_CONTENT_TYPE);
257 #define E() if (trace) e()
260 mem_put_byte (unsigned int address, unsigned char value)
266 tc = mtypec (address);
267 m = rx_mem_ptr (address, MPA_WRITING);
269 printf (" %02x%c", value, tc);
273 case 0x0008c02a: /* PA.DR */
275 static int old_led = -1;
279 if (old_led != value)
282 for (i = 0; i < 8; i++)
283 if (value & (1 << i))
287 fputs ("\033[31m", stdout);
290 fputs (" @", stdout);
296 fputs ("\033[0m", stdout);
299 fputs (" *", stdout);
303 fputs ("\033[0m", stdout);
305 fputs ("\r", stdout);
313 case 0x0008c02b: /* PB.DR */
316 halt_pipeline_stats ();
318 reset_pipeline_stats ();
322 case 0x00088263: /* SCI4.TDR */
324 static int pending_exit = 0;
325 if (pending_exit == 2)
327 step_result = RX_MAKE_EXITED(value);
328 longjmp (decode_jmp_buf, 1);
340 if (is_reserved_address (address))
341 generate_access_exception ();
346 mem_put_qi (int address, unsigned char value)
349 mem_put_byte (address, value & 0xff);
354 #ifdef CYCLE_ACCURATE
359 mem_put_hi (int address, unsigned short value)
364 #ifdef CYCLE_ACCURATE
365 case 0x00088126: /* TPU1.TCNT */
366 tpu_base = regs.cycle_count;
368 case 0x00088136: /* TPU2.TCNT */
369 tpu_base = regs.cycle_count;
375 mem_put_byte (address, value >> 8);
376 mem_put_byte (address + 1, value & 0xff);
380 mem_put_byte (address, value & 0xff);
381 mem_put_byte (address + 1, value >> 8);
389 mem_put_psi (int address, unsigned long value)
394 mem_put_byte (address, value >> 16);
395 mem_put_byte (address + 1, (value >> 8) & 0xff);
396 mem_put_byte (address + 2, value & 0xff);
400 mem_put_byte (address, value & 0xff);
401 mem_put_byte (address + 1, (value >> 8) & 0xff);
402 mem_put_byte (address + 2, value >> 16);
409 mem_put_si (int address, unsigned long value)
414 mem_put_byte (address + 0, (value >> 24) & 0xff);
415 mem_put_byte (address + 1, (value >> 16) & 0xff);
416 mem_put_byte (address + 2, (value >> 8) & 0xff);
417 mem_put_byte (address + 3, value & 0xff);
421 mem_put_byte (address + 0, value & 0xff);
422 mem_put_byte (address + 1, (value >> 8) & 0xff);
423 mem_put_byte (address + 2, (value >> 16) & 0xff);
424 mem_put_byte (address + 3, (value >> 24) & 0xff);
431 mem_put_blk (int address, void *bufptr, int nbytes)
435 mem_counters[1][1] += nbytes;
437 mem_put_byte (address++, *(unsigned char *) bufptr++);
442 mem_get_pc (int address)
444 unsigned char *m = rx_mem_ptr (address, MPA_READING);
450 mem_get_byte (unsigned int address)
455 m = rx_mem_ptr (address, MPA_READING);
458 case 0x00088264: /* SCI4.SSR */
460 return 0x04; /* transmitter empty */
464 printf (" %02x%c", *m, mtypec (address));
465 if (is_reserved_address (address))
466 generate_access_exception ();
474 mem_get_qi (int address)
478 rv = mem_get_byte (address);
485 mem_get_hi (int address)
491 #ifdef CYCLE_ACCURATE
492 case 0x00088126: /* TPU1.TCNT */
493 rv = (regs.cycle_count - tpu_base) >> 16;
495 case 0x00088136: /* TPU2.TCNT */
496 rv = (regs.cycle_count - tpu_base) >> 0;
503 rv = mem_get_byte (address) << 8;
504 rv |= mem_get_byte (address + 1);
508 rv = mem_get_byte (address);
509 rv |= mem_get_byte (address + 1) << 8;
518 mem_get_psi (int address)
524 rv = mem_get_byte (address + 2);
525 rv |= mem_get_byte (address + 1) << 8;
526 rv |= mem_get_byte (address) << 16;
530 rv = mem_get_byte (address);
531 rv |= mem_get_byte (address + 1) << 8;
532 rv |= mem_get_byte (address + 2) << 16;
540 mem_get_si (int address)
546 rv = mem_get_byte (address + 3);
547 rv |= mem_get_byte (address + 2) << 8;
548 rv |= mem_get_byte (address + 1) << 16;
549 rv |= mem_get_byte (address) << 24;
553 rv = mem_get_byte (address);
554 rv |= mem_get_byte (address + 1) << 8;
555 rv |= mem_get_byte (address + 2) << 16;
556 rv |= mem_get_byte (address + 3) << 24;
564 mem_get_blk (int address, void *bufptr, int nbytes)
568 mem_counters[0][1] += nbytes;
570 *(char *) bufptr++ = mem_get_byte (address++);
575 sign_ext (int v, int bits)
579 v &= (1 << bits) - 1;
580 if (v & (1 << (bits - 1)))
587 mem_set_content_type (int address, enum mem_content_type type)
589 unsigned char *mt = rx_mem_ptr (address, MPA_CONTENT_TYPE);
594 mem_set_content_range (int start_address, int end_address, enum mem_content_type type)
596 while (start_address < end_address)
601 sz = end_address - start_address;
602 ofs = start_address % L1_LEN;
603 if (sz + ofs > L1_LEN)
606 mt = rx_mem_ptr (start_address, MPA_CONTENT_TYPE);
607 memset (mt, type, sz);
613 enum mem_content_type
614 mem_get_content_type (int address)
616 unsigned char *mt = rx_mem_ptr (address, MPA_CONTENT_TYPE);