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 */
58 fprintf(stderr,"stderr--> reading from sim\n");
61 while ((ch = fgetc(simin))) {
68 fprintf(stderr,"stderr--> reading from sim done\n");
74 /*-----------------------------------------------------------------*/
75 /* waitForSim - wait till simulator is done its job */
76 /*-----------------------------------------------------------------*/
82 /*-----------------------------------------------------------------*/
83 /* openSimulator - create a pipe to talk to simulator */
84 /*-----------------------------------------------------------------*/
85 void openSimulator (char **args, int nargs)
87 struct sockaddr_in sin;
90 char *simargs[32] = { "s51","-P","-r 9756", NULL };
92 /* create the arguments */
93 for ( i = 0 ; i < nargs ;i++) {
94 simargs[i+3] = args[i];
98 /* fork and start the simulator as a subprocess */
99 if ((simPid = fork())) {
101 printf("simulator pid %d\n",(int) simPid);
106 /* we are in the child process : start the simulator */
107 if (execvp("s51",simargs) < 0) {
108 perror("cannot exec simulator");
114 sock = socket(AF_INET,SOCK_STREAM,0);
116 memset(&sin,0,sizeof(sin));
117 sin.sin_family = AF_INET;
118 sin.sin_addr.s_addr = inet_addr("127.0.0.1");
119 sin.sin_port = htons(9756);
122 /* connect to the simulator */
123 if (connect(sock,(struct sockaddr *) &sin, sizeof(sin)) < 0) {
124 /* if failed then wait 1 second & try again
125 do this for 10 secs only */
131 perror("connect failed :");
134 /* go the socket now turn it into a file handle */
135 if (!(simin = fdopen(sock,"r"))) {
136 fprintf(stderr,"cannot open socket for read\n");
140 if (!(simout = fdopen(sock,"w"))) {
141 fprintf(stderr,"cannot open socket for write\n");
145 /* now that we have opend wait for the prompt */
149 /*-----------------------------------------------------------------*/
150 /* simResponse - returns buffer to simulator's response */
151 /*-----------------------------------------------------------------*/
157 /*-----------------------------------------------------------------*/
158 /* sendSim - sends a command to the simuator */
159 /*-----------------------------------------------------------------*/
160 void sendSim(char *s)
166 /*-----------------------------------------------------------------*/
167 /* simGetValue - get value @ address for mem space */
168 /*-----------------------------------------------------------------*/
169 unsigned long simGetValue (unsigned int addr,char mem, int size)
171 unsigned int b[4] = {0,0,0,0}; /* can be a max of four bytes long */
204 /* create the simulator command */
205 sprintf(buffer,"%s 0x%x \n",prefix,addr);
208 resp = simResponse();
210 /* got the response we need to parse it the response
212 [address] [v] [v] [v] ... special case in
213 case of bit variables which case it becomes
214 [address] [assembler bit address] [v] */
215 /* first skip thru white space */
216 while (isspace(*resp)) resp++ ;
218 /* then make the branch for bit variables */
219 /* skip thru the address part */
220 while (isxdigit(*resp)) resp++;
222 if (!strcmp(prefix,"db")) {
224 /* skip white space */
225 while (isspace(*resp)) resp++ ;
227 /* skip thru the assembler bit address */
228 while (!isspace(*resp)) resp++;
231 while (isspace(*resp)) resp++ ;
233 /* scan in the value */
234 sscanf(resp,"%d",&b[0]);
237 for (i = 0 ; i < size ; i++ ) {
238 /* skip white space */
239 while (isspace(*resp)) resp++ ;
241 sscanf(resp,"%x",&b[i]);
244 while (isxdigit(*resp)) resp++;
248 return b[0] | b[1] << 8 | b[2] << 16 | b[3] << 24 ;
252 /*-----------------------------------------------------------------*/
253 /* simSetBP - set break point for a given address */
254 /*-----------------------------------------------------------------*/
255 void simSetBP (unsigned int addr)
259 sprintf(buff,"break 0x%x\n",addr);
264 /*-----------------------------------------------------------------*/
265 /* simClearBP - clear a break point */
266 /*-----------------------------------------------------------------*/
267 void simClearBP (unsigned int addr)
271 sprintf(buff,"clear 0x%x\n",addr);
276 /*-----------------------------------------------------------------*/
277 /* simLoadFile - load the simulator file */
278 /*-----------------------------------------------------------------*/
279 void simLoadFile (char *s)
283 sprintf(buff,"l %s\n",s);
289 /*-----------------------------------------------------------------*/
290 /* simGoTillBp - send 'go' to simulator till a bp then return addr */
291 /*-----------------------------------------------------------------*/
292 unsigned int simGoTillBp ( unsigned int gaddr)
300 sprintf(buf,"g 0x%x\n",gaddr);
307 /* get the simulator response */
308 svr = sr = strdup(simResponse());
310 /* figure out the address of the break point the simulators
311 response in a break point situation is of the form
312 [... F* <addr> <disassembled instruction> ]
313 we will ignore till we get F* then parse the address */
316 if (strncmp(sr,"Stop at",7) == 0) {
322 if (*sr == 'F' && ( *(sr+1) == '*' || *(sr+1) == ' ')) {
332 while (isspace(*sr)) sr++ ;
334 if (sscanf(sr,sfmt,&addr) != 1)
342 /*-----------------------------------------------------------------*/
343 /* simReset - reset the simulator */
344 /*-----------------------------------------------------------------*/
351 /*-----------------------------------------------------------------*/
352 /* getValueStr - read a value followed by a string = */
353 /*-----------------------------------------------------------------*/
354 static unsigned int getValueStr (char *src,char *cstr)
356 int i = strlen(cstr);
358 /* look for the string */
359 if (! (src = strstr(src,cstr)))
365 /* look for the digit */
366 while (*src && !isxdigit(*src)) src++;
367 sscanf(src,"%x",&rv);
371 /*-----------------------------------------------------------------*/
372 /* simRegs - returns value of registers */
373 /*-----------------------------------------------------------------*/
383 /* make it some more readable */
384 resp = simResponse();
386 /* the response is of the form
387 XXXXXX R0 R1 R2 R3 R4 R5 R6 R7 ........
388 XXXXXX XX . ACC=0xxx dd cc B=0xxx dd cc DPTR= 0xxxxx @DPTR= 0xxx dd cc
389 XXXXXX XX . PSW= 0xxx CY=[1|0] AC=[0|1] OV=[0|1] P=[1|0] */
390 memset(regBuff,0,sizeof(regBuff));
391 /* skip the first numerics */
392 while (*resp && !isxdigit(*resp)) resp++;
393 while (*resp && isxdigit(*resp)) resp++;
395 /* now get the eight registers */
396 for (i = 0 ; i < 7 ; i++) {
397 while (*resp && isspace(*resp)) resp++;
400 rv = strtol(resp,&resp,16);
401 sprintf(rb,"R%d : 0x%02X %d %c\n",i,rv,rv,(isprint(rv) ? rv : '.'));
405 if (!*resp) return regBuff;
407 /* skip till end of line */
408 while (*resp && *resp != '\n') resp++;
409 while (*resp && !isxdigit(*resp)) resp++;
410 while (*resp && isxdigit(*resp)) resp++;
412 /* accumulator value */
413 rv = getValueStr(resp,"ACC");
414 sprintf(rb,"ACC : 0x%02X %d %c\n",rv,rv,(isprint(rv) ? rv : '.'));
418 rv = getValueStr(resp,"B=");
419 sprintf(rb,"B : 0x%02X %d %c\n",rv,rv,(isprint(rv) ? rv : '.'));
422 rv = getValueStr(resp,"DPTR=");
423 sprintf(rb,"DPTR: 0x%04X %d\n",rv,rv);
426 rv = getValueStr(resp,"@DPTR=");
427 sprintf(rb,"@DPTR: 0x%02X %d %c\n", rv,rv,(isprint(rv) ? rv : '.'));
430 sprintf(rb,"PSW : 0x%02X | CY : %d | AC : %d | OV : %d | P : %d\n",
431 getValueStr(resp,"PSW="),
432 getValueStr(resp,"CY="),
433 getValueStr(resp,"AC="),
434 getValueStr(resp,"OV="),
435 getValueStr(resp,"P="));
443 /*-----------------------------------------------------------------*/
444 /* closeSimulator - close connection to simulator */
445 /*-----------------------------------------------------------------*/
446 void closeSimulator ()
450 kill (simPid,SIGKILL);