openMSX
NowindHost.cc
Go to the documentation of this file.
1#include "NowindHost.hh"
2
3#include "DiskContainer.hh"
4#include "FileOperations.hh"
5#include "MSXException.hh"
7
8#include "enumerate.hh"
9#include "narrow.hh"
10#include "one_of.hh"
11#include "serialize.hh"
12#include "serialize_stl.hh"
13#include "unreachable.hh"
14#include "view.hh"
15#include "xrange.hh"
16
17#include <algorithm>
18#include <array>
19#include <bit>
20#include <cassert>
21#include <cctype>
22#include <cstdio>
23#include <cstdarg>
24#include <ctime>
25#include <fstream>
26#include <memory>
27
28using std::string;
29
30namespace openmsx {
31
32static constexpr unsigned SECTOR_SIZE = sizeof(SectorBuffer);
33
34static void DBERR(const char* message, ...)
35{
36 (void)message;
37#if 0
38 va_list args;
39 va_start(args, message);
40 printf("nowind: ");
41 vprintf(message, args);
42 va_end(args);
43#endif
44}
45
46
48 : drives(drives_)
49{
50}
51
52NowindHost::~NowindHost() = default;
53
54byte NowindHost::peek() const
55{
56 return isDataAvailable() ? hostToMsxFifo.front() : 0xFF;
57}
58
59// receive: msx <- pc
61{
62 return isDataAvailable() ? hostToMsxFifo.pop_front() : 0xFF;
63}
64
66{
67 return !hostToMsxFifo.empty();
68}
69
70
71// send: msx -> pc
72void NowindHost::write(byte data, unsigned time)
73{
74 unsigned duration = time - lastTime;
75 lastTime = time;
76 if (duration >= 500) {
77 // timeout (500ms), start looking for AF05
78 purge();
79 state = STATE_SYNC1;
80 }
81
82 switch (state) {
83 case STATE_SYNC1:
84 if (data == 0xAF) state = STATE_SYNC2;
85 break;
86 case STATE_SYNC2:
87 switch (data) {
88 case 0x05: state = STATE_COMMAND; recvCount = 0; break;
89 case 0xAF: state = STATE_SYNC2; break;
90 case 0xFF: state = STATE_SYNC1; msxReset(); break;
91 default: state = STATE_SYNC1; break;
92 }
93 break;
94 case STATE_COMMAND:
95 assert(recvCount < 9);
96 cmdData[recvCount] = data;
97 if (++recvCount == 9) {
98 executeCommand();
99 }
100 break;
101 case STATE_DISKREAD:
102 assert(recvCount < 2);
103 extraData[recvCount] = data;
104 if (++recvCount == 2) {
105 doDiskRead2();
106 }
107 break;
108 case STATE_DISKWRITE:
109 assert(recvCount < (transferSize + 2));
110 extraData[recvCount] = data;
111 if (++recvCount == (transferSize + 2)) {
112 doDiskWrite2();
113 }
114 break;
115 case STATE_DEVOPEN:
116 assert(recvCount < 11);
117 extraData[recvCount] = data;
118 if (++recvCount == 11) {
119 deviceOpen();
120 }
121 break;
122 case STATE_IMAGE:
123 assert(recvCount < 40);
124 extraData[recvCount] = data;
125 if (data == one_of(byte(0), byte(':')) ||
126 (++recvCount == 40)) {
127 auto* eData = std::bit_cast<char*>(extraData.data());
128 callImage(string(eData, recvCount));
129 state = STATE_SYNC1;
130 }
131 break;
132 case STATE_MESSAGE:
133 assert(recvCount < (240 - 1));
134 extraData[recvCount] = data;
135 if ((data == 0) || (++recvCount == (240 - 1))) {
136 extraData[recvCount] = 0;
137 DBERR("%s\n", std::bit_cast<char*>(extraData.data()));
138 state = STATE_SYNC1;
139 }
140 break;
141 default:
143 }
144}
145
146void NowindHost::msxReset()
147{
148 for (auto& dev : devices) {
149 dev.fs.reset();
150 }
151 DBERR("MSX reset\n");
152}
153
154SectorAccessibleDisk* NowindHost::getDisk() const
155{
156 byte num = cmdData[7]; // reg_a
157 if (num >= drives.size()) {
158 return nullptr;
159 }
160 return drives[num]->getSectorAccessibleDisk();
161}
162
163void NowindHost::executeCommand()
164{
165 assert(recvCount == 9);
166 byte cmd = cmdData[8];
167 switch (cmd) {
168 //case 0x0D: BDOS_0DH_DiskReset();
169 //case 0x0F: BDOS_0FH_OpenFile();
170 //case 0x10: BDOS_10H_CloseFile();
171 //case 0x11: BDOS_11H_FindFirst();
172 //case 0x12: BDOS_12H_FindNext();
173 //case 0x13: BDOS_13H_DeleteFile();
174 //case 0x14: BDOS_14H_ReadSeq();
175 //case 0x15: BDOS_15H_WriteSeq();
176 //case 0x16: BDOS_16H_CreateFile();
177 //case 0x17: BDOS_17H_RenameFile();
178 //case 0x21: BDOS_21H_ReadRandomFile();
179 //case 0x22: BDOS_22H_WriteRandomFile();
180 //case 0x23: BDOS_23H_GetFileSize();
181 //case 0x24: BDOS_24H_SetRandomRecordField();
182 //case 0x26: BDOS_26H_WriteRandomBlock();
183 //case 0x27: BDOS_27H_ReadRandomBlock();
184 //case 0x28: BDOS_28H_WriteRandomFileWithZeros();
185 //case 0x2A: BDOS_2AH_GetDate();
186 //case 0x2B: BDOS_2BH_SetDate();
187 //case 0x2C: BDOS_2CH_GetTime();
188 //case 0x2D: BDOS_2DH_SetTime();
189 //case 0x2E: BDOS_2EH_Verify();
190 //case 0x2F: BDOS_2FH_ReadLogicalSector();
191 //case 0x30: BDOS_30H_WriteLogicalSector();
192
193 case 0x80: { // DSKIO
194 auto* disk = getDisk();
195 if (!disk) {
196 // no such drive or no disk inserted
197 // (causes a timeout on the MSX side)
198 state = STATE_SYNC1;
199 return;
200 }
201 byte reg_f = cmdData[6];
202 if (reg_f & 1) { // carry flag
203 diskWriteInit(*disk);
204 } else {
205 diskReadInit(*disk);
206 }
207 break;
208 }
209
210 case 0x81: DSKCHG(); state = STATE_SYNC1; break;
211 //case 0x82: GETDPB();
212 //case 0x83: CHOICE();
213 //case 0x84: DSKFMT();
214 case 0x85: DRIVES(); state = STATE_SYNC1; break;
215 case 0x86: INIENV(); state = STATE_SYNC1; break;
216 case 0x87: setDateMSX(); state = STATE_SYNC1; break;
217 case 0x88: state = STATE_DEVOPEN; recvCount = 0; break;
218 case 0x89: deviceClose(); state = STATE_SYNC1; break;
219 //case 0x8A: deviceRandomIO(fcb);
220 case 0x8B: deviceWrite(); state = STATE_SYNC1; break;
221 case 0x8C: deviceRead(); state = STATE_SYNC1; break;
222 //case 0x8D: deviceEof(fcb);
223 //case 0x8E: auxIn();
224 //case 0x8F: auxOut();
225 case 0x90: state = STATE_MESSAGE; recvCount = 0; break;
226 case 0xA0: state = STATE_IMAGE; recvCount = 0; break;
227 //case 0xFF: vramDump();
228 default:
229 // Unknown USB command!
230 state = STATE_SYNC1;
231 break;
232 }
233}
234
235// send: pc -> msx
236void NowindHost::send(byte value)
237{
238 hostToMsxFifo.push_back(value);
239}
240void NowindHost::send16(word value)
241{
242 hostToMsxFifo.push_back(narrow_cast<byte>(value & 255));
243 hostToMsxFifo.push_back(narrow_cast<byte>(value >> 8));
244}
245
246void NowindHost::purge()
247{
248 hostToMsxFifo.clear();
249}
250
251void NowindHost::sendHeader()
252{
253 send(0xFF); // needed because first read might fail!
254 send(0xAF);
255 send(0x05);
256}
257
258void NowindHost::DSKCHG()
259{
260 auto* disk = getDisk();
261 if (!disk) {
262 // no such drive or no disk inserted
263 return;
264 }
265
266 sendHeader();
267 byte num = cmdData[7]; // reg_a
268 assert(num < drives.size());
269 if (drives[num]->diskChanged()) {
270 send(255); // changed
271 // read first FAT sector (contains media descriptor)
272 SectorBuffer sectorBuffer;
273 try {
274 disk->readSectors(std::span{&sectorBuffer, 1}, 1);
275 } catch (MSXException&) {
276 // TODO read error
277 sectorBuffer.raw[0] = 0;
278 }
279 send(sectorBuffer.raw[0]); // new media descriptor
280 } else {
281 send(0); // not changed
282 // TODO shouldn't we send some (dummy) byte here?
283 // nowind-disk-rom seems to read it (but doesn't use it)
284 }
285}
286
287void NowindHost::DRIVES()
288{
289 // at least one drive (MSX-DOS1 cannot handle 0 drives)
290 byte numberOfDrives = std::max<byte>(1, byte(drives.size()));
291
292 byte reg_a = cmdData[7];
293 sendHeader();
294 send(getEnablePhantomDrives() ? 0x02 : 0);
295 send(reg_a | (getAllowOtherDiskRoms() ? 0 : 0x80));
296 send(numberOfDrives);
297
298 romDisk = 255; // no rom disk
299 for (auto [i, drv] : enumerate(drives)) {
300 if (drv->isRomDisk()) {
301 romDisk = byte(i);
302 break;
303 }
304 }
305}
306
307void NowindHost::INIENV()
308{
309 sendHeader();
310 send(romDisk); // calculated in DRIVES()
311}
312
313void NowindHost::setDateMSX()
314{
315 auto td = time(nullptr);
316 auto* tm = localtime(&td);
317
318 sendHeader();
319 send(narrow_cast<uint8_t>(tm->tm_mday)); // day
320 send(narrow_cast<uint8_t>(tm->tm_mon + 1)); // month
321 send16(narrow_cast<uint16_t>(tm->tm_year + 1900)); // year
322}
323
324
325unsigned NowindHost::getSectorAmount() const
326{
327 byte reg_b = cmdData[1];
328 return reg_b;
329}
330unsigned NowindHost::getStartSector() const
331{
332 byte reg_c = cmdData[0];
333 byte reg_e = cmdData[2];
334 byte reg_d = cmdData[3];
335 unsigned startSector = reg_e + (reg_d * 256);
336 if (reg_c < 0x80) {
337 // FAT16 read/write sector
338 startSector += reg_c << 16;
339 }
340 return startSector;
341}
342unsigned NowindHost::getStartAddress() const
343{
344 byte reg_l = cmdData[4];
345 byte reg_h = cmdData[5];
346 return reg_h * 256 + reg_l;
347}
348unsigned NowindHost::getCurrentAddress() const
349{
350 unsigned startAddress = getStartAddress();
351 return startAddress + transferred;
352}
353
354
355void NowindHost::diskReadInit(SectorAccessibleDisk& disk)
356{
357 unsigned sectorAmount = getSectorAmount();
358 buffer.resize(sectorAmount);
359 unsigned startSector = getStartSector();
360 try {
361 disk.readSectors(std::span{buffer.data(), sectorAmount}, startSector);
362 } catch (MSXException&) {
363 // read error
364 state = STATE_SYNC1;
365 return;
366 }
367
368 transferred = 0;
369 retryCount = 0;
370 doDiskRead1();
371}
372
373void NowindHost::doDiskRead1()
374{
375 unsigned bytesLeft = unsigned(buffer.size() * SECTOR_SIZE) - transferred;
376 if (bytesLeft == 0) {
377 sendHeader();
378 send(0x01); // end of receive-loop
379 send(0x00); // no more data
380 state = STATE_SYNC1;
381 return;
382 }
383
384 constexpr unsigned NUMBER_OF_BLOCKS = 32; // 32 * 64 bytes = 2048 bytes
385 transferSize = std::min(bytesLeft, NUMBER_OF_BLOCKS * 64); // hardcoded in firmware
386
387 unsigned address = getCurrentAddress();
388 if (address >= 0x8000) {
389 if (transferSize & 0x003F) {
390 transferSectors(address, transferSize);
391 } else {
392 transferSectorsBackwards(address, transferSize);
393 }
394 } else {
395 // transfer below 0x8000
396 // TODO shouldn't we also test for (transferSize & 0x3F)?
397 unsigned endAddress = address + transferSize;
398 if (endAddress <= 0x8000) {
399 transferSectorsBackwards(address, transferSize);
400 } else {
401 transferSize = 0x8000 - address;
402 transferSectors(address, transferSize);
403 }
404 }
405
406 // wait for 2 bytes
407 state = STATE_DISKREAD;
408 recvCount = 0;
409}
410
411void NowindHost::doDiskRead2()
412{
413 // disk rom sends back the last two bytes read
414 assert(recvCount == 2);
415 byte tail1 = extraData[0];
416 byte tail2 = extraData[1];
417 if ((tail1 == 0xAF) && (tail2 == 0x07)) {
418 transferred += transferSize;
419 retryCount = 0;
420
421 unsigned address = getCurrentAddress();
422 size_t bytesLeft = (buffer.size() * SECTOR_SIZE) - transferred;
423 if ((address == 0x8000) && (bytesLeft > 0)) {
424 sendHeader();
425 send(0x01); // end of receive-loop
426 send(0xff); // more data for page 2/3
427 }
428
429 // continue the rest of the disk read
430 doDiskRead1();
431 } else {
432 purge();
433 if (++retryCount == 10) {
434 // do nothing, timeout on MSX
435 // too many retries, aborting readDisk()
436 state = STATE_SYNC1;
437 return;
438 }
439
440 // try again, wait for two bytes
441 state = STATE_DISKREAD;
442 recvCount = 0;
443 }
444}
445
446// sends "02" + "transfer_addr" + "amount" + "data" + "0F 07"
447void NowindHost::transferSectors(unsigned transferAddress, unsigned amount)
448{
449 sendHeader();
450 send(0x00); // don't exit command, (more) data is coming
451 send16(narrow_cast<uint16_t>(transferAddress));
452 send16(narrow_cast<uint16_t>(amount));
453
454 std::span fullBuf{buffer[0].raw.data(), buffer.size() * SECTOR_SIZE};
455 auto buf = fullBuf.subspan(transferred, amount);
456 for (auto b : buf) {
457 send(b);
458 }
459 send(0xAF);
460 send(0x07); // used for validation
461}
462
463// sends "02" + "transfer_addr" + "amount" + "data" + "0F 07"
464void NowindHost::transferSectorsBackwards(unsigned transferAddress, unsigned amount)
465{
466 sendHeader();
467 send(0x02); // don't exit command, (more) data is coming
468 send16(narrow_cast<uint16_t>(transferAddress + amount));
469 send(narrow_cast<uint8_t>(amount / 64));
470
471 std::span fullBuf{buffer[0].raw.data(), buffer.size() * SECTOR_SIZE};
472 auto buf = fullBuf.subspan(transferred, amount);
473 for (auto b : view::reverse(buf)) {
474 send(b);
475 }
476 send(0xAF);
477 send(0x07); // used for validation
478}
479
480
481void NowindHost::diskWriteInit(SectorAccessibleDisk& disk)
482{
483 if (disk.isWriteProtected()) {
484 sendHeader();
485 send(1);
486 send(0); // WRITE PROTECTED
487 state = STATE_SYNC1;
488 return;
489 }
490
491 unsigned sectorAmount = std::min(128u, getSectorAmount());
492 buffer.resize(sectorAmount);
493 transferred = 0;
494 doDiskWrite1();
495}
496
497void NowindHost::doDiskWrite1()
498{
499 unsigned bytesLeft = unsigned(buffer.size() * SECTOR_SIZE) - transferred;
500 if (bytesLeft == 0) {
501 // All data transferred!
502 auto sectorAmount = unsigned(buffer.size());
503 unsigned startSector = getStartSector();
504 if (auto* disk = getDisk()) {
505 try {
506 disk->writeSectors(std::span{buffer.data(), sectorAmount}, startSector);
507 } catch (MSXException&) {
508 // TODO write error
509 }
510 }
511 sendHeader();
512 send(255);
513 state = STATE_SYNC1;
514 return;
515 }
516
517 constexpr unsigned BLOCKSIZE = 240;
518 transferSize = std::min(bytesLeft, BLOCKSIZE);
519
520 unsigned address = getCurrentAddress();
521 unsigned endAddress = address + transferSize;
522 if ((address ^ endAddress) & 0x8000) {
523 // would cross page 1-2 boundary -> limit to page 1
524 transferSize = 0x8000 - address;
525 }
526
527 sendHeader();
528 send(0); // data ahead!
529 send16(narrow_cast<uint16_t>(address));
530 send16(narrow_cast<uint16_t>(transferSize));
531 send(0xaa);
532
533 // wait for data
534 state = STATE_DISKWRITE;
535 recvCount = 0;
536}
537
538void NowindHost::doDiskWrite2()
539{
540 assert(recvCount == (transferSize + 2));
541 //auto* buf = &buffer[0].raw[transferred];
542 std::span fullBuf{buffer[0].raw.data(), buffer.size() * SECTOR_SIZE};
543 auto dst = fullBuf.subspan(transferred, transferSize);
544 auto src = subspan(extraData, 1, transferSize);
545 ranges::copy(src, dst);
546
547 byte seq1 = extraData[0];
548 byte seq2 = extraData[transferSize + 1];
549 if ((seq1 == 0xaa) && (seq2 == 0xaa)) {
550 // good block received
551 transferred += transferSize;
552
553 unsigned address = getCurrentAddress();
554 size_t bytesLeft = (buffer.size() * SECTOR_SIZE) - transferred;
555 if ((address == 0x8000) && (bytesLeft > 0)) {
556 sendHeader();
557 send(254); // more data for page 2/3
558 }
559 } else {
560 // ERROR!!!
561 // This situation is still not handled correctly!
562 purge();
563 }
564
565 // continue the rest of the disk write
566 doDiskWrite1();
567}
568
569
570word NowindHost::getFCB() const
571{
572 // note: same code as getStartAddress(), merge???
573 byte reg_l = cmdData[4];
574 byte reg_h = cmdData[5];
575 return word(reg_h * 256 + reg_l);
576}
577
578string NowindHost::extractName(int begin, int end) const
579{
580 string result;
581 for (auto i : xrange(begin, end)) {
582 auto c = narrow_cast<char>(extraData[i]);
583 if (c == ' ') break;
584 result += char(toupper(c));
585 }
586 return result;
587}
588
589int NowindHost::getDeviceNum() const
590{
591 auto fcb = getFCB();
592 for (auto [i, dev] : enumerate(devices)) {
593 if (dev.fs && dev.fcb == fcb) {
594 return int(i);
595 }
596 }
597 return -1;
598}
599
600int NowindHost::getFreeDeviceNum()
601{
602 if (int dev = getDeviceNum(); dev != -1) {
603 // There already was a device open with this fcb address,
604 // reuse that device.
605 return dev;
606 }
607 // Search for free device.
608 for (auto [i, dev] : enumerate(devices)) {
609 if (!dev.fs) return int(i);
610 }
611 // All devices are in use. This can't happen when the MSX software
612 // functions correctly. We'll simply reuse the first device. It would
613 // be nicer if we reuse the oldest device, but that's harder to
614 // implement, and actually it doesn't really matter.
615 return 0;
616}
617
618void NowindHost::deviceOpen()
619{
620 state = STATE_SYNC1;
621
622 assert(recvCount == 11);
623 string filename = extractName(0, 8);
624 string ext = extractName(8, 11);
625 if (!ext.empty()) {
626 strAppend(filename, '.', ext);
627 }
628
629 auto fcb = getFCB();
630 unsigned dev = getFreeDeviceNum();
631 devices[dev].fs.emplace(); // takes care of deleting old fs
632 devices[dev].fcb = fcb;
633
634 sendHeader();
635 byte errorCode = 0;
636 byte openMode = cmdData[2]; // reg_e
637 switch (openMode) {
638 case 1: // read-only mode
639 devices[dev].fs->open(filename.c_str(), std::ios::in | std::ios::binary);
640 errorCode = 53; // file not found
641 break;
642 case 2: // create new file, write-only
643 devices[dev].fs->open(filename.c_str(), std::ios::out | std::ios::binary);
644 errorCode = 56; // bad file name
645 break;
646 case 8: // append to existing file, write-only
647 devices[dev].fs->open(filename.c_str(), std::ios::out | std::ios::binary | std::ios::app);
648 errorCode = 53; // file not found
649 break;
650 case 4:
651 send(58); // sequential I/O only
652 return;
653 default:
654 send(0xFF); // TODO figure out a good error number
655 return;
656 }
657 assert(errorCode != 0);
658 if (devices[dev].fs->fail()) {
659 devices[dev].fs.reset();
660 send(errorCode);
661 return;
662 }
663
664 unsigned readLen = 0;
665 bool eof = false;
666 std::array<char, 256> buf;
667 if (openMode == 1) {
668 // read-only mode, already buffer first 256 bytes
669 readLen = readHelper1(dev, buf);
670 assert(readLen <= 256);
671 eof = readLen < 256;
672 }
673
674 send(0x00); // no error
675 send16(fcb);
676 send16(narrow_cast<uint16_t>(9 + readLen + (eof ? 1 : 0))); // number of bytes to transfer
677
678 send(openMode);
679 send(0);
680 send(0);
681 send(0);
682 send(cmdData[3]); // reg_d
683 send(0);
684 send(0);
685 send(0);
686 send(0);
687
688 if (openMode == 1) {
689 readHelper2(subspan(buf, 0, readLen));
690 }
691}
692
693void NowindHost::deviceClose()
694{
695 int dev = getDeviceNum();
696 if (dev == -1) return;
697 devices[dev].fs.reset();
698}
699
700void NowindHost::deviceWrite()
701{
702 int dev = getDeviceNum();
703 if (dev == -1) return;
704 auto data = narrow_cast<char>(cmdData[0]); // reg_c
705 devices[dev].fs->write(&data, 1);
706}
707
708void NowindHost::deviceRead()
709{
710 int dev = getDeviceNum();
711 if (dev == -1) return;
712
713 std::array<char, 256> buf;
714 unsigned readLen = readHelper1(dev, buf);
715 bool eof = readLen < 256;
716 send(0xAF);
717 send(0x05);
718 send(0x00); // dummy
719 send16(narrow_cast<uint16_t>(getFCB() + 9));
720 send16(narrow_cast<uint16_t>(readLen + (eof ? 1 : 0)));
721 readHelper2(subspan(buf, 0, readLen));
722}
723
724unsigned NowindHost::readHelper1(unsigned dev, std::span<char, 256> buf)
725{
726 assert(dev < MAX_DEVICES);
727 unsigned len = 0;
728 for (; len < 256; ++len) {
729 devices[dev].fs->read(&buf[len], 1);
730 if (devices[dev].fs->eof()) break;
731 }
732 return len;
733}
734
735void NowindHost::readHelper2(std::span<const char> buf)
736{
737 for (auto c : buf) {
738 send(c);
739 }
740 if (buf.size() < 256) {
741 send(0x1A); // end-of-file
742 }
743}
744
745
746// strips a string from outer double-quotes and anything outside them
747// ie: 'pre("foo")bar' will result in 'foo'
748static constexpr std::string_view stripQuotes(std::string_view str)
749{
750 auto first = str.find_first_of('\"');
751 if (first == string::npos) {
752 // There are no quotes, return the whole string.
753 return str;
754 }
755 auto last = str.find_last_of ('\"');
756 if (first == last) {
757 // Error, there's only a single double-quote char.
758 return {};
759 }
760 // Return the part between the quotes.
761 return str.substr(first + 1, last - first - 1);
762}
763
764void NowindHost::callImage(const string& filename)
765{
766 byte num = cmdData[7]; // reg_a
767 if (num >= drives.size()) {
768 // invalid drive number
769 return;
770 }
771 if (drives[num]->insertDisk(FileOperations::expandTilde(string(stripQuotes(filename))))) {
772 // TODO error handling
773 }
774}
775
776
777static constexpr std::initializer_list<enum_string<NowindHost::State>> stateInfo = {
778 { "SYNC1", NowindHost::STATE_SYNC1 },
779 { "SYNC2", NowindHost::STATE_SYNC2 },
780 { "COMMAND", NowindHost::STATE_COMMAND },
781 { "DISKREAD", NowindHost::STATE_DISKREAD },
782 { "DISKWRITE", NowindHost::STATE_DISKWRITE },
783 { "DEVOPEN", NowindHost::STATE_DEVOPEN },
784 { "IMAGE", NowindHost::STATE_IMAGE },
785};
787
788template<typename Archive>
789void NowindHost::serialize(Archive& ar, unsigned /*version*/)
790{
791 // drives is serialized elsewhere
792
793 ar.serialize("hostToMsxFifo", hostToMsxFifo,
794 "lastTime", lastTime,
795 "state", state,
796 "recvCount", recvCount,
797 "cmdData", cmdData,
798 "extraData", extraData);
799
800 // for backwards compatibility, serialize buffer as a vector<byte>
801 size_t bufSize = buffer.size() * sizeof(SectorBuffer);
802 std::span<uint8_t> buf{buffer.data()->raw.data(), bufSize};
803 auto tmp = to_vector(buf);
804 ar.serialize("buffer", tmp);
805 ranges::copy(tmp, buf);
806
807 ar.serialize("transfered", transferred, // for bw compat, keep typo in serialize name
808 "retryCount", retryCount,
809 "transferSize", transferSize,
810 "romdisk", romDisk,
811 "allowOtherDiskroms", allowOtherDiskRoms,
812 "enablePhantomDrives", enablePhantomDrives);
813
814 // Note: We don't serialize 'devices'. So after a loadstate it will be
815 // as-if the devices are closed again. The reason for not serializing
816 // this is that it's very hard to serialize a fstream (and we anyway
817 // can't restore the state of the host filesystem).
818}
820
821} // namespace openmsx
bool empty() const
void push_back(U &&u)
const T & front() const
byte peek() const
Definition NowindHost.cc:54
NowindHost(const Drives &drives)
Definition NowindHost.cc:47
void serialize(Archive &ar, unsigned version)
void write(byte data, unsigned time)
Definition NowindHost.cc:72
std::vector< std::unique_ptr< DiskContainer > > Drives
Definition NowindHost.hh:23
bool isDataAvailable() const
Definition NowindHost.cc:65
bool getAllowOtherDiskRoms() const
Definition NowindHost.hh:43
bool getEnablePhantomDrives() const
Definition NowindHost.hh:46
constexpr auto enumerate(Iterable &&iterable)
Heavily inspired by Nathan Reed's blog post: Python-Like enumerate() In C++17 http://reedbeta....
Definition enumerate.hh:28
string expandTilde(string path)
Expand the '~' character to the users home directory.
This file implemented 3 utility functions:
Definition Autofire.cc:11
uint8_t byte
8 bit unsigned integer
Definition openmsx.hh:26
uint16_t word
16 bit unsigned integer
Definition openmsx.hh:29
auto copy(InputRange &&range, OutputIter out)
Definition ranges.hh:250
Definition view.hh:15
constexpr auto reverse(Range &&range)
Definition view.hh:514
constexpr auto subspan(Range &&range, size_t offset, size_t count=std::dynamic_extent)
Definition ranges.hh:471
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
#define SERIALIZE_ENUM(TYPE, INFO)
auto to_vector(Range &&range) -> std::vector< detail::ToVectorType< T, decltype(std::begin(range))> >
Definition stl.hh:275
void strAppend(std::string &result, Ts &&...ts)
Definition strCat.hh:752
#define UNREACHABLE
constexpr auto xrange(T e)
Definition xrange.hh:132
constexpr auto begin(const zstring_view &x)
constexpr auto end(const zstring_view &x)