1 /* mem.c --- memory for M32C simulator.
3 Copyright (C) 2005, 2007, 2008 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/>. */
35 #define L1_LEN (1 << L1_BITS)
36 #define L2_LEN (1 << L2_BITS)
37 #define OFF_LEN (1 << OFF_BITS)
39 static unsigned char **pt[L1_LEN];
41 /* [ get=0/put=1 ][ byte size ] */
42 static unsigned int mem_counters[2][4];
44 #define COUNT(isput,bytes) \
45 if (verbose && enable_counting) mem_counters[isput][bytes]++
52 for (i = 0; i < L1_LEN; i++)
55 for (j = 0; j < L2_LEN; j++)
60 memset (pt, 0, sizeof (pt));
61 memset (mem_counters, 0, sizeof (mem_counters));
64 static unsigned char *
67 int pt1 = (address >> (L2_BITS + OFF_BITS)) & ((1 << L1_BITS) - 1);
68 int pt2 = (address >> OFF_BITS) & ((1 << L2_BITS) - 1);
69 int pto = address & ((1 << OFF_BITS) - 1);
73 printf ("NULL pointer dereference\n");
78 pt[pt1] = (unsigned char **) calloc (L2_LEN, sizeof (char **));
79 if (pt[pt1][pt2] == 0)
81 pt[pt1][pt2] = (unsigned char *) malloc (OFF_LEN);
82 memset (pt[pt1][pt2], 0, OFF_LEN);
85 return pt[pt1][pt2] + pto;
89 used (int rstart, int i, int j)
91 int rend = i << (L2_BITS + OFF_BITS);
92 rend += j << OFF_BITS;
93 if (rstart == 0xe0000 && rend == 0xe1000)
95 printf ("mem: %08x - %08x (%dk bytes)\n", rstart, rend - 1,
96 (rend - rstart) / 1024);
100 mcs (int isput, int bytes)
102 return comma (mem_counters[isput][bytes]);
112 for (i = 0; i < L1_LEN; i++)
115 for (j = 0; j < L2_LEN; j++)
121 rstart = (i << (L2_BITS + OFF_BITS)) + (j << OFF_BITS);
138 /* mem foo: 123456789012 123456789012 123456789012 123456789012
140 printf (" byte short pointer long"
142 printf ("mem get: %12s %12s %12s %12s %12s\n", mcs (0, 1), mcs (0, 2),
143 mcs (0, 3), mcs (0, 4), mcs (0, 0));
144 printf ("mem put: %12s %12s %12s %12s\n", mcs (1, 1), mcs (1, 2),
145 mcs (1, 3), mcs (1, 4));
150 s (int address, char *dir)
153 printf ("MEM[%0*x] %s", membus_mask == 0xfffff ? 5 : 6, address, dir);
157 #define S(d) if (trace) s(address, d)
168 #define E() if (trace) e()
171 mem_put_byte (int address, unsigned char value)
174 address &= membus_mask;
175 m = mem_ptr (address);
177 printf (" %02x", value);
183 static int old_led = -1;
184 static char *led_on[] =
185 { "\033[31m O ", "\033[32m O ", "\033[34m O " };
186 static char *led_off[] = { "\033[0m · ", "\033[0m · ", "\033[0m · " };
188 if (old_led != value)
191 for (i = 0; i < 3; i++)
192 if (value & (1 << i))
193 fputs (led_off[i], stdout);
195 fputs (led_on[i], stdout);
196 fputs ("\033[0m\r", stdout);
203 case 0x3aa: /* uart1tx */
205 static int pending_exit = 0;
210 step_result = M32C_MAKE_EXITED(value);
221 m32c_syscall (value);
229 printf ("SimTrace: %06lx %02x\n", regs.r_pc, value);
233 printf ("SimTrap: %06lx %02x\n", regs.r_pc, value);
239 mem_put_qi (int address, unsigned char value)
242 mem_put_byte (address, value & 0xff);
248 mem_put_hi (int address, unsigned short value)
250 if (address == 0x402)
252 printf ("SimTrace: %06lx %04x\n", regs.r_pc, value);
256 mem_put_byte (address, value & 0xff);
257 mem_put_byte (address + 1, value >> 8);
263 mem_put_psi (int address, unsigned long value)
266 mem_put_byte (address, value & 0xff);
267 mem_put_byte (address + 1, (value >> 8) & 0xff);
268 mem_put_byte (address + 2, value >> 16);
274 mem_put_si (int address, unsigned long value)
277 mem_put_byte (address, value & 0xff);
278 mem_put_byte (address + 1, (value >> 8) & 0xff);
279 mem_put_byte (address + 2, (value >> 16) & 0xff);
280 mem_put_byte (address + 3, (value >> 24) & 0xff);
286 mem_put_blk (int address, void *bufptr, int nbytes)
290 mem_counters[1][1] += nbytes;
292 mem_put_byte (address++, *(unsigned char *) bufptr++);
299 unsigned char *m = mem_ptr (regs.r_pc & membus_mask);
305 mem_get_byte (int address)
308 address &= membus_mask;
310 m = mem_ptr (address);
313 case 0x3ad: /* uart1c1 */
315 return 2; /* transmitter empty */
319 printf (" %02x", *m);
327 mem_get_qi (int address)
331 rv = mem_get_byte (address);
338 mem_get_hi (int address)
342 rv = mem_get_byte (address);
343 rv |= mem_get_byte (address + 1) * 256;
350 mem_get_psi (int address)
354 rv = mem_get_byte (address);
355 rv |= mem_get_byte (address + 1) * 256;
356 rv |= mem_get_byte (address + 2) * 65536;
363 mem_get_si (int address)
367 rv = mem_get_byte (address);
368 rv |= mem_get_byte (address + 1) << 8;
369 rv |= mem_get_byte (address + 2) << 16;
370 rv |= mem_get_byte (address + 3) << 24;
377 mem_get_blk (int address, void *bufptr, int nbytes)
381 mem_counters[0][1] += nbytes;
383 *(char *) bufptr++ = mem_get_byte (address++);
388 sign_ext (int v, int bits)
392 v &= (1 << bits) - 1;
393 if (v & (1 << (bits - 1)))