1 /* gdb->simulator interface.
2    Copyright (C) 1992, 1993, 1994, 1997 Free Software Foundation, Inc.
3 
4 This file is part of Z8KSIM
5 
6 Z8KSIM 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, or (at your option)
9 any later version.
10 
11 Z8KSIM 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 Z8KZIM; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19 
20 #include "ansidecl.h"
21 #include "sim.h"
22 #include "tm.h"
23 #include "signal.h"
24 #include "bfd.h"
25 #include "gdb/callback.h"
26 #include "gdb/remote-sim.h"
27 
28 #ifndef NULL
29 #define NULL 0
30 #endif
31 
32 host_callback *z8k_callback;
33 
34 static SIM_OPEN_KIND sim_kind;
35 static char *myname;
36 
37 void
sim_size(n)38 sim_size (n)
39      int n;
40 {
41   /* Size is fixed.  */
42 }
43 
44 int
sim_store_register(sd,regno,value,length)45 sim_store_register (sd, regno, value, length)
46      SIM_DESC sd;
47      int regno;
48      unsigned char *value;
49      int length;
50 {
51   /* FIXME: Review the computation of regval.  */
52   int regval = (value[0] << 24) | (value[1] << 16) | (value[2] << 8) | value[3];
53 
54   tm_store_register (regno, regval);
55   return -1;
56 }
57 
58 int
sim_fetch_register(sd,regno,buf,length)59 sim_fetch_register (sd, regno, buf, length)
60      SIM_DESC sd;
61      int regno;
62      unsigned char *buf;
63      int length;
64 {
65   tm_fetch_register (regno, buf);
66   return -1;
67 }
68 
69 int
sim_write(sd,where,what,howmuch)70 sim_write (sd, where, what, howmuch)
71      SIM_DESC sd;
72      SIM_ADDR where;
73      unsigned char *what;
74      int howmuch;
75 {
76   int i;
77 
78   for (i = 0; i < howmuch; i++)
79     tm_write_byte (where + i, what[i]);
80   return howmuch;
81 }
82 
83 int
sim_read(sd,where,what,howmuch)84 sim_read (sd, where, what, howmuch)
85      SIM_DESC sd;
86      SIM_ADDR where;
87      unsigned char *what;
88      int howmuch;
89 {
90   int i;
91 
92   for (i = 0; i < howmuch; i++)
93     what[i] = tm_read_byte (where + i);
94   return howmuch;
95 }
96 
97 static void
control_c(sig,code,scp,addr)98 control_c (sig, code, scp, addr)
99      int sig;
100      int code;
101      char *scp;
102      char *addr;
103 {
104   tm_exception (SIM_INTERRUPT);
105 }
106 
107 int
sim_stop(sd)108 sim_stop (sd)
109      SIM_DESC sd;
110 {
111   tm_exception (SIM_INTERRUPT);
112   return 1;
113 }
114 
115 void
sim_resume(sd,step,sig)116 sim_resume (sd, step, sig)
117      SIM_DESC sd;
118      int step;
119      int sig;
120 {
121   void (*prev) ();
122 
123   prev = signal (SIGINT, control_c);
124   tm_resume (step);
125   signal (SIGINT, prev);
126 }
127 
128 void
sim_stop_reason(sd,reason,sigrc)129 sim_stop_reason (sd, reason, sigrc)
130      SIM_DESC sd;
131      enum sim_stop *reason;
132      int *sigrc;
133 {
134   switch (tm_signal ())
135     {
136     case SIM_DIV_ZERO:
137       *sigrc = SIGFPE;
138       break;
139     case SIM_INTERRUPT:
140       *sigrc = SIGINT;
141       break;
142     case SIM_BAD_INST:
143       *sigrc = SIGILL;
144       break;
145     case SIM_BREAKPOINT:
146       *sigrc = SIGTRAP;
147       break;
148     case SIM_SINGLE_STEP:
149       *sigrc = SIGTRAP;
150       break;
151     case SIM_BAD_SYSCALL:
152       *sigrc = SIGILL;
153       break;
154     case SIM_BAD_ALIGN:
155       *sigrc = SIGSEGV;
156       break;
157     case SIM_DONE:
158       {
159 	sim_state_type x;
160 	tm_state (&x);
161 	*sigrc = x.regs[2].word & 255;
162 	*reason = sim_exited;
163 	return;
164       }
165     default:
166       abort ();
167     }
168   *reason = sim_stopped;
169 }
170 
171 void
sim_info(sd,verbose)172 sim_info (sd, verbose)
173      SIM_DESC sd;
174      int verbose;
175 {
176   sim_state_type x;
177 
178   tm_state (&x);
179   tm_info_print (&x);
180 }
181 
182 SIM_DESC
sim_open(kind,cb,abfd,argv)183 sim_open (kind, cb, abfd, argv)
184      SIM_OPEN_KIND kind;
185      host_callback *cb;
186      struct bfd *abfd;
187      char **argv;
188 {
189   /* FIXME: The code in sim_load that determines the exact z8k arch
190      should be moved to here */
191 
192   sim_kind = kind;
193   myname = argv[0];
194   z8k_callback = cb;
195 
196   /* fudge our descriptor for now */
197   return (SIM_DESC) 1;
198 }
199 
200 void
sim_close(sd,quitting)201 sim_close (sd, quitting)
202      SIM_DESC sd;
203      int quitting;
204 {
205   /* nothing to do */
206 }
207 
208 SIM_RC
sim_load(sd,prog,abfd,from_tty)209 sim_load (sd, prog, abfd, from_tty)
210      SIM_DESC sd;
211      char *prog;
212      bfd *abfd;
213      int from_tty;
214 {
215   extern bfd *sim_load_file (); /* ??? Don't know where this should live.  */
216   bfd *prog_bfd;
217 
218   /* FIXME: The code determining the type of z9k processor should be
219      moved from here to sim_open. */
220 
221   prog_bfd = sim_load_file (sd, myname, z8k_callback, prog, abfd,
222 			    sim_kind == SIM_OPEN_DEBUG,
223 			    0, sim_write);
224   if (prog_bfd == NULL)
225     return SIM_RC_FAIL;
226   if (bfd_get_mach (prog_bfd) == bfd_mach_z8001)
227     {
228       extern int sim_z8001_mode;
229       sim_z8001_mode = 1;
230     }
231   /* Close the bfd if we opened it.  */
232   if (abfd == NULL)
233     bfd_close (prog_bfd);
234   return SIM_RC_OK;
235 }
236 
237 SIM_RC
sim_create_inferior(sd,abfd,argv,env)238 sim_create_inferior (sd, abfd, argv, env)
239      SIM_DESC sd;
240      struct bfd *abfd;
241      char **argv;
242      char **env;
243 {
244   if (abfd != NULL)
245     tm_store_register (REG_PC, bfd_get_start_address (abfd));
246   else
247     tm_store_register (REG_PC, 0);
248   return SIM_RC_OK;
249 }
250 
251 void
sim_do_command(sd,cmd)252 sim_do_command (sd, cmd)
253      SIM_DESC sd;
254      char *cmd;
255 {
256 }
257 
258 void
sim_set_callbacks(ptr)259 sim_set_callbacks (ptr)
260      host_callback *ptr;
261 {
262   z8k_callback = ptr;
263 }
264