Use 'ao-dbg' instead of 's51' to communicate with TeleMetrum
[fw/sdcc] / debugger / mcs51 / simi.c
index a9e67b7234b3b57c42ffc78532f7b90e5812c35b..4a771a339fcc4782fa304011bb4b6aa4afb2733b 100644 (file)
@@ -82,7 +82,14 @@ static char *getMemCache(unsigned int addr,int cachenum, int size)
         {
             sendSim("ds 0x80 0xff\n");
         }
-        else
+        else if ( cachenum == CMEM_CACHE )
+       {
+#define CMEM_CACHE_MASK ((MAX_CACHE_SIZE >> 2) - 1)
+            laddr = addr & ~CMEM_CACHE_MASK;
+            sprintf(cache->buffer,"dump rom 0x%x 0x%x\n",laddr,laddr+CMEM_CACHE_MASK );
+            sendSim(cache->buffer);
+       }
+       else
         {
             laddr = addr & 0xffffffc0;
             sprintf(cache->buffer,"dx 0x%x 0x%x\n",laddr,laddr+0xff );
@@ -142,7 +149,7 @@ Dprintf(D_simi, ("simi: waitForSim start(%d)\n", timeout_ms));
     sbp = simibuff;
 
     while ((ch = fgetc(simin)) > 0 ) {
-      *sbp++ = ch;
+        *sbp++ = ch;
     }
     *sbp = 0;
     Dprintf(D_simi, ("waitForSim(%d) got[%s]\n", timeout_ms, simibuff));
@@ -153,7 +160,25 @@ Dprintf(D_simi, ("simi: waitForSim start(%d)\n", timeout_ms));
 /* openSimulator - create a pipe to talk to simulator              */
 /*-----------------------------------------------------------------*/
 #ifdef _WIN32
-char *argsToCmdLine(char **args, int nargs)
+static void init_winsock(void)
+{
+    static int is_initialized = 0;
+
+    if (!is_initialized)
+    {
+        WSADATA wsaData;
+        int iResult;
+
+        // Initialize Winsock
+        if (0 != WSAStartup(MAKEWORD(2,2), &wsaData))
+        {
+            fprintf(stderr, "WSAStartup failed: %d\n", iResult);
+            exit(1);
+        }
+    }
+}
+
+static char *argsToCmdLine(char **args, int nargs)
 {
 #define CHUNCK  256
     int i;
@@ -172,20 +197,20 @@ char *argsToCmdLine(char **args, int nargs)
             argLen += 2;
         }
 
-        if (0 < nargs)
+        if (0 < i)
             ++argLen;
 
-        if (argLen >= cmdLen)
+        if (cmdPos + argLen >= cmdLen)
         {
             do
             {
                 cmdLen += cmdLen;
             }
-            while (argLen >= cmdLen);
+            while (cmdPos + argLen >= cmdLen);
             cmd = Safe_realloc(cmd, cmdLen);
         }
 
-        if (0 < nargs)
+        if (0 < i)
         {
             cmd[cmdPos++] = ' ';
             --argLen;
@@ -204,10 +229,12 @@ char *argsToCmdLine(char **args, int nargs)
             cmd[cmdPos++] = '"';
     }
 
+    cmd[cmdPos] = '\0';
+
     return cmd;
 }
 
-PROCESS_INFORMATION *execSimulator(char **args, int nargs)
+static PROCESS_INFORMATION *execSimulator(char **args, int nargs)
 {
     STARTUPINFO si;
     static PROCESS_INFORMATION pi;
@@ -245,6 +272,9 @@ void openSimulator (char **args, int nargs)
     int retry = 0;
     int i;
     int fh;
+
+    init_winsock();
+
     Dprintf(D_simi, ("simi: openSimulator\n"));
 #ifdef SDCDB_DEBUG
     if (D_simi & sdcdbDebug)
@@ -260,17 +290,21 @@ void openSimulator (char **args, int nargs)
     invalidateCache(XMEM_CACHE);
     invalidateCache(IMEM_CACHE);
     invalidateCache(SREG_CACHE);
+    invalidateCache(CMEM_CACHE);
 
- try_connect:
-    sock = WSASocket(PF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, 0);
+    if (INVALID_SOCKET == (sock = WSASocket(PF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, 0))) {
+        fprintf(stderr, "cannot create socket: %d\n", WSAGetLastError());
+        exit(1);
+    }
 
     memset(&sin,0,sizeof(sin));
     sin.sin_family = AF_INET;
     sin.sin_addr.s_addr = inet_addr("127.0.0.1");
     sin.sin_port = htons(9756);
 
+ try_connect:
     /* connect to the simulator */
-    if (INVALID_SOCKET == connect(sock,(struct sockaddr *) &sin, sizeof(sin)))
+    if (SOCKET_ERROR == connect(sock, (struct sockaddr *)&sin, sizeof(sin)))
     {
         /* if failed then wait 1 second & try again
            do this for 10 secs only */
@@ -285,38 +319,39 @@ void openSimulator (char **args, int nargs)
         perror("connect failed :");
         exit(1);
     }
-    fh = _open_osfhandle((intptr_t)socket, _O_TEXT);
+
+    fh = _open_osfhandle((intptr_t)sock, _O_TEXT);
     if (-1 == fh)
     {
-        fprintf(stderr, "cannot _open_osfhandle\n");
+        perror("cannot _open_osfhandle");
         exit(1);
     }
 
     /* go the socket now turn it into a file handle */
-    if (!(simin = fdopen(sock,"r")))
+    if (!(simin = fdopen(fh, "r")))
     {
-        fprintf(stderr,"cannot open socket for read\n");
+        perror("cannot open socket for read");
         exit(1);
     }
 
-    fh = _open_osfhandle((intptr_t)socket, _O_TEXT);
+    fh = _open_osfhandle((intptr_t)sock, _O_TEXT);
     if (-1 == fh)
     {
-        fprintf(stderr, "cannot _open_osfhandle\n");
+        perror("cannot _open_osfhandle");
         exit(1);
     }
 
-    if (!(simout = fdopen(sock,"w")))
+    if (!(simout = fdopen(fh, "w")))
     {
-        fprintf(stderr,"cannot open socket for write\n");
+        perror("cannot open socket for write");
         exit(1);
     }
     /* now that we have opened, wait for the prompt */
-    waitForSim(200,NULL);
+    waitForSim(200, NULL);
     simactive = 1;
 }
 #else
-int execSimulator(char **args, int nargs)
+static int execSimulator(char **args, int nargs)
 {
     if ((simPid = fork()))
     {
@@ -336,6 +371,7 @@ int execSimulator(char **args, int nargs)
             exit(1);
         }
     }
+    return simPid;
 }
 
 void openSimulator (char **args, int nargs)
@@ -358,15 +394,20 @@ void openSimulator (char **args, int nargs)
     invalidateCache(XMEM_CACHE);
     invalidateCache(IMEM_CACHE);
     invalidateCache(SREG_CACHE);
+    invalidateCache(CMEM_CACHE);
 
- try_connect:
-    sock = socket(AF_INET,SOCK_STREAM,0);
+    if ((sock = socket(AF_INET,SOCK_STREAM,0)) < 0)
+    {
+        perror("cannot create socket");
+        exit(1);
+    }
 
     memset(&sin,0,sizeof(sin));
     sin.sin_family = AF_INET;
     sin.sin_addr.s_addr = inet_addr("127.0.0.1");
     sin.sin_port = htons(9756);
 
+ try_connect:
     /* connect to the simulator */
     if (connect(sock,(struct sockaddr *) &sin, sizeof(sin)) < 0)
     {
@@ -406,7 +447,7 @@ void openSimulator (char **args, int nargs)
 /*-----------------------------------------------------------------*/
 /* simResponse - returns buffer to simulator's response            */
 /*-----------------------------------------------------------------*/
-char *simResponse()
+char *simResponse(void)
 {
     return simibuff;
 }
@@ -442,12 +483,14 @@ static int getMemString(char *buffer, char wrflag,
     {
         case 'A': /* External stack */
         case 'F': /* External ram */
+       case 'P': /* Pdata space */
             prefix = "xram";
             cachenr = XMEM_CACHE;
             break;
         case 'C': /* Code */
         case 'D': /* Code / static segment */
             prefix = "rom";
+           cachenr = CMEM_CACHE;
             break;
         case 'B': /* Internal stack */
         case 'E': /* Internal ram (lower 128) bytes */
@@ -496,6 +539,16 @@ void simSetPC( unsigned int addr )
     simResponse();
 }
 
+unsigned int simGetPC(void)
+{
+    char *sr;
+    sendSim("pc\n");
+    waitForSim(100,NULL);
+    simResponse();
+    sr = simResponse();
+    return strtol(sr+3,0,0);
+}
+    
 int simSetValue (unsigned int addr,char mem, int size, unsigned long val)
 {
     char cachenr, i;
@@ -513,7 +566,7 @@ int simSetValue (unsigned int addr,char mem, int size, unsigned long val)
     s = buffer + strlen(buffer) -1;
     for ( i = 0 ; i < size ; i++ )
     {
-        sprintf(s," 0x%x", val & 0xff);
+        sprintf(s," 0x%lx", val & 0xff);
         s += strlen(s);
         val >>= 8;
     }
@@ -558,7 +611,7 @@ unsigned long simGetValue (unsigned int addr,char mem, int size)
            case of bit variables which case it becomes
            [address] [assembler bit address] [v] */
         /* first skip thru white space */
-        while (isspace(*resp)) resp++ ;
+        resp = trim_left(resp);
 
         if (strncmp(resp, "0x",2) == 0)
             resp += 2;
@@ -580,7 +633,7 @@ unsigned long simGetValue (unsigned int addr,char mem, int size)
         for (i = 0 ; i < size ; i++ )
         {
             /* skip white space */
-            while (isspace(*resp)) resp++ ;
+            resp = trim_left(resp);
 
             b[i] = strtol(resp,&resp,16);
         }
@@ -622,7 +675,7 @@ void simLoadFile (char *s)
     char buff[128];
 
     sprintf(buff,"file \"%s\"\n",s);
-    printf(buff);
+    printf("%s",buff);
     sendSim(buff);
     waitForSim(500,NULL);
 }
@@ -641,34 +694,34 @@ unsigned int simGoTillBp ( unsigned int gaddr)
     invalidateCache(IMEM_CACHE);
     invalidateCache(SREG_CACHE);
     if (gaddr == 0) {
-      /* initial start, start & stop from address 0 */
-      //char buf[20];
-
-         // this program is setting up a bunch of breakpoints automatically
-         // at key places.  Like at startup & main() and other function
-         // entry points.  So we don't need to setup one here..
-      //sendSim("break 0x0\n");
-      //sleep(1);
-      //waitForSim();
-
-      sendSim("reset\n");
-      waitForSim(wait_ms, NULL);
-      sendSim("run 0x0\n");
+        /* initial start, start & stop from address 0 */
+        //char buf[20];
+
+           // this program is setting up a bunch of breakpoints automatically
+           // at key places.  Like at startup & main() and other function
+           // entry points.  So we don't need to setup one here..
+        //sendSim("break 0x0\n");
+        //sleep(1);
+        //waitForSim();
+
+        sendSim("reset\n");
+        waitForSim(wait_ms, NULL);
+        sendSim("run 0x0\n");
     } else      if (gaddr == -1) { /* resume */
-      sendSim ("run\n");
-      wait_ms = 100;
+        sendSim ("run\n");
+        wait_ms = 100;
     }
     else        if (gaddr == 1 ) { /* nexti or next */
-      sendSim ("next\n");
-      wait_ms = 100;
+        sendSim ("next\n");
+        wait_ms = 100;
     }
     else        if (gaddr == 2 ) { /* stepi or step */
-      sendSim ("step\n");
-      wait_ms = 100;
+        sendSim ("step\n");
+        wait_ms = 100;
     }
     else  {
-      printf("Error, simGoTillBp > 0!\n");
-      exit(1);
+        printf("Error, simGoTillBp > 0!\n");
+        exit(1);
     }
 
     waitForSim(wait_ms, NULL);
@@ -707,11 +760,12 @@ unsigned int simGoTillBp ( unsigned int gaddr)
 /*-----------------------------------------------------------------*/
 /* simReset - reset the simulator                                  */
 /*-----------------------------------------------------------------*/
-void simReset ()
+void simReset (void)
 {
     invalidateCache(XMEM_CACHE);
     invalidateCache(IMEM_CACHE);
     invalidateCache(SREG_CACHE);
+    invalidateCache(CMEM_CACHE);
     sendSim("res\n");
     waitForSim(100,NULL);
 }
@@ -720,7 +774,7 @@ void simReset ()
 /*-----------------------------------------------------------------*/
 /* closeSimulator - close connection to simulator                  */
 /*-----------------------------------------------------------------*/
-void closeSimulator ()
+void closeSimulator (void)
 {
 #ifdef _WIN32
     if ( ! simin || ! simout || INVALID_SOCKET == sock )