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