2022-12-04 11:03:45 +00:00
|
|
|
// SPDX-FileCopyrightText: 2019-2022 Connor McLaughlin <stenzek@gmail.com>
|
|
|
|
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
|
|
|
|
|
2019-09-29 15:07:38 +00:00
|
|
|
#pragma once
|
|
|
|
#include "common/bitfield.h"
|
2019-12-08 14:51:52 +00:00
|
|
|
#include "controller.h"
|
2020-09-18 14:26:19 +00:00
|
|
|
#include "memory_card_image.h"
|
2019-09-29 15:07:38 +00:00
|
|
|
#include <array>
|
2019-10-27 06:45:23 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <string_view>
|
|
|
|
|
2020-04-08 14:14:26 +00:00
|
|
|
class TimingEvent;
|
2019-09-29 15:07:38 +00:00
|
|
|
|
2019-12-08 14:51:52 +00:00
|
|
|
class MemoryCard final
|
2019-09-29 15:07:38 +00:00
|
|
|
{
|
|
|
|
public:
|
2020-07-31 07:09:18 +00:00
|
|
|
MemoryCard();
|
2019-12-08 14:51:52 +00:00
|
|
|
~MemoryCard();
|
2019-09-29 15:07:38 +00:00
|
|
|
|
2023-03-16 11:22:08 +00:00
|
|
|
static constexpr u32 STATE_SIZE = 1 + 1 + 2 + 1 + 1 + 1 + MemoryCardImage::DATA_SIZE + 1;
|
|
|
|
|
2021-05-16 06:50:28 +00:00
|
|
|
static std::string SanitizeGameTitleForFileName(const std::string_view& name);
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
static std::unique_ptr<MemoryCard> Create();
|
|
|
|
static std::unique_ptr<MemoryCard> Open(std::string_view filename);
|
2019-09-29 15:07:38 +00:00
|
|
|
|
2020-09-26 05:52:41 +00:00
|
|
|
const MemoryCardImage::DataArray& GetData() const { return m_data; }
|
2021-05-23 07:11:59 +00:00
|
|
|
MemoryCardImage::DataArray& GetData() { return m_data; }
|
2020-09-26 15:33:24 +00:00
|
|
|
const std::string& GetFilename() const { return m_filename; }
|
|
|
|
void SetFilename(std::string filename) { m_filename = std::move(filename); }
|
2020-09-26 05:52:41 +00:00
|
|
|
|
2019-12-08 14:51:52 +00:00
|
|
|
void Reset();
|
|
|
|
bool DoState(StateWrapper& sw);
|
2019-09-29 15:59:35 +00:00
|
|
|
|
2019-12-08 14:51:52 +00:00
|
|
|
void ResetTransferState();
|
|
|
|
bool Transfer(const u8 data_in, u8* data_out);
|
2019-09-29 15:07:38 +00:00
|
|
|
|
2019-09-29 15:53:47 +00:00
|
|
|
void Format();
|
|
|
|
|
2019-09-29 15:07:38 +00:00
|
|
|
private:
|
2020-04-08 14:14:26 +00:00
|
|
|
enum : u32
|
|
|
|
{
|
|
|
|
// save in three seconds, that should be long enough for everything to finish writing
|
|
|
|
SAVE_DELAY_IN_SECONDS = 5,
|
|
|
|
};
|
|
|
|
|
2019-09-29 15:07:38 +00:00
|
|
|
union FLAG
|
|
|
|
{
|
|
|
|
u8 bits;
|
|
|
|
|
|
|
|
BitField<u8, bool, 3, 1> no_write_yet;
|
|
|
|
BitField<u8, bool, 2, 1> write_error;
|
|
|
|
};
|
|
|
|
|
|
|
|
enum class State : u8
|
|
|
|
{
|
|
|
|
Idle,
|
2019-09-30 04:22:57 +00:00
|
|
|
Command,
|
2019-09-29 15:07:38 +00:00
|
|
|
|
|
|
|
ReadCardID1,
|
|
|
|
ReadCardID2,
|
|
|
|
ReadAddressMSB,
|
|
|
|
ReadAddressLSB,
|
|
|
|
ReadACK1,
|
|
|
|
ReadACK2,
|
|
|
|
ReadConfirmAddressMSB,
|
|
|
|
ReadConfirmAddressLSB,
|
|
|
|
ReadData,
|
|
|
|
ReadChecksum,
|
|
|
|
ReadEnd,
|
|
|
|
|
|
|
|
WriteCardID1,
|
|
|
|
WriteCardID2,
|
|
|
|
WriteAddressMSB,
|
|
|
|
WriteAddressLSB,
|
|
|
|
WriteData,
|
|
|
|
WriteChecksum,
|
|
|
|
WriteACK1,
|
|
|
|
WriteACK2,
|
|
|
|
WriteEnd,
|
|
|
|
};
|
|
|
|
|
2020-09-29 13:29:28 +00:00
|
|
|
static TickCount GetSaveDelayInTicks();
|
|
|
|
|
2019-10-27 06:45:23 +00:00
|
|
|
bool LoadFromFile();
|
2020-04-08 14:14:26 +00:00
|
|
|
bool SaveIfChanged(bool display_osd_message);
|
|
|
|
void QueueFileSave();
|
2019-10-27 06:45:23 +00:00
|
|
|
|
2020-04-08 14:14:26 +00:00
|
|
|
std::unique_ptr<TimingEvent> m_save_event;
|
2019-10-27 06:45:23 +00:00
|
|
|
|
2019-09-29 15:07:38 +00:00
|
|
|
State m_state = State::Idle;
|
2020-04-25 04:58:19 +00:00
|
|
|
FLAG m_FLAG = {};
|
2019-09-29 15:07:38 +00:00
|
|
|
u16 m_address = 0;
|
|
|
|
u8 m_sector_offset = 0;
|
|
|
|
u8 m_checksum = 0;
|
|
|
|
u8 m_last_byte = 0;
|
2019-10-27 06:45:23 +00:00
|
|
|
bool m_changed = false;
|
2019-09-29 15:07:38 +00:00
|
|
|
|
2020-09-18 14:26:19 +00:00
|
|
|
MemoryCardImage::DataArray m_data{};
|
2019-10-27 06:45:23 +00:00
|
|
|
|
|
|
|
std::string m_filename;
|
2019-09-29 15:07:38 +00:00
|
|
|
};
|