ROMBUSDriver/rombus.c

207 lines
6.4 KiB
C
Raw Normal View History

2021-08-07 05:01:54 +00:00
#include <Memory.h>
#include <Devices.h>
#include <Files.h>
#include <Disks.h>
#include <Errors.h>
#include <Events.h>
#include <OSUtils.h>
2022-04-19 10:11:35 +00:00
#include <Quickdraw.h>
2021-08-07 05:01:54 +00:00
2021-08-07 17:20:49 +00:00
#include "rombus.h"
2022-04-19 10:11:35 +00:00
#include "rdisk.h"
#include "sdisk.h"
2021-08-07 05:01:54 +00:00
// Decode keyboard/PRAM settings
2022-04-19 10:11:35 +00:00
static void RBDecodeSettings(Ptr unmountROM, Ptr unmountSD, Ptr mountSD) {
2021-08-07 17:20:49 +00:00
// Sample R, S, X keys repeatedly
char r = 0, s = 0, x = 0;
2021-08-07 05:01:54 +00:00
long tmax = TickCount() + 60;
for (long i = 0; i < 1000000; i++) {
2021-08-07 17:20:49 +00:00
r |= RBIsRPressed();
s |= RBIsSPressed();
x |= RBIsXPressed();
if (r || s || x) { break; }
2021-08-07 05:01:54 +00:00
if (TickCount() > tmax) { break; }
}
// Read PRAM
2021-08-07 05:27:46 +00:00
char legacy_startup;
2021-08-07 17:20:49 +00:00
RBReadXPRAM(1, 4, &legacy_startup);
2021-08-07 05:01:54 +00:00
// Decode settings: unmount (don't boot), mount (after boot), RAM disk
2022-04-19 10:11:35 +00:00
if (x) { // X disables SD
*unmountROM = 1; // Unmount ROM disk volume
*unmountSD = 1; // Unmount SD volume
*mountSD = 0; // Don't mount SD later
2021-08-07 17:20:49 +00:00
} else if (r) { // R boots from ROM disk and mount SD later
2022-04-19 10:11:35 +00:00
*unmountROM = 0; // Don't unmount so we boot from ROM
*unmountSD = 1; // Unmount SD volume so we don't boot from it
*mountSD = 1; // Mount SD later
2021-08-07 17:20:49 +00:00
} else if (s) { // S boots from SD
2022-04-19 10:11:35 +00:00
*unmountROM = 1; // Unmount ROM disk volume so we don't boot from it
*unmountSD = 0; // Don't unmount so we boot from SD
*mountSD = 0; // No need to mount SD later since already mounted
2021-08-07 17:20:49 +00:00
} else if (legacy_startup & 0x04) { // Boot from SD
2022-04-19 10:11:35 +00:00
*unmountROM = 1; // Unmount ROM disk volume so we don't boot from it
*unmountSD = 0; // Don't unmount so we boot from SD
*mountSD = 0; // No need to mount later since already mounted
2021-08-07 05:01:54 +00:00
} else {
2022-04-19 10:11:35 +00:00
*unmountROM = !(legacy_startup & 0x01); // Unmount ROM disk if saved in PRAM
*unmountSD = 1; // Unmount SD volume so we don't boot from it
*mountSD = !(legacy_startup & 0x08); // Mount SD later if setting
2021-08-07 05:01:54 +00:00
}
}
// Switch to 32-bit mode and copy
#pragma parameter C24(__A0, __A1, __D0)
void __attribute__ ((noinline)) C24(Ptr sourcePtr, Ptr destPtr, unsigned long byteCount) {
signed char mode = true32b;
SwapMMUMode(&mode);
BlockMove(sourcePtr, destPtr, byteCount);
SwapMMUMode(&mode);
}
// Figure out the first available drive number >= 5
2021-08-07 17:20:49 +00:00
static int RBFindDrvNum() {
2021-08-07 05:01:54 +00:00
DrvQElPtr dq;
int drvNum = 5;
for (dq = (DrvQElPtr)(GetDrvQHdr())->qHead; dq; dq = (DrvQElPtr)dq->qLink) {
if (dq->dQDrive >= drvNum) { drvNum = dq->dQDrive + 1; }
}
return drvNum;
}
2021-08-07 17:20:49 +00:00
#pragma parameter __D0 RBOpen(__A0, __A1)
OSErr RBOpen(IOParamPtr p, DCtlPtr d) {
2021-08-07 05:01:54 +00:00
int drvNum;
2021-08-07 17:20:49 +00:00
RBStorage_t *c;
2021-08-07 05:01:54 +00:00
char legacy_startup;
2022-04-19 10:11:35 +00:00
char *src, *dst;
OSErr ret;
2021-08-07 05:01:54 +00:00
// Do nothing if already opened
if (d->dCtlStorage) { return noErr; }
// Do nothing if inhibited
2021-08-07 17:20:49 +00:00
RBReadXPRAM(1, 4, &legacy_startup);
2021-08-07 05:01:54 +00:00
if (legacy_startup & 0x80) { return noErr; }
// Allocate storage struct
2021-08-07 17:20:49 +00:00
d->dCtlStorage = NewHandleSysClear(sizeof(RBStorage_t));
2021-08-07 05:01:54 +00:00
if (!d->dCtlStorage) { return openErr; }
// Lock our storage struct and get master pointer
HLock(d->dCtlStorage);
2021-08-07 17:20:49 +00:00
c = *(RBStorage_t**)d->dCtlStorage;
2021-08-07 05:01:54 +00:00
2022-04-19 10:11:35 +00:00
// Populate both drives' DrvSts and add each to drive queue
if (ret = RDOpen(p, d ,c) != noErr) { return ret; }
if (ret = SDOpen(p, d ,c) != noErr) { return ret; }
2021-08-07 05:01:54 +00:00
return noErr;
}
// Init is called at beginning of first prime (read/write) call
2021-08-07 17:20:49 +00:00
static void RBInit(IOParamPtr p, DCtlPtr d, RBStorage_t *c) {
2022-04-19 10:11:35 +00:00
char unmountROM, unmountSD;
2021-08-07 05:01:54 +00:00
// Mark init done
c->initialized = 1;
// Decode settings
2022-04-19 10:11:35 +00:00
RBDecodeSettings(&unmountROM, &c->mountROM, &unmountSD, &c->mountSD);
2021-08-07 05:01:54 +00:00
2022-04-19 10:11:35 +00:00
// Unmount if requested
if (unmountROM) { c->rdStatus.diskInPlace = 0; }
if (unmountSD) { c->sdStatus.diskInPlace = 0; }
2021-08-07 05:01:54 +00:00
// If mount enabled, enable accRun to post disk inserted event later
2022-04-19 10:11:35 +00:00
if (c->mountROM || c->mountSD) {
2021-08-07 05:01:54 +00:00
d->dCtlDelay = 150; // Set accRun delay (150 ticks is 2.5 sec.)
d->dCtlFlags |= dNeedTimeMask; // Enable accRun
}
}
2021-08-07 17:20:49 +00:00
#pragma parameter __D0 RBPrime(__A0, __A1)
OSErr RBPrime(IOParamPtr p, DCtlPtr d) {
RBStorage_t *c;
2021-08-07 05:01:54 +00:00
char cmd;
Ptr disk;
// Return disk offline error if dCtlStorage null
if (!d->dCtlStorage) { return notOpenErr; }
// Dereference dCtlStorage to get pointer to our context
2021-08-07 17:20:49 +00:00
c = *(RBStorage_t**)d->dCtlStorage;
2021-08-07 05:01:54 +00:00
// Initialize if this is the first prime call
2021-08-07 17:20:49 +00:00
if (!c->initialized) { RBInit(p, d, c); }
2021-08-07 05:01:54 +00:00
2022-04-19 10:11:35 +00:00
switch (p->ioVRefNum) {
case c->rdStatus.dQDrive: return RDPrime(p, d, c);
case c->sdStatus.dQDrive: return SDPrime(p, d, c);
default: return statusErr;
}
2021-08-07 05:01:54 +00:00
}
2021-08-07 17:20:49 +00:00
#pragma parameter __D0 RBCtl(__A0, __A1)
OSErr RBCtl(CntrlParamPtr p, DCtlPtr d) {
RBStorage_t *c;
2021-08-07 05:01:54 +00:00
// Fail if dCtlStorage null
if (!d->dCtlStorage) { return notOpenErr; }
// Dereference dCtlStorage to get pointer to our context
2021-08-07 17:20:49 +00:00
c = *(RBStorage_t**)d->dCtlStorage;
2022-04-19 10:11:35 +00:00
// Handle control request csCodes common to both volumes
2021-08-07 05:01:54 +00:00
switch (p->csCode) {
2021-08-07 05:27:46 +00:00
case killCode: return noErr;
2021-08-07 05:01:54 +00:00
case accRun:
2022-04-19 10:11:35 +00:00
if (c->mountSD) { // Request to mount SD disk
c->sdStatus.diskInPlace = 8; // 8 is nonejectable disk
PostEvent(diskEvt, c->sdStatus.dQDrive); // Post disk inserted event
return noErr;
} else if (c->mountROM) { // Request to mount ROM disk
c->rdStatus.diskInPlace = 8; // 8 is nonejectable disk
PostEvent(diskEvt, c->rdStatus.dQDrive); // Post disk inserted event
return noErr;
} else { // Nothing to mount
d->dCtlFlags &= ~dNeedTimeMask; // Disable accRun
return noErr;
}
2021-08-07 05:01:54 +00:00
case 2351: // Post-boot
c->initialized = 1; // Skip initialization
d->dCtlDelay = 30; // Set accRun delay (30 ticks is 0.5 sec.)
d->dCtlFlags |= dNeedTimeMask; // Enable accRun
return noErr;
2022-04-19 10:11:35 +00:00
}
// Dispatch based on volume reference number
switch (p->ioVRefNum) {
case c->rdStatus.dQDrive: return RDCtl(p, d, c);
case c->sdStatus.dQDrive: return SDCtl(p, d, c);
2021-08-07 05:01:54 +00:00
default: return controlErr;
}
}
2021-08-07 17:20:49 +00:00
#pragma parameter __D0 RBStat(__A0, __A1)
OSErr RBStat(CntrlParamPtr p, DCtlPtr d) {
RBStorage_t *c;
2021-08-07 05:01:54 +00:00
// Fail if dCtlStorage null
if (!d->dCtlStorage) { return notOpenErr; }
// Dereference dCtlStorage to get pointer to our context
2021-08-07 17:20:49 +00:00
c = *(RBStorage_t**)d->dCtlStorage;
2022-04-19 10:11:35 +00:00
// Dispatch based on volume reference number
switch (p->ioVRefNum) {
case c->rdStatus.dQDrive: return RDStat(p, d, c);
case c->sdStatus.dQDrive: return SDStat(p, d, c);
2021-08-07 05:01:54 +00:00
default: return statusErr;
}
}
2021-08-07 17:20:49 +00:00
#pragma parameter __D0 RBClose(__A0, __A1)
OSErr RBClose(IOParamPtr p, DCtlPtr d) {
2021-08-07 05:01:54 +00:00
// If dCtlStorage not null, dispose of it
if (!d->dCtlStorage) { return noErr; }
2022-04-19 10:11:35 +00:00
RBStorage_t *c = *(RBStorage_t**)d->dCtlStorage;
RDClose(p, d, c);
SDClose(p, d, c);
2021-08-07 05:01:54 +00:00
HUnlock(d->dCtlStorage);
DisposeHandle(d->dCtlStorage);
d->dCtlStorage = NULL;
return noErr;
}