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;
50 #define MS_SLEEP(_ms) usleep(_ms * 1000)
53 /*-----------------------------------------------------------------*/
54 /* waitForSim - wait till simulator is done its job */
55 /*-----------------------------------------------------------------*/
56 void waitForSim(int timeout_ms, char *expect)
61 Dprintf(D_simi, ("waitForSim start(%d)\n", timeout_ms));
64 // MS_SLEEP(timeout_ms); dont need, in blocking mode.
66 while ((ch = fgetc(simin))) {
70 Dprintf(D_simi, ("waitForSim(%d) got[%s]\n", timeout_ms, simibuff));
73 hmmmm, I guess we are not running non-blocking, we may still
74 need this code...Im not sure how the above works, it must block
75 until something gets into the buffer, then fgetc() reads down the
78 while ((ch = fgetc(simin))) {
82 printf("got1[%s]\n", simibuff);
88 if (strstr(expect, sbp)) {
91 } else if (sbp != simibuff) {
95 /* pull in data one more time after delay to try and
96 guarentee we pull in complete responce line */
97 if (timeout_ms <= 0) {
99 while ((ch = fgetc(simin))) {
103 printf("got2[%s]\n", simibuff);
106 while (timeout_ms > 0);
112 /*-----------------------------------------------------------------*/
113 /* openSimulator - create a pipe to talk to simulator */
114 /*-----------------------------------------------------------------*/
115 void openSimulator (char **args, int nargs)
117 struct sockaddr_in sin;
120 Dprintf(D_simi, ("openSimulator\n"));
122 /* fork and start the simulator as a subprocess */
123 if ((simPid = fork())) {
124 Dprintf(D_simi, ("simulator pid %d\n",(int) simPid));
127 /* we are in the child process : start the simulator */
128 if (execvp(args[0],args) < 0) {
129 perror("cannot exec simulator");
135 sock = socket(AF_INET,SOCK_STREAM,0);
137 memset(&sin,0,sizeof(sin));
138 sin.sin_family = AF_INET;
139 sin.sin_addr.s_addr = inet_addr("127.0.0.1");
140 sin.sin_port = htons(9756);
143 /* connect to the simulator */
144 if (connect(sock,(struct sockaddr *) &sin, sizeof(sin)) < 0) {
145 /* if failed then wait 1 second & try again
146 do this for 10 secs only */
152 perror("connect failed :");
155 /* go the socket now turn it into a file handle */
156 if (!(simin = fdopen(sock,"r"))) {
157 fprintf(stderr,"cannot open socket for read\n");
161 if (!(simout = fdopen(sock,"w"))) {
162 fprintf(stderr,"cannot open socket for write\n");
166 /* now that we have opened, wait for the prompt */
167 waitForSim(200,NULL);
170 /*-----------------------------------------------------------------*/
171 /* simResponse - returns buffer to simulator's response */
172 /*-----------------------------------------------------------------*/
178 /*-----------------------------------------------------------------*/
179 /* sendSim - sends a command to the simuator */
180 /*-----------------------------------------------------------------*/
181 void sendSim(char *s)
183 Dprintf(D_simi, ("sendSim-->%s", s)); // s has LF at end already
188 /*-----------------------------------------------------------------*/
189 /* simGetValue - get value @ address for mem space */
190 /*-----------------------------------------------------------------*/
191 unsigned long simGetValue (unsigned int addr,char mem, int size)
193 unsigned int b[4] = {0,0,0,0}; /* can be a max of four bytes long */
227 /* create the simulator command */
228 sprintf(buffer,"%s 0x%x \n",prefix,addr);
230 waitForSim(100,NULL);
231 resp = simResponse();
233 /* got the response we need to parse it the response
235 [address] [v] [v] [v] ... special case in
236 case of bit variables which case it becomes
237 [address] [assembler bit address] [v] */
238 /* first skip thru white space */
239 while (isspace(*resp)) resp++ ;
241 if (strncmp(resp, "0x",2) == 0)
244 /* then make the branch for bit variables */
245 /* skip thru the address part */
246 while (isxdigit(*resp)) resp++;
248 if (!strcmp(prefix,"dump")) {
250 /* skip white space */
251 while (isspace(*resp)) resp++ ;
253 /* skip thru the assembler bit address */
254 while (!isspace(*resp)) resp++;
257 while (isspace(*resp)) resp++ ;
259 /* scan in the value */
260 sscanf(resp,"%d",&b[0]);
263 for (i = 0 ; i < size ; i++ ) {
264 /* skip white space */
265 while (isspace(*resp)) resp++ ;
267 sscanf(resp,"%x",&b[i]);
270 while (isxdigit(*resp)) resp++;
274 return b[0] | b[1] << 8 | b[2] << 16 | b[3] << 24 ;
278 /*-----------------------------------------------------------------*/
279 /* simSetBP - set break point for a given address */
280 /*-----------------------------------------------------------------*/
281 void simSetBP (unsigned int addr)
285 sprintf(buff,"break 0x%x\n",addr);
287 waitForSim(100,NULL);
290 /*-----------------------------------------------------------------*/
291 /* simClearBP - clear a break point */
292 /*-----------------------------------------------------------------*/
293 void simClearBP (unsigned int addr)
297 sprintf(buff,"clear 0x%x\n",addr);
299 waitForSim(100,NULL);
302 /*-----------------------------------------------------------------*/
303 /* simLoadFile - load the simulator file */
304 /*-----------------------------------------------------------------*/
305 void simLoadFile (char *s)
309 sprintf(buff,"l \"%s\"\n",s);
312 waitForSim(500,NULL);
315 /*-----------------------------------------------------------------*/
316 /* simGoTillBp - send 'go' to simulator till a bp then return addr */
317 /*-----------------------------------------------------------------*/
318 unsigned int simGoTillBp ( unsigned int gaddr)
326 /* initial start, start & stop from address 0 */
329 // this program is setting up a bunch of breakpoints automatically
330 // at key places. Like at startup & main() and other function
331 // entry points. So we don't need to setup one here..
332 //sendSim("break 0x0\n");
336 sendSim("run 0x0\n");
337 } else if (gaddr == -1) { /* resume */
342 printf("Error, simGoTillBp > 0!\n");
346 waitForSim(wait_ms, NULL);
348 /* get the simulator response */
349 svr = sr = strdup(simResponse());
351 /* figure out the address of the break point the simulators
352 response in a break point situation is of the form
353 [... F* <addr> <disassembled instruction> ]
354 we will ignore till we get F* then parse the address */
356 if (strncmp(sr,"Stop at",7) == 0) {
362 if (*sr == 'F' && ( *(sr+1) == '*' || *(sr+1) == ' ')) {
371 fprintf(stderr, "Error?, simGoTillBp failed to Stop\n");
375 while (isspace(*sr)) sr++ ;
377 if (sscanf(sr,sfmt,&addr) != 1) {
378 fprintf(stderr, "Error?, simGoTillBp failed to get Addr\n");
385 /*-----------------------------------------------------------------*/
386 /* simReset - reset the simulator */
387 /*-----------------------------------------------------------------*/
391 waitForSim(100,NULL);
394 /*-----------------------------------------------------------------*/
395 /* getValueStr - read a value followed by a string = */
396 /*-----------------------------------------------------------------*/
397 static unsigned int getValueStr (char *src,char *cstr)
399 int i = strlen(cstr);
401 /* look for the string */
402 if (! (src = strstr(src,cstr)))
408 /* look for the digit */
409 while (*src && !isxdigit(*src)) src++;
410 sscanf(src,"%x",&rv);
414 /*-----------------------------------------------------------------*/
415 /* simRegs - returns value of registers */
416 /*-----------------------------------------------------------------*/
424 sendSim("info registers\n");
426 waitForSim(100,NULL);
428 resp = simResponse();
433 Take this out(2-09-02) cant see as its that useful to reformat, karl.
435 /* the response is of the form
436 XXXXXX R0 R1 R2 R3 R4 R5 R6 R7 ........
437 XXXXXX XX . ACC=0xxx dd cc B=0xxx dd cc DPTR= 0xxxxx @DPTR= 0xxx dd cc
438 XXXXXX XX . PSW= 0xxx CY=[1|0] AC=[0|1] OV=[0|1] P=[1|0]
441 0x00 00 00 00 00 00 00 00 00 ........
442 000000 00 . ACC= 0x00 0 . B= 0x00 DPTR= 0x0000 @DPTR= 0x00 0 .
443 000000 00 . PSW= 0x00 CY=0 AC=0 OV=0 P=0
444 F 0x006d 75 87 80 MOV PCON,#80
447 memset(regBuff,0,sizeof(regBuff));
448 /* skip the first numerics */
449 while (*resp && !isxdigit(*resp)) resp++;
451 if (strncmp(resp, "0x", 2)) {
452 fprintf(stderr, "Error: Format1A\n");
456 while (*resp && isxdigit(*resp)) resp++;
458 /* now get the eight registers */
459 for (i = 0 ; i < 7 ; i++) {
460 while (*resp && isspace(*resp)) resp++;
463 rv = strtol(resp,&resp,16);
464 sprintf(rb,"R%d : 0x%02X %d %c\n",i,rv,rv,(isprint(rv) ? rv : '.'));
468 if (!*resp) return regBuff;
470 /* skip till end of line */
471 while (*resp && *resp != '\n') resp++;
472 while (*resp && !isxdigit(*resp)) resp++;
473 while (*resp && isxdigit(*resp)) resp++;
475 /* accumulator value */
476 rv = getValueStr(resp,"ACC");
477 sprintf(rb,"ACC : 0x%02X %d %c\n",rv,rv,(isprint(rv) ? rv : '.'));
481 rv = getValueStr(resp,"B=");
482 sprintf(rb,"B : 0x%02X %d %c\n",rv,rv,(isprint(rv) ? rv : '.'));
485 rv = getValueStr(resp,"DPTR=");
486 sprintf(rb,"DPTR: 0x%04X %d\n",rv,rv);
489 rv = getValueStr(resp,"@DPTR=");
490 sprintf(rb,"@DPTR: 0x%02X %d %c\n", rv,rv,(isprint(rv) ? rv : '.'));
493 sprintf(rb,"PSW : 0x%02X | CY : %d | AC : %d | OV : %d | P : %d\n",
494 getValueStr(resp,"PSW="),
495 getValueStr(resp,"CY="),
496 getValueStr(resp,"AC="),
497 getValueStr(resp,"OV="),
498 getValueStr(resp,"P="));
505 /*-----------------------------------------------------------------*/
506 /* closeSimulator - close connection to simulator */
507 /*-----------------------------------------------------------------*/
508 void closeSimulator ()
512 kill (simPid,SIGKILL);