1 /* mem.c --- memory for RX simulator.
3 Copyright (C) 2005-2024 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 must come before any other includes. */
24 /* This slows down the simulator and we get some false negatives from
25 gcc, like when it uses a long-sized hole to hold a byte-sized
26 variable, knowing that it doesn't care about the other bits. But,
27 if you need to track down a read-from-unitialized bug, set this to
35 #include "opcode/rx.h"
44 #define OFF_BITS PAGE_BITS
46 #define L1_LEN (1 << L1_BITS)
47 #define L2_LEN (1 << L2_BITS)
48 #define OFF_LEN (1 << OFF_BITS)
50 static unsigned char **pt
[L1_LEN
];
51 static unsigned char **ptr
[L1_LEN
];
52 static RX_Opcode_Decoded
***ptdc
[L1_LEN
];
54 /* [ get=0/put=1 ][ byte size ] */
55 static unsigned int mem_counters
[2][5];
57 #define COUNT(isput,bytes) \
58 if (verbose && enable_counting) mem_counters[isput][bytes]++
65 for (i
= 0; i
< L1_LEN
; i
++)
68 for (j
= 0; j
< L2_LEN
; j
++)
73 memset (pt
, 0, sizeof (pt
));
74 memset (ptr
, 0, sizeof (ptr
));
75 memset (mem_counters
, 0, sizeof (mem_counters
));
79 rx_mem_ptr (unsigned long address
, enum mem_ptr_action action
)
81 int pt1
= (address
>> (L2_BITS
+ OFF_BITS
)) & ((1 << L1_BITS
) - 1);
82 int pt2
= (address
>> OFF_BITS
) & ((1 << L2_BITS
) - 1);
83 int pto
= address
& ((1 << OFF_BITS
) - 1);
86 execution_error (SIM_ERR_NULL_POINTER_DEREFERENCE
, 0);
90 pt
[pt1
] = (unsigned char **) calloc (L2_LEN
, sizeof (char **));
91 ptr
[pt1
] = (unsigned char **) calloc (L2_LEN
, sizeof (char **));
92 ptdc
[pt1
] = (RX_Opcode_Decoded
***) calloc (L2_LEN
, sizeof (RX_Opcode_Decoded
***));
94 if (pt
[pt1
][pt2
] == 0)
96 if (action
== MPA_READING
)
97 execution_error (SIM_ERR_READ_UNWRITTEN_PAGES
, address
);
99 pt
[pt1
][pt2
] = (unsigned char *) calloc (OFF_LEN
, 1);
100 ptr
[pt1
][pt2
] = (unsigned char *) calloc (OFF_LEN
, 1);
101 ptdc
[pt1
][pt2
] = (RX_Opcode_Decoded
**) calloc (OFF_LEN
, sizeof(RX_Opcode_Decoded
*));
103 else if (action
== MPA_READING
104 && ptr
[pt1
][pt2
][pto
] == MC_UNINIT
)
105 execution_error (SIM_ERR_READ_UNWRITTEN_BYTES
, address
);
107 if (action
== MPA_WRITING
)
110 if (ptr
[pt1
][pt2
][pto
] == MC_PUSHED_PC
)
111 execution_error (SIM_ERR_CORRUPT_STACK
, address
);
112 ptr
[pt1
][pt2
][pto
] = MC_DATA
;
114 /* The instruction decoder doesn't store it's decoded instructions
115 at word swapped addresses. Therefore, when clearing the decode
116 cache, we have to account for that here. */
117 pto_dc
= pto
^ (rx_big_endian
? 3 : 0);
118 if (ptdc
[pt1
][pt2
][pto_dc
])
120 free (ptdc
[pt1
][pt2
][pto_dc
]);
121 ptdc
[pt1
][pt2
][pto_dc
] = NULL
;
125 if (action
== MPA_CONTENT_TYPE
)
126 return (unsigned char *) (ptr
[pt1
][pt2
] + pto
);
128 if (action
== MPA_DECODE_CACHE
)
129 return (unsigned char *) (ptdc
[pt1
][pt2
] + pto
);
131 return pt
[pt1
][pt2
] + pto
;
135 rx_mem_decode_cache (unsigned long address
)
137 return (RX_Opcode_Decoded
**) rx_mem_ptr (address
, MPA_DECODE_CACHE
);
141 is_reserved_address (unsigned int address
)
143 return (address
>= 0x00020000 && address
< 0x00080000)
144 || (address
>= 0x00100000 && address
< 0x01000000)
145 || (address
>= 0x08000000 && address
< 0xff000000);
149 used (int rstart
, int i
, int j
)
151 int rend
= i
<< (L2_BITS
+ OFF_BITS
);
152 rend
+= j
<< OFF_BITS
;
153 if (rstart
== 0xe0000 && rend
== 0xe1000)
155 printf ("mem: %08x - %08x (%dk bytes)\n", rstart
, rend
- 1,
156 (rend
- rstart
) / 1024);
160 mcs (int isput
, int bytes
)
162 return comma (mem_counters
[isput
][bytes
]);
166 mem_usage_stats (void)
172 for (i
= 0; i
< L1_LEN
; i
++)
175 for (j
= 0; j
< L2_LEN
; j
++)
181 rstart
= (i
<< (L2_BITS
+ OFF_BITS
)) + (j
<< OFF_BITS
);
198 /* mem foo: 123456789012 123456789012 123456789012 123456789012
200 printf (" byte short 3byte long"
204 /* Only use comma separated numbers when being very verbose.
205 Comma separated numbers are hard to parse in awk scripts. */
206 printf ("mem get: %12s %12s %12s %12s %12s\n", mcs (0, 1), mcs (0, 2),
207 mcs (0, 3), mcs (0, 4), mcs (0, 0));
208 printf ("mem put: %12s %12s %12s %12s\n", mcs (1, 1), mcs (1, 2),
209 mcs (1, 3), mcs (1, 4));
213 printf ("mem get: %12u %12u %12u %12u %12u\n",
214 mem_counters
[0][1], mem_counters
[0][2],
215 mem_counters
[0][3], mem_counters
[0][4],
217 printf ("mem put: %12u %12u %12u %12u\n",
218 mem_counters
[1][1], mem_counters
[1][2],
219 mem_counters
[1][3], mem_counters
[1][4]);
224 mem_usage_cycles (void)
226 unsigned long rv
= mem_counters
[0][0];
227 rv
+= mem_counters
[0][1] * 1;
228 rv
+= mem_counters
[0][2] * 2;
229 rv
+= mem_counters
[0][3] * 3;
230 rv
+= mem_counters
[0][4] * 4;
231 rv
+= mem_counters
[1][1] * 1;
232 rv
+= mem_counters
[1][2] * 2;
233 rv
+= mem_counters
[1][3] * 3;
234 rv
+= mem_counters
[1][4] * 4;
240 s (int address
, char *dir
)
243 printf ("MEM[%08x] %s", address
, dir
);
247 #define S(d) if (trace) s(address, d)
261 unsigned char *cp
= rx_mem_ptr (address
, MPA_CONTENT_TYPE
);
265 #define E() if (trace) e()
268 mem_put_byte (unsigned int address
, unsigned char value
)
274 tc
= mtypec (address
);
275 m
= rx_mem_ptr (address
, MPA_WRITING
);
277 printf (" %02x%c", value
, tc
);
281 case 0x0008c02a: /* PA.DR */
283 static int old_led
= -1;
287 if (old_led
!= value
)
290 for (i
= 0; i
< 8; i
++)
291 if (value
& (1 << i
))
295 fputs ("\033[31m", stdout
);
298 fputs (" @", stdout
);
304 fputs ("\033[0m", stdout
);
307 fputs (" *", stdout
);
311 fputs ("\033[0m", stdout
);
313 fputs ("\r", stdout
);
321 case 0x0008c02b: /* PB.DR */
324 halt_pipeline_stats ();
326 reset_pipeline_stats ();
331 case 0x00088263: /* SCI4.TDR */
333 static int pending_exit
= 0;
334 if (pending_exit
== 2)
336 step_result
= RX_MAKE_EXITED(value
);
337 longjmp (decode_jmp_buf
, 1);
349 if (is_reserved_address (address
))
350 generate_access_exception ();
355 mem_put_qi (int address
, unsigned char value
)
358 mem_put_byte (address
, value
& 0xff);
363 #ifdef CYCLE_ACCURATE
368 mem_put_hi (int address
, unsigned short value
)
373 #ifdef CYCLE_ACCURATE
374 case 0x00088126: /* TPU1.TCNT */
375 tpu_base
= regs
.cycle_count
;
377 case 0x00088136: /* TPU2.TCNT */
378 tpu_base
= regs
.cycle_count
;
384 mem_put_byte (address
, value
>> 8);
385 mem_put_byte (address
+ 1, value
& 0xff);
389 mem_put_byte (address
, value
& 0xff);
390 mem_put_byte (address
+ 1, value
>> 8);
398 mem_put_psi (int address
, unsigned long value
)
403 mem_put_byte (address
, value
>> 16);
404 mem_put_byte (address
+ 1, (value
>> 8) & 0xff);
405 mem_put_byte (address
+ 2, value
& 0xff);
409 mem_put_byte (address
, value
& 0xff);
410 mem_put_byte (address
+ 1, (value
>> 8) & 0xff);
411 mem_put_byte (address
+ 2, value
>> 16);
418 mem_put_si (int address
, unsigned long value
)
423 mem_put_byte (address
+ 0, (value
>> 24) & 0xff);
424 mem_put_byte (address
+ 1, (value
>> 16) & 0xff);
425 mem_put_byte (address
+ 2, (value
>> 8) & 0xff);
426 mem_put_byte (address
+ 3, value
& 0xff);
430 mem_put_byte (address
+ 0, value
& 0xff);
431 mem_put_byte (address
+ 1, (value
>> 8) & 0xff);
432 mem_put_byte (address
+ 2, (value
>> 16) & 0xff);
433 mem_put_byte (address
+ 3, (value
>> 24) & 0xff);
440 mem_put_blk (int address
, void *bufptr_void
, int nbytes
)
442 unsigned char *bufptr
= (unsigned char *) bufptr_void
;
446 mem_counters
[1][1] += nbytes
;
448 mem_put_byte (address
++, *bufptr
++);
453 mem_get_pc (int address
)
455 unsigned char *m
= rx_mem_ptr (address
, MPA_READING
);
461 mem_get_byte (unsigned int address
)
466 m
= rx_mem_ptr (address
, MPA_READING
);
469 case 0x00088264: /* SCI4.SSR */
471 return 0x04; /* transmitter empty */
475 printf (" %02x%c", *m
, mtypec (address
));
476 if (is_reserved_address (address
))
477 generate_access_exception ();
485 mem_get_qi (int address
)
489 rv
= mem_get_byte (address
);
496 mem_get_hi (int address
)
502 #ifdef CYCLE_ACCURATE
503 case 0x00088126: /* TPU1.TCNT */
504 rv
= (regs
.cycle_count
- tpu_base
) >> 16;
506 case 0x00088136: /* TPU2.TCNT */
507 rv
= (regs
.cycle_count
- tpu_base
) >> 0;
514 rv
= mem_get_byte (address
) << 8;
515 rv
|= mem_get_byte (address
+ 1);
519 rv
= mem_get_byte (address
);
520 rv
|= mem_get_byte (address
+ 1) << 8;
529 mem_get_psi (int address
)
535 rv
= mem_get_byte (address
+ 2);
536 rv
|= mem_get_byte (address
+ 1) << 8;
537 rv
|= mem_get_byte (address
) << 16;
541 rv
= mem_get_byte (address
);
542 rv
|= mem_get_byte (address
+ 1) << 8;
543 rv
|= mem_get_byte (address
+ 2) << 16;
551 mem_get_si (int address
)
557 rv
= mem_get_byte (address
+ 3);
558 rv
|= mem_get_byte (address
+ 2) << 8;
559 rv
|= mem_get_byte (address
+ 1) << 16;
560 rv
|= mem_get_byte (address
) << 24;
564 rv
= mem_get_byte (address
);
565 rv
|= mem_get_byte (address
+ 1) << 8;
566 rv
|= mem_get_byte (address
+ 2) << 16;
567 rv
|= mem_get_byte (address
+ 3) << 24;
575 mem_get_blk (int address
, void *bufptr_void
, int nbytes
)
577 char *bufptr
= (char *) bufptr_void
;
581 mem_counters
[0][1] += nbytes
;
583 *bufptr
++ = mem_get_byte (address
++);
588 sign_ext (int v
, int bits
)
592 v
&= (1 << bits
) - 1;
593 if (v
& (1 << (bits
- 1)))
600 mem_set_content_type (int address
, enum mem_content_type type
)
602 unsigned char *mt
= rx_mem_ptr (address
, MPA_CONTENT_TYPE
);
607 mem_set_content_range (int start_address
, int end_address
, enum mem_content_type type
)
609 while (start_address
< end_address
)
614 sz
= end_address
- start_address
;
615 ofs
= start_address
% L1_LEN
;
616 if (sz
+ ofs
> L1_LEN
)
619 mt
= rx_mem_ptr (start_address
, MPA_CONTENT_TYPE
);
620 memset (mt
, type
, sz
);
626 enum mem_content_type
627 mem_get_content_type (int address
)
629 unsigned char *mt
= rx_mem_ptr (address
, MPA_CONTENT_TYPE
);