o Add modulo argument to sim_core_attach
[binutils-gdb.git] / sim / tic80 / sim-calls.c
1 /* 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 <signal.h> /* FIXME - should be machine dependant version */
24 #include <stdarg.h>
25 #include <ctype.h>
26
27 #include "bfd.h"
28 #include "sim-main.h"
29 #include "sim-utils.h"
30 #include "sim-options.h"
31
32 #ifdef HAVE_STDLIB_H
33 #include <stdlib.h>
34 #endif
35
36 #ifdef HAVE_STRING_H
37 #include <string.h>
38 #else
39 #ifdef HAVE_STRINGS_H
40 #include <strings.h>
41 #endif
42 #endif
43
44
45 #define SIM_ADDR unsigned
46
47 SIM_DESC
48 sim_open (SIM_OPEN_KIND kind,
49 host_callback *callback,
50 struct _bfd *abfd,
51 char **argv)
52 {
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 /* getopt will print the error message so we just have to exit if this fails.
59 FIXME: Hmmm... in the case of gdb we need getopt to call
60 print_filtered. */
61 if (sim_parse_args (sd, argv) != SIM_RC_OK)
62 {
63 /* Uninstall the modules to avoid memory leaks,
64 file descriptor leaks, etc. */
65 sim_module_uninstall (sd);
66 return 0;
67 }
68
69 /* check for/establish the a reference program image */
70 if (sim_analyze_program (sd,
71 (STATE_PROG_ARGV (sd) != NULL
72 ? *STATE_PROG_ARGV (sd)
73 : NULL),
74 abfd) != SIM_RC_OK)
75 {
76 sim_module_uninstall (sd);
77 return 0;
78 }
79
80 /* establish any remaining configuration options */
81 if (sim_config (sd) != SIM_RC_OK)
82 {
83 sim_module_uninstall (sd);
84 return 0;
85 }
86
87 if (sim_post_argv_init (sd) != SIM_RC_OK)
88 {
89 /* Uninstall the modules to avoid memory leaks,
90 file descriptor leaks, etc. */
91 sim_module_uninstall (sd);
92 return 0;
93 }
94
95 #define TIC80_MEM_START 0x2000000
96 #define TIC80_MEM_SIZE 0x100000
97
98 if (!STATE_MEMOPT_P (sd))
99 {
100 char *buf;
101 /* main memory */
102 asprintf (&buf, "memory region 0x%lx,0x%lx",
103 TIC80_MEM_START, TIC80_MEM_SIZE);
104 sim_do_command (sd, buf);
105 free (buf);
106 /* interrupt memory */
107 sim_do_command (sd, "memory region 0x1010000,0x1000");
108 /* some memory at zero */
109 sim_do_command (sd, "memory region 0,0x100000");
110 }
111
112 /* FIXME: for now */
113 return sd;
114 }
115
116
117 void
118 sim_close (SIM_DESC sd, int quitting)
119 {
120 /* Uninstall the modules to avoid memory leaks,
121 file descriptor leaks, etc. */
122 sim_module_uninstall (sd);
123 }
124
125
126 int
127 sim_read (SIM_DESC sd, SIM_ADDR mem, unsigned char *buf, int length)
128 {
129 return sim_core_read_buffer (sd, NULL, sim_core_write_map,
130 buf, mem, length);
131 }
132
133
134 int
135 sim_write (SIM_DESC sd, SIM_ADDR mem, unsigned char *buf, int length)
136 {
137 return sim_core_write_buffer (sd, NULL, sim_core_write_map,
138 buf, mem, length);
139 }
140
141
142 /* FIXME - these magic numbers need to be moved elsewhere */
143
144 #define SP_REGNUM 1 /* Contains address of top of stack */
145 #define FP_REGNUM 31 /* Contains address of executing stack frame */
146 #define PC_REGNUM 32 /* Contains program counter (FIXME?) */
147 #define NPC_REGNUM 33 /* Contains the next program counter (FIXME?) */
148 #define A0_REGNUM 34 /* Accumulator register 0 */
149 #define A3_REGNUM 37 /* Accumulator register 1 */
150
151 #define R0_REGNUM 0 /* General Purpose Register 0 - for sim */
152 #define Rn_REGNUM 31 /* Last General Purpose Register - for sim */
153 #define An_REGNUM A3_REGNUM /* Last Accumulator register - for sim */
154
155 void
156 sim_fetch_register (SIM_DESC sd, int regnr, unsigned char *buf)
157 {
158 if (regnr == R0_REGNUM)
159 memset (buf, 0, sizeof (unsigned32));
160 else if (regnr > R0_REGNUM && regnr <= Rn_REGNUM)
161 *(unsigned32*)buf = H2T_4 (STATE_CPU (sd, 0)->reg[regnr - R0_REGNUM]);
162 else if (regnr == PC_REGNUM)
163 *(unsigned32*)buf = H2T_4 (STATE_CPU (sd, 0)->cia.ip);
164 else if (regnr == NPC_REGNUM)
165 *(unsigned32*)buf = H2T_4 (STATE_CPU (sd, 0)->cia.dp);
166 else if (regnr >= A0_REGNUM && regnr <= An_REGNUM)
167 *(unsigned64*)buf = H2T_8 (STATE_CPU (sd, 0)->acc[regnr - A0_REGNUM]);
168 else
169 sim_io_error (sd, "sim_fetch_register - unknown register nr %d", regnr);
170 return;
171 }
172
173
174 void
175 sim_store_register (SIM_DESC sd, int regnr, unsigned char *buf)
176 {
177 if (regnr >= R0_REGNUM && regnr <= Rn_REGNUM)
178 STATE_CPU (sd, 0)->reg[regnr - R0_REGNUM] = T2H_4 (*(unsigned32*)buf);
179 else if (regnr == PC_REGNUM)
180 STATE_CPU (sd, 0)->cia.ip = T2H_4 (*(unsigned32*)buf);
181 else if (regnr == NPC_REGNUM)
182 STATE_CPU (sd, 0)->cia.dp = T2H_4 (*(unsigned32*)buf);
183 else if (regnr >= A0_REGNUM && regnr <= An_REGNUM)
184 STATE_CPU (sd, 0)->acc[regnr - A0_REGNUM] = T2H_8 (*(unsigned64*)buf);
185 else
186 sim_io_error (sd, "sim_store_register - unknown register nr %d", regnr);
187 return;
188 }
189
190
191 void
192 sim_info (SIM_DESC sd, int verbose)
193 {
194 }
195
196
197 SIM_RC
198 sim_create_inferior (SIM_DESC sd,
199 struct _bfd *abfd,
200 char **argv,
201 char **envp)
202 {
203 /* clear all registers */
204 memset (&STATE_CPU (sd, 0)->reg, 0, sizeof (STATE_CPU (sd, 0)->reg));
205 memset (&STATE_CPU (sd, 0)->acc, 0, sizeof (STATE_CPU (sd, 0)->acc));
206 memset (&STATE_CPU (sd, 0)->cr, 0, sizeof (STATE_CPU (sd, 0)->cr));
207 STATE_CPU (sd, 0)->is_user_mode = 0;
208 memset (&STATE_CPU (sd, 0)->cia, 0, sizeof (STATE_CPU (sd, 0)->cia));
209 /* initialize any modules */
210 sim_module_init (sd);
211 /* set the stack-pointer/program counter */
212 if (abfd != NULL)
213 STATE_CPU (sd, 0)->cia.ip = bfd_get_start_address (abfd);
214 else
215 STATE_CPU (sd, 0)->cia.ip = 0;
216 STATE_CPU (sd, 0)->cia.dp = (STATE_CPU (sd, 0)->cia.ip
217 + sizeof (instruction_word));
218 STATE_CPU (sd, 0)->cr[IE_CR] |= IE_CR_IE;
219 STATE_CPU (sd, 0)->reg[1] = TIC80_MEM_START + TIC80_MEM_SIZE - 16;
220 return SIM_RC_OK;
221 }
222
223
224 void
225 sim_do_command (SIM_DESC sd, char *cmd)
226 {
227 if (sim_args_command (sd, cmd) != SIM_RC_OK)
228 sim_io_eprintf (sd, "Unknown command `%s'\n", cmd);
229 }