* interp.c (hash): Fix.
[binutils-gdb.git] / sim / v850 / interp.c
1 #include <signal.h>
2 #include "sysdep.h"
3 #include "bfd.h"
4 #include "remote-sim.h"
5 #include "callback.h"
6
7 #include "v850_sim.h"
8
9 #define MEM_SIZE 18 /* V850 memory size is 18 bits XXX */
10
11 uint16 OP[4];
12
13 static struct hash_entry *lookup_hash PARAMS ((uint32 ins));
14
15 #define MAX_HASH 63
16 struct hash_entry
17 {
18 struct hash_entry *next;
19 long opcode;
20 long mask;
21 struct simops *ops;
22 };
23
24 struct hash_entry hash_table[MAX_HASH+1];
25
26 static long
27 hash(insn)
28 long insn;
29 {
30 if ((insn & 0x0600) == 0
31 || (insn & 0x0700) == 0x0200)
32 return (insn & 0x07e0) >> 5;
33 if ((insn & 0x0700) == 0x0300
34 || (insn & 0x0700) == 0x0400
35 || (insn & 0x0700) == 0x0500)
36 return (insn & 0x0780) >> 7;
37 if ((insn & 0x0700) == 0x0600)
38 return (insn & 0x07e0) >> 5;
39 if ((insn & 0x0780) == 0x0700)
40 return (insn & 0x07e0) >> 5;
41 if ((insn & 0x07c0) == 0x0780)
42 return (insn & 0x07c0) >> 6;
43 if ((insn & 0x07E0) == 0x07C0)
44 return (insn & 0x07e0) >> 5;
45 return (insn & 0x07e0) >> 5;
46 }
47
48 static struct hash_entry *
49 lookup_hash (ins)
50 uint32 ins;
51 {
52 struct hash_entry *h;
53
54 h = &hash_table[hash(ins)];
55
56 while ( (ins & h->mask) != h->opcode)
57 {
58 if (h->next == NULL)
59 {
60 printf ("ERROR looking up hash for %x\n",ins);
61 exit(1);
62 }
63 h = h->next;
64 }
65 return (h);
66 }
67
68 uint8
69 get_byte (x)
70 uint8 *x;
71 {
72 return *x;
73 }
74
75 uint16
76 get_half (x)
77 uint8 *x;
78 {
79 uint8 *a = x;
80 return (a[1] << 8) + (a[0]);
81 }
82
83 uint32
84 get_word (x)
85 uint8 *x;
86 {
87 uint8 *a = x;
88 return (a[3]<<24) + (a[2]<<16) + (a[1]<<8) + (a[0]);
89 }
90
91 void
92 put_byte (addr, data)
93 uint8 *addr;
94 uint8 data;
95 {
96 uint8 *a = addr;
97 a[0] = data;
98 }
99
100 void
101 put_half (addr, data)
102 uint8 *addr;
103 uint16 data;
104 {
105 uint8 *a = addr;
106 a[0] = data & 0xff;
107 a[1] = (data >> 8) & 0xff;
108 }
109
110 void
111 put_word (addr, data)
112 uint8 *addr;
113 uint32 data;
114 {
115 uint8 *a = addr;
116 a[0] = data & 0xff;
117 a[1] = (data >> 8) & 0xff;
118 a[2] = (data >> 16) & 0xff;
119 a[3] = (data >> 24) & 0xff;
120 }
121
122 static void
123 do_format_1_2 (insn)
124 uint32 insn;
125 {
126 struct hash_entry *h;
127 printf("format 1 or 2 0x%x\n", insn);
128
129 h = lookup_hash (insn);
130 OP[0] = insn & 0x1f;
131 OP[1] = (insn >> 11) & 0x1f;
132 (h->ops->func) ();
133 }
134
135 static void
136 do_format_3 (insn)
137 uint32 insn;
138 {
139 struct hash_entry *h;
140 printf("format 3 0x%x\n", insn);
141
142 h = lookup_hash (insn);
143 OP[0] = (((insn & 0x70) >> 4) | ((insn & 0xf800) >> 8)) << 1;
144 (h->ops->func) ();
145 }
146
147 static void
148 do_format_4 (insn)
149 uint32 insn;
150 {
151 struct hash_entry *h;
152 printf("format 4 0x%x\n", insn);
153
154 h = lookup_hash (insn);
155 OP[0] = (insn >> 11) & 0x1f;
156 OP[1] = (insn & 0x7f);
157 (h->ops->func) ();
158 }
159
160 static void
161 do_format_5 (insn)
162 uint32 insn;
163 {
164 struct hash_entry *h;
165 printf("format 5 0x%x\n", insn);
166
167 h = lookup_hash (insn);
168 OP[0] = ((insn & 0x3f) | (((insn >> 17) & 0x7fff) << 6)) << 1;
169 OP[1] = (insn >> 11) & 0x1f;
170 (h->ops->func) ();
171 }
172
173 static void
174 do_format_6 (insn)
175 uint32 insn;
176 {
177 struct hash_entry *h;
178 printf("format 6 0x%x\n", insn);
179
180 h = lookup_hash (insn);
181 OP[0] = (insn >> 16) & 0xffff;
182 OP[1] = insn & 0x1f;
183 OP[2] = (insn >> 11) & 0x1f;
184 (h->ops->func) ();
185 }
186
187 static void
188 do_format_7 (insn)
189 uint32 insn;
190 {
191 struct hash_entry *h;
192 printf("format 7 0x%x\n", insn);
193
194 h = lookup_hash (insn);
195 OP[0] = insn & 0x1f;
196 OP[1] = (insn >> 11) & 0x1f;
197 OP[2] = (insn >> 16) & 0xffff;
198 (h->ops->func) ();
199 }
200
201 static void
202 do_format_8 (insn)
203 uint32 insn;
204 {
205 struct hash_entry *h;
206 printf("format 8 0x%x\n", insn);
207
208 h = lookup_hash (insn);
209 OP[0] = insn & 0x1f;
210 OP[1] = (insn >> 11) & 0x7;
211 OP[2] = (insn >> 16) & 0xffff;
212 (h->ops->func) ();
213 }
214
215 static void
216 do_formats_9_10 (insn)
217 uint32 insn;
218 {
219 struct hash_entry *h;
220 printf("formats 9 and 10 0x%x\n", insn);
221
222 h = lookup_hash (insn);
223 OP[0] = insn & 0x1f;
224 OP[1] = (insn >> 11) & 0x1f;
225 (h->ops->func) ();
226 }
227
228 void
229 sim_size (power)
230 int power;
231
232 {
233 if (State.mem)
234 {
235 free (State.mem);
236 }
237
238 State.mem = (uint8 *)calloc(1,1<<MEM_SIZE);
239 if (!State.mem)
240 {
241 fprintf (stderr,"Memory allocation failed.\n");
242 exit(1);
243 }
244 printf ("Allocated %d bytes memory and\n",1<<MEM_SIZE);
245 }
246
247 static void
248 init_system ()
249 {
250 if (!State.mem)
251 sim_size(1);
252 }
253
254 int
255 sim_write (addr, buffer, size)
256 SIM_ADDR addr;
257 unsigned char *buffer;
258 int size;
259 {
260 int i;
261 init_system ();
262
263 /* printf ("sim_write %d bytes to 0x%x\n",size,addr); */
264 for (i = 0; i < size; i++)
265 {
266 State.mem[i+addr] = buffer[i];
267 }
268 return size;
269 }
270
271 void
272 sim_open (args)
273 char *args;
274 {
275 struct simops *s;
276 struct hash_entry *h, *prev;
277 if (args != NULL)
278 printf ("sim_open %s\n",args);
279
280 /* put all the opcodes in the hash table */
281 for (s = Simops; s->func; s++)
282 {
283 h = &hash_table[hash(s->opcode)];
284
285 /* go to the last entry in the chain */
286 while (h->next)
287 h = h->next;
288
289 if (h->ops)
290 {
291 h->next = calloc(1,sizeof(struct hash_entry));
292 h = h->next;
293 }
294 h->ops = s;
295 h->mask = s->mask;
296 h->opcode = s->opcode;
297 }
298 }
299
300
301 void
302 sim_close (quitting)
303 int quitting;
304 {
305 /* nothing to do */
306 }
307
308 void
309 sim_set_profile (n)
310 int n;
311 {
312 printf ("sim_set_profile %d\n",n);
313 }
314
315 void
316 sim_set_profile_size (n)
317 int n;
318 {
319 printf ("sim_set_profile_size %d\n",n);
320 }
321
322 void
323 sim_resume (step, siggnal)
324 int step, siggnal;
325 {
326 uint32 inst, opcode;
327 int i;
328 reg_t oldpc;
329
330 /* printf ("sim_resume (%d,%d) PC=0x%x\n",step,siggnal,PC); */
331
332 if (step)
333 State.exception = SIGTRAP;
334 else
335 State.exception = 0;
336
337 do
338 {
339 inst = RLW (PC);
340 oldpc = PC;
341 opcode = (inst & 0x07e0) >> 5;
342 if ((opcode & 0x30) == 0
343 || (opcode & 0x38) == 0x10)
344 {
345 do_format_1_2 (inst & 0xffff);
346 PC += 2;
347 }
348 else if ((opcode & 0x3C) == 0x18
349 || (opcode & 0x3C) == 0x1C
350 || (opcode & 0x3C) == 0x20
351 || (opcode & 0x3C) == 0x24
352 || (opcode & 0x3C) == 0x28)
353 {
354 do_format_4 (inst & 0xffff);
355 PC += 2;
356 }
357 else if ((opcode & 0x3C) == 0x23)
358 {
359 do_format_3 (inst & 0xffff);
360 /* No PC update, it's done in the instruction. */
361 }
362 else if ((opcode & 0x38) == 0x30)
363 {
364 do_format_6 (inst);
365 PC += 4;
366 }
367 else if ((opcode & 0x3C) == 0x38)
368 {
369 do_format_7 (inst);
370 PC += 4;
371 }
372 else if ((opcode & 0x3E) == 0x3C)
373 {
374 do_format_5 (inst);
375 /* No PC update, it's done in the instruction. */
376 }
377 else if ((opcode & 0x3F) == 0x3E)
378 {
379 do_format_8 (inst);
380 PC += 4;
381 }
382 else
383 {
384 do_formats_9_10 (inst);
385 PC += 4;
386 }
387 }
388 while (!State.exception);
389 }
390
391 int
392 sim_trace ()
393 {
394 printf ("sim_trace\n");
395 return 0;
396 }
397
398 void
399 sim_info (verbose)
400 int verbose;
401 {
402 printf ("sim_info\n");
403 }
404
405 void
406 sim_create_inferior (start_address, argv, env)
407 SIM_ADDR start_address;
408 char **argv;
409 char **env;
410 {
411 printf ("sim_create_inferior: PC=0x%x\n",start_address);
412 PC = start_address;
413 }
414
415
416 void
417 sim_kill ()
418 {
419 /* nothing to do */
420 }
421
422 void
423 sim_set_callbacks(p)
424 host_callback *p;
425 {
426 printf ("sim_set_callbacks\n");
427 /* callback = p; */
428 }
429
430 void
431 sim_stop_reason (reason, sigrc)
432 enum sim_stop *reason;
433 int *sigrc;
434 {
435 /* printf ("sim_stop_reason: PC=0x%x\n",PC); */
436
437 if (State.exception == SIGQUIT)
438 {
439 *reason = sim_exited;
440 *sigrc = State.exception;
441 }
442 else
443 {
444 *reason = sim_stopped;
445 *sigrc = State.exception;
446 }
447 }
448
449 void
450 sim_fetch_register (rn, memory)
451 int rn;
452 unsigned char *memory;
453 {
454 *(uint32 *)memory = State.regs[rn];
455 /* printf ("sim_fetch_register %d 0x%x\n",rn,State.regs[rn]); */
456 }
457
458 void
459 sim_store_register (rn, memory)
460 int rn;
461 unsigned char *memory;
462 {
463 State.regs[rn]= *(uint32 *)memory;
464 /* printf ("store: r%d=0x%x\n",rn,State.regs[rn]); */
465 }
466
467 sim_read (addr, buffer, size)
468 SIM_ADDR addr;
469 unsigned char *buffer;
470 int size;
471 {
472 int i;
473 for (i = 0; i < size; i++)
474 {
475 buffer[i] = State.mem[addr + i];
476 }
477 return size;
478 }
479
480 void
481 sim_do_command (cmd)
482 char *cmd;
483 {
484 printf("sim_do_command: %s\n",cmd);
485 }
486
487 int
488 sim_load (prog, from_tty)
489 char *prog;
490 int from_tty;
491 {
492 /* Return nonzero so GDB will handle it. */
493 return 1;
494 }