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

Subversion Repositories or1k

[/] [or1k/] [tags/] [stable_0_2_0_rc2/] [or1ksim/] [toplevel.c] - Blame information for rev 262

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

powered by: WebSVN 2.1.0

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