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 18

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

powered by: WebSVN 2.1.0

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