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
37 #include <sys/socket.h>
54 #include "cmdconfcl.h"
64 /*cl_option::cl_option(int atype, char sn, char *ln)
71 long_name= strdup(ln);
72 values= new cl_ustrings(1, 1);
75 cl_option::~cl_option(void)
83 cl_option::add_value(char *value)
86 return(values->count - 1);
90 cl_option::get_value(int index)
92 if (index > values->count - 1)
94 return((char*)(values->at(index)));
99 /*cl_options::cl_options(void):
107 ****************************************************************************
112 //options= new cl_options();
114 args= new cl_arguments();
115 in_files= new cl_ustrings(2, 2);
116 options= new cl_options();
120 cl_app::~cl_app(void)
132 cl_app::init(int argc, char *argv[])
135 set_name("application");
137 proc_arguments(argc, argv);
138 class cl_cmdset *cmdset= new cl_cmdset();
140 build_cmdset(cmdset);
141 commander= new cl_commander(this, cmdset/*, sim*/);
158 if (sim->state & SIM_GO)
160 if (commander->input_avail())
161 done= commander->proc_input();
167 commander->wait_input();
168 done= commander->proc_input();
173 commander->wait_input();
174 done= commander->proc_input();
187 * Interpretation of parameters
191 print_help(char *name)
193 printf("%s: %s\n", name, VERSIONSTR);
194 printf("Usage: %s [-hHVvP] [-p prompt] [-t CPU] [-X freq[k|M]]\n"
195 " [-c file] [-s file] [-S optionlist]"
197 " [-Z portnum] [-k portnum]"
200 " [files...]\n", name);
204 " -t CPU Type of CPU: 51, C52, 251, etc.\n"
205 " -X freq[k|M] XTAL frequency\n"
206 " -c file Open command console on `file'\n"
208 " -Z portnum Use localhost:portnumber for command console\n"
209 " -k portnum Use localhost:portnum for serial I/O\n"
211 " -s file Connect serial interface to `file'\n"
212 " -S options `options' is a comma separated list of options according to\n"
213 " serial interface. Know options are:\n"
214 " in=file serial input will be read from file named `file'\n"
215 " out=file serial output will be written to `file'\n"
216 " -p prompt Specify string for prompt\n"
217 " -P Prompt is a null ('\\0') character\n"
219 " -v Print out version number\n"
220 " -H Print out types of known CPUs\n"
221 " -h Print out this help\n"
230 static const char *S_opts[]= {
232 /*[SOPT_OUT]=*/ "out",
237 cl_app::proc_arguments(int argc, char *argv[])
240 char opts[100], *cp, *subopts, *value;
241 char *cpu_type= NULL;
243 strcpy(opts, "c:C:p:PX:vVt:s:S:hHk:");
245 strcat(opts, "Z:r:");
248 while((c= getopt(argc, argv, opts)) != -1)
252 //args->add(new cl_prg_arg('c', 0, optarg));
253 if (!options->set_value("console_on", this, optarg))
254 fprintf(stderr, "Warning: No \"console_on\" option found "
258 args->add(new cl_prg_arg(0, "Config", optarg));
263 args->add(new cl_prg_arg('Z', 0, (long)1));
264 if (!optarg || !isdigit(*optarg))
265 fprintf(stderr, "expected portnumber to follow -Z\n");
268 long l= strtol(optarg, &p, 0);
269 args->add(new cl_prg_arg(0, "Zport", l));
274 //args->add(new cl_prg_arg(0, "prompt", optarg));
275 //class cl_option *o= options->get_option("prompt", this);
276 if (/*o*/!options->set_value("prompt", this, optarg))
277 /*o->set_value(optarg);
279 fprintf(stderr, "Warning: No \"prompt\" option found to set "
280 "parameter of -p as default prompt\n");
284 args->add(new cl_prg_arg('P', 0, (long)1));
285 if (!options->set_value("null_prompt", this, bool(DD_TRUE)))
286 fprintf(stderr, "Warning: No \"null_prompt\" option found\n");
290 args->add(new cl_prg_arg('r', 0,
291 (long)strtol(optarg, NULL, 0)));
297 for (cp= optarg; *cp; *cp= toupper(*cp), cp++);
298 XTAL= strtod(optarg, &cp);
305 fprintf(stderr, "Xtal frequency must be greather than 0\n");
308 args->add(new cl_prg_arg('X', 0, XTAL));
312 printf("%s: %s\n", argv[0], VERSIONSTR);
316 args->add(new cl_prg_arg('V', 0, (long)1));
317 if (!options->set_value("debug", this, (bool)DD_TRUE))
318 fprintf(stderr, "Warning: No \"debug\" option found to set "
319 "by -V parameter\n");
324 cpu_type= strdup(optarg);
325 for (cp= cpu_type; *cp; *cp= toupper(*cp), cp++);
326 args->add(new cl_prg_arg('t', 0, cpu_type));
330 FILE *Ser_in, *Ser_out;
331 if (args->arg_avail('s'))
333 fprintf(stderr, "-s option can not be used more than once.\n");
336 args->add(new cl_prg_arg('s', 0, (long)1));
337 if ((Ser_in= fopen(optarg, "r")) == NULL)
340 "Can't open `%s': %s\n", optarg, strerror(errno));
343 args->add(new cl_prg_arg(0, "Ser_in", Ser_in));
344 if ((Ser_out= fopen(optarg, "w")) == NULL)
347 "Can't open `%s': %s\n", optarg, strerror(errno));
350 args->add(new cl_prg_arg(0, "Ser_out", Ser_out));
354 // socket serial I/O by Alexandre Frey <Alexandre.Frey@trusted-logic.fr>
357 FILE *Ser_in, *Ser_out;
359 unsigned short serverport;
362 if (args->arg_avail("Ser_in")) {
363 fprintf(stderr, "Serial input specified more than once.\n");
365 if (args->arg_avail("Ser_out")) {
366 fprintf(stderr, "Serial output 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 args->add(new cl_prg_arg(0, "Ser_in", Ser_in));
388 if ((Ser_out= fdopen(client_sock, "w")) == NULL) {
389 fprintf(stderr, "Can't create output stream: %s\n", strerror(errno));
392 args->add(new cl_prg_arg(0, "Ser_out", Ser_out));
398 while (*subopts != '\0')
399 switch (get_sub_opt(&subopts, S_opts, &value))
401 FILE *Ser_in, *Ser_out;
404 fprintf(stderr, "No value for -S in\n");
407 if (args->arg_avail("Ser_in"))
409 fprintf(stderr, "Serial input specified more than once.\n");
412 if ((Ser_in= fopen(value, "r")) == NULL)
415 "Can't open `%s': %s\n", value, strerror(errno));
418 args->add(new cl_prg_arg(0, "Ser_in", Ser_in));
422 fprintf(stderr, "No value for -S out\n");
425 if (args->arg_avail("Ser_out"))
427 fprintf(stderr, "Serial output specified more than once.\n");
430 if ((Ser_out= fopen(value, "w")) == NULL)
433 "Can't open `%s': %s\n", value, strerror(errno));
436 args->add(new cl_prg_arg(0, "Ser_out", Ser_out));
439 /* Unknown suboption. */
440 fprintf(stderr, "Unknown suboption `%s' for -S\n", value);
451 while (cpus_51[i].type_str != NULL)
453 printf("%s\n", cpus_51[i].type_str);
460 fprintf(stderr, "Unknown option `-%c'.\n", optopt);
462 fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt);
468 if (!args->arg_avail("prompt"))
469 args->add(new cl_prg_arg(0, "prompt", "> "));
471 for (i= optind; i < argc; i++)
472 in_files->add(argv[i]);
483 return(sim->get_uc());
487 /* Command handling */
490 cl_app::get_cmd(class cl_cmdline *cmdline)
497 * Messages to broadcast
501 cl_app::mem_cell_changed(class cl_mem *mem, t_addr addr)
504 sim->mem_cell_changed(mem, addr);
508 /* Adding and removing components */
511 cl_app::set_simulator(class cl_sim *simulator)
520 cl_app::remove_simulator(void)
529 cl_app::build_cmdset(class cl_cmdset *cmdset)
532 class cl_super_cmd *super_cmd;
533 class cl_cmdset *cset;
536 cset= new cl_cmdset();
538 cset->add(cmd= new cl_conf_cmd("_no_parameters_", 0,
539 "conf Configuration",
540 "long help of conf"));
542 cset->add(cmd= new cl_conf_addmem_cmd("addmem", 0,
545 "long help of conf addmem"));
548 cmdset->add(cmd= new cl_super_cmd("conf", 0,
549 "conf subcommand Information, see `conf' command for more help",
550 "long help of conf", cset));
553 cmd= new cl_help_cmd("help", 0,
554 "help [command] Help about command(s)",
555 "long help of help");
560 cmdset->add(cmd= new cl_quit_cmd("quit", 0,
562 "long help of quit"));
565 cmdset->add(cmd= new cl_kill_cmd("kill", 0,
566 "kill Shutdown simulator",
567 "long help of kill"));
570 cmdset->add(cmd= new cl_exec_cmd("exec", 0,
571 "exec file Execute commands from file",
572 "long help of exec"));
576 cset= new cl_cmdset();
578 cset->add(cmd= new cl_show_copying_cmd("copying", 0,
579 "show copying Conditions for redistributing copies of uCsim",
580 "long help of show copying"));
582 cset->add(cmd= new cl_show_warranty_cmd("warranty", 0,
583 "show warranty Various kinds of warranty you do not have",
584 "long help of show warranty"));
586 cset->add(cmd= new cl_show_option_cmd("option", 0,
587 "show option [name] Show internal data of options",
588 "long help of show option"));
591 cmdset->add(cmd= new cl_super_cmd("show", 0,
592 "show subcommand Generic command for showing things about the uCsim",
593 "long help of show", cset));
597 super_cmd= (class cl_super_cmd *)(cmdset->get_cmd("get"));
599 cset= super_cmd->commands;
601 cset= new cl_cmdset();
604 cset->add(cmd= new cl_get_option_cmd("option", 0,
605 "get option [name] Get value of an option",
606 "long help of get option"));
611 cmdset->add(cmd= new cl_super_cmd("get", 0,
612 "get subcommand Get, see `get' command for more help",
613 "long help of get", cset));
618 super_cmd= (class cl_super_cmd *)(cmdset->get_cmd("set"));
620 cset= super_cmd->commands;
622 cset= new cl_cmdset();
625 cset->add(cmd= new cl_set_option_cmd("option", 0,
626 "set option name|nr value\n"
627 " Set value of an option",
628 "long help of set option"));
633 cmdset->add(cmd= new cl_super_cmd("set", 0,
634 "set subcommand Set, see `set' command for more help",
635 "long help of set", cset));
641 cl_app::mk_options(void)
645 options->new_option(o= new cl_bool_option(this, "null_prompt",
646 "Use \\0 as prompt (-P)"));
648 options->new_option(o= new cl_bool_option(this, "stopit",
649 "Stop if interrupt accepted"));
651 options->new_option(o= new cl_string_option(this, "serial_in_file",
652 "Input file for serial line (-s)"));
654 options->new_option(o= new cl_string_option(this, "serial_out_file",
655 "Output file for serial line (-s)"));
657 options->new_option(o= new cl_string_option(this, "prompt",
658 "String of prompt (-p)"));
660 options->new_option(o= new cl_bool_option(this, "debug",
661 "Print debug messages (-V)"));
663 options->new_option(o= new cl_string_option(this, "console_on",
664 "Open console on this file (-c)"));