a8195694f04a762dac3eb367c6264a4a18ef39ac
[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     set_h8300h (1);
94
95   for (s = abfd->sections; s; s=s->next) 
96     {
97       char *buffer = malloc(bfd_section_size(abfd,s));
98       bfd_get_section_contents(abfd, s, buffer, 0, bfd_section_size(abfd,s));
99       sim_write(s->vma, buffer, bfd_section_size(abfd,s));
100     }
101
102   start_address = bfd_get_start_address(abfd);
103   sim_create_inferior (start_address, NULL, NULL);
104   sim_resume(0,0);
105   if (verbose)
106     sim_info (verbose - 1);
107   sim_stop_reason (&reason, &sigrc);
108   /* FIXME: this test is insufficient but we can't do much
109      about it until sim_stop_reason is cleaned up.  */
110   if (sigrc == SIGILL)
111     abort ();
112   return 0;
113 }
114
115 /* gdb callback used by simulator */
116
117 void
118 printf_filtered (va_alist)
119      va_dcl
120 {
121   char *msg;
122   va_list args;
123
124   va_start (args);
125   msg = va_arg (args, char *);
126   vfprintf (stdout, msg, args);
127   va_end (args);
128 }
129
130 void
131 usage()
132 {
133   fprintf (stderr, "usage: run [-h] [-t] [-v] [-c csize] program\n");
134   exit (1);
135 }