main.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /* This file is part of the program psim.
  2. Copyright (C) 1994-1997, Andrew Cagney <cagney@highland.com.au>
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /* This must come before any other includes. */
  15. #include "defs.h"
  16. #include <stdarg.h>
  17. #include <stdio.h>
  18. #include <fcntl.h>
  19. #include <signal.h>
  20. #include "psim.h"
  21. #include "options.h"
  22. #include "device.h" /* FIXME: psim should provide the interface */
  23. #include "events.h" /* FIXME: psim should provide the interface */
  24. #include "bfd.h"
  25. #include "sim/callback.h"
  26. #include "sim/sim.h"
  27. #include <stdlib.h>
  28. #ifdef HAVE_UNISTD_H
  29. #include <unistd.h>
  30. #endif
  31. #include <string.h>
  32. #include <errno.h>
  33. #include "environ.h"
  34. #if !defined(O_NONBLOCK) || !defined(F_GETFL) || !defined(F_SETFL)
  35. #undef WITH_STDIO
  36. #define WITH_STDIO DO_USE_STDIO
  37. #endif
  38. static psim *simulation = NULL;
  39. void
  40. sim_io_poll_quit (void)
  41. {
  42. /* nothing to do */
  43. }
  44. void
  45. sim_io_printf_filtered(const char *msg, ...)
  46. {
  47. va_list ap;
  48. va_start(ap, msg);
  49. vprintf(msg, ap);
  50. va_end(ap);
  51. }
  52. void
  53. error (const char *msg, ...)
  54. {
  55. va_list ap;
  56. va_start(ap, msg);
  57. vprintf(msg, ap);
  58. printf("\n");
  59. va_end(ap);
  60. /* any final clean up */
  61. if (ppc_trace[trace_print_info] && simulation != NULL)
  62. psim_print_info (simulation, ppc_trace[trace_print_info]);
  63. exit (1);
  64. }
  65. int
  66. sim_io_write_stdout(const char *buf,
  67. int sizeof_buf)
  68. {
  69. switch (CURRENT_STDIO) {
  70. case DO_USE_STDIO:
  71. {
  72. int i;
  73. for (i = 0; i < sizeof_buf; i++) {
  74. putchar(buf[i]);
  75. }
  76. return i;
  77. }
  78. break;
  79. case DONT_USE_STDIO:
  80. return write(1, buf, sizeof_buf);
  81. break;
  82. default:
  83. error("sim_io_write_stdout: invalid switch\n");
  84. }
  85. return 0;
  86. }
  87. int
  88. sim_io_write_stderr(const char *buf,
  89. int sizeof_buf)
  90. {
  91. switch (CURRENT_STDIO) {
  92. case DO_USE_STDIO:
  93. {
  94. int i;
  95. for (i = 0; i < sizeof_buf; i++) {
  96. fputc(buf[i], stderr);
  97. }
  98. return i;
  99. }
  100. break;
  101. case DONT_USE_STDIO:
  102. return write(2, buf, sizeof_buf);
  103. break;
  104. default:
  105. error("sim_io_write_stdout: invalid switch\n");
  106. }
  107. return 0;
  108. }
  109. int
  110. sim_io_read_stdin(char *buf,
  111. int sizeof_buf)
  112. {
  113. switch (CURRENT_STDIO) {
  114. case DO_USE_STDIO:
  115. if (sizeof_buf > 1) {
  116. if (fgets(buf, sizeof_buf, stdin) != NULL)
  117. return strlen(buf);
  118. }
  119. else if (sizeof_buf == 1) {
  120. char b[2];
  121. if (fgets(b, sizeof(b), stdin) != NULL) {
  122. memcpy(buf, b, strlen(b));
  123. return strlen(b);
  124. }
  125. }
  126. else if (sizeof_buf == 0)
  127. return 0;
  128. return sim_io_eof;
  129. break;
  130. case DONT_USE_STDIO:
  131. #if defined(O_NONBLOCK) && defined(F_GETFL) && defined(F_SETFL)
  132. {
  133. /* check for input */
  134. int flags;
  135. int status;
  136. int nr_read;
  137. int result;
  138. /* get the old status */
  139. flags = fcntl(0, F_GETFL, 0);
  140. if (flags == -1) {
  141. perror("sim_io_read_stdin");
  142. return sim_io_eof;
  143. }
  144. /* temp, disable blocking IO */
  145. status = fcntl(0, F_SETFL, flags | O_NONBLOCK);
  146. if (status == -1) {
  147. perror("sim_io_read_stdin");
  148. return sim_io_eof;
  149. }
  150. /* try for input */
  151. nr_read = read(0, buf, sizeof_buf);
  152. if (nr_read > 0
  153. || (nr_read == 0 && sizeof_buf == 0))
  154. result = nr_read;
  155. else if (nr_read == 0)
  156. result = sim_io_eof;
  157. else { /* nr_read < 0 */
  158. if (errno == EAGAIN)
  159. result = sim_io_not_ready;
  160. else
  161. result = sim_io_eof;
  162. }
  163. /* return to regular vewing */
  164. status = fcntl(0, F_SETFL, flags);
  165. if (status == -1) {
  166. perror("sim_io_read_stdin");
  167. return sim_io_eof;
  168. }
  169. return result;
  170. }
  171. break;
  172. #endif
  173. default:
  174. error("sim_io_read_stdin: invalid switch\n");
  175. break;
  176. }
  177. return 0;
  178. }
  179. void
  180. sim_io_flush_stdoutput(void)
  181. {
  182. switch (CURRENT_STDIO) {
  183. case DO_USE_STDIO:
  184. fflush (stdout);
  185. break;
  186. case DONT_USE_STDIO:
  187. break;
  188. default:
  189. error("sim_io_flush_stdoutput: invalid switch\n");
  190. break;
  191. }
  192. }
  193. /* Glue to use sim-fpu module. */
  194. void
  195. sim_io_error (SIM_DESC sd, const char *msg, ...)
  196. {
  197. va_list ap;
  198. va_start(ap, msg);
  199. vprintf(msg, ap);
  200. printf("\n");
  201. va_end(ap);
  202. /* any final clean up */
  203. if (ppc_trace[trace_print_info] && simulation != NULL)
  204. psim_print_info (simulation, ppc_trace[trace_print_info]);
  205. exit (1);
  206. }
  207. void *
  208. zalloc(long size)
  209. {
  210. void *memory = malloc(size);
  211. if (memory == NULL)
  212. error("zalloc failed\n");
  213. memset(memory, 0, size);
  214. return memory;
  215. }
  216. /* When a CNTRL-C occures, queue an event to shut down the simulation */
  217. static RETSIGTYPE
  218. cntrl_c(int sig)
  219. {
  220. psim_stop (simulation);
  221. }
  222. int
  223. main(int argc, char * const *argv)
  224. {
  225. const char *name_of_file;
  226. char *arg_;
  227. psim_status status;
  228. device *root = psim_tree();
  229. /* parse the arguments */
  230. argv = psim_options (root, argv + 1, SIM_OPEN_STANDALONE);
  231. if (argv[0] == NULL) {
  232. if (ppc_trace[trace_opts]) {
  233. print_options ();
  234. return 0;
  235. } else {
  236. psim_usage (0, 0, SIM_OPEN_STANDALONE);
  237. }
  238. }
  239. name_of_file = argv[0];
  240. if (ppc_trace[trace_opts])
  241. print_options ();
  242. /* create the simulator */
  243. simulation = psim_create(name_of_file, root);
  244. /* fudge the environment so that _=prog-name */
  245. arg_ = (char*)zalloc(strlen(argv[0]) + strlen("_=") + 1);
  246. strcpy(arg_, "_=");
  247. strcat(arg_, argv[0]);
  248. putenv(arg_);
  249. /* initialize it */
  250. psim_init(simulation);
  251. psim_stack(simulation, argv, environ);
  252. {
  253. RETSIGTYPE (*prev) ();
  254. prev = signal(SIGINT, cntrl_c);
  255. psim_run(simulation);
  256. signal(SIGINT, prev);
  257. }
  258. /* any final clean up */
  259. if (ppc_trace[trace_print_info])
  260. psim_print_info (simulation, ppc_trace[trace_print_info]);
  261. /* why did we stop */
  262. status = psim_get_status(simulation);
  263. switch (status.reason) {
  264. case was_continuing:
  265. error("psim: continuing while stopped!\n");
  266. return 0;
  267. case was_trap:
  268. error("psim: no trap insn\n");
  269. return 0;
  270. case was_exited:
  271. return status.signal;
  272. case was_signalled:
  273. printf ("%s: Caught signal %d at address 0x%lx\n",
  274. name_of_file, (int)status.signal,
  275. (long)status.program_counter);
  276. return status.signal;
  277. default:
  278. error("unknown halt condition\n");
  279. return 0;
  280. }
  281. }