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>
53 #include "cmdconfcl.h"
61 /*cl_option::cl_option(int atype, char sn, char *ln)
68 long_name= strdup(ln);
69 values= new cl_ustrings(1, 1);
72 cl_option::~cl_option(void)
80 cl_option::add_value(char *value)
83 return(values->count - 1);
87 cl_option::get_value(int index)
89 if (index > values->count - 1)
91 return((char*)(values->at(index)));
96 /*cl_options::cl_options(void):
104 ****************************************************************************
109 //options= new cl_options();
111 args= new cl_arguments();
112 in_files= new cl_ustrings(2, 2);
116 cl_app::~cl_app(void)
127 cl_app::init(int argc, char *argv[])
130 proc_arguments(argc, argv);
131 class cl_cmdset *cmdset= new cl_cmdset();
133 build_cmdset(cmdset);
134 commander= new cl_commander(this, cmdset/*, sim*/);
151 if (sim->state & SIM_GO)
153 if (commander->input_avail())
154 done= commander->proc_input();
160 commander->wait_input();
161 done= commander->proc_input();
166 commander->wait_input();
167 done= commander->proc_input();
180 * Interpretation of parameters
184 print_help(char *name)
186 printf("%s: %s\n", name, VERSIONSTR);
187 printf("Usage: %s [-hHVvP] [-p prompt] [-t CPU] [-X freq[k|M]]\n"
188 " [-c file] [-s file] [-S optionlist]"
190 " [-Z portnum] [-k portnum]"
193 " [files...]\n", name);
197 " -t CPU Type of CPU: 51, C52, 251, etc.\n"
198 " -X freq[k|M] XTAL frequency\n"
199 " -c file Open command console on `file'\n"
201 " -Z portnum Use localhost:portnumber for command console\n"
202 " -k portnum Use localhost:portnum for serial I/O\n"
204 " -s file Connect serial interface to `file'\n"
205 " -S options `options' is a comma separated list of options\n"
206 " according to serial interface. Know options are:\n"
207 " in=file serial input will be read from file named `file'\n"
208 " out=file serial output will be written to `file'\n"
209 " -p prompt Specify string for prompt\n"
210 " -P Prompt is a null ('\\0') character\n"
212 " -v Print out version number\n"
213 " -H Print out types of known CPUs\n"
214 " -h Print out this help\n"
223 static const char *S_opts[]= {
225 /*[SOPT_OUT]=*/ "out",
230 cl_app::proc_arguments(int argc, char *argv[])
233 char opts[100], *cp, *subopts, *value;
234 char *cpu_type= NULL;
236 strcpy(opts, "c:C:p:PX:vVt:s:S:hHk:");
238 strcat(opts, "Z:r:");
241 while((c= getopt(argc, argv, opts)) != -1)
245 args->add(new cl_prg_arg('c', 0, optarg));
248 args->add(new cl_prg_arg(0, "Config", optarg));
253 args->add(new cl_prg_arg('Z', 0, (long)1));
254 if (!optarg || !isdigit(*optarg))
255 fprintf(stderr, "expected portnumber to follow -Z\n");
258 long l= strtol(optarg, &p, 0);
259 args->add(new cl_prg_arg(0, "Zport", l));
264 args->add(new cl_prg_arg(0, "prompt", optarg));
267 args->add(new cl_prg_arg('P', 0, (long)1));
271 args->add(new cl_prg_arg('r', 0,
272 (long)strtol(optarg, NULL, 0)));
278 for (cp= optarg; *cp; *cp= toupper(*cp), cp++);
279 XTAL= strtod(optarg, &cp);
286 fprintf(stderr, "Xtal frequency must be greather than 0\n");
289 args->add(new cl_prg_arg('X', 0, XTAL));
293 printf("%s: %s\n", argv[0], VERSIONSTR);
297 args->add(new cl_prg_arg('V', 0, (long)1));
302 cpu_type= strdup(optarg);
303 for (cp= cpu_type; *cp; *cp= toupper(*cp), cp++);
304 args->add(new cl_prg_arg('t', 0, cpu_type));
308 FILE *Ser_in, *Ser_out;
309 if (args->arg_avail('s'))
311 fprintf(stderr, "-s option can not be used more than once.\n");
314 args->add(new cl_prg_arg('s', 0, (long)1));
315 if ((Ser_in= fopen(optarg, "r")) == NULL)
318 "Can't open `%s': %s\n", optarg, strerror(errno));
321 args->add(new cl_prg_arg(0, "Ser_in", Ser_in));
322 if ((Ser_out= fopen(optarg, "w")) == NULL)
325 "Can't open `%s': %s\n", optarg, strerror(errno));
328 args->add(new cl_prg_arg(0, "Ser_out", Ser_out));
332 // socket serial I/O by Alexandre Frey <Alexandre.Frey@trusted-logic.fr>
335 FILE *Ser_in, *Ser_out;
337 unsigned short serverport;
340 if (args->arg_avail("Ser_in")) {
341 fprintf(stderr, "Serial input specified more than once.\n");
343 if (args->arg_avail("Ser_out")) {
344 fprintf(stderr, "Serial output specified more than once.\n");
347 serverport = atoi(optarg);
348 sock= make_server_socket(serverport);
349 if (listen(sock, 1) < 0) {
350 fprintf(stderr, "Listen on port %d: %s\n", serverport,
354 fprintf(stderr, "Listening on port %d for a serial connection.\n",
356 if ((client_sock= accept(sock, NULL, NULL)) < 0) {
357 fprintf(stderr, "accept: %s\n", strerror(errno));
359 fprintf(stderr, "Serial connection established.\n");
361 if ((Ser_in= fdopen(client_sock, "r")) == NULL) {
362 fprintf(stderr, "Can't create input stream: %s\n", strerror(errno));
365 args->add(new cl_prg_arg(0, "Ser_in", Ser_in));
366 if ((Ser_out= fdopen(client_sock, "w")) == NULL) {
367 fprintf(stderr, "Can't create output stream: %s\n", strerror(errno));
370 args->add(new cl_prg_arg(0, "Ser_out", Ser_out));
376 while (*subopts != '\0')
377 switch (get_sub_opt(&subopts, S_opts, &value))
379 FILE *Ser_in, *Ser_out;
382 fprintf(stderr, "No value for -S in\n");
385 if (args->arg_avail("Ser_in"))
387 fprintf(stderr, "Serial input specified more than once.\n");
390 if ((Ser_in= fopen(value, "r")) == NULL)
393 "Can't open `%s': %s\n", value, strerror(errno));
396 args->add(new cl_prg_arg(0, "Ser_in", Ser_in));
400 fprintf(stderr, "No value for -S out\n");
403 if (args->arg_avail("Ser_out"))
405 fprintf(stderr, "Serial output specified more than once.\n");
408 if ((Ser_out= fopen(value, "w")) == NULL)
411 "Can't open `%s': %s\n", value, strerror(errno));
414 args->add(new cl_prg_arg(0, "Ser_out", Ser_out));
417 /* Unknown suboption. */
418 fprintf(stderr, "Unknown suboption `%s' for -S\n", value);
429 while (cpus_51[i].type_str != NULL)
431 printf("%s\n", cpus_51[i].type_str);
438 fprintf(stderr, "Unknown option `-%c'.\n", optopt);
440 fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt);
446 if (!args->arg_avail("prompt"))
447 args->add(new cl_prg_arg(0, "prompt", "> "));
449 for (i= optind; i < argc; i++)
450 in_files->add(argv[i]);
456 /* Command handling */
459 cl_app::get_cmd(class cl_cmdline *cmdline)
466 * Messages to broadcast
470 cl_app::mem_cell_changed(class cl_mem *mem, t_addr addr)
473 sim->mem_cell_changed(mem, addr);
477 /* Adding and removing components */
480 cl_app::set_simulator(class cl_sim *simulator)
489 cl_app::remove_simulator(void)
498 cl_app::build_cmdset(class cl_cmdset *cmdset)
501 class cl_cmdset *cset;
504 cset= new cl_cmdset();
506 cset->add(cmd= new cl_conf_cmd("_no_parameters_", 0,
507 "conf Configuration",
508 "long help of conf"));
510 cset->add(cmd= new cl_conf_addmem_cmd("addmem", 0,
513 "long help of conf addmem"));
516 cmdset->add(cmd= new cl_super_cmd("conf", 0,
517 "conf subcommand Information, see `conf' command for more help",
518 "long help of conf", cset));
521 cmd= new cl_help_cmd("help", 0,
522 "help [command] Help about command(s)",
523 "long help of help");
528 cmdset->add(cmd= new cl_quit_cmd("quit", 0,
530 "long help of quit"));
533 cmdset->add(cmd= new cl_kill_cmd("kill", 0,
534 "kill Shutdown simulator",
535 "long help of kill"));
539 cset= new cl_cmdset();
541 cset->add(cmd= new cl_show_copying_cmd("copying", 0,
542 "show copying Conditions for redistributing copies of uCsim",
543 "long help of show copying"));
545 cset->add(cmd= new cl_show_warranty_cmd("warranty", 0,
546 "show warranty Various kinds of warranty you do not have",
547 "long help of show warranty"));
550 cmdset->add(cmd= new cl_super_cmd("show", 0,
551 "show subcommand Generic command for showing things about the uCsim",
552 "long help of show", cset));
557 /* End of sim.src/app.cc */