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

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: {
1.1.1.13! root       24:        uint rap;                       // レジスタアドレス (0..3)
1.1       root       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:        // BusIO インタフェース
1.1.1.13! root      177:        // (内蔵 ROM からのアクセス用に特別に public にしてある)
        !           178:        busdata WritePort(uint32 offset, uint32 data);
        !           179:  protected:
1.1.1.2   root      180:        static const uint32 NPORT = 2;
1.1.1.13! root      181:        busdata ReadPort(uint32 offset);
        !           182:        busdata PeekPort(uint32 offset);
1.1.1.2   root      183: 
1.1.1.8   root      184:  private:
                    185:        DECLARE_MONITOR_CALLBACK(MonitorUpdate);
1.1.1.4   root      186:        void MonitorReg(TextScreen&,
                    187:                int x, int y, uint32 data, const char * const * names);
1.1       root      188:        uint32 ReadData();
                    189:        void WriteData(uint32 data);
                    190: 
                    191:        // メモリアクセス
1.1.1.13! root      192:        uint32 ReadMem(uint32 paddr);
        !           193:        void WriteMem(uint32 paddr, uint32 data);
        !           194:        uint32 PeekMem(uint32 paddr) const;
1.1       root      195: 
                    196:        void ChipInit();
                    197:        void ChipStart();
                    198:        void ChipStop();
                    199: 
                    200:        // 割り込み要因をセットし、許可されていれば割り込みを上げる
                    201:        void SetInterrupt(uint32 bit);
1.1.1.6   root      202:        // 割り込み信号線の状態を変える
                    203:        void ChangeInterrupt();
1.1       root      204: 
1.1.1.8   root      205:        void TXIdle(Event&);
1.1.1.9   root      206:        void TXCopy();
1.1.1.8   root      207:        void TXSend(Event&);
                    208:        void TXNext(Event&);
                    209: 
                    210:        void RXIdle(Event&);
1.1.1.9   root      211:        void RXCopy();
1.1.1.8   root      212:        void RXNext(Event&);
1.1.1.9   root      213:        void RXMiss();
1.1.1.8   root      214: 
                    215:        // フェーズ遷移
                    216:        void ChangePhase(TXPhase new_phase, uint64 time = 100_nsec);
                    217:        void ChangePhase(RXPhase new_phase, uint64 time = 100_nsec);
1.1       root      218: 
1.1.1.12  root      219:        // パケット受信通知 (HostNet から EthernetDevice 経由で呼ばれる)
                    220:        void RxMessage(MessageID, uint32);
1.1.1.9   root      221: 
1.1       root      222:        const std::string BitToString(const char * const name[], uint16 bits);
                    223:        const std::string CSR0ToString(uint16 bits);
                    224:        const std::string CSR3ToString(uint16 bits);
                    225:        static const char * const csr0names[];
                    226:        static const char * const csr3names[];
                    227:        static const char * const ib_mode_names[];
                    228:        static const char * const rmd1_names[];
                    229:        static const char * const tmd1_names[];
                    230:        static const char * const tmd3_names[];
                    231: 
                    232:        struct AM7990 reg {};
                    233: 
                    234:        // Initialization Block
                    235:        uint16 initblock[12] {};
                    236: 
1.1.1.9   root      237:        macaddr_t padr {};
1.1.1.8   root      238: 
1.1.1.10  root      239:        uint32 rmd_base {};                     // 受信ディスクリプタの(SubRAMでの)開始アドレス
1.1.1.13! root      240:        uint   rmd_num {};                      // 受信ディスクリプタ数
        !           241:        uint   rmd_cur {};                      // RMD 内の現在の注目インデックス
1.1.1.10  root      242:        uint32 tmd_base {};                     // 送信ディスクリプタの(SubRAMでの)開始アドレス
1.1.1.13! root      243:        uint   tmd_num {};                      // 送信ディスクリプタ数
        !           244:        uint   tmd_cur {};                      // TMD 内の現在の注目インデックス
1.1.1.5   root      245: 
                    246:        uint16 rmd1 {};                         // 現在の RMD1
                    247:        uint32 rmd1_ahead {};           // 次の RMD1 先読み (負なら先読みしてない状態)
1.1.1.8   root      248: 
1.1.1.5   root      249:        uint16 tmd1 {};                         // 現在の TMD1
                    250:        uint32 tmd1_ahead {};           // 次の TMD1 先読み (負なら先読みしてない状態)
1.1.1.9   root      251: 
                    252:        NetPacket rx_packet {};         // 受信バッファ
                    253:        NetPacket tx_packet {};         // 送信バッファ
1.1       root      254: 
1.1.1.8   root      255:        RXPhase rx_phase {};            // 受信フェーズ
                    256:        TXPhase tx_phase {};            // 送信フェーズ
                    257: 
1.1.1.10  root      258:        // 前回の TMD 状態文字列 (デバッグ表示用)
                    259:        std::string last_tmdstr {};
                    260: 
                    261:        InterruptDevice *interrupt {};
                    262:        SubRAMDevice *subram {};
                    263: 
1.1       root      264:        // 待ち時間用イベント
1.1.1.8   root      265:        Event rx_event { this };
                    266:        Event tx_event { this };
                    267: 
                    268:        Monitor monitor { this };
1.1       root      269: };
1.1.1.10  root      270: 
                    271: static inline LanceDevice *GetLanceDevice() {
1.1.1.11  root      272:        return Object::GetObject<LanceDevice>(OBJ_ETHERNET(0));
1.1.1.10  root      273: }

unix.superglobalmegacorp.com

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