OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [tags/] [tn_m001/] [or1ksim/] [toplevel.c] - Blame information for rev 173

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 cvs
/* toplevel.c -- Top level simulator source file
2
   Copyright (C) 1999 Damjan Lampret, lampret@opencores.org
3
 
4
This file is part of OpenRISC 1000 Architectural Simulator.
5
 
6
This program is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2 of the License, or
9
(at your option) any later version.
10
 
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.
15
 
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, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
 
20
/* Simulator commands. Help and version output. SIGINT processing.
21
Stdout redirection is specific to linux (I need to fix this). */
22
 
23 16 jrydberg
#include "config.h"
24
 
25 2 cvs
#include <stdio.h>
26
#include <ctype.h>
27
#include <string.h>
28
#include <stdlib.h>
29 46 lampret
#include <unistd.h>
30 2 cvs
#include <signal.h>
31
#include <stdarg.h>
32 123 markom
/* Added by CZ 24/05/01 */
33
#include <sys/stat.h>
34
#include <sys/types.h>
35
#include <sys/socket.h>
36
#include <netinet/in.h>
37
#include <sys/select.h>
38
#include <sys/poll.h>
39
#include <fcntl.h>
40
#include <netdb.h>
41
#include <netinet/tcp.h>
42 127 chris
#include <inttypes.h>
43 2 cvs
 
44 16 jrydberg
#ifdef HAVE_LIBREADLINE
45 7 jrydberg
#include <readline/readline.h>
46
#include <readline/history.h>
47 16 jrydberg
#endif /* HAVE_LIBREADLINE */
48 7 jrydberg
 
49 2 cvs
#include "arch.h"
50
#include "parse.h"
51
#include "abstract.h"
52
#include "trace.h"
53
#include "execute.h"
54 69 lampret
#include "sim-config.h"
55 103 lampret
#include "spr_defs.h"
56 2 cvs
 
57 28 lampret
#include "coff.h"
58
 
59 123 markom
/* Added by CZ 24/05/01 */
60 127 chris
#include "gdb.h"
61 123 markom
#include <signal.h>
62
#include <errno.h>
63
typedef enum {
64
  false = 0,
65
  true = 1,
66
} Boolean;
67
unsigned int serverIP = 0;
68
unsigned int serverPort = 0;
69
unsigned int server_fd = 0;
70
unsigned int gdb_fd = 0;
71
void HandleServerSocket(Boolean);
72
void JTAGRequest(void);
73
void GDBRequest(void);
74 127 chris
void ProtocolClean(int,int32_t);
75
static int gdb_read(void*,int);
76
static int gdb_write(void*,int);
77
void BlockJTAG(void);
78
 
79 167 markom
#ifndef DEBUGMOD_OFF
80 123 markom
int GlobalMode = 0;   /* Start off in the orginal mode */
81 167 markom
#else  /* no DEBUGMOD_OFF */
82
#define GlobalMode 0
83
#endif /* no DEBUGMOD_OFF */
84 123 markom
 
85 2 cvs
/* CVS revision number. */
86 173 markom
const char rcsrev[] = "$Revision: 1.21 $";
87 2 cvs
 
88
/* Continuos run versus single step tracing switch. */
89
int cont_run;
90
 
91
/* History of execution */
92
int histexec[HISTEXEC_LEN];
93
 
94 7 jrydberg
char *sim_commands [] = {
95
  "q",
96
  "t",
97
  "help",
98 21 cmchen
  "de",
99 7 jrydberg
  "dm",
100
  "run",
101
  "pr",
102
  "pm",
103
  "pc",
104 18 lampret
  "reset",
105
  "break",
106 7 jrydberg
  "hist",
107
  "stats",
108
  "info",
109
  "r",
110 54 lampret
  "dv",
111 7 jrydberg
 
112
};
113
 
114 167 markom
inline void debug(const char *format, ...)
115 2 cvs
{
116 167 markom
#ifndef DEBUGMOD_OFF
117 7 jrydberg
  char *p;
118
  va_list ap;
119 2 cvs
 
120 69 lampret
  if (config.simdebug) {
121
    if ((p = malloc(1000)) == NULL)
122
      return;
123
    va_start(ap, format);
124
    (void) vsnprintf(p, 1000, format, ap);
125
    va_end(ap);
126
    printf("%s\n", p);
127
    fflush(stdout);
128
    free(p);
129
  } else {
130
#if DEBUG
131 7 jrydberg
  if ((p = malloc(1000)) == NULL)
132
    return;
133
  va_start(ap, format);
134
  (void) vsnprintf(p, 1000, format, ap);
135
  va_end(ap);
136
  printf("%s\n", p);
137
  fflush(stdout);
138
  free(p);
139 2 cvs
#endif
140 69 lampret
  }
141 167 markom
#endif /* no DEBUGMOD_OFF */
142 2 cvs
}
143
 
144 30 lampret
/* Strip whitespace from the start and end of STRING.  Return a pointer
145
   into STRING. */
146
#ifndef whitespace
147
#define whitespace(a)   ((a) == '\t' ? 1 : ((a) == ' '? 1 : 0))
148
#endif
149
 
150
char *
151
stripwhite (string)
152
     char *string;
153
{
154
  register char *s, *t;
155
 
156
  for (s = string; whitespace (*s); s++)
157
    ;
158
 
159
  if (*s == 0)
160
    return (s);
161
 
162
  t = s + strlen (s) - 1;
163
  while (t > s && whitespace (*t))
164
    t--;
165
  *++t = '\0';
166
 
167
  return s;
168
}
169
 
170 7 jrydberg
void
171
ctrl_c(signum)
172
     int signum;
173 2 cvs
{
174
        cont_run = 1;
175 123 markom
        config.iprompt = 1;
176 7 jrydberg
  signal(SIGINT, ctrl_c);
177 2 cvs
}
178
 
179 7 jrydberg
void
180
version()
181 2 cvs
{
182 7 jrydberg
        printf ("\n");
183 18 lampret
        printf ("OpenRISC 1000 (OR16+OR32) Architectural Simulator, %s\n", rcsrev);
184
        printf ("Copyright (C) 1999 Damjan Lampret, lampret@opencores.org\n");
185
        printf ("Copyright (C) 2000 Damjan Lampret, lampret@opencores.org\n");
186 21 cmchen
        printf ("                   Jimmy Chen-Min Chen, jimmy@ee.nctu.edu.tw\n");
187 18 lampret
        printf ("                   Johan Rydberg, johan.rydberg@insight.se\n");
188 7 jrydberg
        printf ("Visit http://www.opencores.org for more information about ");
189
        printf ("OpenRISC 1000 and\nother open source cores.\n\n");
190
        printf ("This software comes with ABSOLUTELY NO WARRANTY; for ");
191
        printf ("details see COPYING.\nThis is free software, and you ");
192
        printf ("are welcome to redistribute it under certain\nconditions; ");
193
        printf ("for details see COPYING.\n");
194 2 cvs
}
195
 
196 7 jrydberg
void
197
help()
198 2 cvs
{
199
        printf("q                        - quit simulator\n");
200
        printf("r                        - display all registers\n");
201
        printf("t                        - execute next instruction\n");
202
        printf("run <cycles> [<hush>]    - execute <cycles> instructions, no reg dump if hush\n");
203
        printf("pr <r> <value>           - patch register <r> with <value>\n");
204
        printf("dm <fromaddr> [<toaddr>] - display memory from <fromaddr> to <toaddr>\n");
205 21 cmchen
        printf("de                       - debug insn memory\n");
206 2 cvs
        printf("pm <addr> <value>        - patch memory location <addr> with <value>\n");
207
        printf("pc <value>               - patch PC register with <value>\n");
208 18 lampret
        printf("break <addr>             - toggle breakpoint at address <addr>\n");
209
        printf("reset                    - simulator reset\n");
210 2 cvs
        printf("hist                     - execution history\n");
211 6 lampret
        printf("stats <num|clear>        - execution statistics num or clear it.\n");
212
        printf("info                     - configuration info (caches etc.)\n");
213 54 lampret
        printf("dv <fromaddr> [<toaddr>] [<modname>] - dumps memory as verilog (use redirect)\n");
214 86 lampret
        printf("dh <fromaddr> [<toaddr>] - dumps memory as hex code (use redirect)\n");
215 2 cvs
        printf("<cmd> > <filename>       - redirect simulator stdout to <filename> (and not emulated printf)\n");
216 69 lampret
        printf("debug                    - toggles simulator debug mode\n");
217 2 cvs
        printf("help                     - available commands (this list)\n");
218
}
219
 
220 21 cmchen
void debugmem();
221
 
222 7 jrydberg
main(argc, argv)
223
     int argc;
224
     char *argv[];
225 2 cvs
{
226 7 jrydberg
        char *linestr;
227 167 markom
        char item1[500], b2[500], prev_str[500] = "";
228 2 cvs
        char *redirstr;
229
        int hush;
230 123 markom
        unsigned long endaddr = 0xFFFFFFFF;
231
        int first_prompt = 1;
232
 
233 103 lampret
        srand(getpid());
234
        init_defconfig();
235 123 markom
        if (parse_args(argc, argv)) {
236 103 lampret
                printf("Usage: %s [options] <filename>\n", argv[0]);
237
                printf("Options:\n");
238
                printf(" -v: version and copyright note\n");
239
                printf(" -i: enable interactive command prompt\n");
240
                printf(" -bpb: disable branch prediction buffer analysis\n");
241
                printf(" -btic: disable branch prediction target insn cache analysis\n");
242
                printf(" -hazards: disable dependency hazards analysis\n");
243
                printf(" -history: disable instruction stream history analysis\n");
244
                printf(" -superscalar: disable superscalar analysis\n");
245 123 markom
                printf(" -fast: disable BPB, BTIC, SLP, dependency hazards, history"
246
                       " analysis etc.\n");
247 173 markom
                printf(" -profile: generates profiling data.\n"); /* MM */
248 103 lampret
                printf(" -upr <n>: set UPR to n\n");
249
                printf(" -ver <n>: set VR[VER] to n\n");
250
                printf(" -rev <n>: set VR[REV] to n\n");
251 123 markom
                printf(" -loadmem[@<n>] <filename>: load memory with file, "
252
                       "optionally at address <n>\n"); /* (CZ) */
253
                printf(" -nosrv: do not launch JTAG proxy server\n"); /* (CZ) */
254
                printf(" -srv <n>: launch JTAG proxy server on port <n>\n"); /* (CZ) */
255
                printf(" -initmem <n | random>: initialize memory to value "
256
                       "<n> or random\n"); /* (CZ) */
257 103 lampret
                exit(-1);
258
        }
259 7 jrydberg
 
260 16 jrydberg
#ifdef HAVE_LIBREADLINE
261 7 jrydberg
  initialize_readline ();       /* Bind our completer. */
262 16 jrydberg
#endif  
263 123 markom
 
264
  if(!config.inhibit_server)
265
    {
266
      serverPort = config.server_port;
267
      if(server_fd = GetServerSocket("or1ksim","tcp",serverPort))
268
        printf("JTAG Proxy server started on port %d\n",serverPort);
269
    }
270 16 jrydberg
 
271 173 markom
  if(config.profile) {
272
    config.fprof = fopen("sim-profile","wt+");
273
    if(!config.fprof) {
274
      config.profile = 0;
275
      printf("Problems opening profile file. Profiling disabled. \n");
276
    } else
277
      fprintf(config.fprof, "+00000000 FFFFFFFF FFFFFFFF main\n");
278
  }
279
 
280 103 lampret
        print_config();
281 2 cvs
        signal(SIGINT, ctrl_c);
282
        initstats();
283 138 markom
        build_automata();
284 123 markom
 
285
        /* Modified by CZ on 24/05/01 ... if a filename is
286
           specified, behave as the simulator always has. This way,
287
           no existing test suites should be broken. If a filename
288
           is not specified, default to the new style behavior. Let
289
           the simulator start up and execute garbage, the same way
290
           a real CPU would. This should maximize the reality of
291
           the capabilities. In this mode, we will expect that
292
           someone will attach to us over the JTAG Proxy interface
293
           and begin debugging that way. */
294
 
295
        if(config.filename)
296
          {
297
            endaddr = loadcode(config.filename, MEMORY_START, 0);
298
            if (endaddr == -1) {
299
              printf("Problems loading boot code.\n");
300
              exit(1);
301
            }
302
          }
303
        else
304
          {
305
            if(config.random_mem)
306
              {
307
                int n = 0;
308
                int len = sizeof(mem);
309
                unsigned int* mptr = (unsigned int*)mem;
310
                unsigned int val = 0;
311
                int seed = time(NULL);
312
 
313
                srandom(seed);
314
                /* Print out the seed just in case we ever need to debug */
315
                printf("Seeding random generator with value %d\n",seed);
316
 
317
                for(n=0;n<len;n+=sizeof(unsigned int))
318
                  {
319
                    val = random();
320
                    if(random() > RAND_MAX/2)
321
                      val |= 0x80000000;
322
                    *mptr++ = val;
323
                  }
324
              }
325
            else if(config.pattern_mem)
326
              {
327
                int n = 0;
328
                int len = sizeof(mem);
329
                unsigned int* mptr = (unsigned int*)mem;
330
 
331
                for(n=0;n<len;n+=sizeof(unsigned int))
332
                  *mptr++ = config.pattern_mem;
333
              }
334
            else
335
              memset(mem,0,sizeof(mem));
336
 
337
            if(config.memory)
338
              {
339
                MemoryBlock* block = config.memory;
340
 
341
                while(block)
342
                  {
343
                    int fd = open(block->file,O_RDONLY);
344
                    int len,i;
345
                    struct stat buf;
346
                    char *mptr = (char*)mem;
347
                    char buffer[8192];
348
 
349
                    if(fd < 0)
350
                      {
351
                        perror(block->file);
352
                        exit(1);
353
                      }
354
                    if(fstat(fd,&buf) < 0)
355
                      {
356
                        char sTemp[256];
357
 
358
                        sprintf(sTemp,"stat(\"%s\")",block->file);
359
                        perror(sTemp);
360
                        exit(1);
361
                      }
362
                    if(!S_ISREG(buf.st_mode))
363
                      {
364
                        fprintf(stderr,"File \"%s\" is not a regular file.\n",
365
                                block->file);
366
                        exit(1);
367
                      }
368
 
369
                    len = buf.st_size;
370
                    mptr += block->address;
371
                    for(i=0;i<len;)
372
                      {
373
                        int n = read(fd,buffer,sizeof(buffer));
374
 
375
                        switch(n)
376
                          {
377
                          case -1:
378
                            if(errno == EINTR)
379
                              continue;
380
                            perror(block->file);
381
                            exit(1);
382
                          case 0:
383
                            fprintf(stderr,"File \"%s\": premature end of file.\n",
384
                                    block->file);
385
                            exit(1);
386
                          default:
387
                            memcpy(mptr,buffer,n);
388
                            i+= n;
389
                            break;
390
                          }
391
                      }
392
                    close(fd);
393
                  }
394
              }
395
          }
396 167 markom
#ifndef DEBUGMOD_OFF
397 123 markom
        GlobalMode = config.filename == NULL; /* Old mode = 0, New mode = 1 */
398 167 markom
#endif
399 123 markom
 
400 30 lampret
        uart_reset();
401 92 lampret
        tick_reset();
402 103 lampret
        pm_reset();
403
        pic_reset();
404 2 cvs
        reset();
405 123 markom
        if(!GlobalMode)  /* Only in old mode */
406 138 markom
          set_reg32(3, endaddr);
407 7 jrydberg
 
408 103 lampret
        while(1) {
409
                if (config.iprompt) {
410 123 markom
                  if(server_fd)
411
                    {
412
                      printf ("(sim) ");
413
                      fflush(stdout);
414
                      HandleServerSocket(true);  /* block & check_stdin = true */
415
                    }
416 16 jrydberg
#ifdef HAVE_LIBREADLINE
417 123 markom
                  /* Must disable readline in new mode. It isn't compatible
418
                     with non blocking environments */
419
                  if(!server_fd)
420
                    linestr = readline("(sim) ");
421
                  else
422
                    linestr = fgets(b2, sizeof b2, stdin);
423 16 jrydberg
#else
424 123 markom
                  if(!server_fd)
425
                    printf ("(sim) ");
426
                  linestr = fgets(b2, sizeof b2, stdin);
427 16 jrydberg
#endif
428 103 lampret
                } else
429 173 markom
                        strcpy(linestr = b2, "run -1 hush");
430 7 jrydberg
 
431 103 lampret
                if (!linestr)
432
                        break;
433
                linestr = stripwhite (linestr);
434 7 jrydberg
 
435 16 jrydberg
#ifdef HAVE_LIBREADLINE
436 123 markom
                /* Readline only works in the old mode */
437
                if(!server_fd)
438
                  {
439
                    if (strlen(linestr) == 0) {
440
                      char *l = repeat_last_command ();
441
 
442
                      if (l) {
443
                        free (linestr);
444
                        linestr = l;
445
                      }
446
                    }
447
 
448
                    if (*linestr) {
449
                      add_history (linestr);
450
                    }
451
                  }
452 16 jrydberg
#endif /* HAVE_LIBREADLINE */
453 2 cvs
 
454 103 lampret
                if (redirstr = strstr(linestr, ">")) {
455
                        *redirstr = '\0';
456
                        strtoken(&redirstr[1], item1, 1);
457
                        freopen(item1, "w+", stdout);
458
                }
459 2 cvs
 
460 167 markom
                if (linestr[0] == '\n')
461
                  strcpy (linestr, &prev_str[0]);
462
                else
463
                  strcpy (&prev_str[0], linestr);
464
 
465 2 cvs
                strtoken(linestr, item1, 1);
466 167 markom
                if (strcmp(item1, "q") == 0) {   /* quit */
467
                  printf ("\n");
468 173 markom
                  if (config.profile) {
469
                    extern int cycles;
470
                    fprintf(config.fprof,"-%08X FFFFFFFF\n", cycles);
471
                    fclose(config.fprof);
472
                  }
473 167 markom
                  exit(0);
474
                } else
475 2 cvs
                if (strcmp(item1, "help") == 0)  /* help */
476
                        help();
477
                else
478
                if (strcmp(item1, "t") == 0) {   /* trace */
479
                        cont_run = 1;
480
                } else
481
                if (strcmp(item1, "dm") == 0) {  /* dump memory */
482
                        char item2[20];
483
                        char item3[20];
484
                        static int from = 0, to = 0;
485
 
486
                        strtoken(linestr, item2, 2);
487
                        strtoken(linestr, item3, 3);
488
 
489
                        if (strlen(item2)) {
490
                                if (item2[0] == '_')
491
                                        from = eval_label(item2);
492
                                else
493
                                        from = strtoul(item2, NULL, 0);
494
                                to = from + 0x40;
495
                        }
496
                        if (strlen(item3))
497
                                to = strtoul(item3, NULL, 0);
498 167 markom
                        dumpmemory(from, to, 0);
499 54 lampret
                        printf("\n");
500 2 cvs
                } else
501 54 lampret
                if (strcmp(item1, "dv") == 0) {/* dump memory as verilog*/
502
                        char item2[20];
503
                        char item3[20];
504
                        char item4[20];
505
                        static int from = 0, to = 0;
506
 
507
                        strtoken(linestr, item2, 2);
508
                        strtoken(linestr, item3, 3);
509
                        strtoken(linestr, item4, 4);
510
 
511
                        if (strlen(item2)) {
512
                                if (item2[0] == '_')
513
                                        from = eval_label(item2);
514
                                else
515
                                        from = strtoul(item2, NULL, 0);
516
                                to = from + 0x40;
517
                        }
518
                        if (strlen(item3))
519
                                to = strtoul(item3, NULL, 0);
520
                        if (!strlen(item4))
521
                                strcpy(item4, "or1k_mem");
522
                        dumpverilog(item4, from, to);
523
                        printf("\n");
524
                } else
525 86 lampret
                if (strcmp(item1, "dh") == 0) {/* dump memory as hex*/
526
                        char item2[20];
527
                        char item3[20];
528
                        static int from = 0, to = 0;
529
 
530
                        strtoken(linestr, item2, 2);
531
                        strtoken(linestr, item3, 3);
532
 
533
                        if (strlen(item2)) {
534
                                if (item2[0] == '_')
535
                                        from = eval_label(item2);
536
                                else
537
                                        from = strtoul(item2, NULL, 0);
538
                                to = from + 0x40;
539
                        }
540
                        if (strlen(item3))
541
                                to = strtoul(item3, NULL, 0);
542
                        dumphex(from, to);
543
                        printf("\n");
544
                } else
545 2 cvs
                if (strcmp(item1, "pm") == 0) {  /* patch memory */
546
                        char item2[20];
547
                        char item3[20];
548
                        static int addr = 0;
549 123 markom
                        int breakpoint = 0;
550
 
551 2 cvs
                        strtoken(linestr, item2, 2);
552
                        strtoken(linestr, item3, 3);
553
                        if (strlen(item2))
554
                                if (item2[0] == '_')
555
                                        addr = eval_label(item2);
556
                                else
557
                                        addr = strtoul(item2, NULL, 0);
558 123 markom
                        set_mem32(addr, strtoul(item3, NULL, 0), &breakpoint);
559 2 cvs
                } else
560
                if (strcmp(item1, "pr") == 0) {  /* patch regs */
561
                        char item2[20];
562
                        char item3[20];
563
 
564
                        strtoken(linestr, item2, 2);
565
                        strtoken(linestr, item3, 3);
566 138 markom
                        set_reg32(strtoul(item2, NULL,0), strtoul(item3, NULL, 0));
567 2 cvs
                } else
568
                if (strcmp(item1, "pc") == 0) {  /* patch PC */
569
                        char item2[20];
570
 
571
                        strtoken(linestr, item2, 2);
572 86 lampret
                        pcnext = strtoul(item2, NULL, 0);
573 2 cvs
                } else
574 7 jrydberg
                if (strcmp(item1, "break") == 0) {       /* set/clear breakpoint */
575 2 cvs
                        char item2[20];
576
 
577
                        strtoken(linestr, item2, 2);
578
                        set_insnbrkpoint(strtoul(item2, NULL, 0));
579
                } else
580
                if (strcmp(item1, "r") == 0) {   /* dump regs */
581
                        dumpreg();
582
                } else
583 21 cmchen
                if (strcmp(item1, "de") == 0) {  /* reset simulator */
584
                        debugmem();
585
                } else
586 18 lampret
                if (strcmp(item1, "reset") == 0) {       /* reset simulator */
587 30 lampret
                        uart_reset();
588 92 lampret
                        tick_reset();
589 103 lampret
                        pm_reset();
590
                        pic_reset();
591 123 markom
                        reset(); /* Old or new mode */
592 18 lampret
                } else
593 69 lampret
                if (strcmp(item1, "debug") == 0) {       /* debug mode */
594
                        config.simdebug ^= 1;
595
                } else
596 2 cvs
                if (strcmp(item1, "hist") == 0) {        /* dump history */
597
                        int i;
598
                        for(i = HISTEXEC_LEN; i; i--)
599 167 markom
                                dumpmemory(histexec[i - 1], histexec[i - 1] + 4, 1);
600 46 lampret
                        printf("\n");
601 2 cvs
                } else
602
                if (strcmp(item1, "run") == 0) { /* run */
603
                        char item2[20];
604
                        char item3[20];
605
 
606
                        strtoken(linestr, item2, 2);
607
                        strtoken(linestr, item3, 3);
608
                        if (strcmp(item3, "hush") == 0)
609
                                hush = 1;
610
                        else
611
                                hush = 0;
612 173 markom
                        cont_run = strtol(item2, NULL, 0);
613 2 cvs
                } else
614
                if (strcmp(item1, "stats") == 0) { /* stats */
615 6 lampret
                        char item2[20];
616
                        int i = 0;
617
 
618
                        strtoken(linestr, item2, 2);
619
                        if (strcmp(item2, "clear") == 0) {
620
                                initstats();
621
                                printf("Cleared.\n");
622
                        } else {
623
                                i = strtoul(item2, NULL, 0);
624
                                printstats(i);
625
                        }
626
                } else
627
                if (strcmp(item1, "info") == 0) { /* configuration info */
628 78 lampret
                        itlb_status(-1);
629
                        dtlb_status(-1);
630 6 lampret
                        bpb_info();
631
                        btic_info();
632
                        ic_info();
633
                        dc_info();
634 30 lampret
                        uart_status();
635
                        sprs_status();
636 7 jrydberg
                } else {
637 103 lampret
                        printf("%s: Unknown command.\n", linestr);
638
                }
639
 
640 138 markom
                /* MM: 'run -1' means endless execution.  */
641
                while(cont_run != 0) {
642 123 markom
                  extern int cycle_delay;  /* Added by CZ 27/05/01. Set during exception. */
643 127 chris
                  extern int cpu_stalled;  /* CZ from debug_interface */
644 123 markom
 
645 127 chris
                  if(cpu_stalled)
646
                    {
647
                      BlockJTAG();
648
                      HandleServerSocket(false);
649
                      continue;
650
                    }
651
 
652 167 markom
                        if (!testsprbits(SPR_PMR, SPR_PMR_DME | SPR_PMR_SME)) {
653 123 markom
                          if(cycle_delay <= 0)
654
                            {
655 173 markom
                              if (cont_run > 0) cont_run--;
656 123 markom
                              if(fetch()) {
657
                                cont_run = 0; /* memory breakpoint encountered */
658
                                break;
659
                              }
660
                              decode_execute(&iqueue[0]);
661
                              update_pc();
662
                              analysis();
663
                              if (!hush)
664
                                dumpreg();
665
                            }
666
                          else
667
                            cycle_delay--;
668
 
669
                          pic_clock();
670
                          dc_clock();
671
                          ic_clock();
672 103 lampret
                        }
673 167 markom
                        if (!testsprbits(SPR_PMR, SPR_PMR_SME))
674 123 markom
                          tick_clock();
675 103 lampret
                        pm_clock();
676 30 lampret
                        uart_clock();
677 167 markom
#ifndef DEBUGMOD_OFF
678 123 markom
                        HandleServerSocket(false); /* block & check_stdin = false */
679 167 markom
#endif
680 2 cvs
                }
681
                hush = 0;
682
                fflush(stdout);
683
                freopen("/dev/fd/0", "w+", stdout);
684
 
685 173 markom
                if (!config.iprompt && !GlobalMode) {   /* non-interactive quit in old mode */
686
                  if (config.profile) {
687
                    extern int cycles;
688
                    fprintf(config.fprof,"-%08X FFFFFFFF\n", cycles);
689
                    fclose(config.fprof);
690
                  }
691
                  exit(0);
692
                }
693 16 jrydberg
#ifdef HAVE_LIBREADLINE
694 103 lampret
                if (linestr)
695
                        free (linestr);
696 16 jrydberg
#endif
697 7 jrydberg
 
698 2 cvs
        }
699
        exit(0);
700
}
701 7 jrydberg
 
702 16 jrydberg
#ifdef HAVE_LIBREADLINE
703 7 jrydberg
char *command_generator ();
704
char **sim_completion ();
705
 
706
/* Tell the GNU readline library how to complete.  We want to try to complete
707
   on command names if this is the first word in the line, or on filenames
708
   if not. */
709
void initialize_readline ()
710
{
711
  /* Allow conditional parsing of the ~/.inputrc file. */
712
  rl_readline_name = "or1ksim";
713
 
714
  /* Tell the completer that we want a crack first. */
715
  rl_attempted_completion_function = (CPPFunction *)sim_completion;
716
}
717
 
718
/* Attempt to complete on the contents of TEXT.  START and END bound the
719
   region of rl_line_buffer that contains the word to complete.  TEXT is
720
   the word to complete.  We can use the entire contents of rl_line_buffer
721
   in case we want to do some simple parsing.  Return the array of matches,
722
   or NULL if there aren't any. */
723
char **
724
sim_completion (text, start, end)
725
     char *text;
726
     int start, end;
727
{
728
  char **matches;
729
 
730
  matches = (char **)NULL;
731
 
732
  /* If this word is at the start of the line, then it is a command
733
     to complete.  Otherwise it is the name of a file in the current
734
     directory. */
735
  if (start == 0)
736
    matches = completion_matches (text, command_generator);
737
 
738
  return (matches);
739
}
740
 
741
/* Generator function for command completion.  STATE lets us know whether
742
   to start from scratch; without any state (i.e. STATE == 0), then we
743
   start at the top of the list. */
744
char *
745
command_generator (text, state)
746
     char *text;
747
     int state;
748
{
749
  static int list_index, len;
750
  char *name;
751
 
752
  /* If this is a new word to complete, initialize now.  This includes
753
     saving the length of TEXT for efficiency, and initializing the index
754
     variable to 0. */
755
  if (!state)
756
    {
757
      list_index = 0;
758
      len = strlen (text);
759
    }
760
 
761
  /* Return the next name which partially matches from the command list. */
762
  while (name = sim_commands[list_index])
763
    {
764
      list_index++;
765
 
766
      if (strncmp (name, text, len) == 0)
767
        return (dupstr(name));
768
    }
769
 
770
  /* If no names matched, then return NULL. */
771
  return ((char *)NULL);
772
}
773
 
774
char *
775
dupstr (s)
776
     char *s;
777
{
778
  char *r;
779
 
780
  r = xmalloc (strlen (s) + 1);
781
  strcpy (r, s);
782
  return (r);
783
}
784
 
785
/* Repeats the last command.  */
786
char *
787
repeat_last_command ()
788
{
789
  int offset = where_history ();
790
  HIST_ENTRY *hist;
791
 
792
  if (hist = history_get (offset))
793
    {
794
      return dupstr (hist->line);
795
    }
796
  return 0;
797
}
798 16 jrydberg
 
799
#endif
800
 
801 138 markom
extern char *disassembled;
802 21 cmchen
void debugmem() {
803 138 markom
  int i;
804
  printf("starting to dump mem...\n");
805
  for(i=0; i<500; i++) {
806
    printf("i=%x :: ", i);
807
    if (mem[i].label)
808
      printf("label: %s |", mem[i].label->name);
809
    {
810
      unsigned int _insn;
811
      _insn = ((unsigned long)mem[i].data << 24);
812
      _insn |= ((unsigned long)mem[i + 1].data << 16);
813
      _insn |= ((unsigned long)mem[i + 2].data << 8);
814
      _insn |= ((unsigned long)mem[i + 3].data);
815
      iqueue[0].insn_index = insn_decode(_insn);
816
      iqueue[0].insn = _insn;
817
      disassemble_insn (_insn);
818
      printf("%s\n", disassembled);
819
    }
820
  }
821 21 cmchen
}
822 123 markom
 
823 127 chris
static int tcp_level = 0;
824
 
825 123 markom
/* Added by CZ 24/05/01 */
826
int GetServerSocket(const char* name,const char* proto,int port)
827
{
828
  struct servent *service;
829
  struct protoent *protocol;
830
  struct sockaddr_in sa;
831
  struct hostent *hp;
832
  int sockfd;
833
  char myname[256];
834
  int flags;
835
  char sTemp[256];
836
 
837
  /* First, get the protocol number of TCP */
838
  if(!(protocol = getprotobyname(proto)))
839
    {
840
      sprintf(sTemp,"Unable to load protocol \"%s\"",proto);
841
      perror(sTemp);
842
      return 0;
843
    }
844 127 chris
  tcp_level = protocol->p_proto; /* Save for later */
845
 
846 123 markom
  /* If we weren't passed a non standard port, get the port
847
     from the services directory. */
848
  if(!port)
849
    {
850
      if(service = getservbyname(name,protocol->p_name))
851
        port = ntohs(service->s_port);
852
    }
853
 
854
  /* Create the socket using the TCP protocol */
855
  if((sockfd = socket(PF_INET,SOCK_STREAM,protocol->p_proto)) < 0)
856
    {
857
      perror("Unable to create socket");
858
      return 0;
859
    }
860
 
861
  flags = 1;
862
  if(setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,(const char*)&flags,sizeof(int))
863
 < 0)
864
    {
865
      sprintf(sTemp,"Can not set SO_REUSEADDR option on socket %d",sockfd);
866
      perror(sTemp);
867
      close(sockfd);
868
      return 0;
869
    }
870
 
871
  /* The server should also be non blocking. Get the current flags. */
872
  if(fcntl(sockfd,F_GETFL,&flags) < 0)
873
    {
874
      sprintf(sTemp,"Unable to get flags for socket %d",sockfd);
875
      perror(sTemp);
876
      close(sockfd);
877
      return 0;
878
    }
879
 
880
  /* Set the nonblocking flag */
881
  if(fcntl(sockfd,F_SETFL, flags | O_NONBLOCK) < 0)
882
    {
883
      sprintf(sTemp,"Unable to set flags for socket %d to value 0x%08x",
884
              sockfd,flags | O_NONBLOCK);
885
      perror(sTemp);
886
      close(sockfd);
887
      return 0;
888
    }
889
 
890
  /* Find out what our address is */
891
  memset(&sa,0,sizeof(struct sockaddr_in));
892
  gethostname(myname,sizeof(myname));
893
  if(!(hp = gethostbyname(myname)))
894
    {
895
      perror("Unable to read hostname");
896
      close(sockfd);
897
      return 0;
898
    }
899
 
900
  /* Bind our socket to the appropriate address */
901
  sa.sin_family = hp->h_addrtype;
902
  sa.sin_port = htons(port);
903
  if(bind(sockfd,(struct sockaddr*)&sa,sizeof(struct sockaddr_in)) < 0)
904
    {
905
      sprintf(sTemp,"Unable to bind socket %d to port %d",sockfd,port);
906
      perror(sTemp);
907
      close(sockfd);
908
      return 0;
909
    }
910
  serverIP = sa.sin_addr.s_addr;
911
  flags = sizeof(struct sockaddr_in);
912
  if(getsockname(sockfd,(struct sockaddr*)&sa,&flags) < 0)
913
    {
914
      sprintf(sTemp,"Unable to get socket information for socket %d",sockfd);
915
      perror(sTemp);
916
      close(sockfd);
917
      return 0;
918
    }
919
  serverPort = ntohs(sa.sin_port);
920
 
921
  /* Set the backlog to 1 connections */
922
  if(listen(sockfd,1) < 0)
923
    {
924
      sprintf(sTemp,"Unable to set backlog on socket %d to %d",sockfd,1);
925
      perror(sTemp);
926
      close(sockfd);
927
      return 0;
928
    }
929
 
930
  return sockfd;
931
}
932
 
933 127 chris
void BlockJTAG()
934 123 markom
{
935
  struct pollfd fds[2];
936
  int n = 0;
937 127 chris
 
938
  fds[n].fd = server_fd;
939
  fds[n].events = POLLIN;
940
  fds[n++].revents = 0;
941
  if(gdb_fd)
942
    {
943
      fds[n].fd = gdb_fd;
944
      fds[n].events = POLLIN;
945
      fds[n++].revents = 0;
946
    }
947
  poll(fds,n,-1);
948
}
949
 
950
void HandleServerSocket(Boolean block)
951
{
952
  struct pollfd fds[3];
953
  int n = 0;
954 123 markom
  int timeout = block ? -1 : 0;
955
  int server_index = -1;
956
  int gdb_index = -1;
957
  Boolean data_on_stdin = false;
958
  int o_serv_fd = server_fd;
959
 
960
  if(!o_serv_fd && !gdb_fd)
961
    return;
962
 
963
  if(o_serv_fd)
964
    {
965
      fds[n].fd = o_serv_fd;
966
      fds[n].events = POLLIN;
967
      fds[n++].revents = 0;
968
    }
969
  if(gdb_fd)
970
    {
971
      fds[n].fd = gdb_fd;
972
      fds[n].events = POLLIN;
973
      fds[n++].revents = 0;
974
    }
975
  if(block)
976
    {
977
      fds[n].fd = 0;
978
      fds[n].events = POLLIN;
979
      fds[n++].revents = 0;
980
    }
981
 
982
  while(!data_on_stdin)
983
    {
984
      switch(poll(fds,n,timeout))
985
        {
986
        case -1:
987
          if(errno == EINTR)
988
            continue;
989
          perror("poll");
990
          server_fd = 0;
991
          break;
992
        case 0: /* Nothing interesting going on */
993
          data_on_stdin = true; /* Can only get here if nonblocking */
994
          break;
995
        default:
996 127 chris
          /* Make sure to handle the gdb port first! */
997 123 markom
          if((fds[0].revents && (gdb_fd && !o_serv_fd) ||
998
              fds[1].revents && (server_fd && gdb_fd)))
999
            {
1000
              int revents = o_serv_fd ? fds[1].revents : fds[0].revents;
1001
 
1002
              if(revents & POLLIN)
1003
                GDBRequest();
1004
              else /* Error Occurred */
1005
                {
1006
                  fprintf(stderr,"Received flags 0x%08x on gdb socket. Shutting down.\n",revents);
1007
                  close(gdb_fd);
1008
                  gdb_fd = 0;
1009
                }
1010
            }
1011 127 chris
          if(fds[0].revents && o_serv_fd)
1012
            {
1013
              if(fds[0].revents & POLLIN)
1014
                JTAGRequest();
1015
              else /* Error Occurred */
1016
                {
1017
                  fprintf(stderr,"Received flags 0x%08x on server. Shutting down.\n",fds[0].revents);
1018
                  close(o_serv_fd);
1019
                  server_fd = 0;
1020
                  serverPort = 0;
1021
                  serverIP = 0;
1022
                }
1023
            }
1024 123 markom
          if(fds[2].revents || (fds[1].revents && !gdb_fd))
1025
            data_on_stdin = true;
1026
          break;
1027
        } /* End of switch statement */
1028
    } /* End of while statement */
1029
}
1030
 
1031
void JTAGRequest()
1032
{
1033
  struct sockaddr_in sa;
1034
  struct sockaddr* addr = (struct sockaddr*)&sa;
1035
  int n = sizeof(struct sockaddr_in);
1036
  int fd = accept(server_fd,addr,&n);
1037
  int on_off = 0; /* Turn off Nagel's algorithm on the socket */
1038
  int flags;
1039
  char sTemp[256];
1040
 
1041
  if(fd < 0)
1042
    {
1043
      /* This is valid, because a connection could have started,
1044
         and then terminated due to a protocol error or user
1045
         initiation before the accept could take place. */
1046
      if(errno != EWOULDBLOCK && errno != EAGAIN)
1047
        {
1048
          perror("accept");
1049
          close(server_fd);
1050
          server_fd = 0;
1051
          serverPort = 0;
1052
          serverIP = 0;
1053
        }
1054
      return;
1055
    }
1056
 
1057
  if(gdb_fd)
1058
    {
1059
      close(fd);
1060
      return;
1061
    }
1062
 
1063
  if(fcntl(fd,F_GETFL,&flags) < 0)
1064
    {
1065
      sprintf(sTemp,"Unable to get flags for gdb socket %d",fd);
1066
      perror(sTemp);
1067
      close(fd);
1068
      return;
1069
    }
1070
 
1071
  if(fcntl(fd,F_SETFL, flags | O_NONBLOCK) < 0)
1072
    {
1073
      sprintf(sTemp,"Unable to set flags for gdb socket %d to value 0x%08x",
1074
              fd,flags | O_NONBLOCK);
1075
      perror(sTemp);
1076
      close(fd);
1077
      return;
1078
    }
1079
 
1080 127 chris
  if(setsockopt(fd,tcp_level,TCP_NODELAY,&on_off,sizeof(int)) < 0)
1081 123 markom
    {
1082
      sprintf(sTemp,"Unable to disable Nagel's algorithm for socket %d.\nsetsockopt",fd);
1083
      perror(sTemp);
1084
      close(fd);
1085
      return;
1086
    }
1087
 
1088
  gdb_fd = fd;
1089
}
1090
 
1091
void GDBRequest()
1092
{
1093 127 chris
  JTAGProxyWriteMessage msg_write;
1094
  JTAGProxyReadMessage msg_read;
1095
  JTAGProxyChainMessage msg_chain;
1096
  JTAGProxyWriteResponse resp_write;
1097
  JTAGProxyReadResponse resp_read;
1098
  JTAGProxyChainResponse resp_chain;
1099 139 chris
  JTAGProxyBlockWriteMessage *msg_bwrite;
1100
  JTAGProxyBlockReadMessage msg_bread;
1101
  JTAGProxyBlockWriteResponse resp_bwrite;
1102
  JTAGProxyBlockReadResponse *resp_bread;
1103 127 chris
  char *buf;
1104
  unsigned long long data;
1105 139 chris
  int err = 0;
1106 127 chris
  uint32_t command,length;
1107 139 chris
  int len,i;
1108 127 chris
 
1109
  /* First, we must read the incomming command */
1110
  if(gdb_read(&command,sizeof(uint32_t)) < 0)
1111
    {
1112
      if(gdb_fd)
1113
        {
1114
          perror("gdb socket - 1");
1115
          close(gdb_fd);
1116
          gdb_fd = 0;
1117
        }
1118
      return;
1119
    }
1120
  if(gdb_read(&length,sizeof(uint32_t)) < 0)
1121
    {
1122
      if(gdb_fd)
1123
        {
1124
          perror("gdb socket - 2");
1125
          close(gdb_fd);
1126
          gdb_fd = 0;
1127
        }
1128
      return;
1129
    }
1130
  length = ntohl(length);
1131
 
1132
  /* Now, verify the protocol and implement the command */
1133
  switch(ntohl(command))
1134
    {
1135
    case JTAG_COMMAND_WRITE:
1136
      if(length != sizeof(msg_write) - 8)
1137
        {
1138
          ProtocolClean(length,JTAG_PROXY_PROTOCOL_ERROR);
1139
          return;
1140
        }
1141
      buf = (char*)&msg_write;
1142
      if(gdb_read(&buf[8],length) < 0)
1143
        {
1144
          if(gdb_fd)
1145
            {
1146
              perror("gdb socket - 3");
1147
              close(gdb_fd);
1148
              gdb_fd = 0;
1149
            }
1150
          return;
1151
        }
1152
      msg_write.address = ntohl(msg_write.address);
1153
      msg_write.data_H = ntohl(msg_write.data_H);
1154
      msg_write.data_L = ntohl(msg_write.data_L);
1155
      err = DebugSetRegister(msg_write.address,msg_write.data_L);
1156
      resp_write.status = htonl(err);
1157
      if(gdb_write(&resp_write,sizeof(resp_write)) < 0)
1158
        {
1159
          if(gdb_fd)
1160
            {
1161
              perror("gdb socket - 4");
1162
              close(gdb_fd);
1163
              gdb_fd = 0;
1164
            }
1165
          return;
1166
        }
1167
      break;
1168
    case JTAG_COMMAND_READ:
1169
      if(length != sizeof(msg_read) - 8)
1170
        {
1171
          ProtocolClean(length,JTAG_PROXY_PROTOCOL_ERROR);
1172
          return;
1173
        }
1174
      buf = (char*)&msg_read;
1175
      if(gdb_read(&buf[8],length) < 0)
1176
        {
1177
          if(gdb_fd)
1178
            {
1179
              perror("gdb socket - 5");
1180
              close(gdb_fd);
1181
              gdb_fd = 0;
1182
            }
1183
          return;
1184
        }
1185
      msg_read.address = ntohl(msg_read.address);
1186
      err = DebugGetRegister(msg_read.address,&resp_read.data_L);
1187
      resp_read.status = htonl(err);
1188
      resp_read.data_H = 0;
1189
      resp_read.data_L = htonl(resp_read.data_L);
1190
      if(gdb_write(&resp_read,sizeof(resp_read)) < 0)
1191
        {
1192
          if(gdb_fd)
1193
            {
1194
              perror("gdb socket - 6");
1195
              close(gdb_fd);
1196
              gdb_fd = 0;
1197
            }
1198
          return;
1199
        }
1200
      break;
1201 139 chris
    case JTAG_COMMAND_BLOCK_WRITE:
1202
      if(length < sizeof(JTAGProxyBlockWriteMessage)-8)
1203
        {
1204
          ProtocolClean(length,JTAG_PROXY_PROTOCOL_ERROR);
1205
          return;
1206
        }
1207
      if(!(buf = (char*)malloc(8+length)))
1208
        {
1209
          ProtocolClean(length,JTAG_PROXY_OUT_OF_MEMORY);
1210
          return;
1211
        }
1212
      msg_bwrite = (JTAGProxyBlockWriteMessage*)buf;
1213
      if(gdb_read(&buf[8],length) < 0)
1214
        {
1215
          if(gdb_fd)
1216
            {
1217
              perror("gdb socket - 5");
1218
              close(gdb_fd);
1219
              gdb_fd = 0;
1220
            }
1221
          free(buf);
1222
          return;
1223
        }
1224
      msg_bwrite->address = ntohl(msg_bwrite->address);
1225
      msg_bwrite->nRegisters = ntohl(msg_bwrite->nRegisters);
1226
      for(i=0;i<msg_bwrite->nRegisters;i++)
1227
        {
1228
          int t_err;
1229
 
1230
          msg_bwrite->data[i] = ntohl(msg_bwrite->data[i]);
1231
          t_err = DebugSetRegister(msg_bwrite->address+i,msg_bwrite->data[i]);
1232
          err = err ? err : t_err;
1233
        }
1234
      resp_bwrite.status = htonl(err);
1235
      free(buf);
1236
      buf = msg_bwrite = NULL;
1237
      if(gdb_write(&resp_bwrite,sizeof(resp_bwrite)) < 0)
1238
        {
1239
          if(gdb_fd)
1240
            {
1241
              perror("gdb socket - 4");
1242
              close(gdb_fd);
1243
              gdb_fd = 0;
1244
            }
1245
          return;
1246
        }
1247
      break;
1248
    case JTAG_COMMAND_BLOCK_READ:
1249
      if(length != sizeof(msg_bread) - 8)
1250
        {
1251
          ProtocolClean(length,JTAG_PROXY_PROTOCOL_ERROR);
1252
          return;
1253
        }
1254
      buf = (char*)&msg_bread;
1255
      if(gdb_read(&buf[8],length) < 0)
1256
        {
1257
          if(gdb_fd)
1258
            {
1259
              perror("gdb socket - 5");
1260
              close(gdb_fd);
1261
              gdb_fd = 0;
1262
            }
1263
          return;
1264
        }
1265
      msg_bread.address = ntohl(msg_bread.address);
1266
      msg_bread.nRegisters = ntohl(msg_bread.nRegisters);
1267
      len = sizeof(JTAGProxyBlockReadResponse) + 4*(msg_bread.nRegisters-1);
1268
      if(!(buf = (char*)malloc(len)))
1269
        {
1270
          ProtocolClean(0,JTAG_PROXY_OUT_OF_MEMORY);
1271
          return;
1272
        }
1273
      resp_bread = (JTAGProxyBlockReadResponse*)buf;
1274
      for(i=0;i<msg_bread.nRegisters;i++)
1275
        {
1276
          int t_err;
1277
 
1278
          t_err = DebugGetRegister(msg_bread.address+i,&resp_bread->data[i]);
1279
          resp_bread->data[i] = htonl(resp_bread->data[i]);
1280
          err = err ? err : t_err;
1281
        }
1282
      resp_bread->status = htonl(err);
1283
      resp_bread->nRegisters = htonl(msg_bread.nRegisters);
1284
      if(gdb_write(resp_bread,len) < 0)
1285
        {
1286
          if(gdb_fd)
1287
            {
1288
              perror("gdb socket - 6");
1289
              close(gdb_fd);
1290
              gdb_fd = 0;
1291
            }
1292
          free(buf);
1293
          return;
1294
        }
1295
      free(buf);
1296
      buf = resp_bread = NULL;
1297
      break;
1298 127 chris
    case JTAG_COMMAND_CHAIN:
1299
      if(length != sizeof(msg_chain) - 8)
1300
        {
1301
          ProtocolClean(length,JTAG_PROXY_PROTOCOL_ERROR);
1302
          return;
1303
        }
1304
      buf = (char*)&msg_chain;
1305
      if(gdb_read(&buf[8],sizeof(msg_chain)-8) < 0)
1306
        {
1307
          if(gdb_fd)
1308
            {
1309
              perror("gdb socket - 7");
1310
              close(gdb_fd);
1311
              gdb_fd = 0;
1312
            }
1313
          return;
1314
        }
1315
      msg_chain.chain = htonl(msg_chain.chain);
1316
      err = DebugSetChain(msg_chain.chain);
1317
      resp_chain.status = htonl(err);
1318
      if(gdb_write(&resp_chain,sizeof(resp_chain)) < 0)
1319
        {
1320
          if(gdb_fd)
1321
            {
1322
              perror("gdb socket - 8");
1323
              close(gdb_fd);
1324
              gdb_fd = 0;
1325
            }
1326
          return;
1327
        }
1328
      break;
1329
    default:
1330
      ProtocolClean(length,JTAG_PROXY_COMMAND_NOT_IMPLEMENTED);
1331
      break;
1332
    }
1333 123 markom
}
1334 127 chris
 
1335
void ProtocolClean(int length,int32_t err)
1336
{
1337
  char buf[4096];
1338
 
1339
  err = htonl(err);
1340
  if((gdb_read(buf,length) < 0) ||
1341
      (gdb_write(&err,sizeof(err)) < 0) && gdb_fd)
1342
    {
1343
      perror("gdb socket - 9");
1344
      close(gdb_fd);
1345
      gdb_fd = 0;
1346
    }
1347
}
1348
 
1349
static int gdb_write(void* buf,int len)
1350
{
1351
  int n;
1352
  char* w_buf = (char*)buf;
1353
  struct pollfd block;
1354
 
1355
  while(len)
1356
    {
1357
      if((n = write(gdb_fd,w_buf,len)) < 0)
1358
        {
1359
          switch(errno)
1360
            {
1361
            case EWOULDBLOCK: /* or EAGAIN */
1362
              /* We've been called on a descriptor marked
1363
                 for nonblocking I/O. We better simulate
1364
                 blocking behavior. */
1365
              block.fd = gdb_fd;
1366
              block.events = POLLOUT;
1367
              block.revents = 0;
1368
              poll(&block,1,-1);
1369
              continue;
1370
            case EINTR:
1371
              continue;
1372
            case EPIPE:
1373
              close(gdb_fd);
1374
              gdb_fd = 0;
1375
              return -1;
1376
            default:
1377
              return -1;
1378
            }
1379
        }
1380
      else
1381
        {
1382
          len -= n;
1383
          w_buf += n;
1384
        }
1385
    }
1386
  return 0;
1387
}
1388
 
1389
static int gdb_read(void* buf,int len)
1390
{
1391
  int n;
1392
  char* r_buf = (char*)buf;
1393
  struct pollfd block;
1394
 
1395
  while(len)
1396
    {
1397
      if((n = read(gdb_fd,r_buf,len)) < 0)
1398
        {
1399
          switch(errno)
1400
            {
1401
            case EWOULDBLOCK: /* or EAGAIN */
1402
              /* We've been called on a descriptor marked
1403
                 for nonblocking I/O. We better simulate
1404
                 blocking behavior. */
1405
              block.fd = gdb_fd;
1406
              block.events = POLLIN;
1407
              block.revents = 0;
1408
              poll(&block,1,-1);
1409
              continue;
1410
            case EINTR:
1411
              continue;
1412
            default:
1413
              return -1;
1414
            }
1415
        }
1416
      else if(n == 0)
1417
        {
1418
          close(gdb_fd);
1419
          gdb_fd = 0;
1420
          return -1;
1421
        }
1422
      else
1423
        {
1424
          len -= n;
1425
          r_buf += n;
1426
        }
1427
    }
1428
  return 0;
1429
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.