* compile.c (sim_load): Treat the H8/S like the H8/300H for now.
[platform/upstream/binutils.git] / sim / h8300 / run.c
1 /* front end to the simulator.
2
3    Written by Steve Chamberlain of Cygnus Support.
4    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
12    makes no warranty with regard to the software or it's performance
13    and the user accepts the software "AS IS" with all faults.
14
15    CYGNUS DISCLAIMS ANY WARRANTIES, EXPRESS OR IMPLIED, WITH REGARD TO
16    THIS SOFTWARE INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17    MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18
19 */
20
21 #include "config.h"
22
23 #include <varargs.h>
24 #include <stdio.h>
25 #include <signal.h>
26 #ifdef HAVE_STDLIB_H
27 #include <stdlib.h>
28 #endif
29 #include "getopt.h"
30 #include "bfd.h"
31 #include "remote-sim.h"
32
33 void usage();
34 extern int optind;
35 extern char *optarg;
36
37 int
38 main (ac, av)
39      int ac;
40      char **av;
41 {
42   bfd *abfd;
43   bfd_vma start_address;
44   asection *s;
45   int i;
46   int verbose = 0;
47   int trace = 0;
48   char *name = "";
49   int sigrc;
50   enum sim_stop reason;
51
52   while ((i = getopt (ac, av, "c:htv")) != EOF)
53     switch (i) 
54       {
55       case 'c':
56         sim_csize (atoi (optarg));
57         break;
58       case 'h':
59         set_h8300h (1);
60         break;
61       case 't':
62         trace = 1;
63         break;
64       case 'v':
65         verbose = 1;
66         break;
67       default:
68         usage();
69       }
70
71   if (ac - optind != 1)
72     usage();
73
74   name = av[ac - 1];
75
76   if (verbose)
77     printf ("run %s\n", name);
78
79   abfd = bfd_openr (name, "coff-h8300");
80   if (! abfd)
81     {
82       fprintf (stderr, "%s: unable to open %s\n", av[0], name);
83       exit (1);
84     }
85
86   if (! bfd_check_format(abfd, bfd_object)) 
87     {
88       fprintf (stderr, "%s: %s is not a valid executable\n", av[0], name);
89       exit (1);
90     }
91
92   if (abfd->arch_info->mach == bfd_mach_h8300h
93 /* start-sanitize-h8s */
94       || abfd->arch_info->mach == bfd_mach_h8300s
95 /* end-sanitize-h8s */
96       )
97     set_h8300h (1);
98
99   for (s = abfd->sections; s; s=s->next) 
100     {
101       char *buffer = malloc(bfd_section_size(abfd,s));
102       bfd_get_section_contents(abfd, s, buffer, 0, bfd_section_size(abfd,s));
103       sim_write(s->vma, buffer, bfd_section_size(abfd,s));
104     }
105
106   start_address = bfd_get_start_address(abfd);
107   sim_create_inferior (start_address, NULL, NULL);
108   sim_resume(0,0);
109   if (verbose)
110     sim_info (verbose - 1);
111   sim_stop_reason (&reason, &sigrc);
112   /* FIXME: this test is insufficient but we can't do much
113      about it until sim_stop_reason is cleaned up.  */
114   if (sigrc == SIGILL)
115     abort ();
116   return 0;
117 }
118
119 /* gdb callback used by simulator */
120
121 void
122 printf_filtered (va_alist)
123      va_dcl
124 {
125   char *msg;
126   va_list args;
127
128   va_start (args);
129   msg = va_arg (args, char *);
130   vfprintf (stdout, msg, args);
131   va_end (args);
132 }
133
134 void
135 usage()
136 {
137   fprintf (stderr, "usage: run [-h] [-t] [-v] [-c csize] program\n");
138   exit (1);
139 }