1 /* mem.c --- memory for M32C simulator.
3 Copyright (C) 2005, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
5 Contributed by Red Hat, Inc.
7 This file is part of the GNU simulators.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
29 #include <sys/types.h>
31 #ifdef HAVE_SYS_SELECT_H
32 #include <sys/select.h>
51 #define L1_LEN (1 << L1_BITS)
52 #define L2_LEN (1 << L2_BITS)
53 #define OFF_LEN (1 << OFF_BITS)
55 static unsigned char **pt[L1_LEN];
58 int m32c_console_ifd = 0;
60 int m32c_console_ofd = 1;
62 int m32c_use_raw_console = 0;
69 /* [ get=0/put=1 ][ byte size ] */
70 static unsigned int mem_counters[2][5];
72 #define COUNT(isput,bytes) \
73 if (verbose && enable_counting) mem_counters[isput][bytes]++
80 for (i = 0; i < L1_LEN; i++)
83 for (j = 0; j < L2_LEN; j++)
88 memset (pt, 0, sizeof (pt));
89 memset (mem_counters, 0, sizeof (mem_counters));
92 static unsigned char *
95 static int recursing = 0;
96 int pt1 = (address >> (L2_BITS + OFF_BITS)) & ((1 << L1_BITS) - 1);
97 int pt2 = (address >> OFF_BITS) & ((1 << L2_BITS) - 1);
98 int pto = address & ((1 << OFF_BITS) - 1);
100 if (address == 0 && !recursing)
103 put_reg (pc, m32c_opcode_pc);
104 printf ("NULL pointer dereference at pc=0x%x\n", get_reg (pc));
105 step_result = M32C_MAKE_HIT_BREAK ();
107 /* This code can be re-enabled to help diagnose NULL pointer
108 bugs that aren't debuggable in GDB. */
109 m32c_dump_all_registers ();
115 pt[pt1] = (unsigned char **) calloc (L2_LEN, sizeof (char **));
116 if (pt[pt1][pt2] == 0)
118 pt[pt1][pt2] = (unsigned char *) malloc (OFF_LEN);
119 memset (pt[pt1][pt2], 0, OFF_LEN);
122 return pt[pt1][pt2] + pto;
126 used (int rstart, int i, int j)
128 int rend = i << (L2_BITS + OFF_BITS);
129 rend += j << OFF_BITS;
130 if (rstart == 0xe0000 && rend == 0xe1000)
132 printf ("mem: %08x - %08x (%dk bytes)\n", rstart, rend - 1,
133 (rend - rstart) / 1024);
137 mcs (int isput, int bytes)
139 return comma (mem_counters[isput][bytes]);
149 for (i = 0; i < L1_LEN; i++)
152 for (j = 0; j < L2_LEN; j++)
158 rstart = (i << (L2_BITS + OFF_BITS)) + (j << OFF_BITS);
175 /* mem foo: 123456789012 123456789012 123456789012 123456789012
177 printf (" byte short pointer long"
179 printf ("mem get: %12s %12s %12s %12s %12s\n", mcs (0, 1), mcs (0, 2),
180 mcs (0, 3), mcs (0, 4), mcs (0, 0));
181 printf ("mem put: %12s %12s %12s %12s\n", mcs (1, 1), mcs (1, 2),
182 mcs (1, 3), mcs (1, 4));
187 s (int address, char *dir)
190 printf ("MEM[%0*x] %s", membus_mask == 0xfffff ? 5 : 6, address, dir);
194 #define S(d) if (trace) s(address, d)
205 #define E() if (trace) e()
207 extern int m32c_disassemble;
210 mem_put_byte (int address, unsigned char value)
213 address &= membus_mask;
214 m = mem_ptr (address);
216 printf (" %02x", value);
222 static int old_led = -1;
223 static char *led_on[] =
224 { "\033[31m O ", "\033[32m O ", "\033[34m O " };
225 static char *led_off[] = { "\033[0m · ", "\033[0m · ", "\033[0m · " };
227 if (old_led != value)
230 for (i = 0; i < 3; i++)
231 if (value & (1 << i))
232 fputs (led_off[i], stdout);
234 fputs (led_on[i], stdout);
235 fputs ("\033[0m\r", stdout);
243 case 0x346: /* TA0low */
244 timer_a.count = (timer_a.count & 0xff00) | value;
245 timer_a.reload = timer_a.count;
247 case 0x347: /* TA0high */
248 timer_a.count = (timer_a.count & 0x00ff) | (value << 8);
249 timer_a.reload = timer_a.count;
251 case 0x340: /* TABSR */
254 case 0x356: /* TA0MR */
255 timer_a.mode = value;
257 case 0x35f: /* TCSPR */
258 timer_a.tcspr = value;
260 case 0x006c: /* TA0IC */
265 case 0x100: /* TRACR */
268 case 0x102: /* TRAMR */
269 timer_a.mode = value;
271 case 0x104: /* TRA */
272 timer_a.count = value;
273 timer_a.reload = value;
275 case 0x103: /* TRAPRE */
276 timer_a.tcspr = value;
278 case 0x0056: /* TA0IC */
283 case 0x2ea: /* m32c uart1tx */
284 case 0x3aa: /* m16c uart1tx */
286 static int pending_exit = 0;
291 step_result = M32C_MAKE_EXITED (value);
298 write (m32c_console_ofd, &value, 1);
304 m32c_syscall (value);
312 printf ("SimTrace: %06lx %02x\n", regs.r_pc, value);
316 printf ("SimTrap: %06lx %02x\n", regs.r_pc, value);
322 mem_put_qi (int address, unsigned char value)
325 mem_put_byte (address, value & 0xff);
331 mem_put_hi (int address, unsigned short value)
333 if (address == 0x402)
335 printf ("SimTrace: %06lx %04x\n", regs.r_pc, value);
339 mem_put_byte (address, value & 0xff);
340 mem_put_byte (address + 1, value >> 8);
346 mem_put_psi (int address, unsigned long value)
349 mem_put_byte (address, value & 0xff);
350 mem_put_byte (address + 1, (value >> 8) & 0xff);
351 mem_put_byte (address + 2, value >> 16);
357 mem_put_si (int address, unsigned long value)
360 mem_put_byte (address, value & 0xff);
361 mem_put_byte (address + 1, (value >> 8) & 0xff);
362 mem_put_byte (address + 2, (value >> 16) & 0xff);
363 mem_put_byte (address + 3, (value >> 24) & 0xff);
369 mem_put_blk (int address, const void *bufptr, int nbytes)
373 mem_counters[1][1] += nbytes;
375 mem_put_byte (address++, *(const unsigned char *) bufptr++);
382 unsigned char *m = mem_ptr (regs.r_pc & membus_mask);
387 #ifdef HAVE_TERMIOS_H
388 static int console_raw = 0;
389 static struct termios oattr;
401 FD_SET (m32c_console_ifd, &ifd);
402 n = select (1, &ifd, 0, 0, &t);
407 m32c_sim_restore_console ()
410 tcsetattr (m32c_console_ifd, TCSANOW, &oattr);
416 mem_get_byte (int address)
419 address &= membus_mask;
420 m = mem_ptr (address);
423 #ifdef HAVE_TERMIOS_H
424 case 0x2ed: /* m32c uart1c1 */
425 case 0x3ad: /* m16c uart1c1 */
427 if (!console_raw && m32c_use_raw_console)
430 tcgetattr (m32c_console_ifd, &attr);
431 tcgetattr (m32c_console_ifd, &oattr);
432 /* We want each key to be sent as the user presses them. */
433 attr.c_lflag &= ~(ICANON | ECHO | ECHOE);
434 tcsetattr (m32c_console_ifd, TCSANOW, &attr);
436 atexit (m32c_sim_restore_console);
440 return 0x02; /* tx empty and rx full */
442 return 0x0a; /* transmitter empty */
444 case 0x2ee: /* m32c uart1 rx */
447 read (m32c_console_ifd, &c, 1);
448 if (m32c_console_ifd == 0 && c == 3) /* Ctrl-C */
450 printf ("Ctrl-C!\n");
454 if (m32c_console_ifd != 1)
457 printf ("\033[31m%c\033[0m", c);
459 printf ("\033[31m%02x\033[0m", c);
466 case 0x346: /* TA0low */
467 return timer_a.count & 0xff;
468 case 0x347: /* TA0high */
469 return (timer_a.count >> 8) & 0xff;
470 case 0x104: /* TRA */
471 return timer_a.count;
475 /* In case both cases above are not included. */
481 printf (" %02x", *m);
487 mem_get_qi (int address)
491 rv = mem_get_byte (address);
498 mem_get_hi (int address)
502 rv = mem_get_byte (address);
503 rv |= mem_get_byte (address + 1) * 256;
510 mem_get_psi (int address)
514 rv = mem_get_byte (address);
515 rv |= mem_get_byte (address + 1) * 256;
516 rv |= mem_get_byte (address + 2) * 65536;
523 mem_get_si (int address)
527 rv = mem_get_byte (address);
528 rv |= mem_get_byte (address + 1) << 8;
529 rv |= mem_get_byte (address + 2) << 16;
530 rv |= mem_get_byte (address + 3) << 24;
537 mem_get_blk (int address, void *bufptr, int nbytes)
541 mem_counters[0][1] += nbytes;
543 *(char *) bufptr++ = mem_get_byte (address++);
548 sign_ext (int v, int bits)
552 v &= (1 << bits) - 1;
553 if (v & (1 << (bits - 1)))
566 if (timer_a.prescale < 0)
570 switch (timer_a.mode & 0xc0)
573 timer_a.prescale = 0;
576 timer_a.prescale = 8;
579 timer_a.prescale = timer_a.tcspr & 0x0f;
582 timer_a.prescale = 32;
588 timer_a.prescale = timer_a.tcspr;
591 if (timer_a.count < 0)
593 timer_a.count = timer_a.reload;
597 mem_put_qi (0x6c, timer_a.ic | 0x08);
599 mem_put_qi (0x56, timer_a.ic | 0x08);
605 if (regs.r_flags & FLAGBIT_I /* interrupts enabled */
606 && timer_a.ic & 0x08 /* timer A interrupt triggered */
607 && (timer_a.ic & 0x07) > ((regs.r_flags >> 12) & 0x07))
610 trigger_peripheral_interrupt (12, 0x06c);
612 trigger_peripheral_interrupt (22, 0x056);