--- nono/vm/lance.h 2026/04/29 17:04:28 1.1.1.1 +++ nono/vm/lance.h 2026/04/29 17:04:55 1.1.1.8 @@ -1,11 +1,13 @@ // // nono -// Copyright (C) 2018 isaki@NetBSD.org +// Copyright (C) 2020 nono project +// Licensed under nono-license.txt // #pragma once #include "ethernet.h" +#include "monitor.h" #include "scheduler.h" struct AM7990 @@ -134,47 +136,46 @@ struct AM7990 bool IsBSWP() const { return (csr3 & CSR3_BSWP); } }; -class LanceDevice - : public EthernetDevice +class LanceDevice : public EthernetDevice { - typedef EthernetDevice inherited; - public: - static const uint32 REQ_TXIDLE = 0x00000001; // ポーリングフェーズ - static const uint32 REQ_TXCOPY = 0x00000002; // バッファ読込フェーズ - static const uint32 REQ_TXSEND = 0x00000004; // 送信フェーズ - static const uint32 REQ_TXNEXT = 0x00000008; // TMD 更新フェーズ - static const uint32 REQ_RXIDLE = 0x00000010; // ポーリングフェーズ - static const uint32 REQ_RXCOPY = 0x00000020; // バッファ書込フェーズ - static const uint32 REQ_RXNEXT = 0x00000040; // RMD 更新フェーズ - static const uint32 REQ_RXMISS = 0x00000080; // 受信 MISS - static const uint32 REQ_RXRECV = 0x00000100; // 受信イベント - static const uint32 REQ_TERMINATE = 0x80000000; // スレッド終了指示 + using inherited = EthernetDevice; + + // 受信フェーズ + enum class RXPhase { + IDLE = 0, // ポーリング + COPY, // バッファ書き込み + NEXT, // RMD 更新 + MISS, // 受信ミス + }; + // 送信フェーズ + enum class TXPhase { + IDLE = 0, // ポーリング + COPY, // バッファ読み込み + SEND, // 送信 + NEXT, // TMD 更新 + }; public: LanceDevice(); - virtual ~LanceDevice(); - - virtual bool Init(); - virtual void ResetHard(); + virtual ~LanceDevice() override; - virtual uint64 Read8(uint32 addr); - virtual uint64 Read16(uint32 addr); - virtual uint64 Read32(uint32 addr); - virtual uint64 Write8(uint32 addr, uint32 data); - virtual uint64 Write16(uint32 addr, uint32 data); - virtual uint64 Write32(uint32 addr, uint32 data); - virtual uint64 Peek8(uint32 addr); + bool Init() override; + void ResetHard() override; - virtual bool MonitorUpdate(); + protected: + // BusIO インタフェース + static const uint32 NPORT = 2; + uint64 Read(uint32 offset); + uint64 Write(uint32 offset, uint32 data); + uint64 Peek(uint32 offset); - // ワーカスレッド (エントリポイントから呼ばれる) - void ThreadRun(); - - // パケットを受信した (ホストネットワークスレッドから呼ばれる) - virtual bool RecvPacket(qvector& buf); + // パケット受信通知 (親クラスから) + void NotifyRecvPacket() override; private: - void MonitorReg(int x, int y, uint32 data, const char * const * names); + DECLARE_MONITOR_CALLBACK(MonitorUpdate); + void MonitorReg(TextScreen&, + int x, int y, uint32 data, const char * const * names); uint32 ReadData(); void WriteData(uint32 data); @@ -189,19 +190,23 @@ class LanceDevice // 割り込み要因をセットし、許可されていれば割り込みを上げる void SetInterrupt(uint32 bit); - // 許可されていれば割り込みを上げる - void Interrupt(); + // 割り込み信号線の状態を変える + void ChangeInterrupt(); - void TXIdle(); - void TXCopy(); - void TXSend(); - void TXNext(); - void RXIdle(); - void RXCopy(); - void RXNext(); - void RXRecv(); - void RXMiss(); - void Callback(int arg); + void TXIdle(Event&); + void TXCopy(Event&); + void TXSend(Event&); + void TXNext(Event&); + + void RXIdle(Event&); + void RXCopy(Event&); + void RXNext(Event&); + void RXRecv(Event&); + void RXMiss(Event&); + + // フェーズ遷移 + void ChangePhase(TXPhase new_phase, uint64 time = 100_nsec); + void ChangePhase(RXPhase new_phase, uint64 time = 100_nsec); const std::string BitToString(const char * const name[], uint16 bits); const std::string CSR0ToString(uint16 bits); @@ -218,27 +223,33 @@ class LanceDevice // Initialization Block uint16 initblock[12] {}; - uint32 rmd_base = 0; // 受信ディスクリプタの(ZRAM での)開始アドレス - int rmd_num = 0; // 受信ディスクリプタ数 - int rmd_cur = 0; // RMD 内の現在の注目インデックス - uint32 tmd_base = 0; // 送信ディスクリプタの(ZRAM での)開始アドレス - int tmd_num = 0; // 送信ディスクリプタ数 - int tmd_cur = 0; // TMD 内の現在の注目インデックス - - uint16 rmd1 = 0; // 現在の RMD1 - uint32 rmd1_ahead = 0; // 次の RMD1 先読み (負なら先読みしてない状態) - qvector rxq {}; // 受信バッファ + uint64 padr {}; // PADR を下位詰めしたもの + + uint32 rmd_base {}; // 受信ディスクリプタの(ZRAM での)開始アドレス + int rmd_num {}; // 受信ディスクリプタ数 + int rmd_cur {}; // RMD 内の現在の注目インデックス + uint32 tmd_base {}; // 送信ディスクリプタの(ZRAM での)開始アドレス + int tmd_num {}; // 送信ディスクリプタ数 + int tmd_cur {}; // TMD 内の現在の注目インデックス - uint16 tmd1 = 0; // 現在の TMD1 - uint32 tmd1_ahead = 0; // 次の TMD1 先読み (負なら先読みしてない状態) + uint16 rmd1 {}; // 現在の RMD1 + uint32 rmd1_ahead {}; // 次の RMD1 先読み (負なら先読みしてない状態) + + RXPacket rxpacket {}; // 受信バッファ + + uint16 tmd1 {}; // 現在の TMD1 + uint32 tmd1_ahead {}; // 次の TMD1 先読み (負なら先読みしてない状態) std::vector txq {}; // 送信バッファ + RXPhase rx_phase {}; // 受信フェーズ + TXPhase tx_phase {}; // 送信フェーズ + // 待ち時間用イベント - Event rx_event {}; - Event tx_event {}; + Event rx_event { this }; + Event tx_event { this }; + + Monitor monitor { this }; // 前回の TMD 状態文字列 (デバッグ表示用) std::string last_tmdstr {}; }; - -extern LanceDevice *gLance;