OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [gdb-5.3/] [sim/] [h8300/] [compile.c] - Blame information for rev 1767

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1181 sfurman
/*
2
 * Simulator for the Hitachi H8/300 architecture.
3
 *
4
 * Written by Steve Chamberlain of Cygnus Support. sac@cygnus.com
5
 *
6
 * This file is part of H8/300 sim
7
 *
8
 *
9
 * THIS SOFTWARE IS NOT COPYRIGHTED
10
 *
11
 * Cygnus offers the following for use in the public domain.  Cygnus makes no
12
 * warranty with regard to the software or its performance and the user
13
 * accepts the software "AS IS" with all faults.
14
 *
15
 * CYGNUS DISCLAIMS ANY WARRANTIES, EXPRESS OR IMPLIED, WITH REGARD TO THIS
16
 * SOFTWARE INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY
17
 * AND FITNESS FOR A PARTICULAR PURPOSE.
18
 */
19
 
20
#include "config.h"
21
 
22
#include <stdio.h>
23
#include <signal.h>
24
#ifdef HAVE_TIME_H
25
#include <time.h>
26
#endif
27
#ifdef HAVE_STDLIB_H
28
#include <stdlib.h>
29
#endif
30
#ifdef HAVE_SYS_PARAM_H
31
#include <sys/param.h>
32
#endif
33
#include "ansidecl.h"
34
#include "bfd.h"
35
#include "gdb/callback.h"
36
#include "gdb/remote-sim.h"
37
#include "gdb/sim-h8300.h"
38
 
39
#ifndef SIGTRAP
40
# define SIGTRAP 5
41
#endif
42
 
43
int debug;
44
 
45
host_callback *sim_callback;
46
 
47
static SIM_OPEN_KIND sim_kind;
48
static char *myname;
49
 
50
/* FIXME: Needs to live in header file.
51
   This header should also include the things in remote-sim.h.
52
   One could move this to remote-sim.h but this function isn't needed
53
   by gdb.  */
54
void sim_set_simcache_size PARAMS ((int));
55
 
56
#define X(op, size)  op * 4 + size
57
 
58
#define SP (h8300hmode ? SL : SW)
59
#define SB 0
60
#define SW 1
61
#define SL 2
62
#define OP_REG 1
63
#define OP_DEC 2
64
#define OP_DISP 3
65
#define OP_INC 4
66
#define OP_PCREL 5
67
#define OP_MEM 6
68
#define OP_CCR 7
69
#define OP_IMM 8
70
#define OP_ABS 10
71
#define OP_EXR 11
72
#define h8_opcodes ops
73
#define DEFINE_TABLE
74
#include "opcode/h8300.h"
75
 
76
#include "inst.h"
77
 
78
/* The rate at which to call the host's poll_quit callback.  */
79
 
80
#define POLL_QUIT_INTERVAL 0x80000
81
 
82
#define LOW_BYTE(x) ((x) & 0xff)
83
#define HIGH_BYTE(x) (((x) >> 8) & 0xff)
84
#define P(X,Y) ((X << 8) | Y)
85
 
86
#define BUILDSR()   cpu.ccr = (I << 7) | (UI << 6)| (H<<5) | (U<<4) | \
87
                              (N << 3) | (Z << 2) | (V<<1) | C;
88
 
89
#define BUILDEXR()          \
90
  if (h8300smode) cpu.exr = (trace<<7) | intMask;
91
 
92
#define GETSR()             \
93
  c = (cpu.ccr >> 0) & 1;\
94
  v = (cpu.ccr >> 1) & 1;\
95
  nz = !((cpu.ccr >> 2) & 1);\
96
  n = (cpu.ccr >> 3) & 1;\
97
  u = (cpu.ccr >> 4) & 1;\
98
  h = (cpu.ccr >> 5) & 1;\
99
  ui = ((cpu.ccr >> 6) & 1);\
100
  intMaskBit = (cpu.ccr >> 7) & 1;
101
 
102
#define GETEXR()            \
103
  if (h8300smode) { \
104
    trace = (cpu.exr >> 7) & 1;\
105
    intMask = cpu.exr & 7; }
106
 
107
#ifdef __CHAR_IS_SIGNED__
108
#define SEXTCHAR(x) ((char) (x))
109
#endif
110
 
111
#ifndef SEXTCHAR
112
#define SEXTCHAR(x) ((x & 0x80) ? (x | ~0xff): x & 0xff)
113
#endif
114
 
115
#define UEXTCHAR(x) ((x) & 0xff)
116
#define UEXTSHORT(x) ((x) & 0xffff)
117
#define SEXTSHORT(x) ((short) (x))
118
 
119
static cpu_state_type cpu;
120
 
121
int h8300hmode = 0;
122
int h8300smode = 0;
123
 
124
static int memory_size;
125
 
126
static int
127
get_now ()
128
{
129
  return time (0);       /* WinXX HAS UNIX like 'time', so why not using it? */
130
}
131
 
132
static int
133
now_persec ()
134
{
135
  return 1;
136
}
137
 
138
static int
139
bitfrom (x)
140
{
141
  switch (x & SIZE)
142
    {
143
    case L_8:
144
      return SB;
145
    case L_16:
146
      return SW;
147
    case L_32:
148
      return SL;
149
    case L_P:
150
      return h8300hmode ? SL : SW;
151
    }
152
}
153
 
154
static unsigned int
155
lvalue (x, rn)
156
{
157
  switch (x / 4)
158
    {
159
    case OP_DISP:
160
      if (rn == 8)
161
        {
162
          return X (OP_IMM, SP);
163
        }
164
      return X (OP_REG, SP);
165
 
166
    case OP_MEM:
167
      return X (OP_MEM, SP);
168
 
169
    default:
170
      abort (); /* ?? May be something more usefull? */
171
    }
172
}
173
 
174
static unsigned int
175
decode (addr, data, dst)
176
     int addr;
177
     unsigned char *data;
178
     decoded_inst *dst;
179
 
180
{
181
  int rs = 0;
182
  int rd = 0;
183
  int rdisp = 0;
184
  int abs = 0;
185
  int bit = 0;
186
  int plen = 0;
187
  struct h8_opcode *q;
188
  int size = 0;
189
 
190
  dst->dst.type = -1;
191
  dst->src.type = -1;
192
 
193
  /* Find the exact opcode/arg combo.  */
194
  for (q = h8_opcodes; q->name; q++)
195
    {
196
      op_type *nib = q->data.nib;
197
      unsigned int len = 0;
198
 
199
      while (1)
200
        {
201
          op_type looking_for = *nib;
202
          int thisnib = data[len >> 1];
203
 
204
          thisnib = (len & 1) ? (thisnib & 0xf) : ((thisnib >> 4) & 0xf);
205
 
206
          if (looking_for < 16 && looking_for >= 0)
207
            {
208
              if (looking_for != thisnib)
209
                goto fail;
210
            }
211
          else
212
            {
213
              if ((int) looking_for & (int) B31)
214
                {
215
                  if (!(((int) thisnib & 0x8) != 0))
216
                    goto fail;
217
 
218
                  looking_for = (op_type) ((int) looking_for & ~(int) B31);
219
                  thisnib &= 0x7;
220
                }
221
 
222
              if ((int) looking_for & (int) B30)
223
                {
224
                  if (!(((int) thisnib & 0x8) == 0))
225
                    goto fail;
226
 
227
                  looking_for = (op_type) ((int) looking_for & ~(int) B30);
228
                }
229
 
230
              if (looking_for & DBIT)
231
                {
232
                  /* Exclude adds/subs by looking at bit 0 and 2, and
233
                     make sure the operand size, either w or l,
234
                     matches by looking at bit 1.  */
235
                  if ((looking_for & 7) != (thisnib & 7))
236
                    goto fail;
237
 
238
                  abs = (thisnib & 0x8) ? 2 : 1;
239
                }
240
              else if (looking_for & (REG | IND | INC | DEC))
241
                {
242
                  if (looking_for & REG)
243
                    {
244
                      /* Can work out size from the register.  */
245
                      size = bitfrom (looking_for);
246
                    }
247
                  if (looking_for & SRC)
248
                    rs = thisnib;
249
                  else
250
                    rd = thisnib;
251
                }
252
              else if (looking_for & L_16)
253
                {
254
                  abs = (data[len >> 1]) * 256 + data[(len + 2) >> 1];
255
                  plen = 16;
256
                  if (looking_for & (PCREL | DISP))
257
                    {
258
                      abs = (short) (abs);
259
                    }
260
                }
261
              else if (looking_for & ABSJMP)
262
                {
263
                  abs = (data[1] << 16) | (data[2] << 8) | (data[3]);
264
                }
265
              else if (looking_for & MEMIND)
266
                {
267
                  abs = data[1];
268
                }
269
              else if (looking_for & L_32)
270
                {
271
                  int i = len >> 1;
272
 
273
                  abs = (data[i] << 24)
274
                    | (data[i + 1] << 16)
275
                    | (data[i + 2] << 8)
276
                    | (data[i + 3]);
277
 
278
                  plen = 32;
279
                }
280
              else if (looking_for & L_24)
281
                {
282
                  int i = len >> 1;
283
 
284
                  abs = (data[i] << 16) | (data[i + 1] << 8) | (data[i + 2]);
285
                  plen = 24;
286
                }
287
              else if (looking_for & IGNORE)
288
                {
289
                  ;
290
                }
291
              else if (looking_for & DISPREG)
292
                {
293
                  rdisp = thisnib & 0x7;
294
                }
295
              else if (looking_for & KBIT)
296
                {
297
                  switch (thisnib)
298
                    {
299
                    case 9:
300
                      abs = 4;
301
                      break;
302
                    case 8:
303
                      abs = 2;
304
                      break;
305
                    case 0:
306
                      abs = 1;
307
                      break;
308
                    default:
309
                      goto fail;
310
                    }
311
                }
312
              else if (looking_for & L_8)
313
                {
314
                  plen = 8;
315
 
316
                  if (looking_for & PCREL)
317
                    {
318
                      abs = SEXTCHAR (data[len >> 1]);
319
                    }
320
                  else if (looking_for & ABS8MEM)
321
                    {
322
                      plen = 8;
323
                      abs = h8300hmode ? ~0xff0000ff : ~0xffff00ff;
324
                      abs |= data[len >> 1] & 0xff;
325
                    }
326
                  else
327
                    {
328
                      abs = data[len >> 1] & 0xff;
329
                    }
330
                }
331
              else if (looking_for & L_3)
332
                {
333
                  plen = 3;
334
 
335
                  bit = thisnib;
336
                }
337
              else if (looking_for == E)
338
                {
339
                  dst->op = q;
340
 
341
                  /* Fill in the args.  */
342
                  {
343
                    op_type *args = q->args.nib;
344
                    int hadone = 0;
345
 
346
                    while (*args != E)
347
                      {
348
                        int x = *args;
349
                        int rn = (x & DST) ? rd : rs;
350
                        ea_type *p;
351
 
352
                        if (x & DST)
353
                          p = &(dst->dst);
354
                        else
355
                          p = &(dst->src);
356
 
357
                        if (x & L_3)
358
                          {
359
                            p->type = X (OP_IMM, size);
360
                            p->literal = bit;
361
                          }
362
                        else if (x & (IMM | KBIT | DBIT))
363
                          {
364
                            p->type = X (OP_IMM, size);
365
                            p->literal = abs;
366
                          }
367
                        else if (x & REG)
368
                          {
369
                            /* Reset the size.
370
                               Some ops (like mul) have two sizes.  */
371
 
372
                            size = bitfrom (x);
373
                            p->type = X (OP_REG, size);
374
                            p->reg = rn;
375
                          }
376
                        else if (x & INC)
377
                          {
378
                            p->type = X (OP_INC, size);
379
                            p->reg = rn & 0x7;
380
                          }
381
                        else if (x & DEC)
382
                          {
383
                            p->type = X (OP_DEC, size);
384
                            p->reg = rn & 0x7;
385
                          }
386
                        else if (x & IND)
387
                          {
388
                            p->type = X (OP_DISP, size);
389
                            p->reg = rn & 0x7;
390
                            p->literal = 0;
391
                          }
392
                        else if (x & (ABS | ABSJMP | ABS8MEM))
393
                          {
394
                            p->type = X (OP_DISP, size);
395
                            p->literal = abs;
396
                            p->reg = 8;
397
                          }
398
                        else if (x & MEMIND)
399
                          {
400
                            p->type = X (OP_MEM, size);
401
                            p->literal = abs;
402
                          }
403
                        else if (x & PCREL)
404
                          {
405
                            p->type = X (OP_PCREL, size);
406
                            p->literal = abs + addr + 2;
407
                            if (x & L_16)
408
                              p->literal += 2;
409
                          }
410
                        else if (x & ABSJMP)
411
                          {
412
                            p->type = X (OP_IMM, SP);
413
                            p->literal = abs;
414
                          }
415
                        else if (x & DISP)
416
                          {
417
                            p->type = X (OP_DISP, size);
418
                            p->literal = abs;
419
                            p->reg = rdisp & 0x7;
420
                          }
421
                        else if (x & CCR)
422
                          {
423
                            p->type = OP_CCR;
424
                          }
425
                        else if (x & EXR)
426
                          {
427
                            p->type = OP_EXR;
428
                          }
429
                        else
430
                          printf ("Hmmmm %x", x);
431
 
432
                        args++;
433
                      }
434
                  }
435
 
436
                  /* But a jmp or a jsr gets automagically lvalued,
437
                     since we branch to their address not their
438
                     contents.  */
439
                  if (q->how == O (O_JSR, SB)
440
                      || q->how == O (O_JMP, SB))
441
                    {
442
                      dst->src.type = lvalue (dst->src.type, dst->src.reg);
443
                    }
444
 
445
                  if (dst->dst.type == -1)
446
                    dst->dst = dst->src;
447
 
448
                  dst->opcode = q->how;
449
                  dst->cycles = q->time;
450
 
451
                  /* And a jsr to 0xc4 is turned into a magic trap.  */
452
 
453
                  if (dst->opcode == O (O_JSR, SB))
454
                    {
455
                      if (dst->src.literal == 0xc4)
456
                        {
457
                          dst->opcode = O (O_SYSCALL, SB);
458
                        }
459
                    }
460
 
461
                  dst->next_pc = addr + len / 2;
462
                  return;
463
                }
464
              else
465
                printf ("Don't understand %x \n", looking_for);
466
            }
467
 
468
          len++;
469
          nib++;
470
        }
471
 
472
    fail:
473
      ;
474
    }
475
 
476
  /* Fell off the end.  */
477
  dst->opcode = O (O_ILL, SB);
478
}
479
 
480
static void
481
compile (pc)
482
{
483
  int idx;
484
 
485
  /* Find the next cache entry to use.  */
486
  idx = cpu.cache_top + 1;
487
  cpu.compiles++;
488
  if (idx >= cpu.csize)
489
    {
490
      idx = 1;
491
    }
492
  cpu.cache_top = idx;
493
 
494
  /* Throw away its old meaning.  */
495
  cpu.cache_idx[cpu.cache[idx].oldpc] = 0;
496
 
497
  /* Set to new address.  */
498
  cpu.cache[idx].oldpc = pc;
499
 
500
  /* Fill in instruction info.  */
501
  decode (pc, cpu.memory + pc, cpu.cache + idx);
502
 
503
  /* Point to new cache entry.  */
504
  cpu.cache_idx[pc] = idx;
505
}
506
 
507
 
508
static unsigned char *breg[18];
509
static unsigned short *wreg[18];
510
static unsigned int *lreg[18];
511
 
512
#define GET_B_REG(x) *(breg[x])
513
#define SET_B_REG(x,y) (*(breg[x])) = (y)
514
#define GET_W_REG(x) *(wreg[x])
515
#define SET_W_REG(x,y) (*(wreg[x])) = (y)
516
 
517
#define GET_L_REG(x) *(lreg[x])
518
#define SET_L_REG(x,y) (*(lreg[x])) = (y)
519
 
520
#define GET_MEMORY_L(x) \
521
  (x < memory_size \
522
   ? ((cpu.memory[x+0] << 24) | (cpu.memory[x+1] << 16) \
523
      | (cpu.memory[x+2] << 8) | cpu.memory[x+3]) \
524
   : ((cpu.eightbit[(x+0) & 0xff] << 24) | (cpu.eightbit[(x+1) & 0xff] << 16) \
525
      | (cpu.eightbit[(x+2) & 0xff] << 8) | cpu.eightbit[(x+3) & 0xff]))
526
 
527
#define GET_MEMORY_W(x) \
528
  (x < memory_size \
529
   ? ((cpu.memory[x+0] << 8) | (cpu.memory[x+1] << 0)) \
530
   : ((cpu.eightbit[(x+0) & 0xff] << 8) | (cpu.eightbit[(x+1) & 0xff] << 0)))
531
 
532
 
533
#define GET_MEMORY_B(x) \
534
  (x < memory_size ? (cpu.memory[x]) : (cpu.eightbit[x & 0xff]))
535
 
536
#define SET_MEMORY_L(x,y)  \
537
{  register unsigned char *_p; register int __y = y; \
538
   _p = (x < memory_size ? cpu.memory+x : cpu.eightbit + (x & 0xff)); \
539
   _p[0] = (__y)>>24; _p[1] = (__y)>>16; \
540
   _p[2] = (__y)>>8; _p[3] = (__y)>>0;}
541
 
542
#define SET_MEMORY_W(x,y) \
543
{  register unsigned char *_p; register int __y = y; \
544
   _p = (x < memory_size ? cpu.memory+x : cpu.eightbit + (x & 0xff)); \
545
   _p[0] = (__y)>>8; _p[1] =(__y);}
546
 
547
#define SET_MEMORY_B(x,y) \
548
  (x < memory_size ? (cpu.memory[(x)] = y) : (cpu.eightbit[x & 0xff] = y))
549
 
550
int
551
fetch (arg, n)
552
     ea_type *arg;
553
{
554
  int rn = arg->reg;
555
  int abs = arg->literal;
556
  int r;
557
  int t;
558
 
559
  switch (arg->type)
560
    {
561
    case X (OP_REG, SB):
562
      return GET_B_REG (rn);
563
    case X (OP_REG, SW):
564
      return GET_W_REG (rn);
565
    case X (OP_REG, SL):
566
      return GET_L_REG (rn);
567
    case X (OP_IMM, SB):
568
    case X (OP_IMM, SW):
569
    case X (OP_IMM, SL):
570
      return abs;
571
    case X (OP_DEC, SB):
572
      abort ();
573
 
574
    case X (OP_INC, SB):
575
      t = GET_L_REG (rn);
576
      t &= cpu.mask;
577
      r = GET_MEMORY_B (t);
578
      t++;
579
      t = t & cpu.mask;
580
      SET_L_REG (rn, t);
581
      return r;
582
      break;
583
    case X (OP_INC, SW):
584
      t = GET_L_REG (rn);
585
      t &= cpu.mask;
586
      r = GET_MEMORY_W (t);
587
      t += 2;
588
      t = t & cpu.mask;
589
      SET_L_REG (rn, t);
590
      return r;
591
    case X (OP_INC, SL):
592
      t = GET_L_REG (rn);
593
      t &= cpu.mask;
594
      r = GET_MEMORY_L (t);
595
 
596
      t += 4;
597
      t = t & cpu.mask;
598
      SET_L_REG (rn, t);
599
      return r;
600
 
601
    case X (OP_DISP, SB):
602
      t = GET_L_REG (rn) + abs;
603
      t &= cpu.mask;
604
      return GET_MEMORY_B (t);
605
 
606
    case X (OP_DISP, SW):
607
      t = GET_L_REG (rn) + abs;
608
      t &= cpu.mask;
609
      return GET_MEMORY_W (t);
610
 
611
    case X (OP_DISP, SL):
612
      t = GET_L_REG (rn) + abs;
613
      t &= cpu.mask;
614
      return GET_MEMORY_L (t);
615
 
616
    case X (OP_MEM, SL):
617
      t = GET_MEMORY_L (abs);
618
      t &= cpu.mask;
619
      return t;
620
 
621
    case X (OP_MEM, SW):
622
      t = GET_MEMORY_W (abs);
623
      t &= cpu.mask;
624
      return t;
625
 
626
    default:
627
      abort (); /* ?? May be something more usefull? */
628
 
629
    }
630
}
631
 
632
 
633
static void
634
store (arg, n)
635
     ea_type *arg;
636
     int n;
637
{
638
  int rn = arg->reg;
639
  int abs = arg->literal;
640
  int t;
641
 
642
  switch (arg->type)
643
    {
644
    case X (OP_REG, SB):
645
      SET_B_REG (rn, n);
646
      break;
647
    case X (OP_REG, SW):
648
      SET_W_REG (rn, n);
649
      break;
650
    case X (OP_REG, SL):
651
      SET_L_REG (rn, n);
652
      break;
653
 
654
    case X (OP_DEC, SB):
655
      t = GET_L_REG (rn) - 1;
656
      t &= cpu.mask;
657
      SET_L_REG (rn, t);
658
      SET_MEMORY_B (t, n);
659
 
660
      break;
661
    case X (OP_DEC, SW):
662
      t = (GET_L_REG (rn) - 2) & cpu.mask;
663
      SET_L_REG (rn, t);
664
      SET_MEMORY_W (t, n);
665
      break;
666
 
667
    case X (OP_DEC, SL):
668
      t = (GET_L_REG (rn) - 4) & cpu.mask;
669
      SET_L_REG (rn, t);
670
      SET_MEMORY_L (t, n);
671
      break;
672
 
673
    case X (OP_DISP, SB):
674
      t = GET_L_REG (rn) + abs;
675
      t &= cpu.mask;
676
      SET_MEMORY_B (t, n);
677
      break;
678
 
679
    case X (OP_DISP, SW):
680
      t = GET_L_REG (rn) + abs;
681
      t &= cpu.mask;
682
      SET_MEMORY_W (t, n);
683
      break;
684
 
685
    case X (OP_DISP, SL):
686
      t = GET_L_REG (rn) + abs;
687
      t &= cpu.mask;
688
      SET_MEMORY_L (t, n);
689
      break;
690
    default:
691
      abort ();
692
    }
693
}
694
 
695
 
696
static union
697
{
698
  short int i;
699
  struct
700
    {
701
      char low;
702
      char high;
703
    }
704
  u;
705
}
706
 
707
littleendian;
708
 
709
static void
710
init_pointers ()
711
{
712
  static int init;
713
 
714
  if (!init)
715
    {
716
      int i;
717
 
718
      init = 1;
719
      littleendian.i = 1;
720
 
721
      if (h8300smode)
722
        memory_size = H8300S_MSIZE;
723
      else if (h8300hmode)
724
        memory_size = H8300H_MSIZE;
725
      else
726
        memory_size = H8300_MSIZE;
727
      cpu.memory = (unsigned char *) calloc (sizeof (char), memory_size);
728
      cpu.cache_idx = (unsigned short *) calloc (sizeof (short), memory_size);
729
      cpu.eightbit = (unsigned char *) calloc (sizeof (char), 256);
730
 
731
      /* `msize' must be a power of two.  */
732
      if ((memory_size & (memory_size - 1)) != 0)
733
        abort ();
734
      cpu.mask = memory_size - 1;
735
 
736
      for (i = 0; i < 9; i++)
737
        {
738
          cpu.regs[i] = 0;
739
        }
740
 
741
      for (i = 0; i < 8; i++)
742
        {
743
          unsigned char *p = (unsigned char *) (cpu.regs + i);
744
          unsigned char *e = (unsigned char *) (cpu.regs + i + 1);
745
          unsigned short *q = (unsigned short *) (cpu.regs + i);
746
          unsigned short *u = (unsigned short *) (cpu.regs + i + 1);
747
          cpu.regs[i] = 0x00112233;
748
          while (p < e)
749
            {
750
              if (*p == 0x22)
751
                {
752
                  breg[i] = p;
753
                }
754
              if (*p == 0x33)
755
                {
756
                  breg[i + 8] = p;
757
                }
758
              p++;
759
            }
760
          while (q < u)
761
            {
762
              if (*q == 0x2233)
763
                {
764
                  wreg[i] = q;
765
                }
766
              if (*q == 0x0011)
767
                {
768
                  wreg[i + 8] = q;
769
                }
770
              q++;
771
            }
772
          cpu.regs[i] = 0;
773
          lreg[i] = &cpu.regs[i];
774
        }
775
 
776
      lreg[8] = &cpu.regs[8];
777
 
778
      /* Initialize the seg registers.  */
779
      if (!cpu.cache)
780
        sim_set_simcache_size (CSIZE);
781
    }
782
}
783
 
784
static void
785
control_c (sig, code, scp, addr)
786
     int sig;
787
     int code;
788
     char *scp;
789
     char *addr;
790
{
791
  cpu.state = SIM_STATE_STOPPED;
792
  cpu.exception = SIGINT;
793
}
794
 
795
#define C (c != 0)
796
#define Z (nz == 0)
797
#define V (v != 0)
798
#define N (n != 0)
799
#define U (u != 0)
800
#define H (h != 0)
801
#define UI (ui != 0)
802
#define I (intMaskBit != 0)
803
 
804
static int
805
mop (code, bsize, sign)
806
     decoded_inst *code;
807
     int bsize;
808
     int sign;
809
{
810
  int multiplier;
811
  int multiplicand;
812
  int result;
813
  int n, nz;
814
 
815
  if (sign)
816
    {
817
      multiplicand =
818
        bsize ? SEXTCHAR (GET_W_REG (code->dst.reg)) :
819
        SEXTSHORT (GET_W_REG (code->dst.reg));
820
      multiplier =
821
        bsize ? SEXTCHAR (GET_B_REG (code->src.reg)) :
822
        SEXTSHORT (GET_W_REG (code->src.reg));
823
    }
824
  else
825
    {
826
      multiplicand = bsize ? UEXTCHAR (GET_W_REG (code->dst.reg)) :
827
        UEXTSHORT (GET_W_REG (code->dst.reg));
828
      multiplier =
829
        bsize ? UEXTCHAR (GET_B_REG (code->src.reg)) :
830
        UEXTSHORT (GET_W_REG (code->src.reg));
831
 
832
    }
833
  result = multiplier * multiplicand;
834
 
835
  if (sign)
836
    {
837
      n = result & (bsize ? 0x8000 : 0x80000000);
838
      nz = result & (bsize ? 0xffff : 0xffffffff);
839
    }
840
  if (bsize)
841
    {
842
      SET_W_REG (code->dst.reg, result);
843
    }
844
  else
845
    {
846
      SET_L_REG (code->dst.reg, result);
847
    }
848
#if 0
849
  return ((n == 1) << 1) | (nz == 1);
850
#endif
851
}
852
 
853
#define ONOT(name, how) \
854
case O (name, SB):                              \
855
{                                               \
856
  int t;                                        \
857
  int hm = 0x80;                                \
858
  rd = GET_B_REG (code->src.reg);               \
859
  how;                                          \
860
  goto shift8;                                  \
861
}                                               \
862
case O (name, SW):                              \
863
{                                               \
864
  int t;                                        \
865
  int hm = 0x8000;                              \
866
  rd = GET_W_REG (code->src.reg);               \
867
  how;                                          \
868
  goto shift16;                                 \
869
}                                               \
870
case O (name, SL):                              \
871
{                                               \
872
  int t;                                        \
873
  int hm = 0x80000000;                          \
874
  rd = GET_L_REG (code->src.reg);               \
875
  how;                                          \
876
  goto shift32;                                 \
877
}
878
 
879
#define OSHIFTS(name, how1, how2) \
880
case O (name, SB):                              \
881
{                                               \
882
  int t;                                        \
883
  int hm = 0x80;                                \
884
  rd = GET_B_REG (code->src.reg);               \
885
  if ((GET_MEMORY_B (pc + 1) & 0x40) == 0)       \
886
    {                                           \
887
      how1;                                     \
888
    }                                           \
889
  else                                          \
890
    {                                           \
891
      how2;                                     \
892
    }                                           \
893
  goto shift8;                                  \
894
}                                               \
895
case O (name, SW):                              \
896
{                                               \
897
  int t;                                        \
898
  int hm = 0x8000;                              \
899
  rd = GET_W_REG (code->src.reg);               \
900
  if ((GET_MEMORY_B (pc + 1) & 0x40) == 0)       \
901
    {                                           \
902
      how1;                                     \
903
    }                                           \
904
  else                                          \
905
    {                                           \
906
      how2;                                     \
907
    }                                           \
908
  goto shift16;                                 \
909
}                                               \
910
case O (name, SL):                              \
911
{                                               \
912
  int t;                                        \
913
  int hm = 0x80000000;                          \
914
  rd = GET_L_REG (code->src.reg);               \
915
  if ((GET_MEMORY_B (pc + 1) & 0x40) == 0)       \
916
    {                                           \
917
      how1;                                     \
918
    }                                           \
919
  else                                          \
920
    {                                           \
921
      how2;                                     \
922
    }                                           \
923
  goto shift32;                                 \
924
}
925
 
926
#define OBITOP(name,f, s, op)                   \
927
case O (name, SB):                              \
928
{                                               \
929
  int m;                                        \
930
  int b;                                        \
931
  if (f) ea = fetch (&code->dst);               \
932
  m=1<< fetch (&code->src);                     \
933
  op;                                           \
934
  if (s) store (&code->dst,ea); goto next;      \
935
}
936
 
937
int
938
sim_stop (sd)
939
     SIM_DESC sd;
940
{
941
  cpu.state = SIM_STATE_STOPPED;
942
  cpu.exception = SIGINT;
943
  return 1;
944
}
945
 
946
#define R0_REGNUM       0
947
#define R1_REGNUM       1
948
#define R2_REGNUM       2
949
#define R3_REGNUM       3
950
#define R4_REGNUM       4
951
#define R5_REGNUM       5
952
#define R6_REGNUM       6
953
#define R7_REGNUM       7
954
 
955
#define SP_REGNUM       R7_REGNUM       /* Contains address of top of stack */
956
#define FP_REGNUM       R6_REGNUM       /* Contains address of executing
957
                                         * stack frame */
958
 
959
#define CCR_REGNUM      8       /* Contains processor status */
960
#define PC_REGNUM       9       /* Contains program counter */
961
 
962
#define CYCLE_REGNUM    10
963
 
964
#define EXR_REGNUM      11
965
#define INST_REGNUM     12
966
#define TICK_REGNUM     13
967
 
968
void
969
sim_resume (sd, step, siggnal)
970
     SIM_DESC sd;
971
{
972
  static int init1;
973
  int cycles = 0;
974
  int insts = 0;
975
  int tick_start = get_now ();
976
  void (*prev) ();
977
  int poll_count = 0;
978
  int res;
979
  int tmp;
980
  int rd;
981
  int ea;
982
  int bit;
983
  int pc;
984
  int c, nz, v, n, u, h, ui, intMaskBit;
985
  int trace, intMask;
986
  int oldmask;
987
  init_pointers ();
988
 
989
  prev = signal (SIGINT, control_c);
990
 
991
  if (step)
992
    {
993
      cpu.state = SIM_STATE_STOPPED;
994
      cpu.exception = SIGTRAP;
995
    }
996
  else
997
    {
998
      cpu.state = SIM_STATE_RUNNING;
999
      cpu.exception = 0;
1000
    }
1001
 
1002
  pc = cpu.pc;
1003
 
1004
  /* The PC should never be odd.  */
1005
  if (pc & 0x1)
1006
    abort ();
1007
 
1008
  GETSR ();
1009
  GETEXR ();
1010
 
1011
  oldmask = cpu.mask;
1012
  if (!h8300hmode)
1013
    cpu.mask = 0xffff;
1014
  do
1015
    {
1016
      int cidx;
1017
      decoded_inst *code;
1018
 
1019
    top:
1020
      cidx = cpu.cache_idx[pc];
1021
      code = cpu.cache + cidx;
1022
 
1023
 
1024
#define ALUOP(STORE, NAME, HOW) \
1025
    case O (NAME,SB):  HOW; if (STORE)goto alu8;else goto just_flags_alu8;  \
1026
    case O (NAME, SW): HOW; if (STORE)goto alu16;else goto just_flags_alu16; \
1027
    case O (NAME,SL):  HOW; if (STORE)goto alu32;else goto just_flags_alu32;
1028
 
1029
 
1030
#define LOGOP(NAME, HOW) \
1031
    case O (NAME,SB): HOW; goto log8;\
1032
    case O (NAME, SW): HOW; goto log16;\
1033
    case O (NAME,SL): HOW; goto log32;
1034
 
1035
 
1036
 
1037
#if ADEBUG
1038
      if (debug)
1039
        {
1040
          printf ("%x %d %s\n", pc, code->opcode,
1041
                  code->op ? code->op->name : "**");
1042
        }
1043
      cpu.stats[code->opcode]++;
1044
 
1045
#endif
1046
 
1047
      if (code->opcode)
1048
        {
1049
          cycles += code->cycles;
1050
          insts++;
1051
        }
1052
 
1053
      switch (code->opcode)
1054
        {
1055
        case 0:
1056
          /*
1057
           * This opcode is a fake for when we get to an
1058
           * instruction which hasnt been compiled
1059
           */
1060
          compile (pc);
1061
          goto top;
1062
          break;
1063
 
1064
 
1065
        case O (O_SUBX, SB):
1066
          rd = fetch (&code->dst);
1067
          ea = fetch (&code->src);
1068
          ea = -(ea + C);
1069
          res = rd + ea;
1070
          goto alu8;
1071
 
1072
        case O (O_ADDX, SB):
1073
          rd = fetch (&code->dst);
1074
          ea = fetch (&code->src);
1075
          ea = C + ea;
1076
          res = rd + ea;
1077
          goto alu8;
1078
 
1079
#define EA    ea = fetch (&code->src);
1080
#define RD_EA ea = fetch (&code->src); rd = fetch (&code->dst);
1081
 
1082
          ALUOP (1, O_SUB, RD_EA;
1083
                 ea = -ea;
1084
                 res = rd + ea);
1085
          ALUOP (1, O_NEG, EA;
1086
                 ea = -ea;
1087
                 rd = 0;
1088
                 res = rd + ea);
1089
 
1090
        case O (O_ADD, SB):
1091
          rd = GET_B_REG (code->dst.reg);
1092
          ea = fetch (&code->src);
1093
          res = rd + ea;
1094
          goto alu8;
1095
        case O (O_ADD, SW):
1096
          rd = GET_W_REG (code->dst.reg);
1097
          ea = fetch (&code->src);
1098
          res = rd + ea;
1099
          goto alu16;
1100
        case O (O_ADD, SL):
1101
          rd = GET_L_REG (code->dst.reg);
1102
          ea = fetch (&code->src);
1103
          res = rd + ea;
1104
          goto alu32;
1105
 
1106
 
1107
          LOGOP (O_AND, RD_EA;
1108
                 res = rd & ea);
1109
 
1110
          LOGOP (O_OR, RD_EA;
1111
                 res = rd | ea);
1112
 
1113
          LOGOP (O_XOR, RD_EA;
1114
                 res = rd ^ ea);
1115
 
1116
 
1117
        case O (O_MOV_TO_MEM, SB):
1118
          res = GET_B_REG (code->src.reg);
1119
          goto log8;
1120
        case O (O_MOV_TO_MEM, SW):
1121
          res = GET_W_REG (code->src.reg);
1122
          goto log16;
1123
        case O (O_MOV_TO_MEM, SL):
1124
          res = GET_L_REG (code->src.reg);
1125
          goto log32;
1126
 
1127
 
1128
        case O (O_MOV_TO_REG, SB):
1129
          res = fetch (&code->src);
1130
          SET_B_REG (code->dst.reg, res);
1131
          goto just_flags_log8;
1132
        case O (O_MOV_TO_REG, SW):
1133
          res = fetch (&code->src);
1134
          SET_W_REG (code->dst.reg, res);
1135
          goto just_flags_log16;
1136
        case O (O_MOV_TO_REG, SL):
1137
          res = fetch (&code->src);
1138
          SET_L_REG (code->dst.reg, res);
1139
          goto just_flags_log32;
1140
 
1141
        case O (O_EEPMOV, SB):
1142
        case O (O_EEPMOV, SW):
1143
          if (h8300hmode||h8300smode)
1144
            {
1145
              register unsigned char *_src,*_dst;
1146
              unsigned int count = (code->opcode == O(O_EEPMOV, SW))?cpu.regs[R4_REGNUM]&0xffff:
1147
                cpu.regs[R4_REGNUM]&0xff;
1148
 
1149
              _src = cpu.regs[R5_REGNUM] < memory_size ? cpu.memory+cpu.regs[R5_REGNUM] :
1150
                cpu.eightbit + (cpu.regs[R5_REGNUM] & 0xff);
1151
              if ((_src+count)>=(cpu.memory+memory_size))
1152
                {
1153
                  if ((_src+count)>=(cpu.eightbit+0x100))
1154
                    goto illegal;
1155
                }
1156
              _dst = cpu.regs[R6_REGNUM] < memory_size ? cpu.memory+cpu.regs[R6_REGNUM] :
1157
                                                              cpu.eightbit + (cpu.regs[R6_REGNUM] & 0xff);
1158
              if ((_dst+count)>=(cpu.memory+memory_size))
1159
                {
1160
                  if ((_dst+count)>=(cpu.eightbit+0x100))
1161
                    goto illegal;
1162
                }
1163
              memcpy(_dst,_src,count);
1164
 
1165
              cpu.regs[R5_REGNUM]+=count;
1166
              cpu.regs[R6_REGNUM]+=count;
1167
              cpu.regs[R4_REGNUM]&=(code->opcode == O(O_EEPMOV, SW))?(~0xffff):(~0xff);
1168
              cycles += 2*count;
1169
              goto next;
1170
            }
1171
          goto illegal;
1172
 
1173
        case O (O_ADDS, SL):
1174
          SET_L_REG (code->dst.reg,
1175
                     GET_L_REG (code->dst.reg)
1176
                     + code->src.literal);
1177
 
1178
          goto next;
1179
 
1180
        case O (O_SUBS, SL):
1181
          SET_L_REG (code->dst.reg,
1182
                     GET_L_REG (code->dst.reg)
1183
                     - code->src.literal);
1184
          goto next;
1185
 
1186
        case O (O_CMP, SB):
1187
          rd = fetch (&code->dst);
1188
          ea = fetch (&code->src);
1189
          ea = -ea;
1190
          res = rd + ea;
1191
          goto just_flags_alu8;
1192
 
1193
        case O (O_CMP, SW):
1194
          rd = fetch (&code->dst);
1195
          ea = fetch (&code->src);
1196
          ea = -ea;
1197
          res = rd + ea;
1198
          goto just_flags_alu16;
1199
 
1200
        case O (O_CMP, SL):
1201
          rd = fetch (&code->dst);
1202
          ea = fetch (&code->src);
1203
          ea = -ea;
1204
          res = rd + ea;
1205
          goto just_flags_alu32;
1206
 
1207
 
1208
        case O (O_DEC, SB):
1209
          rd = GET_B_REG (code->src.reg);
1210
          ea = -1;
1211
          res = rd + ea;
1212
          SET_B_REG (code->src.reg, res);
1213
          goto just_flags_inc8;
1214
 
1215
        case O (O_DEC, SW):
1216
          rd = GET_W_REG (code->dst.reg);
1217
          ea = -code->src.literal;
1218
          res = rd + ea;
1219
          SET_W_REG (code->dst.reg, res);
1220
          goto just_flags_inc16;
1221
 
1222
        case O (O_DEC, SL):
1223
          rd = GET_L_REG (code->dst.reg);
1224
          ea = -code->src.literal;
1225
          res = rd + ea;
1226
          SET_L_REG (code->dst.reg, res);
1227
          goto just_flags_inc32;
1228
 
1229
 
1230
        case O (O_INC, SB):
1231
          rd = GET_B_REG (code->src.reg);
1232
          ea = 1;
1233
          res = rd + ea;
1234
          SET_B_REG (code->src.reg, res);
1235
          goto just_flags_inc8;
1236
 
1237
        case O (O_INC, SW):
1238
          rd = GET_W_REG (code->dst.reg);
1239
          ea = code->src.literal;
1240
          res = rd + ea;
1241
          SET_W_REG (code->dst.reg, res);
1242
          goto just_flags_inc16;
1243
 
1244
        case O (O_INC, SL):
1245
          rd = GET_L_REG (code->dst.reg);
1246
          ea = code->src.literal;
1247
          res = rd + ea;
1248
          SET_L_REG (code->dst.reg, res);
1249
          goto just_flags_inc32;
1250
 
1251
#define GET_CCR(x) BUILDSR();x = cpu.ccr
1252
#define GET_EXR(x) BUILDEXR ();x = cpu.exr
1253
 
1254
        case O (O_LDC, SB):
1255
        case O (O_LDC, SW):
1256
          res = fetch (&code->src);
1257
          goto setc;
1258
        case O (O_STC, SB):
1259
        case O (O_STC, SW):
1260
          if (code->src.type == OP_CCR)
1261
            {
1262
              GET_CCR (res);
1263
            }
1264
          else if (code->src.type == OP_EXR && h8300smode)
1265
            {
1266
              GET_EXR (res);
1267
            }
1268
          else
1269
            goto illegal;
1270
          store (&code->dst, res);
1271
          goto next;
1272
 
1273
        case O (O_ANDC, SB):
1274
          if (code->dst.type == OP_CCR)
1275
            {
1276
              GET_CCR (rd);
1277
            }
1278
          else if (code->dst.type == OP_EXR && h8300smode)
1279
            {
1280
              GET_EXR (rd);
1281
            }
1282
          else
1283
            goto illegal;
1284
          ea = code->src.literal;
1285
          res = rd & ea;
1286
          goto setc;
1287
 
1288
        case O (O_ORC, SB):
1289
          if (code->dst.type == OP_CCR)
1290
            {
1291
              GET_CCR (rd);
1292
            }
1293
          else if (code->dst.type == OP_EXR && h8300smode)
1294
            {
1295
              GET_EXR (rd);
1296
            }
1297
          else
1298
            goto illegal;
1299
          ea = code->src.literal;
1300
          res = rd | ea;
1301
          goto setc;
1302
 
1303
        case O (O_XORC, SB):
1304
          if (code->dst.type == OP_CCR)
1305
            {
1306
              GET_CCR (rd);
1307
            }
1308
          else if (code->dst.type == OP_EXR && h8300smode)
1309
            {
1310
              GET_EXR (rd);
1311
            }
1312
          else
1313
            goto illegal;
1314
          ea = code->src.literal;
1315
          res = rd ^ ea;
1316
          goto setc;
1317
 
1318
 
1319
        case O (O_BRA, SB):
1320
          if (1)
1321
            goto condtrue;
1322
          goto next;
1323
 
1324
        case O (O_BRN, SB):
1325
          if (0)
1326
            goto condtrue;
1327
          goto next;
1328
 
1329
        case O (O_BHI, SB):
1330
          if ((C || Z) == 0)
1331
            goto condtrue;
1332
          goto next;
1333
 
1334
 
1335
        case O (O_BLS, SB):
1336
          if ((C || Z))
1337
            goto condtrue;
1338
          goto next;
1339
 
1340
        case O (O_BCS, SB):
1341
          if ((C == 1))
1342
            goto condtrue;
1343
          goto next;
1344
 
1345
        case O (O_BCC, SB):
1346
          if ((C == 0))
1347
            goto condtrue;
1348
          goto next;
1349
 
1350
        case O (O_BEQ, SB):
1351
          if (Z)
1352
            goto condtrue;
1353
          goto next;
1354
        case O (O_BGT, SB):
1355
          if (((Z || (N ^ V)) == 0))
1356
            goto condtrue;
1357
          goto next;
1358
 
1359
 
1360
        case O (O_BLE, SB):
1361
          if (((Z || (N ^ V)) == 1))
1362
            goto condtrue;
1363
          goto next;
1364
 
1365
        case O (O_BGE, SB):
1366
          if ((N ^ V) == 0)
1367
            goto condtrue;
1368
          goto next;
1369
        case O (O_BLT, SB):
1370
          if ((N ^ V))
1371
            goto condtrue;
1372
          goto next;
1373
        case O (O_BMI, SB):
1374
          if ((N))
1375
            goto condtrue;
1376
          goto next;
1377
        case O (O_BNE, SB):
1378
          if ((Z == 0))
1379
            goto condtrue;
1380
          goto next;
1381
 
1382
        case O (O_BPL, SB):
1383
          if (N == 0)
1384
            goto condtrue;
1385
          goto next;
1386
        case O (O_BVC, SB):
1387
          if ((V == 0))
1388
            goto condtrue;
1389
          goto next;
1390
        case O (O_BVS, SB):
1391
          if ((V == 1))
1392
            goto condtrue;
1393
          goto next;
1394
 
1395
        case O (O_SYSCALL, SB):
1396
          {
1397
            char c = cpu.regs[2];
1398
            sim_callback->write_stdout (sim_callback, &c, 1);
1399
          }
1400
          goto next;
1401
 
1402
          ONOT (O_NOT, rd = ~rd; v = 0;);
1403
          OSHIFTS (O_SHLL,
1404
                   c = rd & hm; v = 0; rd <<= 1,
1405
                   c = rd & (hm >> 1); v = 0; rd <<= 2);
1406
          OSHIFTS (O_SHLR,
1407
                   c = rd & 1; v = 0; rd = (unsigned int) rd >> 1,
1408
                   c = rd & 2; v = 0; rd = (unsigned int) rd >> 2);
1409
          OSHIFTS (O_SHAL,
1410
                   c = rd & hm; v = (rd & hm) != ((rd & (hm >> 1)) << 1); rd <<= 1,
1411
                   c = rd & (hm >> 1); v = (rd & (hm >> 1)) != ((rd & (hm >> 2)) << 2); rd <<= 2);
1412
          OSHIFTS (O_SHAR,
1413
                   t = rd & hm; c = rd & 1; v = 0; rd >>= 1; rd |= t,
1414
                   t = rd & hm; c = rd & 2; v = 0; rd >>= 2; rd |= t | t >> 1);
1415
          OSHIFTS (O_ROTL,
1416
                   c = rd & hm; v = 0; rd <<= 1; rd |= C,
1417
                   c = rd & hm; v = 0; rd <<= 1; rd |= C; c = rd & hm; rd <<= 1; rd |= C);
1418
          OSHIFTS (O_ROTR,
1419
                   c = rd & 1; v = 0; rd = (unsigned int) rd >> 1; if (c) rd |= hm,
1420
                   c = rd & 1; v = 0; rd = (unsigned int) rd >> 1; if (c) rd |= hm; c = rd & 1; rd = (unsigned int) rd >> 1; if (c) rd |= hm);
1421
          OSHIFTS (O_ROTXL,
1422
                   t = rd & hm; rd <<= 1; rd |= C; c = t; v = 0,
1423
                   t = rd & hm; rd <<= 1; rd |= C; c = t; v = 0; t = rd & hm; rd <<= 1; rd |= C; c = t);
1424
          OSHIFTS (O_ROTXR,
1425
                   t = rd & 1; rd = (unsigned int) rd >> 1; if (C) rd |= hm; c = t; v = 0,
1426
                   t = rd & 1; rd = (unsigned int) rd >> 1; if (C) rd |= hm; c = t; v = 0; t = rd & 1; rd = (unsigned int) rd >> 1; if (C) rd |= hm; c = t);
1427
 
1428
        case O (O_JMP, SB):
1429
          {
1430
            pc = fetch (&code->src);
1431
            goto end;
1432
 
1433
          }
1434
 
1435
        case O (O_JSR, SB):
1436
          {
1437
            int tmp;
1438
            pc = fetch (&code->src);
1439
          call:
1440
            tmp = cpu.regs[7];
1441
 
1442
            if (h8300hmode)
1443
              {
1444
                tmp -= 4;
1445
                SET_MEMORY_L (tmp, code->next_pc);
1446
              }
1447
            else
1448
              {
1449
                tmp -= 2;
1450
                SET_MEMORY_W (tmp, code->next_pc);
1451
              }
1452
            cpu.regs[7] = tmp;
1453
 
1454
            goto end;
1455
          }
1456
        case O (O_BSR, SB):
1457
          pc = code->src.literal;
1458
          goto call;
1459
 
1460
        case O (O_RTS, SN):
1461
          {
1462
            int tmp;
1463
 
1464
            tmp = cpu.regs[7];
1465
 
1466
            if (h8300hmode)
1467
              {
1468
                pc = GET_MEMORY_L (tmp);
1469
                tmp += 4;
1470
              }
1471
            else
1472
              {
1473
                pc = GET_MEMORY_W (tmp);
1474
                tmp += 2;
1475
              }
1476
 
1477
            cpu.regs[7] = tmp;
1478
            goto end;
1479
          }
1480
 
1481
        case O (O_ILL, SB):
1482
          cpu.state = SIM_STATE_STOPPED;
1483
          cpu.exception = SIGILL;
1484
          goto end;
1485
        case O (O_SLEEP, SN):
1486
          /* FIXME: Doesn't this break for breakpoints when r0
1487
             contains just the right (er, wrong) value?  */
1488
          cpu.state = SIM_STATE_STOPPED;
1489
          /* The format of r0 is defined by target newlib.  Expand
1490
             the macros here instead of looking for .../sys/wait.h.  */
1491
#define SIM_WIFEXITED(v) (((v) & 0xff) == 0)
1492
#define SIM_WIFSIGNALED(v) (((v) & 0x7f) > 0 && (((v) & 0x7f) < 0x7f))
1493
          if (! SIM_WIFEXITED (cpu.regs[0]) && SIM_WIFSIGNALED (cpu.regs[0]))
1494
            cpu.exception = SIGILL;
1495
          else
1496
            cpu.exception = SIGTRAP;
1497
          goto end;
1498
        case O (O_BPT, SN):
1499
          cpu.state = SIM_STATE_STOPPED;
1500
          cpu.exception = SIGTRAP;
1501
          goto end;
1502
 
1503
          OBITOP (O_BNOT, 1, 1, ea ^= m);
1504
          OBITOP (O_BTST, 1, 0, nz = ea & m);
1505
          OBITOP (O_BCLR, 1, 1, ea &= ~m);
1506
          OBITOP (O_BSET, 1, 1, ea |= m);
1507
          OBITOP (O_BLD, 1, 0, c = ea & m);
1508
          OBITOP (O_BILD, 1, 0, c = !(ea & m));
1509
          OBITOP (O_BST, 1, 1, ea &= ~m;
1510
                  if (C) ea |= m);
1511
          OBITOP (O_BIST, 1, 1, ea &= ~m;
1512
                  if (!C) ea |= m);
1513
          OBITOP (O_BAND, 1, 0, c = (ea & m) && C);
1514
          OBITOP (O_BIAND, 1, 0, c = !(ea & m) && C);
1515
          OBITOP (O_BOR, 1, 0, c = (ea & m) || C);
1516
          OBITOP (O_BIOR, 1, 0, c = !(ea & m) || C);
1517
          OBITOP (O_BXOR, 1, 0, c = (ea & m) != C);
1518
          OBITOP (O_BIXOR, 1, 0, c = !(ea & m) != C);
1519
 
1520
#define MOP(bsize, signed)                      \
1521
  mop (code, bsize, signed);                    \
1522
  goto next;
1523
 
1524
        case O (O_MULS, SB):
1525
          MOP (1, 1);
1526
          break;
1527
        case O (O_MULS, SW):
1528
          MOP (0, 1);
1529
          break;
1530
        case O (O_MULU, SB):
1531
          MOP (1, 0);
1532
          break;
1533
        case O (O_MULU, SW):
1534
          MOP (0, 0);
1535
          break;
1536
 
1537
        case O (O_TAS, SB):
1538
          if (!h8300smode || code->src.type != X (OP_REG, SL))
1539
            goto illegal;
1540
          switch (code->src.reg)
1541
            {
1542
            case R0_REGNUM:
1543
            case R1_REGNUM:
1544
            case R4_REGNUM:
1545
            case R5_REGNUM:
1546
              break;
1547
            default:
1548
              goto illegal;
1549
            }
1550
          res = fetch (&code->src);
1551
          store (&code->src,res|0x80);
1552
          goto just_flags_log8;
1553
 
1554
        case O (O_DIVU, SB):
1555
          {
1556
            rd = GET_W_REG (code->dst.reg);
1557
            ea = GET_B_REG (code->src.reg);
1558
            if (ea)
1559
              {
1560
                tmp = (unsigned) rd % ea;
1561
                rd = (unsigned) rd / ea;
1562
              }
1563
            SET_W_REG (code->dst.reg, (rd & 0xff) | (tmp << 8));
1564
            n = ea & 0x80;
1565
            nz = ea & 0xff;
1566
 
1567
            goto next;
1568
          }
1569
        case O (O_DIVU, SW):
1570
          {
1571
            rd = GET_L_REG (code->dst.reg);
1572
            ea = GET_W_REG (code->src.reg);
1573
            n = ea & 0x8000;
1574
            nz = ea & 0xffff;
1575
            if (ea)
1576
              {
1577
                tmp = (unsigned) rd % ea;
1578
                rd = (unsigned) rd / ea;
1579
              }
1580
            SET_L_REG (code->dst.reg, (rd & 0xffff) | (tmp << 16));
1581
            goto next;
1582
          }
1583
 
1584
        case O (O_DIVS, SB):
1585
          {
1586
 
1587
            rd = SEXTSHORT (GET_W_REG (code->dst.reg));
1588
            ea = SEXTCHAR (GET_B_REG (code->src.reg));
1589
            if (ea)
1590
              {
1591
                tmp = (int) rd % (int) ea;
1592
                rd = (int) rd / (int) ea;
1593
                n = rd & 0x8000;
1594
                nz = 1;
1595
              }
1596
            else
1597
              nz = 0;
1598
            SET_W_REG (code->dst.reg, (rd & 0xff) | (tmp << 8));
1599
            goto next;
1600
          }
1601
        case O (O_DIVS, SW):
1602
          {
1603
            rd = GET_L_REG (code->dst.reg);
1604
            ea = SEXTSHORT (GET_W_REG (code->src.reg));
1605
            if (ea)
1606
              {
1607
                tmp = (int) rd % (int) ea;
1608
                rd = (int) rd / (int) ea;
1609
                n = rd & 0x80000000;
1610
                nz = 1;
1611
              }
1612
            else
1613
              nz = 0;
1614
            SET_L_REG (code->dst.reg, (rd & 0xffff) | (tmp << 16));
1615
            goto next;
1616
          }
1617
        case O (O_EXTS, SW):
1618
          rd = GET_B_REG (code->src.reg + 8) & 0xff; /* Yes, src, not dst.  */
1619
          ea = rd & 0x80 ? -256 : 0;
1620
          res = rd + ea;
1621
          goto log16;
1622
        case O (O_EXTS, SL):
1623
          rd = GET_W_REG (code->src.reg) & 0xffff;
1624
          ea = rd & 0x8000 ? -65536 : 0;
1625
          res = rd + ea;
1626
          goto log32;
1627
        case O (O_EXTU, SW):
1628
          rd = GET_B_REG (code->src.reg + 8) & 0xff;
1629
          ea = 0;
1630
          res = rd + ea;
1631
          goto log16;
1632
        case O (O_EXTU, SL):
1633
          rd = GET_W_REG (code->src.reg) & 0xffff;
1634
          ea = 0;
1635
          res = rd + ea;
1636
          goto log32;
1637
 
1638
        case O (O_NOP, SN):
1639
          goto next;
1640
 
1641
        case O (O_STM, SL):
1642
          {
1643
            int nregs, firstreg, i;
1644
 
1645
            nregs = GET_MEMORY_B (pc + 1);
1646
            nregs >>= 4;
1647
            nregs &= 0xf;
1648
            firstreg = GET_MEMORY_B (pc + 3);
1649
            firstreg &= 0xf;
1650
            for (i = firstreg; i <= firstreg + nregs; i++)
1651
              {
1652
                cpu.regs[7] -= 4;
1653
                SET_MEMORY_L (cpu.regs[7], cpu.regs[i]);
1654
              }
1655
          }
1656
          goto next;
1657
 
1658
        case O (O_LDM, SL):
1659
          {
1660
            int nregs, firstreg, i;
1661
 
1662
            nregs = GET_MEMORY_B (pc + 1);
1663
            nregs >>= 4;
1664
            nregs &= 0xf;
1665
            firstreg = GET_MEMORY_B (pc + 3);
1666
            firstreg &= 0xf;
1667
            for (i = firstreg; i >= firstreg - nregs; i--)
1668
              {
1669
                cpu.regs[i] = GET_MEMORY_L (cpu.regs[7]);
1670
                cpu.regs[7] += 4;
1671
              }
1672
          }
1673
          goto next;
1674
 
1675
        default:
1676
        illegal:
1677
          cpu.state = SIM_STATE_STOPPED;
1678
          cpu.exception = SIGILL;
1679
          goto end;
1680
 
1681
        }
1682
      abort ();
1683
 
1684
    setc:
1685
      if (code->dst.type == OP_CCR)
1686
        {
1687
          cpu.ccr = res;
1688
          GETSR ();
1689
        }
1690
      else if (code->dst.type == OP_EXR && h8300smode)
1691
        {
1692
          cpu.exr = res;
1693
          GETEXR ();
1694
        }
1695
      else
1696
        goto illegal;
1697
 
1698
      goto next;
1699
 
1700
    condtrue:
1701
      /* When a branch works */
1702
      pc = code->src.literal;
1703
      goto end;
1704
 
1705
      /* Set the cond codes from res */
1706
    bitop:
1707
 
1708
      /* Set the flags after an 8 bit inc/dec operation */
1709
    just_flags_inc8:
1710
      n = res & 0x80;
1711
      nz = res & 0xff;
1712
      v = (rd & 0x7f) == 0x7f;
1713
      goto next;
1714
 
1715
 
1716
      /* Set the flags after an 16 bit inc/dec operation */
1717
    just_flags_inc16:
1718
      n = res & 0x8000;
1719
      nz = res & 0xffff;
1720
      v = (rd & 0x7fff) == 0x7fff;
1721
      goto next;
1722
 
1723
 
1724
      /* Set the flags after an 32 bit inc/dec operation */
1725
    just_flags_inc32:
1726
      n = res & 0x80000000;
1727
      nz = res & 0xffffffff;
1728
      v = (rd & 0x7fffffff) == 0x7fffffff;
1729
      goto next;
1730
 
1731
 
1732
    shift8:
1733
      /* Set flags after an 8 bit shift op, carry,overflow set in insn */
1734
      n = (rd & 0x80);
1735
      nz = rd & 0xff;
1736
      SET_B_REG (code->src.reg, rd);
1737
      goto next;
1738
 
1739
    shift16:
1740
      /* Set flags after an 16 bit shift op, carry,overflow set in insn */
1741
      n = (rd & 0x8000);
1742
      nz = rd & 0xffff;
1743
      SET_W_REG (code->src.reg, rd);
1744
      goto next;
1745
 
1746
    shift32:
1747
      /* Set flags after an 32 bit shift op, carry,overflow set in insn */
1748
      n = (rd & 0x80000000);
1749
      nz = rd & 0xffffffff;
1750
      SET_L_REG (code->src.reg, rd);
1751
      goto next;
1752
 
1753
    log32:
1754
      store (&code->dst, res);
1755
    just_flags_log32:
1756
      /* flags after a 32bit logical operation */
1757
      n = res & 0x80000000;
1758
      nz = res & 0xffffffff;
1759
      v = 0;
1760
      goto next;
1761
 
1762
    log16:
1763
      store (&code->dst, res);
1764
    just_flags_log16:
1765
      /* flags after a 16bit logical operation */
1766
      n = res & 0x8000;
1767
      nz = res & 0xffff;
1768
      v = 0;
1769
      goto next;
1770
 
1771
 
1772
    log8:
1773
      store (&code->dst, res);
1774
    just_flags_log8:
1775
      n = res & 0x80;
1776
      nz = res & 0xff;
1777
      v = 0;
1778
      goto next;
1779
 
1780
    alu8:
1781
      SET_B_REG (code->dst.reg, res);
1782
    just_flags_alu8:
1783
      n = res & 0x80;
1784
      nz = res & 0xff;
1785
      c = (res & 0x100);
1786
      switch (code->opcode / 4)
1787
        {
1788
        case O_ADD:
1789
          v = ((rd & 0x80) == (ea & 0x80)
1790
               && (rd & 0x80) != (res & 0x80));
1791
          break;
1792
        case O_SUB:
1793
        case O_CMP:
1794
          v = ((rd & 0x80) != (-ea & 0x80)
1795
               && (rd & 0x80) != (res & 0x80));
1796
          break;
1797
        case O_NEG:
1798
          v = (rd == 0x80);
1799
          break;
1800
        }
1801
      goto next;
1802
 
1803
    alu16:
1804
      SET_W_REG (code->dst.reg, res);
1805
    just_flags_alu16:
1806
      n = res & 0x8000;
1807
      nz = res & 0xffff;
1808
      c = (res & 0x10000);
1809
      switch (code->opcode / 4)
1810
        {
1811
        case O_ADD:
1812
          v = ((rd & 0x8000) == (ea & 0x8000)
1813
               && (rd & 0x8000) != (res & 0x8000));
1814
          break;
1815
        case O_SUB:
1816
        case O_CMP:
1817
          v = ((rd & 0x8000) != (-ea & 0x8000)
1818
               && (rd & 0x8000) != (res & 0x8000));
1819
          break;
1820
        case O_NEG:
1821
          v = (rd == 0x8000);
1822
          break;
1823
        }
1824
      goto next;
1825
 
1826
    alu32:
1827
      SET_L_REG (code->dst.reg, res);
1828
    just_flags_alu32:
1829
      n = res & 0x80000000;
1830
      nz = res & 0xffffffff;
1831
      switch (code->opcode / 4)
1832
        {
1833
        case O_ADD:
1834
          v = ((rd & 0x80000000) == (ea & 0x80000000)
1835
               && (rd & 0x80000000) != (res & 0x80000000));
1836
          c = ((unsigned) res < (unsigned) rd) || ((unsigned) res < (unsigned) ea);
1837
          break;
1838
        case O_SUB:
1839
        case O_CMP:
1840
          v = ((rd & 0x80000000) != (-ea & 0x80000000)
1841
               && (rd & 0x80000000) != (res & 0x80000000));
1842
          c = (unsigned) rd < (unsigned) -ea;
1843
          break;
1844
        case O_NEG:
1845
          v = (rd == 0x80000000);
1846
          c = res != 0;
1847
          break;
1848
        }
1849
      goto next;
1850
 
1851
    next:;
1852
      pc = code->next_pc;
1853
 
1854
    end:
1855
      ;
1856
#if 0
1857
      if (cpu.regs[8])
1858
        abort ();
1859
#endif
1860
 
1861
      if (--poll_count < 0)
1862
        {
1863
          poll_count = POLL_QUIT_INTERVAL;
1864
          if ((*sim_callback->poll_quit) != NULL
1865
              && (*sim_callback->poll_quit) (sim_callback))
1866
            sim_stop (sd);
1867
        }
1868
 
1869
    }
1870
  while (cpu.state == SIM_STATE_RUNNING);
1871
  cpu.ticks += get_now () - tick_start;
1872
  cpu.cycles += cycles;
1873
  cpu.insts += insts;
1874
 
1875
  cpu.pc = pc;
1876
  BUILDSR ();
1877
  BUILDEXR ();
1878
  cpu.mask = oldmask;
1879
  signal (SIGINT, prev);
1880
}
1881
 
1882
int
1883
sim_trace (sd)
1884
     SIM_DESC sd;
1885
{
1886
  /* FIXME: Unfinished.  */
1887
  abort ();
1888
}
1889
 
1890
int
1891
sim_write (sd, addr, buffer, size)
1892
     SIM_DESC sd;
1893
     SIM_ADDR addr;
1894
     unsigned char *buffer;
1895
     int size;
1896
{
1897
  int i;
1898
 
1899
  init_pointers ();
1900
  if (addr < 0)
1901
    return 0;
1902
  for (i = 0; i < size; i++)
1903
    {
1904
      if (addr < memory_size)
1905
        {
1906
          cpu.memory[addr + i] = buffer[i];
1907
          cpu.cache_idx[addr + i] = 0;
1908
        }
1909
      else
1910
        cpu.eightbit[(addr + i) & 0xff] = buffer[i];
1911
    }
1912
  return size;
1913
}
1914
 
1915
int
1916
sim_read (sd, addr, buffer, size)
1917
     SIM_DESC sd;
1918
     SIM_ADDR addr;
1919
     unsigned char *buffer;
1920
     int size;
1921
{
1922
  init_pointers ();
1923
  if (addr < 0)
1924
    return 0;
1925
  if (addr < memory_size)
1926
    memcpy (buffer, cpu.memory + addr, size);
1927
  else
1928
    memcpy (buffer, cpu.eightbit + (addr & 0xff), size);
1929
  return size;
1930
}
1931
 
1932
 
1933
int
1934
sim_store_register (sd, rn, value, length)
1935
     SIM_DESC sd;
1936
     int rn;
1937
     unsigned char *value;
1938
     int length;
1939
{
1940
  int longval;
1941
  int shortval;
1942
  int intval;
1943
  longval = (value[0] << 24) | (value[1] << 16) | (value[2] << 8) | value[3];
1944
  shortval = (value[0] << 8) | (value[1]);
1945
  intval = h8300hmode ? longval : shortval;
1946
 
1947
  init_pointers ();
1948
  switch (rn)
1949
    {
1950
    case PC_REGNUM:
1951
      cpu.pc = intval;
1952
      break;
1953
    default:
1954
      abort ();
1955
    case R0_REGNUM:
1956
    case R1_REGNUM:
1957
    case R2_REGNUM:
1958
    case R3_REGNUM:
1959
    case R4_REGNUM:
1960
    case R5_REGNUM:
1961
    case R6_REGNUM:
1962
    case R7_REGNUM:
1963
      cpu.regs[rn] = intval;
1964
      break;
1965
    case CCR_REGNUM:
1966
      cpu.ccr = intval;
1967
      break;
1968
    case EXR_REGNUM:
1969
      cpu.exr = intval;
1970
      break;
1971
    case CYCLE_REGNUM:
1972
      cpu.cycles = longval;
1973
      break;
1974
 
1975
    case INST_REGNUM:
1976
      cpu.insts = longval;
1977
      break;
1978
 
1979
    case TICK_REGNUM:
1980
      cpu.ticks = longval;
1981
      break;
1982
    }
1983
  return -1;
1984
}
1985
 
1986
int
1987
sim_fetch_register (sd, rn, buf, length)
1988
     SIM_DESC sd;
1989
     int rn;
1990
     unsigned char *buf;
1991
     int length;
1992
{
1993
  int v;
1994
  int longreg = 0;
1995
 
1996
  init_pointers ();
1997
 
1998
  if (!h8300smode && rn >=EXR_REGNUM)
1999
    rn++;
2000
  switch (rn)
2001
    {
2002
    default:
2003
      abort ();
2004
    case CCR_REGNUM:
2005
      v = cpu.ccr;
2006
      break;
2007
    case EXR_REGNUM:
2008
      v = cpu.exr;
2009
      break;
2010
    case PC_REGNUM:
2011
      v = cpu.pc;
2012
      break;
2013
    case R0_REGNUM:
2014
    case R1_REGNUM:
2015
    case R2_REGNUM:
2016
    case R3_REGNUM:
2017
    case R4_REGNUM:
2018
    case R5_REGNUM:
2019
    case R6_REGNUM:
2020
    case R7_REGNUM:
2021
      v = cpu.regs[rn];
2022
      break;
2023
    case CYCLE_REGNUM:
2024
      v = cpu.cycles;
2025
      longreg = 1;
2026
      break;
2027
    case TICK_REGNUM:
2028
      v = cpu.ticks;
2029
      longreg = 1;
2030
      break;
2031
    case INST_REGNUM:
2032
      v = cpu.insts;
2033
      longreg = 1;
2034
      break;
2035
    }
2036
  if (h8300hmode || longreg)
2037
    {
2038
      buf[0] = v >> 24;
2039
      buf[1] = v >> 16;
2040
      buf[2] = v >> 8;
2041
      buf[3] = v >> 0;
2042
    }
2043
  else
2044
    {
2045
      buf[0] = v >> 8;
2046
      buf[1] = v;
2047
    }
2048
  return -1;
2049
}
2050
 
2051
void
2052
sim_stop_reason (sd, reason, sigrc)
2053
     SIM_DESC sd;
2054
     enum sim_stop *reason;
2055
     int *sigrc;
2056
{
2057
#if 0 /* FIXME: This should work but we can't use it.
2058
         grep for SLEEP above.  */
2059
  switch (cpu.state)
2060
    {
2061
    case SIM_STATE_EXITED : *reason = sim_exited; break;
2062
    case SIM_STATE_SIGNALLED : *reason = sim_signalled; break;
2063
    case SIM_STATE_STOPPED : *reason = sim_stopped; break;
2064
    default : abort ();
2065
    }
2066
#else
2067
  *reason = sim_stopped;
2068
#endif
2069
  *sigrc = cpu.exception;
2070
}
2071
 
2072
/* FIXME: Rename to sim_set_mem_size.  */
2073
 
2074
void
2075
sim_size (n)
2076
     int n;
2077
{
2078
  /* Memory size is fixed.  */
2079
}
2080
 
2081
void
2082
sim_set_simcache_size (n)
2083
{
2084
  if (cpu.cache)
2085
    free (cpu.cache);
2086
  if (n < 2)
2087
    n = 2;
2088
  cpu.cache = (decoded_inst *) malloc (sizeof (decoded_inst) * n);
2089
  memset (cpu.cache, 0, sizeof (decoded_inst) * n);
2090
  cpu.csize = n;
2091
}
2092
 
2093
 
2094
void
2095
sim_info (sd, verbose)
2096
     SIM_DESC sd;
2097
     int verbose;
2098
{
2099
  double timetaken = (double) cpu.ticks / (double) now_persec ();
2100
  double virttime = cpu.cycles / 10.0e6;
2101
 
2102
  (*sim_callback->printf_filtered) (sim_callback,
2103
                                    "\n\n#instructions executed  %10d\n",
2104
                                    cpu.insts);
2105
  (*sim_callback->printf_filtered) (sim_callback,
2106
                                    "#cycles (v approximate) %10d\n",
2107
                                    cpu.cycles);
2108
  (*sim_callback->printf_filtered) (sim_callback,
2109
                                    "#real time taken        %10.4f\n",
2110
                                    timetaken);
2111
  (*sim_callback->printf_filtered) (sim_callback,
2112
                                    "#virtual time taked     %10.4f\n",
2113
                                    virttime);
2114
  if (timetaken != 0.0)
2115
    (*sim_callback->printf_filtered) (sim_callback,
2116
                                      "#simulation ratio       %10.4f\n",
2117
                                      virttime / timetaken);
2118
  (*sim_callback->printf_filtered) (sim_callback,
2119
                                    "#compiles               %10d\n",
2120
                                    cpu.compiles);
2121
  (*sim_callback->printf_filtered) (sim_callback,
2122
                                    "#cache size             %10d\n",
2123
                                    cpu.csize);
2124
 
2125
#ifdef ADEBUG
2126
  /* This to be conditional on `what' (aka `verbose'),
2127
     however it was never passed as non-zero.  */
2128
  if (1)
2129
    {
2130
      int i;
2131
      for (i = 0; i < O_LAST; i++)
2132
        {
2133
          if (cpu.stats[i])
2134
            (*sim_callback->printf_filtered) (sim_callback,
2135
                                              "%d: %d\n", i, cpu.stats[i]);
2136
        }
2137
    }
2138
#endif
2139
}
2140
 
2141
/* Indicate whether the cpu is an H8/300 or H8/300H.
2142
   FLAG is non-zero for the H8/300H.  */
2143
 
2144
void
2145
set_h8300h (h_flag, s_flag)
2146
     int h_flag, s_flag;
2147
{
2148
  /* FIXME: Much of the code in sim_load can be moved to sim_open.
2149
     This function being replaced by a sim_open:ARGV configuration
2150
     option.  */
2151
  h8300hmode = h_flag;
2152
  h8300smode = s_flag;
2153
}
2154
 
2155
SIM_DESC
2156
sim_open (kind, ptr, abfd, argv)
2157
     SIM_OPEN_KIND kind;
2158
     struct host_callback_struct *ptr;
2159
     struct _bfd *abfd;
2160
     char **argv;
2161
{
2162
  /* FIXME: Much of the code in sim_load can be moved here.  */
2163
 
2164
  sim_kind = kind;
2165
  myname = argv[0];
2166
  sim_callback = ptr;
2167
  /* Fudge our descriptor.  */
2168
  return (SIM_DESC) 1;
2169
}
2170
 
2171
void
2172
sim_close (sd, quitting)
2173
     SIM_DESC sd;
2174
     int quitting;
2175
{
2176
  /* Nothing to do.  */
2177
}
2178
 
2179
/* Called by gdb to load a program into memory.  */
2180
 
2181
SIM_RC
2182
sim_load (sd, prog, abfd, from_tty)
2183
     SIM_DESC sd;
2184
     char *prog;
2185
     bfd *abfd;
2186
     int from_tty;
2187
{
2188
  bfd *prog_bfd;
2189
 
2190
  /* FIXME: The code below that sets a specific variant of the H8/300
2191
     being simulated should be moved to sim_open().  */
2192
 
2193
  /* See if the file is for the H8/300 or H8/300H.  */
2194
  /* ??? This may not be the most efficient way.  The z8k simulator
2195
     does this via a different mechanism (INIT_EXTRA_SYMTAB_INFO).  */
2196
  if (abfd != NULL)
2197
    prog_bfd = abfd;
2198
  else
2199
    prog_bfd = bfd_openr (prog, "coff-h8300");
2200
  if (prog_bfd != NULL)
2201
    {
2202
      /* Set the cpu type.  We ignore failure from bfd_check_format
2203
         and bfd_openr as sim_load_file checks too.  */
2204
      if (bfd_check_format (prog_bfd, bfd_object))
2205
        {
2206
          unsigned long mach = bfd_get_mach (prog_bfd);
2207
          set_h8300h (mach == bfd_mach_h8300h || mach == bfd_mach_h8300s,
2208
                      mach == bfd_mach_h8300s);
2209
        }
2210
    }
2211
 
2212
  /* If we're using gdb attached to the simulator, then we have to
2213
     reallocate memory for the simulator.
2214
 
2215
     When gdb first starts, it calls fetch_registers (among other
2216
     functions), which in turn calls init_pointers, which allocates
2217
     simulator memory.
2218
 
2219
     The problem is when we do that, we don't know whether we're
2220
     debugging an H8/300 or H8/300H program.
2221
 
2222
     This is the first point at which we can make that determination,
2223
     so we just reallocate memory now; this will also allow us to handle
2224
     switching between H8/300 and H8/300H programs without exiting
2225
     gdb.  */
2226
 
2227
  if (h8300smode)
2228
    memory_size = H8300S_MSIZE;
2229
  else if (h8300hmode)
2230
    memory_size = H8300H_MSIZE;
2231
  else
2232
    memory_size = H8300_MSIZE;
2233
 
2234
  if (cpu.memory)
2235
    free (cpu.memory);
2236
  if (cpu.cache_idx)
2237
    free (cpu.cache_idx);
2238
  if (cpu.eightbit)
2239
    free (cpu.eightbit);
2240
 
2241
  cpu.memory = (unsigned char *) calloc (sizeof (char), memory_size);
2242
  cpu.cache_idx = (unsigned short *) calloc (sizeof (short), memory_size);
2243
  cpu.eightbit = (unsigned char *) calloc (sizeof (char), 256);
2244
 
2245
  /* `msize' must be a power of two.  */
2246
  if ((memory_size & (memory_size - 1)) != 0)
2247
    abort ();
2248
  cpu.mask = memory_size - 1;
2249
 
2250
  if (sim_load_file (sd, myname, sim_callback, prog, prog_bfd,
2251
                     sim_kind == SIM_OPEN_DEBUG,
2252
                     0, sim_write)
2253
      == NULL)
2254
    {
2255
      /* Close the bfd if we opened it.  */
2256
      if (abfd == NULL && prog_bfd != NULL)
2257
        bfd_close (prog_bfd);
2258
      return SIM_RC_FAIL;
2259
    }
2260
 
2261
  /* Close the bfd if we opened it.  */
2262
  if (abfd == NULL && prog_bfd != NULL)
2263
    bfd_close (prog_bfd);
2264
  return SIM_RC_OK;
2265
}
2266
 
2267
SIM_RC
2268
sim_create_inferior (sd, abfd, argv, env)
2269
     SIM_DESC sd;
2270
     struct _bfd *abfd;
2271
     char **argv;
2272
     char **env;
2273
{
2274
  if (abfd != NULL)
2275
    cpu.pc = bfd_get_start_address (abfd);
2276
  else
2277
    cpu.pc = 0;
2278
  return SIM_RC_OK;
2279
}
2280
 
2281
void
2282
sim_do_command (sd, cmd)
2283
     SIM_DESC sd;
2284
     char *cmd;
2285
{
2286
  (*sim_callback->printf_filtered) (sim_callback,
2287
                                    "This simulator does not accept any commands.\n");
2288
}
2289
 
2290
void
2291
sim_set_callbacks (ptr)
2292
     struct host_callback_struct *ptr;
2293
{
2294
  sim_callback = ptr;
2295
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.