--- nono/vm/scsidev.cpp 2026/04/29 17:05:03 1.1.1.12 +++ nono/vm/scsidev.cpp 2026/04/29 17:05:11 1.1.1.14 @@ -4,6 +4,10 @@ // Licensed under nono-license.txt // +// +// SCSI デバイス +// + // ログレベルは // 1: SCSI コマンドの概要。 // デバイスの動作のうち大きな変更のもの @@ -16,6 +20,7 @@ #include "scsidev.h" #include "config.h" #include "mainapp.h" +#include "scheduler.h" #include "uimessage.h" // @@ -33,6 +38,7 @@ SCSIDevice::~SCSIDevice() { } + // // SCSI ホストデバイス // @@ -43,7 +49,7 @@ SCSIHostDevice::SCSIHostDevice(const std { devtype = SCSI::DevType::Initiator; - monitor_devs.func = (MonitorCallback_t)&SCSIHostDevice::MonitorUpdateDevs; + monitor_devs.func = ToMonitorCallback(&SCSIHostDevice::MonitorUpdateDevs); // サイズは Create で決まる monitor_devs.Regist(ID_MONITOR_SCSIDEVS); @@ -135,7 +141,7 @@ SCSIHostDevice::Init() } // connected_devices によってモニタの大きさを決定。 - monitor_devs.SetSize(80, (connected_devices.size() - 1) * 4 + 1); + monitor_devs.SetSize(75, (connected_devices.size() - 1) * 4 + 1); return true; } @@ -175,9 +181,14 @@ SCSIHostDevice::BusFreeCB(int id) void SCSIHostDevice::MonitorUpdateDevs(Monitor *, TextScreen& screen) { - int x; int y; +// 012345678901234567890123456789012345678901234567890123456789012345678901234 +// ID6: HD(2048MB) +// MediumLoaded Off LogicalBlock 2048 ImageSize 2,147,483,648 +// RemovalPrevent Off WriteProtected + + screen.Clear(); y = 0; @@ -212,58 +223,32 @@ SCSIHostDevice::MonitorUpdateDevs(Monito continue; } - // 1列目 - x = 5; - screen.Print(x, y, + const int x1 = 5; + const int x2 = 29; + const int x3 = 50; + + // 1行目 + screen.Print(x1, y, (disk->IsRemovableDevice() ? TA::Normal : TA::Disable), "MediumLoaded %s", disk->IsMediumLoaded() ? "On" : "Off"); - screen.Print(x, y + 1, + screen.Print(x2, y, "LogicalBlock %d", disk->GetBlocksize()); + std::string imagesize = format_number(disk->GetSize()); + screen.Print(x3, y, "ImageSize %13s", imagesize.c_str()); + y++; + + // 2行目 + screen.Print(x1, y, (disk->IsRemovableDevice() ? TA::Normal : TA::Disable), "RemovalPrevent %s", disk->IsMediumRemovalPrevented() ? "On" : "Off"); - // 2列目 - x = 29; - const char *wstr; - switch (disk->GetWriteMode()) { - case SCSIDisk::RW::ReadOnly: wstr = "ReadOnly"; break; - case SCSIDisk::RW::WriteProtect: wstr = "WriteProtected"; break; - case SCSIDisk::RW::Writeable: wstr = "Writeable"; break; - case SCSIDisk::RW::WriteIgnore: wstr = "WriteIgnored"; break; - default: __unreachable(); - } - screen.Puts(x, y, - (disk->IsMediumLoaded() ? TA::Normal : TA::Disable), wstr); - - // 3列目 - x = 50; - uint64 v = disk->GetSize(); - std::string bytes; - if (v < 1000) { - bytes = string_format("%u", (uint32)v); - } else if (v < 1000 * 1000) { - bytes = string_format("%u,%03u", - (uint)(v / 1000), - (uint)(v % 1000)); - } else if (v < 1000 * 1000 * 1000) { - bytes = string_format("%u,%03u,%03u", - (uint)(v / 1000 / 1000), - (uint)((v / 1000) % 1000), - (uint)(v % 1000)); - } else if (v < 1000ULL * 1000 * 1000 * 1000) { - bytes = string_format("%u,%03u,%03u,%03u", - (uint)(v / 1000 / 1000 / 1000), - (uint)((v / 1000 / 1000) % 1000), - (uint)((v / 1000) % 1000), - (uint)(v % 1000)); - } else { - bytes = string_format("%ju", (uintmax_t)v); - } - screen.Print(x, y, "TotalBytes %s", bytes.c_str()); - screen.Print(x, y + 1, "LogicalBlock %d", disk->GetBlocksize()); + screen.Puts(x2, y, + (disk->IsMediumLoaded() ? TA::Normal : TA::Disable), + disk->GetWriteModeStr()); + y++; - y += 3; + y++; } } @@ -287,17 +272,17 @@ SCSITarget::~SCSITarget() } // 電源オン -bool -SCSITarget::PowerOn() +void +SCSITarget::ResetHard(bool poweron) { - ClearSense(); - cmdseq.clear(); - cmdlen = 0; - cmd.reset(); - bytecount = 0; - lastbyte = false; - - return true; + if (poweron) { + ClearSense(); + cmdseq.clear(); + cmdlen = 0; + cmd.reset(); + bytecount = 0; + lastbyte = false; + } } // バスリセットされた (SCSIBus から呼ばれる) @@ -642,11 +627,6 @@ SCSIDisk::SCSIDisk(SCSIHostDevice *h, in __unreachable(); } - // リムーバブルデバイスのみ UI からの通知を受け取るイベントを用意 - if (IsRemovableDevice()) { - event = new Event(this); - event->SetName(GetName() + " MediaChange"); - } } // デストラクタ @@ -681,6 +661,14 @@ SCSIDisk::Init() return false; } + // リムーバブルデバイスのみ UI からの通知を受け取るイベントを用意 + if (IsRemovableDevice()) { + gScheduler->ConnectMessage(MessageID::SCSIDEV_LOAD(myid), this, + ToMessageCallback(&SCSIDisk::LoadCallback)); + gScheduler->ConnectMessage(MessageID::SCSIDEV_UNLOAD(myid), this, + ToMessageCallback(&SCSIDisk::UnloadCallback)); + } + // オープン前に writeignore をチェック。 // この後パス名を処理するとすぐに UI に通知を出すため、それより前で // 行わないといけない。うーん。 @@ -713,7 +701,14 @@ SCSIDisk::Init() // 設定時点でファイルが指定されていればオープン if (filename.empty() == false) { - const std::string path = gMainApp.GetVMDir() + filename; + std::string path; + if (filename[0] == '/') { + path = filename; + } else { + // 相対パスなら VM ディレクトリから + path = gMainApp.GetVMDir() + filename; + } + if (LoadDisk(path) == false) { return false; } @@ -731,17 +726,15 @@ SCSIDisk::Init() } // 電源オン -bool -SCSIDisk::PowerOn() +void +SCSIDisk::ResetHard(bool poweron) { - if (inherited::PowerOn() == false) { - return false; - } + inherited::ResetHard(poweron); - // メディアの取り出し禁止を解除 - PreventMediumRemoval(false); - - return true; + if (poweron) { + // メディアの取り出し禁止を解除 + PreventMediumRemoval(false); + } } // バスリセット @@ -926,15 +919,13 @@ SCSIDisk::MediaChanged() const UIMessage::Post(UIMessage::SCSI_MEDIA_CHANGE, GetMyID()); } - // メディアを挿入する。 // UI スレッドで実行されるので、VM スレッドに通知するだけ。 void SCSIDisk::LoadDiskUI(const std::string& pathname_) { new_pathname = pathname_; - event->func = (DeviceCallback_t)&SCSIDisk::LoadCallback; - event->Start(); + gScheduler->SendMessage(MessageID::SCSIDEV_LOAD(GetMyID())); } // メディアを排出する。 @@ -942,14 +933,12 @@ SCSIDisk::LoadDiskUI(const std::string& void SCSIDisk::UnloadDiskUI(bool force) { - event->func = (DeviceCallback_t)&SCSIDisk::UnloadCallback; - event->code = force; - event->Start(); + gScheduler->SendMessage(MessageID::SCSIDEV_UNLOAD(GetMyID()), force); } -// メディア挿入コールバック +// メディア挿入メッセージコールバック void -SCSIDisk::LoadCallback(Event& ev) +SCSIDisk::LoadCallback(MessageID msgid, uint32 arg) { if (LoadDisk(new_pathname) == false) { // 失敗したら UI に通知 @@ -957,11 +946,11 @@ SCSIDisk::LoadCallback(Event& ev) } } -// メディア排出コールバック +// メディア排出メッセージコールバック void -SCSIDisk::UnloadCallback(Event& ev) +SCSIDisk::UnloadCallback(MessageID msgid, uint32 arg) { - bool force = ev.code; + bool force = arg; UnloadDisk(force); } @@ -1061,7 +1050,7 @@ SCSIDisk::Inquiry(std::vector& bu prodname = "SCSIMO"; break; default: - assert(false); + PANIC("Unexpected devtype=%d", (int)devtype); } buf[0] = inqtype; @@ -1174,3 +1163,17 @@ SCSIDisk::PreventMediumRemoval(bool val) } medium_removal_prevented = val; } + +// メディアの書き込みモードを文字列で返す +const char * +SCSIDisk::GetWriteModeStr() const +{ + switch (write_mode) { + case RW::ReadOnly: return "ReadOnly"; + case RW::WriteProtect: return "WriteProtected"; + case RW::Writeable: return "Writeable"; + case RW::WriteIgnore: return "WriteIgnore"; + default: + __unreachable(); + } +}