/* mem.c --- memory for M32C simulator.
|
/* mem.c --- memory for M32C simulator.
|
|
|
Copyright (C) 2005, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
|
Copyright (C) 2005, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
|
Contributed by Red Hat, Inc.
|
Contributed by Red Hat, Inc.
|
|
|
This file is part of the GNU simulators.
|
This file is part of the GNU simulators.
|
|
|
This program is free software; you can redistribute it and/or modify
|
This program is free software; you can redistribute it and/or modify
|
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
the Free Software Foundation; either version 3 of the License, or
|
the Free Software Foundation; either version 3 of the License, or
|
(at your option) any later version.
|
(at your option) any later version.
|
|
|
This program is distributed in the hope that it will be useful,
|
This program is distributed in the hope that it will be useful,
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
GNU General Public License for more details.
|
GNU General Public License for more details.
|
|
|
You should have received a copy of the GNU General Public License
|
You should have received a copy of the GNU General Public License
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
|
|
|
|
#include "config.h"
|
#include "config.h"
|
#include <stdio.h>
|
#include <stdio.h>
|
#include <stdlib.h>
|
#include <stdlib.h>
|
#include <string.h>
|
#include <string.h>
|
#include <ctype.h>
|
#include <ctype.h>
|
#include <sys/time.h>
|
#include <sys/time.h>
|
#include <sys/types.h>
|
#include <sys/types.h>
|
#include <unistd.h>
|
#include <unistd.h>
|
#ifdef HAVE_SYS_SELECT_H
|
#ifdef HAVE_SYS_SELECT_H
|
#include <sys/select.h>
|
#include <sys/select.h>
|
#endif
|
#endif
|
#ifdef HAVE_TERMIOS_H
|
#ifdef HAVE_TERMIOS_H
|
#include <termios.h>
|
#include <termios.h>
|
#endif
|
#endif
|
|
|
#include "mem.h"
|
#include "mem.h"
|
#include "cpu.h"
|
#include "cpu.h"
|
#include "syscalls.h"
|
#include "syscalls.h"
|
#include "misc.h"
|
#include "misc.h"
|
#ifdef TIMER_A
|
#ifdef TIMER_A
|
#include "int.h"
|
#include "int.h"
|
#include "timer_a.h"
|
#include "timer_a.h"
|
#endif
|
#endif
|
|
|
#define L1_BITS (10)
|
#define L1_BITS (10)
|
#define L2_BITS (10)
|
#define L2_BITS (10)
|
#define OFF_BITS (12)
|
#define OFF_BITS (12)
|
|
|
#define L1_LEN (1 << L1_BITS)
|
#define L1_LEN (1 << L1_BITS)
|
#define L2_LEN (1 << L2_BITS)
|
#define L2_LEN (1 << L2_BITS)
|
#define OFF_LEN (1 << OFF_BITS)
|
#define OFF_LEN (1 << OFF_BITS)
|
|
|
static unsigned char **pt[L1_LEN];
|
static unsigned char **pt[L1_LEN];
|
|
|
#ifdef HAVE_TERMIOS_H
|
#ifdef HAVE_TERMIOS_H
|
int m32c_console_ifd = 0;
|
int m32c_console_ifd = 0;
|
#endif
|
#endif
|
int m32c_console_ofd = 1;
|
int m32c_console_ofd = 1;
|
#ifdef HAVE_TERMIOS_H
|
#ifdef HAVE_TERMIOS_H
|
int m32c_use_raw_console = 0;
|
int m32c_use_raw_console = 0;
|
#endif
|
#endif
|
|
|
#ifdef TIMER_A
|
#ifdef TIMER_A
|
Timer_A timer_a;
|
Timer_A timer_a;
|
#endif
|
#endif
|
|
|
/* [ get=0/put=1 ][ byte size ] */
|
/* [ get=0/put=1 ][ byte size ] */
|
static unsigned int mem_counters[2][5];
|
static unsigned int mem_counters[2][5];
|
|
|
#define COUNT(isput,bytes) \
|
#define COUNT(isput,bytes) \
|
if (verbose && enable_counting) mem_counters[isput][bytes]++
|
if (verbose && enable_counting) mem_counters[isput][bytes]++
|
|
|
void
|
void
|
init_mem (void)
|
init_mem (void)
|
{
|
{
|
int i, j;
|
int i, j;
|
|
|
for (i = 0; i < L1_LEN; i++)
|
for (i = 0; i < L1_LEN; i++)
|
if (pt[i])
|
if (pt[i])
|
{
|
{
|
for (j = 0; j < L2_LEN; j++)
|
for (j = 0; j < L2_LEN; j++)
|
if (pt[i][j])
|
if (pt[i][j])
|
free (pt[i][j]);
|
free (pt[i][j]);
|
free (pt[i]);
|
free (pt[i]);
|
}
|
}
|
memset (pt, 0, sizeof (pt));
|
memset (pt, 0, sizeof (pt));
|
memset (mem_counters, 0, sizeof (mem_counters));
|
memset (mem_counters, 0, sizeof (mem_counters));
|
}
|
}
|
|
|
static unsigned char *
|
static unsigned char *
|
mem_ptr (address)
|
mem_ptr (address)
|
{
|
{
|
static int recursing = 0;
|
static int recursing = 0;
|
int pt1 = (address >> (L2_BITS + OFF_BITS)) & ((1 << L1_BITS) - 1);
|
int pt1 = (address >> (L2_BITS + OFF_BITS)) & ((1 << L1_BITS) - 1);
|
int pt2 = (address >> OFF_BITS) & ((1 << L2_BITS) - 1);
|
int pt2 = (address >> OFF_BITS) & ((1 << L2_BITS) - 1);
|
int pto = address & ((1 << OFF_BITS) - 1);
|
int pto = address & ((1 << OFF_BITS) - 1);
|
|
|
if (address == 0 && !recursing)
|
if (address == 0 && !recursing)
|
{
|
{
|
recursing = 1;
|
recursing = 1;
|
put_reg (pc, m32c_opcode_pc);
|
put_reg (pc, m32c_opcode_pc);
|
printf ("NULL pointer dereference at pc=0x%x\n", get_reg (pc));
|
printf ("NULL pointer dereference at pc=0x%x\n", get_reg (pc));
|
step_result = M32C_MAKE_HIT_BREAK ();
|
step_result = M32C_MAKE_HIT_BREAK ();
|
#if 0
|
#if 0
|
/* This code can be re-enabled to help diagnose NULL pointer
|
/* This code can be re-enabled to help diagnose NULL pointer
|
bugs that aren't debuggable in GDB. */
|
bugs that aren't debuggable in GDB. */
|
m32c_dump_all_registers ();
|
m32c_dump_all_registers ();
|
exit (1);
|
exit (1);
|
#endif
|
#endif
|
}
|
}
|
|
|
if (pt[pt1] == 0)
|
if (pt[pt1] == 0)
|
pt[pt1] = (unsigned char **) calloc (L2_LEN, sizeof (char **));
|
pt[pt1] = (unsigned char **) calloc (L2_LEN, sizeof (char **));
|
if (pt[pt1][pt2] == 0)
|
if (pt[pt1][pt2] == 0)
|
{
|
{
|
pt[pt1][pt2] = (unsigned char *) malloc (OFF_LEN);
|
pt[pt1][pt2] = (unsigned char *) malloc (OFF_LEN);
|
memset (pt[pt1][pt2], 0, OFF_LEN);
|
memset (pt[pt1][pt2], 0, OFF_LEN);
|
}
|
}
|
|
|
return pt[pt1][pt2] + pto;
|
return pt[pt1][pt2] + pto;
|
}
|
}
|
|
|
static void
|
static void
|
used (int rstart, int i, int j)
|
used (int rstart, int i, int j)
|
{
|
{
|
int rend = i << (L2_BITS + OFF_BITS);
|
int rend = i << (L2_BITS + OFF_BITS);
|
rend += j << OFF_BITS;
|
rend += j << OFF_BITS;
|
if (rstart == 0xe0000 && rend == 0xe1000)
|
if (rstart == 0xe0000 && rend == 0xe1000)
|
return;
|
return;
|
printf ("mem: %08x - %08x (%dk bytes)\n", rstart, rend - 1,
|
printf ("mem: %08x - %08x (%dk bytes)\n", rstart, rend - 1,
|
(rend - rstart) / 1024);
|
(rend - rstart) / 1024);
|
}
|
}
|
|
|
static char *
|
static char *
|
mcs (int isput, int bytes)
|
mcs (int isput, int bytes)
|
{
|
{
|
return comma (mem_counters[isput][bytes]);
|
return comma (mem_counters[isput][bytes]);
|
}
|
}
|
|
|
void
|
void
|
mem_usage_stats ()
|
mem_usage_stats ()
|
{
|
{
|
int i, j;
|
int i, j;
|
int rstart = 0;
|
int rstart = 0;
|
int pending = 0;
|
int pending = 0;
|
|
|
for (i = 0; i < L1_LEN; i++)
|
for (i = 0; i < L1_LEN; i++)
|
if (pt[i])
|
if (pt[i])
|
{
|
{
|
for (j = 0; j < L2_LEN; j++)
|
for (j = 0; j < L2_LEN; j++)
|
if (pt[i][j])
|
if (pt[i][j])
|
{
|
{
|
if (!pending)
|
if (!pending)
|
{
|
{
|
pending = 1;
|
pending = 1;
|
rstart = (i << (L2_BITS + OFF_BITS)) + (j << OFF_BITS);
|
rstart = (i << (L2_BITS + OFF_BITS)) + (j << OFF_BITS);
|
}
|
}
|
}
|
}
|
else if (pending)
|
else if (pending)
|
{
|
{
|
pending = 0;
|
pending = 0;
|
used (rstart, i, j);
|
used (rstart, i, j);
|
}
|
}
|
}
|
}
|
else
|
else
|
{
|
{
|
if (pending)
|
if (pending)
|
{
|
{
|
pending = 0;
|
pending = 0;
|
used (rstart, i, 0);
|
used (rstart, i, 0);
|
}
|
}
|
}
|
}
|
/* mem foo: 123456789012 123456789012 123456789012 123456789012
|
/* mem foo: 123456789012 123456789012 123456789012 123456789012
|
123456789012 */
|
123456789012 */
|
printf (" byte short pointer long"
|
printf (" byte short pointer long"
|
" fetch\n");
|
" fetch\n");
|
printf ("mem get: %12s %12s %12s %12s %12s\n", mcs (0, 1), mcs (0, 2),
|
printf ("mem get: %12s %12s %12s %12s %12s\n", mcs (0, 1), mcs (0, 2),
|
mcs (0, 3), mcs (0, 4), mcs (0, 0));
|
mcs (0, 3), mcs (0, 4), mcs (0, 0));
|
printf ("mem put: %12s %12s %12s %12s\n", mcs (1, 1), mcs (1, 2),
|
printf ("mem put: %12s %12s %12s %12s\n", mcs (1, 1), mcs (1, 2),
|
mcs (1, 3), mcs (1, 4));
|
mcs (1, 3), mcs (1, 4));
|
}
|
}
|
|
|
static int tpr = 0;
|
static int tpr = 0;
|
static void
|
static void
|
s (int address, char *dir)
|
s (int address, char *dir)
|
{
|
{
|
if (tpr == 0)
|
if (tpr == 0)
|
printf ("MEM[%0*x] %s", membus_mask == 0xfffff ? 5 : 6, address, dir);
|
printf ("MEM[%0*x] %s", membus_mask == 0xfffff ? 5 : 6, address, dir);
|
tpr++;
|
tpr++;
|
}
|
}
|
|
|
#define S(d) if (trace) s(address, d)
|
#define S(d) if (trace) s(address, d)
|
static void
|
static void
|
e ()
|
e ()
|
{
|
{
|
if (!trace)
|
if (!trace)
|
return;
|
return;
|
tpr--;
|
tpr--;
|
if (tpr == 0)
|
if (tpr == 0)
|
printf ("\n");
|
printf ("\n");
|
}
|
}
|
|
|
#define E() if (trace) e()
|
#define E() if (trace) e()
|
|
|
extern int m32c_disassemble;
|
extern int m32c_disassemble;
|
|
|
void
|
void
|
mem_put_byte (int address, unsigned char value)
|
mem_put_byte (int address, unsigned char value)
|
{
|
{
|
unsigned char *m;
|
unsigned char *m;
|
address &= membus_mask;
|
address &= membus_mask;
|
m = mem_ptr (address);
|
m = mem_ptr (address);
|
if (trace)
|
if (trace)
|
printf (" %02x", value);
|
printf (" %02x", value);
|
*m = value;
|
*m = value;
|
switch (address)
|
switch (address)
|
{
|
{
|
case 0x00e1:
|
case 0x00e1:
|
{
|
{
|
static int old_led = -1;
|
static int old_led = -1;
|
static char *led_on[] =
|
static char *led_on[] =
|
{ "\033[31m O ", "\033[32m O ", "\033[34m O " };
|
{ "\033[31m O ", "\033[32m O ", "\033[34m O " };
|
static char *led_off[] = { "\033[0m · ", "\033[0m · ", "\033[0m · " };
|
static char *led_off[] = { "\033[0m · ", "\033[0m · ", "\033[0m · " };
|
int i;
|
int i;
|
if (old_led != value)
|
if (old_led != value)
|
{
|
{
|
fputs (" ", stdout);
|
fputs (" ", stdout);
|
for (i = 0; i < 3; i++)
|
for (i = 0; i < 3; i++)
|
if (value & (1 << i))
|
if (value & (1 << i))
|
fputs (led_off[i], stdout);
|
fputs (led_off[i], stdout);
|
else
|
else
|
fputs (led_on[i], stdout);
|
fputs (led_on[i], stdout);
|
fputs ("\033[0m\r", stdout);
|
fputs ("\033[0m\r", stdout);
|
fflush (stdout);
|
fflush (stdout);
|
old_led = value;
|
old_led = value;
|
}
|
}
|
}
|
}
|
break;
|
break;
|
#ifdef TIMER_A
|
#ifdef TIMER_A
|
/* M32C Timer A */
|
/* M32C Timer A */
|
case 0x346: /* TA0low */
|
case 0x346: /* TA0low */
|
timer_a.count = (timer_a.count & 0xff00) | value;
|
timer_a.count = (timer_a.count & 0xff00) | value;
|
timer_a.reload = timer_a.count;
|
timer_a.reload = timer_a.count;
|
break;
|
break;
|
case 0x347: /* TA0high */
|
case 0x347: /* TA0high */
|
timer_a.count = (timer_a.count & 0x00ff) | (value << 8);
|
timer_a.count = (timer_a.count & 0x00ff) | (value << 8);
|
timer_a.reload = timer_a.count;
|
timer_a.reload = timer_a.count;
|
break;
|
break;
|
case 0x340: /* TABSR */
|
case 0x340: /* TABSR */
|
timer_a.bsr = value;
|
timer_a.bsr = value;
|
break;
|
break;
|
case 0x356: /* TA0MR */
|
case 0x356: /* TA0MR */
|
timer_a.mode = value;
|
timer_a.mode = value;
|
break;
|
break;
|
case 0x35f: /* TCSPR */
|
case 0x35f: /* TCSPR */
|
timer_a.tcspr = value;
|
timer_a.tcspr = value;
|
break;
|
break;
|
case 0x006c: /* TA0IC */
|
case 0x006c: /* TA0IC */
|
timer_a.ic = value;
|
timer_a.ic = value;
|
break;
|
break;
|
|
|
/* R8C Timer RA */
|
/* R8C Timer RA */
|
case 0x100: /* TRACR */
|
case 0x100: /* TRACR */
|
timer_a.bsr = value;
|
timer_a.bsr = value;
|
break;
|
break;
|
case 0x102: /* TRAMR */
|
case 0x102: /* TRAMR */
|
timer_a.mode = value;
|
timer_a.mode = value;
|
break;
|
break;
|
case 0x104: /* TRA */
|
case 0x104: /* TRA */
|
timer_a.count = value;
|
timer_a.count = value;
|
timer_a.reload = value;
|
timer_a.reload = value;
|
break;
|
break;
|
case 0x103: /* TRAPRE */
|
case 0x103: /* TRAPRE */
|
timer_a.tcspr = value;
|
timer_a.tcspr = value;
|
break;
|
break;
|
case 0x0056: /* TA0IC */
|
case 0x0056: /* TA0IC */
|
timer_a.ic = value;
|
timer_a.ic = value;
|
break;
|
break;
|
#endif
|
#endif
|
|
|
case 0x2ea: /* m32c uart1tx */
|
case 0x2ea: /* m32c uart1tx */
|
case 0x3aa: /* m16c uart1tx */
|
case 0x3aa: /* m16c uart1tx */
|
{
|
{
|
static int pending_exit = 0;
|
static int pending_exit = 0;
|
if (value == 0)
|
if (value == 0)
|
{
|
{
|
if (pending_exit)
|
if (pending_exit)
|
{
|
{
|
step_result = M32C_MAKE_EXITED (value);
|
step_result = M32C_MAKE_EXITED (value);
|
return;
|
return;
|
}
|
}
|
pending_exit = 1;
|
pending_exit = 1;
|
}
|
}
|
else
|
else
|
{
|
{
|
write (m32c_console_ofd, &value, 1);
|
write (m32c_console_ofd, &value, 1);
|
}
|
}
|
}
|
}
|
break;
|
break;
|
|
|
case 0x400:
|
case 0x400:
|
m32c_syscall (value);
|
m32c_syscall (value);
|
break;
|
break;
|
|
|
case 0x401:
|
case 0x401:
|
putchar (value);
|
putchar (value);
|
break;
|
break;
|
|
|
case 0x402:
|
case 0x402:
|
printf ("SimTrace: %06lx %02x\n", regs.r_pc, value);
|
printf ("SimTrace: %06lx %02x\n", regs.r_pc, value);
|
break;
|
break;
|
|
|
case 0x403:
|
case 0x403:
|
printf ("SimTrap: %06lx %02x\n", regs.r_pc, value);
|
printf ("SimTrap: %06lx %02x\n", regs.r_pc, value);
|
abort ();
|
abort ();
|
}
|
}
|
}
|
}
|
|
|
void
|
void
|
mem_put_qi (int address, unsigned char value)
|
mem_put_qi (int address, unsigned char value)
|
{
|
{
|
S ("<=");
|
S ("<=");
|
mem_put_byte (address, value & 0xff);
|
mem_put_byte (address, value & 0xff);
|
E ();
|
E ();
|
COUNT (1, 1);
|
COUNT (1, 1);
|
}
|
}
|
|
|
void
|
void
|
mem_put_hi (int address, unsigned short value)
|
mem_put_hi (int address, unsigned short value)
|
{
|
{
|
if (address == 0x402)
|
if (address == 0x402)
|
{
|
{
|
printf ("SimTrace: %06lx %04x\n", regs.r_pc, value);
|
printf ("SimTrace: %06lx %04x\n", regs.r_pc, value);
|
return;
|
return;
|
}
|
}
|
S ("<=");
|
S ("<=");
|
mem_put_byte (address, value & 0xff);
|
mem_put_byte (address, value & 0xff);
|
mem_put_byte (address + 1, value >> 8);
|
mem_put_byte (address + 1, value >> 8);
|
E ();
|
E ();
|
COUNT (1, 2);
|
COUNT (1, 2);
|
}
|
}
|
|
|
void
|
void
|
mem_put_psi (int address, unsigned long value)
|
mem_put_psi (int address, unsigned long value)
|
{
|
{
|
S ("<=");
|
S ("<=");
|
mem_put_byte (address, value & 0xff);
|
mem_put_byte (address, value & 0xff);
|
mem_put_byte (address + 1, (value >> 8) & 0xff);
|
mem_put_byte (address + 1, (value >> 8) & 0xff);
|
mem_put_byte (address + 2, value >> 16);
|
mem_put_byte (address + 2, value >> 16);
|
E ();
|
E ();
|
COUNT (1, 3);
|
COUNT (1, 3);
|
}
|
}
|
|
|
void
|
void
|
mem_put_si (int address, unsigned long value)
|
mem_put_si (int address, unsigned long value)
|
{
|
{
|
S ("<=");
|
S ("<=");
|
mem_put_byte (address, value & 0xff);
|
mem_put_byte (address, value & 0xff);
|
mem_put_byte (address + 1, (value >> 8) & 0xff);
|
mem_put_byte (address + 1, (value >> 8) & 0xff);
|
mem_put_byte (address + 2, (value >> 16) & 0xff);
|
mem_put_byte (address + 2, (value >> 16) & 0xff);
|
mem_put_byte (address + 3, (value >> 24) & 0xff);
|
mem_put_byte (address + 3, (value >> 24) & 0xff);
|
E ();
|
E ();
|
COUNT (1, 4);
|
COUNT (1, 4);
|
}
|
}
|
|
|
void
|
void
|
mem_put_blk (int address, void *bufptr, int nbytes)
|
mem_put_blk (int address, void *bufptr, int nbytes)
|
{
|
{
|
S ("<=");
|
S ("<=");
|
if (enable_counting)
|
if (enable_counting)
|
mem_counters[1][1] += nbytes;
|
mem_counters[1][1] += nbytes;
|
while (nbytes--)
|
while (nbytes--)
|
mem_put_byte (address++, *(unsigned char *) bufptr++);
|
mem_put_byte (address++, *(unsigned char *) bufptr++);
|
E ();
|
E ();
|
}
|
}
|
|
|
unsigned char
|
unsigned char
|
mem_get_pc ()
|
mem_get_pc ()
|
{
|
{
|
unsigned char *m = mem_ptr (regs.r_pc & membus_mask);
|
unsigned char *m = mem_ptr (regs.r_pc & membus_mask);
|
COUNT (0, 0);
|
COUNT (0, 0);
|
return *m;
|
return *m;
|
}
|
}
|
|
|
#ifdef HAVE_TERMIOS_H
|
#ifdef HAVE_TERMIOS_H
|
static int console_raw = 0;
|
static int console_raw = 0;
|
static struct termios oattr;
|
static struct termios oattr;
|
|
|
static int
|
static int
|
stdin_ready ()
|
stdin_ready ()
|
{
|
{
|
fd_set ifd;
|
fd_set ifd;
|
int n;
|
int n;
|
struct timeval t;
|
struct timeval t;
|
|
|
t.tv_sec = 0;
|
t.tv_sec = 0;
|
t.tv_usec = 0;
|
t.tv_usec = 0;
|
FD_ZERO (&ifd);
|
FD_ZERO (&ifd);
|
FD_SET (m32c_console_ifd, &ifd);
|
FD_SET (m32c_console_ifd, &ifd);
|
n = select (1, &ifd, 0, 0, &t);
|
n = select (1, &ifd, 0, 0, &t);
|
return n > 0;
|
return n > 0;
|
}
|
}
|
|
|
void
|
void
|
m32c_sim_restore_console ()
|
m32c_sim_restore_console ()
|
{
|
{
|
if (console_raw)
|
if (console_raw)
|
tcsetattr (m32c_console_ifd, TCSANOW, &oattr);
|
tcsetattr (m32c_console_ifd, TCSANOW, &oattr);
|
console_raw = 0;
|
console_raw = 0;
|
}
|
}
|
#endif
|
#endif
|
|
|
static unsigned char
|
static unsigned char
|
mem_get_byte (int address)
|
mem_get_byte (int address)
|
{
|
{
|
unsigned char *m;
|
unsigned char *m;
|
address &= membus_mask;
|
address &= membus_mask;
|
m = mem_ptr (address);
|
m = mem_ptr (address);
|
switch (address)
|
switch (address)
|
{
|
{
|
#ifdef HAVE_TERMIOS_H
|
#ifdef HAVE_TERMIOS_H
|
case 0x2ed: /* m32c uart1c1 */
|
case 0x2ed: /* m32c uart1c1 */
|
case 0x3ad: /* m16c uart1c1 */
|
case 0x3ad: /* m16c uart1c1 */
|
|
|
if (!console_raw && m32c_use_raw_console)
|
if (!console_raw && m32c_use_raw_console)
|
{
|
{
|
struct termios attr;
|
struct termios attr;
|
tcgetattr (m32c_console_ifd, &attr);
|
tcgetattr (m32c_console_ifd, &attr);
|
tcgetattr (m32c_console_ifd, &oattr);
|
tcgetattr (m32c_console_ifd, &oattr);
|
/* We want each key to be sent as the user presses them. */
|
/* We want each key to be sent as the user presses them. */
|
attr.c_lflag &= ~(ICANON | ECHO | ECHOE);
|
attr.c_lflag &= ~(ICANON | ECHO | ECHOE);
|
tcsetattr (m32c_console_ifd, TCSANOW, &attr);
|
tcsetattr (m32c_console_ifd, TCSANOW, &attr);
|
console_raw = 1;
|
console_raw = 1;
|
atexit (m32c_sim_restore_console);
|
atexit (m32c_sim_restore_console);
|
}
|
}
|
|
|
if (stdin_ready ())
|
if (stdin_ready ())
|
return 0x02; /* tx empty and rx full */
|
return 0x02; /* tx empty and rx full */
|
else
|
else
|
return 0x0a; /* transmitter empty */
|
return 0x0a; /* transmitter empty */
|
|
|
case 0x2ee: /* m32c uart1 rx */
|
case 0x2ee: /* m32c uart1 rx */
|
{
|
{
|
char c;
|
char c;
|
read (m32c_console_ifd, &c, 1);
|
read (m32c_console_ifd, &c, 1);
|
if (m32c_console_ifd == 0 && c == 3) /* Ctrl-C */
|
if (m32c_console_ifd == 0 && c == 3) /* Ctrl-C */
|
{
|
{
|
printf ("Ctrl-C!\n");
|
printf ("Ctrl-C!\n");
|
exit (0);
|
exit (0);
|
}
|
}
|
|
|
if (m32c_console_ifd != 1)
|
if (m32c_console_ifd != 1)
|
{
|
{
|
if (isgraph (c))
|
if (isgraph (c))
|
printf ("\033[31m%c\033[0m", c);
|
printf ("\033[31m%c\033[0m", c);
|
else
|
else
|
printf ("\033[31m%02x\033[0m", c);
|
printf ("\033[31m%02x\033[0m", c);
|
}
|
}
|
return c;
|
return c;
|
}
|
}
|
#endif
|
#endif
|
|
|
#ifdef TIMER_A
|
#ifdef TIMER_A
|
case 0x346: /* TA0low */
|
case 0x346: /* TA0low */
|
return timer_a.count & 0xff;
|
return timer_a.count & 0xff;
|
case 0x347: /* TA0high */
|
case 0x347: /* TA0high */
|
return (timer_a.count >> 8) & 0xff;
|
return (timer_a.count >> 8) & 0xff;
|
case 0x104: /* TRA */
|
case 0x104: /* TRA */
|
return timer_a.count;
|
return timer_a.count;
|
#endif
|
#endif
|
|
|
default:
|
default:
|
/* In case both cases above are not included. */
|
/* In case both cases above are not included. */
|
;
|
;
|
}
|
}
|
|
|
S ("=>");
|
S ("=>");
|
if (trace)
|
if (trace)
|
printf (" %02x", *m);
|
printf (" %02x", *m);
|
E ();
|
E ();
|
return *m;
|
return *m;
|
}
|
}
|
|
|
unsigned char
|
unsigned char
|
mem_get_qi (int address)
|
mem_get_qi (int address)
|
{
|
{
|
unsigned char rv;
|
unsigned char rv;
|
S ("=>");
|
S ("=>");
|
rv = mem_get_byte (address);
|
rv = mem_get_byte (address);
|
COUNT (0, 1);
|
COUNT (0, 1);
|
E ();
|
E ();
|
return rv;
|
return rv;
|
}
|
}
|
|
|
unsigned short
|
unsigned short
|
mem_get_hi (int address)
|
mem_get_hi (int address)
|
{
|
{
|
unsigned short rv;
|
unsigned short rv;
|
S ("=>");
|
S ("=>");
|
rv = mem_get_byte (address);
|
rv = mem_get_byte (address);
|
rv |= mem_get_byte (address + 1) * 256;
|
rv |= mem_get_byte (address + 1) * 256;
|
COUNT (0, 2);
|
COUNT (0, 2);
|
E ();
|
E ();
|
return rv;
|
return rv;
|
}
|
}
|
|
|
unsigned long
|
unsigned long
|
mem_get_psi (int address)
|
mem_get_psi (int address)
|
{
|
{
|
unsigned long rv;
|
unsigned long rv;
|
S ("=>");
|
S ("=>");
|
rv = mem_get_byte (address);
|
rv = mem_get_byte (address);
|
rv |= mem_get_byte (address + 1) * 256;
|
rv |= mem_get_byte (address + 1) * 256;
|
rv |= mem_get_byte (address + 2) * 65536;
|
rv |= mem_get_byte (address + 2) * 65536;
|
COUNT (0, 3);
|
COUNT (0, 3);
|
E ();
|
E ();
|
return rv;
|
return rv;
|
}
|
}
|
|
|
unsigned long
|
unsigned long
|
mem_get_si (int address)
|
mem_get_si (int address)
|
{
|
{
|
unsigned long rv;
|
unsigned long rv;
|
S ("=>");
|
S ("=>");
|
rv = mem_get_byte (address);
|
rv = mem_get_byte (address);
|
rv |= mem_get_byte (address + 1) << 8;
|
rv |= mem_get_byte (address + 1) << 8;
|
rv |= mem_get_byte (address + 2) << 16;
|
rv |= mem_get_byte (address + 2) << 16;
|
rv |= mem_get_byte (address + 3) << 24;
|
rv |= mem_get_byte (address + 3) << 24;
|
COUNT (0, 4);
|
COUNT (0, 4);
|
E ();
|
E ();
|
return rv;
|
return rv;
|
}
|
}
|
|
|
void
|
void
|
mem_get_blk (int address, void *bufptr, int nbytes)
|
mem_get_blk (int address, void *bufptr, int nbytes)
|
{
|
{
|
S ("=>");
|
S ("=>");
|
if (enable_counting)
|
if (enable_counting)
|
mem_counters[0][1] += nbytes;
|
mem_counters[0][1] += nbytes;
|
while (nbytes--)
|
while (nbytes--)
|
*(char *) bufptr++ = mem_get_byte (address++);
|
*(char *) bufptr++ = mem_get_byte (address++);
|
E ();
|
E ();
|
}
|
}
|
|
|
int
|
int
|
sign_ext (int v, int bits)
|
sign_ext (int v, int bits)
|
{
|
{
|
if (bits < 32)
|
if (bits < 32)
|
{
|
{
|
v &= (1 << bits) - 1;
|
v &= (1 << bits) - 1;
|
if (v & (1 << (bits - 1)))
|
if (v & (1 << (bits - 1)))
|
v -= (1 << bits);
|
v -= (1 << bits);
|
}
|
}
|
return v;
|
return v;
|
}
|
}
|
|
|
#if TIMER_A
|
#if TIMER_A
|
void
|
void
|
update_timer_a ()
|
update_timer_a ()
|
{
|
{
|
if (timer_a.bsr & 1)
|
if (timer_a.bsr & 1)
|
{
|
{
|
timer_a.prescale--;
|
timer_a.prescale--;
|
if (timer_a.prescale < 0)
|
if (timer_a.prescale < 0)
|
{
|
{
|
if (A24)
|
if (A24)
|
{
|
{
|
switch (timer_a.mode & 0xc0)
|
switch (timer_a.mode & 0xc0)
|
{
|
{
|
case 0x00:
|
case 0x00:
|
timer_a.prescale = 0;
|
timer_a.prescale = 0;
|
break;
|
break;
|
case 0x40:
|
case 0x40:
|
timer_a.prescale = 8;
|
timer_a.prescale = 8;
|
break;
|
break;
|
case 0x80:
|
case 0x80:
|
timer_a.prescale = timer_a.tcspr & 0x0f;
|
timer_a.prescale = timer_a.tcspr & 0x0f;
|
break;
|
break;
|
case 0xc0:
|
case 0xc0:
|
timer_a.prescale = 32;
|
timer_a.prescale = 32;
|
break;
|
break;
|
}
|
}
|
}
|
}
|
else
|
else
|
{
|
{
|
timer_a.prescale = timer_a.tcspr;
|
timer_a.prescale = timer_a.tcspr;
|
}
|
}
|
timer_a.count--;
|
timer_a.count--;
|
if (timer_a.count < 0)
|
if (timer_a.count < 0)
|
{
|
{
|
timer_a.count = timer_a.reload;
|
timer_a.count = timer_a.reload;
|
if (timer_a.ic & 7)
|
if (timer_a.ic & 7)
|
{
|
{
|
if (A24)
|
if (A24)
|
mem_put_qi (0x6c, timer_a.ic | 0x08);
|
mem_put_qi (0x6c, timer_a.ic | 0x08);
|
else
|
else
|
mem_put_qi (0x56, timer_a.ic | 0x08);
|
mem_put_qi (0x56, timer_a.ic | 0x08);
|
}
|
}
|
}
|
}
|
}
|
}
|
}
|
}
|
|
|
if (regs.r_flags & FLAGBIT_I /* interrupts enabled */
|
if (regs.r_flags & FLAGBIT_I /* interrupts enabled */
|
&& timer_a.ic & 0x08 /* timer A interrupt triggered */
|
&& timer_a.ic & 0x08 /* timer A interrupt triggered */
|
&& (timer_a.ic & 0x07) > ((regs.r_flags >> 12) & 0x07))
|
&& (timer_a.ic & 0x07) > ((regs.r_flags >> 12) & 0x07))
|
{
|
{
|
if (A24)
|
if (A24)
|
trigger_peripheral_interrupt (12, 0x06c);
|
trigger_peripheral_interrupt (12, 0x06c);
|
else
|
else
|
trigger_peripheral_interrupt (22, 0x056);
|
trigger_peripheral_interrupt (22, 0x056);
|
}
|
}
|
}
|
}
|
#endif
|
#endif
|
|
|