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/>. */
27 #include <sys/types.h>
29 #include <sys/select.h>
45 #define L1_LEN (1 << L1_BITS)
46 #define L2_LEN (1 << L2_BITS)
47 #define OFF_LEN (1 << OFF_BITS)
49 static unsigned char **pt
[L1_LEN
];
51 int m32c_console_ifd
= 0;
52 int m32c_console_ofd
= 1;
53 int m32c_use_raw_console
= 0;
59 /* [ get=0/put=1 ][ byte size ] */
60 static unsigned int mem_counters
[2][5];
62 #define COUNT(isput,bytes) \
63 if (verbose && enable_counting) mem_counters[isput][bytes]++
70 for (i
= 0; i
< L1_LEN
; i
++)
73 for (j
= 0; j
< L2_LEN
; j
++)
78 memset (pt
, 0, sizeof (pt
));
79 memset (mem_counters
, 0, sizeof (mem_counters
));
82 static unsigned char *
85 static int recursing
= 0;
86 int pt1
= (address
>> (L2_BITS
+ OFF_BITS
)) & ((1 << L1_BITS
) - 1);
87 int pt2
= (address
>> OFF_BITS
) & ((1 << L2_BITS
) - 1);
88 int pto
= address
& ((1 << OFF_BITS
) - 1);
90 if (address
== 0 && !recursing
)
93 put_reg (pc
, m32c_opcode_pc
);
94 printf ("NULL pointer dereference at pc=0x%x\n", get_reg (pc
));
95 step_result
= M32C_MAKE_HIT_BREAK ();
97 /* This code can be re-enabled to help diagnose NULL pointer
98 bugs that aren't debuggable in GDB. */
99 m32c_dump_all_registers ();
105 pt
[pt1
] = (unsigned char **) calloc (L2_LEN
, sizeof (char **));
106 if (pt
[pt1
][pt2
] == 0)
108 pt
[pt1
][pt2
] = (unsigned char *) malloc (OFF_LEN
);
109 memset (pt
[pt1
][pt2
], 0, OFF_LEN
);
112 return pt
[pt1
][pt2
] + pto
;
116 used (int rstart
, int i
, int j
)
118 int rend
= i
<< (L2_BITS
+ OFF_BITS
);
119 rend
+= j
<< OFF_BITS
;
120 if (rstart
== 0xe0000 && rend
== 0xe1000)
122 printf ("mem: %08x - %08x (%dk bytes)\n", rstart
, rend
- 1,
123 (rend
- rstart
) / 1024);
127 mcs (int isput
, int bytes
)
129 return comma (mem_counters
[isput
][bytes
]);
139 for (i
= 0; i
< L1_LEN
; i
++)
142 for (j
= 0; j
< L2_LEN
; j
++)
148 rstart
= (i
<< (L2_BITS
+ OFF_BITS
)) + (j
<< OFF_BITS
);
165 /* mem foo: 123456789012 123456789012 123456789012 123456789012
167 printf (" byte short pointer long"
169 printf ("mem get: %12s %12s %12s %12s %12s\n", mcs (0, 1), mcs (0, 2),
170 mcs (0, 3), mcs (0, 4), mcs (0, 0));
171 printf ("mem put: %12s %12s %12s %12s\n", mcs (1, 1), mcs (1, 2),
172 mcs (1, 3), mcs (1, 4));
177 s (int address
, char *dir
)
180 printf ("MEM[%0*x] %s", membus_mask
== 0xfffff ? 5 : 6, address
, dir
);
184 #define S(d) if (trace) s(address, d)
195 #define E() if (trace) e()
197 extern int m32c_disassemble
;
200 mem_put_byte (int address
, unsigned char value
)
203 address
&= membus_mask
;
204 m
= mem_ptr (address
);
206 printf (" %02x", value
);
212 static int old_led
= -1;
213 static char *led_on
[] =
214 { "\033[31m O ", "\033[32m O ", "\033[34m O " };
215 static char *led_off
[] = { "\033[0m · ", "\033[0m · ", "\033[0m · " };
217 if (old_led
!= value
)
220 for (i
= 0; i
< 3; i
++)
221 if (value
& (1 << i
))
222 fputs (led_off
[i
], stdout
);
224 fputs (led_on
[i
], stdout
);
225 fputs ("\033[0m\r", stdout
);
233 case 0x346: /* TA0low */
234 timer_a
.count
= (timer_a
.count
& 0xff00) | value
;
235 timer_a
.reload
= timer_a
.count
;
237 case 0x347: /* TA0high */
238 timer_a
.count
= (timer_a
.count
& 0x00ff) | (value
<< 8);
239 timer_a
.reload
= timer_a
.count
;
241 case 0x340: /* TABSR */
244 case 0x356: /* TA0MR */
245 timer_a
.mode
= value
;
247 case 0x35f: /* TCSPR */
248 timer_a
.tcspr
= value
;
250 case 0x006c: /* TA0IC */
255 case 0x100: /* TRACR */
258 case 0x102: /* TRAMR */
259 timer_a
.mode
= value
;
261 case 0x104: /* TRA */
262 timer_a
.count
= value
;
263 timer_a
.reload
= value
;
265 case 0x103: /* TRAPRE */
266 timer_a
.tcspr
= value
;
268 case 0x0056: /* TA0IC */
273 case 0x2ea: /* m32c uart1tx */
274 case 0x3aa: /* m16c uart1tx */
276 static int pending_exit
= 0;
281 step_result
= M32C_MAKE_EXITED (value
);
288 write (m32c_console_ofd
, &value
, 1);
294 m32c_syscall (value
);
302 printf ("SimTrace: %06lx %02x\n", regs
.r_pc
, value
);
306 printf ("SimTrap: %06lx %02x\n", regs
.r_pc
, value
);
312 mem_put_qi (int address
, unsigned char value
)
315 mem_put_byte (address
, value
& 0xff);
321 mem_put_hi (int address
, unsigned short value
)
323 if (address
== 0x402)
325 printf ("SimTrace: %06lx %04x\n", regs
.r_pc
, value
);
329 mem_put_byte (address
, value
& 0xff);
330 mem_put_byte (address
+ 1, value
>> 8);
336 mem_put_psi (int address
, unsigned long value
)
339 mem_put_byte (address
, value
& 0xff);
340 mem_put_byte (address
+ 1, (value
>> 8) & 0xff);
341 mem_put_byte (address
+ 2, value
>> 16);
347 mem_put_si (int address
, unsigned long value
)
350 mem_put_byte (address
, value
& 0xff);
351 mem_put_byte (address
+ 1, (value
>> 8) & 0xff);
352 mem_put_byte (address
+ 2, (value
>> 16) & 0xff);
353 mem_put_byte (address
+ 3, (value
>> 24) & 0xff);
359 mem_put_blk (int address
, void *bufptr
, int nbytes
)
363 mem_counters
[1][1] += nbytes
;
365 mem_put_byte (address
++, *(unsigned char *) bufptr
++);
372 unsigned char *m
= mem_ptr (regs
.r_pc
& membus_mask
);
377 static int console_raw
= 0;
378 static struct termios oattr
;
390 FD_SET (m32c_console_ifd
, &ifd
);
391 n
= select (1, &ifd
, 0, 0, &t
);
396 m32c_sim_restore_console ()
399 tcsetattr (m32c_console_ifd
, TCSANOW
, &oattr
);
404 mem_get_byte (int address
)
407 address
&= membus_mask
;
408 m
= mem_ptr (address
);
411 case 0x2ed: /* m32c uart1c1 */
412 case 0x3ad: /* m16c uart1c1 */
414 if (!console_raw
&& m32c_use_raw_console
)
417 tcgetattr (m32c_console_ifd
, &attr
);
418 tcgetattr (m32c_console_ifd
, &oattr
);
419 /* We want each key to be sent as the user presses them. */
420 attr
.c_lflag
&= ~(ICANON
| ECHO
| ECHOE
);
421 tcsetattr (m32c_console_ifd
, TCSANOW
, &attr
);
423 atexit (m32c_sim_restore_console
);
427 return 0x02; /* tx empty and rx full */
429 return 0x0a; /* transmitter empty */
431 case 0x2ee: /* m32c uart1 rx */
434 read (m32c_console_ifd
, &c
, 1);
435 if (m32c_console_ifd
== 0 && c
== 3) /* Ctrl-C */
437 printf ("Ctrl-C!\n");
441 if (m32c_console_ifd
!= 1)
444 printf ("\033[31m%c\033[0m", c
);
446 printf ("\033[31m%02x\033[0m", c
);
452 case 0x346: /* TA0low */
453 return timer_a
.count
& 0xff;
454 case 0x347: /* TA0high */
455 return (timer_a
.count
>> 8) & 0xff;
456 case 0x104: /* TRA */
457 return timer_a
.count
;
464 printf (" %02x", *m
);
470 mem_get_qi (int address
)
474 rv
= mem_get_byte (address
);
481 mem_get_hi (int address
)
485 rv
= mem_get_byte (address
);
486 rv
|= mem_get_byte (address
+ 1) * 256;
493 mem_get_psi (int address
)
497 rv
= mem_get_byte (address
);
498 rv
|= mem_get_byte (address
+ 1) * 256;
499 rv
|= mem_get_byte (address
+ 2) * 65536;
506 mem_get_si (int address
)
510 rv
= mem_get_byte (address
);
511 rv
|= mem_get_byte (address
+ 1) << 8;
512 rv
|= mem_get_byte (address
+ 2) << 16;
513 rv
|= mem_get_byte (address
+ 3) << 24;
520 mem_get_blk (int address
, void *bufptr
, int nbytes
)
524 mem_counters
[0][1] += nbytes
;
526 *(char *) bufptr
++ = mem_get_byte (address
++);
531 sign_ext (int v
, int bits
)
535 v
&= (1 << bits
) - 1;
536 if (v
& (1 << (bits
- 1)))
549 if (timer_a
.prescale
< 0)
553 switch (timer_a
.mode
& 0xc0)
556 timer_a
.prescale
= 0;
559 timer_a
.prescale
= 8;
562 timer_a
.prescale
= timer_a
.tcspr
& 0x0f;
565 timer_a
.prescale
= 32;
571 timer_a
.prescale
= timer_a
.tcspr
;
574 if (timer_a
.count
< 0)
576 timer_a
.count
= timer_a
.reload
;
580 mem_put_qi (0x6c, timer_a
.ic
| 0x08);
582 mem_put_qi (0x56, timer_a
.ic
| 0x08);
588 if (regs
.r_flags
& FLAGBIT_I
/* interrupts enabled */
589 && timer_a
.ic
& 0x08 /* timer A interrupt triggered */
590 && (timer_a
.ic
& 0x07) > ((regs
.r_flags
>> 12) & 0x07))
593 trigger_peripheral_interrupt (12, 0x06c);
595 trigger_peripheral_interrupt (22, 0x056);
This page took 0.054084 seconds and 4 git commands to generate.