X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=debugger%2Fmcs51%2Fsimi.c;h=c44b40718ff2047a48350343c94c1b12f9b54f6c;hb=80972b2e54c9b88f11c27b878874fd2a6a681391;hp=0edc1e98d58d19d55993286b6d37a86ff8ff1f73;hpb=288b9ad179d5164fa969df89ce7bfcc9946cb17c;p=fw%2Fsdcc diff --git a/debugger/mcs51/simi.c b/debugger/mcs51/simi.c index 0edc1e98..c44b4071 100644 --- a/debugger/mcs51/simi.c +++ b/debugger/mcs51/simi.c @@ -1,29 +1,36 @@ /*------------------------------------------------------------------------- simi.c - source file for simulator interaction - - Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999) + Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999) 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, 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - + In other words, you are welcome to use, share and improve this program. You are forbidden to forbid anyone else to use, share and improve - what you give them. Help stamp out software-hoarding! + what you give them. Help stamp out software-hoarding! -------------------------------------------------------------------------*/ #include "sdcdb.h" #include "simi.h" +#include "newalloc.h" +#ifdef _WIN32 +#include +#include +#include +#include +#include +#else #ifdef HAVE_SYS_SOCKET_H #include #include @@ -34,11 +41,18 @@ #else #error "Cannot build debugger without socket support" #endif +#endif + FILE *simin ; /* stream for simulator input */ FILE *simout; /* stream for simulator output */ +#ifdef _WIN32 +SOCKET sock = INVALID_SOCKET; +PROCESS_INFORMATION *simPid = NULL; +#else int sock = -1; /* socket descriptor to comm with simulator */ -pid_t simPid; +pid_t simPid = -1; +#endif static char simibuff[MAX_SIM_BUFF]; /* sim buffer */ static char regBuff[MAX_SIM_BUFF]; static char *sbp = simibuff; /* simulator buffer pointer */ @@ -64,6 +78,10 @@ static char *getMemCache(unsigned int addr,int cachenum, int size) { sendSim("di 0x0 0xff\n"); } + else if ( cachenum == SREG_CACHE ) + { + sendSim("ds 0x80 0xff\n"); + } else { laddr = addr & 0xffffffc0; @@ -109,7 +127,7 @@ static char *getMemCache(unsigned int addr,int cachenum, int size) /*-----------------------------------------------------------------*/ static void invalidateCache( int cachenum ) { - memCache[cachenum].size = 0; + memCache[cachenum].size = 0; } /*-----------------------------------------------------------------*/ @@ -124,7 +142,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)); @@ -134,72 +152,293 @@ Dprintf(D_simi, ("simi: waitForSim start(%d)\n", timeout_ms)); /*-----------------------------------------------------------------*/ /* openSimulator - create a pipe to talk to simulator */ /*-----------------------------------------------------------------*/ +#ifdef _WIN32 +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; + int cmdPos = 0; + char *cmd = Safe_malloc(CHUNCK); + int cmdLen = CHUNCK; + + for (i = 0; i < nargs; i++) + { + int quote = 0; + int argLen = strlen(args[i]); + + if (NULL != strchr(args[i], ' ')) + { + quote = 1; + argLen += 2; + } + + if (0 < i) + ++argLen; + + if (cmdPos + argLen >= cmdLen) + { + do + { + cmdLen += cmdLen; + } + while (cmdPos + argLen >= cmdLen); + cmd = Safe_realloc(cmd, cmdLen); + } + + if (0 < i) + { + cmd[cmdPos++] = ' '; + --argLen; + } + + if (quote) + { + cmd[cmdPos++] = '"'; + --argLen; + } + + memcpy(&cmd[cmdPos], args[i], argLen); + cmdPos += argLen; + + if (quote) + cmd[cmdPos++] = '"'; + } + + cmd[cmdPos] = '\0'; + + return cmd; +} + +static PROCESS_INFORMATION *execSimulator(char **args, int nargs) +{ + STARTUPINFO si; + static PROCESS_INFORMATION pi; + char *cmdLine = argsToCmdLine(args, nargs); + + memset(&si, 0, sizeof(si)); + si.cb = sizeof(si); + memset(&pi, 0, sizeof(pi)); + + // Start the child process. + if (!CreateProcess(NULL, // No module name (use command line) + cmdLine, // Command line + NULL, // Process handle not inheritable + NULL, // Thread handle not inheritable + FALSE, // Set handle inheritance to FALSE + 0, // No creation flags + NULL, // Use parent's environment block + NULL, // Use parent's starting directory + &si, // Pointer to STARTUPINFO structure + &pi) // Pointer to PROCESS_INFORMATION structure + ) + { + Safe_free(cmdLine); + printf( "CreateProcess failed (%d).\n", GetLastError() ); + return NULL; + } + + Safe_free(cmdLine); + return π +} + void openSimulator (char **args, int nargs) { - struct sockaddr_in sin; + struct sockaddr_in sin; int retry = 0; - int i ; - Dprintf(D_simi, ("simi: openSimulator\n")); + int i; + int fh; + + init_winsock(); + Dprintf(D_simi, ("simi: openSimulator\n")); +#ifdef SDCDB_DEBUG + if (D_simi & sdcdbDebug) + { + printf("simi: openSimulator: "); + for (i=0; i < nargs; i++ ) + { + printf("arg%d: %s ",i,args[i]); + } + printf("\n"); + } +#endif invalidateCache(XMEM_CACHE); invalidateCache(IMEM_CACHE); - /* fork and start the simulator as a subprocess */ - if ((simPid = fork())) { - Dprintf(D_simi, ("simi: simulator pid %d\n",(int) simPid)); - } - else { - /* we are in the child process : start the simulator */ - signal(SIGHUP , SIG_IGN ); - signal(SIGINT , SIG_IGN ); - signal(SIGABRT, SIG_IGN ); - signal(SIGCHLD, SIG_IGN ); - - if (execvp(args[0],args) < 0) { - perror("cannot exec simulator"); + invalidateCache(SREG_CACHE); + + if (INVALID_SOCKET == (sock = WSASocket(PF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, 0))) { + fprintf(stderr, "cannot create socket: %d\n", WSAGetLastError()); exit(1); - } } - - try_connect: - sock = socket(AF_INET,SOCK_STREAM,0); - + 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); - - sleep(1); + + try_connect: /* connect to the simulator */ - if (connect(sock,(struct sockaddr *) &sin, sizeof(sin)) < 0) { - /* if failed then wait 1 second & try again - do this for 10 secs only */ - if (retry < 10) { - retry ++; - sleep (1); - goto try_connect; - } - perror("connect failed :"); - exit(1); + if (SOCKET_ERROR == connect(sock, (struct sockaddr *)&sin, sizeof(sin))) + { + /* if failed then wait 1 second & try again + do this for 10 secs only */ + if (retry < 10) + { + if ( !retry ) + simPid = execSimulator(args, nargs); + retry ++; + Sleep(1000); + goto try_connect; + } + perror("connect failed :"); + exit(1); } + + fh = _open_osfhandle((intptr_t)sock, _O_TEXT); + if (-1 == fh) + { + perror("cannot _open_osfhandle"); + exit(1); + } + /* go the socket now turn it into a file handle */ - if (!(simin = fdopen(sock,"r"))) { - fprintf(stderr,"cannot open socket for read\n"); - exit(1); + if (!(simin = fdopen(fh, "r"))) + { + perror("cannot open socket for read"); + exit(1); } - if (!(simout = fdopen(sock,"w"))) { - fprintf(stderr,"cannot open socket for write\n"); - exit(1); + fh = _open_osfhandle((intptr_t)sock, _O_TEXT); + if (-1 == fh) + { + perror("cannot _open_osfhandle"); + exit(1); } + if (!(simout = fdopen(fh, "w"))) + { + perror("cannot open socket for write"); + exit(1); + } + /* now that we have opened, wait for the prompt */ + waitForSim(200, NULL); + simactive = 1; +} +#else +static int execSimulator(char **args, int nargs) +{ + if ((simPid = fork())) + { + Dprintf(D_simi, ("simi: simulator pid %d\n",(int) simPid)); + } + else + { + /* we are in the child process : start the simulator */ + signal(SIGINT , SIG_IGN ); + signal(SIGABRT, SIG_IGN ); + signal(SIGHUP , SIG_IGN ); + signal(SIGCHLD, SIG_IGN ); + + if (execvp(args[0],args) < 0) + { + perror("cannot exec simulator"); + exit(1); + } + } + return simPid; +} + +void openSimulator (char **args, int nargs) +{ + struct sockaddr_in sin; + int retry = 0; + int i; + Dprintf(D_simi, ("simi: openSimulator\n")); +#ifdef SDCDB_DEBUG + if (D_simi & sdcdbDebug) + { + printf("simi: openSimulator: "); + for (i=0; i < nargs; i++ ) + { + printf("arg%d: %s ",i,args[i]); + } + printf("\n"); + } +#endif + invalidateCache(XMEM_CACHE); + invalidateCache(IMEM_CACHE); + invalidateCache(SREG_CACHE); + + 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) + { + /* if failed then wait 1 second & try again + do this for 10 secs only */ + if (retry < 10) + { + if ( !retry ) + { + simPid = execSimulator(args, nargs); + } + retry ++; + sleep (1); + goto try_connect; + } + perror("connect failed :"); + exit(1); + } + /* go the socket now turn it into a file handle */ + if (!(simin = fdopen(sock,"r"))) + { + fprintf(stderr,"cannot open socket for read\n"); + exit(1); + } + + if (!(simout = fdopen(sock,"w"))) + { + fprintf(stderr,"cannot open socket for write\n"); + exit(1); + } /* now that we have opened, wait for the prompt */ waitForSim(200,NULL); simactive = 1; } +#endif + /*-----------------------------------------------------------------*/ /* simResponse - returns buffer to simulator's response */ /*-----------------------------------------------------------------*/ -char *simResponse() +char *simResponse(void) { return simibuff; } @@ -209,7 +448,7 @@ char *simResponse() /*-----------------------------------------------------------------*/ void sendSim(char *s) { - if ( ! simout ) + if ( ! simout ) return; Dprintf(D_simi, ("simi: sendSim-->%s", s)); // s has LF at end already @@ -218,8 +457,8 @@ void sendSim(char *s) } -static int getMemString(char *buffer, char wrflag, - unsigned int addr, char mem, int size ) +static int getMemString(char *buffer, char wrflag, + unsigned int *addr, char mem, int size ) { int cachenr = NMEM_CACHE; char *prefix; @@ -231,7 +470,7 @@ static int getMemString(char *buffer, char wrflag, cmd = "dump"; buffer[0] = '\0' ; - switch (mem) + switch (mem) { case 'A': /* External stack */ case 'F': /* External ram */ @@ -242,7 +481,7 @@ static int getMemString(char *buffer, char wrflag, case 'D': /* Code / static segment */ prefix = "rom"; break; - case 'B': /* Internal stack */ + case 'B': /* Internal stack */ case 'E': /* Internal ram (lower 128) bytes */ case 'G': /* Internal ram */ prefix = "iram"; @@ -255,36 +494,40 @@ static int getMemString(char *buffer, char wrflag, { cmd = "set bit"; } - sprintf(buffer,"%s 0x%x\n",cmd,addr); + sprintf(buffer,"%s 0x%x\n",cmd,*addr); return cachenr; break; case 'I': /* SFR space */ prefix = "sfr" ; + cachenr = SREG_CACHE; break; - case 'R': /* Register space */ - if ( !wrflag ) - { - cachenr = REG_CACHE; - sprintf(buffer,"info reg\n"); - return cachenr; - } + case 'R': /* Register space */ prefix = "iram"; /* get register bank */ - cachenr = simGetValue (0xd0,'I',1); - addr += cachenr & 0x18 ; + cachenr = simGetValue (0xd0,'I',1); + *addr += cachenr & 0x18 ; cachenr = IMEM_CACHE; break; - default: + default: case 'Z': /* undefined space code */ return cachenr; } if ( wrflag ) - sprintf(buffer,"%s %s 0x%x\n",cmd,prefix,addr,addr); + sprintf(buffer,"%s %s 0x%x\n",cmd,prefix,*addr); else - sprintf(buffer,"%s %s 0x%x 0x%x\n",cmd,prefix,addr,addr+size-1); + sprintf(buffer,"%s %s 0x%x 0x%x\n",cmd,prefix,*addr,*addr+size-1); return cachenr; } +void simSetPC( unsigned int addr ) +{ + char buffer[40]; + sprintf(buffer,"pc %d\n", addr); + sendSim(buffer); + waitForSim(100,NULL); + simResponse(); +} + int simSetValue (unsigned int addr,char mem, int size, unsigned long val) { char cachenr, i; @@ -294,7 +537,7 @@ int simSetValue (unsigned int addr,char mem, int size, unsigned long val) if ( size <= 0 ) return 0; - cachenr = getMemString(buffer,1,addr,mem,size); + cachenr = getMemString(buffer,1,&addr,mem,size); if ( cachenr < NMEM_CACHE ) { invalidateCache(cachenr); @@ -302,14 +545,15 @@ 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; } sprintf(s,"\n"); sendSim(buffer); waitForSim(100,NULL); - simResponse(); + simResponse(); + return 0; } @@ -326,7 +570,7 @@ unsigned long simGetValue (unsigned int addr,char mem, int size) if ( size <= 0 ) return 0; - cachenr = getMemString(buffer,0,addr,mem,size); + cachenr = getMemString(buffer,0,&addr,mem,size); resp = NULL; if ( cachenr < NMEM_CACHE ) @@ -341,12 +585,12 @@ unsigned long simGetValue (unsigned int addr,char mem, int size) resp = simResponse(); /* got the response we need to parse it the response - is of the form + is of the form [address] [v] [v] [v] ... special case in 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; @@ -354,39 +598,28 @@ unsigned long simGetValue (unsigned int addr,char mem, int size) /* skip thru the address part */ while (isxdigit(*resp)) resp++; - /* then make the branch for bit variables */ - if ( cachenr == REG_CACHE ) - { - /* skip registers */ - for (i = 0 ; i < addr ; i++ ) - { - while (isspace(*resp)) resp++ ; - /* skip */ - while (isxdigit(*resp)) resp++; - } - } - } + } /* make the branch for bit variables */ - if ( cachenr == BIT_CACHE) + if ( cachenr == BIT_CACHE) { /* skip until newline */ while (*resp && *resp != '\n' ) resp++ ; if ( *--resp != '0' ) b[0] = 1; } - else - { - for (i = 0 ; i < size ; i++ ) + else + { + for (i = 0 ; i < size ; i++ ) { /* skip white space */ - while (isspace(*resp)) resp++ ; - + resp = trim_left(resp); + b[i] = strtol(resp,&resp,16); } } return b[0] | b[1] << 8 | b[2] << 16 | b[3] << 24 ; - + } /*-----------------------------------------------------------------*/ @@ -420,8 +653,8 @@ void simLoadFile (char *s) { char buff[128]; - sprintf(buff,"l \"%s\"\n",s); - printf(buff); + sprintf(buff,"file \"%s\"\n",s); + printf("%s",buff); sendSim(buff); waitForSim(500,NULL); } @@ -431,237 +664,122 @@ void simLoadFile (char *s) /*-----------------------------------------------------------------*/ unsigned int simGoTillBp ( unsigned int gaddr) { - char *sr, *svr; - unsigned addr ; + char *sr; + unsigned addr ; char *sfmt; int wait_ms = 1000; invalidateCache(XMEM_CACHE); 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("run 0x0\n"); - } else if (gaddr == -1) { /* resume */ - sendSim ("run\n"); - wait_ms = 100; + /* 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; } - else if (gaddr == 1 ) { /* nexti */ - sendSim ("next\n"); - wait_ms = 100; + else if (gaddr == 1 ) { /* nexti or next */ + sendSim ("next\n"); + wait_ms = 100; } - else if (gaddr == 2 ) { /* stepi */ - sendSim ("step\n"); - wait_ms = 100; + else if (gaddr == 2 ) { /* stepi or step */ + sendSim ("step\n"); + wait_ms = 100; } - else { - printf("Error, simGoTillBp > 0!\n"); - exit(1); + else { + printf("Error, simGoTillBp > 0!\n"); + exit(1); } waitForSim(wait_ms, NULL); - - /* get the simulator response */ - svr = sr = strdup(simResponse()); - if ( gaddr == 1 || gaddr == 2 ) + /* get the simulator response */ + sr = simResponse(); + /* check for errors */ + while ( *sr ) { - int nl; - for ( nl = 0; nl < 3 ; nl++ ) + while ( *sr && *sr != 'E' ) sr++ ; + if ( !*sr ) + break; + if ( ! strncmp(sr,"Error:",6)) { - while (*sr && *sr != '\n') sr++ ; - sr++ ; + fputs(sr,stdout); + break; } - if ( nl < 3 ) - return 0; - gaddr = strtol(sr,0,0); - /* empty response */ - simibuff[0] = '\0'; - return gaddr; - - } - /* figure out the address of the break point the simulators - response in a break point situation is of the form - [... F* ] - we will ignore till we get F* then parse the address */ - while (*sr) { - if (strncmp(sr,"Stop at",7) == 0) { - sr += 7; - sfmt = "%x"; - break; - } - - if (*sr == 'F' && ( *(sr+1) == '*' || *(sr+1) == ' ')) { - sr += 2; - sfmt = "%x"; - break; - } - sr++; - } - - if (!*sr) { - fprintf(stderr, "Error?, simGoTillBp failed to Stop\n"); - return 0; - } - - while (isspace(*sr)) sr++ ; - - if (sscanf(sr,sfmt,&addr) != 1) { - fprintf(stderr, "Error?, simGoTillBp failed to get Addr\n"); - return 0; - } - return addr; + sr++ ; + } + + nointerrupt = 1; + /* get answer of stop command */ + if ( userinterrupt ) + waitForSim(wait_ms, NULL); + /* better solution: ask pc */ + sendSim ("pc\n"); + waitForSim(100, NULL); + sr = simResponse(); + nointerrupt = 0; + + gaddr = strtol(sr+3,0,0); + return gaddr; } /*-----------------------------------------------------------------*/ /* simReset - reset the simulator */ /*-----------------------------------------------------------------*/ -void simReset () +void simReset (void) { invalidateCache(XMEM_CACHE); invalidateCache(IMEM_CACHE); + invalidateCache(SREG_CACHE); sendSim("res\n"); waitForSim(100,NULL); } -/*-----------------------------------------------------------------*/ -/* getValueStr - read a value followed by a string = */ -/*-----------------------------------------------------------------*/ -static unsigned int getValueStr (char *src,char *cstr) -{ - int i = strlen(cstr); - int rv; - /* look for the string */ - if (! (src = strstr(src,cstr))) - return 0; - - src += i; - if (!*src) return 0; - - /* look for the digit */ - while (*src && !isxdigit(*src)) src++; - sscanf(src,"%x",&rv); - return rv; -} - -/*-----------------------------------------------------------------*/ -/* simRegs - returns value of registers */ -/*-----------------------------------------------------------------*/ -char *simRegs() -{ - char *resp ; - unsigned int rv; - char *rb = regBuff; - int i; - - sendSim("info registers\n"); - - waitForSim(100,NULL); - - resp = simResponse(); - -#if 0 - return resp; - -#else - /*Take this out(2-09-02) cant see as its that useful to reformat, karl.*/ - - /* the response is of the form - XXXXXX R0 R1 R2 R3 R4 R5 R6 R7 ........ - XXXXXX XX . ACC=0xxx dd cc B=0xxx dd cc DPTR= 0xxxxx @DPTR= 0xxx dd cc - XXXXXX XX . PSW= 0xxx CY=[1|0] AC=[0|1] OV=[0|1] P=[1|0] - -Format as of 8-4-01: - 0x00 00 00 00 00 00 00 00 00 ........ - 000000 00 . ACC= 0x00 0 . B= 0x00 DPTR= 0x0000 @DPTR= 0x00 0 . - 000000 00 . PSW= 0x00 CY=0 AC=0 OV=0 P=0 -F 0x006d 75 87 80 MOV PCON,#80 -*/ - - memset(regBuff,0,sizeof(regBuff)); - /* skip the first numerics */ - while (*resp && !isxdigit(*resp)) resp++; - - if (strncmp(resp, "0x", 2)) { - fprintf(stderr, "Error: Format1A\n"); - return regBuff; - } - resp += 2; - while (*resp && isxdigit(*resp)) resp++; - - /* now get the eight registers */ - for (i = 0 ; i < 7 ; i++) { - while (*resp && isspace(*resp)) resp++; - if (!*resp) - break; - rv = strtol(resp,&resp,16); - sprintf(rb,"R%d : 0x%02X %d %c\n",i,rv,rv,(isprint(rv) ? rv : '.')); - rb += strlen(rb); - } - - if (!*resp) return regBuff; - - /* skip till end of line */ - while (*resp && *resp != '\n') resp++; - while (*resp && !isxdigit(*resp)) resp++; - while (*resp && isxdigit(*resp)) resp++; - - /* accumulator value */ - rv = getValueStr(resp,"ACC"); - sprintf(rb,"ACC : 0x%02X %d %c\n",rv,rv,(isprint(rv) ? rv : '.')); - rb += strlen(rb); - - /* value of B */ - rv = getValueStr(resp,"B="); - sprintf(rb,"B : 0x%02X %d %c\n",rv,rv,(isprint(rv) ? rv : '.')); - rb += strlen(rb); - - rv = getValueStr(resp,"DPTR="); - sprintf(rb,"DPTR: 0x%04X %d\n",rv,rv); - rb += strlen(rb); - - rv = getValueStr(resp,"@DPTR="); - sprintf(rb,"@DPTR: 0x%02X %d %c\n", rv,rv,(isprint(rv) ? rv : '.')); - rb += strlen(rb); - - sprintf(rb,"PSW : 0x%02X | CY : %d | AC : %d | OV : %d | P : %d\n", - getValueStr(resp,"PSW="), - getValueStr(resp,"CY="), - getValueStr(resp,"AC="), - getValueStr(resp,"OV="), - getValueStr(resp,"P=")); - - return regBuff; -#endif - -} /*-----------------------------------------------------------------*/ /* closeSimulator - close connection to simulator */ /*-----------------------------------------------------------------*/ -void closeSimulator () +void closeSimulator (void) { +#ifdef _WIN32 + if ( ! simin || ! simout || INVALID_SOCKET == sock ) +#else if ( ! simin || ! simout || sock == -1 ) +#endif { simactive = 0; return; } - sendSim("q\n"); - kill (simPid,SIGKILL); + simactive = 0; + sendSim("quit\n"); fclose (simin); fclose (simout); - shutdown(sock,2); - close(sock); + shutdown(sock,2); +#ifdef _WIN32 + closesocket(sock); sock = -1; + if (NULL != simPid) + TerminateProcess(simPid->hProcess, 0); + // Close process and thread handles. + CloseHandle(simPid->hProcess); + CloseHandle(simPid->hThread); +#else + close(sock); + sock = -1; + if ( simPid > 0 ) + kill (simPid,SIGKILL); +#endif } - -