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

Subversion Repositories or1k

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

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 269 markom
const char rcsrev[] = "$Revision: 1.35 $";
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 269 markom
                  if(GDB_ENABLED && cpu_stalled)
617 127 chris
                    {
618
                      BlockJTAG();
619
                      HandleServerSocket(false);
620
                      continue;
621
                    }
622
 
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 269 markom
      if (GDB_ENABLED && debug_slowdown-- == 0) {
686 261 markom
        debug_slowdown = DEBUG_SLOWDOWN;
687
                          HandleServerSocket(false); /* block & check_stdin = false */
688 103 lampret
                        }
689 2 cvs
                }
690
                hush = 0;
691
                fflush(stdout);
692
                freopen("/dev/fd/0", "w+", stdout);
693
 
694 264 markom
                if (!config.sim.iprompt) {      /* non-interactive quit */
695
                  if (config.sim.profile) {
696 173 markom
                    extern int cycles;
697 264 markom
                    fprintf(config.sim.fprof,"-%08X FFFFFFFF\n", cycles);
698
                    fclose(config.sim.fprof);
699 173 markom
                  }
700
                  exit(0);
701
                }
702 16 jrydberg
#ifdef HAVE_LIBREADLINE
703 103 lampret
                if (linestr)
704
                        free (linestr);
705 16 jrydberg
#endif
706 7 jrydberg
 
707 2 cvs
        }
708
        exit(0);
709
}
710 7 jrydberg
 
711 16 jrydberg
#ifdef HAVE_LIBREADLINE
712 7 jrydberg
char *command_generator ();
713
char **sim_completion ();
714
 
715
/* Tell the GNU readline library how to complete.  We want to try to complete
716
   on command names if this is the first word in the line, or on filenames
717
   if not. */
718
void initialize_readline ()
719
{
720
  /* Allow conditional parsing of the ~/.inputrc file. */
721
  rl_readline_name = "or1ksim";
722
 
723
  /* Tell the completer that we want a crack first. */
724
  rl_attempted_completion_function = (CPPFunction *)sim_completion;
725
}
726
 
727
/* Attempt to complete on the contents of TEXT.  START and END bound the
728
   region of rl_line_buffer that contains the word to complete.  TEXT is
729
   the word to complete.  We can use the entire contents of rl_line_buffer
730
   in case we want to do some simple parsing.  Return the array of matches,
731
   or NULL if there aren't any. */
732
char **
733
sim_completion (text, start, end)
734
     char *text;
735
     int start, end;
736
{
737
  char **matches;
738
 
739
  matches = (char **)NULL;
740
 
741
  /* If this word is at the start of the line, then it is a command
742
     to complete.  Otherwise it is the name of a file in the current
743
     directory. */
744
  if (start == 0)
745
    matches = completion_matches (text, command_generator);
746
 
747
  return (matches);
748
}
749
 
750
/* Generator function for command completion.  STATE lets us know whether
751
   to start from scratch; without any state (i.e. STATE == 0), then we
752
   start at the top of the list. */
753
char *
754
command_generator (text, state)
755
     char *text;
756
     int state;
757
{
758
  static int list_index, len;
759
  char *name;
760
 
761
  /* If this is a new word to complete, initialize now.  This includes
762
     saving the length of TEXT for efficiency, and initializing the index
763
     variable to 0. */
764
  if (!state)
765
    {
766
      list_index = 0;
767
      len = strlen (text);
768
    }
769
 
770
  /* Return the next name which partially matches from the command list. */
771
  while (name = sim_commands[list_index])
772
    {
773
      list_index++;
774
 
775
      if (strncmp (name, text, len) == 0)
776
        return (dupstr(name));
777
    }
778
 
779
  /* If no names matched, then return NULL. */
780
  return ((char *)NULL);
781
}
782
 
783
char *
784
dupstr (s)
785
     char *s;
786
{
787
  char *r;
788
 
789
  r = xmalloc (strlen (s) + 1);
790
  strcpy (r, s);
791
  return (r);
792
}
793
 
794
/* Repeats the last command.  */
795
char *
796
repeat_last_command ()
797
{
798
  int offset = where_history ();
799
  HIST_ENTRY *hist;
800
 
801
  if (hist = history_get (offset))
802
    {
803
      return dupstr (hist->line);
804
    }
805
  return 0;
806
}
807 16 jrydberg
 
808
#endif
809
 
810 138 markom
extern char *disassembled;
811 257 erez
void debugmem( unsigned long from, unsigned long to )
812
{
813 138 markom
  int i;
814
  printf("starting to dump mem...\n");
815 257 erez
  for(i=from; i<to; ) {
816 261 markom
    struct label_entry *entry;
817 221 markom
    unsigned int _insn;
818 261 markom
    printf("i=%x :: ", i);
819
 
820
    if (verify_memoryarea(i) && (entry = get_label(i)))
821
      printf("label: %s |", entry->name);
822 221 markom
 
823
    iqueue[0].insn = _insn = evalsim_mem32(i);
824
    iqueue[0].insn_index = insn_decode(_insn);
825
    disassemble_insn (_insn);
826
    printf("%08x %s\n", _insn, disassembled);
827 257 erez
                i += insn_len( iqueue[0].insn_index );
828 138 markom
  }
829 21 cmchen
}
830 123 markom
 
831 127 chris
static int tcp_level = 0;
832
 
833 123 markom
/* Added by CZ 24/05/01 */
834
int GetServerSocket(const char* name,const char* proto,int port)
835
{
836
  struct servent *service;
837
  struct protoent *protocol;
838
  struct sockaddr_in sa;
839
  struct hostent *hp;
840
  int sockfd;
841
  char myname[256];
842
  int flags;
843
  char sTemp[256];
844
 
845
  /* First, get the protocol number of TCP */
846
  if(!(protocol = getprotobyname(proto)))
847
    {
848
      sprintf(sTemp,"Unable to load protocol \"%s\"",proto);
849
      perror(sTemp);
850
      return 0;
851
    }
852 127 chris
  tcp_level = protocol->p_proto; /* Save for later */
853
 
854 123 markom
  /* If we weren't passed a non standard port, get the port
855
     from the services directory. */
856
  if(!port)
857
    {
858
      if(service = getservbyname(name,protocol->p_name))
859
        port = ntohs(service->s_port);
860
    }
861
 
862
  /* Create the socket using the TCP protocol */
863
  if((sockfd = socket(PF_INET,SOCK_STREAM,protocol->p_proto)) < 0)
864
    {
865
      perror("Unable to create socket");
866
      return 0;
867
    }
868
 
869
  flags = 1;
870
  if(setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,(const char*)&flags,sizeof(int))
871
 < 0)
872
    {
873
      sprintf(sTemp,"Can not set SO_REUSEADDR option on socket %d",sockfd);
874
      perror(sTemp);
875
      close(sockfd);
876
      return 0;
877
    }
878
 
879
  /* The server should also be non blocking. Get the current flags. */
880
  if(fcntl(sockfd,F_GETFL,&flags) < 0)
881
    {
882
      sprintf(sTemp,"Unable to get flags for socket %d",sockfd);
883
      perror(sTemp);
884
      close(sockfd);
885
      return 0;
886
    }
887
 
888
  /* Set the nonblocking flag */
889
  if(fcntl(sockfd,F_SETFL, flags | O_NONBLOCK) < 0)
890
    {
891
      sprintf(sTemp,"Unable to set flags for socket %d to value 0x%08x",
892
              sockfd,flags | O_NONBLOCK);
893
      perror(sTemp);
894
      close(sockfd);
895
      return 0;
896
    }
897
 
898
  /* Find out what our address is */
899
  memset(&sa,0,sizeof(struct sockaddr_in));
900
  gethostname(myname,sizeof(myname));
901
  if(!(hp = gethostbyname(myname)))
902
    {
903
      perror("Unable to read hostname");
904
      close(sockfd);
905
      return 0;
906
    }
907
 
908
  /* Bind our socket to the appropriate address */
909
  sa.sin_family = hp->h_addrtype;
910
  sa.sin_port = htons(port);
911
  if(bind(sockfd,(struct sockaddr*)&sa,sizeof(struct sockaddr_in)) < 0)
912
    {
913
      sprintf(sTemp,"Unable to bind socket %d to port %d",sockfd,port);
914
      perror(sTemp);
915
      close(sockfd);
916
      return 0;
917
    }
918
  serverIP = sa.sin_addr.s_addr;
919
  flags = sizeof(struct sockaddr_in);
920
  if(getsockname(sockfd,(struct sockaddr*)&sa,&flags) < 0)
921
    {
922
      sprintf(sTemp,"Unable to get socket information for socket %d",sockfd);
923
      perror(sTemp);
924
      close(sockfd);
925
      return 0;
926
    }
927
  serverPort = ntohs(sa.sin_port);
928
 
929
  /* Set the backlog to 1 connections */
930
  if(listen(sockfd,1) < 0)
931
    {
932
      sprintf(sTemp,"Unable to set backlog on socket %d to %d",sockfd,1);
933
      perror(sTemp);
934
      close(sockfd);
935
      return 0;
936
    }
937
 
938
  return sockfd;
939
}
940
 
941 127 chris
void BlockJTAG()
942 123 markom
{
943
  struct pollfd fds[2];
944
  int n = 0;
945 127 chris
 
946
  fds[n].fd = server_fd;
947
  fds[n].events = POLLIN;
948
  fds[n++].revents = 0;
949
  if(gdb_fd)
950
    {
951
      fds[n].fd = gdb_fd;
952
      fds[n].events = POLLIN;
953
      fds[n++].revents = 0;
954
    }
955
  poll(fds,n,-1);
956
}
957
 
958
void HandleServerSocket(Boolean block)
959
{
960
  struct pollfd fds[3];
961
  int n = 0;
962 123 markom
  int timeout = block ? -1 : 0;
963
  int server_index = -1;
964
  int gdb_index = -1;
965
  Boolean data_on_stdin = false;
966
  int o_serv_fd = server_fd;
967
 
968
  if(!o_serv_fd && !gdb_fd)
969
    return;
970
 
971
  if(o_serv_fd)
972
    {
973
      fds[n].fd = o_serv_fd;
974
      fds[n].events = POLLIN;
975
      fds[n++].revents = 0;
976
    }
977
  if(gdb_fd)
978
    {
979
      fds[n].fd = gdb_fd;
980
      fds[n].events = POLLIN;
981
      fds[n++].revents = 0;
982
    }
983
  if(block)
984
    {
985
      fds[n].fd = 0;
986
      fds[n].events = POLLIN;
987
      fds[n++].revents = 0;
988
    }
989
 
990
  while(!data_on_stdin)
991
    {
992
      switch(poll(fds,n,timeout))
993
        {
994
        case -1:
995
          if(errno == EINTR)
996
            continue;
997
          perror("poll");
998
          server_fd = 0;
999
          break;
1000
        case 0: /* Nothing interesting going on */
1001
          data_on_stdin = true; /* Can only get here if nonblocking */
1002
          break;
1003
        default:
1004 127 chris
          /* Make sure to handle the gdb port first! */
1005 123 markom
          if((fds[0].revents && (gdb_fd && !o_serv_fd) ||
1006
              fds[1].revents && (server_fd && gdb_fd)))
1007
            {
1008
              int revents = o_serv_fd ? fds[1].revents : fds[0].revents;
1009
 
1010
              if(revents & POLLIN)
1011
                GDBRequest();
1012
              else /* Error Occurred */
1013
                {
1014
                  fprintf(stderr,"Received flags 0x%08x on gdb socket. Shutting down.\n",revents);
1015
                  close(gdb_fd);
1016
                  gdb_fd = 0;
1017
                }
1018
            }
1019 127 chris
          if(fds[0].revents && o_serv_fd)
1020
            {
1021
              if(fds[0].revents & POLLIN)
1022
                JTAGRequest();
1023
              else /* Error Occurred */
1024
                {
1025
                  fprintf(stderr,"Received flags 0x%08x on server. Shutting down.\n",fds[0].revents);
1026
                  close(o_serv_fd);
1027
                  server_fd = 0;
1028
                  serverPort = 0;
1029
                  serverIP = 0;
1030
                }
1031
            }
1032 123 markom
          if(fds[2].revents || (fds[1].revents && !gdb_fd))
1033
            data_on_stdin = true;
1034
          break;
1035
        } /* End of switch statement */
1036
    } /* End of while statement */
1037
}
1038
 
1039
void JTAGRequest()
1040
{
1041
  struct sockaddr_in sa;
1042
  struct sockaddr* addr = (struct sockaddr*)&sa;
1043
  int n = sizeof(struct sockaddr_in);
1044
  int fd = accept(server_fd,addr,&n);
1045
  int on_off = 0; /* Turn off Nagel's algorithm on the socket */
1046
  int flags;
1047
  char sTemp[256];
1048
 
1049
  if(fd < 0)
1050
    {
1051
      /* This is valid, because a connection could have started,
1052
         and then terminated due to a protocol error or user
1053
         initiation before the accept could take place. */
1054
      if(errno != EWOULDBLOCK && errno != EAGAIN)
1055
        {
1056
          perror("accept");
1057
          close(server_fd);
1058
          server_fd = 0;
1059
          serverPort = 0;
1060
          serverIP = 0;
1061
        }
1062
      return;
1063
    }
1064
 
1065
  if(gdb_fd)
1066
    {
1067
      close(fd);
1068
      return;
1069
    }
1070
 
1071
  if(fcntl(fd,F_GETFL,&flags) < 0)
1072
    {
1073
      sprintf(sTemp,"Unable to get flags for gdb socket %d",fd);
1074
      perror(sTemp);
1075
      close(fd);
1076
      return;
1077
    }
1078
 
1079
  if(fcntl(fd,F_SETFL, flags | O_NONBLOCK) < 0)
1080
    {
1081
      sprintf(sTemp,"Unable to set flags for gdb socket %d to value 0x%08x",
1082
              fd,flags | O_NONBLOCK);
1083
      perror(sTemp);
1084
      close(fd);
1085
      return;
1086
    }
1087
 
1088 127 chris
  if(setsockopt(fd,tcp_level,TCP_NODELAY,&on_off,sizeof(int)) < 0)
1089 123 markom
    {
1090
      sprintf(sTemp,"Unable to disable Nagel's algorithm for socket %d.\nsetsockopt",fd);
1091
      perror(sTemp);
1092
      close(fd);
1093
      return;
1094
    }
1095
 
1096
  gdb_fd = fd;
1097
}
1098
 
1099
void GDBRequest()
1100
{
1101 127 chris
  JTAGProxyWriteMessage msg_write;
1102
  JTAGProxyReadMessage msg_read;
1103
  JTAGProxyChainMessage msg_chain;
1104
  JTAGProxyWriteResponse resp_write;
1105
  JTAGProxyReadResponse resp_read;
1106
  JTAGProxyChainResponse resp_chain;
1107 139 chris
  JTAGProxyBlockWriteMessage *msg_bwrite;
1108
  JTAGProxyBlockReadMessage msg_bread;
1109
  JTAGProxyBlockWriteResponse resp_bwrite;
1110
  JTAGProxyBlockReadResponse *resp_bread;
1111 127 chris
  char *buf;
1112
  unsigned long long data;
1113 139 chris
  int err = 0;
1114 127 chris
  uint32_t command,length;
1115 139 chris
  int len,i;
1116 127 chris
 
1117
  /* First, we must read the incomming command */
1118
  if(gdb_read(&command,sizeof(uint32_t)) < 0)
1119
    {
1120
      if(gdb_fd)
1121
        {
1122
          perror("gdb socket - 1");
1123
          close(gdb_fd);
1124
          gdb_fd = 0;
1125
        }
1126
      return;
1127
    }
1128
  if(gdb_read(&length,sizeof(uint32_t)) < 0)
1129
    {
1130
      if(gdb_fd)
1131
        {
1132
          perror("gdb socket - 2");
1133
          close(gdb_fd);
1134
          gdb_fd = 0;
1135
        }
1136
      return;
1137
    }
1138
  length = ntohl(length);
1139
 
1140
  /* Now, verify the protocol and implement the command */
1141
  switch(ntohl(command))
1142
    {
1143
    case JTAG_COMMAND_WRITE:
1144
      if(length != sizeof(msg_write) - 8)
1145
        {
1146
          ProtocolClean(length,JTAG_PROXY_PROTOCOL_ERROR);
1147
          return;
1148
        }
1149
      buf = (char*)&msg_write;
1150
      if(gdb_read(&buf[8],length) < 0)
1151
        {
1152
          if(gdb_fd)
1153
            {
1154
              perror("gdb socket - 3");
1155
              close(gdb_fd);
1156
              gdb_fd = 0;
1157
            }
1158
          return;
1159
        }
1160
      msg_write.address = ntohl(msg_write.address);
1161
      msg_write.data_H = ntohl(msg_write.data_H);
1162
      msg_write.data_L = ntohl(msg_write.data_L);
1163
      err = DebugSetRegister(msg_write.address,msg_write.data_L);
1164
      resp_write.status = htonl(err);
1165
      if(gdb_write(&resp_write,sizeof(resp_write)) < 0)
1166
        {
1167
          if(gdb_fd)
1168
            {
1169
              perror("gdb socket - 4");
1170
              close(gdb_fd);
1171
              gdb_fd = 0;
1172
            }
1173
          return;
1174
        }
1175
      break;
1176
    case JTAG_COMMAND_READ:
1177
      if(length != sizeof(msg_read) - 8)
1178
        {
1179
          ProtocolClean(length,JTAG_PROXY_PROTOCOL_ERROR);
1180
          return;
1181
        }
1182
      buf = (char*)&msg_read;
1183
      if(gdb_read(&buf[8],length) < 0)
1184
        {
1185
          if(gdb_fd)
1186
            {
1187
              perror("gdb socket - 5");
1188
              close(gdb_fd);
1189
              gdb_fd = 0;
1190
            }
1191
          return;
1192
        }
1193
      msg_read.address = ntohl(msg_read.address);
1194
      err = DebugGetRegister(msg_read.address,&resp_read.data_L);
1195
      resp_read.status = htonl(err);
1196
      resp_read.data_H = 0;
1197
      resp_read.data_L = htonl(resp_read.data_L);
1198
      if(gdb_write(&resp_read,sizeof(resp_read)) < 0)
1199
        {
1200
          if(gdb_fd)
1201
            {
1202
              perror("gdb socket - 6");
1203
              close(gdb_fd);
1204
              gdb_fd = 0;
1205
            }
1206
          return;
1207
        }
1208
      break;
1209 139 chris
    case JTAG_COMMAND_BLOCK_WRITE:
1210
      if(length < sizeof(JTAGProxyBlockWriteMessage)-8)
1211
        {
1212
          ProtocolClean(length,JTAG_PROXY_PROTOCOL_ERROR);
1213
          return;
1214
        }
1215
      if(!(buf = (char*)malloc(8+length)))
1216
        {
1217
          ProtocolClean(length,JTAG_PROXY_OUT_OF_MEMORY);
1218
          return;
1219
        }
1220
      msg_bwrite = (JTAGProxyBlockWriteMessage*)buf;
1221
      if(gdb_read(&buf[8],length) < 0)
1222
        {
1223
          if(gdb_fd)
1224
            {
1225
              perror("gdb socket - 5");
1226
              close(gdb_fd);
1227
              gdb_fd = 0;
1228
            }
1229
          free(buf);
1230
          return;
1231
        }
1232
      msg_bwrite->address = ntohl(msg_bwrite->address);
1233
      msg_bwrite->nRegisters = ntohl(msg_bwrite->nRegisters);
1234
      for(i=0;i<msg_bwrite->nRegisters;i++)
1235
        {
1236 221 markom
          int t_err = 0;
1237 139 chris
 
1238
          msg_bwrite->data[i] = ntohl(msg_bwrite->data[i]);
1239
          t_err = DebugSetRegister(msg_bwrite->address+i,msg_bwrite->data[i]);
1240
          err = err ? err : t_err;
1241
        }
1242
      resp_bwrite.status = htonl(err);
1243
      free(buf);
1244 212 erez
      buf = NULL;
1245
      msg_bwrite = NULL;
1246 139 chris
      if(gdb_write(&resp_bwrite,sizeof(resp_bwrite)) < 0)
1247
        {
1248
          if(gdb_fd)
1249
            {
1250
              perror("gdb socket - 4");
1251
              close(gdb_fd);
1252
              gdb_fd = 0;
1253
            }
1254
          return;
1255
        }
1256
      break;
1257
    case JTAG_COMMAND_BLOCK_READ:
1258
      if(length != sizeof(msg_bread) - 8)
1259
        {
1260
          ProtocolClean(length,JTAG_PROXY_PROTOCOL_ERROR);
1261
          return;
1262
        }
1263
      buf = (char*)&msg_bread;
1264
      if(gdb_read(&buf[8],length) < 0)
1265
        {
1266
          if(gdb_fd)
1267
            {
1268
              perror("gdb socket - 5");
1269
              close(gdb_fd);
1270
              gdb_fd = 0;
1271
            }
1272
          return;
1273
        }
1274
      msg_bread.address = ntohl(msg_bread.address);
1275
      msg_bread.nRegisters = ntohl(msg_bread.nRegisters);
1276
      len = sizeof(JTAGProxyBlockReadResponse) + 4*(msg_bread.nRegisters-1);
1277
      if(!(buf = (char*)malloc(len)))
1278
        {
1279
          ProtocolClean(0,JTAG_PROXY_OUT_OF_MEMORY);
1280
          return;
1281
        }
1282
      resp_bread = (JTAGProxyBlockReadResponse*)buf;
1283
      for(i=0;i<msg_bread.nRegisters;i++)
1284
        {
1285
          int t_err;
1286
 
1287
          t_err = DebugGetRegister(msg_bread.address+i,&resp_bread->data[i]);
1288
          resp_bread->data[i] = htonl(resp_bread->data[i]);
1289
          err = err ? err : t_err;
1290
        }
1291
      resp_bread->status = htonl(err);
1292
      resp_bread->nRegisters = htonl(msg_bread.nRegisters);
1293
      if(gdb_write(resp_bread,len) < 0)
1294
        {
1295
          if(gdb_fd)
1296
            {
1297
              perror("gdb socket - 6");
1298
              close(gdb_fd);
1299
              gdb_fd = 0;
1300
            }
1301
          free(buf);
1302
          return;
1303
        }
1304
      free(buf);
1305 212 erez
      buf = NULL;
1306 221 markom
      resp_bread = NULL;
1307 139 chris
      break;
1308 127 chris
    case JTAG_COMMAND_CHAIN:
1309
      if(length != sizeof(msg_chain) - 8)
1310
        {
1311
          ProtocolClean(length,JTAG_PROXY_PROTOCOL_ERROR);
1312
          return;
1313
        }
1314
      buf = (char*)&msg_chain;
1315
      if(gdb_read(&buf[8],sizeof(msg_chain)-8) < 0)
1316
        {
1317
          if(gdb_fd)
1318
            {
1319
              perror("gdb socket - 7");
1320
              close(gdb_fd);
1321
              gdb_fd = 0;
1322
            }
1323
          return;
1324
        }
1325
      msg_chain.chain = htonl(msg_chain.chain);
1326
      err = DebugSetChain(msg_chain.chain);
1327
      resp_chain.status = htonl(err);
1328
      if(gdb_write(&resp_chain,sizeof(resp_chain)) < 0)
1329
        {
1330
          if(gdb_fd)
1331
            {
1332
              perror("gdb socket - 8");
1333
              close(gdb_fd);
1334
              gdb_fd = 0;
1335
            }
1336
          return;
1337
        }
1338
      break;
1339
    default:
1340
      ProtocolClean(length,JTAG_PROXY_COMMAND_NOT_IMPLEMENTED);
1341
      break;
1342
    }
1343 123 markom
}
1344 127 chris
 
1345
void ProtocolClean(int length,int32_t err)
1346
{
1347
  char buf[4096];
1348
 
1349
  err = htonl(err);
1350
  if((gdb_read(buf,length) < 0) ||
1351
      (gdb_write(&err,sizeof(err)) < 0) && gdb_fd)
1352
    {
1353
      perror("gdb socket - 9");
1354
      close(gdb_fd);
1355
      gdb_fd = 0;
1356
    }
1357
}
1358
 
1359
static int gdb_write(void* buf,int len)
1360
{
1361
  int n;
1362
  char* w_buf = (char*)buf;
1363
  struct pollfd block;
1364
 
1365
  while(len)
1366
    {
1367
      if((n = write(gdb_fd,w_buf,len)) < 0)
1368
        {
1369
          switch(errno)
1370
            {
1371
            case EWOULDBLOCK: /* or EAGAIN */
1372
              /* We've been called on a descriptor marked
1373
                 for nonblocking I/O. We better simulate
1374
                 blocking behavior. */
1375
              block.fd = gdb_fd;
1376
              block.events = POLLOUT;
1377
              block.revents = 0;
1378
              poll(&block,1,-1);
1379
              continue;
1380
            case EINTR:
1381
              continue;
1382
            case EPIPE:
1383
              close(gdb_fd);
1384
              gdb_fd = 0;
1385
              return -1;
1386
            default:
1387
              return -1;
1388
            }
1389
        }
1390
      else
1391
        {
1392
          len -= n;
1393
          w_buf += n;
1394
        }
1395
    }
1396
  return 0;
1397
}
1398
 
1399
static int gdb_read(void* buf,int len)
1400
{
1401
  int n;
1402
  char* r_buf = (char*)buf;
1403
  struct pollfd block;
1404
 
1405
  while(len)
1406
    {
1407
      if((n = read(gdb_fd,r_buf,len)) < 0)
1408
        {
1409
          switch(errno)
1410
            {
1411
            case EWOULDBLOCK: /* or EAGAIN */
1412
              /* We've been called on a descriptor marked
1413
                 for nonblocking I/O. We better simulate
1414
                 blocking behavior. */
1415
              block.fd = gdb_fd;
1416
              block.events = POLLIN;
1417
              block.revents = 0;
1418
              poll(&block,1,-1);
1419
              continue;
1420
            case EINTR:
1421
              continue;
1422
            default:
1423
              return -1;
1424
            }
1425
        }
1426
      else if(n == 0)
1427
        {
1428
          close(gdb_fd);
1429
          gdb_fd = 0;
1430
          return -1;
1431
        }
1432
      else
1433
        {
1434
          len -= n;
1435
          r_buf += n;
1436
        }
1437
    }
1438
  return 0;
1439
}

powered by: WebSVN 2.1.0

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