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 */
208 /* create the simulator command */
209 sprintf(buffer,"%s 0x%x \n",prefix,addr);
212 resp = simResponse();
214 /* got the response we need to parse it the response
216 [address] [v] [v] [v] ... special case in
217 case of bit variables which case it becomes
218 [address] [assembler bit address] [v] */
219 /* first skip thru white space */
220 while (isspace(*resp)) resp++ ;
222 /* then make the branch for bit variables */
223 /* skip thru the address part */
224 while (isxdigit(*resp)) resp++;
226 if (!strcmp(prefix,"db")) {
228 /* skip white space */
229 while (isspace(*resp)) resp++ ;
231 /* skip thru the assembler bit address */
232 while (!isspace(*resp)) resp++;
235 while (isspace(*resp)) resp++ ;
237 /* scan in the value */
238 sscanf(resp,"%d",&b[0]);
241 for (i = 0 ; i < size ; i++ ) {
242 /* skip white space */
243 while (isspace(*resp)) resp++ ;
245 sscanf(resp,"%x",&b[i]);
248 while (isxdigit(*resp)) resp++;
252 return b[0] | b[1] << 8 | b[2] << 16 | b[3] << 24 ;
256 /*-----------------------------------------------------------------*/
257 /* simSetBP - set break point for a given address */
258 /*-----------------------------------------------------------------*/
259 void simSetBP (unsigned int addr)
263 sprintf(buff,"break 0x%x\n",addr);
268 /*-----------------------------------------------------------------*/
269 /* simClearBP - clear a break point */
270 /*-----------------------------------------------------------------*/
271 void simClearBP (unsigned int addr)
275 sprintf(buff,"clear 0x%x\n",addr);
280 /*-----------------------------------------------------------------*/
281 /* simLoadFile - load the simulator file */
282 /*-----------------------------------------------------------------*/
283 void simLoadFile (char *s)
287 sprintf(buff,"l \"%s\"\n",s);
293 /*-----------------------------------------------------------------*/
294 /* simGoTillBp - send 'go' to simulator till a bp then return addr */
295 /*-----------------------------------------------------------------*/
296 unsigned int simGoTillBp ( unsigned int gaddr)
302 /* kpb: new code 8-03-01 */
304 /* initial start, start & stop from address 0 */
306 // this program is setting up a bunch of breakpoints automatically
307 // at key places. Like at startup & main() and other function
308 // entry points. So we don't need to setup one here..
309 //sendSim("break 0x0\n");
313 sendSim("run 0x0\n");
314 sleep(1); /* do I need this? */
315 } else if (gaddr == -1) { /* resume */
317 // try adding this(kpb)
325 printf("Error, simGoTillBp > 0!\n");
332 sprintf(buf,"g 0x%x\n",gaddr);
340 /* get the simulator response */
341 svr = sr = strdup(simResponse());
343 /* figure out the address of the break point the simulators
344 response in a break point situation is of the form
345 [... F* <addr> <disassembled instruction> ]
346 we will ignore till we get F* then parse the address */
349 if (strncmp(sr,"Stop at",7) == 0) {
355 if (*sr == 'F' && ( *(sr+1) == '*' || *(sr+1) == ' ')) {
364 fprintf(stderr, "Error?, simGoTillBp failed to Stop\n");
368 while (isspace(*sr)) sr++ ;
370 if (sscanf(sr,sfmt,&addr) != 1) {
371 fprintf(stderr, "Error?, simGoTillBp failed to get Addr\n");
378 /*-----------------------------------------------------------------*/
379 /* simReset - reset the simulator */
380 /*-----------------------------------------------------------------*/
387 /*-----------------------------------------------------------------*/
388 /* getValueStr - read a value followed by a string = */
389 /*-----------------------------------------------------------------*/
390 static unsigned int getValueStr (char *src,char *cstr)
392 int i = strlen(cstr);
394 /* look for the string */
395 if (! (src = strstr(src,cstr)))
401 /* look for the digit */
402 while (*src && !isxdigit(*src)) src++;
403 sscanf(src,"%x",&rv);
407 /*-----------------------------------------------------------------*/
408 /* simRegs - returns value of registers */
409 /*-----------------------------------------------------------------*/
417 sendSim("info registers\n");
418 //kpb(8-5-01) sendSim("dr\n");
421 /* make it some more readable */
422 resp = simResponse();
424 /* the response is of the form
425 XXXXXX R0 R1 R2 R3 R4 R5 R6 R7 ........
426 XXXXXX XX . ACC=0xxx dd cc B=0xxx dd cc DPTR= 0xxxxx @DPTR= 0xxx dd cc
427 XXXXXX XX . PSW= 0xxx CY=[1|0] AC=[0|1] OV=[0|1] P=[1|0]
430 0x00 00 00 00 00 00 00 00 00 ........
431 000000 00 . ACC= 0x00 0 . B= 0x00 DPTR= 0x0000 @DPTR= 0x00 0 .
432 000000 00 . PSW= 0x00 CY=0 AC=0 OV=0 P=0
433 F 0x006d 75 87 80 MOV PCON,#80
436 memset(regBuff,0,sizeof(regBuff));
437 /* skip the first numerics */
438 while (*resp && !isxdigit(*resp)) resp++;
440 if (strncmp(resp, "0x", 2)) {
441 fprintf(stderr, "Error: Format1A\n");
445 while (*resp && isxdigit(*resp)) resp++;
447 /* now get the eight registers */
448 for (i = 0 ; i < 7 ; i++) {
449 while (*resp && isspace(*resp)) resp++;
452 rv = strtol(resp,&resp,16);
453 sprintf(rb,"R%d : 0x%02X %d %c\n",i,rv,rv,(isprint(rv) ? rv : '.'));
457 if (!*resp) return regBuff;
459 /* skip till end of line */
460 while (*resp && *resp != '\n') resp++;
461 while (*resp && !isxdigit(*resp)) resp++;
462 while (*resp && isxdigit(*resp)) resp++;
464 /* accumulator value */
465 rv = getValueStr(resp,"ACC");
466 sprintf(rb,"ACC : 0x%02X %d %c\n",rv,rv,(isprint(rv) ? rv : '.'));
470 rv = getValueStr(resp,"B=");
471 sprintf(rb,"B : 0x%02X %d %c\n",rv,rv,(isprint(rv) ? rv : '.'));
474 rv = getValueStr(resp,"DPTR=");
475 sprintf(rb,"DPTR: 0x%04X %d\n",rv,rv);
478 rv = getValueStr(resp,"@DPTR=");
479 sprintf(rb,"@DPTR: 0x%02X %d %c\n", rv,rv,(isprint(rv) ? rv : '.'));
482 sprintf(rb,"PSW : 0x%02X | CY : %d | AC : %d | OV : %d | P : %d\n",
483 getValueStr(resp,"PSW="),
484 getValueStr(resp,"CY="),
485 getValueStr(resp,"AC="),
486 getValueStr(resp,"OV="),
487 getValueStr(resp,"P="));
495 /*-----------------------------------------------------------------*/
496 /* closeSimulator - close connection to simulator */
497 /*-----------------------------------------------------------------*/
498 void closeSimulator ()
502 kill (simPid,SIGKILL);