--- nono/vm/ethernet.h 2026/04/29 17:04:41 1.1.1.4 +++ nono/vm/ethernet.h 2026/04/29 17:05:29 1.1.1.12 @@ -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 // | @@ -21,100 +15,33 @@ // | // +--------+-------+ // v v -// LanceDevice RTL8019Device -// (LUNA, AM7990) (X68000, RTL8019) -// -// これ自体は IODevice である必要はないが、LanceDevice などの親になるため -// IODevice にしてある。(多重継承はしない) +// LanceDevice RTL8019ASDevice +// (LUNA, AM7990) (X68000, RTL8019AS) -class NetDriver; +#pragma once -// 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; - } - - // 区切り文字なしの文字列形式を返す。主に 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; - } - - // 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; - } -}; +#include "device.h" +#include "macaddr.h" + +class HostNetDevice; class EthernetDevice : public IODevice { using inherited = IODevice; protected: - EthernetDevice(); + explicit EthernetDevice(uint objid_); public: ~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 {}; + // 設定から MAC アドレスを取得するヘルパー関数。 + // (MAC アドレスを保持するデバイスが呼ぶ) + static bool GetConfigMacAddr(uint n, macaddr_t *dst, bool accept_rom); protected: - // MAC アドレスを生成する - static void GenerateMacAddr(macaddr_t *addr); + // ホストスレッドからの受信通知 + void HostRxCallback(); - // MAC アドレス文字列をバイナリに変換する - static bool ParseMacAddr(macaddr_t& dst, const std::string& src); - - // パケットを送信する - bool SendPacket(const std::vector& buf); - - // MAC アドレス - macaddr_t macaddr {}; + std::unique_ptr hostnet /*{}*/; }; - -extern std::unique_ptr gEthernet;