1 /*-------------------------------------------------------------------------
2 simi.c - source file for simulator interaction
4 Written By - Sandeep Dutta . sandeep.dutta@usa.net (1999)
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 In other words, you are welcome to use, share and improve this program.
21 You are forbidden to forbid anyone else to use, share and improve
22 what you give them. Help stamp out software-hoarding!
23 -------------------------------------------------------------------------*/
27 #ifdef HAVE_SYS_SOCKET_H
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
35 #error "Cannot build debugger without socket support"
37 FILE *simin ; /* stream for simulator input */
38 FILE *simout; /* stream for simulator output */
40 int sock = -1; /* socket descriptor to comm with simulator */
42 static char simibuff[MAX_SIM_BUFF]; /* sim buffer */
43 static char regBuff[MAX_SIM_BUFF];
44 static char *sbp = simibuff; /* simulator buffer pointer */
45 extern char **environ;
48 /*-----------------------------------------------------------------*/
49 /* waitForSim - wait till simulator is done its job */
50 /*-----------------------------------------------------------------*/
51 void waitForSim(int timeout_ms, char *expect)
56 Dprintf(D_simi, ("simi: waitForSim start(%d)\n", timeout_ms));
59 while ((ch = fgetc(simin)) > 0 ) {
63 Dprintf(D_simi, ("waitForSim(%d) got[%s]\n", timeout_ms, simibuff));
67 /*-----------------------------------------------------------------*/
68 /* openSimulator - create a pipe to talk to simulator */
69 /*-----------------------------------------------------------------*/
70 void openSimulator (char **args, int nargs)
72 struct sockaddr_in sin;
75 Dprintf(D_simi, ("simi: openSimulator\n"));
77 /* fork and start the simulator as a subprocess */
78 if ((simPid = fork())) {
79 Dprintf(D_simi, ("simi: simulator pid %d\n",(int) simPid));
82 /* we are in the child process : start the simulator */
83 signal(SIGHUP , SIG_IGN );
84 signal(SIGINT , SIG_IGN );
85 signal(SIGABRT, SIG_IGN );
86 signal(SIGCHLD, SIG_IGN );
88 if (execvp(args[0],args) < 0) {
89 perror("cannot exec simulator");
95 sock = socket(AF_INET,SOCK_STREAM,0);
97 memset(&sin,0,sizeof(sin));
98 sin.sin_family = AF_INET;
99 sin.sin_addr.s_addr = inet_addr("127.0.0.1");
100 sin.sin_port = htons(9756);
103 /* connect to the simulator */
104 if (connect(sock,(struct sockaddr *) &sin, sizeof(sin)) < 0) {
105 /* if failed then wait 1 second & try again
106 do this for 10 secs only */
112 perror("connect failed :");
115 /* go the socket now turn it into a file handle */
116 if (!(simin = fdopen(sock,"r"))) {
117 fprintf(stderr,"cannot open socket for read\n");
121 if (!(simout = fdopen(sock,"w"))) {
122 fprintf(stderr,"cannot open socket for write\n");
126 /* now that we have opened, wait for the prompt */
127 waitForSim(200,NULL);
130 /*-----------------------------------------------------------------*/
131 /* simResponse - returns buffer to simulator's response */
132 /*-----------------------------------------------------------------*/
138 /*-----------------------------------------------------------------*/
139 /* sendSim - sends a command to the simuator */
140 /*-----------------------------------------------------------------*/
141 void sendSim(char *s)
146 Dprintf(D_simi, ("simi: sendSim-->%s", s)); // s has LF at end already
151 int simSetValue (unsigned int addr,char mem, int size, unsigned long val)
153 char buffer[40], *s,*prefix;
184 return; /* set registers !! */
190 sprintf(buffer,"set mem %s 0x%x",prefix,addr);
191 s = buffer + strlen(buffer);
192 for ( i = 0 ; i < size ; i++ )
194 sprintf(s," 0x%x", val & 0xff);
200 waitForSim(100,NULL);
204 /*-----------------------------------------------------------------*/
205 /* simGetValue - get value @ address for mem space */
206 /*-----------------------------------------------------------------*/
207 unsigned long simGetValue (unsigned int addr,char mem, int size)
209 unsigned int b[4] = {0,0,0,0}; /* can be a max of four bytes long */
249 /* create the simulator command */
250 sprintf(buffer,"%s 0x%x \n",prefix,addr);
252 waitForSim(100,NULL);
253 resp = simResponse();
255 /* got the response we need to parse it the response
257 [address] [v] [v] [v] ... special case in
258 case of bit variables which case it becomes
259 [address] [assembler bit address] [v] */
260 /* first skip thru white space */
261 while (isspace(*resp)) resp++ ;
263 if (strncmp(resp, "0x",2) == 0)
266 /* then make the branch for bit variables */
267 /* skip thru the address part */
268 while (isxdigit(*resp)) resp++;
270 if (!strcmp(prefix,"i r"))
273 for (i = 0 ; i < addr ; i++ )
275 while (isspace(*resp)) resp++ ;
277 while (isxdigit(*resp)) resp++;
281 if (!strcmp(prefix,"dump")) {
283 /* skip white space */
284 while (isspace(*resp)) resp++ ;
286 /* skip thru the assembler bit address */
287 while (!isspace(*resp)) resp++;
290 while (isspace(*resp)) resp++ ;
292 /* scan in the value */
293 sscanf(resp,"%d",&b[0]);
296 for (i = 0 ; i < size ; i++ ) {
297 /* skip white space */
298 while (isspace(*resp)) resp++ ;
300 sscanf(resp,"%x",&b[i]);
303 while (isxdigit(*resp)) resp++;
307 return b[0] | b[1] << 8 | b[2] << 16 | b[3] << 24 ;
311 /*-----------------------------------------------------------------*/
312 /* simSetBP - set break point for a given address */
313 /*-----------------------------------------------------------------*/
314 void simSetBP (unsigned int addr)
318 sprintf(buff,"break 0x%x\n",addr);
320 waitForSim(100,NULL);
323 /*-----------------------------------------------------------------*/
324 /* simClearBP - clear a break point */
325 /*-----------------------------------------------------------------*/
326 void simClearBP (unsigned int addr)
330 sprintf(buff,"clear 0x%x\n",addr);
332 waitForSim(100,NULL);
335 /*-----------------------------------------------------------------*/
336 /* simLoadFile - load the simulator file */
337 /*-----------------------------------------------------------------*/
338 void simLoadFile (char *s)
342 sprintf(buff,"l \"%s\"\n",s);
345 waitForSim(500,NULL);
348 /*-----------------------------------------------------------------*/
349 /* simGoTillBp - send 'go' to simulator till a bp then return addr */
350 /*-----------------------------------------------------------------*/
351 unsigned int simGoTillBp ( unsigned int gaddr)
359 /* initial start, start & stop from address 0 */
362 // this program is setting up a bunch of breakpoints automatically
363 // at key places. Like at startup & main() and other function
364 // entry points. So we don't need to setup one here..
365 //sendSim("break 0x0\n");
369 sendSim("run 0x0\n");
370 } else if (gaddr == -1) { /* resume */
375 printf("Error, simGoTillBp > 0!\n");
379 waitForSim(wait_ms, NULL);
381 /* get the simulator response */
382 svr = sr = strdup(simResponse());
384 /* figure out the address of the break point the simulators
385 response in a break point situation is of the form
386 [... F* <addr> <disassembled instruction> ]
387 we will ignore till we get F* then parse the address */
389 if (strncmp(sr,"Stop at",7) == 0) {
395 if (*sr == 'F' && ( *(sr+1) == '*' || *(sr+1) == ' ')) {
404 fprintf(stderr, "Error?, simGoTillBp failed to Stop\n");
408 while (isspace(*sr)) sr++ ;
410 if (sscanf(sr,sfmt,&addr) != 1) {
411 fprintf(stderr, "Error?, simGoTillBp failed to get Addr\n");
418 /*-----------------------------------------------------------------*/
419 /* simReset - reset the simulator */
420 /*-----------------------------------------------------------------*/
424 waitForSim(100,NULL);
427 /*-----------------------------------------------------------------*/
428 /* getValueStr - read a value followed by a string = */
429 /*-----------------------------------------------------------------*/
430 static unsigned int getValueStr (char *src,char *cstr)
432 int i = strlen(cstr);
434 /* look for the string */
435 if (! (src = strstr(src,cstr)))
441 /* look for the digit */
442 while (*src && !isxdigit(*src)) src++;
443 sscanf(src,"%x",&rv);
447 /*-----------------------------------------------------------------*/
448 /* simRegs - returns value of registers */
449 /*-----------------------------------------------------------------*/
457 sendSim("info registers\n");
459 waitForSim(100,NULL);
461 resp = simResponse();
467 /*Take this out(2-09-02) cant see as its that useful to reformat, karl.*/
469 /* the response is of the form
470 XXXXXX R0 R1 R2 R3 R4 R5 R6 R7 ........
471 XXXXXX XX . ACC=0xxx dd cc B=0xxx dd cc DPTR= 0xxxxx @DPTR= 0xxx dd cc
472 XXXXXX XX . PSW= 0xxx CY=[1|0] AC=[0|1] OV=[0|1] P=[1|0]
475 0x00 00 00 00 00 00 00 00 00 ........
476 000000 00 . ACC= 0x00 0 . B= 0x00 DPTR= 0x0000 @DPTR= 0x00 0 .
477 000000 00 . PSW= 0x00 CY=0 AC=0 OV=0 P=0
478 F 0x006d 75 87 80 MOV PCON,#80
481 memset(regBuff,0,sizeof(regBuff));
482 /* skip the first numerics */
483 while (*resp && !isxdigit(*resp)) resp++;
485 if (strncmp(resp, "0x", 2)) {
486 fprintf(stderr, "Error: Format1A\n");
490 while (*resp && isxdigit(*resp)) resp++;
492 /* now get the eight registers */
493 for (i = 0 ; i < 7 ; i++) {
494 while (*resp && isspace(*resp)) resp++;
497 rv = strtol(resp,&resp,16);
498 sprintf(rb,"R%d : 0x%02X %d %c\n",i,rv,rv,(isprint(rv) ? rv : '.'));
502 if (!*resp) return regBuff;
504 /* skip till end of line */
505 while (*resp && *resp != '\n') resp++;
506 while (*resp && !isxdigit(*resp)) resp++;
507 while (*resp && isxdigit(*resp)) resp++;
509 /* accumulator value */
510 rv = getValueStr(resp,"ACC");
511 sprintf(rb,"ACC : 0x%02X %d %c\n",rv,rv,(isprint(rv) ? rv : '.'));
515 rv = getValueStr(resp,"B=");
516 sprintf(rb,"B : 0x%02X %d %c\n",rv,rv,(isprint(rv) ? rv : '.'));
519 rv = getValueStr(resp,"DPTR=");
520 sprintf(rb,"DPTR: 0x%04X %d\n",rv,rv);
523 rv = getValueStr(resp,"@DPTR=");
524 sprintf(rb,"@DPTR: 0x%02X %d %c\n", rv,rv,(isprint(rv) ? rv : '.'));
527 sprintf(rb,"PSW : 0x%02X | CY : %d | AC : %d | OV : %d | P : %d\n",
528 getValueStr(resp,"PSW="),
529 getValueStr(resp,"CY="),
530 getValueStr(resp,"AC="),
531 getValueStr(resp,"OV="),
532 getValueStr(resp,"P="));
539 /*-----------------------------------------------------------------*/
540 /* closeSimulator - close connection to simulator */
541 /*-----------------------------------------------------------------*/
542 void closeSimulator ()
544 if ( ! simin || ! simout || sock == -1 )
550 kill (simPid,SIGKILL);