openMSX
SectorBasedDisk.cc
Go to the documentation of this file.
1#include "SectorBasedDisk.hh"
2#include "MSXException.hh"
3#include "xrange.hh"
4#include <cassert>
5
6namespace openmsx {
7
9 : Disk(std::move(name_))
10{
11}
12
13void SectorBasedDisk::writeTrackImpl(uint8_t track, uint8_t side, const RawTrack& input)
14{
15 for (auto& s : input.decodeAll()) {
16 // Ignore 'track' and 'head' information
17 // Always assume sector-size = 512 (so also ignore sizeCode).
18 // Ignore CRC value/errors of both address and data.
19 // Ignore sector type (deleted or not)
20 // Ignore sectors that are outside the range 1..sectorsPerTrack
21 if ((s.sector < 1) || (s.sector > getSectorsPerTrack())) continue;
22 SectorBuffer buf;
23 input.readBlock(s.dataIdx, buf.raw);
24 auto logicalSector = physToLog(track, side, s.sector);
25 writeSector(logicalSector, buf);
26 // it's important to use writeSector() and not writeSectorImpl()
27 // because only the former flushes SHA1 cache
28 }
29}
30
31void SectorBasedDisk::readTrack(uint8_t track, uint8_t side, RawTrack& output)
32{
33 // Try to cache the last result of this method (the cache will be
34 // flushed on any write to the disk). This very simple cache mechanism
35 // will typically already have a very high hit-rate. For example during
36 // emulation of a WD2793 read sector, we also emulate the search for
37 // the correct sector. So the disk rotates from sector to sector, and
38 // each time we re-read the track data (because EmuTime has passed).
39 // Typically the software will also read several sectors from the same
40 // track before moving to the next.
42 int num = track | (side << 8);
43 if (num == cachedTrackNum) {
44 output = cachedTrackData;
45 return;
46 }
47 cachedTrackNum = num;
48
49 // This disk image only stores the actual sector data, not all the
50 // extra gap, sync and header information that is in reality stored
51 // in between the sectors. This function transforms the cooked sector
52 // data back into raw track data. It assumes a standard IBM double
53 // density, 9 sectors/track, 512 bytes/sector track layout.
54 //
55 // -- track --
56 // gap4a 80 x 0x4e
57 // sync 12 x 0x00
58 // index mark 3 x 0xc2(*)
59 // 1 x 0xfc
60 // gap1 50 x 0x4e
61 // 9 x [sector] 9 x [[658]]
62 // gap4b 182 x 0x4e
63 //
64 // -- sector --
65 // sync 12 x 0x00
66 // ID addr mark 3 x 0xa1(*)
67 // 1 x 0xfe
68 // C H R N 4 x [..]
69 // CRC 2 x [..]
70 // gap2 22 x 0x4e
71 // sync 12 x 0x00
72 // data mark 3 x 0xa1(*)
73 // 1 x 0xfb
74 // data 512 x [..] <-- actual sector data
75 // CRC 2 x [..]
76 // gap3 84 x 0x4e
77 //
78 // (*) Missing clock transitions in MFM encoding
79
80 try {
81 output.clear(RawTrack::STANDARD_SIZE); // clear idam positions
82
83 int idx = 0;
84 auto write = [&](unsigned n, uint8_t value) {
85 repeat(n, [&] { output.write(idx++, value); });
86 };
87
88 write(80, 0x4E); // gap4a
89 write(12, 0x00); // sync
90 write( 3, 0xC2); // index mark (1)
91 write( 1, 0xFC); // (2)
92 write(50, 0x4E); // gap1
93
94 for (auto j : xrange(9)) {
95 write(12, 0x00); // sync
96
97 write( 3, 0xA1); // addr mark (1)
98 output.write(idx++, 0xFE, true); // (2) add idam
99 output.write(idx++, track); // C: Cylinder number
100 output.write(idx++, side); // H: Head Address
101 output.write(idx++, j + 1); // R: Record
102 output.write(idx++, 0x02); // N: Number (length of sector: 512 = 128 << 2)
103 uint16_t addrCrc = output.calcCrc(idx - 8, 8);
104 output.write(idx++, addrCrc >> 8); // CRC (high byte)
105 output.write(idx++, addrCrc & 0xff); // (low byte)
106
107 write(22, 0x4E); // gap2
108 write(12, 0x00); // sync
109
110 write( 3, 0xA1); // data mark (1)
111 write( 1, 0xFB); // (2)
112
113 auto logicalSector = physToLog(track, side, j + 1);
114 SectorBuffer buf;
115 readSector(logicalSector, buf);
116 for (auto& r : buf.raw) output.write(idx++, r);
117
118 uint16_t dataCrc = output.calcCrc(idx - (512 + 4), 512 + 4);
119 output.write(idx++, dataCrc >> 8); // CRC (high byte)
120 output.write(idx++, dataCrc & 0xff); // (low byte)
121
122 write(84, 0x4E); // gap3
123 }
124
125 write(182, 0x4E); // gap4b
126 assert(idx == RawTrack::STANDARD_SIZE);
127 } catch (MSXException& /*e*/) {
128 // There was an error while reading the actual sector data.
129 // Most likely this is because we're reading the 81th track on
130 // a disk with only 80 tracks (or similar). If you do this on a
131 // real disk, you simply read an 'empty' track. So we do the
132 // same here.
133 output.clear(RawTrack::STANDARD_SIZE);
134 cachedTrackNum = -1; // needed?
135 }
136 cachedTrackData = output;
137}
138
140{
142 cachedTrackNum = -1;
143}
144
145size_t SectorBasedDisk::getNbSectorsImpl() const
146{
147 assert(nbSectors != size_t(-1)); // must have been initialized
148 return nbSectors;
149}
151{
152 assert(nbSectors == size_t(-1)); // can only set this once
153 nbSectors = num;
154}
155
157{
158 // the following are just heuristics...
159
160 if (getNbSectors() == 1440) {
161 // explicitly check for 720kb filesize
162
163 // "trojka.dsk" is 720kb, but has boot sector and FAT media ID
164 // for a single sided disk. From an emulator point of view it
165 // must be accessed as a double sided disk.
166
167 // "SDSNAT2.DSK" has invalid media ID in both FAT and
168 // boot sector, other data in the boot sector is invalid as well.
169 // Although the first byte of the boot sector is 0xE9 to indicate
170 // valid boot sector data. The only way to detect the format is
171 // to look at the disk image filesize.
172
174 setNbSides(2);
175
176 } else {
177 // Don't check for "360kb -> single sided disk". The MSXMania
178 // disks are double sided disk but are truncated at 360kb.
180 }
181}
182
183} // namespace openmsx
size_t physToLog(uint8_t track, uint8_t side, uint8_t sector)
Definition: Disk.cc:36
unsigned getSectorsPerTrack()
Definition: Disk.cc:63
void setNbSides(unsigned num)
Definition: Disk.hh:44
void setSectorsPerTrack(unsigned num)
Definition: Disk.hh:42
virtual void detectGeometry()
Definition: Disk.cc:79
std::vector< Sector > decodeAll() const
Get info on all sectors in this track.
Definition: RawTrack.cc:110
static constexpr unsigned STANDARD_SIZE
Definition: RawTrack.hh:74
void readBlock(int idx, std::span< uint8_t > destination) const
Like memcpy() but copy from/to circular buffer.
Definition: RawTrack.cc:157
void readSector(size_t sector, SectorBuffer &buf)
void writeSector(size_t sector, const SectorBuffer &buf)
void detectGeometry() override
void setNbSectors(size_t num)
SectorBasedDisk(DiskName name)
This file implemented 3 utility functions:
Definition: Autofire.cc:9
STL namespace.
std::array< uint8_t, 512 > raw
constexpr void repeat(T n, Op op)
Repeat the given operation 'op' 'n' times.
Definition: xrange.hh:148
constexpr auto xrange(T e)
Definition: xrange.hh:133