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 103

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
 
33 16 jrydberg
#ifdef HAVE_LIBREADLINE
34 7 jrydberg
#include <readline/readline.h>
35
#include <readline/history.h>
36 16 jrydberg
#endif /* HAVE_LIBREADLINE */
37 7 jrydberg
 
38 2 cvs
#include "arch.h"
39
#include "parse.h"
40
#include "abstract.h"
41
#include "trace.h"
42
#include "execute.h"
43 69 lampret
#include "sim-config.h"
44 103 lampret
#include "spr_defs.h"
45 2 cvs
 
46 28 lampret
#include "coff.h"
47
 
48 2 cvs
/* CVS revision number. */
49 103 lampret
const char rcsrev[] = "$Revision: 1.15 $";
50 2 cvs
 
51
/* Continuos run versus single step tracing switch. */
52
int cont_run;
53
 
54
/* History of execution */
55
int histexec[HISTEXEC_LEN];
56
 
57 7 jrydberg
char *sim_commands [] = {
58
  "q",
59
  "t",
60
  "help",
61 21 cmchen
  "de",
62 7 jrydberg
  "dm",
63
  "run",
64
  "pr",
65
  "pm",
66
  "pc",
67 18 lampret
  "reset",
68
  "break",
69 7 jrydberg
  "hist",
70
  "stats",
71
  "info",
72
  "r",
73 54 lampret
  "dv",
74 7 jrydberg
 
75
};
76
 
77 2 cvs
void debug(const char *format, ...)
78
{
79 7 jrydberg
  char *p;
80
  va_list ap;
81 2 cvs
 
82 69 lampret
  if (config.simdebug) {
83
    if ((p = malloc(1000)) == NULL)
84
      return;
85
    va_start(ap, format);
86
    (void) vsnprintf(p, 1000, format, ap);
87
    va_end(ap);
88
    printf("%s\n", p);
89
    fflush(stdout);
90
    free(p);
91
  } else {
92
#if DEBUG
93 7 jrydberg
  if ((p = malloc(1000)) == NULL)
94
    return;
95
  va_start(ap, format);
96
  (void) vsnprintf(p, 1000, format, ap);
97
  va_end(ap);
98
  printf("%s\n", p);
99
  fflush(stdout);
100
  free(p);
101 2 cvs
#endif
102 69 lampret
  }
103 7 jrydberg
  return;
104 2 cvs
}
105
 
106 30 lampret
/* Strip whitespace from the start and end of STRING.  Return a pointer
107
   into STRING. */
108
#ifndef whitespace
109
#define whitespace(a)   ((a) == '\t' ? 1 : ((a) == ' '? 1 : 0))
110
#endif
111
 
112
char *
113
stripwhite (string)
114
     char *string;
115
{
116
  register char *s, *t;
117
 
118
  for (s = string; whitespace (*s); s++)
119
    ;
120
 
121
  if (*s == 0)
122
    return (s);
123
 
124
  t = s + strlen (s) - 1;
125
  while (t > s && whitespace (*t))
126
    t--;
127
  *++t = '\0';
128
 
129
  return s;
130
}
131
 
132 7 jrydberg
void
133
ctrl_c(signum)
134
     int signum;
135 2 cvs
{
136
        cont_run = 1;
137 7 jrydberg
  signal(SIGINT, ctrl_c);
138 2 cvs
}
139
 
140 7 jrydberg
void
141
version()
142 2 cvs
{
143 7 jrydberg
        printf ("\n");
144 18 lampret
        printf ("OpenRISC 1000 (OR16+OR32) Architectural Simulator, %s\n", rcsrev);
145
        printf ("Copyright (C) 1999 Damjan Lampret, lampret@opencores.org\n");
146
        printf ("Copyright (C) 2000 Damjan Lampret, lampret@opencores.org\n");
147 21 cmchen
        printf ("                   Jimmy Chen-Min Chen, jimmy@ee.nctu.edu.tw\n");
148 18 lampret
        printf ("                   Johan Rydberg, johan.rydberg@insight.se\n");
149 7 jrydberg
        printf ("Visit http://www.opencores.org for more information about ");
150
        printf ("OpenRISC 1000 and\nother open source cores.\n\n");
151
        printf ("This software comes with ABSOLUTELY NO WARRANTY; for ");
152
        printf ("details see COPYING.\nThis is free software, and you ");
153
        printf ("are welcome to redistribute it under certain\nconditions; ");
154
        printf ("for details see COPYING.\n");
155 2 cvs
}
156
 
157 7 jrydberg
void
158
help()
159 2 cvs
{
160
        printf("q                        - quit simulator\n");
161
        printf("r                        - display all registers\n");
162
        printf("t                        - execute next instruction\n");
163
        printf("run <cycles> [<hush>]    - execute <cycles> instructions, no reg dump if hush\n");
164
        printf("pr <r> <value>           - patch register <r> with <value>\n");
165
        printf("dm <fromaddr> [<toaddr>] - display memory from <fromaddr> to <toaddr>\n");
166 21 cmchen
        printf("de                       - debug insn memory\n");
167 2 cvs
        printf("pm <addr> <value>        - patch memory location <addr> with <value>\n");
168
        printf("pc <value>               - patch PC register with <value>\n");
169 18 lampret
        printf("break <addr>             - toggle breakpoint at address <addr>\n");
170
        printf("reset                    - simulator reset\n");
171 2 cvs
        printf("hist                     - execution history\n");
172 6 lampret
        printf("stats <num|clear>        - execution statistics num or clear it.\n");
173
        printf("info                     - configuration info (caches etc.)\n");
174 54 lampret
        printf("dv <fromaddr> [<toaddr>] [<modname>] - dumps memory as verilog (use redirect)\n");
175 86 lampret
        printf("dh <fromaddr> [<toaddr>] - dumps memory as hex code (use redirect)\n");
176 2 cvs
        printf("<cmd> > <filename>       - redirect simulator stdout to <filename> (and not emulated printf)\n");
177 69 lampret
        printf("debug                    - toggles simulator debug mode\n");
178 2 cvs
        printf("help                     - available commands (this list)\n");
179
}
180
 
181 21 cmchen
void debugmem();
182
 
183 7 jrydberg
main(argc, argv)
184
     int argc;
185
     char *argv[];
186 2 cvs
{
187 7 jrydberg
        char *linestr;
188 16 jrydberg
        char item1[500], b2[500];
189 2 cvs
        char *redirstr;
190
        int hush;
191 69 lampret
        unsigned long endaddr;
192 2 cvs
 
193 103 lampret
        srand(getpid());
194
        init_defconfig();
195
        if ((argc < 2) || parse_args(argc, argv)) {
196
                printf("Usage: %s [options] <filename>\n", argv[0]);
197
                printf("Options:\n");
198
                printf(" -v: version and copyright note\n");
199
                printf(" -i: enable interactive command prompt\n");
200
                printf(" -bpb: disable branch prediction buffer analysis\n");
201
                printf(" -btic: disable branch prediction target insn cache analysis\n");
202
                printf(" -hazards: disable dependency hazards analysis\n");
203
                printf(" -history: disable instruction stream history analysis\n");
204
                printf(" -superscalar: disable superscalar analysis\n");
205
                printf(" -fast: disable BPB, BTIC, SLP, dependency hazards, history analysis etc.\n");
206
                printf(" -upr <n>: set UPR to n\n");
207
                printf(" -ver <n>: set VR[VER] to n\n");
208
                printf(" -rev <n>: set VR[REV] to n\n");
209
                exit(-1);
210
        }
211 7 jrydberg
 
212 16 jrydberg
#ifdef HAVE_LIBREADLINE
213 7 jrydberg
  initialize_readline ();       /* Bind our completer. */
214 16 jrydberg
#endif  
215
 
216 103 lampret
        print_config();
217 2 cvs
        signal(SIGINT, ctrl_c);
218
        initstats();
219 69 lampret
        memset(mem, 0, sizeof(mem));
220 103 lampret
        endaddr = loadcode(argv[argc-1], MEMORY_START, 0);
221 69 lampret
        if (endaddr == -1) {
222
                printf("Problems loading boot code.\n");
223
                exit(1);
224
        }
225 30 lampret
        uart_reset();
226 92 lampret
        tick_reset();
227 103 lampret
        pm_reset();
228
        pic_reset();
229 2 cvs
        reset();
230 69 lampret
        set_reg32("r3", endaddr);
231 7 jrydberg
 
232 103 lampret
        while(1) {
233
                if (config.iprompt) {
234 16 jrydberg
#ifdef HAVE_LIBREADLINE
235 103 lampret
                        linestr = readline("(sim) ");
236 16 jrydberg
#else
237 103 lampret
                        printf ("(sim) ");
238
                        linestr = fgets(b2, sizeof b2, stdin);
239 16 jrydberg
#endif
240 103 lampret
                } else
241
                        strcpy(linestr = b2, "run 100000000 hush");
242 7 jrydberg
 
243 103 lampret
                if (!linestr)
244
                        break;
245
                linestr = stripwhite (linestr);
246 7 jrydberg
 
247 16 jrydberg
#ifdef HAVE_LIBREADLINE
248 103 lampret
                if (strlen(linestr) == 0) {
249
                        char *l = repeat_last_command ();
250 7 jrydberg
 
251 103 lampret
                        if (l) {
252
                                free (linestr);
253
                                linestr = l;
254
                        }
255
                }
256 7 jrydberg
 
257 103 lampret
                if (*linestr) {
258
                        add_history (linestr);
259
                }
260 16 jrydberg
#endif /* HAVE_LIBREADLINE */
261 2 cvs
 
262 103 lampret
                if (redirstr = strstr(linestr, ">")) {
263
                        *redirstr = '\0';
264
                        strtoken(&redirstr[1], item1, 1);
265
                        freopen(item1, "w+", stdout);
266
                }
267 2 cvs
 
268
                strtoken(linestr, item1, 1);
269
                if (strcmp(item1, "q") == 0)     /* quit */
270
                        exit(0);
271
                else
272
                if (strcmp(item1, "help") == 0)  /* help */
273
                        help();
274
                else
275
                if (strcmp(item1, "t") == 0) {   /* trace */
276
                        cont_run = 1;
277
                } else
278
                if (strcmp(item1, "dm") == 0) {  /* dump memory */
279
                        char item2[20];
280
                        char item3[20];
281
                        static int from = 0, to = 0;
282
 
283
                        strtoken(linestr, item2, 2);
284
                        strtoken(linestr, item3, 3);
285
 
286
                        if (strlen(item2)) {
287
                                if (item2[0] == '_')
288
                                        from = eval_label(item2);
289
                                else
290
                                        from = strtoul(item2, NULL, 0);
291
                                to = from + 0x40;
292
                        }
293
                        if (strlen(item3))
294
                                to = strtoul(item3, NULL, 0);
295
                        dumpmemory(from, to);
296 54 lampret
                        printf("\n");
297 2 cvs
                } else
298 54 lampret
                if (strcmp(item1, "dv") == 0) {/* dump memory as verilog*/
299
                        char item2[20];
300
                        char item3[20];
301
                        char item4[20];
302
                        static int from = 0, to = 0;
303
 
304
                        strtoken(linestr, item2, 2);
305
                        strtoken(linestr, item3, 3);
306
                        strtoken(linestr, item4, 4);
307
 
308
                        if (strlen(item2)) {
309
                                if (item2[0] == '_')
310
                                        from = eval_label(item2);
311
                                else
312
                                        from = strtoul(item2, NULL, 0);
313
                                to = from + 0x40;
314
                        }
315
                        if (strlen(item3))
316
                                to = strtoul(item3, NULL, 0);
317
                        if (!strlen(item4))
318
                                strcpy(item4, "or1k_mem");
319
                        dumpverilog(item4, from, to);
320
                        printf("\n");
321
                } else
322 86 lampret
                if (strcmp(item1, "dh") == 0) {/* dump memory as hex*/
323
                        char item2[20];
324
                        char item3[20];
325
                        static int from = 0, to = 0;
326
 
327
                        strtoken(linestr, item2, 2);
328
                        strtoken(linestr, item3, 3);
329
 
330
                        if (strlen(item2)) {
331
                                if (item2[0] == '_')
332
                                        from = eval_label(item2);
333
                                else
334
                                        from = strtoul(item2, NULL, 0);
335
                                to = from + 0x40;
336
                        }
337
                        if (strlen(item3))
338
                                to = strtoul(item3, NULL, 0);
339
                        dumphex(from, to);
340
                        printf("\n");
341
                } else
342 2 cvs
                if (strcmp(item1, "pm") == 0) {  /* patch memory */
343
                        char item2[20];
344
                        char item3[20];
345
                        static int addr = 0;
346
 
347
                        strtoken(linestr, item2, 2);
348
                        strtoken(linestr, item3, 3);
349
                        if (strlen(item2))
350
                                if (item2[0] == '_')
351
                                        addr = eval_label(item2);
352
                                else
353
                                        addr = strtoul(item2, NULL, 0);
354
                        set_mem32(addr, strtoul(item3, NULL, 0));
355
                } else
356
                if (strcmp(item1, "pr") == 0) {  /* patch regs */
357
                        char item2[20];
358
                        char item3[20];
359
 
360
                        strtoken(linestr, item2, 2);
361
                        strtoken(linestr, item3, 3);
362
                        set_reg32(item2, strtoul(item3, NULL, 0));
363
                } else
364
                if (strcmp(item1, "pc") == 0) {  /* patch PC */
365
                        char item2[20];
366
 
367
                        strtoken(linestr, item2, 2);
368 86 lampret
                        pcnext = strtoul(item2, NULL, 0);
369 2 cvs
                } else
370 7 jrydberg
                if (strcmp(item1, "break") == 0) {       /* set/clear breakpoint */
371 2 cvs
                        char item2[20];
372
 
373
                        strtoken(linestr, item2, 2);
374
                        set_insnbrkpoint(strtoul(item2, NULL, 0));
375
                } else
376
                if (strcmp(item1, "r") == 0) {   /* dump regs */
377
                        dumpreg();
378
                } else
379 21 cmchen
                if (strcmp(item1, "de") == 0) {  /* reset simulator */
380
                        debugmem();
381
                } else
382 18 lampret
                if (strcmp(item1, "reset") == 0) {       /* reset simulator */
383 30 lampret
                        uart_reset();
384 92 lampret
                        tick_reset();
385 103 lampret
                        pm_reset();
386
                        pic_reset();
387 18 lampret
                        reset();
388
                } else
389 69 lampret
                if (strcmp(item1, "debug") == 0) {       /* debug mode */
390
                        config.simdebug ^= 1;
391
                } else
392 2 cvs
                if (strcmp(item1, "hist") == 0) {        /* dump history */
393
                        int i;
394
                        for(i = HISTEXEC_LEN; i; i--)
395
                                dumpmemory(histexec[i - 1], histexec[i - 1] + 4);
396 46 lampret
                        printf("\n");
397 2 cvs
                } else
398
                if (strcmp(item1, "run") == 0) { /* run */
399
                        char item2[20];
400
                        char item3[20];
401
 
402
                        strtoken(linestr, item2, 2);
403
                        strtoken(linestr, item3, 3);
404
                        if (strcmp(item3, "hush") == 0)
405
                                hush = 1;
406
                        else
407
                                hush = 0;
408
                        cont_run = strtoul(item2, NULL, 0);
409
                } else
410
                if (strcmp(item1, "stats") == 0) { /* stats */
411 6 lampret
                        char item2[20];
412
                        int i = 0;
413
 
414
                        strtoken(linestr, item2, 2);
415
                        if (strcmp(item2, "clear") == 0) {
416
                                initstats();
417
                                printf("Cleared.\n");
418
                        } else {
419
                                i = strtoul(item2, NULL, 0);
420
                                printstats(i);
421
                        }
422
                } else
423
                if (strcmp(item1, "info") == 0) { /* configuration info */
424 78 lampret
                        itlb_status(-1);
425
                        dtlb_status(-1);
426 6 lampret
                        bpb_info();
427
                        btic_info();
428
                        ic_info();
429
                        dc_info();
430 30 lampret
                        uart_status();
431
                        sprs_status();
432 7 jrydberg
                } else {
433 103 lampret
                        printf("%s: Unknown command.\n", linestr);
434
                }
435
 
436 2 cvs
                while(cont_run) {
437
                        cont_run--;
438 103 lampret
                        if (!getsprbits(SPR_PMR, SPR_PMR_DME | SPR_PMR_SME)) {
439
                                fetch();
440
                                decode_execute(&iqueue[0]);
441
                                analysis();
442
                                if (!hush)
443
                                        dumpreg();
444
                                pic_clock();
445
                                dc_clock();
446
                                ic_clock();
447
                        }
448
                        if (!getsprbits(SPR_PMR, SPR_PMR_SME))
449
                                tick_clock();
450
                        pm_clock();
451 30 lampret
                        uart_clock();
452 2 cvs
                }
453
 
454
                hush = 0;
455
                fflush(stdout);
456
                freopen("/dev/fd/0", "w+", stdout);
457
 
458 103 lampret
                if (!config.iprompt)    /* non-interactive quit */
459
                        exit(0);
460
 
461 16 jrydberg
#ifdef HAVE_LIBREADLINE
462 103 lampret
                if (linestr)
463
                        free (linestr);
464 16 jrydberg
#endif
465 7 jrydberg
 
466 2 cvs
        }
467
        exit(0);
468
}
469 7 jrydberg
 
470 16 jrydberg
#ifdef HAVE_LIBREADLINE
471 7 jrydberg
char *command_generator ();
472
char **sim_completion ();
473
 
474
/* Tell the GNU readline library how to complete.  We want to try to complete
475
   on command names if this is the first word in the line, or on filenames
476
   if not. */
477
void initialize_readline ()
478
{
479
  /* Allow conditional parsing of the ~/.inputrc file. */
480
  rl_readline_name = "or1ksim";
481
 
482
  /* Tell the completer that we want a crack first. */
483
  rl_attempted_completion_function = (CPPFunction *)sim_completion;
484
}
485
 
486
/* Attempt to complete on the contents of TEXT.  START and END bound the
487
   region of rl_line_buffer that contains the word to complete.  TEXT is
488
   the word to complete.  We can use the entire contents of rl_line_buffer
489
   in case we want to do some simple parsing.  Return the array of matches,
490
   or NULL if there aren't any. */
491
char **
492
sim_completion (text, start, end)
493
     char *text;
494
     int start, end;
495
{
496
  char **matches;
497
 
498
  matches = (char **)NULL;
499
 
500
  /* If this word is at the start of the line, then it is a command
501
     to complete.  Otherwise it is the name of a file in the current
502
     directory. */
503
  if (start == 0)
504
    matches = completion_matches (text, command_generator);
505
 
506
  return (matches);
507
}
508
 
509
/* Generator function for command completion.  STATE lets us know whether
510
   to start from scratch; without any state (i.e. STATE == 0), then we
511
   start at the top of the list. */
512
char *
513
command_generator (text, state)
514
     char *text;
515
     int state;
516
{
517
  static int list_index, len;
518
  char *name;
519
 
520
  /* If this is a new word to complete, initialize now.  This includes
521
     saving the length of TEXT for efficiency, and initializing the index
522
     variable to 0. */
523
  if (!state)
524
    {
525
      list_index = 0;
526
      len = strlen (text);
527
    }
528
 
529
  /* Return the next name which partially matches from the command list. */
530
  while (name = sim_commands[list_index])
531
    {
532
      list_index++;
533
 
534
      if (strncmp (name, text, len) == 0)
535
        return (dupstr(name));
536
    }
537
 
538
  /* If no names matched, then return NULL. */
539
  return ((char *)NULL);
540
}
541
 
542
char *
543
dupstr (s)
544
     char *s;
545
{
546
  char *r;
547
 
548
  r = xmalloc (strlen (s) + 1);
549
  strcpy (r, s);
550
  return (r);
551
}
552
 
553
/* Repeats the last command.  */
554
char *
555
repeat_last_command ()
556
{
557
  int offset = where_history ();
558
  HIST_ENTRY *hist;
559
 
560
  if (hist = history_get (offset))
561
    {
562
      return dupstr (hist->line);
563
    }
564
  return 0;
565
}
566 16 jrydberg
 
567
#endif
568
 
569
 
570 21 cmchen
void debugmem() {
571
        int i;
572
        printf("starting to dump mem...\n");
573
        for(i=0; i<500; i++) {
574
                printf("i=%x :: ", i);
575 30 lampret
                if (mem[i].label)
576 28 lampret
                        printf("label: %s |", mem[i].label->name);
577
                printf("%s ", mem[i].insn->insn);
578
                if(strlen(mem[i].insn->op1) != 0) printf("%s ", mem[i].insn->op1);
579
                if(strlen(mem[i].insn->op2) != 0) printf("%s ", mem[i].insn->op2);
580
                if(strlen(mem[i].insn->op3) != 0) printf("%s ", mem[i].insn->op3);
581
                if(strlen(mem[i].insn->op4) != 0) printf("%s ", mem[i].insn->op4);
582 21 cmchen
                printf("\n");
583
        }
584
}

powered by: WebSVN 2.1.0

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