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 123

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 2 cvs
 
43 16 jrydberg
#ifdef HAVE_LIBREADLINE
44 7 jrydberg
#include <readline/readline.h>
45
#include <readline/history.h>
46 16 jrydberg
#endif /* HAVE_LIBREADLINE */
47 7 jrydberg
 
48 2 cvs
#include "arch.h"
49
#include "parse.h"
50
#include "abstract.h"
51
#include "trace.h"
52
#include "execute.h"
53 69 lampret
#include "sim-config.h"
54 103 lampret
#include "spr_defs.h"
55 2 cvs
 
56 28 lampret
#include "coff.h"
57
 
58 123 markom
/* Added by CZ 24/05/01 */
59
#include <signal.h>
60
#include <errno.h>
61
typedef enum {
62
  false = 0,
63
  true = 1,
64
} Boolean;
65
unsigned int serverIP = 0;
66
unsigned int serverPort = 0;
67
unsigned int server_fd = 0;
68
unsigned int gdb_fd = 0;
69
void HandleServerSocket(Boolean);
70
void JTAGRequest(void);
71
void GDBRequest(void);
72
int GlobalMode = 0;   /* Start off in the orginal mode */
73
 
74 2 cvs
/* CVS revision number. */
75 123 markom
const char rcsrev[] = "$Revision: 1.16 $";
76 2 cvs
 
77
/* Continuos run versus single step tracing switch. */
78
int cont_run;
79
 
80
/* History of execution */
81
int histexec[HISTEXEC_LEN];
82
 
83 7 jrydberg
char *sim_commands [] = {
84
  "q",
85
  "t",
86
  "help",
87 21 cmchen
  "de",
88 7 jrydberg
  "dm",
89
  "run",
90
  "pr",
91
  "pm",
92
  "pc",
93 18 lampret
  "reset",
94
  "break",
95 7 jrydberg
  "hist",
96
  "stats",
97
  "info",
98
  "r",
99 54 lampret
  "dv",
100 7 jrydberg
 
101
};
102
 
103 2 cvs
void debug(const char *format, ...)
104
{
105 7 jrydberg
  char *p;
106
  va_list ap;
107 2 cvs
 
108 69 lampret
  if (config.simdebug) {
109
    if ((p = malloc(1000)) == NULL)
110
      return;
111
    va_start(ap, format);
112
    (void) vsnprintf(p, 1000, format, ap);
113
    va_end(ap);
114
    printf("%s\n", p);
115
    fflush(stdout);
116
    free(p);
117
  } else {
118
#if DEBUG
119 7 jrydberg
  if ((p = malloc(1000)) == NULL)
120
    return;
121
  va_start(ap, format);
122
  (void) vsnprintf(p, 1000, format, ap);
123
  va_end(ap);
124
  printf("%s\n", p);
125
  fflush(stdout);
126
  free(p);
127 2 cvs
#endif
128 69 lampret
  }
129 7 jrydberg
  return;
130 2 cvs
}
131
 
132 30 lampret
/* Strip whitespace from the start and end of STRING.  Return a pointer
133
   into STRING. */
134
#ifndef whitespace
135
#define whitespace(a)   ((a) == '\t' ? 1 : ((a) == ' '? 1 : 0))
136
#endif
137
 
138
char *
139
stripwhite (string)
140
     char *string;
141
{
142
  register char *s, *t;
143
 
144
  for (s = string; whitespace (*s); s++)
145
    ;
146
 
147
  if (*s == 0)
148
    return (s);
149
 
150
  t = s + strlen (s) - 1;
151
  while (t > s && whitespace (*t))
152
    t--;
153
  *++t = '\0';
154
 
155
  return s;
156
}
157
 
158 7 jrydberg
void
159
ctrl_c(signum)
160
     int signum;
161 2 cvs
{
162
        cont_run = 1;
163 123 markom
        config.iprompt = 1;
164 7 jrydberg
  signal(SIGINT, ctrl_c);
165 2 cvs
}
166
 
167 7 jrydberg
void
168
version()
169 2 cvs
{
170 7 jrydberg
        printf ("\n");
171 18 lampret
        printf ("OpenRISC 1000 (OR16+OR32) Architectural Simulator, %s\n", rcsrev);
172
        printf ("Copyright (C) 1999 Damjan Lampret, lampret@opencores.org\n");
173
        printf ("Copyright (C) 2000 Damjan Lampret, lampret@opencores.org\n");
174 21 cmchen
        printf ("                   Jimmy Chen-Min Chen, jimmy@ee.nctu.edu.tw\n");
175 18 lampret
        printf ("                   Johan Rydberg, johan.rydberg@insight.se\n");
176 7 jrydberg
        printf ("Visit http://www.opencores.org for more information about ");
177
        printf ("OpenRISC 1000 and\nother open source cores.\n\n");
178
        printf ("This software comes with ABSOLUTELY NO WARRANTY; for ");
179
        printf ("details see COPYING.\nThis is free software, and you ");
180
        printf ("are welcome to redistribute it under certain\nconditions; ");
181
        printf ("for details see COPYING.\n");
182 2 cvs
}
183
 
184 7 jrydberg
void
185
help()
186 2 cvs
{
187
        printf("q                        - quit simulator\n");
188
        printf("r                        - display all registers\n");
189
        printf("t                        - execute next instruction\n");
190
        printf("run <cycles> [<hush>]    - execute <cycles> instructions, no reg dump if hush\n");
191
        printf("pr <r> <value>           - patch register <r> with <value>\n");
192
        printf("dm <fromaddr> [<toaddr>] - display memory from <fromaddr> to <toaddr>\n");
193 21 cmchen
        printf("de                       - debug insn memory\n");
194 2 cvs
        printf("pm <addr> <value>        - patch memory location <addr> with <value>\n");
195
        printf("pc <value>               - patch PC register with <value>\n");
196 18 lampret
        printf("break <addr>             - toggle breakpoint at address <addr>\n");
197
        printf("reset                    - simulator reset\n");
198 2 cvs
        printf("hist                     - execution history\n");
199 6 lampret
        printf("stats <num|clear>        - execution statistics num or clear it.\n");
200
        printf("info                     - configuration info (caches etc.)\n");
201 54 lampret
        printf("dv <fromaddr> [<toaddr>] [<modname>] - dumps memory as verilog (use redirect)\n");
202 86 lampret
        printf("dh <fromaddr> [<toaddr>] - dumps memory as hex code (use redirect)\n");
203 2 cvs
        printf("<cmd> > <filename>       - redirect simulator stdout to <filename> (and not emulated printf)\n");
204 69 lampret
        printf("debug                    - toggles simulator debug mode\n");
205 2 cvs
        printf("help                     - available commands (this list)\n");
206
}
207
 
208 21 cmchen
void debugmem();
209
 
210 7 jrydberg
main(argc, argv)
211
     int argc;
212
     char *argv[];
213 2 cvs
{
214 7 jrydberg
        char *linestr;
215 16 jrydberg
        char item1[500], b2[500];
216 2 cvs
        char *redirstr;
217
        int hush;
218 123 markom
        unsigned long endaddr = 0xFFFFFFFF;
219
        int first_prompt = 1;
220
 
221 103 lampret
        srand(getpid());
222
        init_defconfig();
223 123 markom
        if (parse_args(argc, argv)) {
224 103 lampret
                printf("Usage: %s [options] <filename>\n", argv[0]);
225
                printf("Options:\n");
226
                printf(" -v: version and copyright note\n");
227
                printf(" -i: enable interactive command prompt\n");
228
                printf(" -bpb: disable branch prediction buffer analysis\n");
229
                printf(" -btic: disable branch prediction target insn cache analysis\n");
230
                printf(" -hazards: disable dependency hazards analysis\n");
231
                printf(" -history: disable instruction stream history analysis\n");
232
                printf(" -superscalar: disable superscalar analysis\n");
233 123 markom
                printf(" -fast: disable BPB, BTIC, SLP, dependency hazards, history"
234
                       " analysis etc.\n");
235 103 lampret
                printf(" -upr <n>: set UPR to n\n");
236
                printf(" -ver <n>: set VR[VER] to n\n");
237
                printf(" -rev <n>: set VR[REV] to n\n");
238 123 markom
                printf(" -loadmem[@<n>] <filename>: load memory with file, "
239
                       "optionally at address <n>\n"); /* (CZ) */
240
                printf(" -nosrv: do not launch JTAG proxy server\n"); /* (CZ) */
241
                printf(" -srv <n>: launch JTAG proxy server on port <n>\n"); /* (CZ) */
242
                printf(" -initmem <n | random>: initialize memory to value "
243
                       "<n> or random\n"); /* (CZ) */
244 103 lampret
                exit(-1);
245
        }
246 7 jrydberg
 
247 16 jrydberg
#ifdef HAVE_LIBREADLINE
248 7 jrydberg
  initialize_readline ();       /* Bind our completer. */
249 16 jrydberg
#endif  
250 123 markom
 
251
  if(!config.inhibit_server)
252
    {
253
      serverPort = config.server_port;
254
      if(server_fd = GetServerSocket("or1ksim","tcp",serverPort))
255
        printf("JTAG Proxy server started on port %d\n",serverPort);
256
    }
257 16 jrydberg
 
258 103 lampret
        print_config();
259 2 cvs
        signal(SIGINT, ctrl_c);
260
        initstats();
261 123 markom
 
262
        /* Modified by CZ on 24/05/01 ... if a filename is
263
           specified, behave as the simulator always has. This way,
264
           no existing test suites should be broken. If a filename
265
           is not specified, default to the new style behavior. Let
266
           the simulator start up and execute garbage, the same way
267
           a real CPU would. This should maximize the reality of
268
           the capabilities. In this mode, we will expect that
269
           someone will attach to us over the JTAG Proxy interface
270
           and begin debugging that way. */
271
 
272
        if(config.filename)
273
          {
274
            endaddr = loadcode(config.filename, MEMORY_START, 0);
275
            if (endaddr == -1) {
276
              printf("Problems loading boot code.\n");
277
              exit(1);
278
            }
279
          }
280
        else
281
          {
282
            if(config.random_mem)
283
              {
284
                int n = 0;
285
                int len = sizeof(mem);
286
                unsigned int* mptr = (unsigned int*)mem;
287
                unsigned int val = 0;
288
                int seed = time(NULL);
289
 
290
                srandom(seed);
291
                /* Print out the seed just in case we ever need to debug */
292
                printf("Seeding random generator with value %d\n",seed);
293
 
294
                for(n=0;n<len;n+=sizeof(unsigned int))
295
                  {
296
                    val = random();
297
                    if(random() > RAND_MAX/2)
298
                      val |= 0x80000000;
299
                    *mptr++ = val;
300
                  }
301
              }
302
            else if(config.pattern_mem)
303
              {
304
                int n = 0;
305
                int len = sizeof(mem);
306
                unsigned int* mptr = (unsigned int*)mem;
307
 
308
                for(n=0;n<len;n+=sizeof(unsigned int))
309
                  *mptr++ = config.pattern_mem;
310
              }
311
            else
312
              memset(mem,0,sizeof(mem));
313
 
314
            if(config.memory)
315
              {
316
                MemoryBlock* block = config.memory;
317
 
318
                while(block)
319
                  {
320
                    int fd = open(block->file,O_RDONLY);
321
                    int len,i;
322
                    struct stat buf;
323
                    char *mptr = (char*)mem;
324
                    char buffer[8192];
325
 
326
                    if(fd < 0)
327
                      {
328
                        perror(block->file);
329
                        exit(1);
330
                      }
331
                    if(fstat(fd,&buf) < 0)
332
                      {
333
                        char sTemp[256];
334
 
335
                        sprintf(sTemp,"stat(\"%s\")",block->file);
336
                        perror(sTemp);
337
                        exit(1);
338
                      }
339
                    if(!S_ISREG(buf.st_mode))
340
                      {
341
                        fprintf(stderr,"File \"%s\" is not a regular file.\n",
342
                                block->file);
343
                        exit(1);
344
                      }
345
 
346
                    len = buf.st_size;
347
                    mptr += block->address;
348
                    for(i=0;i<len;)
349
                      {
350
                        int n = read(fd,buffer,sizeof(buffer));
351
 
352
                        switch(n)
353
                          {
354
                          case -1:
355
                            if(errno == EINTR)
356
                              continue;
357
                            perror(block->file);
358
                            exit(1);
359
                          case 0:
360
                            fprintf(stderr,"File \"%s\": premature end of file.\n",
361
                                    block->file);
362
                            exit(1);
363
                          default:
364
                            memcpy(mptr,buffer,n);
365
                            i+= n;
366
                            break;
367
                          }
368
                      }
369
                    close(fd);
370
                  }
371
              }
372
          }
373
        GlobalMode = config.filename == NULL; /* Old mode = 0, New mode = 1 */
374
 
375 30 lampret
        uart_reset();
376 92 lampret
        tick_reset();
377 103 lampret
        pm_reset();
378
        pic_reset();
379 2 cvs
        reset();
380 123 markom
        if(!GlobalMode)  /* Only in old mode */
381
          set_reg32("r3", endaddr);
382 7 jrydberg
 
383 103 lampret
        while(1) {
384
                if (config.iprompt) {
385 123 markom
                  if(server_fd)
386
                    {
387
                      printf ("(sim) ");
388
                      fflush(stdout);
389
                      HandleServerSocket(true);  /* block & check_stdin = true */
390
                    }
391 16 jrydberg
#ifdef HAVE_LIBREADLINE
392 123 markom
                  /* Must disable readline in new mode. It isn't compatible
393
                     with non blocking environments */
394
                  if(!server_fd)
395
                    linestr = readline("(sim) ");
396
                  else
397
                    linestr = fgets(b2, sizeof b2, stdin);
398 16 jrydberg
#else
399 123 markom
                  if(!server_fd)
400
                    printf ("(sim) ");
401
                  linestr = fgets(b2, sizeof b2, stdin);
402 16 jrydberg
#endif
403 103 lampret
                } else
404
                        strcpy(linestr = b2, "run 100000000 hush");
405 7 jrydberg
 
406 103 lampret
                if (!linestr)
407
                        break;
408
                linestr = stripwhite (linestr);
409 7 jrydberg
 
410 16 jrydberg
#ifdef HAVE_LIBREADLINE
411 123 markom
                /* Readline only works in the old mode */
412
                if(!server_fd)
413
                  {
414
                    if (strlen(linestr) == 0) {
415
                      char *l = repeat_last_command ();
416
 
417
                      if (l) {
418
                        free (linestr);
419
                        linestr = l;
420
                      }
421
                    }
422
 
423
                    if (*linestr) {
424
                      add_history (linestr);
425
                    }
426
                  }
427 16 jrydberg
#endif /* HAVE_LIBREADLINE */
428 2 cvs
 
429 103 lampret
                if (redirstr = strstr(linestr, ">")) {
430
                        *redirstr = '\0';
431
                        strtoken(&redirstr[1], item1, 1);
432
                        freopen(item1, "w+", stdout);
433
                }
434 2 cvs
 
435
                strtoken(linestr, item1, 1);
436
                if (strcmp(item1, "q") == 0)     /* quit */
437
                        exit(0);
438
                else
439
                if (strcmp(item1, "help") == 0)  /* help */
440
                        help();
441
                else
442
                if (strcmp(item1, "t") == 0) {   /* trace */
443
                        cont_run = 1;
444
                } else
445
                if (strcmp(item1, "dm") == 0) {  /* dump memory */
446
                        char item2[20];
447
                        char item3[20];
448
                        static int from = 0, to = 0;
449
 
450
                        strtoken(linestr, item2, 2);
451
                        strtoken(linestr, item3, 3);
452
 
453
                        if (strlen(item2)) {
454
                                if (item2[0] == '_')
455
                                        from = eval_label(item2);
456
                                else
457
                                        from = strtoul(item2, NULL, 0);
458
                                to = from + 0x40;
459
                        }
460
                        if (strlen(item3))
461
                                to = strtoul(item3, NULL, 0);
462
                        dumpmemory(from, to);
463 54 lampret
                        printf("\n");
464 2 cvs
                } else
465 54 lampret
                if (strcmp(item1, "dv") == 0) {/* dump memory as verilog*/
466
                        char item2[20];
467
                        char item3[20];
468
                        char item4[20];
469
                        static int from = 0, to = 0;
470
 
471
                        strtoken(linestr, item2, 2);
472
                        strtoken(linestr, item3, 3);
473
                        strtoken(linestr, item4, 4);
474
 
475
                        if (strlen(item2)) {
476
                                if (item2[0] == '_')
477
                                        from = eval_label(item2);
478
                                else
479
                                        from = strtoul(item2, NULL, 0);
480
                                to = from + 0x40;
481
                        }
482
                        if (strlen(item3))
483
                                to = strtoul(item3, NULL, 0);
484
                        if (!strlen(item4))
485
                                strcpy(item4, "or1k_mem");
486
                        dumpverilog(item4, from, to);
487
                        printf("\n");
488
                } else
489 86 lampret
                if (strcmp(item1, "dh") == 0) {/* dump memory as hex*/
490
                        char item2[20];
491
                        char item3[20];
492
                        static int from = 0, to = 0;
493
 
494
                        strtoken(linestr, item2, 2);
495
                        strtoken(linestr, item3, 3);
496
 
497
                        if (strlen(item2)) {
498
                                if (item2[0] == '_')
499
                                        from = eval_label(item2);
500
                                else
501
                                        from = strtoul(item2, NULL, 0);
502
                                to = from + 0x40;
503
                        }
504
                        if (strlen(item3))
505
                                to = strtoul(item3, NULL, 0);
506
                        dumphex(from, to);
507
                        printf("\n");
508
                } else
509 2 cvs
                if (strcmp(item1, "pm") == 0) {  /* patch memory */
510
                        char item2[20];
511
                        char item3[20];
512
                        static int addr = 0;
513 123 markom
                        int breakpoint = 0;
514
 
515 2 cvs
                        strtoken(linestr, item2, 2);
516
                        strtoken(linestr, item3, 3);
517
                        if (strlen(item2))
518
                                if (item2[0] == '_')
519
                                        addr = eval_label(item2);
520
                                else
521
                                        addr = strtoul(item2, NULL, 0);
522 123 markom
                        set_mem32(addr, strtoul(item3, NULL, 0), &breakpoint);
523 2 cvs
                } else
524
                if (strcmp(item1, "pr") == 0) {  /* patch regs */
525
                        char item2[20];
526
                        char item3[20];
527
 
528
                        strtoken(linestr, item2, 2);
529
                        strtoken(linestr, item3, 3);
530
                        set_reg32(item2, strtoul(item3, NULL, 0));
531
                } else
532
                if (strcmp(item1, "pc") == 0) {  /* patch PC */
533
                        char item2[20];
534
 
535
                        strtoken(linestr, item2, 2);
536 86 lampret
                        pcnext = strtoul(item2, NULL, 0);
537 2 cvs
                } else
538 7 jrydberg
                if (strcmp(item1, "break") == 0) {       /* set/clear breakpoint */
539 2 cvs
                        char item2[20];
540
 
541
                        strtoken(linestr, item2, 2);
542
                        set_insnbrkpoint(strtoul(item2, NULL, 0));
543
                } else
544
                if (strcmp(item1, "r") == 0) {   /* dump regs */
545
                        dumpreg();
546
                } else
547 21 cmchen
                if (strcmp(item1, "de") == 0) {  /* reset simulator */
548
                        debugmem();
549
                } else
550 18 lampret
                if (strcmp(item1, "reset") == 0) {       /* reset simulator */
551 30 lampret
                        uart_reset();
552 92 lampret
                        tick_reset();
553 103 lampret
                        pm_reset();
554
                        pic_reset();
555 123 markom
                        reset(); /* Old or new mode */
556 18 lampret
                } else
557 69 lampret
                if (strcmp(item1, "debug") == 0) {       /* debug mode */
558
                        config.simdebug ^= 1;
559
                } else
560 2 cvs
                if (strcmp(item1, "hist") == 0) {        /* dump history */
561
                        int i;
562
                        for(i = HISTEXEC_LEN; i; i--)
563
                                dumpmemory(histexec[i - 1], histexec[i - 1] + 4);
564 46 lampret
                        printf("\n");
565 2 cvs
                } else
566
                if (strcmp(item1, "run") == 0) { /* run */
567
                        char item2[20];
568
                        char item3[20];
569
 
570
                        strtoken(linestr, item2, 2);
571
                        strtoken(linestr, item3, 3);
572
                        if (strcmp(item3, "hush") == 0)
573
                                hush = 1;
574
                        else
575
                                hush = 0;
576
                        cont_run = strtoul(item2, NULL, 0);
577
                } else
578
                if (strcmp(item1, "stats") == 0) { /* stats */
579 6 lampret
                        char item2[20];
580
                        int i = 0;
581
 
582
                        strtoken(linestr, item2, 2);
583
                        if (strcmp(item2, "clear") == 0) {
584
                                initstats();
585
                                printf("Cleared.\n");
586
                        } else {
587
                                i = strtoul(item2, NULL, 0);
588
                                printstats(i);
589
                        }
590
                } else
591
                if (strcmp(item1, "info") == 0) { /* configuration info */
592 78 lampret
                        itlb_status(-1);
593
                        dtlb_status(-1);
594 6 lampret
                        bpb_info();
595
                        btic_info();
596
                        ic_info();
597
                        dc_info();
598 30 lampret
                        uart_status();
599
                        sprs_status();
600 7 jrydberg
                } else {
601 103 lampret
                        printf("%s: Unknown command.\n", linestr);
602
                }
603
 
604 2 cvs
                while(cont_run) {
605 123 markom
                  extern int cycle_delay;  /* Added by CZ 27/05/01. Set during exception. */
606
 
607 103 lampret
                        if (!getsprbits(SPR_PMR, SPR_PMR_DME | SPR_PMR_SME)) {
608 123 markom
                          if(cycle_delay <= 0)
609
                            {
610
                              cont_run--;
611
                              if(fetch()) {
612
                                cont_run = 0; /* memory breakpoint encountered */
613
                                break;
614
                              }
615
                              decode_execute(&iqueue[0]);
616
                              update_pc();
617
                              analysis();
618
                              if (!hush)
619
                                dumpreg();
620
                            }
621
                          else
622
                            cycle_delay--;
623
 
624
                          pic_clock();
625
                          dc_clock();
626
                          ic_clock();
627 103 lampret
                        }
628
                        if (!getsprbits(SPR_PMR, SPR_PMR_SME))
629 123 markom
                          tick_clock();
630 103 lampret
                        pm_clock();
631 30 lampret
                        uart_clock();
632 123 markom
                        HandleServerSocket(false); /* block & check_stdin = false */
633 2 cvs
                }
634
                hush = 0;
635
                fflush(stdout);
636
                freopen("/dev/fd/0", "w+", stdout);
637
 
638 123 markom
                if (!config.iprompt && !GlobalMode)     /* non-interactive quit in old mode */
639 103 lampret
                        exit(0);
640
 
641 16 jrydberg
#ifdef HAVE_LIBREADLINE
642 103 lampret
                if (linestr)
643
                        free (linestr);
644 16 jrydberg
#endif
645 7 jrydberg
 
646 2 cvs
        }
647
        exit(0);
648
}
649 7 jrydberg
 
650 16 jrydberg
#ifdef HAVE_LIBREADLINE
651 7 jrydberg
char *command_generator ();
652
char **sim_completion ();
653
 
654
/* Tell the GNU readline library how to complete.  We want to try to complete
655
   on command names if this is the first word in the line, or on filenames
656
   if not. */
657
void initialize_readline ()
658
{
659
  /* Allow conditional parsing of the ~/.inputrc file. */
660
  rl_readline_name = "or1ksim";
661
 
662
  /* Tell the completer that we want a crack first. */
663
  rl_attempted_completion_function = (CPPFunction *)sim_completion;
664
}
665
 
666
/* Attempt to complete on the contents of TEXT.  START and END bound the
667
   region of rl_line_buffer that contains the word to complete.  TEXT is
668
   the word to complete.  We can use the entire contents of rl_line_buffer
669
   in case we want to do some simple parsing.  Return the array of matches,
670
   or NULL if there aren't any. */
671
char **
672
sim_completion (text, start, end)
673
     char *text;
674
     int start, end;
675
{
676
  char **matches;
677
 
678
  matches = (char **)NULL;
679
 
680
  /* If this word is at the start of the line, then it is a command
681
     to complete.  Otherwise it is the name of a file in the current
682
     directory. */
683
  if (start == 0)
684
    matches = completion_matches (text, command_generator);
685
 
686
  return (matches);
687
}
688
 
689
/* Generator function for command completion.  STATE lets us know whether
690
   to start from scratch; without any state (i.e. STATE == 0), then we
691
   start at the top of the list. */
692
char *
693
command_generator (text, state)
694
     char *text;
695
     int state;
696
{
697
  static int list_index, len;
698
  char *name;
699
 
700
  /* If this is a new word to complete, initialize now.  This includes
701
     saving the length of TEXT for efficiency, and initializing the index
702
     variable to 0. */
703
  if (!state)
704
    {
705
      list_index = 0;
706
      len = strlen (text);
707
    }
708
 
709
  /* Return the next name which partially matches from the command list. */
710
  while (name = sim_commands[list_index])
711
    {
712
      list_index++;
713
 
714
      if (strncmp (name, text, len) == 0)
715
        return (dupstr(name));
716
    }
717
 
718
  /* If no names matched, then return NULL. */
719
  return ((char *)NULL);
720
}
721
 
722
char *
723
dupstr (s)
724
     char *s;
725
{
726
  char *r;
727
 
728
  r = xmalloc (strlen (s) + 1);
729
  strcpy (r, s);
730
  return (r);
731
}
732
 
733
/* Repeats the last command.  */
734
char *
735
repeat_last_command ()
736
{
737
  int offset = where_history ();
738
  HIST_ENTRY *hist;
739
 
740
  if (hist = history_get (offset))
741
    {
742
      return dupstr (hist->line);
743
    }
744
  return 0;
745
}
746 16 jrydberg
 
747
#endif
748
 
749
 
750 21 cmchen
void debugmem() {
751
        int i;
752
        printf("starting to dump mem...\n");
753
        for(i=0; i<500; i++) {
754
                printf("i=%x :: ", i);
755 30 lampret
                if (mem[i].label)
756 28 lampret
                        printf("label: %s |", mem[i].label->name);
757 123 markom
                printf("%s ", insn_name (mem[i].insn->insn_index));
758 28 lampret
                if(strlen(mem[i].insn->op1) != 0) printf("%s ", mem[i].insn->op1);
759
                if(strlen(mem[i].insn->op2) != 0) printf("%s ", mem[i].insn->op2);
760
                if(strlen(mem[i].insn->op3) != 0) printf("%s ", mem[i].insn->op3);
761
                if(strlen(mem[i].insn->op4) != 0) printf("%s ", mem[i].insn->op4);
762 21 cmchen
                printf("\n");
763
        }
764
}
765 123 markom
 
766
/* Added by CZ 24/05/01 */
767
int GetServerSocket(const char* name,const char* proto,int port)
768
{
769
  struct servent *service;
770
  struct protoent *protocol;
771
  struct sockaddr_in sa;
772
  struct hostent *hp;
773
  int sockfd;
774
  char myname[256];
775
  int flags;
776
  char sTemp[256];
777
 
778
  /* First, get the protocol number of TCP */
779
  if(!(protocol = getprotobyname(proto)))
780
    {
781
      sprintf(sTemp,"Unable to load protocol \"%s\"",proto);
782
      perror(sTemp);
783
      return 0;
784
    }
785
 
786
  /* If we weren't passed a non standard port, get the port
787
     from the services directory. */
788
  if(!port)
789
    {
790
      if(service = getservbyname(name,protocol->p_name))
791
        port = ntohs(service->s_port);
792
    }
793
 
794
  /* Create the socket using the TCP protocol */
795
  if((sockfd = socket(PF_INET,SOCK_STREAM,protocol->p_proto)) < 0)
796
    {
797
      perror("Unable to create socket");
798
      return 0;
799
    }
800
 
801
  flags = 1;
802
  if(setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,(const char*)&flags,sizeof(int))
803
 < 0)
804
    {
805
      sprintf(sTemp,"Can not set SO_REUSEADDR option on socket %d",sockfd);
806
      perror(sTemp);
807
      close(sockfd);
808
      return 0;
809
    }
810
 
811
  /* The server should also be non blocking. Get the current flags. */
812
  if(fcntl(sockfd,F_GETFL,&flags) < 0)
813
    {
814
      sprintf(sTemp,"Unable to get flags for socket %d",sockfd);
815
      perror(sTemp);
816
      close(sockfd);
817
      return 0;
818
    }
819
 
820
  /* Set the nonblocking flag */
821
  if(fcntl(sockfd,F_SETFL, flags | O_NONBLOCK) < 0)
822
    {
823
      sprintf(sTemp,"Unable to set flags for socket %d to value 0x%08x",
824
              sockfd,flags | O_NONBLOCK);
825
      perror(sTemp);
826
      close(sockfd);
827
      return 0;
828
    }
829
 
830
  /* Find out what our address is */
831
  memset(&sa,0,sizeof(struct sockaddr_in));
832
  gethostname(myname,sizeof(myname));
833
  if(!(hp = gethostbyname(myname)))
834
    {
835
      perror("Unable to read hostname");
836
      close(sockfd);
837
      return 0;
838
    }
839
 
840
  /* Bind our socket to the appropriate address */
841
  sa.sin_family = hp->h_addrtype;
842
  sa.sin_port = htons(port);
843
  if(bind(sockfd,(struct sockaddr*)&sa,sizeof(struct sockaddr_in)) < 0)
844
    {
845
      sprintf(sTemp,"Unable to bind socket %d to port %d",sockfd,port);
846
      perror(sTemp);
847
      close(sockfd);
848
      return 0;
849
    }
850
  serverIP = sa.sin_addr.s_addr;
851
  flags = sizeof(struct sockaddr_in);
852
  if(getsockname(sockfd,(struct sockaddr*)&sa,&flags) < 0)
853
    {
854
      sprintf(sTemp,"Unable to get socket information for socket %d",sockfd);
855
      perror(sTemp);
856
      close(sockfd);
857
      return 0;
858
    }
859
  serverPort = ntohs(sa.sin_port);
860
 
861
  /* Set the backlog to 1 connections */
862
  if(listen(sockfd,1) < 0)
863
    {
864
      sprintf(sTemp,"Unable to set backlog on socket %d to %d",sockfd,1);
865
      perror(sTemp);
866
      close(sockfd);
867
      return 0;
868
    }
869
 
870
  return sockfd;
871
}
872
 
873
void HandleServerSocket(Boolean block)
874
{
875
  struct pollfd fds[2];
876
  int n = 0;
877
  int timeout = block ? -1 : 0;
878
  int server_index = -1;
879
  int gdb_index = -1;
880
  Boolean data_on_stdin = false;
881
  int o_serv_fd = server_fd;
882
 
883
  if(!o_serv_fd && !gdb_fd)
884
    return;
885
 
886
  if(o_serv_fd)
887
    {
888
      fds[n].fd = o_serv_fd;
889
      fds[n].events = POLLIN;
890
      fds[n++].revents = 0;
891
    }
892
  if(gdb_fd)
893
    {
894
      fds[n].fd = gdb_fd;
895
      fds[n].events = POLLIN;
896
      fds[n++].revents = 0;
897
    }
898
  if(block)
899
    {
900
      fds[n].fd = 0;
901
      fds[n].events = POLLIN;
902
      fds[n++].revents = 0;
903
    }
904
 
905
  while(!data_on_stdin)
906
    {
907
      switch(poll(fds,n,timeout))
908
        {
909
        case -1:
910
          if(errno == EINTR)
911
            continue;
912
          perror("poll");
913
          server_fd = 0;
914
          break;
915
        case 0: /* Nothing interesting going on */
916
          data_on_stdin = true; /* Can only get here if nonblocking */
917
          break;
918
        default:
919
          if(fds[0].revents && o_serv_fd)
920
            {
921
              if(fds[0].revents & POLLIN)
922
                JTAGRequest();
923
              else /* Error Occurred */
924
                {
925
                  fprintf(stderr,"Received flags 0x%08x on server. Shutting down.\n",fds[0].revents);
926
                  close(o_serv_fd);
927
                  server_fd = 0;
928
                  serverPort = 0;
929
                  serverIP = 0;
930
                }
931
            }
932
          if((fds[0].revents && (gdb_fd && !o_serv_fd) ||
933
              fds[1].revents && (server_fd && gdb_fd)))
934
            {
935
              int revents = o_serv_fd ? fds[1].revents : fds[0].revents;
936
 
937
              if(revents & POLLIN)
938
                GDBRequest();
939
              else /* Error Occurred */
940
                {
941
                  fprintf(stderr,"Received flags 0x%08x on gdb socket. Shutting down.\n",revents);
942
                  close(gdb_fd);
943
                  gdb_fd = 0;
944
                }
945
            }
946
          if(fds[2].revents || (fds[1].revents && !gdb_fd))
947
            data_on_stdin = true;
948
          break;
949
        } /* End of switch statement */
950
    } /* End of while statement */
951
}
952
 
953
void JTAGRequest()
954
{
955
  struct sockaddr_in sa;
956
  struct sockaddr* addr = (struct sockaddr*)&sa;
957
  int n = sizeof(struct sockaddr_in);
958
  int fd = accept(server_fd,addr,&n);
959
  int on_off = 0; /* Turn off Nagel's algorithm on the socket */
960
  int flags;
961
  char sTemp[256];
962
 
963
  if(fd < 0)
964
    {
965
      /* This is valid, because a connection could have started,
966
         and then terminated due to a protocol error or user
967
         initiation before the accept could take place. */
968
      if(errno != EWOULDBLOCK && errno != EAGAIN)
969
        {
970
          perror("accept");
971
          close(server_fd);
972
          server_fd = 0;
973
          serverPort = 0;
974
          serverIP = 0;
975
        }
976
      return;
977
    }
978
 
979
  if(gdb_fd)
980
    {
981
      close(fd);
982
      return;
983
    }
984
 
985
  if(fcntl(fd,F_GETFL,&flags) < 0)
986
    {
987
      sprintf(sTemp,"Unable to get flags for gdb socket %d",fd);
988
      perror(sTemp);
989
      close(fd);
990
      return;
991
    }
992
 
993
  if(fcntl(fd,F_SETFL, flags | O_NONBLOCK) < 0)
994
    {
995
      sprintf(sTemp,"Unable to set flags for gdb socket %d to value 0x%08x",
996
              fd,flags | O_NONBLOCK);
997
      perror(sTemp);
998
      close(fd);
999
      return;
1000
    }
1001
 
1002
  if(setsockopt(fd,SOL_TCP,TCP_NODELAY,&on_off,sizeof(int)) < 0)
1003
    {
1004
      sprintf(sTemp,"Unable to disable Nagel's algorithm for socket %d.\nsetsockopt",fd);
1005
      perror(sTemp);
1006
      close(fd);
1007
      return;
1008
    }
1009
 
1010
  gdb_fd = fd;
1011
}
1012
 
1013
void GDBRequest()
1014
{
1015
  /* Debug interface simulation should go here. */
1016
}

powered by: WebSVN 2.1.0

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