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 28

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

powered by: WebSVN 2.1.0

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