瀏覽代碼

Merge pull request #52 from BlueSCSI/eric/implicitImgDir

Implicit ImgDir
Eric Helgeson 2 年之前
父節點
當前提交
43221ad032
共有 5 個文件被更改,包括 118 次插入71 次删除
  1. 1 0
      .gitignore
  2. 13 13
      src/BlueSCSI_cdrom.cpp
  3. 64 41
      src/BlueSCSI_config.cpp
  4. 5 1
      src/BlueSCSI_config.h
  5. 35 16
      src/BlueSCSI_disk.cpp

+ 1 - 0
.gitignore

@@ -8,3 +8,4 @@ dist/
 .vscode/launch.json
 .vscode/ipch
 .vscode/extensions.json
+cc-nc-hardware/*/*-backups/

+ 13 - 13
src/BlueSCSI_cdrom.cpp

@@ -749,7 +749,7 @@ void doReadHeader(bool MSF, uint32_t lba, uint16_t allocationLength)
 
 #if ENABLE_AUDIO_OUTPUT
     // terminate audio playback if active on this target (Annex C)
-    audio_stop(img.scsiId & 7);
+    audio_stop(img.scsiId & S2S_CFG_TARGET_ID_BITS);
 #endif
 
     uint8_t mode = 1;
@@ -1154,7 +1154,7 @@ void cdromCloseTray(image_config_t &img)
 {
     if (img.ejected)
     {
-        uint8_t target = img.scsiId & 7;
+        uint8_t target = img.scsiId & S2S_CFG_TARGET_ID_BITS;
         debuglog("------ CDROM close tray on ID ", (int)target);
         img.ejected = false;
         img.cdrom_events = 2; // New media
@@ -1165,7 +1165,7 @@ void cdromCloseTray(image_config_t &img)
 // Switch image on ejection.
 void cdromPerformEject(image_config_t &img)
 {
-    uint8_t target = img.scsiId & 7;
+    uint8_t target = img.scsiId & S2S_CFG_TARGET_ID_BITS;
 #if ENABLE_AUDIO_OUTPUT
     // terminate audio playback if active on this target (MMC-1 Annex C)
     audio_stop(target);
@@ -1189,7 +1189,7 @@ void cdromReinsertFirstImage(image_config_t &img)
     if (img.image_index > 0)
     {
         // Multiple images for this drive, force restart from first one
-        uint8_t target = img.scsiId & 7;
+        uint8_t target = img.scsiId & S2S_CFG_TARGET_ID_BITS;
         debuglog("---- Restarting from first CD-ROM image for ID ", (int)target);
         img.image_index = -1;
         img.current_image[0] = '\0';
@@ -1207,7 +1207,7 @@ bool cdromSwitchNextImage(image_config_t &img)
 {
     // Check if we have a next image to load, so that drive is closed next time the host asks.
     char filename[MAX_FILE_PATH];
-    int target_idx = img.scsiId & 7;
+    int target_idx = img.scsiId & S2S_CFG_TARGET_ID_BITS;
     scsiDiskGetNextImageName(img, filename, sizeof(filename));
 
 #ifdef ENABLE_AUDIO_OUTPUT
@@ -1219,7 +1219,7 @@ bool cdromSwitchNextImage(image_config_t &img)
 
     if (filename[0] != '\0')
     {
-        log("Switching to next CD-ROM image for ", target_idx, ": ", filename);
+        log("Switching to next CD-ROM image for SCSI ID: ", target_idx, ": ", filename);
         img.file.close();
         bool status = scsiDiskOpenHDDImage(target_idx, filename, target_idx, 0,
                                            getBlockSize(filename, target_idx, 2048));
@@ -1287,7 +1287,7 @@ void cdromGetAudioPlaybackStatus(uint8_t *status, uint32_t *current_lba, bool cu
 
 #ifdef ENABLE_AUDIO_OUTPUT
     if (status) {
-        uint8_t target = img.scsiId & 7;
+        uint8_t target = img.scsiId & S2S_CFG_TARGET_ID_BITS;
         if (current_only) {
             *status = audio_is_playing(target) ? 1 : 0;
         } else {
@@ -1312,12 +1312,12 @@ static void doPlayAudio(uint32_t lba, uint32_t length)
 #ifdef ENABLE_AUDIO_OUTPUT
     debuglog("------ CD-ROM Play Audio request at ", lba, " for ", length, " sectors");
     image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
-    uint8_t target_id = img.scsiId & 7;
+    uint8_t target_id = img.scsiId & S2S_CFG_TARGET_ID_BITS;
 
     // Per Annex C terminate playback immediately if already in progress on
     // the current target. Non-current targets may also get their audio
     // interrupted later due to hardware limitations
-    audio_stop(img.scsiId & 7);
+    audio_stop(target_id);
 
     // if transfer length is zero no audio playback happens.
     // don't treat as an error per SCSI-2; handle via short-circuit
@@ -1398,7 +1398,7 @@ static void doPauseResumeAudio(bool resume)
 #ifdef ENABLE_AUDIO_OUTPUT
     log("------ CD-ROM ", resume ? "resume" : "pause", " audio playback");
     image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
-    uint8_t target_id = img.scsiId & 7;
+    uint8_t target_id = img.scsiId & S2S_CFG_TARGET_ID_BITS;
 
     if (audio_is_playing(target_id))
     {
@@ -1427,7 +1427,7 @@ static void doStopAudio()
     debuglog("------ CD-ROM Stop Audio request");
 #ifdef ENABLE_AUDIO_OUTPUT
     image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
-    uint8_t target_id = img.scsiId & 7;
+    uint8_t target_id = img.scsiId & S2S_CFG_TARGET_ID_BITS;
     audio_stop(target_id);
 #endif
 }
@@ -1467,7 +1467,7 @@ static void doReadCD(uint32_t lba, uint32_t length, uint8_t sector_type,
 
 #if ENABLE_AUDIO_OUTPUT
     // terminate audio playback if active on this target (Annex C)
-    audio_stop(img.scsiId & 7);
+    audio_stop(img.scsiId & S2S_CFG_TARGET_ID_BITS);
 #endif
 
     CUEParser parser;
@@ -1849,7 +1849,7 @@ extern "C" int scsiCDRomCommand()
     {
 #if ENABLE_AUDIO_OUTPUT
         // terminate audio playback if active on this target (MMC-1 Annex C)
-        audio_stop(img.scsiId & 7);
+        audio_stop(img.scsiId & S2S_CFG_TARGET_ID_BITS);
 #endif
         if ((scsiDev.cdb[4] & 2))
         {

+ 64 - 41
src/BlueSCSI_config.cpp

@@ -1,41 +1,64 @@
-/** 
- * Copyright (C) 2023 Eric Helgeson
- * 
- * This file is part of BlueSCSI
- * 
- * This program 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 3 of the License, or
- * (at your option) any later version. 
- * 
- * This program 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 this program.  If not, see <https://www.gnu.org/licenses/>.
-**/
-
-#include "minIni.h"
-#include "BlueSCSI_config.h"
-
-int getBlockSize(char *filename, int scsiId, int default_size)
-{
-  char section[6] = "SCSI0";
-  section[4] = '0' + scsiId;
-
-  default_size = ini_getl(section, "BlockSize", default_size, CONFIGFILE);
-  // Parse block size (HD00_NNNN)
-  const char *blksize = strchr(filename, '_');
-  if (blksize)
-  {
-    int blktmp = strtoul(blksize + 1, NULL, 10);
-    if (blktmp == 256 || blktmp == 512 || blktmp == 1024 ||
-        blktmp == 2048 || blktmp == 4096 || blktmp == 8192)
-    {
-      return blktmp;
-    }
-  }
-  return default_size;
-}
+/** 
+ * Copyright (C) 2023 Eric Helgeson
+ * 
+ * This file is part of BlueSCSI
+ * 
+ * This program 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 3 of the License, or
+ * (at your option) any later version. 
+ * 
+ * This program 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 this program.  If not, see <https://www.gnu.org/licenses/>.
+**/
+
+#include "minIni.h"
+#include "BlueSCSI_config.h"
+
+int getBlockSize(char *filename, int scsiId, int default_size)
+{
+  char section[6] = "SCSI0";
+  section[4] = '0' + scsiId;
+
+  default_size = ini_getl(section, "BlockSize", default_size, CONFIGFILE);
+  // Parse block size (HD00_NNNN)
+  const char *blksize = strchr(filename, '_');
+  if (blksize)
+  {
+    int blktmp = strtoul(blksize + 1, NULL, 10);
+    if (blktmp == 256 || blktmp == 512 || blktmp == 1024 ||
+        blktmp == 2048 || blktmp == 4096 || blktmp == 8192)
+    {
+      return blktmp;
+    }
+  }
+  return default_size;
+}
+
+int getImgDir(int scsiId, char* dirname)
+{
+  char section[6] = "SCSI0";
+  section[4] = '0' + scsiId;
+
+  char key[] = "ImgDir";
+  int dirlen = ini_gets(section, key, "", dirname, sizeof(dirname), CONFIGFILE);
+  return dirlen;
+}
+
+
+int getImg(int scsiId, int img_index, char* filename)
+{
+  char section[6] = "SCSI0";
+  section[4] = '0' + scsiId;
+
+  char key[] = "IMG0";
+  key[3] = '0' + img_index;
+
+  int dirlen = ini_gets(section, key, "", filename, sizeof(filename), CONFIGFILE);
+  return dirlen;
+}

+ 5 - 1
src/BlueSCSI_config.h

@@ -80,4 +80,8 @@
  * @scsiId - ID of the device we're looking to get the block size for
  * @default_size - if block size cant be determined use this value
 */
-int getBlockSize(char *filename, int scsiId, int default_size);
+int getBlockSize(char *filename, int scsiId, int default_size);
+
+int getImgDir(int scsiId, char* dirname);
+
+int getImg(int scsiId, int img_index, char* filename);

+ 35 - 16
src/BlueSCSI_disk.cpp

@@ -545,11 +545,30 @@ static void scsiDiskLoadConfig(int target_idx, const char *section)
     if (strlen(section) == 5 && strncmp(section, "SCSI", 4) == 0) // allow within target [SCSIx] blocks only
     {
         ini_gets(section, "ImgDir", "", tmp, sizeof(tmp), CONFIGFILE);
+        getImgDir(target_idx, tmp);
         if (tmp[0])
         {
             log("-- SCSI", target_idx, " using image directory \'", tmp, "'");
             img.image_directory = true;
         }
+        else
+        {
+            strcpy(tmp, "CDX");
+            tmp[2] = '0' + target_idx;
+            if(SD.exists(tmp))
+            {
+                log("-- SCSI ID: ", target_idx, " using Optical image directory \'", tmp, "'");
+                img.deviceType = S2S_CFG_OPTICAL;
+                img.image_directory = true;
+            }
+            strcpy(tmp, "HDX");
+            tmp[2] = '0' + target_idx;
+            if(SD.exists(tmp))
+            {
+                log("-- SCSI ID: ", target_idx, " using Drive image directory \'", tmp, "'");
+                img.image_directory = true;
+            }
+        }
     }
 }
 
@@ -563,7 +582,7 @@ static int findNextImageAfter(image_config_t &img,
     FsFile dir;
     if (dirname[0] == '\0')
     {
-        log("Image directory name invalid for ID", (img.scsiId & 7));
+        log("Image directory name invalid for ID", (img.scsiId & S2S_CFG_TARGET_ID_BITS));
         return 0;
     }
     if (!dir.open(dirname))
@@ -631,10 +650,7 @@ static int findNextImageAfter(image_config_t &img,
 
 int scsiDiskGetNextImageName(image_config_t &img, char *buf, size_t buflen)
 {
-    int target_idx = img.scsiId & 7;
-
-    char section[6] = "SCSI0";
-    section[4] = '0' + target_idx;
+    int target_idx = img.scsiId & S2S_CFG_TARGET_ID_BITS;
 
     // sanity check: is provided buffer is long enough to store a filename?
     assert(buflen >= MAX_FILE_PATH);
@@ -643,14 +659,20 @@ int scsiDiskGetNextImageName(image_config_t &img, char *buf, size_t buflen)
     {
         // image directory was found during startup
         char dirname[MAX_FILE_PATH];
-        char key[] = "ImgDir";
-        int dirlen = ini_gets(section, key, "", dirname, sizeof(dirname), CONFIGFILE);
+        int dirlen = getImgDir(target_idx, dirname);
         if (!dirlen)
         {
-            // If image_directory set but ImageDir is not, could be used to
-            // indicate an image directory configured via folder structure.
-            // Not implemented, so treat this as equivalent to missing ImageDir
-            return 0;
+            // If image_directory set but ImgDir is not look for an well known ImgDir
+            if(img.deviceType == S2S_CFG_OPTICAL)
+                strcpy(dirname, "CDX");
+            else
+                strcpy(dirname, "HDX");
+            dirname[2] = '0' + target_idx;
+            if(!SD.exists(dirname))
+            {
+                debuglog("ERROR: Looking for ", dirname, " to load images, but was not found.");
+                return 0;
+            }
         }
 
         // find the next filename
@@ -684,10 +706,7 @@ int scsiDiskGetNextImageName(image_config_t &img, char *buf, size_t buflen)
             img.image_index = 0;
         }
 
-        char key[5] = "IMG0";
-        key[3] = '0' + img.image_index;
-
-        int ret = ini_gets(section, key, "", buf, buflen, CONFIGFILE);
+        int ret = getImg(target_idx, img.image_index, buf);
         if (buf[0] != '\0')
         {
             return ret;
@@ -729,7 +748,7 @@ void scsiDiskLoadConfig(int target_idx)
     if (scsiDiskGetNextImageName(img, filename, sizeof(filename)))
     {
         int blocksize = getBlockSize(filename, target_idx, (img.deviceType == S2S_CFG_OPTICAL) ? 2048 : 512);
-        log("-- Opening '", filename, "' for id:", target_idx, ", specified in " CONFIGFILE);
+        log("-- Opening '", filename, "' for ID:", target_idx);
         scsiDiskOpenHDDImage(target_idx, filename, target_idx, 0, blocksize);
     }
 }