-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
EXI_DeviceMemoryCard.cpp
479 lines (413 loc) · 11.4 KB
/
EXI_DeviceMemoryCard.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
// Copyright (C) 2003 Dolphin Project.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License 2.0 for more details.
// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/
// Official SVN repository and contact information can be found at
// http://code.google.com/p/dolphin-emu/
#include "Common.h"
#include "FileUtil.h"
#include "StringUtil.h"
#include "../Core.h"
#include "../CoreTiming.h"
#include "../ConfigManager.h"
#include "../Movie.h"
#include "EXI.h"
#include "EXI_Device.h"
#include "EXI_DeviceMemoryCard.h"
#include "Sram.h"
#include "GCMemcard.h"
#define MC_STATUS_BUSY 0x80
#define MC_STATUS_UNLOCKED 0x40
#define MC_STATUS_SLEEP 0x20
#define MC_STATUS_ERASEERROR 0x10
#define MC_STATUS_PROGRAMEERROR 0x08
#define MC_STATUS_READY 0x01
#define SIZE_TO_Mb (1024 * 8 * 16)
#define MC_HDR_SIZE 0xA000
void CEXIMemoryCard::FlushCallback(u64 userdata, int cyclesLate)
{
// note that userdata is forbidden to be a pointer, due to the implemenation of EventDoState
int card_index = (int)userdata;
CEXIMemoryCard* pThis = (CEXIMemoryCard*)ExpansionInterface::FindDevice(EXIDEVICE_MEMORYCARD, card_index);
if (pThis)
pThis->Flush();
}
CEXIMemoryCard::CEXIMemoryCard(const int index)
: card_index(index)
, m_bDirty(false)
{
m_strFilename = (card_index == 0) ? SConfig::GetInstance().m_strMemoryCardA : SConfig::GetInstance().m_strMemoryCardB;
if (Movie::g_bMemcard && Movie::IsPlayingInput() && Movie::IsConfigSaved() && Movie::g_bBlankMC)
m_strFilename = "Movie.raw";
// we're potentially leaking events here, since there's no UnregisterEvent until emu shutdown, but I guess it's inconsequential
et_this_card = CoreTiming::RegisterEvent((card_index == 0) ? "memcardA" : "memcardB", FlushCallback);
interruptSwitch = 0;
m_bInterruptSet = 0;
command = 0;
status = MC_STATUS_BUSY | MC_STATUS_UNLOCKED | MC_STATUS_READY;
m_uPosition = 0;
memset(programming_buffer, 0, sizeof(programming_buffer));
formatDelay = 0;
//Nintendo Memory Card EXI IDs
//0x00000004 Memory Card 59 4Mbit
//0x00000008 Memory Card 123 8Mb
//0x00000010 Memory Card 251 16Mb
//0x00000020 Memory Card 507 32Mb
//0x00000040 Memory Card 1019 64Mb
//0x00000080 Memory Card 2043 128Mb
//0x00000510 16Mb "bigben" card
//card_id = 0xc243;
card_id = 0xc221; // It's a nintendo brand memcard
File::IOFile pFile(m_strFilename, "rb");
if (pFile)
{
// Measure size of the memcard file.
memory_card_size = (int)pFile.GetSize();
nintendo_card_id = memory_card_size / SIZE_TO_Mb;
memory_card_content = new u8[memory_card_size];
memset(memory_card_content, 0xFF, memory_card_size);
INFO_LOG(EXPANSIONINTERFACE, "Reading memory card %s", m_strFilename.c_str());
pFile.ReadBytes(memory_card_content, memory_card_size);
}
else
{
// Create a new 128Mb memcard
nintendo_card_id = 0x00000080;
memory_card_size = nintendo_card_id * SIZE_TO_Mb;
memory_card_content = new u8[memory_card_size];
GCMemcard::Format(memory_card_content, m_strFilename.find(".JAP.raw") != std::string::npos, nintendo_card_id);
memset(memory_card_content+MC_HDR_SIZE, 0xFF, memory_card_size-MC_HDR_SIZE);
WARN_LOG(EXPANSIONINTERFACE, "No memory card found. Will create new.");
}
SetCardFlashID(memory_card_content, card_index);
}
void innerFlush(FlushData* data)
{
File::IOFile pFile(data->filename, "r+b");
if (!pFile)
{
std::string dir;
SplitPath(data->filename, &dir, 0, 0);
if (!File::IsDirectory(dir))
File::CreateFullPath(dir);
pFile.Open(data->filename, "wb");
}
if (!pFile) // Note - pFile changed inside above if
{
PanicAlertT("Could not write memory card file %s.\n\n"
"Are you running Dolphin from a CD/DVD, or is the save file maybe write protected?", data->filename.c_str());
return;
}
pFile.WriteBytes(data->memcardContent, data->memcardSize);
if (!data->bExiting)
Core::DisplayMessage(StringFromFormat("Wrote memory card %c contents to %s",
data->memcardIndex ? 'B' : 'A', data->filename.c_str()).c_str(), 4000);
return;
}
// Flush memory card contents to disc
void CEXIMemoryCard::Flush(bool exiting)
{
if(!m_bDirty)
return;
if (flushThread.joinable())
{
flushThread.join();
}
if(!exiting)
Core::DisplayMessage(StringFromFormat("Writing to memory card %c", card_index ? 'B' : 'A'), 1000);
flushData.filename = m_strFilename;
flushData.memcardContent = memory_card_content;
flushData.memcardIndex = card_index;
flushData.memcardSize = memory_card_size;
flushData.bExiting = exiting;
flushThread = std::thread(innerFlush, &flushData);
if (exiting)
flushThread.join();
m_bDirty = false;
}
CEXIMemoryCard::~CEXIMemoryCard()
{
CoreTiming::RemoveEvent(et_this_card);
Flush(true);
delete[] memory_card_content;
memory_card_content = NULL;
if (flushThread.joinable())
{
flushThread.join();
}
}
bool CEXIMemoryCard::IsPresent()
{
return true;
}
void CEXIMemoryCard::SetCS(int cs)
{
// So that memory card won't be invalidated during flushing
if (flushThread.joinable())
{
flushThread.join();
}
if (cs) // not-selected to selected
m_uPosition = 0;
else
{
switch (command)
{
case cmdSectorErase:
if (m_uPosition > 2)
{
memset(memory_card_content + (address & (memory_card_size-1)), 0xFF, 0x2000);
status |= MC_STATUS_BUSY;
status &= ~MC_STATUS_READY;
//???
status |= MC_STATUS_READY;
status &= ~MC_STATUS_BUSY;
m_bInterruptSet = 1;
m_bDirty = true;
}
break;
case cmdChipErase:
if (m_uPosition > 2)
{
memset(memory_card_content, 0xFF, memory_card_size);
status &= ~MC_STATUS_BUSY;
m_bDirty = true;
}
break;
case cmdPageProgram:
if (m_uPosition >= 5)
{
int count = m_uPosition - 5;
int i=0;
status &= ~0x80;
while (count--)
{
memory_card_content[address] = programming_buffer[i++];
i &= 127;
address = (address & ~0x1FF) | ((address+1) & 0x1FF);
}
status |= MC_STATUS_READY;
status &= ~MC_STATUS_BUSY;
m_bInterruptSet = 1;
m_bDirty = true;
}
// Page written to memory card, not just to buffer - let's schedule a flush 0.5b cycles into the future (1 sec)
// But first we unschedule already scheduled flushes - no point in flushing once per page for a large write.
CoreTiming::RemoveEvent(et_this_card);
CoreTiming::ScheduleEvent(500000000, et_this_card, (u64)card_index);
break;
}
}
}
void CEXIMemoryCard::Update()
{
if (formatDelay)
{
formatDelay--;
if (!formatDelay)
{
status |= MC_STATUS_READY;
status &= ~MC_STATUS_BUSY;
m_bInterruptSet = 1;
}
}
}
bool CEXIMemoryCard::IsInterruptSet()
{
if (interruptSwitch)
return m_bInterruptSet;
return false;
}
void CEXIMemoryCard::TransferByte(u8 &byte)
{
DEBUG_LOG(EXPANSIONINTERFACE, "EXI MEMCARD: > %02x", byte);
if (m_uPosition == 0)
{
command = byte; // first byte is command
byte = 0xFF; // would be tristate, but we don't care.
switch (command) // This seems silly, do we really need it?
{
case cmdNintendoID:
case cmdReadArray:
case cmdArrayToBuffer:
case cmdSetInterrupt:
case cmdWriteBuffer:
case cmdReadStatus:
case cmdReadID:
case cmdReadErrorBuffer:
case cmdWakeUp:
case cmdSleep:
case cmdClearStatus:
case cmdSectorErase:
case cmdPageProgram:
case cmdExtraByteProgram:
case cmdChipErase:
INFO_LOG(EXPANSIONINTERFACE, "EXI MEMCARD: command %02x at position 0. seems normal.", command);
break;
default:
WARN_LOG(EXPANSIONINTERFACE, "EXI MEMCARD: command %02x at position 0", command);
break;
}
if (command == cmdClearStatus)
{
status &= ~MC_STATUS_PROGRAMEERROR;
status &= ~MC_STATUS_ERASEERROR;
status |= MC_STATUS_READY;
m_bInterruptSet = 0;
byte = 0xFF;
m_uPosition = 0;
}
}
else
{
switch (command)
{
case cmdNintendoID:
//
// nintendo card:
// 00 | 80 00 00 00 10 00 00 00
// "bigben" card:
// 00 | ff 00 00 05 10 00 00 00 00 00 00 00 00 00 00
// we do it the nintendo way.
if (m_uPosition == 1)
byte = 0x80; // dummy cycle
else
byte = (u8)(nintendo_card_id >> (24-(((m_uPosition-2) & 3) * 8)));
break;
case cmdReadArray:
switch (m_uPosition)
{
case 1: // AD1
address = byte << 17;
byte = 0xFF;
break;
case 2: // AD2
address |= byte << 9;
break;
case 3: // AD3
address |= (byte & 3) << 7;
break;
case 4: // BA
address |= (byte & 0x7F);
break;
}
if (m_uPosition > 1) // not specified for 1..8, anyway
{
byte = memory_card_content[address & (memory_card_size-1)];
// after 9 bytes, we start incrementing the address,
// but only the sector offset - the pointer wraps around
if (m_uPosition >= 9)
address = (address & ~0x1FF) | ((address+1) & 0x1FF);
}
break;
case cmdReadStatus:
// (unspecified for byte 1)
byte = status;
break;
case cmdReadID:
if (m_uPosition == 1) // (unspecified)
byte = (u8)(card_id >> 8);
else
byte = (u8)((m_uPosition & 1) ? (card_id) : (card_id >> 8));
break;
case cmdSectorErase:
switch (m_uPosition)
{
case 1: // AD1
address = byte << 17;
break;
case 2: // AD2
address |= byte << 9;
break;
}
byte = 0xFF;
break;
case cmdSetInterrupt:
if (m_uPosition == 1)
{
interruptSwitch = byte;
}
byte = 0xFF;
break;
case cmdChipErase:
byte = 0xFF;
break;
case cmdPageProgram:
switch (m_uPosition)
{
case 1: // AD1
address = byte << 17;
break;
case 2: // AD2
address |= byte << 9;
break;
case 3: // AD3
address |= (byte & 3) << 7;
break;
case 4: // BA
address |= (byte & 0x7F);
break;
}
if(m_uPosition >= 5)
programming_buffer[((m_uPosition - 5) & 0x7F)] = byte; // wrap around after 128 bytes
byte = 0xFF;
break;
default:
WARN_LOG(EXPANSIONINTERFACE, "EXI MEMCARD: unknown command byte %02x\n", byte);
byte = 0xFF;
}
}
m_uPosition++;
DEBUG_LOG(EXPANSIONINTERFACE, "EXI MEMCARD: < %02x", byte);
}
void CEXIMemoryCard::PauseAndLock(bool doLock, bool unpauseOnUnlock)
{
if (doLock)
{
// we don't exactly have anything to pause,
// but let's make sure the flush thread isn't running.
if (flushThread.joinable())
{
flushThread.join();
}
}
}
void CEXIMemoryCard::DoState(PointerWrap &p)
{
// for movie sync, we need to save/load memory card contents (and other data) in savestates.
// otherwise, we'll assume the user wants to keep their memcards and saves separate,
// unless we're loading (in which case we let the savestate contents decide, in order to stay aligned with them).
bool storeContents = (Movie::IsRecordingInput() || Movie::IsPlayingInput());
p.Do(storeContents);
if (storeContents)
{
p.Do(interruptSwitch);
p.Do(m_bInterruptSet);
p.Do(command);
p.Do(status);
p.Do(m_uPosition);
p.Do(programming_buffer);
p.Do(formatDelay);
p.Do(m_bDirty);
p.Do(address);
p.Do(nintendo_card_id);
p.Do(card_id);
p.Do(memory_card_size);
p.DoArray(memory_card_content, memory_card_size);
p.Do(card_index);
}
}
IEXIDevice* CEXIMemoryCard::FindDevice(TEXIDevices device_type, int customIndex)
{
if (device_type != m_deviceType)
return NULL;
if (customIndex != card_index)
return NULL;
return this;
}