OXYGENHUB
◆ main openmsx / src / memory / EEPROM_93C46.hh
1.9 KiB · C++ · last changed chore(): initial commit 1 month ago source blame history raw
e8555c89 Andy McCall 1
#ifndef EEPROM_93C46_HH
2
#define EEPROM_93C46_HH
3
4
#include "EmuTime.hh"
5
#include "SRAM.hh"
6
7
#include <climits>
8
#include <cstdint>
9
#include <string>
10
11
namespace openmsx {
12
13
class DeviceConfig;
14
class XMLElement;
15
16
class EEPROM_93C46
17
{
18
public:
19
	static constexpr uint8_t ADDRESS_BITS = 7;
20
	static constexpr uint32_t NUM_ADDRESSES = 1 << ADDRESS_BITS;
21
	static constexpr uint32_t ADDRESS_MASK = NUM_ADDRESSES - 1;
22
	static constexpr uint8_t DATA_BITS = 8; // only 8-bit mode implemented
23
24
public:
25
	explicit EEPROM_93C46(const XMLElement& xml); // unittest
26
	EEPROM_93C46(const std::string& name, const DeviceConfig& config);
27
28
	void reset();
29
30
	[[nodiscard]] bool read_DO(EmuTime time) const;
31
	void write_CS (bool value, EmuTime time);
32
	void write_CLK(bool value, EmuTime time);
33
	void write_DI (bool value, EmuTime time);
34
35
	template<typename Archive>
36
	void serialize(Archive& ar, unsigned version);
37
38
	// for unittest
39
	[[nodiscard]] const uint8_t* backdoor() const {
40
		return &sram[0];
41
	}
42
43
private:
44
	[[nodiscard]] uint8_t read(unsigned addr) const;
45
	void write(unsigned addr, uint8_t value, EmuTime time);
46
	void writeAll(uint8_t value, EmuTime time);
47
	void erase(unsigned addr, EmuTime time);
48
	void eraseAll(EmuTime time);
49
50
	[[nodiscard]] bool ready(EmuTime time) const;
51
	void clockEvent(EmuTime time);
52
	void execute_command(EmuTime time);
53
54
public: // for serialize
55
	enum class State : uint8_t {
56
		IN_RESET,
57
		WAIT_FOR_START_BIT,
58
		WAIT_FOR_COMMAND,
59
		READING_DATA,
60
		WAIT_FOR_WRITE,
61
		WAIT_FOR_WRITE_ALL,
62
	};
63
64
private:
65
	SRAM sram;
66
	EmuTime completionTime = EmuTime::zero();
67
	EmuTime csTime = EmuTime::zero();
68
	State state = State::IN_RESET;
69
	uint16_t shiftRegister = 0;
70
	uint8_t bits = 0;
71
	uint8_t address = 0;
72
	bool pinCS  = false;
73
	bool pinCLK = false;
74
	bool pinDI = false;
75
	bool writeProtected = true;
76
77
	static constexpr int SHIFT_REG_BITS = sizeof(shiftRegister) * CHAR_BIT;
78
};
79
80
} // namespace openmsx
81
82
#endif