* gch1272.{ch,exp}, gch1280.{ch,exp}, pr-9946.{ch,exp}:
[deliverable/binutils-gdb.git] / sim / d10v / interp.c
CommitLineData
d70b4d42 1#include <signal.h>
2934d1c9
MH
2#include "sysdep.h"
3#include "bfd.h"
4#include "remote-sim.h"
5#include "callback.h"
6
7#include "d10v_sim.h"
8
9#define IMEM_SIZE 18 /* D10V instruction memory size is 18 bits */
10#define DMEM_SIZE 16 /* Data memory */
11
12uint16 OP[4];
13
14static struct hash_entry *lookup_hash PARAMS ((uint32 ins, int size));
15
16#define MAX_HASH 63
17struct hash_entry
18{
19 struct hash_entry *next;
20 long opcode;
21 long mask;
22 struct simops *ops;
23};
24
25struct hash_entry hash_table[MAX_HASH+1];
26
27static long
28hash(insn, format)
29 long insn;
30 int format;
31{
32 if (format & LONG_OPCODE)
33 return ((insn & 0x3F000000) >> 24);
34 else
35 return((insn & 0x7E00) >> 9);
36}
37
38static struct hash_entry *
39lookup_hash (ins, size)
40 uint32 ins;
41 int size;
42{
43 struct hash_entry *h;
44
45 if (size)
46 h = &hash_table[(ins & 0x3F000000) >> 24];
47 else
48 h = &hash_table[(ins & 0x7E00) >> 9];
49
50 while ( (ins & h->mask) != h->opcode)
51 {
52 if (h->next == NULL)
53 {
54 printf ("ERROR looking up hash for %x\n",ins);
55 exit(1);
56 }
57 h = h->next;
58 }
59 return (h);
60}
61
62uint32
d70b4d42
MH
63get_longword (x)
64 uint8 *x;
2934d1c9 65{
d70b4d42 66 uint8 *a = x;
2934d1c9
MH
67 return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + (a[3]);
68}
69
d70b4d42
MH
70int64
71get_longlong (x)
72 uint8 *x;
73{
74 uint8 *a = x;
75 return ((int64)a[0]<<56) + ((int64)a[1]<<48) + ((int64)a[2]<<40) + ((int64)a[3]<<32) +
76 ((int64)a[4]<< 24) + ((int64)a[5]<<16) + ((int64)a[6]<<8) + (int64)a[7];
77}
78
2934d1c9 79uint16
d70b4d42
MH
80get_word (x)
81 uint8 *x;
2934d1c9 82{
d70b4d42
MH
83 uint8 *a = x;
84 return ((uint16)a[0]<<8) + a[1];
2934d1c9
MH
85}
86
87void
d70b4d42
MH
88write_word (addr, data)
89 uint8 *addr;
90 uint16 data;
2934d1c9 91{
d70b4d42 92 uint8 *a = addr;
2934d1c9
MH
93 a[0] = data >> 8;
94 a[1] = data & 0xff;
95}
96
d70b4d42
MH
97void
98write_longlong (addr, data)
99 uint8 *addr;
100 int64 data;
101{
102 uint8 *a = addr;
103 a[0] = data >> 56;
104 a[1] = (data >> 48) & 0xff;
105 a[2] = (data >> 40) & 0xff;
106 a[3] = (data >> 32) & 0xff;
107 a[4] = (data >> 24) & 0xff;
108 a[5] = (data >> 16) & 0xff;
109 a[6] = (data >> 8) & 0xff;
110 a[7] = data & 0xff;
111}
112
2934d1c9
MH
113
114static void
115get_operands (struct simops *s, uint32 ins)
116{
117 int i, shift, bits, flags;
118 uint32 mask;
119 for (i=0; i < s->numops; i++)
120 {
121 shift = s->operands[3*i];
122 bits = s->operands[3*i+1];
123 flags = s->operands[3*i+2];
124 mask = 0x7FFFFFFF >> (31 - bits);
125 OP[i] = (ins >> shift) & mask;
126 }
127}
128
129static void
130do_long (ins)
131 uint32 ins;
132{
133 struct hash_entry *h;
134 /* printf ("do_long %x\n",ins); */
135 h = lookup_hash (ins, 1);
136 get_operands (h->ops, ins);
137 (h->ops->func)();
138}
139static void
140do_2_short (ins1, ins2)
141 uint16 ins1, ins2;
142{
143 struct hash_entry *h;
144 /* printf ("do_2_short %x -> %x\n",ins1,ins2); */
145 h = lookup_hash (ins1, 0);
146 get_operands (h->ops, ins1);
147 (h->ops->func)();
148 h = lookup_hash (ins2, 0);
149 get_operands (h->ops, ins2);
150 (h->ops->func)();
151}
152static void
153do_parallel (ins1, ins2)
154 uint16 ins1, ins2;
155{
156 struct hash_entry *h1, *h2;
d70b4d42 157 /* printf ("do_parallel %x || %x\n",ins1,ins2); */
2934d1c9 158 h1 = lookup_hash (ins1, 0);
2934d1c9 159 h2 = lookup_hash (ins2, 0);
d70b4d42 160
2934d1c9
MH
161 if (h1->ops->exec_type == PARONLY)
162 {
d70b4d42 163 get_operands (h1->ops, ins1);
2934d1c9
MH
164 (h1->ops->func)();
165 if (State.exe)
d70b4d42
MH
166 {
167 get_operands (h2->ops, ins2);
168 (h2->ops->func)();
169 }
2934d1c9
MH
170 }
171 else if (h2->ops->exec_type == PARONLY)
172 {
d70b4d42 173 get_operands (h2->ops, ins2);
2934d1c9
MH
174 (h2->ops->func)();
175 if (State.exe)
d70b4d42
MH
176 {
177 get_operands (h1->ops, ins1);
178 (h1->ops->func)();
179 }
2934d1c9
MH
180 }
181 else
182 {
d70b4d42 183 get_operands (h1->ops, ins1);
2934d1c9 184 (h1->ops->func)();
d70b4d42 185 get_operands (h2->ops, ins2);
2934d1c9
MH
186 (h2->ops->func)();
187 }
188}
189
190
191void
192sim_size (power)
193 int power;
194
195{
196 if (State.imem)
197 {
198 free (State.imem);
199 free (State.dmem);
200 }
201
202 State.imem = (uint8 *)calloc(1,1<<IMEM_SIZE);
203 State.dmem = (uint8 *)calloc(1,1<<DMEM_SIZE);
204 if (!State.imem || !State.dmem )
205 {
206 fprintf (stderr,"Memory allocation failed.\n");
207 exit(1);
208 }
209 printf ("Allocated %d bytes instruction memory and\n",1<<IMEM_SIZE);
210 printf (" %d bytes data memory.\n",1<<DMEM_SIZE);
211}
212
213static void
214init_system ()
215{
216 if (!State.imem)
217 sim_size(1);
218}
219
220int
221sim_write (addr, buffer, size)
222 SIM_ADDR addr;
223 unsigned char *buffer;
224 int size;
225{
226 int i;
227 init_system ();
228
d70b4d42 229 /* printf ("sim_write %d bytes to 0x%x\n",size,addr); */
2934d1c9
MH
230 for (i = 0; i < size; i++)
231 {
232 State.imem[i+addr] = buffer[i];
233 }
234 return size;
235}
236
237void
238sim_open (args)
239 char *args;
240{
241 struct simops *s;
242 struct hash_entry *h, *prev;
243 if (args != NULL)
244 printf ("sim_open %s\n",args);
245
246 /* put all the opcodes in the hash table */
247 for (s = Simops; s->func; s++)
248 {
249 h = &hash_table[hash(s->opcode,s->format)];
250
251 /* go to the last entry in the chain */
252 while (h->next)
253 h = h->next;
254
255 if (h->ops)
256 {
257 h->next = calloc(1,sizeof(struct hash_entry));
258 h = h->next;
259 }
260 h->ops = s;
261 h->mask = s->mask;
262 h->opcode = s->opcode;
263 }
264}
265
266
267void
268sim_close (quitting)
269 int quitting;
270{
271 /* nothing to do */
272}
273
274void
275sim_set_profile (n)
276 int n;
277{
278 printf ("sim_set_profile %d\n",n);
279}
280
281void
282sim_set_profile_size (n)
283 int n;
284{
285 printf ("sim_set_profile_size %d\n",n);
286}
287
288void
289sim_resume (step, siggnal)
290 int step, siggnal;
291{
292 uint32 inst;
293 int i;
294 reg_t oldpc;
295
d70b4d42 296/* printf ("sim_resume (%d,%d) PC=0x%x\n",step,siggnal,PC); */
2934d1c9 297
d70b4d42
MH
298 if (step)
299 State.exception = SIGTRAP;
300 else
301 State.exception = 0;
302
303 do
304 {
305 inst = RLW (PC << 2);
306 oldpc = PC;
307 switch (inst & 0xC0000000)
308 {
309 case 0xC0000000:
310 /* long instruction */
311 do_long (inst & 0x3FFFFFFF);
312 break;
313 case 0x80000000:
314 /* R -> L */
315 do_2_short ( inst & 0x7FFF, (inst & 0x3FFF8000) >> 15);
316 break;
317 case 0x40000000:
318 /* L -> R */
319 do_2_short ((inst & 0x3FFF8000) >> 15, inst & 0x7FFF);
320 break;
321 case 0:
322 do_parallel ((inst & 0x3FFF8000) >> 15, inst & 0x7FFF);
323 break;
324 }
325
326 if (State.RP && PC == RPT_E)
327 {
328 RPT_C -= 1;
329 if (RPT_C == 0)
330 State.RP = 0;
331 else
332 PC = RPT_S;
333 }
334
335 /* FIXME */
336 if (PC == oldpc)
337 PC++;
338 }
339 while (!State.exception);
2934d1c9
MH
340}
341
342int
343sim_trace ()
344{
345 printf ("sim_trace\n");
346 return 0;
347}
348
349void
350sim_info (verbose)
351 int verbose;
352{
d70b4d42 353 printf ("sim_info\n");
2934d1c9
MH
354}
355
356void
357sim_create_inferior (start_address, argv, env)
358 SIM_ADDR start_address;
359 char **argv;
360 char **env;
361{
362 printf ("sim_create_inferior: PC=0x%x\n",start_address);
363 PC = start_address >> 2;
364}
365
366
367void
368sim_kill ()
369{
370 /* nothing to do */
371}
372
373void
374sim_set_callbacks(p)
375 host_callback *p;
376{
377 printf ("sim_set_callbacks\n");
378 /* callback = p; */
379}
380
381void
382sim_stop_reason (reason, sigrc)
383 enum sim_stop *reason;
384 int *sigrc;
385{
d70b4d42
MH
386/* printf ("sim_stop_reason: PC=0x%x\n",PC<<2); */
387
388 if (State.exception == SIGQUIT)
389 {
390 *reason = sim_exited;
391 *sigrc = State.exception;
392 }
393 else
394 {
395 *reason = sim_stopped;
396 *sigrc = State.exception;
397 }
398}
399
400void
401sim_fetch_register (rn, memory)
402 int rn;
403 unsigned char *memory;
404{
405 if (rn > 31)
406 {
407 WRITE_64 (memory, State.a[rn-32]);
408 /* printf ("sim_fetch_register %d 0x%llx\n",rn,State.a[rn-32]); */
409 }
410 else
411 {
412 WRITE_16 (memory, State.regs[rn]);
413 /* printf ("sim_fetch_register %d 0x%x\n",rn,State.regs[rn]); */
414 }
415}
416
417void
418sim_store_register (rn, memory)
419 int rn;
420 unsigned char *memory;
421{
422 if (rn > 31)
423 {
424 State.a[rn-32] = READ_64 (memory) & MASK40;
425 /* printf ("store: a%d=0x%llx\n",rn-32,State.a[rn-32]); */
426 }
427 else
428 {
429 State.regs[rn]= READ_16 (memory);
430 /* printf ("store: r%d=0x%x\n",rn,State.regs[rn]); */
431 }
2934d1c9 432}
d70b4d42
MH
433
434sim_read (addr, buffer, size)
435 SIM_ADDR addr;
436 unsigned char *buffer;
437 int size;
438{
439 int i;
440 for (i = 0; i < size; i++)
441 {
442 buffer[i] = State.imem[addr + i];
443 }
444 return size;
445}
446
447void
448sim_do_command (cmd)
449 char *cmd;
450{
451 printf("sim_do_command: %s\n",cmd);
452}
453
454int
455sim_load (prog, from_tty)
456 char *prog;
457 int from_tty;
458{
459 /* Return nonzero so GDB will handle it. */
460 return 1;
461}
This page took 0.133088 seconds and 4 git commands to generate.