--- nono/vm/scsidev.h 2026/04/29 17:04:39 1.1.1.4 +++ nono/vm/scsidev.h 2026/04/29 17:05:18 1.1.1.14 @@ -4,12 +4,9 @@ // Licensed under nono-license.txt // -// SCSI デバイス共通部分 - -#pragma once - -#include "mappedfile.h" -#include "scsibus.h" +// +// SCSI デバイス +// // IODevice (SPCDevice の親になるため) // | @@ -21,9 +18,15 @@ // SCSIHostDevice SCSITarget (SCSI ホスト/ターゲットとしての共通部分) // | | // v v -// SPCDevice SCSIHD (各デバイス、各ターゲット固有部分) +// SPCDevice SCSIDisk (各デバイス、各ターゲット固有部分) + +#pragma once -class SCSICmd; +#include "diskimage.h" +#include "message.h" +#include "monitor.h" +#include "scsibus.h" +#include "scsicmd.h" // // SCSI デバイスとしての共通部分。 @@ -31,9 +34,10 @@ class SCSICmd; // class SCSIDevice : public IODevice { + using inherited = IODevice; public: - SCSIDevice(); - ~SCSIDevice() override; + SCSIDevice(int objid_); + virtual ~SCSIDevice() override; // バスに接続 (SCSIBus::Attach から呼ばれる) void Attach(SCSIBus *bus_, int id) { @@ -41,6 +45,12 @@ class SCSIDevice : public IODevice myid = id; } + // SCSI ID を返す + int GetMyID() const { return myid; } + + // デバイス種別を返す (主にデバッグ表示用) + SCSI::DevType GetDevType() const { return devtype; } + // // 信号線制御 // (ソースコード的なショートカットのため) @@ -102,11 +112,14 @@ class SCSIDevice : public IODevice protected: // 自身の ID - uint myid; + uint myid {}; + + // デバイス種別 + SCSI::DevType devtype {}; // 自身が繋がっているバスへのポインタ。 // (イニシエータ自身も含めて) 所有していない。 - SCSIBus *bus; + SCSIBus *bus {}; }; // @@ -114,9 +127,10 @@ class SCSIDevice : public IODevice // class SCSIHostDevice : public SCSIDevice { + using inherited = SCSIDevice; public: - SCSIHostDevice(); - ~SCSIHostDevice() override; + SCSIHostDevice(int objid_); + virtual ~SCSIHostDevice() override; bool Create() override; bool Init() override; @@ -127,17 +141,27 @@ class SCSIHostDevice : public SCSIDevice // バスフリーになった (全員に通知される) void BusFreeCB(int id) override; - // ターゲットデバイス取得 (ROM エミュレーション用) + // ターゲットデバイス取得 (ROM エミュレーション/UI用) SCSITarget *GetTarget(int id) const { return target[id].get(); } + // 接続中のデバイスを ID 順に並べたリストを返す (UI ステータスパネル用) + const std::vector& GetConnectedDevices() const { + return connected_devices; + } + + // SCSIBus を取得 (UI からインジケータ表示用) + std::shared_ptr GetSCSIBus() const { return scsibus; } + protected: + DECLARE_MONITOR_CALLBACK(MonitorUpdateDevs); + // 設定ファイルに使われるこのホストデバイスを示す接頭語を設定する // (ここで設定項目を Add するので、継承クラスのコンストラクタから呼ぶこと) void SetConfigName(const char *name); // 設定ファイルに使われるこのホストデバイスを示す接頭語。 // ("spc0" とか) - const char *config_name = NULL; + const char *config_name {}; // SCSI バス (とターゲットオブジェクト) // @@ -159,13 +183,19 @@ class SCSIHostDevice : public SCSIDevice // ID7 ||<----> bus | | // || +----------+ | // || | - // || SCSIHD(SCSIDevice) | + // || SCSIDisk(SCSIDevice) | // || +----------+ | // ID0 ||<----> bus |<----------+ // || +----------+ own // - std::unique_ptr scsibus; - std::unique_ptr target[8]; + std::shared_ptr scsibus {}; + std::unique_ptr target[8] {}; + + // 接続中のデバイス(イニシエータ含む)を ID 順に並べたリスト (モニタ用) + // (継承クラスの Init() で初期化している) + std::vector connected_devices {}; + + Monitor monitor_devs { this }; }; // @@ -174,24 +204,28 @@ class SCSIHostDevice : public SCSIDevice class SCSITarget : public SCSIDevice { using inherited = SCSIDevice; - friend class SCSICmdModeSelect6; public: - SCSITarget(SCSIHostDevice *h, int id, std::vector& inq); - ~SCSITarget() override; + SCSITarget(SCSIHostDevice *h, int id); + virtual ~SCSITarget() override; - // デバイス種別を返す (主にデバッグ表示用) - SCSI::DevType GetDevType() const { return devtype; } + void ResetHard(bool poweron) override; - // Inquiry 応答データ - std::vector inquiry_data; + // Inquiry データを返す (Cmd から呼ばれる) + virtual bool Inquiry(std::vector& buf, int lun) = 0; // 現在のコマンド列を返す (モニタ用) std::vector GetCmdSeq() const { return cmdseq; } + // コマンドを選択する (ROM30 エミュレーション用) + SCSICmd *SelectCommand(const std::vector& cmdseq_); + // 論理ブロック長 uint32 GetBlocksize() const { return blocksize; } void SetBlocksize(uint32 val) { blocksize = val; } + // ModeSense コマンドのデバイス固有パラメータを返す + virtual uint8 GetDeviceSpecificParam() const { return 0; } + // センスキーと ASC/ASCQ uint8 sensekey {}; uint16 senseasc {}; @@ -220,66 +254,156 @@ class SCSITarget : public SCSIDevice void TransferAckCB() override; protected: - // デバイス種別 - SCSI::DevType devtype; + // SCSI コマンドのディスパッチャ + virtual void DispatchCmd(); + // ホスト - SCSIHostDevice *host; + SCSIHostDevice *host {}; // コマンド std::vector cmdseq {}; + // このコマンドの長さ。 // cmdseq の1バイト目で決まるやつのことで、受信目標バイト数。 // 今何バイト受信しているかは cmdseq.size()。 - int cmdlen; - - // SCSI コマンドのディスパッチャ - virtual void DispatchCmd(); + int cmdlen {}; // 現在実行中の SCSI コマンド - std::unique_ptr cmd; + std::unique_ptr cmd {}; // 現在のフェーズでの転送カウント。 // IN と OUT でカウント方向が違うことに注意。 - uint32 bytecount = 0; + uint32 bytecount {}; // 現在処理中のバイトが(各)フェーズの最終バイトなら true - bool lastbyte = false; + bool lastbyte {}; - // 論理ブロック長 (ModeSelect コマンドでホストから指定される) - // XXX: LUNA88K で ROM がたぶんやる。。。 - uint32 blocksize = 512; + // 論理ブロック長 + uint32 blocksize {}; }; // -// SCSI HD +// SCSI Disk (HD,CD,...) // -class SCSIHD : public SCSITarget +class SCSIDisk : public SCSITarget { using inherited = SCSITarget; - friend class SCSICmdRead6; - friend class SCSICmdWrite6; - friend class SCSICmdReadCapacity; - friend class SCSICmdRead10; public: - SCSIHD(SCSIHostDevice *h, int id); - ~SCSIHD() override; + // メディアへの書き込みモード + // + // Device Media Ignore + // RO (RO) * ReadOnly + // RW RO * WriteProtect + // RW RW no Writeable + // RW RW yes WriteIgnore + enum class RW { + ReadOnly = 2, // 読み込み専用デバイス + WriteProtect = 1, // 書き込み禁止メディア + Writeable = 0, // 書き込み可能 + WriteIgnore = -1, // 書き込み可能メディアで書き込みを無視 + }; + + public: + SCSIDisk(SCSIHostDevice *h, int id, SCSI::DevType type); + virtual ~SCSIDisk() override; bool Init() override; + void ResetHard(bool poweron) override; + void DispatchCmd() override; + bool Inquiry(std::vector& buf, int lun) override; + + // ModeSense コマンドのデバイス固有パラメータを返す + uint8 GetDeviceSpecificParam() const override; + + // buf にデータを読み込む。 + // 単位はセクタではなくバイトなことに注意。 + bool Read(std::vector& buf, uint64 start); - // このディスクをディスクイメージだと思うとして (いや実際そうだけど) + // buf のデータを書き込む。 + // 単位はセクタではなくバイトなことに注意。 + bool Write(const std::vector& buf, uint64 start); + + // このデバイスのバイトサイズ + off_t GetSize() const; + + // このデバイスをディスクイメージだと思うとして (いや実際そうだけど) // 先頭から start バイト目から len バイト読み出して返す。 // 単位はセクタではなくバイトなことに注意。 // (エミュレータ特権アクセス) - bool PeekImage(void *buf, uint32 start, uint32 len) const; + bool Peek(void *buf, uint64 start, uint32 len) const; + + // シーク時間[nsec] + uint64 GetSeekTime() const { return seektime; } + + // リムーバブルデバイスなら true + bool IsRemovableDevice() const { return removable_device; } + + // 書き込み可能デバイスなら true + bool IsWriteableDevice() const { return writeable_device; } + + // メディアが入っていれば true + bool IsMediumLoaded() const { return medium_loaded; } + + // メディアの書き込みモードを返す + RW GetWriteMode() const { return write_mode; } + // メディアの書き込みモードを文字列で返す (モニタ用) + const char *GetWriteModeStr() const; + + // メディア(ディスク)イメージを開く/閉じる。(同一スレッドから呼ぶこと) + bool LoadDisk(const std::string& path_); + void UnloadDisk(bool force = false); + + // メディア(ディスク)イメージを開く/閉じる。(UI スレッドから呼ぶ) + void LoadDiskUI(const std::string& path_); + void UnloadDiskUI(bool force); + + // メディアの取り出し可否 + bool IsMediumRemovalPrevented() const { return medium_removal_prevented; } + bool IsMediumRemovalAllowed() const { return !medium_removal_prevented; } + void PreventMediumRemoval(bool val); + + // イメージファイルのフルパスを返す + const std::string& GetPathName() const { return pathname; } + + // SCSIBus コールバック + + // バスリセットされた (全員に通知される) + void BusResetCB() override; private: - MappedFile file {}; - uint8 *mem {}; - bool writeprotect = false; + // ディスクイメージを閉じる (内部用) + bool UnloadDisk_internal(); + + // ディスク状態をクリアする + void Clear(); + + // メディア状態変更を UI に通知する + void MediaChanged() const; + + // メディア挿入/排出コールバック + void LoadCallback(MessageID, uint32); + void UnloadCallback(MessageID, uint32); + + // イメージファイルのフルパス + std::string pathname {}; + // 新しいイメージファイルのフルパス (UIから) + std::string new_pathname {}; + + // イメージファイル + DiskImage image {}; + + bool removable_device {}; // リムーバブルデバイスなら true + bool writeable_device {}; // 書き込み可能なデバイスなら true + bool write_ignore {}; // 書き込み無視オプション + bool medium_loaded {}; // メディアが挿入されていれば true + + // メディアの書き込みモード + RW write_mode {}; - // ディスクイメージ長を取得。 - off_t GetSize() const { return file.GetMemSize(); } + // メディアの取り出しを禁止するなら true。 + // XXX Bus Device Reset メッセージでも解除できるがメッセージは未実装 + bool medium_removal_prevented {}; - static std::vector inquiry_data; + uint64 seektime {}; };