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 263

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

powered by: WebSVN 2.1.0

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