1 |
578 |
markom |
/* This file is part of the program psim.
|
2 |
|
|
|
3 |
|
|
Copyright (C) 1994-1996, Andrew Cagney <cagney@highland.com.au>
|
4 |
|
|
Copyright (C) 1997, Free Software Foundation
|
5 |
|
|
|
6 |
|
|
This program is free software; you can redistribute it and/or modify
|
7 |
|
|
it under the terms of the GNU General Public License as published by
|
8 |
|
|
the Free Software Foundation; either version 2 of the License, or
|
9 |
|
|
(at your option) any later version.
|
10 |
|
|
|
11 |
|
|
This program is distributed in the hope that it will be useful,
|
12 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
|
|
GNU General Public License for more details.
|
15 |
|
|
|
16 |
|
|
You should have received a copy of the GNU General Public License
|
17 |
|
|
along with this program; if not, write to the Free Software
|
18 |
|
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
19 |
|
|
|
20 |
|
|
*/
|
21 |
|
|
|
22 |
|
|
|
23 |
|
|
#include <stdarg.h>
|
24 |
|
|
#include <ctype.h>
|
25 |
|
|
|
26 |
|
|
#include "bfd.h"
|
27 |
|
|
#include "sim-main.h"
|
28 |
|
|
#include "sim-utils.h"
|
29 |
|
|
#include "sim-options.h"
|
30 |
|
|
|
31 |
|
|
#ifdef HAVE_STDLIB_H
|
32 |
|
|
#include <stdlib.h>
|
33 |
|
|
#endif
|
34 |
|
|
|
35 |
|
|
#ifdef HAVE_STRING_H
|
36 |
|
|
#include <string.h>
|
37 |
|
|
#else
|
38 |
|
|
#ifdef HAVE_STRINGS_H
|
39 |
|
|
#include <strings.h>
|
40 |
|
|
#endif
|
41 |
|
|
#endif
|
42 |
|
|
|
43 |
|
|
|
44 |
|
|
#define SIM_ADDR unsigned
|
45 |
|
|
|
46 |
|
|
SIM_DESC
|
47 |
|
|
sim_open (SIM_OPEN_KIND kind,
|
48 |
|
|
host_callback *callback,
|
49 |
|
|
struct _bfd *abfd,
|
50 |
|
|
char **argv)
|
51 |
|
|
{
|
52 |
|
|
char *buf;
|
53 |
|
|
SIM_DESC sd = sim_state_alloc (kind, callback);
|
54 |
|
|
|
55 |
|
|
if (sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK)
|
56 |
|
|
return 0;
|
57 |
|
|
|
58 |
|
|
#define TIC80_MEM_START 0x2000000
|
59 |
|
|
#define TIC80_MEM_SIZE 0x100000
|
60 |
|
|
|
61 |
|
|
/* main memory */
|
62 |
|
|
asprintf (&buf, "memory region 0x%lx,0x%lx",
|
63 |
|
|
TIC80_MEM_START, TIC80_MEM_SIZE);
|
64 |
|
|
sim_do_command (sd, buf);
|
65 |
|
|
free (buf);
|
66 |
|
|
/* interrupt memory */
|
67 |
|
|
sim_do_command (sd, "memory region 0x1010000,0x1000");
|
68 |
|
|
/* some memory at zero */
|
69 |
|
|
sim_do_command (sd, "memory region 0,0x100000");
|
70 |
|
|
|
71 |
|
|
/* getopt will print the error message so we just have to exit if this fails.
|
72 |
|
|
FIXME: Hmmm... in the case of gdb we need getopt to call
|
73 |
|
|
print_filtered. */
|
74 |
|
|
if (sim_parse_args (sd, argv) != SIM_RC_OK)
|
75 |
|
|
{
|
76 |
|
|
/* Uninstall the modules to avoid memory leaks,
|
77 |
|
|
file descriptor leaks, etc. */
|
78 |
|
|
sim_module_uninstall (sd);
|
79 |
|
|
return 0;
|
80 |
|
|
}
|
81 |
|
|
|
82 |
|
|
/* check for/establish the a reference program image */
|
83 |
|
|
if (sim_analyze_program (sd,
|
84 |
|
|
(STATE_PROG_ARGV (sd) != NULL
|
85 |
|
|
? *STATE_PROG_ARGV (sd)
|
86 |
|
|
: NULL),
|
87 |
|
|
abfd) != SIM_RC_OK)
|
88 |
|
|
{
|
89 |
|
|
sim_module_uninstall (sd);
|
90 |
|
|
return 0;
|
91 |
|
|
}
|
92 |
|
|
|
93 |
|
|
/* establish any remaining configuration options */
|
94 |
|
|
if (sim_config (sd) != SIM_RC_OK)
|
95 |
|
|
{
|
96 |
|
|
sim_module_uninstall (sd);
|
97 |
|
|
return 0;
|
98 |
|
|
}
|
99 |
|
|
|
100 |
|
|
if (sim_post_argv_init (sd) != SIM_RC_OK)
|
101 |
|
|
{
|
102 |
|
|
/* Uninstall the modules to avoid memory leaks,
|
103 |
|
|
file descriptor leaks, etc. */
|
104 |
|
|
sim_module_uninstall (sd);
|
105 |
|
|
return 0;
|
106 |
|
|
}
|
107 |
|
|
|
108 |
|
|
/* FIXME: for now */
|
109 |
|
|
return sd;
|
110 |
|
|
}
|
111 |
|
|
|
112 |
|
|
|
113 |
|
|
void
|
114 |
|
|
sim_close (SIM_DESC sd, int quitting)
|
115 |
|
|
{
|
116 |
|
|
/* Uninstall the modules to avoid memory leaks,
|
117 |
|
|
file descriptor leaks, etc. */
|
118 |
|
|
sim_module_uninstall (sd);
|
119 |
|
|
}
|
120 |
|
|
|
121 |
|
|
|
122 |
|
|
/* FIXME - these magic numbers need to be moved elsewhere */
|
123 |
|
|
|
124 |
|
|
#define SP_REGNUM 1 /* Contains address of top of stack */
|
125 |
|
|
#define FP_REGNUM 31 /* Contains address of executing stack frame */
|
126 |
|
|
#define PC_REGNUM 32 /* Contains program counter (FIXME?) */
|
127 |
|
|
#define NPC_REGNUM 33 /* Contains the next program counter (FIXME?) */
|
128 |
|
|
#define A0_REGNUM 34 /* Accumulator register 0 */
|
129 |
|
|
#define A3_REGNUM 37 /* Accumulator register 1 */
|
130 |
|
|
|
131 |
|
|
#define R0_REGNUM 0 /* General Purpose Register 0 - for sim */
|
132 |
|
|
#define Rn_REGNUM 31 /* Last General Purpose Register - for sim */
|
133 |
|
|
#define An_REGNUM A3_REGNUM /* Last Accumulator register - for sim */
|
134 |
|
|
|
135 |
|
|
int
|
136 |
|
|
sim_fetch_register (SIM_DESC sd, int regnr, unsigned char *buf, int length)
|
137 |
|
|
{
|
138 |
|
|
if (regnr == R0_REGNUM)
|
139 |
|
|
memset (buf, 0, sizeof (unsigned32));
|
140 |
|
|
else if (regnr > R0_REGNUM && regnr <= Rn_REGNUM)
|
141 |
|
|
*(unsigned32*)buf = H2T_4 (STATE_CPU (sd, 0)->reg[regnr - R0_REGNUM]);
|
142 |
|
|
else if (regnr == PC_REGNUM)
|
143 |
|
|
*(unsigned32*)buf = H2T_4 (STATE_CPU (sd, 0)->cia.ip);
|
144 |
|
|
else if (regnr == NPC_REGNUM)
|
145 |
|
|
*(unsigned32*)buf = H2T_4 (STATE_CPU (sd, 0)->cia.dp);
|
146 |
|
|
else if (regnr >= A0_REGNUM && regnr <= An_REGNUM)
|
147 |
|
|
*(unsigned64*)buf = H2T_8 (STATE_CPU (sd, 0)->acc[regnr - A0_REGNUM]);
|
148 |
|
|
else
|
149 |
|
|
sim_io_error (sd, "sim_fetch_register - unknown register nr %d", regnr);
|
150 |
|
|
return -1;
|
151 |
|
|
}
|
152 |
|
|
|
153 |
|
|
|
154 |
|
|
int
|
155 |
|
|
sim_store_register (SIM_DESC sd, int regnr, unsigned char *buf, int length)
|
156 |
|
|
{
|
157 |
|
|
if (regnr >= R0_REGNUM && regnr <= Rn_REGNUM)
|
158 |
|
|
STATE_CPU (sd, 0)->reg[regnr - R0_REGNUM] = T2H_4 (*(unsigned32*)buf);
|
159 |
|
|
else if (regnr == PC_REGNUM)
|
160 |
|
|
STATE_CPU (sd, 0)->cia.ip = T2H_4 (*(unsigned32*)buf);
|
161 |
|
|
else if (regnr == NPC_REGNUM)
|
162 |
|
|
STATE_CPU (sd, 0)->cia.dp = T2H_4 (*(unsigned32*)buf);
|
163 |
|
|
else if (regnr >= A0_REGNUM && regnr <= An_REGNUM)
|
164 |
|
|
STATE_CPU (sd, 0)->acc[regnr - A0_REGNUM] = T2H_8 (*(unsigned64*)buf);
|
165 |
|
|
else
|
166 |
|
|
sim_io_error (sd, "sim_store_register - unknown register nr %d", regnr);
|
167 |
|
|
return -1;
|
168 |
|
|
}
|
169 |
|
|
|
170 |
|
|
|
171 |
|
|
SIM_RC
|
172 |
|
|
sim_create_inferior (SIM_DESC sd,
|
173 |
|
|
struct _bfd *abfd,
|
174 |
|
|
char **argv,
|
175 |
|
|
char **envp)
|
176 |
|
|
{
|
177 |
|
|
/* clear all registers */
|
178 |
|
|
memset (&STATE_CPU (sd, 0)->reg, 0, sizeof (STATE_CPU (sd, 0)->reg));
|
179 |
|
|
memset (&STATE_CPU (sd, 0)->acc, 0, sizeof (STATE_CPU (sd, 0)->acc));
|
180 |
|
|
memset (&STATE_CPU (sd, 0)->cr, 0, sizeof (STATE_CPU (sd, 0)->cr));
|
181 |
|
|
STATE_CPU (sd, 0)->is_user_mode = 0;
|
182 |
|
|
memset (&STATE_CPU (sd, 0)->cia, 0, sizeof (STATE_CPU (sd, 0)->cia));
|
183 |
|
|
/* initialize any modules */
|
184 |
|
|
sim_module_init (sd);
|
185 |
|
|
/* set the stack-pointer/program counter */
|
186 |
|
|
if (abfd != NULL)
|
187 |
|
|
STATE_CPU (sd, 0)->cia.ip = bfd_get_start_address (abfd);
|
188 |
|
|
else
|
189 |
|
|
STATE_CPU (sd, 0)->cia.ip = 0;
|
190 |
|
|
STATE_CPU (sd, 0)->cia.dp = (STATE_CPU (sd, 0)->cia.ip
|
191 |
|
|
+ sizeof (instruction_word));
|
192 |
|
|
STATE_CPU (sd, 0)->cr[IE_CR] |= IE_CR_IE;
|
193 |
|
|
STATE_CPU (sd, 0)->reg[1] = TIC80_MEM_START + TIC80_MEM_SIZE - 16;
|
194 |
|
|
return SIM_RC_OK;
|
195 |
|
|
}
|
196 |
|
|
|
197 |
|
|
|
198 |
|
|
void
|
199 |
|
|
sim_do_command (SIM_DESC sd, char *cmd)
|
200 |
|
|
{
|
201 |
|
|
if (sim_args_command (sd, cmd) != SIM_RC_OK)
|
202 |
|
|
sim_io_eprintf (sd, "Unknown command `%s'\n", cmd);
|
203 |
|
|
}
|