1 /*	Copyright 2012 Theo Berkau <cwx@cyberwarriorx.com>
2 
3 	This file is part of Yabause.
4 
5 	Yabause is free software; you can redistribute it and/or modify
6 	it under the terms of the GNU General Public License as published by
7 	the Free Software Foundation; either version 2 of the License, or
8 	(at your option) any later version.
9 
10 	Yabause 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
13 	GNU General Public License for more details.
14 
15 	You should have received a copy of the GNU General Public License
16 	along with Yabause; if not, write to the Free Software
17 	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
18 */
19 #include "UIMemoryTransfer.h"
20 #include "UIHexInput.h"
21 #include "../CommonDialogs.h"
22 
23 #include <QPushButton>
24 
UIMemoryTransfer(YabauseThread * mYabauseThread,QWidget * p)25 UIMemoryTransfer::UIMemoryTransfer( YabauseThread *mYabauseThread, QWidget* p )
26 	: QDialog( p )
27 {
28 	// setup dialog
29 	setupUi( this );
30 
31    this->mYabauseThread = mYabauseThread;
32 
33    leStartAddress->setValidator(new HexValidator(0x00000000, 0xFFFFFFFF, leStartAddress));
34    leEndAddress->setValidator(new HexValidator(0x00000000, 0xFFFFFFFF, leEndAddress));
35 
36    leStartAddress->setText("06004000");
37    leEndAddress->setText("06100000");
38 
39    cbPC->setCheckState(Qt::Checked);
40    rbUpload->setChecked(true);
41 
42 	// retranslate widgets
43 	QtYabause::retranslateWidget( this );
44 }
45 
areSettingsValid()46 bool UIMemoryTransfer::areSettingsValid()
47 {
48    if (leFile->text().length() == 0)
49       return false;
50    if (leStartAddress->text().length() == 0)
51       return false;
52 
53    bool result;
54    u32 startAddress = leStartAddress->text().toUInt(&result, 16);
55    if (!result)
56       return false;
57 
58    if (rbDownload->isChecked())
59    {
60       if (leEndAddress->text().length() == 0)
61          return false;
62 
63       u32 endAddress = leEndAddress->text().toUInt(&result, 16);
64       if (!result)
65          return false;
66 
67       if (startAddress >= endAddress)
68          return false;
69    }
70 
71    return true;
72 }
73 
on_leFile_textChanged(const QString & text)74 void UIMemoryTransfer::on_leFile_textChanged( const QString & text )
75 {
76    dbbButtons->button(QDialogButtonBox::Ok)->setEnabled(areSettingsValid());
77 }
78 
on_leStartAddress_textChanged(const QString & text)79 void UIMemoryTransfer::on_leStartAddress_textChanged( const QString & text )
80 {
81    dbbButtons->button(QDialogButtonBox::Ok)->setEnabled(areSettingsValid());
82 }
83 
on_leEndAddress_textChanged(const QString & text)84 void UIMemoryTransfer::on_leEndAddress_textChanged( const QString & text )
85 {
86    dbbButtons->button(QDialogButtonBox::Ok)->setEnabled(areSettingsValid());
87 }
88 
on_rbUpload_toggled(bool checked)89 void UIMemoryTransfer::on_rbUpload_toggled(bool checked)
90 {
91 	leStartAddress->setEnabled(true);
92    leEndAddress->setEnabled(checked != true);
93    cbPC->setEnabled(checked == true);
94 
95    dbbButtons->button(QDialogButtonBox::Ok)->setEnabled(areSettingsValid());
96 }
97 
on_tbBrowse_clicked()98 void UIMemoryTransfer::on_tbBrowse_clicked()
99 {
100    if (rbDownload->isChecked())
101    {
102       const QString s = CommonDialogs::getSaveFileName( leFile->text(), QtYabause::translate( "Choose a location for binary file" ), QtYabause::translate( "Binary Files (*.bin)" ) );
103       if ( !s.isNull() )
104          leFile->setText( s );
105    }
106    else
107    {
108       const QString s = CommonDialogs::getOpenFileName( leFile->text(), QtYabause::translate( "Choose a binary or program file" ), QtYabause::translate( "Binary Files (*.bin);;COFF Program Files (*.cof *.coff);;ELF Program Files (*.elf);;All Files (*)" ) );
109       if ( !s.isNull() )
110 		{
111          leFile->setText( s );
112 			if (s.endsWith(".cof", Qt::CaseInsensitive) ||
113 				 s.endsWith(".coff", Qt::CaseInsensitive) ||
114 				 s.endsWith(".elf", Qt::CaseInsensitive))
115 			{
116 				 cbPC->setCheckState(Qt::Checked);
117 			    cbPC->setEnabled(false);
118 				 leStartAddress->setEnabled(false);
119 			}
120 			else
121 			{
122 				cbPC->setEnabled(true);
123 				leStartAddress->setEnabled(true);
124 			}
125 		}
126    }
127 }
128 
accept()129 void UIMemoryTransfer::accept()
130 {
131    u32 startAddress = leStartAddress->text().toUInt(0, 16);
132    u32 endAddress = leEndAddress->text().toUInt(0, 16);
133 
134    if (rbDownload->isChecked() && startAddress >= endAddress)
135    {
136       CommonDialogs::information(QtYabause::translate("Invalid Start/End Address Combination"), QtYabause::translate("Error"));
137       return;
138    }
139 
140 	if (mYabauseThread)
141       mYabauseThread->pauseEmulation( false, false );
142 
143    if (rbDownload->isChecked())
144    {
145       // Let's do a ram dump
146       MappedMemorySave(MSH2, leFile->text().toLatin1(), startAddress, endAddress - startAddress);
147    }
148    else
149    {
150       // upload to ram and possibly execute
151 
152       // Is this a program?
153       if (cbPC->checkState() == Qt::Checked)
154       {
155          MappedMemoryLoadExec(leFile->text().toLatin1(), startAddress);
156       }
157       else
158       {
159          MappedMemoryLoad(MSH2, leFile->text().toLatin1(), startAddress);
160       }
161    }
162 
163 	QDialog::accept();
164 }
165