1 /* mem.c --- memory for M32C simulator.
3 Copyright (C) 2005, 2007, 2008, 2009 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/>. */
28 #include <sys/types.h>
30 #ifdef HAVE_SYS_SELECT_H
31 #include <sys/select.h>
50 #define L1_LEN (1 << L1_BITS)
51 #define L2_LEN (1 << L2_BITS)
52 #define OFF_LEN (1 << OFF_BITS)
54 static unsigned char **pt[L1_LEN];
57 int m32c_console_ifd = 0;
59 int m32c_console_ofd = 1;
61 int m32c_use_raw_console = 0;
68 /* [ get=0/put=1 ][ byte size ] */
69 static unsigned int mem_counters[2][5];
71 #define COUNT(isput,bytes) \
72 if (verbose && enable_counting) mem_counters[isput][bytes]++
79 for (i = 0; i < L1_LEN; i++)
82 for (j = 0; j < L2_LEN; j++)
87 memset (pt, 0, sizeof (pt));
88 memset (mem_counters, 0, sizeof (mem_counters));
91 static unsigned char *
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);
99 if (address == 0 && !recursing)
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 ();
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 ();
114 pt[pt1] = (unsigned char **) calloc (L2_LEN, sizeof (char **));
115 if (pt[pt1][pt2] == 0)
117 pt[pt1][pt2] = (unsigned char *) malloc (OFF_LEN);
118 memset (pt[pt1][pt2], 0, OFF_LEN);
121 return pt[pt1][pt2] + pto;
125 used (int rstart, int i, int j)
127 int rend = i << (L2_BITS + OFF_BITS);
128 rend += j << OFF_BITS;
129 if (rstart == 0xe0000 && rend == 0xe1000)
131 printf ("mem: %08x - %08x (%dk bytes)\n", rstart, rend - 1,
132 (rend - rstart) / 1024);
136 mcs (int isput, int bytes)
138 return comma (mem_counters[isput][bytes]);
148 for (i = 0; i < L1_LEN; i++)
151 for (j = 0; j < L2_LEN; j++)
157 rstart = (i << (L2_BITS + OFF_BITS)) + (j << OFF_BITS);
174 /* mem foo: 123456789012 123456789012 123456789012 123456789012
176 printf (" byte short pointer long"
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));
186 s (int address, char *dir)
189 printf ("MEM[%0*x] %s", membus_mask == 0xfffff ? 5 : 6, address, dir);
193 #define S(d) if (trace) s(address, d)
204 #define E() if (trace) e()
206 extern int m32c_disassemble;
209 mem_put_byte (int address, unsigned char value)
212 address &= membus_mask;
213 m = mem_ptr (address);
215 printf (" %02x", value);
221 static int old_led = -1;
222 static char *led_on[] =
223 { "\033[31m O ", "\033[32m O ", "\033[34m O " };
224 static char *led_off[] = { "\033[0m · ", "\033[0m · ", "\033[0m · " };
226 if (old_led != value)
229 for (i = 0; i < 3; i++)
230 if (value & (1 << i))
231 fputs (led_off[i], stdout);
233 fputs (led_on[i], stdout);
234 fputs ("\033[0m\r", stdout);
242 case 0x346: /* TA0low */
243 timer_a.count = (timer_a.count & 0xff00) | value;
244 timer_a.reload = timer_a.count;
246 case 0x347: /* TA0high */
247 timer_a.count = (timer_a.count & 0x00ff) | (value << 8);
248 timer_a.reload = timer_a.count;
250 case 0x340: /* TABSR */
253 case 0x356: /* TA0MR */
254 timer_a.mode = value;
256 case 0x35f: /* TCSPR */
257 timer_a.tcspr = value;
259 case 0x006c: /* TA0IC */
264 case 0x100: /* TRACR */
267 case 0x102: /* TRAMR */
268 timer_a.mode = value;
270 case 0x104: /* TRA */
271 timer_a.count = value;
272 timer_a.reload = value;
274 case 0x103: /* TRAPRE */
275 timer_a.tcspr = value;
277 case 0x0056: /* TA0IC */
282 case 0x2ea: /* m32c uart1tx */
283 case 0x3aa: /* m16c uart1tx */
285 static int pending_exit = 0;
290 step_result = M32C_MAKE_EXITED (value);
297 write (m32c_console_ofd, &value, 1);
303 m32c_syscall (value);
311 printf ("SimTrace: %06lx %02x\n", regs.r_pc, value);
315 printf ("SimTrap: %06lx %02x\n", regs.r_pc, value);
321 mem_put_qi (int address, unsigned char value)
324 mem_put_byte (address, value & 0xff);
330 mem_put_hi (int address, unsigned short value)
332 if (address == 0x402)
334 printf ("SimTrace: %06lx %04x\n", regs.r_pc, value);
338 mem_put_byte (address, value & 0xff);
339 mem_put_byte (address + 1, value >> 8);
345 mem_put_psi (int address, unsigned long value)
348 mem_put_byte (address, value & 0xff);
349 mem_put_byte (address + 1, (value >> 8) & 0xff);
350 mem_put_byte (address + 2, value >> 16);
356 mem_put_si (int address, unsigned long value)
359 mem_put_byte (address, value & 0xff);
360 mem_put_byte (address + 1, (value >> 8) & 0xff);
361 mem_put_byte (address + 2, (value >> 16) & 0xff);
362 mem_put_byte (address + 3, (value >> 24) & 0xff);
368 mem_put_blk (int address, void *bufptr, int nbytes)
372 mem_counters[1][1] += nbytes;
374 mem_put_byte (address++, *(unsigned char *) bufptr++);
381 unsigned char *m = mem_ptr (regs.r_pc & membus_mask);
386 #ifdef HAVE_TERMIOS_H
387 static int console_raw = 0;
388 static struct termios oattr;
400 FD_SET (m32c_console_ifd, &ifd);
401 n = select (1, &ifd, 0, 0, &t);
406 m32c_sim_restore_console ()
409 tcsetattr (m32c_console_ifd, TCSANOW, &oattr);
415 mem_get_byte (int address)
418 address &= membus_mask;
419 m = mem_ptr (address);
422 #ifdef HAVE_TERMIOS_H
423 case 0x2ed: /* m32c uart1c1 */
424 case 0x3ad: /* m16c uart1c1 */
426 if (!console_raw && m32c_use_raw_console)
429 tcgetattr (m32c_console_ifd, &attr);
430 tcgetattr (m32c_console_ifd, &oattr);
431 /* We want each key to be sent as the user presses them. */
432 attr.c_lflag &= ~(ICANON | ECHO | ECHOE);
433 tcsetattr (m32c_console_ifd, TCSANOW, &attr);
435 atexit (m32c_sim_restore_console);
439 return 0x02; /* tx empty and rx full */
441 return 0x0a; /* transmitter empty */
443 case 0x2ee: /* m32c uart1 rx */
446 read (m32c_console_ifd, &c, 1);
447 if (m32c_console_ifd == 0 && c == 3) /* Ctrl-C */
449 printf ("Ctrl-C!\n");
453 if (m32c_console_ifd != 1)
456 printf ("\033[31m%c\033[0m", c);
458 printf ("\033[31m%02x\033[0m", c);
465 case 0x346: /* TA0low */
466 return timer_a.count & 0xff;
467 case 0x347: /* TA0high */
468 return (timer_a.count >> 8) & 0xff;
469 case 0x104: /* TRA */
470 return timer_a.count;
474 /* In case both cases above are not included. */
480 printf (" %02x", *m);
486 mem_get_qi (int address)
490 rv = mem_get_byte (address);
497 mem_get_hi (int address)
501 rv = mem_get_byte (address);
502 rv |= mem_get_byte (address + 1) * 256;
509 mem_get_psi (int address)
513 rv = mem_get_byte (address);
514 rv |= mem_get_byte (address + 1) * 256;
515 rv |= mem_get_byte (address + 2) * 65536;
522 mem_get_si (int address)
526 rv = mem_get_byte (address);
527 rv |= mem_get_byte (address + 1) << 8;
528 rv |= mem_get_byte (address + 2) << 16;
529 rv |= mem_get_byte (address + 3) << 24;
536 mem_get_blk (int address, void *bufptr, int nbytes)
540 mem_counters[0][1] += nbytes;
542 *(char *) bufptr++ = mem_get_byte (address++);
547 sign_ext (int v, int bits)
551 v &= (1 << bits) - 1;
552 if (v & (1 << (bits - 1)))
565 if (timer_a.prescale < 0)
569 switch (timer_a.mode & 0xc0)
572 timer_a.prescale = 0;
575 timer_a.prescale = 8;
578 timer_a.prescale = timer_a.tcspr & 0x0f;
581 timer_a.prescale = 32;
587 timer_a.prescale = timer_a.tcspr;
590 if (timer_a.count < 0)
592 timer_a.count = timer_a.reload;
596 mem_put_qi (0x6c, timer_a.ic | 0x08);
598 mem_put_qi (0x56, timer_a.ic | 0x08);
604 if (regs.r_flags & FLAGBIT_I /* interrupts enabled */
605 && timer_a.ic & 0x08 /* timer A interrupt triggered */
606 && (timer_a.ic & 0x07) > ((regs.r_flags >> 12) & 0x07))
609 trigger_peripheral_interrupt (12, 0x06c);
611 trigger_peripheral_interrupt (22, 0x056);