openMSX
AmdFlash.hh
Go to the documentation of this file.
1 #ifndef AMDFLASH_HH
2 #define AMDFLASH_HH
3 
4 #include "MemBuffer.hh"
5 #include "openmsx.hh"
6 #include "serialize_meta.hh"
7 #include "span.hh"
8 #include <memory>
9 
10 namespace openmsx {
11 
12 class MSXMotherBoard;
13 class Rom;
14 class SRAM;
15 class DeviceConfig;
16 
17 class AmdFlash
18 {
19 public:
20  struct SectorInfo {
21  unsigned size;
23  };
24  enum class Addressing {
25  BITS_11,
26  BITS_12,
27  };
28  enum class Load {
29  NORMAL,
30  DONT, // don't load nor save modified flash content
31  };
32 
49  AmdFlash(const Rom& rom, span<const SectorInfo> sectorInfo,
50  word ID, Addressing addressing,
51  const DeviceConfig& config, Load load = Load::NORMAL);
52  AmdFlash(const std::string& name, span<const SectorInfo> sectorInfo,
53  word ID, Addressing addressing,
54  const DeviceConfig& config);
56 
57  void reset();
64  void setVppWpPinLow(bool value) { vppWpPinLow = value; }
65 
66  [[nodiscard]] unsigned getSize() const { return size; }
67  [[nodiscard]] byte read(unsigned address) const;
68  [[nodiscard]] byte peek(unsigned address) const;
69  void write(unsigned address, byte value);
70  [[nodiscard]] const byte* getReadCacheLine(unsigned address) const;
71 
72  template<typename Archive>
73  void serialize(Archive& ar, unsigned version);
74 
75 //private:
76  struct AmdCmd {
77  unsigned addr;
78  byte value;
79 
80  template<typename Archive>
81  void serialize(Archive& ar, unsigned version);
82  };
83 
84  enum State { ST_IDLE, ST_IDENT };
85 
86 private:
87  void init(const std::string& name, const DeviceConfig& config, Load load, const Rom* rom);
88  struct GetSectorInfoResult { unsigned sector, sectorSize, offset; };
89  [[nodiscard]] GetSectorInfoResult getSectorInfo(unsigned address) const;
90 
91  void setState(State newState);
92  [[nodiscard]] bool checkCommandReset();
93  [[nodiscard]] bool checkCommandEraseSector();
94  [[nodiscard]] bool checkCommandEraseChip();
95  [[nodiscard]] bool checkCommandProgramHelper(unsigned numBytes, const byte* cmdSeq, size_t cmdLen);
96  [[nodiscard]] bool checkCommandProgram();
97  [[nodiscard]] bool checkCommandDoubleByteProgram();
98  [[nodiscard]] bool checkCommandQuadrupleByteProgram();
99  [[nodiscard]] bool checkCommandManufacturer();
100  [[nodiscard]] bool partialMatch(size_t len, const byte* dataSeq) const;
101 
102  [[nodiscard]] bool isSectorWritable(unsigned sector) const;
103 
104 private:
105  MSXMotherBoard& motherBoard;
106  std::unique_ptr<SRAM> ram;
107  MemBuffer<int> writeAddress;
108  MemBuffer<const byte*> readAddress;
109  const span<const SectorInfo> sectorInfo;
110  const unsigned size;
111  const word ID;
112  const Addressing addressing;
113 
114  static constexpr unsigned MAX_CMD_SIZE = 8;
115  AmdCmd cmd[MAX_CMD_SIZE];
116  unsigned cmdIdx;
117  State state = ST_IDLE;
118  bool vppWpPinLow = false; // true = protection on
119 };
121 
122 } // namespace openmsx
123 
124 #endif
unsigned getSize() const
Definition: AmdFlash.hh:66
void write(unsigned address, byte value)
Definition: AmdFlash.cc:268
void serialize(Archive &ar, unsigned version)
Definition: AmdFlash.cc:407
byte peek(unsigned address) const
Definition: AmdFlash.cc:215
void setVppWpPinLow(bool value)
Setting the Vpp/WP# pin LOW enables a certain kind of write protection of some sectors.
Definition: AmdFlash.hh:64
byte read(unsigned address) const
Definition: AmdFlash.cc:251
AmdFlash(const Rom &rom, span< const SectorInfo > sectorInfo, word ID, Addressing addressing, const DeviceConfig &config, Load load=Load::NORMAL)
Create AmdFlash with given configuration.
Definition: AmdFlash.cc:25
const byte * getReadCacheLine(unsigned address) const
Definition: AmdFlash.cc:257
Definition: span.hh:126
XMLElement load(const string &filename, string_view systemID)
Definition: XMLLoader.cc:34
This file implemented 3 utility functions:
Definition: Autofire.cc:5
uint16_t word
16 bit unsigned integer
Definition: openmsx.hh:29
SERIALIZE_CLASS_VERSION(CassettePlayer, 2)
SRAM
Definition: SRAM.cc:150
void serialize(Archive &ar, unsigned version)
Definition: AmdFlash.cc:400