2 * Simulator of microcontrollers (app.cc)
4 * Copyright (C) 2001,01 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
40 # include <winsock2.h>
42 #elif defined HAVE_SYS_SOCKET_H
43 # include <sys/socket.h>
61 #include "cmdconfcl.h"
71 /*cl_option::cl_option(int atype, char sn, char *ln)
78 long_name= strdup(ln);
79 values= new cl_ustrings(1, 1);
82 cl_option::~cl_option(void)
90 cl_option::add_value(char *value)
93 return(values->count - 1);
97 cl_option::get_value(int index)
99 if (index > values->count - 1)
101 return((char*)(values->at(index)));
104 /* List of options */
106 /*cl_options::cl_options(void):
114 ****************************************************************************
120 in_files= new cl_ustrings(2, 2, "input files");
121 options= new cl_options();
125 cl_app::~cl_app(void)
134 cl_app::init(int argc, char *argv[])
137 set_name("application");
139 proc_arguments(argc, argv);
140 class cl_cmdset *cmdset= new cl_cmdset();
142 build_cmdset(cmdset);
143 commander= new cl_commander(this, cmdset/*, sim*/);
160 if (sim->state & SIM_GO)
162 if (commander->input_avail())
163 done= commander->proc_input();
169 commander->wait_input();
170 done= commander->proc_input();
175 commander->wait_input();
176 done= commander->proc_input();
189 * Interpretation of parameters
193 print_help(char *name)
195 printf("%s: %s\n", name, VERSIONSTR);
196 printf("Usage: %s [-hHVvP] [-p prompt] [-t CPU] [-X freq[k|M]]\n"
197 " [-c file] [-s file] [-S optionlist]"
199 " [-Z portnum] [-k portnum]"
202 " [files...]\n", name);
206 " -t CPU Type of CPU: 51, C52, 251, etc.\n"
207 " -X freq[k|M] XTAL frequency\n"
208 " -c file Open command console on `file'\n"
210 " -Z portnum Use localhost:portnumber for command console\n"
211 " -k portnum Use localhost:portnum for serial I/O\n"
213 " -s file Connect serial interface to `file'\n"
214 " -S options `options' is a comma separated list of options according to\n"
215 " serial interface. Know options are:\n"
216 " in=file serial input will be read from file named `file'\n"
217 " out=file serial output will be written to `file'\n"
218 " -p prompt Specify string for prompt\n"
219 " -P Prompt is a null ('\\0') character\n"
221 " -v Print out version number\n"
222 " -H Print out types of known CPUs\n"
223 " -h Print out this help\n"
232 static const char *S_opts[]= {
234 /*[SOPT_OUT]=*/ "out",
239 cl_app::proc_arguments(int argc, char *argv[])
242 char opts[100], *cp, *subopts, *value;
243 char *cpu_type= NULL;
244 bool s_done= DD_FALSE, k_done= DD_FALSE;
245 bool S_i_done= DD_FALSE, S_o_done= DD_FALSE;
247 strcpy(opts, "c:C:p:PX:vVt:s:S:hHk:");
249 strcat(opts, "Z:r:");
252 while((c= getopt(argc, argv, opts)) != -1)
256 if (!options->set_value("console_on", this, optarg))
257 fprintf(stderr, "Warning: No \"console_on\" option found "
261 if (!options->set_value("config_file", this, optarg))
262 fprintf(stderr, "Warning: No \"config_file\" option found to set "
263 "parameter of -C as config file\n");
271 options->new_option(o= new cl_number_option(this, "port_number",
272 "Listen on port (-Z)"));
275 if (!options->set_value("port_number", this, strtol(optarg, NULL, 0)))
276 fprintf(stderr, "Warning: No \"port_number\" option found"
277 " to set parameter of -Z as pot number to listen on\n");
282 if (!options->set_value("prompt", this, optarg))
283 fprintf(stderr, "Warning: No \"prompt\" option found to set "
284 "parameter of -p as default prompt\n");
288 if (!options->set_value("null_prompt", this, bool(DD_TRUE)))
289 fprintf(stderr, "Warning: No \"null_prompt\" option found\n");
294 for (cp= optarg; *cp; *cp= toupper(*cp), cp++);
295 XTAL= strtod(optarg, &cp);
302 fprintf(stderr, "Xtal frequency must be greather than 0\n");
305 if (!options->set_value("xtal", this, XTAL))
306 fprintf(stderr, "Warning: No \"xtal\" option found to set "
307 "parameter of -X as XTAL frequency\n");
311 printf("%s: %s\n", argv[0], VERSIONSTR);
315 if (!options->set_value("debug", this, (bool)DD_TRUE))
316 fprintf(stderr, "Warning: No \"debug\" option found to set "
317 "by -V parameter\n");
323 cpu_type= case_string(case_upper, optarg);
324 if (!options->set_value("cpu_type", this, /*optarg*/cpu_type))
325 fprintf(stderr, "Warning: No \"cpu_type\" option found to set "
326 "parameter of -t as type of controller\n");
331 FILE *Ser_in, *Ser_out;
334 fprintf(stderr, "-s option can not be used more than once.\n");
338 if ((Ser_in= fopen(optarg, "r")) == NULL)
341 "Can't open `%s': %s\n", optarg, strerror(errno));
344 if (!options->set_value("serial_in_file", this, (void*)Ser_in))
345 fprintf(stderr, "Warning: No \"serial_in_file\" option found to set "
346 "parameter of -s as serial input file\n");
347 if ((Ser_out= fopen(optarg, "w")) == NULL)
350 "Can't open `%s': %s\n", optarg, strerror(errno));
353 if (!options->set_value("serial_out_file", this, Ser_out))
354 fprintf(stderr, "Warning: No \"serial_out_file\" option found "
355 "to set parameter of -s as serial output file\n");
359 // socket serial I/O by Alexandre Frey <Alexandre.Frey@trusted-logic.fr>
362 FILE *Ser_in, *Ser_out;
364 unsigned short serverport;
368 fprintf(stderr, "Serial input specified more than once.\n");
372 serverport = atoi(optarg);
373 sock= make_server_socket(serverport);
374 if (listen(sock, 1) < 0) {
375 fprintf(stderr, "Listen on port %d: %s\n", serverport,
379 fprintf(stderr, "Listening on port %d for a serial connection.\n",
381 if ((client_sock= accept(sock, NULL, NULL)) < 0) {
382 fprintf(stderr, "accept: %s\n", strerror(errno));
384 fprintf(stderr, "Serial connection established.\n");
386 if ((Ser_in= fdopen(client_sock, "r")) == NULL) {
387 fprintf(stderr, "Can't create input stream: %s\n", strerror(errno));
390 if (!options->set_value("serial_in_file", this, (void*)Ser_in))
391 fprintf(stderr, "Warning: No \"serial_in_file\" option found to "
392 "set parameter of -s as serial input file\n");
393 if ((Ser_out= fdopen(client_sock, "w")) == NULL) {
394 fprintf(stderr, "Can't create output stream: %s\n", strerror(errno));
397 if (!options->set_value("serial_out_file", this, Ser_out))
398 fprintf(stderr, "Warning: No \"serial_out_file\" option found "
399 "to set parameter of -s as serial output file\n");
405 while (*subopts != '\0')
406 switch (get_sub_opt(&subopts, S_opts, &value))
408 FILE *Ser_in, *Ser_out;
411 fprintf(stderr, "No value for -S in\n");
416 fprintf(stderr, "Serial input specified more than once.\n");
420 if ((Ser_in= fopen(value, "r")) == NULL)
423 "Can't open `%s': %s\n", value, strerror(errno));
426 if (!options->set_value("serial_in_file", this, (void*)Ser_in))
427 fprintf(stderr, "Warning: No \"serial_in_file\" option found "
428 "to set parameter of -s as serial input file\n");
432 fprintf(stderr, "No value for -S out\n");
437 fprintf(stderr, "Serial output specified more than once.\n");
440 if ((Ser_out= fopen(value, "w")) == NULL)
443 "Can't open `%s': %s\n", value, strerror(errno));
446 if (!options->set_value("serial_out_file", this, Ser_out))
447 fprintf(stderr, "Warning: No \"serial_out_file\" option found "
448 "to set parameter of -s as serial output file\n");
451 /* Unknown suboption. */
452 fprintf(stderr, "Unknown suboption `%s' for -S\n", value);
465 fprintf(stderr, "CPU type is not selectable\n");
469 while (cpus[i].type_str != NULL)
471 printf("%s\n", cpus[i].type_str);
479 fprintf(stderr, "Unknown option `-%c'.\n", optopt);
481 fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt);
488 for (i= optind; i < argc; i++)
489 in_files->add(argv[i]);
500 return(sim->get_uc());
504 /* Command handling */
507 cl_app::get_cmd(class cl_cmdline *cmdline)
514 * Messages to broadcast
519 cl_app::mem_cell_changed(class cl_m *mem, t_addr addr)
522 sim->mem_cell_changed(mem, addr);
527 /* Adding and removing components */
530 cl_app::set_simulator(class cl_sim *simulator)
539 cl_app::remove_simulator(void)
548 cl_app::build_cmdset(class cl_cmdset *cmdset)
551 class cl_super_cmd *super_cmd;
552 class cl_cmdset *cset;
555 cset= new cl_cmdset();
557 cset->add(cmd= new cl_conf_cmd("_no_parameters_", 0,
558 "conf Configuration",
559 "long help of conf"));
561 cset->add(cmd= new cl_conf_objects_cmd("objects", 0,
562 "conf objects Show object tree",
563 "long help of conf objects"));
566 cmdset->add(cmd= new cl_super_cmd("conf", 0,
567 "conf subcommand Information, see `conf' command for more help",
568 "long help of conf", cset));
571 cmd= new cl_help_cmd("help", 0,
572 "help [command] Help about command(s)",
573 "long help of help");
578 cmdset->add(cmd= new cl_quit_cmd("quit", 0,
580 "long help of quit"));
583 cmdset->add(cmd= new cl_kill_cmd("kill", 0,
584 "kill Shutdown simulator",
585 "long help of kill"));
588 cmdset->add(cmd= new cl_exec_cmd("exec", 0,
589 "exec file Execute commands from file",
590 "long help of exec"));
593 cmdset->add(cmd= new cl_expression_cmd("expression", 0,
594 "expression expr Evaluate the expression",
595 "long help of expression "));
597 cmd->add_name("let");
600 super_cmd= (class cl_super_cmd *)(cmdset->get_cmd("show"));
602 cset= super_cmd->commands;
604 cset= new cl_cmdset();
607 cset->add(cmd= new cl_show_copying_cmd("copying", 0,
608 "show copying Conditions for redistributing copies of uCsim",
609 "long help of show copying"));
611 cset->add(cmd= new cl_show_warranty_cmd("warranty", 0,
612 "show warranty Various kinds of warranty you do not have",
613 "long help of show warranty"));
615 cset->add(cmd= new cl_show_option_cmd("option", 0,
616 "show option [name] Show internal data of options",
617 "long help of show option"));
619 cset->add(cmd= new cl_show_error_cmd("error", 0,
620 "show error Show class of errors",
621 "long help of show error"));
626 cmdset->add(cmd= new cl_super_cmd("show", 0,
627 "show subcommand Generic command for showing things about the uCsim",
628 "long help of show", cset));
633 super_cmd= (class cl_super_cmd *)(cmdset->get_cmd("get"));
635 cset= super_cmd->commands;
637 cset= new cl_cmdset();
640 cset->add(cmd= new cl_get_option_cmd("option", 0,
641 "get option [name] Get value of an option",
642 "long help of get option"));
647 cmdset->add(cmd= new cl_super_cmd("get", 0,
648 "get subcommand Get, see `get' command for more help",
649 "long help of get", cset));
654 super_cmd= (class cl_super_cmd *)(cmdset->get_cmd("set"));
656 cset= super_cmd->commands;
658 cset= new cl_cmdset();
661 cset->add(cmd= new cl_set_option_cmd("option", 0,
662 "set option name|nr value\n"
663 " Set value of an option",
664 "long help of set option"));
666 cset->add(cmd= new cl_set_error_cmd("error", 0,
667 "set error error_name on|off|unset\n"
668 " Set value of an error",
669 "long help of set error"));
674 cmdset->add(cmd= new cl_super_cmd("set", 0,
675 "set subcommand Set, see `set' command for more help",
676 "long help of set", cset));
682 cl_app::mk_options(void)
686 options->new_option(o= new cl_bool_option(this, "null_prompt",
687 "Use \\0 as prompt (-P)"));
690 options->new_option(o= new cl_pointer_option(this, "serial_in_file",
691 "Input file for serial line (-s)"));
695 options->new_option(o= new cl_pointer_option(this, "serial_out_file",
696 "Output file for serial line (-s)"));
700 options->new_option(o= new cl_string_option(this, "prompt",
701 "String of prompt (-p)"));
704 options->new_option(o= new cl_bool_option(this, "debug",
705 "Print debug messages (-V)"));
708 options->new_option(o= new cl_string_option(this, "console_on",
709 "Open console on this file (-c)"));
713 options->new_option(o= new cl_string_option(this, "config_file",
714 "Execute this file at startup (-C)"));
718 options->new_option(o= new cl_float_option(this, "xtal",
719 "Frequency of XTAL in Hz"));
721 o->set_value(11059200.0);
723 options->new_option(o= new cl_string_option(this, "cpu_type",
724 "Type of controller (-t)"));
731 cl_app::dd_printf(char *format, ...)
738 va_start(ap, format);
739 int i= commander->dd_printf(format, ap);
745 cl_app::debug(char *format, ...)
752 va_start(ap, format);
753 int i= commander->debug(format, ap);