first stage in function unit support; add new switches & latest code from andrew
[deliverable/binutils-gdb.git] / sim / ppc / main.c
CommitLineData
4f35cbff
MM
1/* This file is part of the program psim.
2
3 Copyright (C) 1994-1995, Andrew Cagney <cagney@highland.com.au>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19 */
20
21
22#include <stdarg.h>
4f35cbff 23#include <stdio.h>
4f35cbff
MM
24
25#include "psim.h"
73c4941b 26#include "function_unit.h"
4f35cbff 27
c494cadd
MM
28#ifdef HAVE_STDLIB_H
29#include <stdlib.h>
30#endif
31
73c4941b
MM
32#ifdef HAVE_UNISTD_H
33#include <unistd.h>
34#endif
35
c494cadd
MM
36#ifdef HAVE_STRING_H
37#include <string.h>
38#else
39#ifdef HAVE_STRINGS_H
40#include <strings.h>
41#endif
42#endif
43
4f35cbff
MM
44extern char **environ;
45extern char *optarg;
46extern int optind;
47extern int optopt;
48extern int opterr;
49
50void
d8d46596 51printf_filtered(const char *msg, ...)
4f35cbff
MM
52{
53 va_list ap;
54 va_start(ap, msg);
55 vprintf(msg, ap);
22ddef46 56 va_end(ap);
4f35cbff
MM
57}
58
59void
60error (char *msg, ...)
61{
62 va_list ap;
63 va_start(ap, msg);
64 vprintf(msg, ap);
22ddef46 65 va_end(ap);
4f35cbff
MM
66 exit (1);
67}
68
69void *
70zalloc(long size)
71{
72 void *memory = malloc(size);
73 if (memory == NULL)
74 error("zmalloc failed\n");
75 bzero(memory, size);
76 return memory;
77}
78
79void
80zfree(void *chunk)
81{
82 free(chunk);
83}
84
85static void
86usage(void)
87{
73c4941b 88 printf_filtered("Usage:\n\tpsim [ -t <trace-option> ] [-m model] [-i] [-I] <image> [ <image-args> ... ]\n");
a983c8f0
MM
89 trace_usage();
90 error("");
4f35cbff
MM
91}
92
93int
94main(int argc, char **argv)
95{
96 psim *system;
4f35cbff
MM
97 const char *name_of_file;
98 char *arg_;
4f35cbff
MM
99 psim_status status;
100 int letter;
83d96c6e 101 int print_info = 0;
4f35cbff 102
8eab189b
MM
103 /* check for arguments -- note sim_calls.c also contains argument processing
104 code for the simulator linked within gdb. */
73c4941b 105 while ((letter = getopt (argc, argv, "Iim:t:")) != EOF)
4f35cbff 106 {
d8d46596 107 switch (letter) {
a983c8f0
MM
108 case 't':
109 trace_option(optarg);
4f35cbff 110 break;
73c4941b
MM
111 case 'm':
112 function_unit_model(optarg);
113 break;
114 case 'i':
83d96c6e
MM
115 print_info = 1;
116 break;
73c4941b
MM
117 case 'I':
118 print_info = 2;
119 break;
4f35cbff
MM
120 default:
121 usage();
122 }
123 }
d8d46596 124 if (optind >= argc)
4f35cbff 125 usage();
d8d46596 126 name_of_file = argv[optind];
4f35cbff
MM
127
128 /* create the simulator */
a983c8f0 129 system = psim_create(name_of_file);
4f35cbff
MM
130
131 /* fudge the environment so that _=prog-name */
d8d46596 132 arg_ = (char*)zalloc(strlen(argv[optind]) + strlen("_=") + 1);
4f35cbff 133 strcpy(arg_, "_=");
d8d46596 134 strcat(arg_, argv[optind]);
4f35cbff
MM
135 putenv(arg_);
136
137 /* initialize it */
22ddef46 138 psim_init(system);
d8d46596 139 psim_stack(system, &argv[optind], environ);
4f35cbff
MM
140
141 psim_run(system);
142
22ddef46 143 /* any final clean up */
83d96c6e 144 if (print_info)
73c4941b 145 psim_print_info (system, print_info);
83d96c6e 146
4f35cbff
MM
147 /* why did we stop */
148 status = psim_get_status(system);
149 switch (status.reason) {
150 case was_continuing:
151 error("psim: continuing while stoped!\n");
152 return 0;
153 case was_trap:
154 error("psim: no trap insn\n");
155 return 0;
156 case was_exited:
157 return status.signal;
158 case was_signalled:
22ddef46 159 printf ("%s: Caught signal %d at address 0x%lx\n",
a983c8f0
MM
160 name_of_file, (int)status.signal,
161 (long)status.program_counter);
4f35cbff
MM
162 return status.signal;
163 default:
164 error("unknown halt condition\n");
165 return 0;
166 }
167}
This page took 0.03626 seconds and 4 git commands to generate.