-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathdisk_drive.cpp
86 lines (75 loc) · 3.17 KB
/
disk_drive.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
/*
* disk_drive.cpp - Virtual disk drive class.
*
* Copyright (c) 2012 Whizzo Software LLC (Daniel Noguerol)
*
* This file is part of the SIO2Arduino project which emulates
* Atari 8-bit SIO devices on Arduino hardware.
*
* SIO2Arduino is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* SIO2Arduino is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with SIO2Arduino; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "disk_drive.h"
DiskDrive::DiskDrive() {
// reset device status
memset(&m_driveStatus.statusFrame, 0, sizeof(m_driveStatus.statusFrame));
// set standard attributes
m_driveStatus.statusFrame.timeout_lsb = 0xE0;
}
DriveStatus* DiskDrive::getStatus() {
m_driveStatus.statusFrame.commandStatus.writeProtect = m_diskImage.isReadOnly() ? 0x01 : 0x00;
return &m_driveStatus;
}
boolean DiskDrive::setImageFile(SdFile *file) {
boolean result = m_diskImage.setFile(file);
if (result) {
// set device status
memset(&m_driveStatus.statusFrame, 0, sizeof(m_driveStatus.statusFrame));
m_driveStatus.statusFrame.commandStatus.enhancedDensity = m_diskImage.isEnhancedDensity() ? 0x01 : 0x00;
m_driveStatus.statusFrame.commandStatus.doubleDensity = m_diskImage.isDoubleDensity() ? 0x01 : 0x00;
m_driveStatus.statusFrame.hardwareStatus.writeProtect = m_diskImage.isReadOnly() ? 0x00 : 0x01;
m_driveStatus.sectorSize = m_diskImage.getSectorSize();
}
return result;
}
SectorDataInfo* DiskDrive::getSectorData(unsigned long sector, byte *data) {
if (m_diskImage.hasImage()) {
unsigned long startTime = micros();
SectorDataInfo *info = m_diskImage.getSectorData(sector, data);
// store the status frame if valid
if (info->validStatusFrame) {
memcpy(&m_driveStatus.statusFrame, &(info->statusFrame), sizeof(m_driveStatus.statusFrame));
}
// for images with copy-protection, make read time consistent across all sector reads to
// allow skew-based protection to work
if (m_diskImage.hasCopyProtection()) {
unsigned long time = micros() - startTime;
unsigned long delta = (MIN_PRO_SECTOR_READ * ((time - 1) / MIN_PRO_SECTOR_READ + 1)) - time;
delay(delta / 1000); // Arduino's delayMicroseconds() can't reliably
delayMicroseconds(delta % 1000); // handle an argument larger than 16,383
}
return info;
} else {
return NULL;
}
}
unsigned long DiskDrive::writeSectorData(unsigned long sector, byte *data, unsigned long len) {
return m_diskImage.writeSectorData(sector, data, len);
}
boolean DiskDrive::formatImage(SdFile *file, int density) {
return m_diskImage.format(file, density);
}
boolean DiskDrive::hasImage() {
return m_diskImage.hasImage();
}