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 46

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

powered by: WebSVN 2.1.0

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