Commit | Line | Data |
---|---|---|
d45a4bef JB |
1 | /* mem.c --- memory for M32C simulator. |
2 | ||
ecd75fc8 | 3 | Copyright (C) 2005-2014 Free Software Foundation, Inc. |
d45a4bef JB |
4 | Contributed by Red Hat, Inc. |
5 | ||
6 | This file is part of the GNU simulators. | |
7 | ||
4744ac1b JB |
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. | |
d45a4bef | 12 | |
4744ac1b JB |
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. | |
d45a4bef JB |
17 | |
18 | You should have received a copy of the GNU General Public License | |
4744ac1b | 19 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
d45a4bef JB |
20 | |
21 | ||
340cf1cf | 22 | #include "config.h" |
d45a4bef JB |
23 | #include <stdio.h> |
24 | #include <stdlib.h> | |
25 | #include <string.h> | |
3877a145 DD |
26 | #include <ctype.h> |
27 | #include <sys/time.h> | |
28 | #include <sys/types.h> | |
29 | #include <unistd.h> | |
340cf1cf | 30 | #ifdef HAVE_SYS_SELECT_H |
3877a145 | 31 | #include <sys/select.h> |
340cf1cf DD |
32 | #endif |
33 | #ifdef HAVE_TERMIOS_H | |
3877a145 | 34 | #include <termios.h> |
340cf1cf | 35 | #endif |
d45a4bef JB |
36 | |
37 | #include "mem.h" | |
38 | #include "cpu.h" | |
39 | #include "syscalls.h" | |
40 | #include "misc.h" | |
3877a145 DD |
41 | #ifdef TIMER_A |
42 | #include "int.h" | |
43 | #include "timer_a.h" | |
44 | #endif | |
d45a4bef JB |
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 | ||
340cf1cf | 56 | #ifdef HAVE_TERMIOS_H |
3877a145 | 57 | int m32c_console_ifd = 0; |
340cf1cf | 58 | #endif |
3877a145 | 59 | int m32c_console_ofd = 1; |
340cf1cf | 60 | #ifdef HAVE_TERMIOS_H |
e7ddc197 | 61 | int m32c_use_raw_console = 0; |
340cf1cf | 62 | #endif |
3877a145 DD |
63 | |
64 | #ifdef TIMER_A | |
65 | Timer_A timer_a; | |
66 | #endif | |
67 | ||
d45a4bef | 68 | /* [ get=0/put=1 ][ byte size ] */ |
3877a145 | 69 | static unsigned int mem_counters[2][5]; |
d45a4bef JB |
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 (address) | |
93 | { | |
3877a145 | 94 | static int recursing = 0; |
d45a4bef JB |
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 | ||
3877a145 | 99 | if (address == 0 && !recursing) |
d45a4bef | 100 | { |
3877a145 DD |
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 (); | |
d45a4bef | 109 | exit (1); |
3877a145 | 110 | #endif |
d45a4bef JB |
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 () | |
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" | |
3877a145 | 177 | " fetch\n"); |
d45a4bef JB |
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 () | |
196 | { | |
197 | if (!trace) | |
198 | return; | |
199 | tpr--; | |
200 | if (tpr == 0) | |
201 | printf ("\n"); | |
202 | } | |
203 | ||
204 | #define E() if (trace) e() | |
205 | ||
3877a145 DD |
206 | extern int m32c_disassemble; |
207 | ||
d45a4bef JB |
208 | 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 " }; |