| 1 | /* mem.c --- memory for M32C simulator. |
| 2 | |
| 3 | Copyright (C) 2005-2016 Free Software Foundation, Inc. |
| 4 | Contributed by Red Hat, Inc. |
| 5 | |
| 6 | This file is part of the GNU simulators. |
| 7 | |
| 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. |
| 12 | |
| 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. |
| 17 | |
| 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/>. */ |
| 20 | |
| 21 | |
| 22 | #include "config.h" |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <ctype.h> |
| 27 | #include <sys/time.h> |
| 28 | #include <sys/types.h> |
| 29 | #include <unistd.h> |
| 30 | #ifdef HAVE_SYS_SELECT_H |
| 31 | #include <sys/select.h> |
| 32 | #endif |
| 33 | #ifdef HAVE_TERMIOS_H |
| 34 | #include <termios.h> |
| 35 | #endif |
| 36 | |
| 37 | #include "mem.h" |
| 38 | #include "cpu.h" |
| 39 | #include "syscalls.h" |
| 40 | #include "misc.h" |
| 41 | #ifdef TIMER_A |
| 42 | #include "int.h" |
| 43 | #include "timer_a.h" |
| 44 | #endif |
| 45 | |
| 46 | #define L1_BITS (10) |
| 47 | #define L2_BITS (10) |
| 48 | #define OFF_BITS (12) |
| 49 | |
| 50 | #define L1_LEN (1 << L1_BITS) |
| 51 | #define L2_LEN (1 << L2_BITS) |
| 52 | #define OFF_LEN (1 << OFF_BITS) |
| 53 | |
| 54 | static unsigned char **pt[L1_LEN]; |
| 55 | |
| 56 | #ifdef HAVE_TERMIOS_H |
| 57 | int m32c_console_ifd = 0; |
| 58 | #endif |
| 59 | int m32c_console_ofd = 1; |
| 60 | #ifdef HAVE_TERMIOS_H |
| 61 | int m32c_use_raw_console = 0; |
| 62 | #endif |
| 63 | |
| 64 | #ifdef TIMER_A |
| 65 | Timer_A timer_a; |
| 66 | #endif |
| 67 | |
| 68 | /* [ get=0/put=1 ][ byte size ] */ |
| 69 | static unsigned int mem_counters[2][5]; |
| 70 | |
| 71 | #define COUNT(isput,bytes) \ |
| 72 | if (verbose && enable_counting) mem_counters[isput][bytes]++ |
| 73 | |
| 74 | void |
| 75 | init_mem (void) |
| 76 | { |
| 77 | int i, j; |
| 78 | |
| 79 | for (i = 0; i < L1_LEN; i++) |
| 80 | if (pt[i]) |
| 81 | { |
| 82 | for (j = 0; j < L2_LEN; j++) |
| 83 | if (pt[i][j]) |
| 84 | free (pt[i][j]); |
| 85 | free (pt[i]); |
| 86 | } |
| 87 | memset (pt, 0, sizeof (pt)); |
| 88 | memset (mem_counters, 0, sizeof (mem_counters)); |
| 89 | } |
| 90 | |
| 91 | static unsigned char * |
| 92 | mem_ptr (int address) |
| 93 | { |
| 94 | static int recursing = 0; |
| 95 | int pt1 = (address >> (L2_BITS + OFF_BITS)) & ((1 << L1_BITS) - 1); |
| 96 | int pt2 = (address >> OFF_BITS) & ((1 << L2_BITS) - 1); |
| 97 | int pto = address & ((1 << OFF_BITS) - 1); |
| 98 | |
| 99 | if (address == 0 && !recursing) |
| 100 | { |
| 101 | recursing = 1; |
| 102 | put_reg (pc, m32c_opcode_pc); |
| 103 | printf ("NULL pointer dereference at pc=0x%x\n", get_reg (pc)); |
| 104 | step_result = M32C_MAKE_HIT_BREAK (); |
| 105 | #if 0 |
| 106 | /* This code can be re-enabled to help diagnose NULL pointer |
| 107 | bugs that aren't debuggable in GDB. */ |
| 108 | m32c_dump_all_registers (); |
| 109 | exit (1); |
| 110 | #endif |
| 111 | } |
| 112 | |
| 113 | if (pt[pt1] == 0) |
| 114 | pt[pt1] = (unsigned char **) calloc (L2_LEN, sizeof (char **)); |
| 115 | if (pt[pt1][pt2] == 0) |
| 116 | { |
| 117 | pt[pt1][pt2] = (unsigned char *) malloc (OFF_LEN); |
| 118 | memset (pt[pt1][pt2], 0, OFF_LEN); |
| 119 | } |
| 120 | |
| 121 | return pt[pt1][pt2] + pto; |
| 122 | } |
| 123 | |
| 124 | static void |
| 125 | used (int rstart, int i, int j) |
| 126 | { |
| 127 | int rend = i << (L2_BITS + OFF_BITS); |
| 128 | rend += j << OFF_BITS; |
| 129 | if (rstart == 0xe0000 && rend == 0xe1000) |
| 130 | return; |
| 131 | printf ("mem: %08x - %08x (%dk bytes)\n", rstart, rend - 1, |
| 132 | (rend - rstart) / 1024); |
| 133 | } |
| 134 | |
| 135 | static char * |
| 136 | mcs (int isput, int bytes) |
| 137 | { |
| 138 | return comma (mem_counters[isput][bytes]); |
| 139 | } |
| 140 | |
| 141 | void |
| 142 | mem_usage_stats (void) |
| 143 | { |
| 144 | int i, j; |
| 145 | int rstart = 0; |
| 146 | int pending = 0; |
| 147 | |
| 148 | for (i = 0; i < L1_LEN; i++) |
| 149 | if (pt[i]) |
| 150 | { |
| 151 | for (j = 0; j < L2_LEN; j++) |
| 152 | if (pt[i][j]) |
| 153 | { |
| 154 | if (!pending) |
| 155 | { |
| 156 | pending = 1; |
| 157 | rstart = (i << (L2_BITS + OFF_BITS)) + (j << OFF_BITS); |
| 158 | } |
| 159 | } |
| 160 | else if (pending) |
| 161 | { |
| 162 | pending = 0; |
| 163 | used (rstart, i, j); |
| 164 | } |
| 165 | } |
| 166 | else |
| 167 | { |
| 168 | if (pending) |
| 169 | { |
| 170 | pending = 0; |
| 171 | used (rstart, i, 0); |
| 172 | } |
| 173 | } |
| 174 | /* mem foo: 123456789012 123456789012 123456789012 123456789012 |
| 175 | 123456789012 */ |
| 176 | printf (" byte short pointer long" |
| 177 | " fetch\n"); |
| 178 | printf ("mem get: %12s %12s %12s %12s %12s\n", mcs (0, 1), mcs (0, 2), |
| 179 | mcs (0, 3), mcs (0, 4), mcs (0, 0)); |
| 180 | printf ("mem put: %12s %12s %12s %12s\n", mcs (1, 1), mcs (1, 2), |
| 181 | mcs (1, 3), mcs (1, 4)); |
| 182 | } |
| 183 | |
| 184 | static int tpr = 0; |
| 185 | static void |
| 186 | s (int address, char *dir) |
| 187 | { |
| 188 | if (tpr == 0) |
| 189 | printf ("MEM[%0*x] %s", membus_mask == 0xfffff ? 5 : 6, address, dir); |
| 190 | tpr++; |
| 191 | } |
| 192 | |
| 193 | #define S(d) if (trace) s(address, d) |
| 194 | static void |
| 195 | e (void) |
| 196 | { |
| 197 | if (!trace) |
| 198 | return; |
| 199 | tpr--; |
| 200 | if (tpr == 0) |
| 201 | printf ("\n"); |
| 202 | } |
| 203 | |
| 204 | #define E() if (trace) e() |
| 205 | |
| 206 | extern int m32c_disassemble; |
| 207 | |
| 208 | static void |
| 209 | mem_put_byte (int address, unsigned char value) |
| 210 | { |
| 211 | unsigned char *m; |
| 212 | address &= membus_mask; |
| 213 | m = mem_ptr (address); |
| 214 | if (trace) |
| 215 | printf (" %02x", value); |
| 216 | *m = value; |
| 217 | switch (address) |
| 218 | { |
| 219 | case 0x00e1: |
| 220 | { |
| 221 | static int old_led = -1; |
| 222 | static char *led_on[] = |
| 223 | { "\033[31m O ", "\033[32m O ", "\033[34m O " }; |