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
|
#include "FloppyDrive.class.h"
#include "FloppyController.class.h"
#include <TaskManager/Task.ns.h>
#include <DeviceManager/Time.ns.h>
#include <VTManager/SimpleVT.class.h>
#include <Core/Log.ns.h>
using namespace Sys;
FloppyDrive::FloppyDrive(FloppyController *fdc, u8int number, u8int type) {
m_fdc = fdc;
m_driveNumber = number;
m_driveType = type;
fdc->m_drives[number] = this;
switch (m_driveType) {
case FT_360K525:
m_cylinders = 40;
m_sectors = 9;
break;
case FT_12M525:
m_cylinders = 80;
m_sectors = 15;
break;
case FT_720K35:
m_cylinders = 80;
m_sectors = 9;
break;
case FT_288M35:
m_cylinders = 80;
m_sectors = 36;
break;
case FT_144M35:
default: //all unknown types are 1.44M
m_cylinders = 80;
m_sectors = 18;
break;
}
m_buffCyl = 0xFFFF; //Invalid cylinder
m_buffTime = 0;
}
String FloppyDrive::getClass() {
return "block.floppy";
}
String FloppyDrive::getName() {
char *fdTypes[] = {
"NONE",
"360kB 5.25in",
"1.2MB 5.25in",
"720kB 3.5in",
"1.44MB 3.5in",
"2.88MB 3.5in" };
String ret;
if (m_driveNumber == 0) {
ret = "Floppy drive #0 : ";
} else {
ret = "Floppy drive #1 : ";
}
ret += fdTypes[m_driveType];
return ret;
}
bool FloppyDrive::setup() {
m_fdc->setActiveDrive(m_driveNumber);
//Set transfer rate
u8int xfer_rate;
if (m_driveType == FT_360K525) {
xfer_rate = 2;
} else if (m_driveType == FT_720K35) {
xfer_rate = 1;
} else {
xfer_rate = 0;
}
outb(m_fdc->m_base + FR_CCR, xfer_rate);
m_fdc->writeCmd(FC_SPECIFY);
m_fdc->writeCmd(0xDF);
m_fdc->writeCmd(0x02);
bool ret = calibrate();
m_fdc->setNoActiveDrive();
return ret;
}
bool FloppyDrive::calibrate() {
if (!m_fdc->m_driveMutex.locked() or m_fdc->m_activeDrive != m_driveNumber) return false;
int st0, cyl = -1;
if (!setMotorState(true)) return false;
for (int i = 0; i < 10; i++) {
m_fdc->resetIrq();
m_fdc->writeCmd(FC_RECALIBRATE);
m_fdc->writeCmd(m_driveNumber);
m_fdc->waitIrq();
m_fdc->checkInterrupt(&st0, &cyl);
if (st0 & 0xC0) {
continue;
}
if (!cyl) { //Cylinder 0 reached
setMotorState(false);
return true;
}
}
setMotorState(false);
*kvt << getName() << ": calibrate fail\n";
return false;
}
bool FloppyDrive::setMotorState(bool on) {
if (!m_fdc->m_driveMutex.locked() or m_fdc->m_activeDrive != m_driveNumber) return false;
if (on) {
if (m_motorState == FS_MOTOROFF) {
m_motorState = FS_MOTORON;
m_fdc->setDOR();
Task::currThread()->sleep(500);
}
m_motorState = FS_MOTORON;
} else {
m_motorState = FS_MOTORWAIT;
m_motorTimeout = 4;
}
return true;
}
bool FloppyDrive::killMotor() {
m_motorState = FS_MOTOROFF;
m_fdc->setDOR();
return true;
}
bool FloppyDrive::seek(u32int cyli, s32int head) {
if (cyli >= m_cylinders) return false;
m_fdc->setActiveDrive(m_driveNumber);
setMotorState(true); //Turn on motor
int st0, cyl = -1;
for (u32int i = 0; i < 10; i++) {
m_fdc->resetIrq();
m_fdc->writeCmd(FC_SEEK);
m_fdc->writeCmd(head << 2);
m_fdc->writeCmd(cyl);
m_fdc->waitIrq();
m_fdc->checkInterrupt(&st0, &cyl);
if (st0 & 0xC0) { //Error
continue;
}
if (cyl == 0xFF or cyl == 0x00 or cyl == (int)cyli) { //0xFF for bochs, 0x00 for qemu :D
setMotorState(false);
m_fdc->setNoActiveDrive();
return true;
}
}
setMotorState(false);
m_fdc->setNoActiveDrive();
*kvt << getName() << ": seek fail\n";
return false;
}
bool FloppyDrive::doTrack(u32int cyl, u8int dir) {
if (!seek(cyl, 0)) return false;
if (!seek(cyl, 1)) return false;
m_fdc->setActiveDrive(m_driveNumber);
u8int cmd, flags = 0xC0;
switch (dir) {
case FD_READ:
cmd = FC_READ_DATA | flags;
break;
case FD_WRITE:
cmd = FC_WRITE_DATA | flags;
break;
default:
m_fdc->setNoActiveDrive();
return false;
}
for (int i = 0; i < 20; i++) {
setMotorState(true);
if (!FloppyController::dmaInit(dir, 0x4800)) {
m_fdc->setNoActiveDrive();
setMotorState(false);
return false;
}
Task::currThread()->sleep(100);
m_fdc->resetIrq();
m_fdc->writeCmd(cmd);
m_fdc->writeCmd(m_driveNumber); //Drive number & first head << 2
m_fdc->writeCmd(cyl); //Cylinder
m_fdc->writeCmd(0); //First head
m_fdc->writeCmd(1); //First sector
m_fdc->writeCmd(2);
m_fdc->writeCmd(18); //Number of sectors
m_fdc->writeCmd(0x1B);
m_fdc->writeCmd(0xFF);
m_fdc->waitIrq();
u8int st0, st1, st2, rcy, rhe, rse, bps;
st0 = m_fdc->readData();
st1 = m_fdc->readData();
st2 = m_fdc->readData();
rcy = m_fdc->readData();
rhe = m_fdc->readData();
rse = m_fdc->readData();
bps = m_fdc->readData();
int error = 0;
if (st0 & 0xC0) error = 1;
if (st1 & 0x80) { Log::log(KL_ERROR, "FloppyDrive.class : error while I/O : end of cyilnder."); error = 1; }
if (st0 & 0x08) { Log::log(KL_ERROR, "FloppyDrive.class : error while I/O : drive not ready."); error = 1; }
if (st1 & 0x20) { Log::log(KL_ERROR, "FloppyDrive.class : error while I/O : CRC error."); error = 1; }
if (st1 & 0x10) { Log::log(KL_ERROR, "FloppyDrive.class : error while I/O : controller timeout."); error = 1; }
if (st1 & 0x04) { Log::log(KL_ERROR, "FloppyDrive.class : error while I/O : no data found."); error = 1; }
if ((st2|st1) & 0x01) {
Log::log(KL_ERROR, "FloppyDrive.class : error while I/O : no address mark found.");
error=1;
}
if (st2 & 0x40) { Log::log(KL_ERROR, "FloppyDrive.class : error while I/O : deleted address mark."); error = 1; }
if (st2 & 0x20) { Log::log(KL_ERROR, "FloppyDrive.class : error while I/O : CRC error in data."); error = 1; }
if (st2 & 0x10) { Log::log(KL_ERROR, "FloppyDrive.class : error while I/O : wrong cylinder."); error = 1; }
if (st2 & 0x04) {
Log::log(KL_ERROR, "FloppyDrive.class : error while I/O : uPD765 sector not found.");
error = 1;
}
if (st2 & 0x02) { Log::log(KL_ERROR, "FloppyDrive.class : error while I/O : bad cylinder."); error = 1; }
if (bps != 0x2) {
Log::log(KL_ERROR, String("FloppyDrive.class : error while I/O : wanted 512 bytes/sector, got ")
+= String::number(1 << (bps + 7)));
error = 1;
}
if (st1 & 0x02) { Log::log(KL_ERROR, "FloppyDrive.class : error while I/O : not writable."); error = 2; }
if (!error) CMem::memcpy(m_buffer, FloppyController::dmabuff, FLOPPY_DMALEN); //Copy data to internal buffer
FloppyController::dmaRelease();
if (!error) {
setMotorState(false);
m_fdc->setNoActiveDrive();
return true;
}
if (error > 1) {
//Not retrying
setMotorState(false);
m_fdc->setNoActiveDrive();
return false;
}
}
setMotorState(false);
return false;
}
bool FloppyDrive::readOnly() {
m_fdc->setActiveDrive(m_driveNumber);
m_fdc->resetIrq();
m_fdc->writeCmd(FC_SENSE_DRIVE_STATUS);
m_fdc->writeCmd(m_driveNumber);
m_fdc->waitIrq();
u8int st3 = m_fdc->readData();
bool ret = (st3 & 0x80 ? true : false);
m_fdc->setNoActiveDrive();
return ret;
}
u64int FloppyDrive::blocks() {
return m_cylinders * m_sectors * 2;
}
bool FloppyDrive::readBlocks(u64int start, u32int count, u8int *data) {
u32int startblock = start;
if (count == 1) {
u32int cylinder = (startblock / (m_sectors * 2)), offset = (startblock % (m_sectors * 2)) * 512;
if (m_buffCyl != cylinder or m_buffTime < Time::uptime() - 4) {
if (!doTrack(cylinder, FD_READ)) return false;
m_buffCyl = cylinder;
m_buffTime = Time::uptime();
}
memcpy(data, (const u8int*)(&m_buffer[offset]), 512);
return true;
} else {
m_buffCyl = 0xFFFF; //Invalid cylinder
m_buffTime = 0;
u32int startcylinder = (startblock / (m_sectors * 2)), stoffset = startblock % (m_sectors * 2);
u32int endcylinder = ((startblock + count - 1) / (m_sectors * 2)),
endoffset = (startblock + count - 1) % (m_sectors * 2);
for (u32int i = startcylinder; i <= endcylinder; i++) {
if (!doTrack(i, FD_READ)) return false; //Read cylinder
//Figure out what to memcpy in data
u32int start = 0; //Where to start memcpying in buffer
if (i == startcylinder) start = stoffset * 512;
u32int count = FLOPPY_DMALEN - start; //How much we must memcpy
if (i == endcylinder) count = (endoffset * 512) - start + 512;
u32int where = 0;
if (i != startcylinder) where = (i - startcylinder - 1) * (512 * 2 * m_sectors) + (stoffset * 512);
memcpy(&data[where], (const u8int*)(&m_buffer[start]), count);
}
}
return true;
}
bool FloppyDrive::writeBlocks(u64int start, u32int count, u8int *data) { //TODO
return false;
}
u32int FloppyDrive::blockSize() {
return 512;
}
u64int FloppyDrive::chs2lba(u32int cylinder, u32int head, u32int sector) {
return ((u64int)cylinder * (u64int)m_cylinders) + ((u64int)head + 2ULL) + (u64int)sector - 1ULL;
}
|