Annotation of nono/vm/lance.h, revision 1.1.1.12

1.1       root        1: //
                      2: // nono
1.1.1.3   root        3: // Copyright (C) 2020 nono project
                      4: // Licensed under nono-license.txt
1.1       root        5: //
                      6: 
1.1.1.9   root        7: //
                      8: // Lance (AM7990)
                      9: //
                     10: 
1.1       root       11: #pragma once
                     12: 
                     13: #include "ethernet.h"
1.1.1.9   root       14: #include "event.h"
                     15: #include "hostnet.h"
                     16: #include "message.h"
1.1.1.8   root       17: #include "monitor.h"
1.1       root       18: 
1.1.1.10  root       19: class InterruptDevice;
                     20: class SubRAMDevice;
                     21: 
1.1       root       22: struct AM7990
                     23: {
                     24:        int rap;                        // レジスタアドレス (0..3)
                     25: 
                     26:        // CSR0
                     27:        static const uint16 CSR0_ERR    = 0x8000;
                     28:        static const uint16 CSR0_BABL   = 0x4000;
                     29:        static const uint16 CSR0_CERR   = 0x2000;
                     30:        static const uint16 CSR0_MISS   = 0x1000;
                     31:        static const uint16 CSR0_MERR   = 0x0800;
                     32:        static const uint16 CSR0_RINT   = 0x0400;
                     33:        static const uint16 CSR0_TINT   = 0x0200;
                     34:        static const uint16 CSR0_IDON   = 0x0100;
                     35:        static const uint16 CSR0_INTR   = 0x0080;
                     36:        static const uint16 CSR0_INEA   = 0x0040;
                     37:        static const uint16 CSR0_RXON   = 0x0020;
                     38:        static const uint16 CSR0_TXON   = 0x0010;
                     39:        static const uint16 CSR0_TDMD   = 0x0008;
                     40:        static const uint16 CSR0_STOP   = 0x0004;
                     41:        static const uint16 CSR0_STRT   = 0x0002;
                     42:        static const uint16 CSR0_INIT   = 0x0001;
                     43:        // CSR0_ERR はこれらの和
                     44:        static const uint16 CSR0_ERR_BITS =
                     45:                CSR0_BABL |
                     46:                CSR0_CERR |
                     47:                CSR0_MISS |
                     48:                CSR0_MERR;
                     49:        // CSR0_INTR はこれらの和
                     50:        static const uint16 CSR0_INTR_BITS =
                     51:                CSR0_BABL |
                     52:                CSR0_MISS |
                     53:                CSR0_MERR |
                     54:                CSR0_RINT |
                     55:                CSR0_TINT |
                     56:                CSR0_IDON;
                     57: 
                     58:        // CSR1
                     59:        static const uint16 CSR1_MASK   = 0xfffe;
                     60:        static const uint16 IADR_L_MASK = 0xfffe;
                     61: 
                     62:        // CSR2
                     63:        static const uint16 CSR2_MASK   = 0x00ff;
                     64:        static const uint16 IADR_H_MASK = 0x00ff;
                     65: 
                     66:        // CSR3
                     67:        static const uint16 CSR3_MASK   = 0x0007;
                     68:        static const uint16 CSR3_BSWP   = 0x0004;
                     69:        static const uint16 CSR3_ACON   = 0x0002;
                     70:        static const uint16 CSR3_BCON   = 0x0001;
                     71: 
                     72:        // IBMODE
                     73:        static const uint16 IBMODE_PROM = 0x8000;
                     74:        static const uint16 IBMODE_INTL = 0x0040;
                     75:        static const uint16 IBMODE_DRTY = 0x0020;
                     76:        static const uint16 IBMODE_COLL = 0x0010;
                     77:        static const uint16 IBMODE_DTCR = 0x0008;
                     78:        static const uint16 IBMODE_LOOP = 0x0004;
                     79:        static const uint16 IBMODE_DTX  = 0x0002;
                     80:        static const uint16 IBMODE_DRX  = 0x0001;
                     81:        // RMD1
                     82:        static const uint16 RMD1_OWN    = 0x8000;
                     83:        static const uint16 RMD1_ERR    = 0x4000;
                     84:        static const uint16 RMD1_FRAM   = 0x2000;
                     85:        static const uint16 RMD1_OFLO   = 0x1000;
                     86:        static const uint16 RMD1_CRC    = 0x0800;
                     87:        static const uint16 RMD1_BUFF   = 0x0400;
                     88:        static const uint16 RMD1_STP    = 0x0200;
                     89:        static const uint16 RMD1_ENP    = 0x0100;
                     90:        // TMD1
                     91:        static const uint16 TMD1_OWN    = 0x8000;
                     92:        static const uint16 TMD1_ERR    = 0x4000;
                     93:        static const uint16 TMD1_resv   = 0x2000;
                     94:        static const uint16 TMD1_MORE   = 0x1000;
                     95:        static const uint16 TMD1_ONE    = 0x0800;
                     96:        static const uint16 TMD1_DEF    = 0x0400;
                     97:        static const uint16 TMD1_STP    = 0x0200;
                     98:        static const uint16 TMD1_ENP    = 0x0100;
                     99:        // TMD3
                    100:        static const uint16 TMD3_BUFF   = 0x8000;
                    101:        static const uint16 TMD3_UFLO   = 0x4000;
                    102:        static const uint16 TMD3_resv   = 0x2000;
                    103:        static const uint16 TMD3_LCOL   = 0x1000;
                    104:        static const uint16 TMD3_LCAR   = 0x0800;
                    105:        static const uint16 TMD3_BTRY   = 0x0400;
                    106: 
                    107:        // CSR0 ビットごとに処理方法が異なる。
                    108:        //
                    109:        // b15 ERR :    値は計算で求める。csr0 の該当ビットは常時クリア。
                    110:        // b14 BABL:    csr0 で保持。
                    111:        // b13 CERR:    csr0 で保持。
                    112:        // b12 MISS:    csr0 で保持。
                    113:        // b11 ERR :    csr0 で保持。
                    114:        // b10 INT :    csr0 で保持。
                    115:        // b9  TINT:    csr0 で保持。
                    116:        // b8  IDON:    csr0 で保持。
                    117:        // b7  INTR:    値は計算で求める。csr0 の該当ビットは常時クリア。
                    118:        // b6  INEA:    csr0 で保持。
                    119:        // b5  RXON:    csr0 で保持。
                    120:        // b4  TXON:    csr0 で保持。
                    121:        // b2  STOP:    csr0 で保持。
                    122:        // b1  STRT:    csr0 で保持。
                    123:        // b0  INIT:    csr0 で保持。
                    124:        //
                    125:        // レジスタ値や状態を更新する際にはこれらに注意すること。
                    126:        // レジスタ値を読み出す時は GetCSR0() を使用すること。
                    127:        uint16 csr0;
                    128:        bool IsERR() const              { return ((csr0 & CSR0_ERR_BITS) != 0); }
                    129:        bool IsINTR() const             { return ((csr0 & CSR0_INTR_BITS) != 0); }
                    130:        bool IsSTOP() const             { return ((csr0 & CSR0_STOP) != 0); }
                    131: 
                    132:        // CSR0 の読み出し値を返す
                    133:        uint16 GetCSR0() const  {
                    134:                // ERR, INTR ビットだけ計算による
                    135:                uint16 intr = IsINTR() ? CSR0_INTR : 0;
                    136:                uint16 errbit = IsERR() ? CSR0_ERR : 0;
                    137:                return (csr0 | intr | errbit);
                    138:        }
                    139: 
                    140:        // CSR1, CSR2 は IADR の状態で保持する
                    141:        uint32 iadr;
                    142: 
                    143:        // CSR3
                    144:        uint16 csr3;
                    145:        bool IsBSWP() const             { return (csr3 & CSR3_BSWP); }
                    146: };
                    147: 
1.1.1.8   root      148: class LanceDevice : public EthernetDevice
1.1       root      149: {
1.1.1.3   root      150:        using inherited = EthernetDevice;
1.1.1.8   root      151: 
                    152:        // 受信フェーズ
                    153:        enum class RXPhase {
1.1.1.9   root      154:                STOP = 0,       // (動作していない)
                    155:                IDLE,           // ポーリング
1.1.1.8   root      156:                COPY,           // バッファ書き込み
                    157:                NEXT,           // RMD 更新
                    158:                MISS,           // 受信ミス
                    159:        };
                    160:        // 送信フェーズ
                    161:        enum class TXPhase {
1.1.1.9   root      162:                STOP = 0,       // (動作していない)
                    163:                IDLE,           // ポーリング
1.1.1.8   root      164:                COPY,           // バッファ読み込み
                    165:                SEND,           // 送信
                    166:                NEXT,           // TMD 更新
                    167:        };
1.1       root      168: 
                    169:  public:
                    170:        LanceDevice();
1.1.1.12! root      171:        ~LanceDevice() override;
1.1       root      172: 
1.1.1.3   root      173:        bool Init() override;
1.1.1.9   root      174:        void ResetHard(bool poweron) override;
1.1       root      175: 
1.1.1.2   root      176:  protected:
                    177:        // BusIO インタフェース
                    178:        static const uint32 NPORT = 2;
1.1.1.12! root      179:        busdata Read(uint32 offset);
        !           180:        busdata Write(uint32 offset, uint32 data);
        !           181:        busdata Peek(uint32 offset);
1.1.1.2   root      182: 
1.1.1.8   root      183:  private:
                    184:        DECLARE_MONITOR_CALLBACK(MonitorUpdate);
1.1.1.4   root      185:        void MonitorReg(TextScreen&,
                    186:                int x, int y, uint32 data, const char * const * names);
1.1       root      187:        uint32 ReadData();
                    188:        void WriteData(uint32 data);
                    189: 
                    190:        // メモリアクセス
                    191:        uint64 ReadMem(uint32 paddr);
                    192:        uint64 WriteMem(uint32 paddr, uint32 data);
                    193:        uint64 PeekMem(uint32 paddr);
                    194: 
                    195:        void ChipInit();
                    196:        void ChipStart();
                    197:        void ChipStop();
                    198: 
                    199:        // 割り込み要因をセットし、許可されていれば割り込みを上げる
                    200:        void SetInterrupt(uint32 bit);
1.1.1.6   root      201:        // 割り込み信号線の状態を変える
                    202:        void ChangeInterrupt();
1.1       root      203: 
1.1.1.8   root      204:        void TXIdle(Event&);
1.1.1.9   root      205:        void TXCopy();
1.1.1.8   root      206:        void TXSend(Event&);
                    207:        void TXNext(Event&);
                    208: 
                    209:        void RXIdle(Event&);
1.1.1.9   root      210:        void RXCopy();
1.1.1.8   root      211:        void RXNext(Event&);
1.1.1.9   root      212:        void RXMiss();
1.1.1.8   root      213: 
                    214:        // フェーズ遷移
                    215:        void ChangePhase(TXPhase new_phase, uint64 time = 100_nsec);
                    216:        void ChangePhase(RXPhase new_phase, uint64 time = 100_nsec);
1.1       root      217: 
1.1.1.12! root      218:        // パケット受信通知 (HostNet から EthernetDevice 経由で呼ばれる)
        !           219:        void RxMessage(MessageID, uint32);
1.1.1.9   root      220: 
1.1       root      221:        const std::string BitToString(const char * const name[], uint16 bits);
                    222:        const std::string CSR0ToString(uint16 bits);
                    223:        const std::string CSR3ToString(uint16 bits);
                    224:        static const char * const csr0names[];
                    225:        static const char * const csr3names[];
                    226:        static const char * const ib_mode_names[];
                    227:        static const char * const rmd1_names[];
                    228:        static const char * const tmd1_names[];
                    229:        static const char * const tmd3_names[];
                    230: 
                    231:        struct AM7990 reg {};
                    232: 
                    233:        // Initialization Block
                    234:        uint16 initblock[12] {};
                    235: 
1.1.1.9   root      236:        macaddr_t padr {};
1.1.1.8   root      237: 
1.1.1.10  root      238:        uint32 rmd_base {};                     // 受信ディスクリプタの(SubRAMでの)開始アドレス
1.1.1.5   root      239:        int    rmd_num {};                      // 受信ディスクリプタ数
                    240:        int    rmd_cur {};                      // RMD 内の現在の注目インデックス
1.1.1.10  root      241:        uint32 tmd_base {};                     // 送信ディスクリプタの(SubRAMでの)開始アドレス
1.1.1.5   root      242:        int    tmd_num {};                      // 送信ディスクリプタ数
                    243:        int    tmd_cur {};                      // TMD 内の現在の注目インデックス
                    244: 
                    245:        uint16 rmd1 {};                         // 現在の RMD1
                    246:        uint32 rmd1_ahead {};           // 次の RMD1 先読み (負なら先読みしてない状態)
1.1.1.8   root      247: 
1.1.1.5   root      248:        uint16 tmd1 {};                         // 現在の TMD1
                    249:        uint32 tmd1_ahead {};           // 次の TMD1 先読み (負なら先読みしてない状態)
1.1.1.9   root      250: 
                    251:        NetPacket rx_packet {};         // 受信バッファ
                    252:        NetPacket tx_packet {};         // 送信バッファ
1.1       root      253: 
1.1.1.8   root      254:        RXPhase rx_phase {};            // 受信フェーズ
                    255:        TXPhase tx_phase {};            // 送信フェーズ
                    256: 
1.1.1.10  root      257:        // 前回の TMD 状態文字列 (デバッグ表示用)
                    258:        std::string last_tmdstr {};
                    259: 
                    260:        InterruptDevice *interrupt {};
                    261:        SubRAMDevice *subram {};
                    262: 
1.1       root      263:        // 待ち時間用イベント
1.1.1.8   root      264:        Event rx_event { this };
                    265:        Event tx_event { this };
                    266: 
                    267:        Monitor monitor { this };
1.1       root      268: };
1.1.1.10  root      269: 
                    270: static inline LanceDevice *GetLanceDevice() {
1.1.1.11  root      271:        return Object::GetObject<LanceDevice>(OBJ_ETHERNET(0));
1.1.1.10  root      272: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.