/
EXI_DeviceMemoryCard.cpp
569 lines (509 loc) · 16.3 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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
// Copyright 2008 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#include <cstring>
#include <memory>
#include <string>
#include "Common/ChunkFile.h"
#include "Common/CommonPaths.h"
#include "Common/CommonTypes.h"
#include "Common/FileUtil.h"
#include "Common/IniFile.h"
#include "Common/Logging/Log.h"
#include "Common/NandPaths.h"
#include "Common/StringUtil.h"
#include "Core/ConfigManager.h"
#include "Core/CoreTiming.h"
#include "Core/HW/EXI.h"
#include "Core/HW/EXI_Channel.h"
#include "Core/HW/EXI_Device.h"
#include "Core/HW/EXI_DeviceMemoryCard.h"
#include "Core/HW/GCMemcard.h"
#include "Core/HW/GCMemcardDirectory.h"
#include "Core/HW/GCMemcardRaw.h"
#include "Core/HW/Memmap.h"
#include "Core/HW/Sram.h"
#include "Core/HW/SystemTimers.h"
#include "Core/Movie.h"
#include "DiscIO/Enums.h"
#include "DiscIO/NANDContentLoader.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)
static const u32 MC_TRANSFER_RATE_READ = 512 * 1024;
static const u32 MC_TRANSFER_RATE_WRITE = (u32)(96.125f * 1024.0f);
// Takes care of the nasty recovery of the 'this' pointer from card_index,
// stored in the userdata parameter of the CoreTiming event.
void CEXIMemoryCard::EventCompleteFindInstance(u64 userdata,
std::function<void(CEXIMemoryCard*)> callback)
{
int card_index = (int)userdata;
CEXIMemoryCard* pThis =
(CEXIMemoryCard*)ExpansionInterface::FindDevice(EXIDEVICE_MEMORYCARD, card_index);
if (pThis == nullptr)
{
pThis = (CEXIMemoryCard*)ExpansionInterface::FindDevice(EXIDEVICE_MEMORYCARDFOLDER, card_index);
}
if (pThis)
{
callback(pThis);
}
}
void CEXIMemoryCard::CmdDoneCallback(u64 userdata, s64 cyclesLate)
{
EventCompleteFindInstance(userdata, [](CEXIMemoryCard* instance) { instance->CmdDone(); });
}
void CEXIMemoryCard::TransferCompleteCallback(u64 userdata, s64 cyclesLate)
{
EventCompleteFindInstance(userdata,
[](CEXIMemoryCard* instance) { instance->TransferComplete(); });
}
CEXIMemoryCard::CEXIMemoryCard(const int index, bool gciFolder) : card_index(index)
{
struct
{
const char* done;
const char* transfer_complete;
} const event_names[] = {
{"memcardDoneA", "memcardTransferCompleteA"}, {"memcardDoneB", "memcardTransferCompleteB"},
};
if ((size_t)index >= ArraySize(event_names))
{
PanicAlertT("Trying to create invalid memory card index.");
}
// Because the memory cards can be changed while emulation is running, the callbacks may
// have already been registered by previous instances.
et_cmd_done = CoreTiming::RegisterEvent(event_names[index].done, CmdDoneCallback,
CoreTiming::Registration::ExpectExisting);
et_transfer_complete =
CoreTiming::RegisterEvent(event_names[index].transfer_complete, TransferCompleteCallback,
CoreTiming::Registration::ExpectExisting);
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));
// 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
// The following games have issues with memory cards bigger than 16Mb
// Darkened Skye GDQE6S GDQP6S
// WTA Tour Tennis GWTEA4 GWTJA4 GWTPA4
// Disney Sports : Skate Boarding GDXEA4 GDXPA4 GDXJA4
// Disney Sports : Soccer GDKEA4
// Use a 16Mb (251 block) memory card for these games
bool useMC251;
IniFile gameIni = SConfig::GetInstance().LoadGameIni();
gameIni.GetOrCreateSection("Core")->Get("MemoryCard251", &useMC251, false);
u16 sizeMb = useMC251 ? MemCard251Mb : MemCard2043Mb;
if (gciFolder)
{
SetupGciFolder(sizeMb);
}
else
{
SetupRawMemcard(sizeMb);
}
memory_card_size = memorycard->GetCardId() * SIZE_TO_Mb;
u8 header[20] = {0};
memorycard->Read(0, static_cast<s32>(ArraySize(header)), header);
SetCardFlashID(header, card_index);
}
void CEXIMemoryCard::SetupGciFolder(u16 sizeMb)
{
DiscIO::Country country_code = DiscIO::Country::COUNTRY_UNKNOWN;
auto strUniqueID = SConfig::GetInstance().m_strUniqueID;
u32 CurrentGameId = 0;
if (strUniqueID == TITLEID_SYSMENU_STRING)
{
const DiscIO::CNANDContentLoader& SysMenu_Loader =
DiscIO::CNANDContentManager::Access().GetNANDLoader(TITLEID_SYSMENU,
Common::FROM_SESSION_ROOT);
if (SysMenu_Loader.IsValid())
{
country_code = DiscIO::CountrySwitch(SysMenu_Loader.GetCountryChar());
}
}
else if (strUniqueID.length() >= 4)
{
country_code = DiscIO::CountrySwitch(strUniqueID.at(3));
CurrentGameId = BE32((u8*)strUniqueID.c_str());
}
bool ascii = true;
std::string strDirectoryName = File::GetUserPath(D_GCUSER_IDX);
switch (country_code)
{
case DiscIO::Country::COUNTRY_JAPAN:
ascii = false;
strDirectoryName += JAP_DIR DIR_SEP;
break;
case DiscIO::Country::COUNTRY_USA:
strDirectoryName += USA_DIR DIR_SEP;
break;
case DiscIO::Country::COUNTRY_UNKNOWN:
{
// The current game's region is not passed down to the EXI device level.
// Usually, we can retrieve the region from SConfig::GetInstance().m_strUniqueId.
// The Wii System Menu requires a lookup based on the version number.
// This is not possible in some cases ( e.g. FIFO logs, homebrew elf/dol files).
// Instead, we then lookup the region from the memory card name
// Earlier in the boot process the region is added to the memory card name (This is done by the
// function checkMemcardPath)
// For now take advantage of this.
// Future options:
// Set memory card directory path in the checkMemcardPath function.
// or Add region to SConfig::GetInstance().
// or Pass region down to the EXI device creation.
std::string memcardFilename = (card_index == 0) ? SConfig::GetInstance().m_strMemoryCardA :
SConfig::GetInstance().m_strMemoryCardB;
std::string region = memcardFilename.substr(memcardFilename.size() - 7, 3);
if (region == JAP_DIR)
{
country_code = DiscIO::Country::COUNTRY_JAPAN;
ascii = false;
strDirectoryName += JAP_DIR DIR_SEP;
break;
}
else if (region == USA_DIR)
{
country_code = DiscIO::Country::COUNTRY_USA;
strDirectoryName += USA_DIR DIR_SEP;
break;
}
}
default:
country_code = DiscIO::Country::COUNTRY_EUROPE;
strDirectoryName += EUR_DIR DIR_SEP;
}
strDirectoryName += StringFromFormat("Card %c", 'A' + card_index);
if (!File::Exists(strDirectoryName)) // first use of memcard folder, migrate automatically
{
MigrateFromMemcardFile(strDirectoryName + DIR_SEP, card_index);
}
else if (!File::IsDirectory(strDirectoryName))
{
if (File::Rename(strDirectoryName, strDirectoryName + ".original"))
{
PanicAlertT("%s was not a directory, moved to *.original", strDirectoryName.c_str());
MigrateFromMemcardFile(strDirectoryName + DIR_SEP, card_index);
}
else // we tried but the user wants to crash
{
// TODO more user friendly abort
PanicAlertT("%s is not a directory, failed to move to *.original.\n Verify your write "
"permissions or move "
"the file outside of Dolphin",
strDirectoryName.c_str());
exit(0);
}
}
memorycard = std::make_unique<GCMemcardDirectory>(strDirectoryName + DIR_SEP, card_index, sizeMb,
ascii, country_code, CurrentGameId);
}
void CEXIMemoryCard::SetupRawMemcard(u16 sizeMb)
{
std::string filename = (card_index == 0) ? SConfig::GetInstance().m_strMemoryCardA :
SConfig::GetInstance().m_strMemoryCardB;
if (Movie::IsPlayingInput() && Movie::IsConfigSaved() && Movie::IsUsingMemcard(card_index) &&
Movie::IsStartingFromClearSave())
filename = File::GetUserPath(D_GCUSER_IDX) +
StringFromFormat("Movie%s.raw", (card_index == 0) ? "A" : "B");
if (sizeMb == MemCard251Mb)
{
filename.insert(filename.find_last_of("."), ".251");
}
memorycard = std::make_unique<MemoryCard>(filename, card_index, sizeMb);
}
CEXIMemoryCard::~CEXIMemoryCard()
{
CoreTiming::RemoveEvent(et_cmd_done);
CoreTiming::RemoveEvent(et_transfer_complete);
}
bool CEXIMemoryCard::UseDelayedTransferCompletion() const
{
return true;
}
bool CEXIMemoryCard::IsPresent() const
{
return true;
}
void CEXIMemoryCard::CmdDone()
{
status |= MC_STATUS_READY;
status &= ~MC_STATUS_BUSY;
m_bInterruptSet = 1;
ExpansionInterface::UpdateInterrupts();
}
void CEXIMemoryCard::TransferComplete()
{
// Transfer complete, send interrupt
ExpansionInterface::GetChannel(card_index)->SendTransferComplete();
}
void CEXIMemoryCard::CmdDoneLater(u64 cycles)
{
CoreTiming::RemoveEvent(et_cmd_done);
CoreTiming::ScheduleEvent((int)cycles, et_cmd_done, (u64)card_index);
}
void CEXIMemoryCard::SetCS(int cs)
{
if (cs) // not-selected to selected
{
m_uPosition = 0;
}
else
{
switch (command)
{
case cmdSectorErase:
if (m_uPosition > 2)
{
memorycard->ClearBlock(address & (memory_card_size - 1));
status |= MC_STATUS_BUSY;
status &= ~MC_STATUS_READY;
//???
CmdDoneLater(5000);
}
break;
case cmdChipErase:
if (m_uPosition > 2)
{
// TODO: Investigate on HW, I (LPFaint99) believe that this only
// erases the system area (Blocks 0-4)
memorycard->ClearAll();
status &= ~MC_STATUS_BUSY;
}
break;
case cmdPageProgram:
if (m_uPosition >= 5)
{
int count = m_uPosition - 5;
int i = 0;
status &= ~0x80;
while (count--)
{
memorycard->Write(address, 1, &(programming_buffer[i++]));
i &= 127;
address = (address & ~0x1FF) | ((address + 1) & 0x1FF);
}
CmdDoneLater(5000);
}
break;
}
}
}
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)(memorycard->GetCardId() >> (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
{
memorycard->Read(address & (memory_card_size - 1), 1, &byte);
// 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::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::IsMovieActive());
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(address);
memorycard->DoState(p);
p.Do(card_index);
}
}
IEXIDevice* CEXIMemoryCard::FindDevice(TEXIDevices device_type, int customIndex)
{
if (device_type != m_deviceType)
return nullptr;
if (customIndex != card_index)
return nullptr;
return this;
}
// DMA reads are preceded by all of the necessary setup via IMMRead
// read all at once instead of single byte at a time as done by IEXIDevice::DMARead
void CEXIMemoryCard::DMARead(u32 _uAddr, u32 _uSize)
{
memorycard->Read(address, _uSize, Memory::GetPointer(_uAddr));
if ((address + _uSize) % BLOCK_SIZE == 0)
{
DEBUG_LOG(EXPANSIONINTERFACE, "reading from block: %x", address / BLOCK_SIZE);
}
// Schedule transfer complete later based on read speed
CoreTiming::ScheduleEvent(_uSize * (SystemTimers::GetTicksPerSecond() / MC_TRANSFER_RATE_READ),
et_transfer_complete, (u64)card_index);
}
// DMA write are preceded by all of the necessary setup via IMMWrite
// write all at once instead of single byte at a time as done by IEXIDevice::DMAWrite
void CEXIMemoryCard::DMAWrite(u32 _uAddr, u32 _uSize)
{
memorycard->Write(address, _uSize, Memory::GetPointer(_uAddr));
if (((address + _uSize) % BLOCK_SIZE) == 0)
{
DEBUG_LOG(EXPANSIONINTERFACE, "writing to block: %x", address / BLOCK_SIZE);
}
// Schedule transfer complete later based on write speed
CoreTiming::ScheduleEvent(_uSize * (SystemTimers::GetTicksPerSecond() / MC_TRANSFER_RATE_WRITE),
et_transfer_complete, (u64)card_index);
}