2 * Simulator of microcontrollers (sim51.cc)
4 * Copyright (C) 1999,99 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 <sys/socket.h>
44 //#include "cmd51cl.h"
48 #include "uc89c51rcl.h"
53 cl_sim51::cl_sim51(int iargc, char *iargv[]):
54 cl_sim("t:s:S:hHk:", iargc, iargv)
58 print_help(char *name)
60 printf("%s: %s\n", name, VERSIONSTR);
61 printf("Usage: %s [-hHVvP] [-p prompt] [-t CPU] [-X freq[k|M]]\n"
62 " [-c file] [-s file] [-S optionlist]"
64 " [-Z portnum] [-k portnum]"
67 " [files...]\n", name);
71 " -t CPU Type of CPU: 51, C52, 251, etc.\n"
72 " -X freq[k|M] XTAL frequency\n"
73 " -c file Open command console on `file'\n"
75 " -Z portnum Use localhost:portnumber for command console\n"
76 " -k portnum Use localhost:portnum for serial I/O\n"
78 " -s file Connect serial interface to `file'\n"
79 " -S options `options' is a comma separated list of options\n"
80 " according to serial interface. Know options are:\n"
81 " in=file serial input will be read from file named `file'\n"
82 " out=file serial output will be written to `file'\n"
83 " -p prompt Specify string for prompt\n"
84 " -P Prompt is a null ('\\0') character\n"
86 " -v Print out version number\n"
87 " -H Print out types of known CPUs\n"
88 " -h Print out this help\n"
97 static const char *S_opts[]= {
99 /*[SOPT_OUT]=*/ "out",
104 cl_sim51::proc_arg(char optopt, char *optarg)
106 char *cpu_type= NULL, *cp;
108 char *subopts, *value;
117 cpu_type= strdup(optarg);
118 for (cp= cpu_type; *cp; *cp= toupper(*cp), cp++);
119 arguments->add(new cl_prg_arg('t', 0, cpu_type));
124 FILE *Ser_in, *Ser_out;
127 fprintf(stderr, "-s option can not be used more than once.\n");
130 arguments->add(new cl_prg_arg('s', 0, (long)1));
131 if ((Ser_in= fopen(optarg, "r")) == NULL)
134 "Can't open `%s': %s\n", optarg, strerror(errno));
137 arguments->add(new cl_prg_arg(0, "Ser_in", Ser_in));
138 if ((Ser_out= fopen(optarg, "w")) == NULL)
141 "Can't open `%s': %s\n", optarg, strerror(errno));
144 arguments->add(new cl_prg_arg(0, "Ser_out", Ser_out));
149 // socket serial I/O by Alexandre Frey <Alexandre.Frey@trusted-logic.fr>
152 FILE *Ser_in, *Ser_out;
154 unsigned short serverport;
157 if (arg_avail("Ser_in")) {
158 fprintf(stderr, "Serial input specified more than once.\n");
160 if (arg_avail("Ser_out")) {
161 fprintf(stderr, "Serial output specified more than once.\n");
164 serverport = atoi(optarg);
165 sock = make_server_socket(serverport);
166 if (listen(sock, 1) < 0) {
167 fprintf(stderr, "Listen on port %d: %s\n", serverport,
171 fprintf(stderr, "Listening on port %d for a serial connection.\n",
173 if ((client_sock = accept(sock, NULL, NULL)) < 0) {
174 fprintf(stderr, "accept: %s\n", strerror(errno));
176 fprintf(stderr, "Serial connection established.\n");
178 if ((Ser_in = fdopen(client_sock, "r")) == NULL) {
179 fprintf(stderr, "Can't create input stream: %s\n", strerror(errno));
182 arguments->add(new cl_prg_arg(0, "Ser_in", Ser_in));
183 if ((Ser_out = fdopen(client_sock, "w")) == NULL) {
184 fprintf(stderr, "Can't create output stream: %s\n", strerror(errno));
187 arguments->add(new cl_prg_arg(0, "Ser_out", Ser_out));
195 while (*subopts != '\0')
196 switch (get_sub_opt(&subopts, S_opts, &value))
198 FILE *Ser_in, *Ser_out;
201 fprintf(stderr, "No value for -S in\n");
204 if (arg_avail("Ser_in"))
206 fprintf(stderr, "Serial input specified more than once.\n");
209 if ((Ser_in= fopen(value, "r")) == NULL)
212 "Can't open `%s': %s\n", value, strerror(errno));
215 arguments->add(new cl_prg_arg(0, "Ser_in", Ser_in));
219 fprintf(stderr, "No value for -S out\n");
222 if (arg_avail("Ser_out"))
224 fprintf(stderr, "Serial output specified more than once.\n");
227 if ((Ser_out= fopen(value, "w")) == NULL)
230 "Can't open `%s': %s\n", value, strerror(errno));
233 arguments->add(new cl_prg_arg(0, "Ser_out", Ser_out));
236 /* Unknown suboption. */
237 fprintf(stderr, "Unknown suboption `%s' for -S\n", value);
251 while (cpus_51[i].type_str != NULL)
253 printf("%s\n", cpus_51[i].type_str);
262 fprintf(stderr, "Unknown option `-%c'.\n", optopt);
264 fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt);
269 // should never happen...
277 cl_sim51::mk_controller(void)
282 if (get_sarg('t', 0) == NULL)
283 arguments->add(new cl_prg_arg('t', 0, "C51"));
284 while ((cpus_51[i].type_str != NULL) &&
285 (strcmp(get_sarg('t', 0), cpus_51[i].type_str) != 0))
287 if (cpus_51[i].type_str == NULL)
289 fprintf(stderr, "Unknown processor type. "
290 "Use -H option to see known types.\n");
293 switch (cpus_51[i].type)
295 case CPU_51: case CPU_31:
296 return(new t_uc51(cpus_51[i].type, cpus_51[i].technology, this));
297 case CPU_52: case CPU_32:
298 return(new t_uc52(cpus_51[i].type, cpus_51[i].technology, this));
300 return(new t_uc51r(cpus_51[i].type, cpus_51[i].technology, this));
302 return(new t_uc89c51r(cpus_51[i].type, cpus_51[i].technology, this));
304 return(new t_uc251(cpus_51[i].type, cpus_51[i].technology, this));
310 /* End of s51.src/sim51.cc */