Commit | Line | Data |
---|---|---|
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" | |
290ad14a | 26 | #include "cpu.h" |
28816f45 | 27 | #include "options.h" |
4f35cbff | 28 | |
c494cadd MM |
29 | #ifdef HAVE_STDLIB_H |
30 | #include <stdlib.h> | |
31 | #endif | |
32 | ||
73c4941b MM |
33 | #ifdef HAVE_UNISTD_H |
34 | #include <unistd.h> | |
35 | #endif | |
36 | ||
c494cadd MM |
37 | #ifdef HAVE_STRING_H |
38 | #include <string.h> | |
39 | #else | |
40 | #ifdef HAVE_STRINGS_H | |
41 | #include <strings.h> | |
42 | #endif | |
43 | #endif | |
44 | ||
4f35cbff MM |
45 | extern char **environ; |
46 | extern char *optarg; | |
47 | extern int optind; | |
48 | extern int optopt; | |
49 | extern int opterr; | |
50 | ||
51 | void | |
d8d46596 | 52 | printf_filtered(const char *msg, ...) |
4f35cbff MM |
53 | { |
54 | va_list ap; | |
55 | va_start(ap, msg); | |
56 | vprintf(msg, ap); | |
22ddef46 | 57 | va_end(ap); |
4f35cbff MM |
58 | } |
59 | ||
60 | void | |
61 | error (char *msg, ...) | |
62 | { | |
63 | va_list ap; | |
64 | va_start(ap, msg); | |
65 | vprintf(msg, ap); | |
22ddef46 | 66 | va_end(ap); |
4f35cbff MM |
67 | exit (1); |
68 | } | |
69 | ||
70 | void * | |
71 | zalloc(long size) | |
72 | { | |
73 | void *memory = malloc(size); | |
74 | if (memory == NULL) | |
75 | error("zmalloc failed\n"); | |
1dc7c0ed | 76 | memset(memory, 0, size); |
4f35cbff MM |
77 | return memory; |
78 | } | |
79 | ||
80 | void | |
81 | zfree(void *chunk) | |
82 | { | |
83 | free(chunk); | |
84 | } | |
85 | ||
86 | static void | |
87 | usage(void) | |
88 | { | |
73c4941b | 89 | printf_filtered("Usage:\n\tpsim [ -t <trace-option> ] [-m model] [-i] [-I] <image> [ <image-args> ... ]\n"); |
a983c8f0 MM |
90 | trace_usage(); |
91 | error(""); | |
4f35cbff MM |
92 | } |
93 | ||
94 | int | |
95 | main(int argc, char **argv) | |
96 | { | |
97 | psim *system; | |
4f35cbff MM |
98 | const char *name_of_file; |
99 | char *arg_; | |
4f35cbff MM |
100 | psim_status status; |
101 | int letter; | |
83d96c6e | 102 | int print_info = 0; |
4f35cbff | 103 | |
8eab189b MM |
104 | /* check for arguments -- note sim_calls.c also contains argument processing |
105 | code for the simulator linked within gdb. */ | |
73c4941b | 106 | while ((letter = getopt (argc, argv, "Iim:t:")) != EOF) |
4f35cbff | 107 | { |
d8d46596 | 108 | switch (letter) { |
a983c8f0 MM |
109 | case 't': |
110 | trace_option(optarg); | |
4f35cbff | 111 | break; |
73c4941b | 112 | case 'm': |
28816f45 | 113 | model_set(optarg); |
73c4941b MM |
114 | break; |
115 | case 'i': | |
83d96c6e MM |
116 | print_info = 1; |
117 | break; | |
73c4941b | 118 | case 'I': |
290ad14a | 119 | current_model_issue = MODEL_ISSUE_PROCESS; |
73c4941b MM |
120 | print_info = 2; |
121 | break; | |
4f35cbff MM |
122 | default: |
123 | usage(); | |
124 | } | |
125 | } | |
d8d46596 | 126 | if (optind >= argc) |
4f35cbff | 127 | usage(); |
d8d46596 | 128 | name_of_file = argv[optind]; |
4f35cbff | 129 | |
28816f45 MM |
130 | if (ppc_trace[trace_opts]) |
131 | print_options (); | |
132 | ||
4f35cbff | 133 | /* create the simulator */ |
a983c8f0 | 134 | system = psim_create(name_of_file); |
4f35cbff MM |
135 | |
136 | /* fudge the environment so that _=prog-name */ | |
d8d46596 | 137 | arg_ = (char*)zalloc(strlen(argv[optind]) + strlen("_=") + 1); |
4f35cbff | 138 | strcpy(arg_, "_="); |
d8d46596 | 139 | strcat(arg_, argv[optind]); |
4f35cbff MM |
140 | putenv(arg_); |
141 | ||
142 | /* initialize it */ | |
22ddef46 | 143 | psim_init(system); |
d8d46596 | 144 | psim_stack(system, &argv[optind], environ); |
4f35cbff MM |
145 | |
146 | psim_run(system); | |
147 | ||
22ddef46 | 148 | /* any final clean up */ |
83d96c6e | 149 | if (print_info) |
73c4941b | 150 | psim_print_info (system, print_info); |
83d96c6e | 151 | |
4f35cbff MM |
152 | /* why did we stop */ |
153 | status = psim_get_status(system); | |
154 | switch (status.reason) { | |
155 | case was_continuing: | |
156 | error("psim: continuing while stoped!\n"); | |
157 | return 0; | |
158 | case was_trap: | |
159 | error("psim: no trap insn\n"); | |
160 | return 0; | |
161 | case was_exited: | |
162 | return status.signal; | |
163 | case was_signalled: | |
22ddef46 | 164 | printf ("%s: Caught signal %d at address 0x%lx\n", |
a983c8f0 MM |
165 | name_of_file, (int)status.signal, |
166 | (long)status.program_counter); | |
4f35cbff MM |
167 | return status.signal; |
168 | default: | |
169 | error("unknown halt condition\n"); | |
170 | return 0; | |
171 | } | |
172 | } |