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
55 cl_sim::cl_sim(char *more_args, int iargc, char *iargv[]):
58 argc= iargc; argv= iargv;
61 arguments= new cl_list(2, 2);
62 accept_args= more_args?strdup(more_args):0;
63 in_files= new cl_ustrings(2, 2);
72 proc_arguments(argc, argv);
76 if (!(uc= mk_controller()))
81 if (cmd->cons->get_count() == 0)
83 fprintf(stderr, "No command console available.\n");
86 for (i= 0; i < in_files->count; i++)
88 char *fname= (char *)(in_files->at(i));
90 if ((l= uc->read_hex_file(fname)) >= 0)
92 cmd->all_printf("%ld words read from %s\n", l, fname);
93 fprintf(stderr, "%ld words read from %s\n", l, fname);
97 /*FIXME: putc('\0', f)*/;
99 cmd->all_printf("%s", arg_avail('P')?"\0":
100 ((prompt= get_sarg(0, "prompt"))?prompt:"> "));
105 cl_sim::~cl_sim(void)
112 cl_sim::proc_arguments(int argc, char *argv[])
117 opts= (char*)malloc((accept_args?strlen(accept_args):0)+100);
118 strcpy(opts, "c:p:PX:vV");
120 strcat(opts, "Z:r:");
123 strcat(opts, accept_args);
125 while((c= getopt(argc, argv, opts)) != -1)
130 arguments->add(new cl_prg_arg('c', 0, optarg));
136 arguments->add(new cl_prg_arg('Z', 0, (long long)1));
137 if (!optarg || !isdigit(*optarg))
138 fprintf(stderr, "expected portnumber to follow -Z\n");
141 long long l= strtol(optarg, &p, 0);
142 arguments->add(new cl_prg_arg(0, "Zport", l));
148 arguments->add(new cl_prg_arg(0, "prompt", optarg));
152 arguments->add(new cl_prg_arg('P', 0, (long long)1));
157 arguments->add(new cl_prg_arg('r', 0,
158 (long long)strtol(optarg, NULL, 0)));
165 for (cp= optarg; *cp; *cp= toupper(*cp), cp++);
166 XTAL= strtod(optarg, &cp);
173 fprintf(stderr, "Xtal frequency must be greather than 0\n");
176 arguments->add(new cl_prg_arg('X', 0, XTAL));
181 printf("%s: %s\n", argv[0], VERSIONSTR);
186 arguments->add(new cl_prg_arg('V', 0, (long long)1));
190 if ((c= proc_arg(c, optarg)))
195 if ((c= proc_arg(c, optarg)))
198 if (!arg_avail("prompt"))
199 arguments->add(new cl_prg_arg(0, "prompt", "> "));
201 for (i= optind; i < argc; i++)
202 in_files->add(argv[i]);
209 cl_sim::proc_arg(char arg, char *optarg)
215 cl_sim::arg_avail(char name)
220 for (i= 0; i < arguments->count; i++)
222 a= (class cl_prg_arg *)(arguments->at(i));
223 if (a->short_name == name)
230 cl_sim::arg_avail(char *name)
235 for (i= 0; i < arguments->count; i++)
237 a= (class cl_prg_arg *)(arguments->at(i));
239 strcmp(a->long_name, name) == 0)
246 cl_sim::get_iarg(char sname, char *lname)
251 for (i= 0; i < arguments->count; i++)
253 a= (class cl_prg_arg *)(arguments->at(i));
254 if ((sname && a->short_name == sname) ||
255 (lname && a->long_name && strcmp(a->long_name, lname) == 0))
256 return(a->get_ivalue());
262 cl_sim::get_sarg(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))
272 return(a->get_svalue());
279 cl_sim::get_farg(char sname, char *lname)
284 for (i= 0; i < arguments->count; i++)
286 a= (class cl_prg_arg *)(arguments->at(i));
287 if ((sname && a->short_name == sname) ||
288 (lname && a->long_name && strcmp(a->long_name, lname) == 0))
289 return(a->get_fvalue());
295 cl_sim::get_parg(char sname, char *lname)
300 for (i= 0; i < arguments->count; i++)
302 a= (class cl_prg_arg *)(arguments->at(i));
303 if ((sname && a->short_name == sname) ||
304 (lname && a->long_name && strcmp(a->long_name, lname) == 0))
305 return(a->get_pvalue());
311 cl_sim::mk_commander()
313 class cl_commander *cmd= new cl_commander(this);
318 cl_sim::mk_controller(void)
320 return(new cl_uc(this));
324 cl_sim::mk_cmdset(void)
326 return(new cl_cmdset(this));
330 cl_sim::mk_cmd_int_arg(long long i)
332 class cl_cmd_arg *arg= new cl_cmd_int_arg(i);
338 cl_sim::mk_cmd_sym_arg(char *s)
340 class cl_cmd_arg *arg= new cl_cmd_sym_arg(s);
346 cl_sim::mk_cmd_str_arg(char *s)
348 class cl_cmd_arg *arg= new cl_cmd_str_arg(s);
354 cl_sim::mk_cmd_bit_arg(class cl_cmd_arg *sfr, class cl_cmd_arg *bit)
356 class cl_cmd_arg *arg= new cl_cmd_bit_arg(sfr, bit);
363 * Main cycle of the simulator
372 (state & SIM_QUIT) == 0)
377 if (cmd->input_avail())
378 done= cmd->proc_input();
383 done= cmd->proc_input();
390 cl_sim::do_cmd(char *cmdstr, class cl_console *console)
392 class cl_cmdline *cmdline;
396 cmdline= new cl_cmdline(cmdstr);
398 if (console->old_command(cmdline))
399 return(console->interpret(cmdstr));
400 cmd= cmdset->get_cmd(cmdline);
402 retval= cmd->work(cmdline, console);
406 return(console->interpret(cmdstr));
410 cl_sim::start(class cl_console *con)
413 con->flags|= CONS_FROZEN;
414 cmd->frozen_console= con;
418 cl_sim::stop(int reason)
421 if (cmd->frozen_console)
423 if (reason == resUSER &&
424 cmd->frozen_console->input_avail())
425 cmd->frozen_console->read_line();
426 cmd->frozen_console->printf("Stop at 0x%06x: (%d) ", uc->PC, reason);
430 cmd->frozen_console->printf("Halted\n");
433 cmd->frozen_console->printf("Invalid address\n");
436 cmd->frozen_console->printf("Stack overflow\n");
439 cmd->frozen_console->printf("Breakpoint\n");
442 cmd->frozen_console->printf("Interrupt\n");
445 cmd->frozen_console->printf("Watchdog reset\n");
448 cmd->frozen_console->printf("User stopped\n");
451 cmd->frozen_console->printf("Invalid instruction 0x%04x\n",
452 uc->get_mem(MEM_ROM, uc->PC));
455 cmd->frozen_console->printf("Unknown reason\n");
458 cmd->frozen_console->printf("F 0x%06x\n", uc->PC); // for sdcdb
459 //if (cmd->actual_console != cmd->frozen_console)
460 cmd->frozen_console->flags&= ~CONS_FROZEN;
461 cmd->frozen_console->print_prompt();
462 cmd->frozen_console= 0;
468 * Obsolete methods for old commander
475 cmd->cons->get_count() == 0)
477 if (cmd->actual_console)
478 return(cmd->actual_console->in?cmd->actual_console->in:stdin);
479 class cl_console *con= (class cl_console *)(cmd->cons->at(0));
480 return(con->in?con->in:stdin);
484 cl_sim::cmd_out(void)
487 cmd->cons->get_count() == 0)
489 if (cmd->actual_console)
490 return(cmd->actual_console->out?cmd->actual_console->out:stdout);
491 class cl_console *con= (class cl_console *)(cmd->cons->at(0));
492 return(con->out?con->out:stdout);
500 cl_sim::build_cmd_set(void)
503 class cl_cmdset *cset;
505 cmdset->add(cmd= new cl_conf_cmd(this, "conf", 0,
506 "conf Configuration",
507 "long help of conf"));
510 cmdset->add(cmd= new cl_state_cmd(this, "state", 0,
511 "state State of simulator",
512 "long help of state"));
515 cmdset->add(cmd= new cl_file_cmd(this, "file", 0,
516 "file \"FILE\" Load FILE into ROM",
517 "long help of file"));
519 cmd->add_name("load");
521 cmdset->add(cmd= new cl_dl_cmd(this, "download", 0,
522 "download,dl Load (intel.hex) data",
523 "long help of download"));
527 cset= new cl_cmdset(this);
529 cset->add(cmd= new cl_info_bp_cmd(this, "breakpoints", 0,
530 "info breakpoints Status of user-settable breakpoints",
531 "long help of info breakpoints"));
534 cset->add(cmd= new cl_info_reg_cmd(this, "registers", 0,
535 "info registers List of integer registers and their contents",
536 "long help of info registers"));
538 cset->add(cmd= new cl_info_hw_cmd(this, "hardware", 0,
539 "info hardware cathegory\n"
540 " Status of hardware elements of the CPU",
541 "long help of info hardware"));
545 cmdset->add(cmd= new cl_super_cmd(this, "info", 0,
546 "info subcommand Information, see `info' command for more help",
547 "long help of info", cset));
550 cmdset->add(cmd= new cl_get_cmd(this, "get", 0,
552 "long help of get"));
555 cmdset->add(cmd= new cl_set_cmd(this, "set", 0,
557 "long help of set"));
560 cmdset->add(cmd= new cl_timer_cmd(this, "timer", 0,
561 "timer a|d|g|r|s|v id [direction|value]\n"
562 " Timer add|del|get|run|stop|value",
563 "timer add|create|make id [direction] -- create a new timer\n"
564 "timer del id -- delete a timer\n"
565 "timer get id -- list timers\n"
566 "timer run id -- turn a timer ON\n"
567 "timer stop id -- turn a timer OFF\n"
568 "timer value id val -- set value of a timer to `val'"));
571 cmdset->add(cmd= new cl_run_cmd(this, "run", 0,
573 "long help of run"));
575 //cmd->add_name("g");
577 cmdset->add(cmd= new cl_step_cmd(this, "step", 0,
579 "long help of step"));
583 cmdset->add(cmd= new cl_reset_cmd(this, "reset", 0,
585 "long help of reset"));
588 cmdset->add(cmd= new cl_dump_cmd(this, "dump", 0,
589 "dump i|x|r|s [start [stop]]\n"
591 "long help of dump"));
594 cmdset->add(cmd= new cl_di_cmd(this, "di", 0,
595 "di [start [stop]] Dump Internal RAM",
599 cmdset->add(cmd= new cl_dx_cmd(this, "dx", 0,
600 "dx [start [stop]] Dump External RAM",
604 cmdset->add(cmd= new cl_ds_cmd(this, "ds", 0,
605 "ds [start [stop]] Dump SFR",
609 cmdset->add(cmd= new cl_dch_cmd(this, "dch", 0,
610 "dch [start [stop]] Dump code in hex form",
611 "long help of dch"));
614 cmdset->add(cmd= new cl_dc_cmd(this, "dc", 0,
615 "dc [start [stop]] Dump code in disass form",
619 cmdset->add(cmd= new cl_break_cmd(this, "break", 0,
620 "break addr [hit] Set fix breakpoint",
621 "long help of break"));
624 cmdset->add(cmd= new cl_tbreak_cmd(this, "tbreak", 0,
625 "tbreak addr [hit] Set temporary breakpoint",
626 "long help of tbreak"));
629 cmdset->add(cmd= new cl_clear_cmd(this, "clear", 0,
630 "clear [addr...] Clear fix breakpoint",
631 "long help of clear"));
634 cmdset->add(cmd= new cl_help_cmd(this, "help", 0,
636 "long help of help"));
640 cmdset->add(cmd= new cl_quit_cmd(this, "quit", 0,
642 "long help of quit"));
645 cmdset->add(cmd= new cl_kill_cmd(this, "kill", 0,
646 "kill Shutdown simulator",
647 "long help of kill"));
652 /* End of sim.src/sim.cc */