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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_61/] [or1ksim/] [toplevel.c] - Blame information for rev 242

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

powered by: WebSVN 2.1.0

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