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