-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathWvd.cpp
635 lines (520 loc) · 15.4 KB
/
Wvd.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
// ------------------------------------------------------------------------
// Wvd class implementation
// ------------------------------------------------------------------------
#include "IoCardDisk.h"
#include "Ui.h"
#include "Wvd.h"
#include "host.h" // for dbglog()
#include <fstream>
#ifdef _DEBUG
#define DBG (0) // turn on some debug logging
#else
#define DBG (0)
#endif
#ifdef _MSC_VER
#pragma warning( disable: 4127 ) // conditional expression is constant
#endif
// address DCT/D1x : x=0 means fixed drive, x>0 means platter x-1 of removable
// therefore the most platters a drive could ever have is 15 platters
#define WVD_MAX_PLATTERS 15
// =====================================================
// public interface
// =====================================================
Wvd::~Wvd()
{
close();
}
// create a new disk image -- a name will be associated with it and the
// file actually created upon the call to save().
void
Wvd::create(int disk_type, int platters, int sectors_per_platter)
{
assert(m_file == nullptr);
setDiskType(disk_type);
setNumPlatters(platters);
setNumSectors(sectors_per_platter);
setLabel("Your comment here");
m_metadata_stale = false;
}
// open up an existing virtual disk file
bool
Wvd::open(const std::string &filename)
{
assert(m_file == nullptr);
assert(!m_has_path);
assert(!filename.empty());
// set up a file handle
m_file = std::make_unique<std::fstream>(
filename.c_str(),
std::fstream::in | std::fstream::out | std::fstream::binary);
if (!m_file) {
return false;
}
if (!m_file->good()) {
UI_error("Couldn't open file '%s'", filename.c_str());
m_file = nullptr;
return false;
}
m_has_path = true;
m_path = filename;
const bool ok = readHeader();
m_metadata_stale = !ok;
return ok;
}
// forget about current state
void
Wvd::close()
{
if (m_file != nullptr) {
if (m_file->is_open()) {
m_file->close();
}
m_file = nullptr;
}
// reinitialize in case the Wvd object gets recycled
setPath("");
setLabel("");
setDiskType(0);
setNumPlatters(0);
setNumSectors(0);
setWriteProtect(false);
setModified(false);
}
// -------------------------------------------------------------------------
// metadata access
// -------------------------------------------------------------------------
bool
Wvd::isModified() const noexcept
{
return m_metadata_modified;
}
void
Wvd::setModified(bool modified) noexcept
{
m_metadata_modified = modified;
}
std::string
Wvd::getPath() const
{
return m_path;
}
void
Wvd::setPath(const std::string &filename)
{
if (filename.empty()) {
m_has_path = false;
m_path = "";
} else if (!m_has_path || m_path != filename) {
m_has_path = true;
m_path = filename;
setModified();
}
}
int
Wvd::getDiskType()
{
refreshMetadata();
return m_disk_type;
}
void
Wvd::setDiskType(int type)
{
refreshMetadata();
if (m_disk_type != type) {
m_disk_type = static_cast<disktype_t>(type);
setModified();
}
}
int
Wvd::getNumPlatters()
{
refreshMetadata();
return m_num_platters;
}
void
Wvd::setNumPlatters(int num)
{
refreshMetadata();
if (m_num_platters != num) {
m_num_platters = num;
setModified();
}
}
int
Wvd::getNumSectors()
{
refreshMetadata();
return m_num_platter_sectors;
}
void
Wvd::setNumSectors(int num)
{
refreshMetadata();
if (m_num_platter_sectors != num) {
m_num_platter_sectors = num;
setModified();
}
}
bool
Wvd::getWriteProtect()
{
refreshMetadata();
return m_write_protect;
}
void
Wvd::setWriteProtect(bool wp)
{
refreshMetadata();
if (m_write_protect != wp) {
m_write_protect = wp;
setModified();
}
}
std::string
Wvd::getLabel()
{
refreshMetadata();
return m_label;
}
void
Wvd::setLabel(const std::string &newlabel)
{
refreshMetadata();
if (m_label != newlabel) {
m_label = newlabel;
setModified();
}
}
// -------------------------------------------------------------------------
// logical sector access
// -------------------------------------------------------------------------
// logical sector read.
// returns true on success, false on failure.
bool
Wvd::readSector(int platter, int sector, const uint8 *buffer)
{
assert(buffer != nullptr);
refreshMetadata();
assert(platter >= 0 && platter < m_num_platters);
assert(sector >= 0 && sector < m_num_platter_sectors);
assert(m_file != nullptr);
const int abs_sector = m_num_platter_sectors*platter + sector + 1;
return rawReadSector(abs_sector, buffer);
}
// logical sector write.
// returns true on success, false on failure.
bool
Wvd::writeSector(int platter, int sector, const uint8 *buffer)
{
assert(buffer != nullptr);
refreshMetadata();
assert(platter >= 0 && platter < m_num_platters);
assert(sector >= 0 && sector < m_num_platter_sectors);
assert(m_file != nullptr);
const int abs_sector = m_num_platter_sectors*platter + sector + 1;
return rawWriteSector(abs_sector, buffer);
}
// flush any pending write and close the filehandle,
// but keep the association (unlike close())
// this function is called when another function wants to touch
// a file which we have opened. this closes the file, and later
// when the Wvd code is called again, reopen() is called to
// refresh the metadata that we cache.
void
Wvd::flush()
{
if (m_file != nullptr) {
if (m_file->is_open()) {
m_file->flush();
}
m_file->close();
m_metadata_stale = true;
}
}
// if the state was initialized by a call to open(), we already have the
// filename we need to save the modified state back to that same file.
void
Wvd::save()
{
assert(m_has_path);
assert(!m_path.empty());
if (isModified()) {
if (writeHeader()) {
setModified(false);
} else {
UI_error("Error: operation failed");
}
}
}
// if the state was constructed following a call to create(), we need
// to create the entire disk file from scratch.
void
Wvd::save(const std::string &filename)
{
assert(!filename.empty());
assert(!m_has_path);
if (createFile(filename)) {
setModified(false);
} else {
UI_error("Error: operation failed");
}
}
// -------------------------------------------------------------------------
// private functions: absolute sector access
// -------------------------------------------------------------------------
// this writes an absolute sector to the virtual disk image.
// sector 0 contains the disk metadata, while logical sector 0
// starts at absolute sector 1.
// return true on success
bool
Wvd::rawWriteSector(const int sector, const uint8 *data)
{
assert(m_has_path);
assert(sector >= 0 && sector < m_num_platters*m_num_platter_sectors+1);
assert(data != nullptr);
assert(m_file->is_open());
if (DBG > 0) {
dbglog("========== writing absolute sector %d ==========\n", sector);
for (int i=0; i < 256; i+=16) {
char str[200];
sprintf(&str[0], "%02X:", i);
for (int ii=0; ii < 16; ii++) {
sprintf(&str[3+3*ii], " %02X", data[i+ii]);
}
dbglog("%s\n", &str[0]);
}
}
// go to the start of the Nth sector
m_file->seekp(256LL*sector);
if (!m_file->good()) {
UI_error("Error seeking to write sector %d of '%s'",
sector, m_path.c_str());
m_file->close();
return false;
}
// write them all
m_file->write(reinterpret_cast<const char*>(data), 256);
if (!m_file->good()) {
UI_error("Error writing to sector %d of '%s'",
sector, m_path.c_str());
m_file->close();
return false;
}
// slower, but safer in case of unexpected shutdown
m_file->flush();
return true;
}
// read from an absolute sector address on the disk.
// return true on success
bool
Wvd::rawReadSector(const int sector, const uint8 *data)
{
assert(m_has_path);
assert(sector >= 0 && sector < m_num_platters*m_num_platter_sectors+1);
assert(data != nullptr);
assert(m_file->is_open());
// go to the start of the Nth sector
m_file->seekg(256LL * sector);
if (!m_file->good()) {
UI_error("Error seeking to read sector %d of '%s'",
sector, m_path.c_str());
m_file->close();
return false;
}
m_file->read((char*)data, 256);
if (!m_file->good()) {
UI_error("Error reading from sector %d of '%s'",
sector, m_path.c_str());
m_file->close();
return false;
}
if (DBG > 0) {
dbglog("========== reading absolute sector %d ==========\n", sector);
for (int i=0; i < 256; i+=16) {
char str[200];
sprintf(&str[0], "%02X:", i);
for (int ii=0; ii < 16; ii++) {
sprintf(&str[3+3*ii], " %02X", data[i+ii]);
}
dbglog("%s\n", &str[0]);
}
}
return true;
}
// write header block for wang virtual disk
// header format
// bytes 0- 4: "WANG\0"
// bytes 5 : write format version
// bytes 6 : read format version
// bytes 7 : write protect
// bytes 8- 9: number of sectors per platter
// byte 10 : disk type
// bytes 11 : number of platters minus one
// bytes 12- 15: unused (zeros)
// bytes 16-255: disk label
// return true on success
bool
Wvd::writeHeader()
{
assert(m_num_platters > 0 && m_num_platters <= 15);
assert(m_num_platter_sectors > 0 && m_num_platter_sectors <= WVD_MAX_SECTORS);
// header block -- zap it to zeros
uint8 data[256];
memset(&data[0], static_cast<uint8>(0x00), 256);
// magic string
data[0] = 'W';
data[1] = 'A';
data[2] = 'N';
data[3] = 'G';
data[4] = 0;
// the point of having a read format and a write format is that different
// write formats indicate incompatible versions of the disk format, while
// read format indicates that the information is a superset of a previous
// version and that what is read by the old program is still usable. For
// example, say the format is rev'd to add a seek time parameter, but
// everything else is the same. An older emulator can still read and use
// the disk, so the read format is left at 0, but the write format number
// is set to 1 so a new emulator knows if the seek time parameter is
// usable.
data[5] = static_cast<uint8>(0x00); // write format version
data[6] = static_cast<uint8>(0x00); // read format version
data[7] = static_cast<uint8>(m_write_protect ? 1 : 0);
// number of sectors per platter
data[8] = static_cast<uint8>((m_num_platter_sectors >> 0) & 0xFF);
data[9] = static_cast<uint8>((m_num_platter_sectors >> 8) & 0xFF);
data[10] = static_cast<uint8>(m_disk_type);
data[11] = static_cast<uint8>(m_num_platters-1);
strncpy(reinterpret_cast<char*>(&data[16]), m_label.c_str(), WVD_MAX_LABEL_LEN);
// write the header block -- first absolute sector of disk image
return rawWriteSector(0, &data[0]);
}
// make sure metadata is up to date
void
Wvd::reopen()
{
assert(m_file != nullptr);
if (m_metadata_stale) {
assert(!m_file->is_open()); // make sure we cached it properly
// set up a file handle
m_file->open(m_path.c_str(), std::fstream::in | std::fstream::out | std::fstream::binary);
if (!m_file->good()) {
UI_error("Couldn't open file '%s'", m_path.c_str());
m_file = nullptr;
return;
}
const bool ok = readHeader();
if (!ok) {
m_file = nullptr;
return;
}
}
m_metadata_stale = false;
}
// retrieve the metadata from the virtual disk image.
// if the file is already open, just read it;
// if the file isn't open, open it, read the metadata, and leave it open.
// return true on success, otherwise complain why and return false.
bool
Wvd::readHeader()
{
assert(m_file != nullptr);
// set it so rawReadSector() knows what to operate on
m_num_platters = 1;
m_num_platter_sectors = 1;
uint8 data[256];
const bool ok = rawReadSector(0, &data[0]);
if (!ok) {
return false;
}
// check magic
if (data[0] != 'W' ||
data[1] != 'A' ||
data[2] != 'N' ||
data[3] != 'G' ||
data[4] != '\0') {
UI_warn("This isn't a Wang Virtual Disk");
return false;
}
// check read format
if (data[6] != 0x00) {
UI_error("This disk is from a more recent version of WangEmu.\n"
"Please use a more recent emulator.");
return false;
}
const bool tmp_write_protect = (data[7] != 0x00);
const int tmp_sectors = static_cast<int>((data[9] << 8) | data[8]);
if (tmp_sectors > WVD_MAX_SECTORS) {
UI_error("The disk claims to have more than %d platters", WVD_MAX_SECTORS);
return false;
}
disktype_t tmp_disktype = static_cast<disktype_t>(data[10]);
if (tmp_disktype >= DISKTYPE_ILLEGAL) {
UI_error("The disktype field of the disk image isn't legal\nForcing it to 5.25\" floppy");
tmp_disktype = DISKTYPE_FD5;
}
const int tmp_platters = static_cast<int>(data[11] + 1);
if (tmp_platters > WVD_MAX_PLATTERS) {
UI_error("The disk claims to have more than %d platters", WVD_MAX_PLATTERS);
return false;
}
if (strlen(reinterpret_cast<const char*>(&data[16])) > WVD_MAX_LABEL_LEN-1) {
UI_error("The label appears to be too long.\n"
"Is the disk image corrupt?");
return false;
}
std::string tmp_label(reinterpret_cast<const char*>(&data[16]));
m_metadata_modified = false;
m_label = tmp_label;
m_disk_type = tmp_disktype;
m_num_platters = tmp_platters;
m_num_platter_sectors = tmp_sectors;
m_write_protect = tmp_write_protect;
return true;
}
// format the given platter of the virtual disk image.
// returns true if successful.
bool
Wvd::format(const int platter)
{
assert(platter >= 0 && platter < m_num_platters);
// fill all non-header sectors with 0x00
uint8 data[256];
memset(&data[0], static_cast<uint8>(0x00), 256);
bool ok = true;
for (int n=0; ok && n < m_num_platter_sectors; n++) {
ok = writeSector(platter, n, &data[0]);
}
return ok;
}
// create a virtual disk file if it doesn't exist, erase it if does.
// write the header and then format all platters.
// returns true on success.
bool
Wvd::createFile(const std::string &filename)
{
assert(m_file == nullptr);
assert(!m_has_path);
assert(!filename.empty());
m_has_path = true;
m_path = filename;
// create the file if it doesn't exist; erase if it does
m_file = std::make_unique<std::fstream>(
filename.c_str(),
std::fstream::in | std::fstream::out | std::fstream::trunc | std::fstream::binary);
if (!m_file) {
return false;
}
if (!m_file->good()) {
UI_error("Couldn't create file '%s'", m_path.c_str());
m_file = nullptr;
return false;
}
bool ok = writeHeader();
if (ok) {
for (int p=0; ok && p < m_num_platters; p++) {
ok = format(p);
}
}
return ok;
}
// vim: ts=8:et:sw=4:smarttab