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

Subversion Repositories or1k

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

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

powered by: WebSVN 2.1.0

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