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 HEADER_SOCKET
58 #include "cmdconfcl.h"
68 /*cl_option::cl_option(int atype, char sn, char *ln)
75 long_name= strdup(ln);
76 values= new cl_ustrings(1, 1);
79 cl_option::~cl_option(void)
87 cl_option::add_value(char *value)
90 return(values->count - 1);
94 cl_option::get_value(int index)
96 if (index > values->count - 1)
98 return((char*)(values->at(index)));
101 /* List of options */
103 /*cl_options::cl_options(void):
111 ****************************************************************************
117 in_files= new cl_ustrings(2, 2, "input files");
118 options= new cl_options();
122 cl_app::~cl_app(void)
131 cl_app::init(int argc, char *argv[])
134 set_name("application");
136 proc_arguments(argc, argv);
137 class cl_cmdset *cmdset= new cl_cmdset();
139 build_cmdset(cmdset);
140 commander= new cl_commander(this, cmdset/*, sim*/);
157 if (sim->state & SIM_GO)
159 if (commander->input_avail())
160 done= commander->proc_input();
166 commander->wait_input();
167 done= commander->proc_input();
172 commander->wait_input();
173 done= commander->proc_input();
186 * Interpretation of parameters
190 print_help(char *name)
192 printf("%s: %s\n", name, VERSIONSTR);
193 printf("Usage: %s [-hHVvP] [-p prompt] [-t CPU] [-X freq[k|M]]\n"
194 " [-c file] [-s file] [-S optionlist]"
196 " [-Z portnum] [-k portnum]"
199 " [files...]\n", name);
203 " -t CPU Type of CPU: 51, C52, 251, etc.\n"
204 " -X freq[k|M] XTAL frequency\n"
205 " -c file Open command console on `file'\n"
207 " -Z portnum Use localhost:portnumber for command console\n"
208 " -k portnum Use localhost:portnum for serial I/O\n"
210 " -s file Connect serial interface to `file'\n"
211 " -S options `options' is a comma separated list of options according to\n"
212 " serial interface. Know options are:\n"
213 " in=file serial input will be read from file named `file'\n"
214 " out=file serial output will be written to `file'\n"
215 " -p prompt Specify string for prompt\n"
216 " -P Prompt is a null ('\\0') character\n"
218 " -v Print out version number\n"
219 " -H Print out types of known CPUs\n"
220 " -h Print out this help\n"
229 static const char *S_opts[]= {
231 /*[SOPT_OUT]=*/ "out",
236 cl_app::proc_arguments(int argc, char *argv[])
239 char opts[100], *cp, *subopts, *value;
240 char *cpu_type= NULL;
241 bool s_done= DD_FALSE, k_done= DD_FALSE;
242 bool S_i_done= DD_FALSE, S_o_done= DD_FALSE;
244 strcpy(opts, "c:C:p:PX:vVt:s:S:hHk:");
246 strcat(opts, "Z:r:");
249 while((c= getopt(argc, argv, opts)) != -1)
253 if (!options->set_value("console_on", this, optarg))
254 fprintf(stderr, "Warning: No \"console_on\" option found "
258 if (!options->set_value("config_file", this, optarg))
259 fprintf(stderr, "Warning: No \"config_file\" option found to set "
260 "parameter of -C as config file\n");
268 options->new_option(o= new cl_number_option(this, "port_number",
269 "Listen on port (-Z)"));
272 if (!options->set_value("port_number", this, strtol(optarg, NULL, 0)))
273 fprintf(stderr, "Warning: No \"port_number\" option found"
274 " to set parameter of -Z as pot number to listen on\n");
279 if (!options->set_value("prompt", this, optarg))
280 fprintf(stderr, "Warning: No \"prompt\" option found to set "
281 "parameter of -p as default prompt\n");
285 if (!options->set_value("null_prompt", this, bool(DD_TRUE)))
286 fprintf(stderr, "Warning: No \"null_prompt\" option found\n");
291 for (cp= optarg; *cp; *cp= toupper(*cp), cp++);
292 XTAL= strtod(optarg, &cp);
299 fprintf(stderr, "Xtal frequency must be greather than 0\n");
302 if (!options->set_value("xtal", this, XTAL))
303 fprintf(stderr, "Warning: No \"xtal\" option found to set "
304 "parameter of -X as XTAL frequency\n");
308 printf("%s: %s\n", argv[0], VERSIONSTR);
312 if (!options->set_value("debug", this, (bool)DD_TRUE))
313 fprintf(stderr, "Warning: No \"debug\" option found to set "
314 "by -V parameter\n");
320 cpu_type= case_string(case_upper, optarg);
321 if (!options->set_value("cpu_type", this, /*optarg*/cpu_type))
322 fprintf(stderr, "Warning: No \"cpu_type\" option found to set "
323 "parameter of -t as type of controller\n");
328 FILE *Ser_in, *Ser_out;
331 fprintf(stderr, "-s option can not be used more than once.\n");
335 if ((Ser_in= fopen(optarg, "r")) == NULL)
338 "Can't open `%s': %s\n", optarg, strerror(errno));
341 if (!options->set_value("serial_in_file", this, (void*)Ser_in))
342 fprintf(stderr, "Warning: No \"serial_in_file\" option found to set "
343 "parameter of -s as serial input file\n");
344 if ((Ser_out= fopen(optarg, "w")) == NULL)
347 "Can't open `%s': %s\n", optarg, strerror(errno));
350 if (!options->set_value("serial_out_file", this, Ser_out))
351 fprintf(stderr, "Warning: No \"serial_out_file\" option found "
352 "to set parameter of -s as serial output file\n");
356 // socket serial I/O by Alexandre Frey <Alexandre.Frey@trusted-logic.fr>
359 FILE *Ser_in, *Ser_out;
361 unsigned short serverport;
365 fprintf(stderr, "Serial input specified more than once.\n");
369 serverport = atoi(optarg);
370 sock= make_server_socket(serverport);
371 if (listen(sock, 1) < 0) {
372 fprintf(stderr, "Listen on port %d: %s\n", serverport,
376 fprintf(stderr, "Listening on port %d for a serial connection.\n",
378 if ((client_sock= accept(sock, NULL, NULL)) < 0) {
379 fprintf(stderr, "accept: %s\n", strerror(errno));
381 fprintf(stderr, "Serial connection established.\n");
383 if ((Ser_in= fdopen(client_sock, "r")) == NULL) {
384 fprintf(stderr, "Can't create input stream: %s\n", strerror(errno));
387 if (!options->set_value("serial_in_file", this, (void*)Ser_in))
388 fprintf(stderr, "Warning: No \"serial_in_file\" option found to "
389 "set parameter of -s as serial input file\n");
390 if ((Ser_out= fdopen(client_sock, "w")) == NULL) {
391 fprintf(stderr, "Can't create output stream: %s\n", strerror(errno));
394 if (!options->set_value("serial_out_file", this, Ser_out))
395 fprintf(stderr, "Warning: No \"serial_out_file\" option found "
396 "to set parameter of -s as serial output file\n");
402 while (*subopts != '\0')
403 switch (get_sub_opt(&subopts, S_opts, &value))
405 FILE *Ser_in, *Ser_out;
408 fprintf(stderr, "No value for -S in\n");
413 fprintf(stderr, "Serial input specified more than once.\n");
417 if ((Ser_in= fopen(value, "r")) == NULL)
420 "Can't open `%s': %s\n", value, strerror(errno));
423 if (!options->set_value("serial_in_file", this, (void*)Ser_in))
424 fprintf(stderr, "Warning: No \"serial_in_file\" option found "
425 "to set parameter of -s as serial input file\n");
429 fprintf(stderr, "No value for -S out\n");
434 fprintf(stderr, "Serial output specified more than once.\n");
437 if ((Ser_out= fopen(value, "w")) == NULL)
440 "Can't open `%s': %s\n", value, strerror(errno));
443 if (!options->set_value("serial_out_file", this, Ser_out))
444 fprintf(stderr, "Warning: No \"serial_out_file\" option found "
445 "to set parameter of -s as serial output file\n");
448 /* Unknown suboption. */
449 fprintf(stderr, "Unknown suboption `%s' for -S\n", value);
462 fprintf(stderr, "CPU type is not selectable\n");
466 while (cpus[i].type_str != NULL)
468 printf("%s\n", cpus[i].type_str);
476 fprintf(stderr, "Unknown option `-%c'.\n", optopt);
478 fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt);
485 for (i= optind; i < argc; i++)
486 in_files->add(argv[i]);
497 return(sim->get_uc());
501 /* Command handling */
504 cl_app::get_cmd(class cl_cmdline *cmdline)
511 * Messages to broadcast
516 cl_app::mem_cell_changed(class cl_m *mem, t_addr addr)
519 sim->mem_cell_changed(mem, addr);
524 /* Adding and removing components */
527 cl_app::set_simulator(class cl_sim *simulator)
536 cl_app::remove_simulator(void)
545 cl_app::build_cmdset(class cl_cmdset *cmdset)
548 class cl_super_cmd *super_cmd;
549 class cl_cmdset *cset;
552 cset= new cl_cmdset();
554 cset->add(cmd= new cl_conf_cmd("_no_parameters_", 0,
555 "conf Configuration",
556 "long help of conf"));
558 cset->add(cmd= new cl_conf_objects_cmd("objects", 0,
559 "conf objects Show object tree",
560 "long help of conf objects"));
563 cmdset->add(cmd= new cl_super_cmd("conf", 0,
564 "conf subcommand Information, see `conf' command for more help",
565 "long help of conf", cset));
568 cmd= new cl_help_cmd("help", 0,
569 "help [command] Help about command(s)",
570 "long help of help");
575 cmdset->add(cmd= new cl_quit_cmd("quit", 0,
577 "long help of quit"));
580 cmdset->add(cmd= new cl_kill_cmd("kill", 0,
581 "kill Shutdown simulator",
582 "long help of kill"));
585 cmdset->add(cmd= new cl_exec_cmd("exec", 0,
586 "exec file Execute commands from file",
587 "long help of exec"));
590 cmdset->add(cmd= new cl_expression_cmd("expression", 0,
591 "expression expr Evaluate the expression",
592 "long help of expression "));
594 cmd->add_name("let");
597 super_cmd= (class cl_super_cmd *)(cmdset->get_cmd("show"));
599 cset= super_cmd->commands;
601 cset= new cl_cmdset();
604 cset->add(cmd= new cl_show_copying_cmd("copying", 0,
605 "show copying Conditions for redistributing copies of uCsim",
606 "long help of show copying"));
608 cset->add(cmd= new cl_show_warranty_cmd("warranty", 0,
609 "show warranty Various kinds of warranty you do not have",
610 "long help of show warranty"));
612 cset->add(cmd= new cl_show_option_cmd("option", 0,
613 "show option [name] Show internal data of options",
614 "long help of show option"));
616 cset->add(cmd= new cl_show_error_cmd("error", 0,
617 "show error Show class of errors",
618 "long help of show error"));
623 cmdset->add(cmd= new cl_super_cmd("show", 0,
624 "show subcommand Generic command for showing things about the uCsim",
625 "long help of show", cset));
630 super_cmd= (class cl_super_cmd *)(cmdset->get_cmd("get"));
632 cset= super_cmd->commands;
634 cset= new cl_cmdset();
637 cset->add(cmd= new cl_get_option_cmd("option", 0,
638 "get option [name] Get value of an option",
639 "long help of get option"));
644 cmdset->add(cmd= new cl_super_cmd("get", 0,
645 "get subcommand Get, see `get' command for more help",
646 "long help of get", cset));
651 super_cmd= (class cl_super_cmd *)(cmdset->get_cmd("set"));
653 cset= super_cmd->commands;
655 cset= new cl_cmdset();
658 cset->add(cmd= new cl_set_option_cmd("option", 0,
659 "set option name|nr value\n"
660 " Set value of an option",
661 "long help of set option"));
663 cset->add(cmd= new cl_set_error_cmd("error", 0,
664 "set error error_name on|off|unset\n"
665 " Set value of an error",
666 "long help of set error"));
671 cmdset->add(cmd= new cl_super_cmd("set", 0,
672 "set subcommand Set, see `set' command for more help",
673 "long help of set", cset));
679 cl_app::mk_options(void)
683 options->new_option(o= new cl_bool_option(this, "null_prompt",
684 "Use \\0 as prompt (-P)"));
687 options->new_option(o= new cl_pointer_option(this, "serial_in_file",
688 "Input file for serial line (-s)"));
692 options->new_option(o= new cl_pointer_option(this, "serial_out_file",
693 "Output file for serial line (-s)"));
697 options->new_option(o= new cl_string_option(this, "prompt",
698 "String of prompt (-p)"));
701 options->new_option(o= new cl_bool_option(this, "debug",
702 "Print debug messages (-V)"));
705 options->new_option(o= new cl_string_option(this, "console_on",
706 "Open console on this file (-c)"));
710 options->new_option(o= new cl_string_option(this, "config_file",
711 "Execute this file at startup (-C)"));
715 options->new_option(o= new cl_float_option(this, "xtal",
716 "Frequency of XTAL in Hz"));
718 o->set_value(11059200.0);
720 options->new_option(o= new cl_string_option(this, "cpu_type",
721 "Type of controller (-t)"));
728 cl_app::dd_printf(char *format, ...)
735 va_start(ap, format);
736 int i= commander->dd_printf(format, ap);
742 cl_app::debug(char *format, ...)
749 va_start(ap, format);
750 int i= commander->debug(format, ap);