main.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /* main.c --- main function for stand-alone M32C simulator.
  2. Copyright (C) 2005-2022 Free Software Foundation, Inc.
  3. Contributed by Red Hat, Inc.
  4. This file is part of the GNU simulators.
  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 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /* This must come before any other includes. */
  16. #include "defs.h"
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. #include <assert.h>
  22. #include <setjmp.h>
  23. #include <signal.h>
  24. #include <sys/types.h>
  25. #ifdef HAVE_SYS_SOCKET_H
  26. #ifdef HAVE_NETINET_IN_H
  27. #ifdef HAVE_NETINET_TCP_H
  28. #define HAVE_networking
  29. #endif
  30. #endif
  31. #endif
  32. #ifdef HAVE_networking
  33. #include <sys/socket.h>
  34. #include <netinet/in.h>
  35. #include <netinet/tcp.h>
  36. #endif
  37. #include "bfd.h"
  38. #include "cpu.h"
  39. #include "mem.h"
  40. #include "misc.h"
  41. #include "load.h"
  42. #include "trace.h"
  43. #ifdef TIMER_A
  44. #include "int.h"
  45. #include "timer_a.h"
  46. #endif
  47. #ifdef HAVE_networking
  48. extern int m32c_console_ofd;
  49. extern int m32c_console_ifd;
  50. #endif
  51. int m32c_disassemble = 0;
  52. static unsigned int cycles = 0;
  53. static void
  54. done (int exit_code)
  55. {
  56. if (verbose)
  57. {
  58. stack_heap_stats ();
  59. mem_usage_stats ();
  60. printf ("insns: %14s\n", comma (cycles));
  61. }
  62. exit (exit_code);
  63. }
  64. #ifdef HAVE_networking
  65. static void
  66. setup_tcp_console (char *portname)
  67. {
  68. int port = atoi (portname);
  69. struct sockaddr_in address;
  70. int isocket;
  71. socklen_t as;
  72. unsigned char *a;
  73. if (port < 1024)
  74. {
  75. printf ("invalid port number %d\n", port);
  76. exit (1);
  77. }
  78. printf ("waiting for tcp console on port %d\n", port);
  79. memset (&address, 0, sizeof (address));
  80. address.sin_family = AF_INET;
  81. address.sin_port = htons (port);
  82. isocket = socket (AF_INET, SOCK_STREAM, 0);
  83. if (isocket == -1)
  84. {
  85. perror ("socket");
  86. exit (1);
  87. }
  88. if (bind (isocket, (struct sockaddr *) &address, sizeof (address)))
  89. {
  90. perror ("bind");
  91. exit (1);
  92. }
  93. listen (isocket, 2);
  94. printf ("waiting for connection...\n");
  95. as = sizeof (address);
  96. m32c_console_ifd = accept (isocket, (struct sockaddr *) &address, &as);
  97. if (m32c_console_ifd == -1)
  98. {
  99. perror ("accept");
  100. exit (1);
  101. }
  102. a = (unsigned char *) (&address.sin_addr.s_addr);
  103. printf ("connection from %d.%d.%d.%d\n", a[0], a[1], a[2], a[3]);
  104. m32c_console_ofd = m32c_console_ifd;
  105. }
  106. #endif
  107. int
  108. main (int argc, char **argv)
  109. {
  110. int o;
  111. int save_trace;
  112. bfd *prog;
  113. #ifdef HAVE_networking
  114. char *console_port_s = 0;
  115. #endif
  116. setbuf (stdout, 0);
  117. in_gdb = 0;
  118. while ((o = getopt (argc, argv, "tc:vdm:C")) != -1)
  119. switch (o)
  120. {
  121. case 't':
  122. trace++;
  123. break;
  124. case 'c':
  125. #ifdef HAVE_networking
  126. console_port_s = optarg;
  127. #else
  128. fprintf (stderr, "Nework console not available in this build.\n");
  129. #endif
  130. break;
  131. case 'C':
  132. #ifdef HAVE_TERMIOS_H
  133. m32c_use_raw_console = 1;
  134. #else
  135. fprintf (stderr, "Raw console not available in this build.\n");
  136. #endif
  137. break;
  138. case 'v':
  139. verbose++;
  140. break;
  141. case 'd':
  142. m32c_disassemble++;
  143. break;
  144. case 'm':
  145. if (strcmp (optarg, "r8c") == 0 || strcmp (optarg, "m16c") == 0)
  146. default_machine = bfd_mach_m16c;
  147. else if (strcmp (optarg, "m32cm") == 0
  148. || strcmp (optarg, "m32c") == 0)
  149. default_machine = bfd_mach_m32c;
  150. else
  151. {
  152. fprintf (stderr, "Invalid machine: %s\n", optarg);
  153. exit (1);
  154. }
  155. break;
  156. case '?':
  157. fprintf (stderr,
  158. "usage: run [-v] [-C] [-c port] [-t] [-d] [-m r8c|m16c|m32cm|m32c]"
  159. " program\n");
  160. exit (1);
  161. }
  162. prog = bfd_openr (argv[optind], 0);
  163. if (!prog)
  164. {
  165. fprintf (stderr, "Can't read %s\n", argv[optind]);
  166. exit (1);
  167. }
  168. if (!bfd_check_format (prog, bfd_object))
  169. {
  170. fprintf (stderr, "%s not a m32c program\n", argv[optind]);
  171. exit (1);
  172. }
  173. save_trace = trace;
  174. trace = 0;
  175. m32c_load (prog);
  176. trace = save_trace;
  177. #ifdef HAVE_networking
  178. if (console_port_s)
  179. setup_tcp_console (console_port_s);
  180. #endif
  181. sim_disasm_init (prog);
  182. while (1)
  183. {
  184. int rc;
  185. if (trace)
  186. printf ("\n");
  187. if (m32c_disassemble)
  188. sim_disasm_one ();
  189. enable_counting = verbose;
  190. cycles++;
  191. rc = decode_opcode ();
  192. enable_counting = 0;
  193. if (M32C_HIT_BREAK (rc))
  194. done (1);
  195. else if (M32C_EXITED (rc))
  196. done (M32C_EXIT_STATUS (rc));
  197. else
  198. assert (M32C_STEPPED (rc));
  199. trace_register_changes ();
  200. #ifdef TIMER_A
  201. update_timer_a ();
  202. #endif
  203. }
  204. }