27 |
|
*/ |
28 |
|
|
29 |
|
#include <string.h> |
30 |
+ |
#include <vector> |
31 |
+ |
|
32 |
+ |
#ifndef NO_STD_NAMESPACE |
33 |
+ |
using std::vector; |
34 |
+ |
#endif |
35 |
|
|
36 |
|
#include "sysdeps.h" |
37 |
|
#include "cpu_emulation.h" |
70 |
|
|
71 |
|
|
72 |
|
// Struct for each drive |
73 |
< |
struct DriveInfo { |
74 |
< |
DriveInfo() |
75 |
< |
{ |
76 |
< |
next = NULL; |
77 |
< |
num = 0; |
73 |
< |
fh = NULL; |
74 |
< |
read_only = false; |
75 |
< |
status = 0; |
76 |
< |
} |
73 |
> |
struct disk_drive_info { |
74 |
> |
disk_drive_info() : num(0), fh(NULL), read_only(false), status(0) {} |
75 |
> |
disk_drive_info(void *fh_, bool ro) : num(0), fh(fh_), read_only(ro), status(0) {} |
76 |
> |
|
77 |
> |
void close_fh(void) { Sys_close(fh); } |
78 |
|
|
78 |
– |
DriveInfo *next; // Pointer to next DriveInfo (must be first in struct!) |
79 |
|
int num; // Drive number |
80 |
|
void *fh; // File handle |
81 |
|
uint32 num_blocks; // Size in 512-byte blocks |
84 |
|
uint32 status; // Mac address of drive status record |
85 |
|
}; |
86 |
|
|
87 |
< |
// Linked list of DriveInfos |
88 |
< |
static DriveInfo *first_drive_info; |
87 |
> |
// List of drives handled by this driver |
88 |
> |
typedef vector<disk_drive_info> drive_vec; |
89 |
> |
static drive_vec drives; |
90 |
|
|
91 |
|
// Icon address (Mac address space, set by PatchROM()) |
92 |
|
uint32 DiskIconAddr; |
96 |
|
|
97 |
|
|
98 |
|
/* |
99 |
< |
* Get pointer to drive info, NULL = invalid drive number |
99 |
> |
* Get pointer to drive info or drives.end() if not found |
100 |
|
*/ |
101 |
|
|
102 |
< |
static DriveInfo *get_drive_info(int num) |
102 |
> |
static drive_vec::iterator get_drive_info(int num) |
103 |
|
{ |
104 |
< |
DriveInfo *info = first_drive_info; |
105 |
< |
while (info != NULL) { |
104 |
> |
drive_vec::iterator info, end = drives.end(); |
105 |
> |
for (info = drives.begin(); info != end; ++info) { |
106 |
|
if (info->num == num) |
107 |
|
return info; |
107 |
– |
info = info->next; |
108 |
|
} |
109 |
< |
return NULL; |
109 |
> |
return info; |
110 |
|
} |
111 |
|
|
112 |
|
|
116 |
|
|
117 |
|
void DiskInit(void) |
118 |
|
{ |
119 |
– |
first_drive_info = NULL; |
120 |
– |
|
119 |
|
// No drives specified in prefs? Then add defaults |
120 |
|
if (PrefsFindString("disk", 0) == NULL) |
121 |
|
SysAddDiskPrefs(); |
122 |
|
|
123 |
|
// Add drives specified in preferences |
124 |
< |
int32 index = 0; |
124 |
> |
int index = 0; |
125 |
|
const char *str; |
126 |
|
while ((str = PrefsFindString("disk", index++)) != NULL) { |
127 |
|
bool read_only = false; |
130 |
|
str++; |
131 |
|
} |
132 |
|
void *fh = Sys_open(str, read_only); |
133 |
< |
if (fh) { |
134 |
< |
D(bug(" adding drive '%s'\n", str)); |
137 |
< |
DriveInfo *info = new DriveInfo; |
138 |
< |
info->fh = fh; |
139 |
< |
info->read_only = SysIsReadOnly(fh); |
140 |
< |
DriveInfo *p = (DriveInfo *)&first_drive_info; |
141 |
< |
while (p->next != NULL) |
142 |
< |
p = p->next; |
143 |
< |
p->next = info; |
144 |
< |
} |
133 |
> |
if (fh) |
134 |
> |
drives.push_back(disk_drive_info(fh, SysIsReadOnly(fh))); |
135 |
|
} |
136 |
|
} |
137 |
|
|
142 |
|
|
143 |
|
void DiskExit(void) |
144 |
|
{ |
145 |
< |
DriveInfo *info = first_drive_info, *next; |
146 |
< |
while (info != NULL) { |
147 |
< |
Sys_close(info->fh); |
148 |
< |
next = info->next; |
159 |
< |
delete info; |
160 |
< |
info = next; |
161 |
< |
} |
145 |
> |
drive_vec::iterator info, end = drives.end(); |
146 |
> |
for (info = drives.begin(); info != end; ++info) |
147 |
> |
info->close_fh(); |
148 |
> |
drives.clear(); |
149 |
|
} |
150 |
|
|
151 |
|
|
155 |
|
|
156 |
|
bool DiskMountVolume(void *fh) |
157 |
|
{ |
158 |
< |
DriveInfo *info; |
159 |
< |
for (info = first_drive_info; info != NULL && info->fh != fh; info = info->next) ; |
160 |
< |
if (info) { |
158 |
> |
drive_vec::iterator info = drives.begin(), end = drives.end(); |
159 |
> |
while (info != end && info->fh != fh) |
160 |
> |
++info; |
161 |
> |
if (info != end) { |
162 |
|
if (SysIsDiskInserted(info->fh)) { |
163 |
|
info->read_only = SysIsReadOnly(info->fh); |
164 |
|
WriteMacInt8(info->status + dsDiskInPlace, 1); // Inserted removable disk |
181 |
|
|
182 |
|
static void mount_mountable_volumes(void) |
183 |
|
{ |
184 |
< |
DriveInfo *info = first_drive_info; |
185 |
< |
while (info != NULL) { |
184 |
> |
drive_vec::iterator info, end = drives.end(); |
185 |
> |
for (info = drives.begin(); info != end; ++info) { |
186 |
|
|
187 |
|
// Disk in drive? |
188 |
|
if (!ReadMacInt8(info->status + dsDiskInPlace)) { |
201 |
|
Execute68kTrap(0xa02f, &r); // PostEvent() |
202 |
|
info->to_be_mounted = false; |
203 |
|
} |
216 |
– |
|
217 |
– |
info = info->next; |
204 |
|
} |
205 |
|
} |
206 |
|
|
218 |
|
acc_run_called = false; |
219 |
|
|
220 |
|
// Install drives |
221 |
< |
for (DriveInfo *info = first_drive_info; info; info = info->next) { |
221 |
> |
drive_vec::iterator info, end = drives.end(); |
222 |
> |
for (info = drives.begin(); info != end; ++info) { |
223 |
|
|
224 |
|
info->num = FindFreeDriveNumber(1); |
225 |
|
info->to_be_mounted = false; |
276 |
|
WriteMacInt32(pb + ioActCount, 0); |
277 |
|
|
278 |
|
// Drive valid and disk inserted? |
279 |
< |
DriveInfo *info; |
280 |
< |
if ((info = get_drive_info(ReadMacInt16(pb + ioVRefNum))) == NULL) |
279 |
> |
drive_vec::iterator info = get_drive_info(ReadMacInt16(pb + ioVRefNum)); |
280 |
> |
if (info == drives.end()) |
281 |
|
return nsDrvErr; |
282 |
|
if (!ReadMacInt8(info->status + dsDiskInPlace)) |
283 |
|
return offLinErr; |
339 |
|
} |
340 |
|
|
341 |
|
// Drive valid? |
342 |
< |
DriveInfo *info; |
343 |
< |
if ((info = get_drive_info(ReadMacInt16(pb + ioVRefNum))) == NULL) |
342 |
> |
drive_vec::iterator info = get_drive_info(ReadMacInt16(pb + ioVRefNum)); |
343 |
> |
if (info == drives.end()) |
344 |
|
return nsDrvErr; |
345 |
|
|
346 |
|
// Drive-specific codes |
404 |
|
|
405 |
|
int16 DiskStatus(uint32 pb, uint32 dce) |
406 |
|
{ |
407 |
< |
DriveInfo *info = get_drive_info(ReadMacInt16(pb + ioVRefNum)); |
407 |
> |
drive_vec::iterator info = get_drive_info(ReadMacInt16(pb + ioVRefNum)); |
408 |
|
uint16 code = ReadMacInt16(pb + csCode); |
409 |
|
D(bug("DiskStatus %d\n", code)); |
410 |
|
|
411 |
< |
// General codes |
411 |
> |
// General codes (we can get these even if the drive was invalid) |
412 |
|
switch (code) { |
413 |
|
case 43: { // Driver gestalt |
414 |
|
uint32 sel = ReadMacInt32(pb + csParam); |
418 |
|
WriteMacInt32(pb + csParam + 4, 0x01008000); |
419 |
|
break; |
420 |
|
case FOURCC('d','e','v','t'): // Device type |
421 |
< |
if (info != NULL) { |
421 |
> |
if (info != drives.end()) { |
422 |
|
if (ReadMacInt8(info->status + dsDiskInPlace) == 8) |
423 |
|
WriteMacInt32(pb + csParam + 4, FOURCC('d','i','s','k')); |
424 |
|
else |
433 |
|
WriteMacInt32(pb + csParam + 4, 0x01000000); |
434 |
|
break; |
435 |
|
case FOURCC('b','o','o','t'): // Boot ID |
436 |
< |
if (info != NULL) |
436 |
> |
if (info != drives.end()) |
437 |
|
WriteMacInt16(pb + csParam + 4, info->num); |
438 |
|
else |
439 |
|
WriteMacInt16(pb + csParam + 4, 0); |
462 |
|
} |
463 |
|
|
464 |
|
// Drive valid? |
465 |
< |
if (info == NULL) |
465 |
> |
if (info == drives.end()) |
466 |
|
return nsDrvErr; |
467 |
|
|
468 |
|
// Drive-specific codes |