--- nono/vm/ethernet.h 2026/04/29 17:04:28 1.1 +++ nono/vm/ethernet.h 2026/04/29 17:05:38 1.1.1.13 @@ -1,17 +1,12 @@ // // nono -// Copyright (C) 2019 isaki@NetBSD.org +// Copyright (C) 2020 nono project +// Licensed under nono-license.txt // -#pragma once - -#include -#include -#include -#include -#include "device.h" -#include "mystring.h" -#include "qvector.h" +// +// Ethernet 基本クラス +// // IODevice // | @@ -20,81 +15,37 @@ // | // +--------+-------+ // 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 -{ - typedef std::array inherited; - public: - // この MAC アドレスが 00:00:00:00:00:00 なら true - bool empty() const { - for (const auto& v : *this) { - if (v != 0) { - return false; - } - } - return true; - } - - // 文字列形式を返す - std::string to_string() const { - std::string buf; - for (const auto& v : *this) { - buf += string_format("%02x:", v); - } - buf.pop_back(); - return buf; - } -}; +#include "device.h" +#include "macaddr.h" + +class HostNetDevice; class EthernetDevice : public IODevice { - typedef IODevice inherited; + using inherited = IODevice; protected: - EthernetDevice(); + explicit EthernetDevice(uint objid_); public: - virtual ~EthernetDevice(); + ~EthernetDevice() override; - virtual bool Create(); - virtual bool Init(); + bool Create() override; - // パケットを受信した。 - // ホストネットワークドライバから呼ばれる。 - // ホストネットワークスレッドで mtx 取得状態で呼ばれるので、 - // バッファをコピーしてこちらのスレッドに処理を引き渡すところまでを行う。 - // buf はプリアンブルを含まず、イーサネットフレームヘッダ(14バイト)、 - // データ(46-1500バイト)、FCS(CRC) (4バイト) からなる。 - virtual bool RecvPacket(qvector& buf) = 0; - - // パケット受信許可。 - // true の場合のみホストドライバは RecvPacket() をコールしてよい。 - bool RecvEnabled = false; - - // このデバイスと継承デバイスのメンバ変数を読み書きする場合は - // 必ずこのロックを取得する。? - // cv もこのロックを使う。 - std::mutex mtx {}; - // ワーカスレッドへ指示を出すための条件変数 - std::condition_variable cv {}; - // ワーカスレッドへのリクエストフラグ - uint32 request = 0; + // 設定から MAC アドレスを取得するヘルパー関数。 + // (MAC アドレスを保持するデバイスが呼ぶ) + static bool GetConfigMacAddr(uint n, MacAddr *dst, bool accept_rom); - protected: - // MAC アドレスを生成する - static void GenerateMacAddr(macaddr_t *addr); + // CRC32 を計算する。 + static uint32 CRC32(const uint8 *buf, size_t buflen); + static uint32 CRC32(const MacAddr& mac); - // MAC アドレス文字列をバイナリに変換する - static bool ParseMacAddr(macaddr_t& dst, const std::string& src); + protected: + // ホストスレッドからの受信通知 + void HostRxCallback(); - // パケットを送信する - bool SendPacket(const std::vector& buf); + std::unique_ptr hostnet /*{}*/; }; - -extern EthernetDevice *gEthernet;