s51: get start address from ihx file. re-enable breakpoints after reset.
[fw/altos] / s51 / s51-main.c
index e8bf2d7dbf65d0020ffa511b2426ffc1ded748df..27ed571aca68eb360f5267d8aaff33d27ee55720 100644 (file)
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
+#include <signal.h>
+#include <stdarg.h>
+#include <poll.h>
 
 static int s51_port = 0;
 static char *cpu = "8051";
 static double freq = 11059200;
 char *s51_prompt = "> ";
 struct ccdbg *s51_dbg;
+int s51_interrupted = 0;
+
+static FILE *s51_input;
+static FILE *s51_output;
 
 static void
 usage(void)
@@ -35,15 +42,19 @@ usage(void)
        exit(1);
 }
 
+void s51_sigint()
+{
+       s51_interrupted = 1;
+}
+
 int
 main(int argc, char **argv)
 {
        int flags, opt;
-       FILE *console_in = stdin;
-       FILE *console_out = stdout;
        char *endptr;
+       struct sigvec vec, ovec;
 
-       while ((opt = getopt(argc, argv, "PVvHht:X:c:Z:s:S:p:")) != -1) {
+       while ((opt = getopt(argc, argv, "PVvHht:X:c:r:Z:s:S:p:")) != -1) {
                switch (opt) {
                case 't':
                        cpu = optarg;
@@ -63,6 +74,7 @@ main(int argc, char **argv)
                        break;
                case 'c':
                        break;
+               case 'r':
                case 'Z':
                        s51_port = strtol(optarg, &endptr, 0);
                        if (endptr == optarg || strlen(endptr) != 0)
@@ -100,6 +112,11 @@ main(int argc, char **argv)
                        perror ("socket");
                        exit(1);
                }
+               r = setsockopt(l, SOL_SOCKET, SO_REUSEADDR, &one, sizeof (int));
+               if (r) {
+                       perror("setsockopt");
+                       exit(1);
+               }
                in.sin_family = AF_INET;
                in.sin_port = htons(s51_port);
                in.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
@@ -108,11 +125,6 @@ main(int argc, char **argv)
                        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");
@@ -121,23 +133,93 @@ main(int argc, char **argv)
                for (;;) {
                        struct sockaddr_in client_addr;
                        socklen_t client_len = sizeof (struct sockaddr_in);
-                       FILE *client;
                        
-                       s = accept(r, (struct sockaddr *)
+                       s = accept(l, (struct sockaddr *)
                                   &client_addr, &client_len);
                        if (s < 0) {
                                perror("accept");
                                exit(1);
                        }
-                       client = fdopen(s, "rw");
-                       if (!client) {
+                       s51_input = fdopen(s, "r");
+                       s51_output = fdopen(s, "w");
+                       if (!s51_input || !s51_output) {
                                perror("fdopen");
                                exit(1);
                        }
-                       command_read(client, client);
-                       fclose(client);
+                       vec.sv_handler = s51_sigint;
+                       vec.sv_mask = 0;
+                       vec.sv_flags = 0;
+                       sigvec(SIGINT, &vec, &ovec);
+                       command_read();
+                       sigvec(SIGINT, &ovec, NULL);
+                       fclose(s51_input);
+                       fclose(s51_output);
                }
-       } else
-               command_read(console_in, console_out);
+       } else {
+               s51_input = stdin;
+               s51_output = stdout;
+               vec.sv_handler = s51_sigint;
+               vec.sv_mask = 0;
+               vec.sv_flags = 0;
+               sigvec(SIGINT, &vec, &ovec);
+               command_read();
+       }
        exit(0);
 }
+
+void
+s51_printf(char *format, ...)
+{
+       va_list ap;
+
+       va_start(ap, format);
+       vfprintf(s51_output, format, ap);
+#if 1
+       if (s51_port)
+               vfprintf(stdout, format, ap);
+#endif
+       va_end(ap);
+}
+
+void
+s51_putc(int c)
+{
+       putc(c, s51_output);
+}
+
+int
+s51_read_line(char *line, int len)
+{
+       int ret;
+       if (s51_prompt)
+               s51_printf("%s", s51_prompt);
+       else
+               s51_putc('\0');
+       fflush(s51_output);
+       ret = fgets(line, len, s51_input) != NULL;
+#if 1
+       if (s51_port)
+               printf("> %s", line);
+#endif
+       fflush(stdout);
+       return ret;
+}
+
+int
+s51_check_input(void)
+{
+       struct pollfd   input;
+       int r;
+       int c;
+
+       input.fd = fileno(s51_input);
+       input.events = POLLIN;
+       r = poll(&input, 1, 0);
+       if (r > 0) {
+               char line[256];
+               (void) s51_read_line(line, sizeof (line));
+               return 1; 
+       }
+       return 0;
+}
+