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