// SPDX-License-Identifier: GPL-3.0-or-later #include #include typedef unsigned char byte; static byte packet[256]; static byte directory[256]; static byte listing, recordNumber; // Stock CoCo 3 BASIC shadows its ROM in RAM. Briefly expose the *live* // cartridge ROM, checking its SDC-DOS identifier as well as the MPI routes. // Merely writing a test value to $FF42 could send it to a Becker port; $FF43 // is writable RAM-disk data on another cartridge. Neither is a safe probe. static asm byte selectedSDC(void) { asm { program_end import ldx #program_end cmpx #$8000 bhi @no cmps #$8000 bhi @no ldd >$c000 cmpd #$444b ; Disk BASIC "DK" bne @no ldd >$e000 cmpd #$00e6 ; Super Extended BASIC ROM header bne @no ldb >$ff7f tfr b,a lsra lsra lsra lsra pshs b eora ,s+ bita #3 ; I/O and cartridge ROM routed to same slot bne @no sta >$ffde ; overlay external ROM at $c000-$fdff ldd >$e000 cmpd #$5344 ; "SDC-DOS " bne @restoreNo ldd >$e002 cmpd #$432d bne @restoreNo ldd >$e004 cmpd #$444f bne @restoreNo ldd >$e006 cmpd #$5320 bne @restoreNo sta >$ffdf ; restore all-RAM mapping ldb #1 rts @restoreNo sta >$ffdf @no clrb } } // BUSY may precede READY for a block. A device reporting an error or going // idle without the expected data must never leave the foreground wedged. static asm byte waitForBlock(void) { asm { ldx #$4000 @again ldb >$ff48 bmi @done bitb #2 bne @ok bitb #1 beq @missing leax -1,x bne @again ldb #3 ; SDC_TIMEOUT rts @missing ldb #$88 ; SDC_IO rts @ok clrb @done } } static asm byte waitForIdle(void) { asm { ldx #$4000 @again ldb >$ff48 bmi @done bitb #1 beq @ok leax -1,x bne @again ldb #3 ; SDC_TIMEOUT rts @ok clrb @done } } static asm void readBlock(byte *data) { asm { ldx 2,s lda #128 pshs a @pair ldd >$ff4a std ,x++ dec ,s bne @pair leas 1,s } } static asm void writeBlock(const byte *data) { asm { ldx 2,s lda #128 pshs a @pair ldd ,x++ std >$ff4a dec ,s bne @pair leas 1,s } } // Enter with the latch owned. Atkinson's CoCo SDC User Guide, chapter 7, // requires an idle controller before issue and a settling interval afterward. static byte issue(byte code, byte parameter, byte *block) { byte status = waitForIdle(); if (status != SDC_OK) return status; asm { lda :parameter sta >$ff49 lda :code sta >$ff48 ldb #10 ; at least 20 us on a stock fast CoCo 3 @settle decb bne @settle } if (block) { status = waitForBlock(); if (status != SDC_OK) return status; if ((code & 0xf0) == 0xe0) writeBlock(block); else readBlock(block); } status = waitForIdle(); if (status == SDC_OK && !block) { // The V command returns a nonzero revision in the data pair. asm { ldd >$ff4a bne @revision ldb #2 stb :status @revision } } return status; } // An entire command owns the controller since interrupts can use the floppy // latch. Save CC before probing and release the latch on every exit path. // Only command $E0 (L: or M:) writes a packet. No raw-sector writes occur. static byte command(byte code, byte parameter, byte *block) { byte savedCC, status; asm { tfr cc,b orcc #$50 stb :savedCC } status = SDC_UNAVAILABLE; if (selectedSDC()) { asm { lda #$43 sta >$ff40 } status = issue(code, parameter, block); asm { clr >$ff40 } } asm { ldb :savedCC tfr b,cc } return status; } byte sdc_probe(void) { return command(0xc0, 'V', NULL); } static byte preparePacket(byte operation, const char *path) { unsigned i; if (!path || !*path) return SDC_INVALID; for (i = 0; path[i]; ++i) { byte c = path[i]; if (i == 253 || c < ' ' || c > '~' || c == '\\' || c == ':') return SDC_INVALID; } memset(packet, 0, sizeof(packet)); packet[0] = operation; packet[1] = ':'; memcpy(packet + 2, path, i); return SDC_OK; } static byte startListing(void) { byte status = command(0xe0, 0, packet); listing = 0; if (status == SDC_OK) { recordNumber = 16; // Fetch the first page on demand. listing = 1; } return status; } byte sdc_list_begin(const char *pattern) { byte status = preparePacket('L', pattern && *pattern ? pattern : "*"); if (status != SDC_OK) { listing = 0; return status; } return startListing(); } static void decodeName(SdcEntry *entry, const byte *record) { byte i, n = 0; for (i = 0; i < 8 && record[i] != ' '; ++i) entry->name[n++] = record[i]; if (record[8] != ' ') { entry->name[n++] = '.'; for (i = 8; i < 11 && record[i] != ' '; ++i) entry->name[n++] = record[i]; } entry->name[n] = 0; entry->attributes = record[11]; } byte sdc_list_next(SdcEntry *entry) { const byte *record; byte status; if (!entry) return SDC_INVALID; if (!listing) return SDC_END; if (recordNumber == 16) { status = command(0xc0, '>', directory); if (status != SDC_OK) { listing = 0; return status; } recordNumber = 0; } record = directory + ((unsigned)recordNumber << 4); if (!record[0]) { listing = 0; return SDC_END; } ++recordNumber; decodeName(entry, record); memcpy(&entry->size, record + 12, 4); // Listing size is big-endian like the CPU. return SDC_OK; } byte sdc_mounted(byte drive, SdcEntry *entry) { byte status, i; if (drive > 1 || !entry) return SDC_INVALID; status = command(0xc0 | drive, 'I', packet); if (status != SDC_OK) return status; decodeName(entry, packet); // Mounted-image information uses the FAT directory's little-endian size. for (i = 0; i < 4; ++i) ((byte *)&entry->size)[i] = packet[31 - i]; return SDC_OK; } // M: also accepts wildcards, implicit extensions, directories and disk sets. // Require an explicit 8.3 image filename so a typo cannot select a different // first match. Only directories may be preceded by "." or ".." components. static byte exactFilename(const char *path) { const char *base = path; const char *p; byte len = 0, ext = 0; for (p = path; *p; ++p) { if (*p == '*' || *p == '?') return 0; if (*p == '/') base = p + 1; } for (p = base; *p; ++p) { if (*p == '.') { if (!len || ext) return 0; ext = 1; len = 0; } else { if (*p <= ' ' || strchr("\"+,;<=>[]|", *p) || ++len > (ext ? 3 : 8)) return 0; } } return ext && len; } byte sdc_mount(byte drive, const char *path) { byte status; byte *control; SdcEntry entry; if (drive > 1) return SDC_INVALID; status = preparePacket('L', path); if (status != SDC_OK || !exactFilename(path)) return SDC_INVALID; control = (byte *)0x0800 + (unsigned)drive * 74; if (control[0] || control[1]) // Disk BASIC has open files or a pending FAT. return SDC_IN_USE; // The native M: command interprets even a folder named "X.DSK" as a // numbered disk set. Look up the exact card entry before authorizing M:. status = startListing(); if (status != SDC_OK) return status; status = sdc_list_next(&entry); listing = 0; if (status == SDC_END) return SDC_NOT_FOUND; if (status != SDC_OK) return status; if (entry.attributes & SDC_DIRECTORY) return SDC_INVALID; // The search used the same path; only the operation byte differs. packet[0] = 'M'; return command(0xe0 | drive, 0, packet); }