X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=sim%2Fucsim%2Fapp.cc;h=241454730371d2018a77eee8122d2de0653ea4f1;hb=34d14506fd0c12ee5434fec0f9dec27753e1aab5;hp=3c2a4d847569bd4cd3ff97778e3c39931be65221;hpb=0e1bba0730e55b3d40c1c644d94befc99c87270d;p=fw%2Fsdcc diff --git a/sim/ucsim/app.cc b/sim/ucsim/app.cc index 3c2a4d84..24145473 100644 --- a/sim/ucsim/app.cc +++ b/sim/ucsim/app.cc @@ -2,7 +2,7 @@ * Simulator of microcontrollers (app.cc) * * Copyright (C) 2001,01 Drotos Daniel, Talker Bt. - * + * * To contact author send email to drdani@mazsola.iit.uni-miskolc.hu * */ @@ -29,12 +29,15 @@ Software Foundation, 59 Temple Place - Suite 330, Boston, MA #include #include -#include +#include +#ifdef HAVE_UNISTD_H +# include +#endif #ifdef HAVE_GETOPT_H # include #endif #ifdef SOCKET_AVAIL -#include +# include HEADER_SOCKET #endif #include #include @@ -44,6 +47,7 @@ Software Foundation, 59 Temple Place - Suite 330, Boston, MA #include "utils.h" #include "appcl.h" #include "optioncl.h" +#include "globals.h" // sim.src #include "simcl.h" @@ -55,6 +59,11 @@ Software Foundation, 59 Temple Place - Suite 330, Boston, MA #include "showcl.h" #include "getcl.h" #include "setcl.h" +#ifdef _WIN32 +#include "newcmdwin32cl.h" +#else +#include "newcmdposixcl.h" +#endif /* @@ -109,21 +118,16 @@ cl_option::get_value(int index) cl_app::cl_app(void) { - //options= new cl_options(); sim= 0; - args= new cl_arguments(); - in_files= new cl_ustrings(2, 2); + in_files= new cl_ustrings(2, 2, "input files"); options= new cl_options(); going= 1; } cl_app::~cl_app(void) { - //delete options; remove_simulator(); delete commander; - //delete cmdset; - delete args; delete in_files; delete options; } @@ -151,28 +155,28 @@ cl_app::run(void) int done= 0; while (!done && - going) + going) { if (sim) - { - if (sim->state & SIM_GO) - { - if (commander->input_avail()) - done= commander->proc_input(); - else - sim->step(); - } - else - { - commander->wait_input(); - done= commander->proc_input(); - } - } + { + if (sim->state & SIM_GO) + { + if (commander->input_avail()) + done= commander->proc_input(); + else + sim->step(); + } + else + { + commander->wait_input(); + done= commander->proc_input(); + } + } else - { - commander->wait_input(); - done= commander->proc_input(); - } + { + commander->wait_input(); + done= commander->proc_input(); + } } return(0); } @@ -188,16 +192,16 @@ cl_app::done(void) */ static void -print_help(char *name) +print_help(const char *name) { printf("%s: %s\n", name, VERSIONSTR); printf("Usage: %s [-hHVvP] [-p prompt] [-t CPU] [-X freq[k|M]]\n" - " [-c file] [-s file] [-S optionlist]" + " [-c file] [-s file] [-S optionlist]" #ifdef SOCKET_AVAIL - " [-Z portnum] [-k portnum]" + " [-Z portnum] [-k portnum]" #endif - "\n" - " [files...]\n", name); + "\n" + " [files...]\n", name); printf ( "Options:\n" @@ -239,234 +243,312 @@ cl_app::proc_arguments(int argc, char *argv[]) int i, c; char opts[100], *cp, *subopts, *value; char *cpu_type= NULL; + bool s_done= DD_FALSE, k_done= DD_FALSE; + bool S_i_done= DD_FALSE, S_o_done= DD_FALSE; strcpy(opts, "c:C:p:PX:vVt:s:S:hHk:"); #ifdef SOCKET_AVAIL strcat(opts, "Z:r:"); #endif - //int opterr= 0; + while((c= getopt(argc, argv, opts)) != -1) switch (c) { case 'c': - //args->add(new cl_prg_arg('c', 0, optarg)); - if (!options->set_value("console_on", this, optarg)) - fprintf(stderr, "Warning: No \"console_on\" option found " - "to set by -c\n"); - break; + if (!options->set_value("console_on", this, optarg)) + fprintf(stderr, "Warning: No \"console_on\" option found " + "to set by -c\n"); + break; case 'C': - args->add(new cl_prg_arg(0, "Config", optarg)); - break; + if (!options->set_value("config_file", this, optarg)) + fprintf(stderr, "Warning: No \"config_file\" option found to set " + "parameter of -C as config file\n"); + break; #ifdef SOCKET_AVAIL - case 'Z': - // By Sandeep - args->add(new cl_prg_arg('Z', 0, (long)1)); - if (!optarg || !isdigit(*optarg)) - fprintf(stderr, "expected portnumber to follow -Z\n"); - else { - char *p; - long l= strtol(optarg, &p, 0); - args->add(new cl_prg_arg(0, "Zport", l)); - } - break; + case 'Z': case 'r': + { + // By Sandeep + // Modified by DD + class cl_option *o; + options->new_option(o= new cl_number_option(this, "port_number", + "Listen on port (-Z)")); + o->init(); + o->hide(); + if (!options->set_value("port_number", this, strtol(optarg, NULL, 0))) + fprintf(stderr, "Warning: No \"port_number\" option found" + " to set parameter of -Z as pot number to listen on\n"); + break; + } #endif case 'p': { - //args->add(new cl_prg_arg(0, "prompt", optarg)); - //class cl_option *o= options->get_option("prompt", this); - if (/*o*/!options->set_value("prompt", this, optarg)) - /*o->set_value(optarg); - else*/ - fprintf(stderr, "Warning: No \"prompt\" option found to set " - "parameter of -p as default prompt\n"); - break; + if (!options->set_value("prompt", this, optarg)) + fprintf(stderr, "Warning: No \"prompt\" option found to set " + "parameter of -p as default prompt\n"); + break; } case 'P': - args->add(new cl_prg_arg('P', 0, (long)1)); - if (!options->set_value("null_prompt", this, bool(DD_TRUE))) - fprintf(stderr, "Warning: No \"null_prompt\" option found\n"); - break; -#ifdef SOCKET_AVAIL - case 'r': - args->add(new cl_prg_arg('r', 0, - (long)strtol(optarg, NULL, 0))); - break; -#endif + if (!options->set_value("null_prompt", this, bool(DD_TRUE))) + fprintf(stderr, "Warning: No \"null_prompt\" option found\n"); + break; case 'X': - { - double XTAL; - for (cp= optarg; *cp; *cp= toupper(*cp), cp++); - XTAL= strtod(optarg, &cp); - if (*cp == 'K') - XTAL*= 1e3; - if (*cp == 'M') - XTAL*= 1e6; - if (XTAL == 0) - { - fprintf(stderr, "Xtal frequency must be greather than 0\n"); - exit(1); - } - args->add(new cl_prg_arg('X', 0, XTAL)); - break; - } + { + double XTAL; + for (cp= optarg; *cp; *cp= toupper(*cp), cp++); + XTAL= strtod(optarg, &cp); + if (*cp == 'K') + XTAL*= 1e3; + if (*cp == 'M') + XTAL*= 1e6; + if (XTAL == 0) + { + fprintf(stderr, "Xtal frequency must be greather than 0\n"); + exit(1); + } + if (!options->set_value("xtal", this, XTAL)) + fprintf(stderr, "Warning: No \"xtal\" option found to set " + "parameter of -X as XTAL frequency\n"); + break; + } case 'v': - printf("%s: %s\n", argv[0], VERSIONSTR); + printf("%s: %s\n", argv[0], VERSIONSTR); exit(0); break; case 'V': - args->add(new cl_prg_arg('V', 0, (long)1)); - if (!options->set_value("debug", this, (bool)DD_TRUE)) - fprintf(stderr, "Warning: No \"debug\" option found to set " - "by -V parameter\n"); - break; + if (!options->set_value("debug", this, (bool)DD_TRUE)) + fprintf(stderr, "Warning: No \"debug\" option found to set " + "by -V parameter\n"); + break; case 't': - if (cpu_type) - free(cpu_type); - cpu_type= strdup(optarg); - for (cp= cpu_type; *cp; *cp= toupper(*cp), cp++); - args->add(new cl_prg_arg('t', 0, cpu_type)); - break; + { + if (cpu_type) + free(cpu_type); + cpu_type= case_string(case_upper, optarg); + if (!options->set_value("cpu_type", this, /*optarg*/cpu_type)) + fprintf(stderr, "Warning: No \"cpu_type\" option found to set " + "parameter of -t as type of controller\n"); + break; + } case 's': { - FILE *Ser_in, *Ser_out; - if (args->arg_avail('s')) - { - fprintf(stderr, "-s option can not be used more than once.\n"); - break; - } - args->add(new cl_prg_arg('s', 0, (long)1)); - if ((Ser_in= fopen(optarg, "r")) == NULL) - { - fprintf(stderr, - "Can't open `%s': %s\n", optarg, strerror(errno)); - return(4); - } - args->add(new cl_prg_arg(0, "Ser_in", Ser_in)); - if ((Ser_out= fopen(optarg, "w")) == NULL) - { - fprintf(stderr, - "Can't open `%s': %s\n", optarg, strerror(errno)); - return(4); - } - args->add(new cl_prg_arg(0, "Ser_out", Ser_out)); - break; +#ifdef _WIN32 + /* TODO: this code should be probably used for all platforms? */ + FILE *Ser; + if (s_done) + { + fprintf(stderr, "-s option can not be used more than once.\n"); + break; + } + s_done= DD_TRUE; + if ((Ser= fopen(optarg, "r+")) == NULL) + { + fprintf(stderr, + "Can't open `%s': %s\n", optarg, strerror(errno)); + return(4); + } + if (!options->set_value("serial_in_file", this, Ser)) + fprintf(stderr, "Warning: No \"serial_in_file\" option found to set " + "parameter of -s as serial input file\n"); + if (!options->set_value("serial_out_file", this, Ser)) + fprintf(stderr, "Warning: No \"serial_out_file\" option found " + "to set parameter of -s as serial output file\n"); +#else + FILE *Ser_in, *Ser_out; + if (s_done) + { + fprintf(stderr, "-s option can not be used more than once.\n"); + break; + } + s_done= DD_TRUE; + if ((Ser_in= fopen(optarg, "r")) == NULL) + { + fprintf(stderr, + "Can't open `%s': %s\n", optarg, strerror(errno)); + return(4); + } + if (!options->set_value("serial_in_file", this, Ser_in)) + fprintf(stderr, "Warning: No \"serial_in_file\" option found to set " + "parameter of -s as serial input file\n"); + if ((Ser_out= fopen(optarg, "w")) == NULL) + { + fprintf(stderr, + "Can't open `%s': %s\n", optarg, strerror(errno)); + return(4); + } + if (!options->set_value("serial_out_file", this, Ser_out)) + fprintf(stderr, "Warning: No \"serial_out_file\" option found " + "to set parameter of -s as serial output file\n"); +#endif + break; } #ifdef SOCKET_AVAIL // socket serial I/O by Alexandre Frey case 'k': - { - FILE *Ser_in, *Ser_out; - int sock; - unsigned short serverport; - int client_sock; - - if (args->arg_avail("Ser_in")) { - fprintf(stderr, "Serial input specified more than once.\n"); - } - if (args->arg_avail("Ser_out")) { - fprintf(stderr, "Serial output specified more than once.\n"); - } - - serverport = atoi(optarg); - sock= make_server_socket(serverport); - if (listen(sock, 1) < 0) { - fprintf(stderr, "Listen on port %d: %s\n", serverport, - strerror(errno)); - return (4); - } - fprintf(stderr, "Listening on port %d for a serial connection.\n", - serverport); - if ((client_sock= accept(sock, NULL, NULL)) < 0) { - fprintf(stderr, "accept: %s\n", strerror(errno)); - } - fprintf(stderr, "Serial connection established.\n"); - - if ((Ser_in= fdopen(client_sock, "r")) == NULL) { - fprintf(stderr, "Can't create input stream: %s\n", strerror(errno)); - return (4); - } - args->add(new cl_prg_arg(0, "Ser_in", Ser_in)); - if ((Ser_out= fdopen(client_sock, "w")) == NULL) { - fprintf(stderr, "Can't create output stream: %s\n", strerror(errno)); - return (4); - } - args->add(new cl_prg_arg(0, "Ser_out", Ser_out)); - break; - } + { + FILE *Ser_in, *Ser_out; + UCSOCKET_T sock; + unsigned short serverport; + UCSOCKET_T client_sock; + + if (k_done) + { + fprintf(stderr, "Serial input specified more than once.\n"); + } + k_done= DD_TRUE; + + serverport = atoi(optarg); + sock = make_server_socket(serverport); +#ifdef _WIN32 + if (SOCKET_ERROR == listen((SOCKET)sock, 1)) + { + fprintf(stderr, "Listen on port %d: %d\n", serverport, + WSAGetLastError()); + return (4); + } + fprintf(stderr, "Listening on port %d for a serial connection.\n", + serverport); + if (INVALID_SOCKET == (client_sock = accept(sock, NULL, NULL))) + { + fprintf(stderr, "accept: %d\n", WSAGetLastError()); + return (4); + } + fprintf(stderr, "Serial connection established.\n"); + + int fh = _open_osfhandle((intptr_t)client_sock, 0); + if (-1 == fh) + { + perror("_open_osfhandle"); + return (4); + } + if (NULL == (Ser_in = fdopen(fh, "r"))) + { + fprintf(stderr, "Can't create input stream: %s\n", strerror(errno)); + return (4); + } + + fh = _open_osfhandle((intptr_t)client_sock, 0); + if (-1 == fh) + { + perror("_open_osfhandle"); + } + if (NULL == (Ser_out = fdopen(fh, "w"))) { + fprintf(stderr, "Can't create output stream: %s\n", strerror(errno)); + return (4); + } +#else + if (listen(sock, 1) < 0) { + fprintf(stderr, "Listen on port %d: %s\n", serverport, + strerror(errno)); + return (4); + } + fprintf(stderr, "Listening on port %d for a serial connection.\n", + serverport); + if ((client_sock= accept(sock, NULL, NULL)) < 0) { + fprintf(stderr, "accept: %s\n", strerror(errno)); + } + fprintf(stderr, "Serial connection established.\n"); + + if ((Ser_in= fdopen(client_sock, "r")) == NULL) { + fprintf(stderr, "Can't create input stream: %s\n", strerror(errno)); + return (4); + } + if ((Ser_out= fdopen(client_sock, "w")) == NULL) { + fprintf(stderr, "Can't create output stream: %s\n", strerror(errno)); + return (4); + } +#endif + if (!options->set_value("serial_in_file", this, (void*)Ser_in)) + fprintf(stderr, "Warning: No \"serial_in_file\" option found to " + "set parameter of -s as serial input file\n"); + if (!options->set_value("serial_out_file", this, Ser_out)) + fprintf(stderr, "Warning: No \"serial_out_file\" option found " + "to set parameter of -s as serial output file\n"); + break; + } #endif case 'S': - subopts= optarg; - while (*subopts != '\0') - switch (get_sub_opt(&subopts, S_opts, &value)) - { - FILE *Ser_in, *Ser_out; - case SOPT_IN: - if (value == NULL) { - fprintf(stderr, "No value for -S in\n"); - exit(1); - } - if (args->arg_avail("Ser_in")) - { - fprintf(stderr, "Serial input specified more than once.\n"); - break; - } - if ((Ser_in= fopen(value, "r")) == NULL) - { - fprintf(stderr, - "Can't open `%s': %s\n", value, strerror(errno)); - exit(4); - } - args->add(new cl_prg_arg(0, "Ser_in", Ser_in)); - break; - case SOPT_OUT: - if (value == NULL) { - fprintf(stderr, "No value for -S out\n"); - exit(1); - } - if (args->arg_avail("Ser_out")) - { - fprintf(stderr, "Serial output specified more than once.\n"); - break; - } - if ((Ser_out= fopen(value, "w")) == NULL) - { - fprintf(stderr, - "Can't open `%s': %s\n", value, strerror(errno)); - exit(4); - } - args->add(new cl_prg_arg(0, "Ser_out", Ser_out)); - break; - default: - /* Unknown suboption. */ - fprintf(stderr, "Unknown suboption `%s' for -S\n", value); - exit(1); - break; - } - break; + subopts= optarg; + while (*subopts != '\0') + switch (get_sub_opt(&subopts, S_opts, &value)) + { + FILE *Ser_in, *Ser_out; + case SOPT_IN: + if (value == NULL) { + fprintf(stderr, "No value for -S in\n"); + exit(1); + } + if (S_i_done) + { + fprintf(stderr, "Serial input specified more than once.\n"); + break; + } + S_i_done= DD_TRUE; + if ((Ser_in= fopen(value, "r")) == NULL) + { + fprintf(stderr, + "Can't open `%s': %s\n", value, strerror(errno)); + exit(4); + } + if (!options->set_value("serial_in_file", this, (void*)Ser_in)) + fprintf(stderr, "Warning: No \"serial_in_file\" option found " + "to set parameter of -s as serial input file\n"); + break; + case SOPT_OUT: + if (value == NULL) { + fprintf(stderr, "No value for -S out\n"); + exit(1); + } + if (S_o_done) + { + fprintf(stderr, "Serial output specified more than once.\n"); + break; + } + if ((Ser_out= fopen(value, "w")) == NULL) + { + fprintf(stderr, + "Can't open `%s': %s\n", value, strerror(errno)); + exit(4); + } + if (!options->set_value("serial_out_file", this, Ser_out)) + fprintf(stderr, "Warning: No \"serial_out_file\" option found " + "to set parameter of -s as serial output file\n"); + break; + default: + /* Unknown suboption. */ + fprintf(stderr, "Unknown suboption `%s' for -S\n", value); + exit(1); + break; + } + break; case 'h': - print_help("s51"); - exit(0); - break; + print_help("s51"); + exit(0); + break; case 'H': - /*i= 0; - while (cpus_51[i].type_str != NULL) - { - printf("%s\n", cpus_51[i].type_str); - i++; - }*/ - exit(0); - break; + { + if (!cpus) + { + fprintf(stderr, "CPU type is not selectable\n"); + exit(0); + } + i= 0; + while (cpus[i].type_str != NULL) + { + printf("%s\n", cpus[i].type_str); + i++; + } + exit(0); + break; + } case '?': - if (isprint(optopt)) - fprintf(stderr, "Unknown option `-%c'.\n", optopt); - else - fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt); - return(1); - break; + if (isprint(optopt)) + fprintf(stderr, "Unknown option `-%c'.\n", optopt); + else + fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt); + return(1); + break; default: - exit(c); + exit(c); } - if (!args->arg_avail("prompt")) - args->add(new cl_prg_arg(0, "prompt", "> ")); for (i= optind; i < argc; i++) in_files->add(argv[i]); @@ -497,12 +579,14 @@ cl_app::get_cmd(class cl_cmdline *cmdline) * Messages to broadcast */ +/* void -cl_app::mem_cell_changed(class cl_mem *mem, t_addr addr) +cl_app::mem_cell_changed(class cl_m *mem, t_addr addr) { if (sim) sim->mem_cell_changed(mem, addr); } +*/ /* Adding and removing components */ @@ -513,7 +597,7 @@ cl_app::set_simulator(class cl_sim *simulator) if (sim) remove_simulator(); sim= simulator; - + } void @@ -539,10 +623,9 @@ cl_app::build_cmdset(class cl_cmdset *cmdset) "conf Configuration", "long help of conf")); cmd->init(); - cset->add(cmd= new cl_conf_addmem_cmd("addmem", 0, -"conf addmem\n" -" Make memory", -"long help of conf addmem")); + cset->add(cmd= new cl_conf_objects_cmd("objects", 0, +"conf objects Show object tree", +"long help of conf objects")); cmd->init(); } cmdset->add(cmd= new cl_super_cmd("conf", 0, @@ -572,14 +655,25 @@ cl_app::build_cmdset(class cl_cmdset *cmdset) "long help of exec")); cmd->init(); + cmdset->add(cmd= new cl_expression_cmd("expression", 0, +"expression expr Evaluate the expression", +"long help of expression ")); + cmd->init(); + cmd->add_name("let"); + { - cset= new cl_cmdset(); - cset->init(); - cset->add(cmd= new cl_show_copying_cmd("copying", 0, + super_cmd= (class cl_super_cmd *)(cmdset->get_cmd("show")); + if (super_cmd) + cset= super_cmd->commands; + else { + cset= new cl_cmdset(); + cset->init(); + } + cset->add(cmd= new cl_show_copying_cmd("copying", 0, "show copying Conditions for redistributing copies of uCsim", "long help of show copying")); cmd->init(); - cset->add(cmd= new cl_show_warranty_cmd("warranty", 0, + cset->add(cmd= new cl_show_warranty_cmd("warranty", 0, "show warranty Various kinds of warranty you do not have", "long help of show warranty")); cmd->init(); @@ -587,11 +681,18 @@ cl_app::build_cmdset(class cl_cmdset *cmdset) "show option [name] Show internal data of options", "long help of show option")); cmd->init(); + cset->add(cmd= new cl_show_error_cmd("error", 0, +"show error Show class of errors", +"long help of show error")); + cmd->init(); } - cmdset->add(cmd= new cl_super_cmd("show", 0, + if (!super_cmd) + { + cmdset->add(cmd= new cl_super_cmd("show", 0, "show subcommand Generic command for showing things about the uCsim", "long help of show", cset)); - cmd->init(); + cmd->init(); + } { super_cmd= (class cl_super_cmd *)(cmdset->get_cmd("get")); @@ -627,6 +728,11 @@ cl_app::build_cmdset(class cl_cmdset *cmdset) " Set value of an option", "long help of set option")); cmd->init(); + cset->add(cmd= new cl_set_error_cmd("error", 0, +"set error error_name on|off|unset\n" +" Set value of an error", +"long help of set error")); + cmd->init(); } if (!super_cmd) { @@ -643,28 +749,76 @@ cl_app::mk_options(void) class cl_option *o; options->new_option(o= new cl_bool_option(this, "null_prompt", - "Use \\0 as prompt (-P)")); + "Use \\0 as prompt (-P)")); o->init(); - options->new_option(o= new cl_bool_option(this, "stopit", - "Stop if interrupt accepted")); - o->init(); - options->new_option(o= new cl_string_option(this, "serial_in_file", - "Input file for serial line (-s)")); + + options->new_option(o= new cl_pointer_option(this, "serial_in_file", + "Input file for serial line (-s)")); o->init(); - options->new_option(o= new cl_string_option(this, "serial_out_file", - "Output file for serial line (-s)")); + o->hide(); + + options->new_option(o= new cl_pointer_option(this, "serial_out_file", + "Output file for serial line (-s)")); o->init(); + o->hide(); + options->new_option(o= new cl_string_option(this, "prompt", - "String of prompt (-p)")); + "String of prompt (-p)")); o->init(); + options->new_option(o= new cl_bool_option(this, "debug", - "Print debug messages (-V)")); + "Print debug messages (-V)")); o->init(); + options->new_option(o= new cl_string_option(this, "console_on", - "Open console on this file (-c)")); + "Open console on this file (-c)")); + o->init(); + o->hide(); + + options->new_option(o= new cl_string_option(this, "config_file", + "Execute this file at startup (-C)")); + o->init(); + o->hide(); + + options->new_option(o= new cl_float_option(this, "xtal", + "Frequency of XTAL in Hz")); + o->init(); + o->set_value(11059200.0); + + options->new_option(o= new cl_string_option(this, "cpu_type", + "Type of controller (-t)")); o->init(); o->hide(); } +int +cl_app::dd_printf(const char *format, ...) +{ + va_list ap; + + if (!commander) + return(0); + + va_start(ap, format); + int i= commander->dd_printf(format, ap); + va_end(ap); + return(i); +} + +int +cl_app::debug(const char *format, ...) +{ + va_list ap; + + if (!commander) + return(0); + + va_start(ap, format); + int i= commander->debug(format, ap); + va_end(ap); + return(i); +} + + /* End of app.cc */