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

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [or1ksim/] [sim-cmd.c] - Blame information for rev 146

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 19 jeremybenn
/* sim-cmd.c -- Simulator command parsing
2
 
3
   Copyright (C) 1999 Damjan Lampret, lampret@opencores.org
4
   Copyright (C) 2005 György `nog' Jeney, nog@sdf.lonestar.org
5
   Copyright (C) 2008 Embecosm Limited
6
 
7
   Contributor Jeremy Bennett <jeremy.bennett@embecosm.com>
8
 
9
   This file is part of Or1ksim, the OpenRISC 1000 Architectural Simulator.
10
 
11
   This program is free software; you can redistribute it and/or modify it
12
   under the terms of the GNU General Public License as published by the Free
13
   Software Foundation; either version 3 of the License, or (at your option)
14
   any later version.
15
 
16
   This program is distributed in the hope that it will be useful, but WITHOUT
17
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
19
   more details.
20
 
21
   You should have received a copy of the GNU General Public License along
22
   with this program.  If not, see <http://www.gnu.org/licenses/>.  */
23
 
24
/* This program is commented throughout in a fashion suitable for processing
25
   with Doxygen. */
26
 
27
 
28
/* Autoconf and/or portability configuration */
29
#include "config.h"
30
 
31
/* System includes */
32
#include <stdlib.h>
33
#include <unistd.h>
34
#include <stdio.h>
35
 
36
#ifdef HAVE_LIBREADLINE
37
#include <readline/readline.h>
38
#include <readline/history.h>
39
#endif /* HAVE_LIBREADLINE */
40
 
41
/* Package includes */
42
#include "sim-cmd.h"
43
#include "sim-config.h"
44
#include "execute.h"
45
#include "labels.h"
46
#include "gdbcomm.h"
47
#include "sched.h"
48
#include "toplevel-support.h"
49
#include "dumpverilog.h"
50
#include "profiler.h"
51
#include "mprofiler.h"
52
#include "trace.h"
53
#include "debug-unit.h"
54
#include "stats.h"
55
#include "sprs.h"
56
#include "dcache-model.h"
57
#include "branch-predict.h"
58
#include "debug.h"
59
#include "cuc.h"
60
#include "rsp-server.h"
61
 
62
 
63
/* The number of instructions to execute before droping into interactive mode */
64
static long long to_insn_num;
65
 
66
struct sim_stat
67
{
68
  void (*stat_func) (void *dat);
69
  void *dat;
70
  struct sim_stat *next;
71
};
72
 
73
static struct sim_stat *sim_stats = NULL;
74
 
75
/* Registers a status printing callback */
76
void
77
reg_sim_stat (void (*stat_func) (void *dat), void *dat)
78
{
79
  struct sim_stat *new = malloc (sizeof (struct sim_stat));
80
 
81
  if (!new)
82
    {
83
      fprintf (stderr, "reg_sim_stat: Out-of-memory\n");
84
      exit (1);
85
    }
86
 
87
  new->stat_func = stat_func;
88
  new->dat = dat;
89
  new->next = sim_stats;
90
  sim_stats = new;
91
}
92
 
93
/* Scheduler job that drops us back into interactive mode after the next
94
 * instruction has executed */
95
void
96
reenter_int (void *dat)
97
{
98
  if (!runtime.sim.hush)
99
    dumpreg ();
100
  handle_sim_command ();
101
}
102
 
103
static int
104
sim_cmd_quit (int argc, char **argv)    /* quit */
105
{
106
  PRINTF ("\n");
107
  sim_done ();
108
  return 0;
109
}
110
 
111
static int
112
sim_cmd_help (int argc, char **argv)    /* help */
113
{
114
  PRINTF ("q                              - quit simulator\n");
115
  PRINTF ("r                              - display all registers\n");
116
  PRINTF ("t                              - execute next instruction\n");
117
  PRINTF
118
    ("run <instructions> [<hush>]    - execute <instruction> instructions, no reg\n");
119
  PRINTF ("                                 dump if hush\n");
120
  PRINTF
121
    ("pr <r> <value>                 - patch register <r> with <value>\n");
122
  PRINTF
123
    ("dm <fromaddr> [<toaddr>]       - display memory from <fromaddr> to <toaddr>\n");
124
  PRINTF ("de <fromaddr> [<toaddr>]       - debug insn memory\n");
125
  PRINTF
126
    ("pm <addr> <value>              - patch memory location <addr> with <value>\n");
127
  PRINTF
128
    ("pc <value>                     - patch PC register with <value>\n");
129
  PRINTF ("cm <fromaddr> <toaddr> <size>  - copy memory\n");
130
  PRINTF
131
    ("break <addr>                   - toggle breakpoint at address <addr>\n");
132
  PRINTF ("breaks                         - print all set breakpoints\n");
133
  PRINTF ("reset                          - simulator reset\n");
134
  PRINTF ("hist                           - execution history\n");
135
  PRINTF
136
    ("stall                          - stalls the processor and gives control to\n");
137
  PRINTF ("                                 the debugger\n");
138
  PRINTF ("unstall                        - unstalls the processor\n");
139
  PRINTF
140
    ("stats <num|clear>              - execution statistics num or clear it.\n");
141
  PRINTF
142
    ("info                           - configuration info (caches etc.)\n");
143
  PRINTF
144
    ("dv <fromaddr> [<toaddr>] [<m>] - dumps memory as verilog module <m>\n");
145
  PRINTF ("                                 (use redirect to capture)\n");
146
  PRINTF
147
    ("dh <fromaddr> [<toaddr>]       - dumps memory as hex code (use redirect)\n");
148
  PRINTF ("setdbch                        - toggles debug channels on/off\n");
149
  PRINTF
150
    ("set <section> <item> = <param> - set configuration.  See sim.cfg for more\n");
151
  PRINTF ("                                 information.\n");
152
  PRINTF ("debug                          - toggles simulator debug mode\n");
153
  PRINTF
154
    ("cuc                            - enters Custom Unit Compiler command prompt\n");
155
  PRINTF
156
    ("help                           - available commands (this list)\n");
157
  PRINTF
158
    ("<cmd> > <filename>             - redirect simulator stdout to <filename>\n");
159
  PRINTF ("                                 (and not emulated PRINTF)\n\n");
160
  (void)main_mprofiler (0, NULL, 1);
161
  PRINTF ("\n");
162
  (void)main_profiler (0, NULL, 1);
163
  return 0;
164
}
165
 
166
static int
167
sim_cmd_trace (int argc, char **argv)   /* trace */
168
{
169
  runtime.sim.hush = 0;
170
  sched_next_insn (reenter_int, NULL);
171
  return 1;
172
}
173
 
174
static int
175
sim_cmd_dm (int argc, char **argv)      /* dump memory */
176
{
177
  static oraddr_t from = 0, to = 0;
178
 
179
  if (argc >= 2)
180
    {
181
      if (argv[1][0] == '_')
182
        from = eval_label (argv[1]);
183
      else
184
        from = strtoul (argv[1], NULL, 0);
185
      to = from + 0x40;
186
    }
187
  if (argc >= 3)
188
    to = strtoul (argv[2], NULL, 0);
189
  dump_memory (from, to);
190
  PRINTF ("\n");
191
  return 0;
192
}
193
 
194
static int
195
sim_cmd_dv (int argc, char **argv)      /* dump memory as verilog */
196
{
197
  static oraddr_t from = 0, to = 0;
198
 
199
  if (argc >= 2)
200
    {
201
      if (argv[1][0] == '_')
202
        from = eval_label (argv[1]);
203
      else
204
        from = strtoul (argv[1], NULL, 0);
205
      to = from + 0x40;
206
    }
207
  if (argc >= 3)
208
    to = strtoul (argv[2], NULL, 0);
209
 
210
  if (argc < 4)
211
    dumpverilog ("or1k_mem", from, to);
212
  else
213
    dumpverilog (argv[3], from, to);
214
 
215
  PRINTF ("\n");
216
  return 0;
217
}
218
 
219
static int
220
sim_cmd_dh (int argc, char **argv)      /* dump memory as hex */
221
{
222
  static oraddr_t from = 0, to = 0;
223
 
224
  if (argc >= 2)
225
    {
226
      if (argv[1][0] == '_')
227
        from = eval_label (argv[1]);
228
      else
229
        from = strtoul (argv[1], NULL, 0);
230
      to = from + 0x40;
231
    }
232
  if (argc >= 3)
233
    to = strtoul (argv[2], NULL, 0);
234
 
235
  dumphex (from, to);
236
  PRINTF ("\n");
237
  return 0;
238
}
239
 
240
static int
241
sim_cmd_pm (int argc, char **argv)      /* patch memory */
242
{
243
  static oraddr_t addr = 0;
244
  int breakpoint = 0;
245
 
246
  if (argc != 3)
247
    {
248
      PRINTF ("pm <address> <value>\n");
249
      return 0;
250
    }
251
 
252
  if (argc >= 2)
253
    {
254
      if (argv[1][0] == '_')
255
        addr = eval_label (argv[1]);
256
      else
257
        addr = strtoul (argv[1], NULL, 0);
258
    }
259
  set_mem32 (addr, strtoul (argv[2], NULL, 0), &breakpoint);
260
  return 0;
261
}
262
 
263
static int
264
sim_cmd_cm (int argc, char **argv)      /* copy memory 2004-01-20 hpanther */
265
{
266
  static oraddr_t from = 0, to = 0;
267
  static unsigned int size = 0;
268
  int i;
269
 
270
  if (argc >= 2)
271
    {
272
      if (argv[1][0] == '_')
273
        from = eval_label (argv[1]);
274
      else
275
        from = strtoul (argv[1], NULL, 0);
276
    }
277
 
278
  if (argc >= 3)
279
    {
280
      if (argv[2][0] == '_')
281
        to = eval_label (argv[2]);
282
      else
283
        to = strtoul (argv[2], NULL, 0);
284
    }
285
 
286
  if (argc >= 4)
287
    {
288
      if (argv[3][0] == '_')
289
        size = eval_label (argv[3]);
290
      else
291
        size = strtoul (argv[3], NULL, 0);
292
    }
293
 
294
  for (i = 0; i < size; i += 4)
295
    set_direct32 (to + i, eval_direct32 (from + i, 0, 0), 0, 0);
296
  return 0;
297
}
298
 
299
static int
300
sim_cmd_pr (int argc, char **argv)      /* patch regs */
301
{
302
  if (argc != 3)
303
    {
304
      PRINTF ("pr <register> <value>\n");
305
      return 0;
306
    }
307
  setsim_reg (strtoul (argv[1], NULL, 0), strtoul (argv[2], NULL, 0));
308
#if DYNAMIC_EXECUTION
309
  PRINTF
310
    ("WARNING: Patching registers may not work with the dynamic execution model\n");
311
#endif
312
  return 0;
313
}
314
 
315
static int
316
sim_cmd_pc (int argc, char **argv)      /* patch PC */
317
{
318
#if DYNAMIC_EXECUTION
319
  PRINTF ("Patching the pc in the dynamic execution model doesn't work\n");
320
#else
321
  if (argc != 2)
322
    {
323
      PRINTF ("pc <value>\n");
324
      return 0;
325
    }
326
 
327
  cpu_state.pc = strtoul (argv[1], NULL, 0);
328
  pcnext = cpu_state.pc + 4;
329
#endif
330
  return 0;
331
}
332
 
333
static int
334
sim_cmd_breaks (int argc, char **argv)  /* print breakpoints */
335
{
336
  print_breakpoints ();
337
  return 0;
338
}
339
 
340
static int
341
sim_cmd_break (int argc, char **argv)   /* set/clear breakpoint */
342
{
343
#if DYNAMIC_EXECUTION
344
  PRINTF
345
    ("Setting simulator breakpoints is not support with the recompiler\n");
346
  return 0;
347
#else
348
  char *p;
349
  oraddr_t addr;
350
  struct label_entry *l;
351
 
352
  if (argc != 2)
353
    {
354
      PRINTF ("break <label or address>\n");
355
      return 0;
356
    }
357
 
358
  addr = strtoul (argv[1], &p, 0);
359
  if (*p)
360
    {
361
      l = find_label (argv[1]);
362
      if (l)
363
        set_insnbrkpoint (l->addr);
364
      else
365
        PRINTF ("Label `%s' does not exist\n", argv[1]);
366
    }
367
  else
368
    set_insnbrkpoint (addr);
369
  return 0;
370
#endif
371
}
372
 
373
static int
374
sim_cmd_r (int argc, char **argv)       /* dump regs */
375
{
376
  dumpreg ();
377
  return 0;
378
}
379
 
380
static int
381
sim_cmd_de (int argc, char **argv)      /* disassemble */
382
{
383
  static oraddr_t from = 0, to = 0;
384
 
385
  if (argc >= 2)
386
    {
387
      if (argv[1][0] == '_')
388
        from = eval_label (argv[1]);
389
      else
390
        from = strtoul (argv[1], NULL, 0);
391
      to = from + 0x40;
392
    }
393
 
394
  if (argc >= 3)
395
    to = strtoul (argv[2], NULL, 0);
396
 
397
  disassemble_memory (from, to, 1);
398
  PRINTF ("\n");
399
  return 0;
400
}
401
 
402
static int
403
sim_cmd_reset (int argc, char **argv)   /* reset simulator */
404
{
405
  sim_reset ();
406
  return 0;
407
}
408
 
409
static int
410
sim_cmd_hist (int argc, char **argv)    /* dump history */
411
{
412
  int i;
413
  struct hist_exec *cur;
414
  if (!config.sim.history)
415
    {
416
      PRINTF ("Simulation history disabled.\n");
417
      return 0;
418
    }
419
  for (i = HISTEXEC_LEN, cur = hist_exec_tail->next; i; i--, cur = cur->next)
420
    disassemble_memory (cur->addr, cur->addr + 4, 1);
421
  PRINTF ("\n");
422
  return 0;
423
}
424
 
425
/* Called when it is suspisous that runtime.sim.instructions has reached
426
 * to_insn_num */
427
void
428
check_insn_exec (void *dat)
429
{
430
  if (runtime.cpu.instructions < to_insn_num)
431
    {
432
      /* Instruction count has not yet been reached, reschedule */
433
      long long int delta = to_insn_num - runtime.cpu.instructions;
434
      SCHED_ADD (check_insn_exec, NULL,
435
                 (delta > INT32_MAX) ? INT32_MAX : delta);
436
      return;
437
    }
438
  handle_sim_command ();
439
}
440
 
441
void
442
print_insn_exec (void *dat)
443
{
444
  dumpreg ();
445
  if (runtime.cpu.instructions < to_insn_num)
446
    {
447
      /* Instruction count has not yet been reached, reschedule */
448
      sched_next_insn (print_insn_exec, NULL);
449
      return;
450
    }
451
  handle_sim_command ();
452
}
453
 
454
static int
455
sim_cmd_run (int argc, char **argv)     /* run */
456
{
457
  runtime.sim.hush = 0;
458
  if (argc >= 3)
459
    {
460
      if (!strcmp (argv[2], "hush"))
461
        runtime.sim.hush = 1;
462
    }
463
 
464
  if (argc >= 2)
465
    {
466
      if ((to_insn_num = strtoll (argv[1], NULL, 0)) != -1)
467
        {
468
          if (runtime.sim.hush)
469
            {
470
              /* Schedule a job to run in to_insn_num cycles time since an instruction
471
               * may execute in only 1 cycle.  check_insn_exec will check if the right
472
               * number of instructions have been executed.  If not it will
473
               * reschedule.  */
474
              SCHED_ADD (check_insn_exec, NULL,
475
                         (to_insn_num > INT32_MAX) ? INT32_MAX : to_insn_num);
476
            }
477
          else
478
            {
479
              /* The user wants to see the execution dumps.  Schedule a task to show
480
               * it to him after each cycle */
481
              sched_next_insn (print_insn_exec, NULL);
482
            }
483
          to_insn_num += runtime.cpu.instructions;
484
        }
485
      else
486
        {
487
          if (!runtime.sim.hush)
488
            sched_next_insn (print_insn_exec, NULL);
489
        }
490
    }
491
  else
492
    /* Run 0 instructions */
493
    return 0;
494
 
495
  return 1;
496
}
497
 
498
static int
499
sim_cmd_stall (int argc, char **argv)   /* Added by CZ 210801 */
500
{
501
#if DYNAMIC_EXECUTION
502
  PRINTF ("Can't stall the cpu with the dynamic recompiler\n");
503
  return 0;
504
#else
505
  set_stall_state (1);
506
  runtime.sim.iprompt = 0;
507
  runtime.sim.hush = 1;
508
  return 1;
509
#endif
510
}
511
 
512
static int
513
sim_cmd_unstall (int argc, char **argv) /* Added by CZ 210801 */
514
{
515
#if DYNAMIC_EXECUTION
516
  PRINTF ("Can't unstall the cpu with the dynamic recompiler\n");
517
  return 0;
518
#else
519
  set_stall_state (0);
520
  return 0;
521
#endif
522
}
523
 
524
static int
525
sim_cmd_stats (int argc, char **argv)   /* stats */
526
{
527
  if (argc != 2)
528
    {
529
      PRINTF ("stats <stat no. or `clear'>\n");
530
      return 0;
531
    }
532
 
533
  if (strcmp (argv[1], "clear") == 0)
534
    {
535
      initstats ();
536
      PRINTF ("Cleared.\n");
537
    }
538
  else
539
    {
540
      printstats (strtoul (argv[1], NULL, 0));
541
    }
542
  return 0;
543
}
544
 
545
static int
546
sim_cmd_info (int argc, char **argv)    /* configuration info */
547
{
548
  struct sim_stat *cur_stat = sim_stats;
549
 
550
  /* Display info about various modules */
551
  sprs_status ();
552
  PRINTF ("\n");
553
  memory_table_status ();
554
  if (config.dc.enabled)
555
    dc_info ();
556
 
557
  if (config.bpb.enabled)
558
    bpb_info ();
559
  if (config.bpb.btic)
560
    btic_info ();
561
 
562
  while (cur_stat)
563
    {
564
      cur_stat->stat_func (cur_stat->dat);
565
      cur_stat = cur_stat->next;
566
    }
567
 
568
  return 0;
569
}
570
 
571
static int
572
sim_cmd_setdbch (int argc, char **argv) /* Toggle debug channel on/off */
573
{
574
  if (argc != 2)
575
    {
576
      PRINTF ("setdbch <channel>\n");
577
      return 0;
578
    }
579
  parse_dbchs (argv[1]);
580
  return 0;
581
}
582
 
583
static int
584
sim_cmd_debug (int argc, char **argv)   /* debug mode */
585
{
586
  config.sim.debug ^= 1;
587
  return 0;
588
}
589
 
590
static int
591
sim_cmd_profile (int argc, char **argv) /* run profiler utility */
592
{
593
  return  main_profiler (argc, argv, 0);
594
}
595
 
596
static int
597
sim_cmd_mprofile (int argc, char **argv)        /* run mprofiler utility */
598
{
599
  return  main_mprofiler (argc, argv, 0);
600
}
601
 
602
static int
603
sim_cmd_cuc (int argc, char **argv)     /* run Custom Unit Compiler */
604
{
605
  main_cuc (runtime.sim.filename);
606
  return 0;
607
}
608
 
609
static int
610
sim_cmd_set (int argc, char **argv)     /* configuration info */
611
{
612
  set_config_command (argc, argv);
613
  return 0;
614
}
615
 
616
static char *
617
strip_space (char *str)
618
{
619
  while (isblank (*str) && *str)
620
    str++;
621
  return str;
622
}
623
 
624
struct sim_command
625
{
626
  const char *name;
627
  int (*cmd_handle) (int argc, char **argv);
628
};
629
 
630
static const struct sim_command sim_commands[] = {
631
  {"q", sim_cmd_quit},
632
  {"help", sim_cmd_help},
633
  {"t", sim_cmd_trace},
634
  {"dm", sim_cmd_dm},
635
  {"dv", sim_cmd_dv},
636
  {"dh", sim_cmd_dh},
637
  {"pm", sim_cmd_pm},
638
  {"cm", sim_cmd_cm},
639
  {"pr", sim_cmd_pr},
640
  {"pc", sim_cmd_pc},
641
  {"breaks", sim_cmd_breaks},
642
  {"break", sim_cmd_break},
643
  {"r", sim_cmd_r},
644
  {"de", sim_cmd_de},
645
  {"reset", sim_cmd_reset},
646
  {"hist", sim_cmd_hist},
647
  {"stall", sim_cmd_stall},
648
  {"unstall", sim_cmd_unstall},
649
  {"stats", sim_cmd_stats},
650
  {"info", sim_cmd_info},
651
  {"run", sim_cmd_run},
652
  {"setdbch", sim_cmd_setdbch},
653
  {"debug", sim_cmd_debug},
654
  {"profile", sim_cmd_profile},
655
  {"mprofile", sim_cmd_mprofile},
656
  {"cuc", sim_cmd_cuc},
657
  {"set", sim_cmd_set},
658
  {NULL, NULL}
659
};
660
 
661
#ifdef HAVE_LIBREADLINE
662
static void initialize_readline (void);
663
#endif
664
 
665
void
666
handle_sim_command (void)
667
{
668
  char *redirstr;
669
  int argc;
670
  char *argv[5];
671
  char *cur_arg;
672
  const struct sim_command *cur_cmd;
673
#ifdef HAVE_LIBREADLINE
674
  static char *prev_str = NULL;
675
#else
676
  char b2[500];
677
  static char prev_str[500] = { 0 };
678
#endif
679
 
680
  runtime.sim.iprompt_run = 1;
681
 
682
  /* Make sure that check_insn_exec is not left hanging in the scheduler (and
683
   * breaking the sim when the user doesn't want it to break). */
684
  SCHED_FIND_REMOVE (check_insn_exec, NULL);
685
  SCHED_FIND_REMOVE (print_insn_exec, NULL);
686
 
687
#ifdef HAVE_LIBREADLINE
688
  initialize_readline ();       /* Bind our completer. */
689
#endif
690
 
691
  for (;;)
692
    {
693
#ifdef HAVE_LIBREADLINE
694
      cur_arg = readline ("(sim) ");
695
#else
696
      PRINTF ("(sim) ");
697
 
698
      /* RSP does not get involved during CLI, so only check legacy interface
699
         here. */
700
      if (config.debug.gdb_enabled)
701
        {
702
          fflush (stdout);
703
          handle_server_socket (TRUE);  /* block & check_stdin = true */
704
        }
705
 
706
      cur_arg = fgets (b2, sizeof (b2), stdin);
707
 
708
      if (!cur_arg)
709
        sim_done ();
710
 
711
      if (!*cur_arg)
712
        {
713
          usleep (1000);
714
          continue;
715
        }
716
#endif
717
 
718
#ifdef HAVE_LIBREADLINE
719
      if (!*cur_arg)
720
        {
721
          if (prev_str)
722
            {
723
              free (cur_arg);
724
              cur_arg = prev_str;
725
            }
726
        }
727
      else
728
        {
729
          prev_str = cur_arg;
730
          add_history (cur_arg);
731
        }
732
#else
733
      cur_arg = strip_space (cur_arg);
734
      if (*cur_arg == '\n')
735
        strcpy (cur_arg, prev_str);
736
      else
737
        strcpy (prev_str, cur_arg);
738
#endif
739
 
740
      if ((redirstr = strchr (cur_arg, '>')))
741
        {
742
          redirstr = strip_space (++redirstr);
743
 
744
          while (!isspace (*redirstr) && *redirstr)
745
            redirstr++;
746
          *redirstr = '\0';
747
 
748
          redirstr = strchr (cur_arg, '>');
749
          *redirstr = '\0';
750
 
751
          redirstr = strip_space (++redirstr);
752
          runtime.sim.fout = fopen (redirstr, "w+");
753
          if (!runtime.sim.fout)
754
            runtime.sim.fout = stdout;
755
        }
756
 
757
      if (*cur_arg)
758
        {
759
          argc = 0;
760
          while (*cur_arg)
761
            {
762
              argv[argc] = cur_arg;
763
              argc++;
764
              while (!isspace (*cur_arg) && *cur_arg)
765
                cur_arg++;
766
              if (*cur_arg)
767
                {
768
                  *cur_arg = '\0';
769
                  cur_arg = strip_space (cur_arg + 1);
770
                }
771
              else
772
                *cur_arg = '\0';
773
              if (argc == 5)
774
                {
775
                  fprintf (stderr,
776
                           "Too many arguments given to command `%s'\n",
777
                           argv[0]);
778
                  break;
779
                }
780
            }
781
 
782
          for (cur_cmd = sim_commands; cur_cmd->name; cur_cmd++)
783
            {
784
              if (!strcmp (cur_cmd->name, argv[0]))
785
                {
786
                  if (cur_cmd->cmd_handle (argc, argv))
787
                    {
788
                      runtime.sim.iprompt = 0;
789
                      runtime.sim.iprompt_run = 0;
790
                      return;
791
                    }
792
                  break;
793
                }
794
            }
795
 
796
          if (!cur_cmd->name)
797
            PRINTF ("%s: Unknown command.\n", argv[0]);
798
        }
799
 
800
      if (redirstr)
801
        {
802
          redirstr = NULL;
803
          fclose (runtime.sim.fout);
804
          runtime.sim.fout = stdout;
805
        }
806
 
807
    }
808
}
809
 
810
#ifdef HAVE_LIBREADLINE
811
 
812
int
813
check_gdb_comm (void)
814
{
815
  /* Only do anything for legacy debug interface. RSP does not get involved
816
     when the CLI is active */
817
  if (config.debug.gdb_enabled)
818
    {
819
      handle_server_socket (TRUE);      /* block & check_stdin = true */
820
    }
821
 
822
  return 0;
823
}
824
 
825
char *command_generator ();
826
char **sim_completion ();
827
 
828
/* Tell the GNU readline library how to complete.  We want to try to complete
829
   on command names if this is the first word in the line, or on filenames
830
   if not. */
831
static void
832
initialize_readline (void)
833
{
834
  /* Allow conditional parsing of the ~/.inputrc file. */
835
  rl_readline_name = "or1ksim";
836
 
837
  /* Tell the completer that we want a crack first. */
838
  rl_attempted_completion_function = sim_completion;
839
 
840 60 jeremybenn
#if HAVE_DECL_RL_EVENT_HOOK
841 19 jeremybenn
  /* Handle the gdb socket while waiting for input */
842
  rl_event_hook = check_gdb_comm;
843 60 jeremybenn
#endif
844 19 jeremybenn
}
845
 
846
/* Attempt to complete on the contents of TEXT.  START and END bound the
847
   region of rl_line_buffer that contains the word to complete.  TEXT is
848
   the word to complete.  We can use the entire contents of rl_line_buffer
849
   in case we want to do some simple parsing.  Return the array of matches,
850
   or NULL if there aren't any. */
851
/* FIXME: Handle arguments to the `set' command */
852
char **
853
sim_completion (char *text, int start, int end)
854
{
855
  char **matches;
856
 
857
  matches = NULL;
858
 
859
  /* If this word is at the start of the line, then it is a command
860
     to complete.  Otherwise it is the name of a file in the current
861
     directory. */
862
  if (!start)
863
    matches = rl_completion_matches (text, command_generator);
864
 
865
  return matches;
866
}
867
 
868
/* Generator function for command completion.  STATE lets us know whether
869
   to start from scratch; without any state (i.e. STATE == 0), then we
870
   start at the top of the list. */
871
char *
872
command_generator (char *text, int state)
873
{
874
  static int list_index, len;
875
  const char *name;
876
 
877
  /* If this is a new word to complete, initialize now.  This includes
878
     saving the length of TEXT for efficiency, and initializing the index
879
     variable to 0. */
880
  if (!state)
881
    {
882
      list_index = 0;
883
      len = strlen (text);
884
    }
885
 
886
  /* Return the next name which partially matches from the command list. */
887
  while ((name = sim_commands[list_index].name))
888
    {
889
      list_index++;
890
 
891
      if (strncmp (name, text, len) == 0)
892
        return strdup (name);
893
    }
894
 
895
  /* If no names matched, then return NULL. */
896
  return NULL;
897
}
898
 
899
/* Repeats the last command.  */
900
char *
901
repeat_last_command ()
902
{
903
  int offset = where_history ();
904
  HIST_ENTRY *hist;
905
 
906
  if ((hist = history_get (offset)))
907
    return strdup (hist->line);
908
  return 0;
909
}
910
 
911
#endif

powered by: WebSVN 2.1.0

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