2 * Copyright (C) 2012-2013 Jiří Šimek
3 * Copyright (C) 2013 Zbyněk Křivka <krivka@fit.vutbr.cz>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
34 # include HEADER_SOCKET
42 #include "apppblazecl.h"
52 #include "cmd_confcl.h"
53 #include "cmd_showcl.h"
54 #include "cmd_getcl.h"
55 #include "cmd_setcl.h"
57 //#include "newcmdwin32cl.h"
59 #include "newcmdposixcl.h"
63 #define PORTVERSION "0.2.0"
65 * Interpretation of parameters
69 print_help(const char *name
)
71 printf("%s: %s (uCsim %s)\n", name
, PORTVERSION
, VERSIONSTR
);
72 printf("Usage: %s [-hHVvP] [-p prompt] [-t CPU] [-X freq[k|M]]\n"
73 " [-c file] [-s file] [-S optionlist]"
75 " [-Z portnum] [-k portnum]"
78 " [files...]\n", name
);
82 " -t CPU Type of CPU: kcpsm3 or kcpsm6\n"
83 " -X freq[k|M] XTAL frequency\n"
84 " -c file Open command console on `file'\n"
86 " -Z portnum Use localhost:portnumber for command console\n"
87 " -k portnum Use localhost:portnum for serial I/O\n"
89 " -s file Connect serial interface to `file'\n"
90 " -S options `options' is a comma separated list of options according to\n"
91 " serial interface. Know options are:\n"
92 " in=file serial input will be read from file named `file'\n"
93 " out=file serial output will be written to `file'\n"
94 " -p prompt Specify string for prompt\n"
95 " -P Prompt is a null ('\\0') character\n"
97 " -v Print out version number\n"
98 " -H Print out types of known CPUs\n"
99 " -h Print out this help\n"
101 " -x Input file format isn't Intel HEX file, but raw HEX file for PicoBlaze\n"
102 " -i file Read file with interrupts\n"
103 " -I value Address of interrupt vector\n"
104 " -m value Size of RAM (only for KCPSM6). Valid values are 64, 128 and 256.\n"
105 " -M value Size of ROM (only for KCPSM6). Valid values are 1024, 2048 and 4096.\n"
106 " -n file Loads file with inputs.\n"
107 " -w value Built-in hardware constant (only for KCPSM6).\n"
108 " -o file At the end of simulation saves PicoBlaze state and PicoBlaze outputs into files <file>_state.xml and <file>_outputs.xml.\n"
110 "Authors: Jiri Simek and Zbynek Krivka (krivka@fit.vutbr.cz)\n"
112 "Acknowledgement: This software tool has been elaborated in the\n"
113 "framework of the IT4Innovations Centre of Excellence project, \n"
114 "reg. no. CZ.1.05/1.1.00/02.0070 supported by Operational Programme\n"
115 "'Research and Development for Innovations' funded by Structural Funds\n"
116 "of the European Union and state budget of the Czech Republic.\n"
125 static const char *S_opts
[]= {
127 /*[SOPT_OUT]=*/ "out",
133 cl_apppblaze::proc_arguments(int argc
, char *argv
[])
136 char opts
[100], *cp
, *subopts
, *value
;
137 char *cpu_type
= NULL
;
138 bool s_done
= false, k_done
= false;
139 bool S_i_done
= false, S_o_done
= false;
141 strcpy(opts
, "c:C:p:PX:vVt:s:S:hHk:xi:I:m:M:w:n:o:");
143 strcat(opts
, "Z:r:");
146 while((c
= getopt(argc
, argv
, opts
)) != -1)
149 /* ADDED by Jiri Simek */
154 options
->new_option(o
= new cl_bool_option(this, "pblaze_hex","Read pblaze hex file"));
157 if (!options
->set_value("pblaze_hex", this, (bool)true))
158 fprintf(stderr
, "Warning: Cannot set option -x");
160 o
= options
->get_option("pblaze_hex");
168 options
->new_option(o
= new cl_string_option(this, "pblaze_interrupt_file","File with interrupts is specified"));
171 if (!options
->set_value("pblaze_interrupt_file", this, optarg
))
172 fprintf(stderr
, "Warning: No \"pblaze_interrupt_file\" option found to set "
173 "parameter of -i as interrupt file\n");
179 options
->new_option(o
= new cl_number_option(this, "pblaze_interrupt_vector","Address of interrupt vector"));
182 if (!options
->set_value("pblaze_interrupt_vector", this, optarg
))
183 fprintf(stderr
, "Warning: No \"pblaze_interrupt_vector\" option found to set "
184 "parameter of -I as interrupt vector\n");
190 options
->new_option(o
= new cl_number_option(this, "pblaze_ram_size","Set RAM size"));
193 if (!options
->set_value("pblaze_ram_size", this, strtol(optarg
, NULL
, 0)))
194 fprintf(stderr
, "Warning: No \"pblaze_ram_size\" option found to set "
195 "parameter of -m as RAM size\n");
201 options
->new_option(o
= new cl_string_option(this, "pblaze_input_file","File with inputs is specified"));
204 if (!options
->set_value("pblaze_input_file", this, optarg
))
205 fprintf(stderr
, "Warning: No \"pblaze_input_file\" option found to set "
206 "parameter of -i as interrupt file\n");
212 options
->new_option(o
= new cl_number_option(this, "pblaze_rom_size","Set ROM size"));
215 if (!options
->set_value("pblaze_rom_size", this, strtol(optarg
, NULL
, 0)))
216 fprintf(stderr
, "Warning: No \"pblaze_rom_size\" option found to set "
217 "parameter of -M as ROM size\n");
223 options
->new_option(o
= new cl_number_option(this, "pblaze_hw_const","Builtin hardware constant"));
226 if (!options
->set_value("pblaze_hw_const", this, strtol(optarg
, NULL
, 0)))
227 fprintf(stderr
, "Warning: No \"pblaze_hw_const\" option found to set "
228 "parameter of -w as builtin hardware constant\n");
234 options
->new_option(o
= new cl_string_option(this, "pblaze_output_file","Part of file names with PicoBlaze state and outputs at the end of simulation"));
237 if (!options
->set_value("pblaze_output_file", this, optarg
))
238 fprintf(stderr
, "Warning: No \"pblaze_output_file\" option found to set "
239 "parameter of -o as output files\n");
245 // default parameters
247 if (!options
->set_value("console_on", this, optarg
))
248 fprintf(stderr
, "Warning: No \"console_on\" option found "
252 if (!options
->set_value("config_file", this, optarg
))
253 fprintf(stderr
, "Warning: No \"config_file\" option found to set "
254 "parameter of -C as config file\n");
262 options
->new_option(o
= new cl_number_option(this, "port_number",
263 "Listen on port (-Z)"));
266 if (!options
->set_value("port_number", this, strtol(optarg
, NULL
, 0)))
267 fprintf(stderr
, "Warning: No \"port_number\" option found"
268 " to set parameter of -Z as pot number to listen on\n");
273 if (!options
->set_value("prompt", this, optarg
))
274 fprintf(stderr
, "Warning: No \"prompt\" option found to set "
275 "parameter of -p as default prompt\n");
279 if (!options
->set_value("null_prompt", this, bool(true)))
280 fprintf(stderr
, "Warning: No \"null_prompt\" option found\n");
285 for (cp
= optarg
; *cp
; *cp
= toupper(*cp
), cp
++);
286 XTAL
= strtod(optarg
, &cp
);
293 fprintf(stderr
, "Xtal frequency must be greater than 0\n");
296 if (!options
->set_value("xtal", this, XTAL
))
297 fprintf(stderr
, "Warning: No \"xtal\" option found to set "
298 "parameter of -X as XTAL frequency\n");
302 printf("%s: %s\n", argv
[0], VERSIONSTR
);
306 if (!options
->set_value("debug", this, (bool)true))
307 fprintf(stderr
, "Warning: No \"debug\" option found to set "
308 "by -V parameter\n");
314 cpu_type
= case_string(case_upper
, optarg
);
315 if (!options
->set_value("cpu_type", this, /*optarg*/cpu_type
))
316 fprintf(stderr
, "Warning: No \"cpu_type\" option found to set "
317 "parameter of -t as type of controller\n");
323 /* TODO: this code should be probably used for all platforms? */
327 fprintf(stderr
, "-s option can not be used more than once.\n");
331 if ((Ser
= fopen(optarg
, "r+")) == NULL
)
334 "Can't open `%s': %s\n", optarg
, strerror(errno
));
337 if (!options
->set_value("serial_in_file", this, Ser
))
338 fprintf(stderr
, "Warning: No \"serial_in_file\" option found to set "
339 "parameter of -s as serial input file\n");
340 if (!options
->set_value("serial_out_file", this, Ser
))
341 fprintf(stderr
, "Warning: No \"serial_out_file\" option found "
342 "to set parameter of -s as serial output file\n");
344 FILE *Ser_in
, *Ser_out
;
347 fprintf(stderr
, "-s option can not be used more than once.\n");
351 if ((Ser_in
= fopen(optarg
, "r")) == NULL
)
354 "Can't open `%s': %s\n", optarg
, strerror(errno
));
357 if (!options
->set_value("serial_in_file", this, Ser_in
))
358 fprintf(stderr
, "Warning: No \"serial_in_file\" option found to set "
359 "parameter of -s as serial input file\n");
360 if ((Ser_out
= fopen(optarg
, "w")) == NULL
)
363 "Can't open `%s': %s\n", optarg
, strerror(errno
));
366 if (!options
->set_value("serial_out_file", this, Ser_out
))
367 fprintf(stderr
, "Warning: No \"serial_out_file\" option found "
368 "to set parameter of -s as serial output file\n");
373 // socket serial I/O by Alexandre Frey <Alexandre.Frey@trusted-logic.fr>
376 FILE *Ser_in
, *Ser_out
;
378 unsigned short serverport
;
379 UCSOCKET_T client_sock
;
383 fprintf(stderr
, "Serial input specified more than once.\n");
387 serverport
= atoi(optarg
);
388 sock
= make_server_socket(serverport
);
390 if (SOCKET_ERROR
== listen((SOCKET
)sock
, 1))
392 fprintf(stderr
, "Listen on port %d: %d\n", serverport
,
396 fprintf(stderr
, "Listening on port %d for a serial connection.\n",
398 if (INVALID_SOCKET
== (client_sock
= accept(sock
, NULL
, NULL
)))
400 fprintf(stderr
, "accept: %d\n", WSAGetLastError());
403 fprintf(stderr
, "Serial connection established.\n");
405 int fh
= _open_osfhandle((intptr_t)client_sock
, 0);
408 perror("_open_osfhandle");
411 if (NULL
== (Ser_in
= fdopen(fh
, "r")))
413 fprintf(stderr
, "Can't create input stream: %s\n", strerror(errno
));
417 fh
= _open_osfhandle((intptr_t)client_sock
, 0);
420 perror("_open_osfhandle");
422 if (NULL
== (Ser_out
= fdopen(fh
, "w"))) {
423 fprintf(stderr
, "Can't create output stream: %s\n", strerror(errno
));
427 if (listen(sock
, 1) < 0) {
428 fprintf(stderr
, "Listen on port %d: %s\n", serverport
,
432 fprintf(stderr
, "Listening on port %d for a serial connection.\n",
434 if ((client_sock
= accept(sock
, NULL
, NULL
)) < 0) {
435 fprintf(stderr
, "accept: %s\n", strerror(errno
));
437 fprintf(stderr
, "Serial connection established.\n");
439 if ((Ser_in
= fdopen(client_sock
, "r")) == NULL
) {
440 fprintf(stderr
, "Can't create input stream: %s\n", strerror(errno
));
443 if ((Ser_out
= fdopen(client_sock
, "w")) == NULL
) {
444 fprintf(stderr
, "Can't create output stream: %s\n", strerror(errno
));
448 if (!options
->set_value("serial_in_file", this, (void*)Ser_in
))
449 fprintf(stderr
, "Warning: No \"serial_in_file\" option found to "
450 "set parameter of -s as serial input file\n");
451 if (!options
->set_value("serial_out_file", this, Ser_out
))
452 fprintf(stderr
, "Warning: No \"serial_out_file\" option found "
453 "to set parameter of -s as serial output file\n");
459 while (*subopts
!= '\0')
460 switch (get_sub_opt(&subopts
, S_opts
, &value
))
462 FILE *Ser_in
, *Ser_out
;
465 fprintf(stderr
, "No value for -S in\n");
470 fprintf(stderr
, "Serial input specified more than once.\n");
474 if ((Ser_in
= fopen(value
, "r")) == NULL
)
477 "Can't open `%s': %s\n", value
, strerror(errno
));
480 if (!options
->set_value("serial_in_file", this, (void*)Ser_in
))
481 fprintf(stderr
, "Warning: No \"serial_in_file\" option found "
482 "to set parameter of -s as serial input file\n");
486 fprintf(stderr
, "No value for -S out\n");
491 fprintf(stderr
, "Serial output specified more than once.\n");
494 if ((Ser_out
= fopen(value
, "w")) == NULL
)
497 "Can't open `%s': %s\n", value
, strerror(errno
));
500 if (!options
->set_value("serial_out_file", this, Ser_out
))
501 fprintf(stderr
, "Warning: No \"serial_out_file\" option found "
502 "to set parameter of -s as serial output file\n");
505 /* Unknown suboption. */
506 fprintf(stderr
, "Unknown suboption `%s' for -S\n", value
);
512 print_help("spblaze");
519 fprintf(stderr
, "CPU type is not selectable\n");
523 while (cpus
[i
].type_str
!= NULL
)
525 printf("%s\n", cpus
[i
].type_str
);
533 fprintf(stderr
, "Unknown option `-%c'.\n", optopt
);
535 fprintf(stderr
, "Unknown option character `\\x%x'.\n", optopt
);
542 for (i
= optind
; i
< argc
; i
++)
543 in_files
->add(argv
[i
]);
548 /* End of pblaze.src/apppblaze.cc */