2 * Simulator of microcontrollers (sim.cc)
4 * Copyright (C) 1999,99 Drotos Daniel, Talker Bt.
6 * To contact author send email to drdani@mazsola.iit.uni-miskolc.hu
10 /* This file is part of microcontroller simulator: ucsim.
12 UCSIM is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 UCSIM is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with UCSIM; see the file COPYING. If not, write to the Free
24 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
51 #include "cmdconfcl.h"
61 cl_sim::cl_sim(char *more_args, int iargc, char *iargv[]):
64 argc= iargc; argv= iargv;
67 arguments= new cl_list(2, 2);
68 accept_args= more_args?strdup(more_args):0;
69 in_files= new cl_ustrings(2, 2);
70 gui= new cl_gui(this);
79 proc_arguments(argc, argv);
80 class cl_cmdset *cmdset= new cl_cmdset(this);
82 build_cmd_set(cmdset);
83 if (!(uc= mk_controller()))
86 cmd= new cl_commander(cmdset, this);
88 char *Config= get_sarg(0, "Config");
91 class cl_console *con= cmd->mk_console(Config, 0/*"/dev/tty"*/, this);
92 cmd->add_console(con);
94 if (cmd->cons->get_count() == 0)
96 fprintf(stderr, "No command console available.\n");
99 for (i= 0; i < in_files->count; i++)
101 char *fname= (char *)(in_files->at(i));
103 if ((l= uc->read_hex_file(fname)) >= 0)
105 cmd->all_printf("%ld words read from %s\n", l, fname);
106 fprintf(stderr, "%ld words read from %s\n", l, fname);
110 cmd->all_print("\0", 1);
112 cmd->all_printf("%s", (prompt= get_sarg(0, "prompt"))?prompt:"> ") ;
117 cl_sim::~cl_sim(void)
124 cl_sim::proc_arguments(int argc, char *argv[])
129 opts= (char*)malloc((accept_args?strlen(accept_args):0)+100);
130 strcpy(opts, "c:C:p:PX:vV");
132 strcat(opts, "Z:r:");
135 strcat(opts, accept_args);
137 while((c= getopt(argc, argv, opts)) != -1)
142 arguments->add(new cl_prg_arg('c', 0, optarg));
146 arguments->add(new cl_prg_arg(0, "Config", optarg));
152 arguments->add(new cl_prg_arg('Z', 0, (long)1));
153 if (!optarg || !isdigit(*optarg))
154 fprintf(stderr, "expected portnumber to follow -Z\n");
157 long l= strtol(optarg, &p, 0);
158 arguments->add(new cl_prg_arg(0, "Zport", l));
164 arguments->add(new cl_prg_arg(0, "prompt", optarg));
168 arguments->add(new cl_prg_arg('P', 0, (long)1));
173 arguments->add(new cl_prg_arg('r', 0,
174 (long)strtol(optarg, NULL, 0)));
181 for (cp= optarg; *cp; *cp= toupper(*cp), cp++);
182 XTAL= strtod(optarg, &cp);
189 fprintf(stderr, "Xtal frequency must be greather than 0\n");
192 arguments->add(new cl_prg_arg('X', 0, XTAL));
197 printf("%s: %s\n", argv[0], VERSIONSTR);
202 arguments->add(new cl_prg_arg('V', 0, (long)1));
206 if ((c= proc_arg(c, optarg)))
211 if ((c= proc_arg(c, optarg)))
214 if (!arg_avail("prompt"))
215 arguments->add(new cl_prg_arg(0, "prompt", "> "));
217 for (i= optind; i < argc; i++)
218 in_files->add(argv[i]);
225 cl_sim::proc_arg(char arg, char *optarg)
231 cl_sim::arg_avail(char name)
236 for (i= 0; i < arguments->count; i++)
238 a= (class cl_prg_arg *)(arguments->at(i));
239 if (a->short_name == name)
246 cl_sim::arg_avail(char *name)
251 for (i= 0; i < arguments->count; i++)
253 a= (class cl_prg_arg *)(arguments->at(i));
255 strcmp(a->long_name, name) == 0)
262 cl_sim::get_iarg(char sname, char *lname)
267 for (i= 0; i < arguments->count; i++)
269 a= (class cl_prg_arg *)(arguments->at(i));
270 if ((sname && a->short_name == sname) ||
271 (lname && a->long_name && strcmp(a->long_name, lname) == 0))
274 if (a->get_ivalue(&iv))
285 cl_sim::get_sarg(char sname, char *lname)
290 for (i= 0; i < arguments->count; i++)
292 a= (class cl_prg_arg *)(arguments->at(i));
293 if ((sname && a->short_name == sname) ||
294 (lname && a->long_name && strcmp(a->long_name, lname) == 0))
295 return(a->get_svalue());
302 cl_sim::get_farg(char sname, char *lname)
307 for (i= 0; i < arguments->count; i++)
309 a= (class cl_prg_arg *)(arguments->at(i));
310 if ((sname && a->short_name == sname) ||
311 (lname && a->long_name && strcmp(a->long_name, lname) == 0))
312 return(a->get_fvalue());
318 cl_sim::get_parg(char sname, char *lname)
323 for (i= 0; i < arguments->count; i++)
325 a= (class cl_prg_arg *)(arguments->at(i));
326 if ((sname && a->short_name == sname) ||
327 (lname && a->long_name && strcmp(a->long_name, lname) == 0))
328 return(a->get_pvalue());
334 cl_sim::mk_controller(void)
336 return(new cl_uc(this));
340 cl_sim::mk_cmd_int_arg(long long i)
342 class cl_cmd_arg *arg= new cl_cmd_int_arg(uc, i);
348 cl_sim::mk_cmd_sym_arg(char *s)
350 class cl_cmd_arg *arg= new cl_cmd_sym_arg(uc, s);
356 cl_sim::mk_cmd_str_arg(char *s)
358 class cl_cmd_arg *arg= new cl_cmd_str_arg(uc, s);
364 cl_sim::mk_cmd_bit_arg(class cl_cmd_arg *sfr, class cl_cmd_arg *bit)
366 class cl_cmd_arg *arg= new cl_cmd_bit_arg(uc, sfr, bit);
372 cl_sim::mk_cmd_array_arg(class cl_cmd_arg *aname, class cl_cmd_arg *aindex)
374 class cl_cmd_arg *arg= new cl_cmd_array_arg(uc, aname, aindex);
381 * Main cycle of the simulator
390 (state & SIM_QUIT) == 0)
395 if (cmd->input_avail())
397 done= cmd->proc_input();
403 done= cmd->proc_input();
410 cl_sim::do_cmd(char *cmdstr, class cl_console *console)
412 class cl_cmdline *cmdline;
416 cmdline= new cl_cmdline(cmdstr, console);
418 cm= cmd->cmdset->get_cmd(cmdline);
420 retval= cm->work(cmdline, console);
424 return(console->interpret(cmdstr));
428 cl_sim::start(class cl_console *con)
431 con->flags|= CONS_FROZEN;
432 cmd->frozen_console= con;
437 cl_sim::stop(int reason)
440 if (cmd->frozen_console)
442 if (reason == resUSER &&
443 cmd->frozen_console->input_avail())
444 cmd->frozen_console->read_line();
445 cmd->frozen_console->printf("Stop at 0x%06x: (%d) ", uc->PC, reason);
449 cmd->frozen_console->printf("Halted\n");
452 cmd->frozen_console->printf("Invalid address\n");
455 cmd->frozen_console->printf("Stack overflow\n");
458 cmd->frozen_console->printf("Breakpoint\n");
461 cmd->frozen_console->printf("Interrupt\n");
464 cmd->frozen_console->printf("Watchdog reset\n");
467 cmd->frozen_console->printf("User stopped\n");
470 cmd->frozen_console->printf("Invalid instruction 0x%04x\n",
471 uc->get_mem(MEM_ROM, uc->PC));
474 cmd->frozen_console->printf("Unknown reason\n");
477 cmd->frozen_console->printf("F 0x%06x\n", uc->PC); // for sdcdb
478 //if (cmd->actual_console != cmd->frozen_console)
479 cmd->frozen_console->flags&= ~CONS_FROZEN;
480 cmd->frozen_console->print_prompt();
481 cmd->frozen_console= 0;
491 cl_sim::build_cmd_set(class cl_cmdset *cmdset)
494 class cl_cmdset *cset;
497 cset= new cl_cmdset(this);
499 cset->add(cmd= new cl_conf_cmd("_no_parameters_", 0,
500 "conf Configuration",
501 "long help of conf"));
503 cset->add(cmd= new cl_conf_addmem_cmd("addmem", 0,
506 "long help of conf addmem"));
509 cmdset->add(cmd= new cl_super_cmd("conf", 0,
510 "conf subcommand Information, see `conf' command for more help",
511 "long help of conf", cset));
514 cmdset->add(cmd= new cl_state_cmd("state", 0,
515 "state State of simulator",
516 "long help of state"));
519 cmdset->add(cmd= new cl_file_cmd("file", 0,
520 "file \"FILE\" Load FILE into ROM",
521 "long help of file"));
523 cmd->add_name("load");
525 cmdset->add(cmd= new cl_dl_cmd("download", 0,
526 "download,dl Load (intel.hex) data",
527 "long help of download"));
532 cset= new cl_cmdset(this);
534 cset->add(cmd= new cl_info_bp_cmd("breakpoints", 0,
535 "info breakpoints Status of user-settable breakpoints",
536 "long help of info breakpoints"));
539 cset->add(cmd= new cl_info_reg_cmd("registers", 0,
540 "info registers List of integer registers and their contents",
541 "long help of info registers"));
543 cset->add(cmd= new cl_info_hw_cmd("hardware", 0,
544 "info hardware cathegory\n"
545 " Status of hardware elements of the CPU",
546 "long help of info hardware"));
547 cmd->add_name("h w");
550 cmdset->add(cmd= new cl_super_cmd("info", 0,
551 "info subcommand Information, see `info' command for more help",
552 "long help of info", cset));
556 cset= new cl_cmdset(this);
558 cset->add(cmd= new cl_get_sfr_cmd("sfr", 0,
559 "get sfr address...\n"
560 " Get value of addressed SFRs",
561 "long help of get sfr"));
563 cset->add(cmd= new cl_get_option_cmd("option", 0,
565 " Get value of an option",
566 "long help of get option"));
569 cmdset->add(cmd= new cl_super_cmd("get", 0,
570 "get subcommand Get, see `get' command for more help",
571 "long help of get", cset));
575 cset= new cl_cmdset(this);
577 cset->add(cmd= new cl_set_mem_cmd("memory", 0,
578 "set memory memory_type address data...\n"
579 " Place list of data into memory",
580 "long help of set memory"));
582 cset->add(cmd= new cl_set_bit_cmd("bit", 0,
583 "set bit addr 0|1 Set specified bit to 0 or 1",
584 "long help of set bit"));
586 cset->add(cmd= new cl_set_port_cmd("port", 0,
587 "set port hw data Set data connected to port",
588 "long help of set port"));
590 cset->add(cmd= new cl_set_option_cmd("option", 0,
591 "set option name value\n"
592 " Set value of an option",
593 "long help of set option"));
596 cmdset->add(cmd= new cl_super_cmd("set", 0,
597 "set subcommand Set, see `set' command for more help",
598 "long help of set", cset));
601 cmdset->add(cmd= new cl_timer_cmd("timer", 0,
602 "timer a|d|g|r|s|v id [direction|value]\n"
603 " Timer add|del|get|run|stop|value",
604 "timer add|create|make id [direction] -- create a new timer\n"
605 "timer del id -- delete a timer\n"
606 "timer get id -- list timers\n"
607 "timer run id -- turn a timer ON\n"
608 "timer stop id -- turn a timer OFF\n"
609 "timer value id val -- set value of a timer to `val'"));
612 cmdset->add(cmd= new cl_run_cmd("run", 0,
613 "run [start [stop]] Go",
614 "long help of run"));
619 cmdset->add(cmd= new cl_stop_cmd("stop", 0,
621 "long help of stop"));
624 cmdset->add(cmd= new cl_step_cmd("step", 0,
626 "long help of step"));
630 cmdset->add(cmd= new cl_next_cmd("next", 0,
632 "long help of next"));
636 cmdset->add(cmd= new cl_pc_cmd("pc", 0,
637 "pc [addr] Set/get PC",
641 cmdset->add(cmd= new cl_reset_cmd("reset", 0,
643 "long help of reset"));
646 cmdset->add(cmd= new cl_dump_cmd("dump", 0,
647 "dump memory_type [start [stop [bytes_per_line]]]\n"
648 " Dump memory of specified type\n"
649 "dump bit... Dump bits",
650 "long help of dump"));
653 cmdset->add(cmd= new cl_di_cmd("di", 0,
654 "di [start [stop]] Dump Internal RAM",
658 cmdset->add(cmd= new cl_dx_cmd("dx", 0,
659 "dx [start [stop]] Dump External RAM",
663 cmdset->add(cmd= new cl_ds_cmd("ds", 0,
664 "ds [start [stop]] Dump SFR",
668 cmdset->add(cmd= new cl_dch_cmd("dch", 0,
669 "dch [start [stop]] Dump code in hex form",
670 "long help of dch"));
673 cmdset->add(cmd= new cl_dc_cmd("dc", 0,
674 "dc [start [stop]] Dump code in disass form",
678 cmdset->add(cmd= new cl_disassemble_cmd("disassemble", 0,
679 "disassemble [start [offset [lines]]]\n"
681 "long help of disassemble"));
684 cmdset->add(cmd= new cl_fill_cmd("fill", 0,
685 "fill memory_type start end data\n"
686 " Fill memory region with data",
687 "long help of fill"));
690 cmdset->add(cmd= new cl_where_cmd("where", 0,
691 "where memory_type data...\n"
692 " Case unsensitive search for data",
693 "long help of where"));
696 cmdset->add(cmd= new cl_Where_cmd("Where", 0,
697 "Where memory_type data...\n"
698 " Case sensitive search for data",
699 "long help of Where"));
702 cmdset->add(cmd= new cl_break_cmd("break", 0,
703 "break addr [hit] Set fix breakpoint\n"
704 "break mem_type r|w addr [hit]\n"
705 " Set fix event breakpoint",
706 "long help of break"));
709 cmdset->add(cmd= new cl_tbreak_cmd("tbreak", 0,
710 "tbreak addr [hit] Set temporary breakpoint\n"
711 "tbreak mem_type r|w addr [hit]\n"
712 " Set temporary event breakpoint",
713 "long help of tbreak"));
716 cmdset->add(cmd= new cl_clear_cmd("clear", 0,
717 "clear [addr...] Clear fix breakpoint",
718 "long help of clear"));
721 cmdset->add(cmd= new cl_delete_cmd("delete", 0,
722 "delete [nr...] Delete breakpoint(s)",
723 "long help of clear"));
726 cmdset->add(cmd= new cl_help_cmd("help", 0,
727 "help [command] Help about command(s)",
728 "long help of help"));
732 cmdset->add(cmd= new cl_quit_cmd("quit", 0,
734 "long help of quit"));
737 cmdset->add(cmd= new cl_kill_cmd("kill", 0,
738 "kill Shutdown simulator",
739 "long help of kill"));
743 cset= new cl_cmdset(this);
745 cset->add(cmd= new cl_show_copying_cmd("copying", 0,
746 "show copying Conditions for redistributing copies of uCsim",
747 "long help of show copying"));
749 cset->add(cmd= new cl_show_warranty_cmd("warranty", 0,
750 "show warranty Various kinds of warranty you do not have",
751 "long help of show warranty"));
754 cmdset->add(cmd= new cl_super_cmd("show", 0,
755 "show subcommand Generic command for showing things about the uCsim",
756 "long help of show", cset));
760 cset= new cl_cmdset(this);
762 cset->add(cmd= new cl_gui_start_cmd("start", 0,
763 "gui start Start interfacing with GUI tool",
764 "long help of gui start"));
766 cset->add(cmd= new cl_gui_stop_cmd("stop", 0,
767 "gui stop Stop interfacing with GUI tool",
768 "long help of gui stop"));
771 cmdset->add(cmd= new cl_super_cmd("gui", 0,
772 "gui subcommand Operations to support GUI tools",
773 "long help of gui", cset));
778 /* End of sim.src/sim.cc */