00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifdef HAVE_CONFIG_H
00026 #include <config.h>
00027 #endif
00028
00029 #include <wx/wxprec.h>
00030
00031 #ifndef WX_PRECOMP
00032 #include <wx/wx.h>
00033 #endif
00034
00035 #include <cstring>
00036 #include <fstream>
00037
00038 #include "model/SaveSlot.hh"
00039 #include "model/SRAMFile.hh"
00040
00041 using namespace emuWorks;
00042
00043 SRAMFile::SRAMFile(wxString &filename) {
00044 current = -1;
00045 load(filename);
00046 }
00047
00048 SRAMFile::~SRAMFile() {
00049 delete file;
00050 delete data;
00051 delete games[0];
00052 delete games[1];
00053 delete games[2];
00054 }
00055
00056 bool SRAMFile::isModified() {
00057 if (current == -1) {
00058 return false;
00059 }
00060
00061 for (int game = 0; game < 3; game++) {
00062 if (games[game]->isModified()) {
00063 return true;
00064 }
00065 }
00066
00067 return false;
00068 }
00069
00070 SaveSlot *SRAMFile::getCurrentGame() {
00071 if (current == -1) {
00072 return 0;
00073 }
00074
00075 return games[current];
00076 }
00077
00078 bool SRAMFile::setCurrentGame(unsigned int current) {
00079 if (current > 2) {
00080 return false;
00081 }
00082
00083 if (!isValidGame(current)) {
00084 return false;
00085 }
00086
00087 this->current = current;
00088 return true;
00089 }
00090
00091 bool SRAMFile::isValidGame(int game) {
00092 return games[game]->isValid();
00093 }
00094
00095 bool SRAMFile::save() {
00096 return save(*file);
00097 }
00098
00099 bool SRAMFile::save(wxString &filename) {
00100 for (int game = 0; game < 3; game++) {
00101 if (isValidGame(game)) {
00102 memcpy((data + GAME_OFFSET + (game * GAME_SIZE)),
00103 games[game]->nvram,
00104 GAME_SIZE);
00105 }
00106 }
00107
00108 std::ofstream out(filename.mb_str(), std::ios::binary | std::ios::out);
00109
00110 if (!out) {
00111 wxMessageBox(wxT("Unable to open the SRAM file."),
00112 wxT("File Open Error"), wxOK | wxICON_ERROR);
00113
00114 return false;
00115 }
00116
00117 out.write(data, SRAM_SIZE);
00118
00119 if (out.rdstate() & std::ios::failbit) {
00120 wxMessageBox(wxT("Unable to write to the SRAM file."),
00121 wxT("File I/O error"), wxOK | wxICON_ERROR);
00122
00123 out.close();
00124 return false;
00125 }
00126
00127 out.close();
00128
00129 games[0]->setModified(false);
00130 games[1]->setModified(false);
00131 games[2]->setModified(false);
00132
00133 return true;
00134 }
00135
00136 void SRAMFile::load(wxString &filename) {
00137 std::ifstream in(filename.mb_str(), std::ios::in | std::ios::binary);
00138
00139 if (!in) {
00140 wxMessageBox(wxT("Unable to open the SRAM file."),
00141 wxT("File Open Error"), wxOK | wxICON_ERROR);
00142 return;
00143 }
00144
00145 data = new char[SRAM_SIZE];
00146 in.read(data, SRAM_SIZE);
00147
00148 if (in.rdstate() & std::ios::failbit) {
00149 wxMessageBox(wxT("Unable to read the SRAM file."),
00150 wxT("File I/O Error"), wxOK | wxICON_ERROR);
00151
00152 in.close();
00153 delete data;
00154
00155 return;
00156 }
00157
00158 in.close();
00159
00160 for (int slot = 2; slot >= 0; slot--) {
00161 games[slot] = new SaveSlot(data + GAME_OFFSET + (slot * GAME_SIZE));
00162
00163 if (games[slot]->isValid()) {
00164 current = slot;
00165 }
00166 }
00167
00168 file = new wxString(filename);
00169
00170 if (current != -1) {
00171 wxString bakfile = filename + wxT(".bak");
00172 std::ofstream out(bakfile.mb_str(), std::ios::out | std::ios::binary);
00173
00174 if (out) {
00175 out.write(data, SRAM_SIZE);
00176 out.close();
00177 }
00178 }
00179 }
00180