MacIIROMDiskDriver/rdisk.c

383 lines
12 KiB
C
Raw Normal View History

2020-06-22 04:10:31 +00:00
#include <Memory.h>
#include <Devices.h>
#include <Files.h>
#include <Disks.h>
#include <Errors.h>
#include <Events.h>
#include <OSUtils.h>
2020-06-26 07:12:08 +00:00
#include "rdisk.h"
2020-07-02 07:06:35 +00:00
const char RDiskIcon[285] = {
2020-06-27 02:13:22 +00:00
// Icon
2020-07-02 07:06:35 +00:00
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b11111111, 0b11111111, 0b11111111, 0b11111111,
0b10000000, 0b00000000, 0b00000000, 0b00000001,
0b10001111, 0b00011110, 0b00111100, 0b01111001,
0b10001001, 0b00010010, 0b00100100, 0b01001001,
0b10001001, 0b00010010, 0b00100100, 0b01001001,
0b10001001, 0b00010010, 0b00100100, 0b01001001,
0b10001111, 0b00011110, 0b00111100, 0b01111001,
0b11000000, 0b00000000, 0b00000000, 0b00000001,
0b01010101, 0b01010101, 0b11010101, 0b01010101,
0b01111111, 0b11111111, 0b01111111, 0b11111111,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
2020-06-27 02:13:22 +00:00
// Mask
2020-07-02 07:06:35 +00:00
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b11111111, 0b11111111, 0b11111111, 0b11111111,
0b11111111, 0b11111111, 0b11111111, 0b11111111,
0b11111111, 0b11111111, 0b11111111, 0b11111111,
0b11111111, 0b11111111, 0b11111111, 0b11111111,
0b11111111, 0b11111111, 0b11111111, 0b11111111,
0b11111111, 0b11111111, 0b11111111, 0b11111111,
0b11111111, 0b11111111, 0b11111111, 0b11111111,
0b11111111, 0b11111111, 0b11111111, 0b11111111,
0b01111111, 0b11111111, 0b11111111, 0b11111111,
0b01111111, 0b11111111, 0b11111111, 0b11111111,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000, 0b00000000, 0b00000000, 0b00000000,
27, 'G', 'a', 'r', 'r', 'e', 't', 't', '\'', 's', ' ',
'W', 'o', 'r', 'k', 's', 'h', 'o', 'p', ' ',
'R', 'O', 'M', ' ', 'D', 'i', 's', 'k', 0
2020-06-27 02:13:22 +00:00
};
2020-07-02 07:06:35 +00:00
static void RDiskDecodeSettings(RDiskStorage_t *c, char *enable, char* boot, char* mount, char* ram) {
char rKey, aKey, legacy_startup, legacy_ram;
// Read PRAM and keys
RDiskReadXPRAM(1, 4, &legacy_startup);
RDiskReadXPRAM(1, 5, &legacy_ram);
rKey = RDiskIsRPressed();
aKey = RDiskIsAPressed();
// Decode settings
if (c->postBoot) {
*enable = 1;
*boot = 1; // Boot set so first access works
*mount = 1;
*ram = 0;
} else if (rKey) { // R boots from ROM disk
*enable = 1;
*boot = 1;
*mount = 0;
*ram = aKey; // A enables RAM disk
} else if (legacy_startup) {
*enable = 1;
*boot = (legacy_startup & 0xFD) && (legacy_startup != 0xFD);
*mount = !*boot && legacy_startup & 0x02;
*ram = legacy_ram;
} else {
*enable = 0;
*boot = 0;
*mount = 0;
*ram = 0;
}
2020-06-30 01:08:50 +00:00
}
// Switch to 24-bit mode and copy
void RDiskCopy24(Ptr sourcePtr, Ptr destPtr, unsigned long byteCount) {
2020-06-22 04:10:31 +00:00
char mode = true32b;
SwapMMUMode(&mode);
BlockMove(sourcePtr, destPtr, byteCount);
2020-06-22 04:10:31 +00:00
SwapMMUMode(&mode);
}
#pragma parameter __D0 RDiskOpen(__A0, __A1)
2020-06-22 04:10:31 +00:00
OSErr RDiskOpen(IOParamPtr p, DCtlPtr d) {
DrvQElPtr dq;
2020-06-24 17:18:47 +00:00
int drvNum;
Ptr copy24;
2020-06-22 04:10:31 +00:00
RDiskStorage_t *c;
// Do nothing if already opened
if (d->dCtlStorage) { return noErr; }
2020-06-24 17:11:59 +00:00
2020-06-22 04:10:31 +00:00
// Figure out first available drive number
2020-06-24 17:18:47 +00:00
drvNum = 1;
2020-06-22 04:10:31 +00:00
for (dq = (DrvQElPtr)(GetDrvQHdr())->qHead; dq; dq = (DrvQElPtr)dq->qLink) {
2020-06-24 17:18:47 +00:00
if (dq->dQDrive >= drvNum) { drvNum = dq->dQDrive + 1; }
2020-06-22 04:10:31 +00:00
}
// Allocate storage struct
d->dCtlStorage = NewHandleSysClear(sizeof(RDiskStorage_t));
if (!d->dCtlStorage) { return openErr; }
// Allocate copy function buffer and copy RDiskCopy24 to it
copy24 = NewPtrSys(64);
if (!copy24) { DisposeHandle(d->dCtlStorage); return openErr; }
BlockMove(&RDiskCopy24, copy24, 64);
2020-06-22 04:10:31 +00:00
// Lock our storage struct and get master pointer
HLock(d->dCtlStorage);
c = *(RDiskStorage_t**)d->dCtlStorage;
2020-06-24 17:18:47 +00:00
// Initialize storage struct fields
2020-06-30 01:08:50 +00:00
c->initialized = 0;
2020-07-02 07:06:35 +00:00
c->removed = 0;
c->postBoot = 0;
2020-06-24 17:18:47 +00:00
c->ramdisk = NULL;
c->copy24 = (RDiskCopy_t)copy24;
2020-06-24 17:18:47 +00:00
2020-06-22 04:10:31 +00:00
// Set drive status
c->status.writeProt = -1;
2020-07-02 07:06:35 +00:00
c->status.diskInPlace = 8; // 8 means nonejectable disk
c->status.dQDrive = drvNum;
c->status.dQRefNum = d->dCtlRefNum;
c->status.driveSize = RDiskSize / 512;
c->status.driveS1 = (RDiskSize / 512) >> 16;
2020-06-24 17:18:47 +00:00
2020-07-02 07:06:35 +00:00
// Set driver flags
d->dCtlFlags |= dReadEnableMask | dWritEnableMask |
2020-06-22 04:10:31 +00:00
dCtlEnableMask | dStatEnableMask |
2020-07-02 07:06:35 +00:00
dNeedLockMask; // 0x4F
2020-06-22 04:10:31 +00:00
// Add drive to drive queue and return
RDiskAddDrive(c->status.dQRefNum, drvNum, (DrvQElPtr)&c->status.qLink);
2020-06-22 04:10:31 +00:00
return noErr;
}
2020-06-30 01:08:50 +00:00
static OSErr RDiskInit(IOParamPtr p, DCtlPtr d, RDiskStorage_t *c) {
2020-07-02 07:06:35 +00:00
char rdiskEN, mountEN, bootEN, ramEN;
2020-06-22 04:10:31 +00:00
2020-06-24 17:18:47 +00:00
// Mark init done
2020-06-30 01:08:50 +00:00
c->initialized = 1;
2020-06-22 04:10:31 +00:00
2020-07-02 07:06:35 +00:00
// Decode settings
RDiskDecodeSettings(c, &rdiskEN, &bootEN, &mountEN, &ramEN);
2020-06-22 04:10:31 +00:00
2020-07-02 07:06:35 +00:00
// If ROM disk not enabled, remove drive from drive queue
if (!rdiskEN) {
QHdrPtr head = GetDrvQHdr();
DrvQElPtr dq = (DrvQElPtr)head->qHead;
2020-07-02 07:06:35 +00:00
// Remove our drive from the system drive queue
// Loop through entire drive queue, searching for our deive
while ((dq != (DrvQElPtr)(head->qTail)) &&
2020-06-26 07:18:16 +00:00
(dq->dQRefNum != d->dCtlRefNum)) {
2020-06-22 04:10:31 +00:00
dq = (DrvQElPtr)(dq->qLink);
}
2020-07-02 07:06:35 +00:00
// If we found our drive, remove it from the queue
2020-06-22 04:10:31 +00:00
if (dq->dQRefNum == d->dCtlRefNum) {
Dequeue((QElemPtr)dq, head);
2020-06-22 04:10:31 +00:00
}
2020-07-02 07:06:35 +00:00
// Mark drive removed and offline
c->removed = 1;
c->status.diskInPlace = 0; // 0 means no disk in drive
2020-06-26 07:18:16 +00:00
return offLinErr;
2020-06-26 07:12:08 +00:00
}
2020-07-02 07:06:35 +00:00
// If RAM disk enabled, try to allocate RAM disk buffer
if (ramEN) {
if (*MMU32bit) { // 32-bit mode
unsigned long minBufPtr, newBufPtr;
// Compute if there is enough high memory
minBufPtr = ((unsigned long)*MemTop / 2) + 1024;
newBufPtr = (unsigned long)*BufPtr - RDiskSize;
if (newBufPtr > minBufPtr && (unsigned long)*BufPtr > newBufPtr) {
// Allocate RAM disk buffer by lowering BufPtr
*BufPtr = (Ptr)newBufPtr;
// Set RAM disk buffer pointer.
c->ramdisk = *BufPtr;
// Copy ROM disk image to RAM disk
BlockMove(RDiskBuf, c->ramdisk, RDiskSize);
// Clearing write protect marks RAM disk enabled
c->status.writeProt = 0;
} else { c->status.writeProt = -1; } // Not enough RAM. Stay write-only
} else { // 24-bit mode
// Put RAM disk just past 8MB
c->ramdisk = (char*)(8 * 1024 * 1024);
// Copy ROM disk image to RAM disk
((RDiskCopy_t)StripAddress(c->copy24))(
RDiskBuf, StripAddress(c->ramdisk), RDiskSize);
// Clearing write protect marks RAM disk enabled
c->status.writeProt = 0;
//FIXME: what if we don't have enough RAM?
// Will this wrap around and overwrite low memory?
// That's not the worst, since the system would just crash,
// but it would be better to switch to read-only status
}
} else { c->status.writeProt = -1; } // Otherwise write-only
// If boot disabled...
if (!bootEN) {
// Set drive offline
c->status.diskInPlace = 0; // 0 means no disk in drive
// Enable accRun to post disk inserted event if mount enabled
if (mountEN) {
d->dCtlDelay = 150;
d->dCtlFlags |= dNeedTimeMask;
}
return offLinErr;
}
return noErr;
2020-06-22 04:10:31 +00:00
}
#pragma parameter __D0 RDiskPrime(__A0, __A1)
2020-06-22 04:10:31 +00:00
OSErr RDiskPrime(IOParamPtr p, DCtlPtr d) {
RDiskStorage_t *c;
char cmd;
char *disk;
2020-06-26 07:12:08 +00:00
long offset;
2020-06-22 04:10:31 +00:00
// Return disk offline error if dCtlStorage null
if (!d->dCtlStorage) { return offLinErr; }
// Dereference dCtlStorage to get pointer to our context
c = *(RDiskStorage_t**)d->dCtlStorage;
2020-07-02 07:06:35 +00:00
// Return disk offline error if virtual disk not inserted
if (!c->status.diskInPlace) { return offLinErr; }
2020-06-22 04:10:31 +00:00
// Initialize if this is the first prime call
2020-06-30 01:08:50 +00:00
if (!c->initialized) {
2020-06-22 04:10:31 +00:00
OSErr ret = RDiskInit(p, d, c);
2020-06-24 17:18:47 +00:00
if (ret != noErr) { return ret; }
}
2020-06-22 04:10:31 +00:00
// Get pointer to RAM or ROM disk buffer
disk = c->ramdisk ? c->ramdisk : RDiskBuf;
2020-06-22 04:10:31 +00:00
// Add offset to buffer pointer according to positioning mode
switch (p->ioPosMode & 0x000F) {
case fsAtMark: offset = d->dCtlPosition; break;
2020-06-26 07:12:08 +00:00
case fsFromStart: offset = p->ioPosOffset; break;
2020-06-24 17:18:47 +00:00
case fsFromMark: offset = d->dCtlPosition + p->ioPosOffset; break;
2020-06-26 07:12:08 +00:00
default: break;
2020-06-22 04:10:31 +00:00
}
2020-06-24 18:11:30 +00:00
disk += offset;
2020-07-02 07:06:35 +00:00
// Bounds checking disabled
if (offset >= RDiskSize || p->ioReqCount >= RDiskSize ||
offset + p->ioReqCount >= RDiskSize) { return posErr; }
2020-06-22 04:10:31 +00:00
// Service read or write request
cmd = p->ioTrap & 0x00FF;
if (cmd == aRdCmd) { // Read
// Return immediately if verify operation requested
//FIXME: follow either old (verify) or new (read uncached) convention
if (p->ioPosMode & rdVerifyMask) {
2020-06-24 17:18:47 +00:00
return noErr;
}
2020-06-22 04:10:31 +00:00
// Read from disk into buffer.
2020-06-26 07:12:08 +00:00
if (*MMU32bit) { BlockMove(disk, p->ioBuffer, p->ioReqCount); }
else {
((RDiskCopy_t)StripAddress(c->copy24))(
disk, StripAddress(p->ioBuffer), p->ioReqCount);
2020-06-22 04:10:31 +00:00
}
// Update count
p->ioActCount = p->ioReqCount;
2020-06-26 07:12:08 +00:00
d->dCtlPosition = offset + p->ioReqCount;
p->ioPosOffset = d->dCtlPosition;
2020-06-22 04:10:31 +00:00
return noErr;
} else if (cmd == aWrCmd) { // Write
2020-06-26 07:12:08 +00:00
// Fail if write protected or RAM disk buffer not set up
if (c->status.writeProt || !c->ramdisk) { return wPrErr; }
2020-06-22 04:10:31 +00:00
// Write from buffer into disk.
if (*MMU32bit) { BlockMove(p->ioBuffer, disk, p->ioReqCount); }
else {
((RDiskCopy_t)StripAddress(c->copy24))(
StripAddress(p->ioBuffer), disk, p->ioReqCount);
2020-06-22 04:10:31 +00:00
}
// Update count and position/offset
p->ioActCount = p->ioReqCount;
2020-06-26 07:12:08 +00:00
d->dCtlPosition = offset + p->ioReqCount;
p->ioPosOffset = d->dCtlPosition;
return noErr;
2020-06-22 04:10:31 +00:00
} else { return noErr; }
//FIXME: Should we fail if cmd isn't read or write?
}
#pragma parameter __D0 RDiskControl(__A0, __A1)
2020-06-26 07:12:08 +00:00
OSErr RDiskControl(CntrlParamPtr p, DCtlPtr d) {
2020-06-22 04:10:31 +00:00
RDiskStorage_t *c;
2020-06-26 07:12:08 +00:00
// Fail if dCtlStorage null
if (!d->dCtlStorage) { return controlErr; }
2020-06-22 04:10:31 +00:00
// Dereference dCtlStorage to get pointer to our context
c = *(RDiskStorage_t**)d->dCtlStorage;
// Handle control request based on csCode
2020-06-26 07:12:08 +00:00
switch (p->csCode) {
2020-07-02 07:06:35 +00:00
case 6: // Format not implemented
case 5: return controlErr; // Verify (after format) not implemented
case accRun:
// Disable accRun
d->dCtlFlags &= ~dNeedTimeMask;
// If drive present in drive queue, set drive online
if (!c->removed) {
c->status.diskInPlace = 8; // 8 means nonejectable disk
PostEvent(diskEvt, c->status.dQDrive);
}
return noErr;
case 21: case 22: // Get icon
2020-06-27 02:13:22 +00:00
*(Ptr*)&p->csParam = (Ptr)&RDiskIcon;
return noErr;
2020-07-02 07:06:35 +00:00
case 128:
c->postBoot = 1;
return noErr;
2020-06-22 04:10:31 +00:00
default: return controlErr;
}
}
#pragma parameter __D0 RDiskStatus(__A0, __A1)
2020-06-26 07:12:08 +00:00
OSErr RDiskStatus(CntrlParamPtr p, DCtlPtr d) {
2020-06-27 02:13:22 +00:00
RDiskStorage_t *c;
2020-06-26 07:12:08 +00:00
// Fail if dCtlStorage null
if (!d->dCtlStorage) { return statusErr; }
2020-06-27 02:13:22 +00:00
// Dereference dCtlStorage to get pointer to our context
c = *(RDiskStorage_t**)d->dCtlStorage;
2020-06-22 04:10:31 +00:00
// Handle status request based on csCode
2020-06-26 07:12:08 +00:00
switch (p->csCode) {
2020-06-22 04:10:31 +00:00
case drvStsCode:
2020-06-26 07:12:08 +00:00
BlockMove(*d->dCtlStorage, &p->csParam, sizeof(DrvSts2));
2020-06-22 04:10:31 +00:00
return noErr;
2020-07-02 07:06:35 +00:00
case 128: // Get size
*((long*)p->csParam) = RDiskSize;
return noErr;
2020-06-22 04:10:31 +00:00
default: return statusErr;
}
}
#pragma parameter __D0 RDiskClose(__A0, __A1)
2020-06-22 04:10:31 +00:00
OSErr RDiskClose(IOParamPtr p, DCtlPtr d) {
// If dCtlStorage not null, dispose of it and its contents
2020-06-26 07:12:08 +00:00
if (!d->dCtlStorage) { return noErr; }
RDiskStorage_t *c = *(RDiskStorage_t**)d->dCtlStorage;
if (c->copy24) { DisposePtr((Ptr)c->copy24); }
2020-06-26 07:12:08 +00:00
HUnlock(d->dCtlStorage);
DisposeHandle(d->dCtlStorage);
2020-06-22 04:10:31 +00:00
d->dCtlStorage = NULL;
return noErr;
}