@@ -53,6 +53,11 @@ MainWindow::MainWindow(const std::shared_ptr<QStringList> _log_messages, QWidget
5353 this ->hex_widget ->setSizePolicy (QSizePolicy::Expanding, QSizePolicy::Expanding);
5454 container_layout->addWidget (this ->hex_widget );
5555
56+ this ->button_reload_file = new QPushButton (" Reload file" );
57+ container_layout->addWidget (this ->button_reload_file );
58+ this ->button_reload_file ->setEnabled (false );
59+ connect (this ->button_reload_file , SIGNAL (released ()), this , SLOT (slot_reload_file ()));
60+
5661 // create central widget for writing data
5762 QScrollArea *scroll_area = new QScrollArea ();
5863 layout->addWidget (scroll_area);
@@ -569,9 +574,10 @@ void MainWindow::slot_open() {
569574 return ;
570575 }
571576
572- file.open (QIODevice::ReadOnly);
573577 if (file.exists ()) {
578+ file.open (QIODevice::ReadOnly);
574579 QByteArray data = file.readAll ();
580+ this ->current_filename = file.fileName ();
575581 int filesize = data.size ();
576582
577583 // ask the user whether they want to expand the image
@@ -582,11 +588,15 @@ void MainWindow::slot_open() {
582588 " be used as a cartridge, would you like to to expand it "
583589 " to 16kb by padding the data with 0x00?" , QMessageBox::Yes|QMessageBox::No);
584590 if (reply == QMessageBox::Yes) {
591+ this ->current_file_expanded = true ;
585592 resize_qbytearray (&data, 0x4000 );
593+ } else {
594+ this ->current_file_expanded = false ;
586595 }
587596 }
588597
589598 this ->hex_widget ->set_data (data);
599+ this ->button_reload_file ->setEnabled (true );
590600
591601 QByteArray hash = QCryptographicHash::hash (data, QCryptographicHash::Md5);
592602 QFileInfo finfo (file);
@@ -607,6 +617,46 @@ void MainWindow::slot_open() {
607617 }
608618}
609619
620+ /* *
621+ * @brief MainWindow::reload a file
622+ */
623+ void MainWindow::slot_reload_file () {
624+ QFile file (this ->current_filename );
625+
626+ if (file.exists ()) {
627+ file.open (QIODevice::ReadOnly);
628+ QByteArray data = file.readAll ();
629+ this ->current_filename = file.fileName ();
630+ int filesize = data.size ();
631+
632+ // ask the user whether they want to expand the image
633+ if (data.size () < 0x4000 && this ->current_file_expanded ) {
634+ resize_qbytearray (&data, 0x4000 );
635+ }
636+
637+ this ->hex_widget ->set_data (data);
638+
639+ QByteArray hash = QCryptographicHash::hash (data, QCryptographicHash::Md5);
640+ QFileInfo finfo (file);
641+
642+ QString usage;
643+ if (data.size () == 0x4000 ) {
644+ usage = QString (" | %1 kb / %2 kb (%3 %)" )
645+ .arg (QString::number ((float )filesize/(float )1024 , ' f' , 1 ))
646+ .arg (QString::number (16 , ' f' , 1 ))
647+ .arg ((float )filesize/(float )(16 * 1024 ) * 100 , 0 , ' f' , 1 );
648+ }
649+
650+ this ->label_data_descriptor ->setText (QString (" <b>%1</b> | Size: %2 kb | MD5: %3" + usage)
651+ .arg (finfo.fileName ())
652+ .arg (data.size () / 1024 )
653+ .arg (QString (hash.toHex ()))
654+ );
655+
656+ statusBar ()->showMessage (tr (" Reloaded %1 from drive." ).arg (this ->current_filename ));
657+ }
658+ }
659+
610660/* *
611661 * @brief Open a binary file
612662 */
@@ -741,6 +791,7 @@ void MainWindow::load_default_image() {
741791 if (timer.isActive ()) {
742792 QByteArray data = fd->downloadedData ();
743793 this ->hex_widget ->set_data (data);
794+ this ->button_reload_file ->setEnabled (false );
744795
745796 QString rom_name = image.split (QChar (' /' )).back ();
746797
@@ -870,6 +921,7 @@ void MainWindow::read_rom() {
870921 connect (this ->readerthread .get (), SIGNAL (read_result_ready ()), this , SLOT (read_result_ready ()));
871922 connect (this ->readerthread .get (), SIGNAL (read_block_start (uint,uint)), this , SLOT (read_block_start (uint,uint)));
872923 connect (this ->readerthread .get (), SIGNAL (read_block_done (uint,uint)), this , SLOT (read_block_done (uint,uint)));
924+ connect (this ->readerthread .get (), SIGNAL (thread_abort (const QString&)), this , SLOT (thread_abort (const QString&)));
873925 this ->readerthread ->start ();
874926}
875927
@@ -899,6 +951,7 @@ void MainWindow::read_cartridge() {
899951 connect (this ->cartridgereaderthread .get (), SIGNAL (read_result_ready ()), this , SLOT (read_result_ready ()));
900952 connect (this ->cartridgereaderthread .get (), SIGNAL (read_block_start (uint,uint)), this , SLOT (read_block_start (uint,uint)));
901953 connect (this ->cartridgereaderthread .get (), SIGNAL (read_block_done (uint,uint)), this , SLOT (read_block_done (uint,uint)));
954+ connect (this ->readerthread .get (), SIGNAL (thread_abort (const QString&)), this , SLOT (thread_abort (const QString&)));
902955 this ->cartridgereaderthread ->start ();
903956}
904957
@@ -955,7 +1008,17 @@ void MainWindow::flash_rom() {
9551008
9561009 // verify whether the chip is correct
9571010 qDebug () << " Verifying chip" ;
958- this ->verify_chip ();
1011+ try {
1012+ this ->verify_chip ();
1013+ } catch (const std::exception& e) {
1014+ this ->raise_error_window (QMessageBox::Critical,
1015+ " Cannot flash this ROM to bank due to a problem with "
1016+ " with the CHIP id. Please carefully check the chip and "
1017+ " whether it is properly inserted into the socket."
1018+ " \n\n Error message: " + QString (e.what ())
1019+ );
1020+ return ;
1021+ }
9591022
9601023 if ((this ->num_blocks * 256 ) < this ->flash_data .size ()) {
9611024
@@ -984,7 +1047,7 @@ void MainWindow::flash_rom() {
9841047 connect (this ->flashthread .get (), SIGNAL (flash_result_ready ()), this , SLOT (flash_result_ready ()));
9851048 connect (this ->flashthread .get (), SIGNAL (flash_sector_start (uint,uint)), this , SLOT (flash_sector_start (uint,uint)));
9861049 connect (this ->flashthread .get (), SIGNAL (flash_sector_done (uint,uint)), this , SLOT (flash_sector_done (uint,uint)));
987- connect (this ->flashthread .get (), SIGNAL (flash_chip_id_error (uint )), this , SLOT (flash_chip_id_error (uint )));
1050+ connect (this ->readerthread .get (), SIGNAL (thread_abort ( const QString& )), this , SLOT (thread_abort ( const QString& )));
9881051 flashthread->start ();
9891052
9901053 // disable all buttons
@@ -1011,7 +1074,17 @@ void MainWindow::flash_bank() {
10111074 this ->flash_data = this ->hex_widget ->get_data ();
10121075
10131076 // verify whether the chip is correct
1014- this ->verify_chip ();
1077+ try {
1078+ this ->verify_chip ();
1079+ } catch (const std::exception& e) {
1080+ this ->raise_error_window (QMessageBox::Critical,
1081+ " Cannot flash this ROM to bank due to a problem with "
1082+ " with the CHIP id. Please carefully check the chip and "
1083+ " whether it is properly inserted into the socket."
1084+ " \n\n Error message: " + QString (e.what ())
1085+ );
1086+ return ;
1087+ }
10151088
10161089 if (this ->flash_data .size () > 16 * 1024 ) {
10171090 this ->raise_error_window (QMessageBox::Critical,
@@ -1034,7 +1107,7 @@ void MainWindow::flash_bank() {
10341107 connect (this ->flashthread .get (), SIGNAL (flash_result_ready ()), this , SLOT (flash_result_ready ()));
10351108 connect (this ->flashthread .get (), SIGNAL (flash_sector_start (uint,uint)), this , SLOT (flash_sector_start (uint,uint)));
10361109 connect (this ->flashthread .get (), SIGNAL (flash_sector_done (uint,uint)), this , SLOT (flash_sector_done (uint,uint)));
1037- connect (this ->flashthread .get (), SIGNAL (flash_chip_id_error (uint )), this , SLOT (flash_chip_id_error (uint )));
1110+ connect (this ->readerthread .get (), SIGNAL (thread_abort ( const QString& )), this , SLOT (thread_abort ( const QString& )));
10381111 flashthread->start ();
10391112
10401113 // disable all buttons
@@ -1045,6 +1118,19 @@ void MainWindow::flash_bank() {
10451118 * @brief Put rom on flash cartridge
10461119 */
10471120void MainWindow::erase_chip () {
1121+ // verify whether the chip is correct
1122+ try {
1123+ this ->verify_chip ();
1124+ } catch (const std::exception& e) {
1125+ this ->raise_error_window (QMessageBox::Critical,
1126+ " Cannot flash this ROM to bank due to a problem with "
1127+ " with the CHIP id. Please carefully check the chip and "
1128+ " whether it is properly inserted into the socket."
1129+ " \n\n Error message: " + QString (e.what ())
1130+ );
1131+ return ;
1132+ }
1133+
10481134 QMessageBox::StandardButton reply;
10491135 reply = QMessageBox::question (this , " Wipe chip?" , " Are you sure you want to completely wipe the chip?" , QMessageBox::Yes|QMessageBox::No);
10501136 if (reply != QMessageBox::Yes) {
@@ -1079,6 +1165,7 @@ void MainWindow::scan_chip() {
10791165 connect (this ->readerthread .get (), SIGNAL (read_result_ready ()), this , SLOT (scan_chip_result_ready ()));
10801166 connect (this ->readerthread .get (), SIGNAL (read_block_start (uint,uint)), this , SLOT (read_block_start (uint,uint)));
10811167 connect (this ->readerthread .get (), SIGNAL (read_block_done (uint,uint)), this , SLOT (read_block_done (uint,uint)));
1168+ connect (this ->readerthread .get (), SIGNAL (thread_abort (const QString&)), this , SLOT (thread_abort (const QString&)));
10821169 this ->readerthread ->start ();
10831170}
10841171
@@ -1126,6 +1213,7 @@ void MainWindow::flash_result_ready() {
11261213 connect (this ->readerthread .get (), SIGNAL (read_result_ready ()), this , SLOT (verify_result_ready ()));
11271214 connect (this ->readerthread .get (), SIGNAL (read_block_start (uint,uint)), this , SLOT (verify_block_start (uint,uint)));
11281215 connect (this ->readerthread .get (), SIGNAL (read_block_done (uint,uint)), this , SLOT (verify_block_done (uint,uint)));
1216+ connect (this ->readerthread .get (), SIGNAL (thread_abort (const QString&)), this , SLOT (thread_abort (const QString&)));
11291217 this ->readerthread ->start ();
11301218}
11311219
@@ -1193,3 +1281,29 @@ void MainWindow::verify_result_ready() {
11931281 // this->enable_all_buttons();
11941282 this ->progress_bar_load ->reset ();
11951283}
1284+
1285+ void MainWindow::thread_abort (const QString& error) {
1286+ // clean up threads
1287+ if (this ->readerthread ) {
1288+ while (!this ->readerthread ->isFinished ()) {}
1289+ this ->readerthread .reset (); // delete object
1290+ }
1291+
1292+ if (this ->flashthread ) {
1293+ while (!this ->flashthread ->isFinished ()) {}
1294+ this ->flashthread .reset (); // delete object
1295+ }
1296+
1297+ if (this ->cartridgereaderthread ) {
1298+ while (!this ->cartridgereaderthread ->isFinished ()) {}
1299+ this ->cartridgereaderthread .reset (); // delete object
1300+ }
1301+
1302+ // throw error message
1303+ this ->raise_error_window (QMessageBox::Critical,
1304+ " Operation terminated unexpectedly. Please carefully "
1305+ " check all settings and verify that the chip is "
1306+ " properly inserted into the socket."
1307+ " \n\n Error message: " + error
1308+ );
1309+ }
0 commit comments