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

Subversion Repositories or1k

[/] [or1k/] [tags/] [tn_m001/] [or1ksim/] [toplevel.c] - Blame information for rev 16

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

powered by: WebSVN 2.1.0

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