openMSX
NowindHost.hh
Go to the documentation of this file.
1 #ifndef NOWINDHOST_HH
2 #define NOWINDHOST_HH
3 
4 #include "DiskImageUtils.hh"
5 #include "circular_buffer.hh"
6 #include "openmsx.hh"
7 #include <fstream>
8 #include <memory>
9 #include <optional>
10 #include <string>
11 #include <vector>
12 
13 namespace openmsx {
14 
15 class SectorAccessibleDisk;
16 class DiskContainer;
17 
19 {
20 public:
21  using Drives = std::vector<std::unique_ptr<DiskContainer>>;
22 
23  explicit NowindHost(const Drives& drives);
25 
26  // public for usb-host implementation
27  [[nodiscard]] bool isDataAvailable() const;
28 
29  // read one byte of response-data from the host (msx <- pc)
30  byte read();
31 
32  // like read(), but without side effects (doesn't consume the data)
33  [[nodiscard]] byte peek() const;
34 
35  // Write one byte of command-data to the host (msx -> pc)
36  // Time parameter is in milliseconds. Emulators can pass emulation
37  // time, usbhost can pass real time.
38  void write(byte data, unsigned time);
39 
40  void setAllowOtherDiskroms(bool allow) { allowOtherDiskroms = allow; }
41  [[nodiscard]] bool getAllowOtherDiskroms() const { return allowOtherDiskroms; }
42 
43  void setEnablePhantomDrives(bool enable) { enablePhantomDrives = enable; }
44  [[nodiscard]] bool getEnablePhantomDrives() const { return enablePhantomDrives; }
45 
46  template<typename Archive>
47  void serialize(Archive& ar, unsigned version);
48 
49  // public for serialization
50  enum State {
51  STATE_SYNC1, // waiting for AF
52  STATE_SYNC2, // waiting for 05
53  STATE_COMMAND, // waiting for command (9 bytes)
54  STATE_DISKREAD, // waiting for AF07
55  STATE_DISKWRITE, // waiting for AA<data>AA
56  STATE_DEVOPEN, // waiting for filename (11 bytes)
57  STATE_IMAGE, // waiting for filename
58  STATE_MESSAGE, // waiting for null-terminated message
59  };
60 
61 private:
62  void msxReset();
63  [[nodiscard]] SectorAccessibleDisk* getDisk() const;
64  void executeCommand();
65 
66  void send(byte value);
67  void send16(word value);
68  void sendHeader();
69  void purge();
70 
71  void DRIVES();
72  void DSKCHG();
73  void CHOICE();
74  void INIENV();
75  void setDateMSX();
76 
77  [[nodiscard]] unsigned getSectorAmount() const;
78  [[nodiscard]] unsigned getStartSector() const;
79  [[nodiscard]] unsigned getStartAddress() const;
80  [[nodiscard]] unsigned getCurrentAddress() const;
81 
82  void diskReadInit(SectorAccessibleDisk& disk);
83  void doDiskRead1();
84  void doDiskRead2();
85  void transferSectors(unsigned transferAddress, unsigned amount);
86  void transferSectorsBackwards(unsigned transferAddress, unsigned amount);
87 
88  void diskWriteInit(SectorAccessibleDisk& disk);
89  void doDiskWrite1();
90  void doDiskWrite2();
91 
92  [[nodiscard]] unsigned getFCB() const;
93  [[nodiscard]] std::string extractName(int begin, int end) const;
94  unsigned readHelper1(unsigned dev, char* buffer);
95  void readHelper2(unsigned len, const char* buffer);
96  [[nodiscard]] int getDeviceNum() const;
97  int getFreeDeviceNum();
98  void deviceOpen();
99  void deviceClose();
100  void deviceWrite();
101  void deviceRead();
102 
103  void callImage(const std::string& filename);
104 
105 private:
106  static constexpr unsigned MAX_DEVICES = 16;
107 
108  const Drives& drives;
109 
110  cb_queue<byte> hostToMsxFifo;
111 
112  struct {
113  std::optional<std::fstream> fs; // not in use when fs == nullopt
114  unsigned fcb;
115  } devices[MAX_DEVICES];
116 
117  // state-machine
118  std::vector<SectorBuffer> buffer;// work buffer for diskread/write
119  unsigned lastTime; // last time a byte was received from MSX
120  State state;
121  unsigned recvCount; // how many bytes recv in this state
122  unsigned transferred; // progress within diskread/write
123  unsigned retryCount; // only used for diskread
124  unsigned transferSize; // size of current chunk
125  byte cmdData[9]; // reg_[cbedlhfa] + cmd
126  byte extraData[240 + 2]; // extra data for diskread/write
127 
128  byte romdisk; // index of romdisk (255 = no romdisk)
129  bool allowOtherDiskroms;
130  bool enablePhantomDrives;
131 };
132 
133 } // namespace openmsx
134 
135 #endif // NOWINDHOST_HH
byte peek() const
Definition: NowindHost.cc:52
NowindHost(const Drives &drives)
Definition: NowindHost.cc:40
void serialize(Archive &ar, unsigned version)
Definition: NowindHost.cc:784
void setEnablePhantomDrives(bool enable)
Definition: NowindHost.hh:43
void write(byte data, unsigned time)
Definition: NowindHost.cc:70
std::vector< std::unique_ptr< DiskContainer > > Drives
Definition: NowindHost.hh:21
bool getAllowOtherDiskroms() const
Definition: NowindHost.hh:41
void setAllowOtherDiskroms(bool allow)
Definition: NowindHost.hh:40
bool isDataAvailable() const
Definition: NowindHost.cc:63
bool getEnablePhantomDrives() const
Definition: NowindHost.hh:44
std::optional< std::fstream > fs
Definition: NowindHost.hh:113
This file implemented 3 utility functions:
Definition: Autofire.cc:9
constexpr const char *const filename
uint16_t word
16 bit unsigned integer
Definition: openmsx.hh:29
constexpr auto begin(const zstring_view &x)
Definition: zstring_view.hh:83
constexpr auto end(const zstring_view &x)
Definition: zstring_view.hh:84