@@ -163,13 +163,9 @@ int main(int argc, char **argv)
163163 " " // user_flash
164164 };
165165 /* parse arguments */
166- try {
167- if (parse_opt (argc, argv, &args, &pins_config))
168- return EXIT_SUCCESS;
169- } catch (std::exception &e) {
170- printError (" Error in parse arg step" );
171- return EXIT_FAILURE;
172- }
166+ int ret = parse_opt (argc, argv, &args, &pins_config);
167+ if (ret != 0 )
168+ return (ret == 1 ) ? EXIT_SUCCESS : EXIT_FAILURE;
173169
174170 if (args.is_list_command ) {
175171 displaySupported (args);
@@ -305,7 +301,6 @@ int main(int argc, char **argv)
305301 spi_pins_config.sck_pin = (1 << pins_config.tck_pin );
306302 spi_pins_config.mosi_pin = (1 << pins_config.tdi_pin );
307303 spi_pins_config.miso_pin = (1 << pins_config.tdo_pin );
308- spi_pins_config.miso_pin = (1 << pins_config.tdo_pin );
309304 spi_pins_config.holdn_pin = (1 << pins_config.ext0_pin );
310305 spi_pins_config.wpn_pin = (1 << pins_config.ext1_pin );
311306 }
@@ -868,9 +863,9 @@ static int parse_eng(string arg, double *dst) {
868863int parse_opt (int argc, char **argv, struct arguments *args,
869864 jtag_pins_conf_t *pins_config)
870865{
871- string freqo, rd_reg ;
866+ string freqo;
872867 vector<string> pins, bus_dev_num;
873- bool verbose, quiet;
868+ bool verbose = false , quiet = false ;
874869 int8_t verbose_level = -2 ;
875870 try {
876871 cxxopts::Options options (argv[0 ], " openFPGALoader -- a program to flash FPGA" ,
@@ -1003,7 +998,7 @@ int parse_opt(int argc, char **argv, struct arguments *args,
1003998 (" X,read-xadc" , " Read XADC (Xilinx FPGA only)" ,
1004999 cxxopts::value<bool >(args->read_xadc ))
10051000 (" read-register" , " Read Status Register(Xilinx FPGA only)" ,
1006- cxxopts::value<string>(rd_reg ))
1001+ cxxopts::value<string>(args-> read_register ))
10071002 (" user-flash" , " User flash file (Gowin LittleBee FPGA only)" ,
10081003 cxxopts::value<string>(args->user_flash ))
10091004 (" V,Version" , " Print program version" );
@@ -1018,7 +1013,7 @@ int parse_opt(int argc, char **argv, struct arguments *args,
10181013
10191014 if (verbose && quiet) {
10201015 printError (" Error: can't select quiet and verbose mode in same time" );
1021- throw std::exception () ;
1016+ return - 1 ;
10221017 }
10231018 if (verbose)
10241019 args->verbose = 1 ;
@@ -1028,7 +1023,7 @@ int parse_opt(int argc, char **argv, struct arguments *args,
10281023 if ((verbose && verbose_level != 1 ) ||
10291024 (quiet && verbose_level != -1 )) {
10301025 printError (" Error: mismatch quiet/verbose and verbose-level\n " );
1031- throw std::exception () ;
1026+ return - 1 ;
10321027 }
10331028
10341029 args->verbose = verbose_level;
@@ -1055,20 +1050,25 @@ int parse_opt(int argc, char **argv, struct arguments *args,
10551050
10561051 if (tokens.size () != 3 ) {
10571052 printError (" Error: invalid format for misc-device." );
1058- throw std::exception () ;
1053+ return - 1 ;
10591054 }
10601055
1061- idcode = std::stoul (tokens[0 ], nullptr , 16 );
1062- irlen = std::stoi (tokens[1 ]);
1056+ try {
1057+ idcode = std::stoul (tokens[0 ], nullptr , 16 );
1058+ irlen = std::stoi (tokens[1 ]);
1059+ } catch (const std::exception &) {
1060+ printError (" Error: invalid numeric format for misc-device, expected <hex_idcode,irlen,name>" );
1061+ return -1 ;
1062+ }
10631063 name = tokens[2 ];
10641064 args->user_misc_devs [idcode] = {name, irlen};
10651065 }
10661066 }
10671067
1068- if (result.count (" write-flash" ) && result.count (" write-sram" ) &&
1069- result.count (" dump-flash" )) {
1070- printError (" Error: both write to flash and write to ram enabled " );
1071- throw std::exception () ;
1068+ if (result.count (" write-flash" ) + result.count (" write-sram" ) +
1069+ result.count (" dump-flash" ) > 1 ) {
1070+ printError (" Error: --write-flash, -- write-sram and --dump- flash are mutually exclusive " );
1071+ return - 1 ;
10721072 }
10731073
10741074 if (result.count (" write-flash" ))
@@ -1084,33 +1084,33 @@ int parse_opt(int argc, char **argv, struct arguments *args,
10841084 double freq;
10851085 if (parse_eng (freqo, &freq)) {
10861086 printError (" Error: invalid format for --freq" );
1087- throw std::exception () ;
1087+ return - 1 ;
10881088 }
10891089 if (freq < 1 ) {
10901090 printError (" Error: --freq must be positive" );
1091- throw std::exception () ;
1091+ return - 1 ;
10921092 }
10931093 args->freq = static_cast <uint32_t >(freq);
10941094 }
10951095
10961096 if (result.count (" status-pin" )) {
10971097 if (args->status_pin < 4 || args->status_pin > 15 ) {
10981098 printError (" Error: valid status pin numbers are 4-15." );
1099- throw std::exception () ;
1099+ return - 1 ;
11001100 }
11011101 }
11021102
11031103 if (result.count (" ftdi-channel" )) {
11041104 if (args->ftdi_channel < 0 || args->ftdi_channel > 3 ) {
11051105 printError (" Error: valid FTDI channels are 0-3." );
1106- throw std::exception () ;
1106+ return - 1 ;
11071107 }
11081108 }
11091109
11101110 if (result.count (" busdev-num" )) {
11111111 if (bus_dev_num.size () != 2 ) {
11121112 printError (" Error: busdev-num must be xx:yy" );
1113- throw std::exception () ;
1113+ return - 1 ;
11141114 }
11151115 try {
11161116 args->bus_addr = static_cast <uint8_t >(std::stoi (bus_dev_num[0 ],
@@ -1119,14 +1119,14 @@ int parse_opt(int argc, char **argv, struct arguments *args,
11191119 std::stoi (bus_dev_num[1 ], nullptr , 10 ));
11201120 } catch (std::exception &e) {
11211121 printError (" Error: busdev-num invalid format: must be numeric values" );
1122- throw std::exception () ;
1122+ return - 1 ;
11231123 }
11241124 }
11251125
11261126 if (result.count (" pins" )) {
11271127 if (pins.size () < 4 || pins.size () > 6 ) {
11281128 printError (" Error: pin_config need 4 pins in JTAG mode or 6 pins in SPI Mode" );
1129- throw std::exception () ;
1129+ return - 1 ;
11301130 }
11311131
11321132 static std::map <std::string, int > pins_list = {
@@ -1139,38 +1139,28 @@ int parse_opt(int argc, char **argv, struct arguments *args,
11391139 {" DCD" , FT232RL_DCD},
11401140 {" RI" , FT232RL_RI}};
11411141
1142+ uint8_t *pin_fields[] = {
1143+ &pins_config->tdi_pin ,
1144+ &pins_config->tdo_pin ,
1145+ &pins_config->tck_pin ,
1146+ &pins_config->tms_pin ,
1147+ &pins_config->ext0_pin ,
1148+ &pins_config->ext1_pin
1149+ };
1150+
11421151 for (size_t i = 0 ; i < pins.size (); i++) {
11431152 int pin_num;
11441153 try {
11451154 pin_num = std::stoi (pins[i], nullptr , 0 );
11461155 } catch (std::exception &e) {
11471156 if (pins_list.find (pins[i]) == pins_list.end ()) {
11481157 printError (" Invalid pin name" );
1149- throw std::exception () ;
1158+ return - 1 ;
11501159 }
11511160 pin_num = pins_list[pins[i]];
11521161 }
11531162
1154- switch (i) {
1155- case 0 :
1156- pins_config->tdi_pin = pin_num;
1157- break ;
1158- case 1 :
1159- pins_config->tdo_pin = pin_num;
1160- break ;
1161- case 2 :
1162- pins_config->tck_pin = pin_num;
1163- break ;
1164- case 3 :
1165- pins_config->tms_pin = pin_num;
1166- break ;
1167- case 4 :
1168- pins_config->ext0_pin = pin_num;
1169- break ;
1170- case 5 :
1171- pins_config->ext1_pin = pin_num;
1172- break ;
1173- }
1163+ *pin_fields[i] = pin_num;
11741164 }
11751165 args->pin_config = true ;
11761166 }
@@ -1185,7 +1175,7 @@ int parse_opt(int argc, char **argv, struct arguments *args,
11851175 ) {
11861176 printError (" Error: secondary bitfile not specified" );
11871177 cout << options.help () << endl;
1188- throw std::exception () ;
1178+ return - 1 ;
11891179 }
11901180 }
11911181
@@ -1199,12 +1189,6 @@ int parse_opt(int argc, char **argv, struct arguments *args,
11991189 printf (" pas empty\n " );
12001190 }
12011191
1202- if (result.count (" read-register" )) {
1203- args->read_register = rd_reg;
1204- printf (" read_register" );
1205- std::cout << args->read_register << std::endl;
1206- }
1207-
12081192 if (args->bit_file .empty () &&
12091193 args->secondary_bit_file .empty () &&
12101194 args->file_type .empty () &&
@@ -1224,7 +1208,7 @@ int parse_opt(int argc, char **argv, struct arguments *args,
12241208 args->read_register .empty ()) {
12251209 printError (" Error: bitfile not specified" );
12261210 cout << options.help () << endl;
1227- throw std::exception () ;
1211+ return - 1 ;
12281212 }
12291213
12301214 // user ask detect with flash set
@@ -1235,8 +1219,8 @@ int parse_opt(int argc, char **argv, struct arguments *args,
12351219 args->detect_flash = true ;
12361220 }
12371221 } catch (const cxxopts::OptionException& e) {
1238- cerr << " Error parsing options: " << e.what () << endl ;
1239- throw std::exception () ;
1222+ printError ( " Error parsing options: " + string ( e.what ())) ;
1223+ return - 1 ;
12401224 }
12411225
12421226 return 0 ;
@@ -1305,4 +1289,3 @@ void displaySupported(const struct arguments &args)
13051289 }
13061290#endif
13071291}
1308-
0 commit comments