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 ; /* 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;
47 /*-----------------------------------------------------------------*/
48 /* readSim - reads one character into simulator buffer */
49 /*-----------------------------------------------------------------*/
50 void readSim(int resetp)
53 /* if reset required then point to beginning of buffer */
57 Dprintf(D_simi, ("readSim: reading from sim["));
59 while ((ch = fgetc(simin))) {
70 Dprintf(D_simi, ("] end readSim\n"));
75 /*-----------------------------------------------------------------*/
76 /* waitForSim - wait till simulator is done its job */
77 /*-----------------------------------------------------------------*/
83 /*-----------------------------------------------------------------*/
84 /* openSimulator - create a pipe to talk to simulator */
85 /*-----------------------------------------------------------------*/
86 void openSimulator (char **args, int nargs)
88 struct sockaddr_in sin;
93 char *simargs[32] = { "s51","-P","-r 9756", NULL };
95 /* create the arguments */
96 for ( i = 0 ; i < nargs ;i++) {
97 simargs[i+3] = args[i];
102 /* fork and start the simulator as a subprocess */
103 if ((simPid = fork())) {
104 Dprintf(D_simi, ("simulator pid %d\n",(int) simPid));
108 /* we are in the child process : start the simulator */
109 // if (execvp("s51",simargs) < 0) {
110 if (execvp(args[0],args) < 0) {
111 perror("cannot exec simulator");
117 sock = socket(AF_INET,SOCK_STREAM,0);
119 memset(&sin,0,sizeof(sin));
120 sin.sin_family = AF_INET;
121 sin.sin_addr.s_addr = inet_addr("127.0.0.1");
122 sin.sin_port = htons(9756);
125 /* connect to the simulator */
126 if (connect(sock,(struct sockaddr *) &sin, sizeof(sin)) < 0) {
127 /* if failed then wait 1 second & try again
128 do this for 10 secs only */
134 perror("connect failed :");
137 /* go the socket now turn it into a file handle */
138 if (!(simin = fdopen(sock,"r"))) {
139 fprintf(stderr,"cannot open socket for read\n");
143 if (!(simout = fdopen(sock,"w"))) {
144 fprintf(stderr,"cannot open socket for write\n");
148 /* now that we have opend wait for the prompt */
152 /*-----------------------------------------------------------------*/
153 /* simResponse - returns buffer to simulator's response */
154 /*-----------------------------------------------------------------*/
160 /*-----------------------------------------------------------------*/
161 /* sendSim - sends a command to the simuator */
162 /*-----------------------------------------------------------------*/
163 void sendSim(char *s)
165 Dprintf(D_simi, ("sendSim-->%s", s)); // s has LF at end already
170 /*-----------------------------------------------------------------*/
171 /* simGetValue - get value @ address for mem space */
172 /*-----------------------------------------------------------------*/
173 unsigned long simGetValue (unsigned int addr,char mem, int size)
175 unsigned int b[4] = {0,0,0,0}; /* can be a max of four bytes long */
209 /* create the simulator command */
210 sprintf(buffer,"%s 0x%x \n",prefix,addr);
213 resp = simResponse();
215 /* got the response we need to parse it the response
217 [address] [v] [v] [v] ... special case in
218 case of bit variables which case it becomes
219 [address] [assembler bit address] [v] */
220 /* first skip thru white space */
221 while (isspace(*resp)) resp++ ;
223 if (strncmp(resp, "0x",2) == 0)
226 /* then make the branch for bit variables */
227 /* skip thru the address part */
228 while (isxdigit(*resp)) resp++;
230 if (!strcmp(prefix,"dump")) {
232 /* skip white space */
233 while (isspace(*resp)) resp++ ;
235 /* skip thru the assembler bit address */
236 while (!isspace(*resp)) resp++;
239 while (isspace(*resp)) resp++ ;
241 /* scan in the value */
242 sscanf(resp,"%d",&b[0]);
245 for (i = 0 ; i < size ; i++ ) {
246 /* skip white space */
247 while (isspace(*resp)) resp++ ;
249 sscanf(resp,"%x",&b[i]);
252 while (isxdigit(*resp)) resp++;
256 return b[0] | b[1] << 8 | b[2] << 16 | b[3] << 24 ;
260 /*-----------------------------------------------------------------*/
261 /* simSetBP - set break point for a given address */
262 /*-----------------------------------------------------------------*/
263 void simSetBP (unsigned int addr)
267 sprintf(buff,"break 0x%x\n",addr);
272 /*-----------------------------------------------------------------*/
273 /* simClearBP - clear a break point */
274 /*-----------------------------------------------------------------*/
275 void simClearBP (unsigned int addr)
279 sprintf(buff,"clear 0x%x\n",addr);
284 /*-----------------------------------------------------------------*/
285 /* simLoadFile - load the simulator file */
286 /*-----------------------------------------------------------------*/
287 void simLoadFile (char *s)
291 sprintf(buff,"l \"%s\"\n",s);
297 /*-----------------------------------------------------------------*/
298 /* simGoTillBp - send 'go' to simulator till a bp then return addr */
299 /*-----------------------------------------------------------------*/
300 unsigned int simGoTillBp ( unsigned int gaddr)
306 /* kpb: new code 8-03-01 */
308 /* initial start, start & stop from address 0 */
310 // this program is setting up a bunch of breakpoints automatically
311 // at key places. Like at startup & main() and other function
312 // entry points. So we don't need to setup one here..
313 //sendSim("break 0x0\n");
317 sendSim("run 0x0\n");
318 sleep(1); /* do I need this? */
319 } else if (gaddr == -1) { /* resume */
321 // try adding this(kpb)
329 printf("Error, simGoTillBp > 0!\n");
336 sprintf(buf,"g 0x%x\n",gaddr);
344 /* get the simulator response */
345 svr = sr = strdup(simResponse());
347 /* figure out the address of the break point the simulators
348 response in a break point situation is of the form
349 [... F* <addr> <disassembled instruction> ]
350 we will ignore till we get F* then parse the address */
353 if (strncmp(sr,"Stop at",7) == 0) {
359 if (*sr == 'F' && ( *(sr+1) == '*' || *(sr+1) == ' ')) {
368 fprintf(stderr, "Error?, simGoTillBp failed to Stop\n");
372 while (isspace(*sr)) sr++ ;
374 if (sscanf(sr,sfmt,&addr) != 1) {
375 fprintf(stderr, "Error?, simGoTillBp failed to get Addr\n");
382 /*-----------------------------------------------------------------*/
383 /* simReset - reset the simulator */
384 /*-----------------------------------------------------------------*/
391 /*-----------------------------------------------------------------*/
392 /* getValueStr - read a value followed by a string = */
393 /*-----------------------------------------------------------------*/
394 static unsigned int getValueStr (char *src,char *cstr)
396 int i = strlen(cstr);
398 /* look for the string */
399 if (! (src = strstr(src,cstr)))
405 /* look for the digit */
406 while (*src && !isxdigit(*src)) src++;
407 sscanf(src,"%x",&rv);
411 /*-----------------------------------------------------------------*/
412 /* simRegs - returns value of registers */
413 /*-----------------------------------------------------------------*/
421 sendSim("info registers\n");
422 //kpb(8-5-01) sendSim("dr\n");
425 /* make it some more readable */
426 resp = simResponse();
431 Take this out(2-09-02) cant see as its that useful to reformat, karl.
433 /* the response is of the form
434 XXXXXX R0 R1 R2 R3 R4 R5 R6 R7 ........
435 XXXXXX XX . ACC=0xxx dd cc B=0xxx dd cc DPTR= 0xxxxx @DPTR= 0xxx dd cc
436 XXXXXX XX . PSW= 0xxx CY=[1|0] AC=[0|1] OV=[0|1] P=[1|0]
439 0x00 00 00 00 00 00 00 00 00 ........
440 000000 00 . ACC= 0x00 0 . B= 0x00 DPTR= 0x0000 @DPTR= 0x00 0 .
441 000000 00 . PSW= 0x00 CY=0 AC=0 OV=0 P=0
442 F 0x006d 75 87 80 MOV PCON,#80
445 memset(regBuff,0,sizeof(regBuff));
446 /* skip the first numerics */
447 while (*resp && !isxdigit(*resp)) resp++;
449 if (strncmp(resp, "0x", 2)) {
450 fprintf(stderr, "Error: Format1A\n");
454 while (*resp && isxdigit(*resp)) resp++;
456 /* now get the eight registers */
457 for (i = 0 ; i < 7 ; i++) {
458 while (*resp && isspace(*resp)) resp++;
461 rv = strtol(resp,&resp,16);
462 sprintf(rb,"R%d : 0x%02X %d %c\n",i,rv,rv,(isprint(rv) ? rv : '.'));
466 if (!*resp) return regBuff;
468 /* skip till end of line */
469 while (*resp && *resp != '\n') resp++;
470 while (*resp && !isxdigit(*resp)) resp++;
471 while (*resp && isxdigit(*resp)) resp++;
473 /* accumulator value */
474 rv = getValueStr(resp,"ACC");
475 sprintf(rb,"ACC : 0x%02X %d %c\n",rv,rv,(isprint(rv) ? rv : '.'));
479 rv = getValueStr(resp,"B=");
480 sprintf(rb,"B : 0x%02X %d %c\n",rv,rv,(isprint(rv) ? rv : '.'));
483 rv = getValueStr(resp,"DPTR=");
484 sprintf(rb,"DPTR: 0x%04X %d\n",rv,rv);
487 rv = getValueStr(resp,"@DPTR=");
488 sprintf(rb,"@DPTR: 0x%02X %d %c\n", rv,rv,(isprint(rv) ? rv : '.'));
491 sprintf(rb,"PSW : 0x%02X | CY : %d | AC : %d | OV : %d | P : %d\n",
492 getValueStr(resp,"PSW="),
493 getValueStr(resp,"CY="),
494 getValueStr(resp,"AC="),
495 getValueStr(resp,"OV="),
496 getValueStr(resp,"P="));
503 /*-----------------------------------------------------------------*/
504 /* closeSimulator - close connection to simulator */
505 /*-----------------------------------------------------------------*/
506 void closeSimulator ()
510 kill (simPid,SIGKILL);