OXYGENHUB
◆ main openmsx / src / network / SMXWiFi.hh
3.2 KiB · C++ · last changed chore(): initial commit 1 month ago source blame history raw
1
#ifndef SMXWIFI_HH
2
#define SMXWIFI_HH
3
4
#include "BooleanSetting.hh"
5
#include "EmulatedEsp32.hh"
6
#include "MSXCPU.hh"
7
#include "MSXDevice.hh"
8
#include "RS232Connector.hh"
9
#include "Rom.hh"
10
11
#include "circular_buffer.hh"
12
13
#include <cstdint>
14
#include <memory>
15
#include <mutex>
16
#include <span>
17
18
namespace openmsx {
19
20
class SMXWiFi final : public MSXDevice, public RS232Connector
21
{
22
public:
23
	explicit SMXWiFi(DeviceConfig& config);
24
	~SMXWiFi() override;
25
26
	// MSXDevice
27
	void powerDown(EmuTime time) override;
28
	void reset(EmuTime time) override;
29
	[[nodiscard]] uint8_t readIO(uint16_t port, EmuTime time) override;
30
	[[nodiscard]] uint8_t peekIO(uint16_t port, EmuTime time) const override;
31
	void writeIO(uint16_t port, uint8_t value, EmuTime time) override;
32
	[[nodiscard]] uint8_t readMem(uint16_t address, EmuTime time) override;
33
	[[nodiscard]] uint8_t peekMem(uint16_t address, EmuTime time) const override;
34
	[[nodiscard]] const uint8_t* getReadCacheLine(uint16_t start) const override;
35
36
	// Connector
37
	void plug(Pluggable& device, EmuTime time) override;
38
	void unplug(EmuTime time) override;
39
40
	// RS232Connector
41
	void setDataBits(DataBits bits) override;
42
	void setStopBits(StopBits bits) override;
43
	void setParityBit(bool enable, Parity parity) override;
44
	void recvByte(uint8_t value, EmuTime time) override;
45
	[[nodiscard]] bool ready() override;
46
	[[nodiscard]] bool acceptsData() override;
47
	[[nodiscard]] bool directByteDelivery() const override;
48
49
	template<typename Archive>
50
	void serialize(Archive& ar, unsigned version);
51
52
private:
53
	[[nodiscard]] uint8_t readFIFO(EmuTime time);
54
	[[nodiscard]] uint8_t peekFIFO() const;
55
	[[nodiscard]] uint8_t readStatus();
56
	[[nodiscard]] uint8_t peekStatus() const;
57
	[[nodiscard]] uint8_t peekStatusLocked() const;
58
	void writeCommand(uint8_t value, EmuTime time);
59
	void resetFIFO();
60
	void pushToFifo(std::span<const uint8_t> data);
61
62
	// Endpoint routing: the MSX UART talks either to the real ESP32 on
63
	// the serial port (an RS232Raw whose port is actually open) or to the
64
	// emulated ESP32.
65
	[[nodiscard]] bool isRealEndpoint() const;
66
	void updateEndpoint(EmuTime time);
67
	// Pushes the current UART configuration to the plugged device (the
68
	// format is fixed 8N1; only the baud rate is configurable).
69
	void applyParamsToReal(EmuTime time) const;
70
71
	cb_queue<uint8_t> fifo;
72
	mutable std::mutex fifoMutex;
73
74
	// Emulated ESP32, used while no real ESP32 is reachable. Created in
75
	// the constructor body, after all members are constructed, so its
76
	// emulation thread can always use the FIFO through the sink callback.
77
	std::unique_ptr<EmulatedEsp32> emulated;
78
79
	// ROM at 0x4000-0x7FFF (ESPUNAPI_IO.rom)
80
	Rom rom;
81
82
	// Cached CPU reference for emulated-time waits (VDPIODelay pattern)
83
	MSXCPU& cpu;
84
85
	// Quick receive: assert WAIT when the UART FIFO is empty (status
86
	// bit 3 reports it, so the driver can fall back to polled reads).
87
	BooleanSetting quickRcvSetting;
88
89
	// Small scalars, grouped at the end (members are ordered
90
	// largest-alignment-first to avoid padding).
91
	unsigned uartSpeed = 0; // UART_SPEED_859372
92
	bool underrun = false;
93
	// Whether the real serial endpoint is currently active (tracked to
94
	// detect endpoint switches in updateEndpoint()).
95
	bool realEndpointActive = false;
96
};
97
98
} // namespace openmsx
99
100
#endif