Add preliminary version of s51, a UI clone of the 8051 emulator.
[fw/altos] / s51 / s51-main.c
diff --git a/s51/s51-main.c b/s51/s51-main.c
new file mode 100644 (file)
index 0000000..e8bf2d7
--- /dev/null
@@ -0,0 +1,143 @@
+/*
+ * Copyright © 2008 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+#include "s51.h"
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+
+static int s51_port = 0;
+static char *cpu = "8051";
+static double freq = 11059200;
+char *s51_prompt = "> ";
+struct ccdbg *s51_dbg;
+
+static void
+usage(void)
+{
+       fprintf(stderr, "You're doing it wrong.\n");
+       exit(1);
+}
+
+int
+main(int argc, char **argv)
+{
+       int flags, opt;
+       FILE *console_in = stdin;
+       FILE *console_out = stdout;
+       char *endptr;
+
+       while ((opt = getopt(argc, argv, "PVvHht:X:c:Z:s:S:p:")) != -1) {
+               switch (opt) {
+               case 't':
+                       cpu = optarg;
+                       break;
+               case 'X':
+                       freq = strtod(optarg, &endptr);
+                       if (endptr == optarg)
+                               usage();
+                       if (endptr[0] != '\0') {
+                               if (!strcmp(endptr, "k"))
+                                       freq *= 1000;
+                               else if (!strcmp(endptr, "M") )
+                                       freq *= 1000000;
+                               else
+                                       usage ();
+                       }
+                       break;
+               case 'c':
+                       break;
+               case 'Z':
+                       s51_port = strtol(optarg, &endptr, 0);
+                       if (endptr == optarg || strlen(endptr) != 0)
+                               usage();
+                       break;
+               case 's':
+                       break;
+               case 'S':
+                       break;
+               case 'p':
+                       s51_prompt = optarg;
+                       break;
+               case 'P':
+                       s51_prompt = NULL;
+                       break;
+               case 'V':
+                       break;
+               case 'v':
+                       break;
+               case 'H':
+                       exit (0);
+                       break;
+               case 'h':
+                       usage ();
+                       break;
+               }
+       }
+       if (s51_port) {
+               int l, r, one = 1;
+               int s;
+               struct sockaddr_in in;
+
+               l = socket(AF_INET, SOCK_STREAM, 0);
+               if (l < 0) {
+                       perror ("socket");
+                       exit(1);
+               }
+               in.sin_family = AF_INET;
+               in.sin_port = htons(s51_port);
+               in.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+               r = bind(l, (struct sockaddr *) &in, sizeof (in));
+               if (r) {
+                       perror("bind");
+                       exit(1);
+               }
+               r = setsockopt(l, SOL_SOCKET, SO_REUSEADDR, &one, sizeof (int));
+               if (r) {
+                       perror("setsockopt");
+                       exit(1);
+               }
+               r = listen(l, 5);
+               if (r) {
+                       perror("listen");
+                       exit(1);
+               }
+               for (;;) {
+                       struct sockaddr_in client_addr;
+                       socklen_t client_len = sizeof (struct sockaddr_in);
+                       FILE *client;
+                       
+                       s = accept(r, (struct sockaddr *)
+                                  &client_addr, &client_len);
+                       if (s < 0) {
+                               perror("accept");
+                               exit(1);
+                       }
+                       client = fdopen(s, "rw");
+                       if (!client) {
+                               perror("fdopen");
+                               exit(1);
+                       }
+                       command_read(client, client);
+                       fclose(client);
+               }
+       } else
+               command_read(console_in, console_out);
+       exit(0);
+}