--- nono/vm/ethernet.h 2026/04/29 17:04:41 1.1.1.4 +++ nono/vm/ethernet.h 2026/04/29 17:05:10 1.1.1.8 @@ -4,15 +4,9 @@ // Licensed under nono-license.txt // -#pragma once - -#include "device.h" -#include "mystring.h" -#include "qvector.h" -#include -#include -#include -#include +// +// Ethernet 基本クラス +// // IODevice // | @@ -23,98 +17,41 @@ // v v // LanceDevice RTL8019Device // (LUNA, AM7990) (X68000, RTL8019) -// -// これ自体は IODevice である必要はないが、LanceDevice などの親になるため -// IODevice にしてある。(多重継承はしない) - -class NetDriver; -// MAC アドレス格納用のクラス -class macaddr_t : public std::array -{ - using inherited = std::array; - public: - // この MAC アドレスが 00:00:00:00:00:00 なら true - bool empty() const { - for (const auto& v : *this) { - if (v != 0) { - return false; - } - } - return true; - } +#pragma once - // 区切り文字なしの文字列形式を返す。主に ROM 埋め込み用で英字は大文字。 - // 01:23:AB なら "0123AB" のような感じ。 - std::string to_string() const { - std::string buf; - for (const auto& v : *this) { - buf += string_format("%02X", v); - } - return buf; - } +#include "device.h" +#include "macaddr.h" - // sep を区切り文字とする文字列形式を返す。主に表示用で英字は小文字。 - // 01:23:AB で sep = ":" なら "01:23:ab" のような感じ。 - std::string to_string(char sep) const { - std::string buf; - for (const auto& v : *this) { - buf += string_format("%02x%c", v, sep); - } - buf.pop_back(); - return buf; - } -}; +class HostNetDevice; class EthernetDevice : public IODevice { using inherited = IODevice; protected: - EthernetDevice(); + EthernetDevice(const std::string& objname_); public: - ~EthernetDevice() override; + virtual ~EthernetDevice() override; bool Create() override; - bool Init() override; - - // パケットを受信した。 - // ホストネットワークドライバから呼ばれる。 - // ホストネットワークスレッドで mtx 取得状態で呼ばれるので、 - // バッファをコピーしてこちらのスレッドに処理を引き渡すところまでを行う。 - // buf はプリアンブルを含まず、イーサネットフレームヘッダ(14バイト)、 - // データ(46-1500バイト)、FCS(CRC) (4バイト) からなる。 - virtual bool RecvPacket(qvector& buf) = 0; - - // パケット受信許可。 - // true の場合のみホストドライバは RecvPacket() をコールしてよい。 - bool RecvEnabled {}; // MAC アドレスを取得 macaddr_t GetMacAddr() const { return macaddr; } - // このデバイスと継承デバイスのメンバ変数を読み書きする場合は - // 必ずこのロックを取得する。? - // cv もこのロックを使う。 - std::mutex mtx {}; - // ワーカスレッドへ指示を出すための条件変数 - std::condition_variable cv {}; - // ワーカスレッドへのリクエストフラグ - uint32 request {}; + // ホストデバイスを取得する。Create() 完了後から使用可能。 + // (ステータスパネルが呼び出す) + HostNetDevice *GetHostNet() const { return hostnet.get(); } protected: - // MAC アドレスを生成する - static void GenerateMacAddr(macaddr_t *addr); - - // MAC アドレス文字列をバイナリに変換する - static bool ParseMacAddr(macaddr_t& dst, const std::string& src); - - // パケットを送信する - bool SendPacket(const std::vector& buf); + // ホストスレッドからの受信通知 + void HostRxCallback(); // MAC アドレス macaddr_t macaddr {}; + + std::unique_ptr hostnet /*{}*/; }; -extern std::unique_ptr gEthernet; +extern EthernetDevice *gEthernet;