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(class cl_app *the_app,
62 char *more_args, int iargc, char *iargv[]):
66 argc= iargc; argv= iargv;
69 arguments= new cl_list(2, 2);
70 accept_args= more_args?strdup(more_args):0;
71 in_files= new cl_ustrings(2, 2);
72 gui= new cl_gui(this);
81 proc_arguments(argc, argv);
82 class cl_cmdset *cmdset= new cl_cmdset(this);
84 build_cmd_set(cmdset);
85 if (!(uc= mk_controller()))
88 cmd= new cl_commander(app, cmdset, this);
90 char *Config= get_sarg(0, "Config");
93 class cl_console *con= cmd->mk_console(Config, 0/*"/dev/tty"*/, this);
94 cmd->add_console(con);
96 if (cmd->cons->get_count() == 0)
98 fprintf(stderr, "No command console available.\n");
101 for (i= 0; i < in_files->count; i++)
103 char *fname= (char *)(in_files->at(i));
105 if ((l= uc->read_hex_file(fname)) >= 0)
107 cmd->all_printf("%ld words read from %s\n", l, fname);
108 fprintf(stderr, "%ld words read from %s\n", l, fname);
112 cmd->all_print("\0", 1);
114 cmd->all_printf("%s", (prompt= get_sarg(0, "prompt"))?prompt:"> ") ;
119 cl_sim::~cl_sim(void)
126 cl_sim::proc_arguments(int argc, char *argv[])
131 opts= (char*)malloc((accept_args?strlen(accept_args):0)+100);
132 strcpy(opts, "c:C:p:PX:vV");
134 strcat(opts, "Z:r:");
137 strcat(opts, accept_args);
139 while((c= getopt(argc, argv, opts)) != -1)
144 arguments->add(new cl_prg_arg('c', 0, optarg));
148 arguments->add(new cl_prg_arg(0, "Config", optarg));
154 arguments->add(new cl_prg_arg('Z', 0, (long)1));
155 if (!optarg || !isdigit(*optarg))
156 fprintf(stderr, "expected portnumber to follow -Z\n");
159 long l= strtol(optarg, &p, 0);
160 arguments->add(new cl_prg_arg(0, "Zport", l));
166 arguments->add(new cl_prg_arg(0, "prompt", optarg));
170 arguments->add(new cl_prg_arg('P', 0, (long)1));
175 arguments->add(new cl_prg_arg('r', 0,
176 (long)strtol(optarg, NULL, 0)));
183 for (cp= optarg; *cp; *cp= toupper(*cp), cp++);
184 XTAL= strtod(optarg, &cp);
191 fprintf(stderr, "Xtal frequency must be greather than 0\n");
194 arguments->add(new cl_prg_arg('X', 0, XTAL));
199 printf("%s: %s\n", argv[0], VERSIONSTR);
204 arguments->add(new cl_prg_arg('V', 0, (long)1));
208 if ((c= proc_arg(c, optarg)))
213 if ((c= proc_arg(c, optarg)))
216 if (!arg_avail("prompt"))
217 arguments->add(new cl_prg_arg(0, "prompt", "> "));
219 for (i= optind; i < argc; i++)
220 in_files->add(argv[i]);
227 cl_sim::proc_arg(char arg, char *optarg)
233 cl_sim::arg_avail(char name)
238 for (i= 0; i < arguments->count; i++)
240 a= (class cl_prg_arg *)(arguments->at(i));
241 if (a->short_name == name)
248 cl_sim::arg_avail(char *name)
253 for (i= 0; i < arguments->count; i++)
255 a= (class cl_prg_arg *)(arguments->at(i));
257 strcmp(a->long_name, name) == 0)
264 cl_sim::get_iarg(char sname, char *lname)
269 for (i= 0; i < arguments->count; i++)
271 a= (class cl_prg_arg *)(arguments->at(i));
272 if ((sname && a->short_name == sname) ||
273 (lname && a->long_name && strcmp(a->long_name, lname) == 0))
276 if (a->get_ivalue(&iv))
287 cl_sim::get_sarg(char sname, char *lname)
292 for (i= 0; i < arguments->count; i++)
294 a= (class cl_prg_arg *)(arguments->at(i));
295 if ((sname && a->short_name == sname) ||
296 (lname && a->long_name && strcmp(a->long_name, lname) == 0))
297 return(a->get_svalue());
304 cl_sim::get_farg(char sname, char *lname)
309 for (i= 0; i < arguments->count; i++)
311 a= (class cl_prg_arg *)(arguments->at(i));
312 if ((sname && a->short_name == sname) ||
313 (lname && a->long_name && strcmp(a->long_name, lname) == 0))
314 return(a->get_fvalue());
320 cl_sim::get_parg(char sname, char *lname)
325 for (i= 0; i < arguments->count; i++)
327 a= (class cl_prg_arg *)(arguments->at(i));
328 if ((sname && a->short_name == sname) ||
329 (lname && a->long_name && strcmp(a->long_name, lname) == 0))
330 return(a->get_pvalue());
336 cl_sim::mk_controller(void)
338 return(new cl_uc(this));
342 cl_sim::mk_cmd_int_arg(long long i)
344 class cl_cmd_arg *arg= new cl_cmd_int_arg(uc, i);
350 cl_sim::mk_cmd_sym_arg(char *s)
352 class cl_cmd_arg *arg= new cl_cmd_sym_arg(uc, s);
358 cl_sim::mk_cmd_str_arg(char *s)
360 class cl_cmd_arg *arg= new cl_cmd_str_arg(uc, s);
366 cl_sim::mk_cmd_bit_arg(class cl_cmd_arg *sfr, class cl_cmd_arg *bit)
368 class cl_cmd_arg *arg= new cl_cmd_bit_arg(uc, sfr, bit);
374 cl_sim::mk_cmd_array_arg(class cl_cmd_arg *aname, class cl_cmd_arg *aindex)
376 class cl_cmd_arg *arg= new cl_cmd_array_arg(uc, aname, aindex);
383 * Main cycle of the simulator
392 (state & SIM_QUIT) == 0)
397 if (cmd->input_avail())
399 done= cmd->proc_input();
405 done= cmd->proc_input();
412 cl_sim::do_cmd(char *cmdstr, class cl_console *console)
414 class cl_cmdline *cmdline;
418 cmdline= new cl_cmdline(cmdstr, console);
420 cm= cmd->cmdset->get_cmd(cmdline);
422 retval= cm->work(cmdline, console);
426 return(console->interpret(cmdstr));
430 cl_sim::start(class cl_console *con)
433 con->flags|= CONS_FROZEN;
434 cmd->frozen_console= con;
439 cl_sim::stop(int reason)
442 if (cmd->frozen_console)
444 if (reason == resUSER &&
445 cmd->frozen_console->input_avail())
446 cmd->frozen_console->read_line();
447 cmd->frozen_console->printf("Stop at 0x%06x: (%d) ", uc->PC, reason);
451 cmd->frozen_console->printf("Halted\n");
454 cmd->frozen_console->printf("Invalid address\n");
457 cmd->frozen_console->printf("Stack overflow\n");
460 cmd->frozen_console->printf("Breakpoint\n");
463 cmd->frozen_console->printf("Interrupt\n");
466 cmd->frozen_console->printf("Watchdog reset\n");
469 cmd->frozen_console->printf("User stopped\n");
472 cmd->frozen_console->printf("Invalid instruction 0x%04x\n",
473 uc->get_mem(MEM_ROM, uc->PC));
476 cmd->frozen_console->printf("Unknown reason\n");
479 cmd->frozen_console->printf("F 0x%06x\n", uc->PC); // for sdcdb
480 //if (cmd->actual_console != cmd->frozen_console)
481 cmd->frozen_console->flags&= ~CONS_FROZEN;
482 cmd->frozen_console->print_prompt();
483 cmd->frozen_console= 0;
493 cl_sim::build_cmd_set(class cl_cmdset *cmdset)
496 class cl_cmdset *cset;
499 cset= new cl_cmdset(this);
501 cset->add(cmd= new cl_conf_cmd("_no_parameters_", 0,
502 "conf Configuration",
503 "long help of conf"));
505 cset->add(cmd= new cl_conf_addmem_cmd("addmem", 0,
508 "long help of conf addmem"));
511 cmdset->add(cmd= new cl_super_cmd("conf", 0,
512 "conf subcommand Information, see `conf' command for more help",
513 "long help of conf", cset));
516 cmdset->add(cmd= new cl_state_cmd("state", 0,
517 "state State of simulator",
518 "long help of state"));
521 cmdset->add(cmd= new cl_file_cmd("file", 0,
522 "file \"FILE\" Load FILE into ROM",
523 "long help of file"));
525 cmd->add_name("load");
527 cmdset->add(cmd= new cl_dl_cmd("download", 0,
528 "download,dl Load (intel.hex) data",
529 "long help of download"));
534 cset= new cl_cmdset(this);
536 cset->add(cmd= new cl_info_bp_cmd("breakpoints", 0,
537 "info breakpoints Status of user-settable breakpoints",
538 "long help of info breakpoints"));
541 cset->add(cmd= new cl_info_reg_cmd("registers", 0,
542 "info registers List of integer registers and their contents",
543 "long help of info registers"));
545 cset->add(cmd= new cl_info_hw_cmd("hardware", 0,
546 "info hardware cathegory\n"
547 " Status of hardware elements of the CPU",
548 "long help of info hardware"));
549 cmd->add_name("h w");
552 cmdset->add(cmd= new cl_super_cmd("info", 0,
553 "info subcommand Information, see `info' command for more help",
554 "long help of info", cset));
558 cset= new cl_cmdset(this);
560 cset->add(cmd= new cl_get_sfr_cmd("sfr", 0,
561 "get sfr address...\n"
562 " Get value of addressed SFRs",
563 "long help of get sfr"));
565 cset->add(cmd= new cl_get_option_cmd("option", 0,
567 " Get value of an option",
568 "long help of get option"));
571 cmdset->add(cmd= new cl_super_cmd("get", 0,
572 "get subcommand Get, see `get' command for more help",
573 "long help of get", cset));
577 cset= new cl_cmdset(this);
579 cset->add(cmd= new cl_set_mem_cmd("memory", 0,
580 "set memory memory_type address data...\n"
581 " Place list of data into memory",
582 "long help of set memory"));
584 cset->add(cmd= new cl_set_bit_cmd("bit", 0,
585 "set bit addr 0|1 Set specified bit to 0 or 1",
586 "long help of set bit"));
588 cset->add(cmd= new cl_set_port_cmd("port", 0,
589 "set port hw data Set data connected to port",
590 "long help of set port"));
592 cset->add(cmd= new cl_set_option_cmd("option", 0,
593 "set option name value\n"
594 " Set value of an option",
595 "long help of set option"));
598 cmdset->add(cmd= new cl_super_cmd("set", 0,
599 "set subcommand Set, see `set' command for more help",
600 "long help of set", cset));
603 cmdset->add(cmd= new cl_timer_cmd("timer", 0,
604 "timer a|d|g|r|s|v id [direction|value]\n"
605 " Timer add|del|get|run|stop|value",
606 "timer add|create|make id [direction] -- create a new timer\n"
607 "timer del id -- delete a timer\n"
608 "timer get id -- list timers\n"
609 "timer run id -- turn a timer ON\n"
610 "timer stop id -- turn a timer OFF\n"
611 "timer value id val -- set value of a timer to `val'"));
614 cmdset->add(cmd= new cl_run_cmd("run", 0,
615 "run [start [stop]] Go",
616 "long help of run"));
621 cmdset->add(cmd= new cl_stop_cmd("stop", 0,
623 "long help of stop"));
626 cmdset->add(cmd= new cl_step_cmd("step", 0,
628 "long help of step"));
632 cmdset->add(cmd= new cl_next_cmd("next", 0,
634 "long help of next"));
638 cmdset->add(cmd= new cl_pc_cmd("pc", 0,
639 "pc [addr] Set/get PC",
643 cmdset->add(cmd= new cl_reset_cmd("reset", 0,
645 "long help of reset"));
648 cmdset->add(cmd= new cl_dump_cmd("dump", 0,
649 "dump memory_type [start [stop [bytes_per_line]]]\n"
650 " Dump memory of specified type\n"
651 "dump bit... Dump bits",
652 "long help of dump"));
655 cmdset->add(cmd= new cl_di_cmd("di", 0,
656 "di [start [stop]] Dump Internal RAM",
660 cmdset->add(cmd= new cl_dx_cmd("dx", 0,
661 "dx [start [stop]] Dump External RAM",
665 cmdset->add(cmd= new cl_ds_cmd("ds", 0,
666 "ds [start [stop]] Dump SFR",
670 cmdset->add(cmd= new cl_dch_cmd("dch", 0,
671 "dch [start [stop]] Dump code in hex form",
672 "long help of dch"));
675 cmdset->add(cmd= new cl_dc_cmd("dc", 0,
676 "dc [start [stop]] Dump code in disass form",
680 cmdset->add(cmd= new cl_disassemble_cmd("disassemble", 0,
681 "disassemble [start [offset [lines]]]\n"
683 "long help of disassemble"));
686 cmdset->add(cmd= new cl_fill_cmd("fill", 0,
687 "fill memory_type start end data\n"
688 " Fill memory region with data",
689 "long help of fill"));
692 cmdset->add(cmd= new cl_where_cmd("where", 0,
693 "where memory_type data...\n"
694 " Case unsensitive search for data",
695 "long help of where"));
698 cmdset->add(cmd= new cl_Where_cmd("Where", 0,
699 "Where memory_type data...\n"
700 " Case sensitive search for data",
701 "long help of Where"));
704 cmdset->add(cmd= new cl_break_cmd("break", 0,
705 "break addr [hit] Set fix breakpoint\n"
706 "break mem_type r|w addr [hit]\n"
707 " Set fix event breakpoint",
708 "long help of break"));
711 cmdset->add(cmd= new cl_tbreak_cmd("tbreak", 0,
712 "tbreak addr [hit] Set temporary breakpoint\n"
713 "tbreak mem_type r|w addr [hit]\n"
714 " Set temporary event breakpoint",
715 "long help of tbreak"));
718 cmdset->add(cmd= new cl_clear_cmd("clear", 0,
719 "clear [addr...] Clear fix breakpoint",
720 "long help of clear"));
723 cmdset->add(cmd= new cl_delete_cmd("delete", 0,
724 "delete [nr...] Delete breakpoint(s)",
725 "long help of clear"));
728 cmdset->add(cmd= new cl_help_cmd("help", 0,
729 "help [command] Help about command(s)",
730 "long help of help"));
734 cmdset->add(cmd= new cl_quit_cmd("quit", 0,
736 "long help of quit"));
739 cmdset->add(cmd= new cl_kill_cmd("kill", 0,
740 "kill Shutdown simulator",
741 "long help of kill"));
745 cset= new cl_cmdset(this);
747 cset->add(cmd= new cl_show_copying_cmd("copying", 0,
748 "show copying Conditions for redistributing copies of uCsim",
749 "long help of show copying"));
751 cset->add(cmd= new cl_show_warranty_cmd("warranty", 0,
752 "show warranty Various kinds of warranty you do not have",
753 "long help of show warranty"));
756 cmdset->add(cmd= new cl_super_cmd("show", 0,
757 "show subcommand Generic command for showing things about the uCsim",
758 "long help of show", cset));
762 cset= new cl_cmdset(this);
764 cset->add(cmd= new cl_gui_start_cmd("start", 0,
765 "gui start Start interfacing with GUI tool",
766 "long help of gui start"));
768 cset->add(cmd= new cl_gui_stop_cmd("stop", 0,
769 "gui stop Stop interfacing with GUI tool",
770 "long help of gui stop"));
773 cmdset->add(cmd= new cl_super_cmd("gui", 0,
774 "gui subcommand Operations to support GUI tools",
775 "long help of gui", cset));
780 /* End of sim.src/sim.cc */