Duckstation/src/core/memory_card.h

94 lines
1.6 KiB
C
Raw Normal View History

2019-09-29 15:07:38 +00:00
#pragma once
#include "common/bitfield.h"
#include "pad_device.h"
#include <array>
2019-10-27 06:45:23 +00:00
#include <memory>
#include <string>
#include <string_view>
class System;
2019-09-29 15:07:38 +00:00
class MemoryCard final : public PadDevice
{
public:
enum : u32
{
DATA_SIZE = 128 * 1024, // 1mbit
SECTOR_SIZE = 128,
NUM_SECTORS = DATA_SIZE / SECTOR_SIZE
};
2019-10-27 06:45:23 +00:00
MemoryCard(System* system);
2019-09-29 15:07:38 +00:00
~MemoryCard() override;
2019-10-27 06:45:23 +00:00
static std::shared_ptr<MemoryCard> Create(System* system);
static std::shared_ptr<MemoryCard> Open(System* system, std::string_view filename);
2019-09-29 15:07:38 +00:00
2019-09-29 15:59:35 +00:00
void Reset() override;
bool DoState(StateWrapper& sw) override;
2019-09-29 15:07:38 +00:00
void ResetTransferState() override;
bool Transfer(const u8 data_in, u8* data_out) override;
2019-09-29 15:53:47 +00:00
void Format();
2019-09-29 15:07:38 +00:00
private:
union FLAG
{
u8 bits;
BitField<u8, bool, 3, 1> no_write_yet;
BitField<u8, bool, 2, 1> write_error;
};
FLAG m_FLAG = {};
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,
};
2019-09-29 15:53:47 +00:00
static u8 ChecksumFrame(const u8* fptr);
u8* GetSectorPtr(u32 sector);
2019-10-27 06:45:23 +00:00
bool LoadFromFile();
bool SaveToFile();
System* m_system;
2019-09-29 15:07:38 +00:00
State m_state = State::Idle;
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
std::array<u8, DATA_SIZE> m_data{};
2019-10-27 06:45:23 +00:00
std::string m_filename;
2019-09-29 15:07:38 +00:00
};