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 293

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

powered by: WebSVN 2.1.0

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