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

Subversion Repositories or1k

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

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

powered by: WebSVN 2.1.0

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