|
|
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 root 17:
1.1.1.10 root 18: class InterruptDevice;
19: class SubRAMDevice;
20:
1.1 root 21: struct AM7990
22: {
1.1.1.13 root 23: uint rap; // レジスタアドレス (0..3)
1.1 root 24:
25: // CSR0
26: static const uint16 CSR0_ERR = 0x8000;
27: static const uint16 CSR0_BABL = 0x4000;
28: static const uint16 CSR0_CERR = 0x2000;
29: static const uint16 CSR0_MISS = 0x1000;
30: static const uint16 CSR0_MERR = 0x0800;
31: static const uint16 CSR0_RINT = 0x0400;
32: static const uint16 CSR0_TINT = 0x0200;
33: static const uint16 CSR0_IDON = 0x0100;
34: static const uint16 CSR0_INTR = 0x0080;
35: static const uint16 CSR0_INEA = 0x0040;
36: static const uint16 CSR0_RXON = 0x0020;
37: static const uint16 CSR0_TXON = 0x0010;
38: static const uint16 CSR0_TDMD = 0x0008;
39: static const uint16 CSR0_STOP = 0x0004;
40: static const uint16 CSR0_STRT = 0x0002;
41: static const uint16 CSR0_INIT = 0x0001;
42: // CSR0_ERR はこれらの和
43: static const uint16 CSR0_ERR_BITS =
44: CSR0_BABL |
45: CSR0_CERR |
46: CSR0_MISS |
47: CSR0_MERR;
48: // CSR0_INTR はこれらの和
49: static const uint16 CSR0_INTR_BITS =
50: CSR0_BABL |
51: CSR0_MISS |
52: CSR0_MERR |
53: CSR0_RINT |
54: CSR0_TINT |
55: CSR0_IDON;
56:
57: // CSR1
58: static const uint16 CSR1_MASK = 0xfffe;
59: static const uint16 IADR_L_MASK = 0xfffe;
60:
61: // CSR2
62: static const uint16 CSR2_MASK = 0x00ff;
63: static const uint16 IADR_H_MASK = 0x00ff;
64:
65: // CSR3
66: static const uint16 CSR3_MASK = 0x0007;
67: static const uint16 CSR3_BSWP = 0x0004;
68: static const uint16 CSR3_ACON = 0x0002;
69: static const uint16 CSR3_BCON = 0x0001;
70:
71: // IBMODE
72: static const uint16 IBMODE_PROM = 0x8000;
73: static const uint16 IBMODE_INTL = 0x0040;
74: static const uint16 IBMODE_DRTY = 0x0020;
75: static const uint16 IBMODE_COLL = 0x0010;
76: static const uint16 IBMODE_DTCR = 0x0008;
77: static const uint16 IBMODE_LOOP = 0x0004;
78: static const uint16 IBMODE_DTX = 0x0002;
79: static const uint16 IBMODE_DRX = 0x0001;
80: // RMD1
81: static const uint16 RMD1_OWN = 0x8000;
82: static const uint16 RMD1_ERR = 0x4000;
83: static const uint16 RMD1_FRAM = 0x2000;
84: static const uint16 RMD1_OFLO = 0x1000;
85: static const uint16 RMD1_CRC = 0x0800;
86: static const uint16 RMD1_BUFF = 0x0400;
87: static const uint16 RMD1_STP = 0x0200;
88: static const uint16 RMD1_ENP = 0x0100;
89: // TMD1
90: static const uint16 TMD1_OWN = 0x8000;
91: static const uint16 TMD1_ERR = 0x4000;
92: static const uint16 TMD1_resv = 0x2000;
93: static const uint16 TMD1_MORE = 0x1000;
94: static const uint16 TMD1_ONE = 0x0800;
95: static const uint16 TMD1_DEF = 0x0400;
96: static const uint16 TMD1_STP = 0x0200;
97: static const uint16 TMD1_ENP = 0x0100;
98: // TMD3
99: static const uint16 TMD3_BUFF = 0x8000;
100: static const uint16 TMD3_UFLO = 0x4000;
101: static const uint16 TMD3_resv = 0x2000;
102: static const uint16 TMD3_LCOL = 0x1000;
103: static const uint16 TMD3_LCAR = 0x0800;
104: static const uint16 TMD3_BTRY = 0x0400;
105:
106: // CSR0 ビットごとに処理方法が異なる。
107: //
108: // b15 ERR : 値は計算で求める。csr0 の該当ビットは常時クリア。
109: // b14 BABL: csr0 で保持。
110: // b13 CERR: csr0 で保持。
111: // b12 MISS: csr0 で保持。
112: // b11 ERR : csr0 で保持。
113: // b10 INT : csr0 で保持。
114: // b9 TINT: csr0 で保持。
115: // b8 IDON: csr0 で保持。
116: // b7 INTR: 値は計算で求める。csr0 の該当ビットは常時クリア。
117: // b6 INEA: csr0 で保持。
118: // b5 RXON: csr0 で保持。
119: // b4 TXON: csr0 で保持。
120: // b2 STOP: csr0 で保持。
121: // b1 STRT: csr0 で保持。
122: // b0 INIT: csr0 で保持。
123: //
124: // レジスタ値や状態を更新する際にはこれらに注意すること。
125: // レジスタ値を読み出す時は GetCSR0() を使用すること。
126: uint16 csr0;
127: bool IsERR() const { return ((csr0 & CSR0_ERR_BITS) != 0); }
128: bool IsINTR() const { return ((csr0 & CSR0_INTR_BITS) != 0); }
129: bool IsSTOP() const { return ((csr0 & CSR0_STOP) != 0); }
130:
131: // CSR0 の読み出し値を返す
132: uint16 GetCSR0() const {
133: // ERR, INTR ビットだけ計算による
134: uint16 intr = IsINTR() ? CSR0_INTR : 0;
135: uint16 errbit = IsERR() ? CSR0_ERR : 0;
136: return (csr0 | intr | errbit);
137: }
138:
139: // CSR1, CSR2 は IADR の状態で保持する
140: uint32 iadr;
141:
142: // CSR3
143: uint16 csr3;
144: bool IsBSWP() const { return (csr3 & CSR3_BSWP); }
145: };
146:
1.1.1.8 root 147: class LanceDevice : public EthernetDevice
1.1 root 148: {
1.1.1.3 root 149: using inherited = EthernetDevice;
1.1.1.8 root 150:
151: // 受信フェーズ
152: enum class RXPhase {
1.1.1.9 root 153: STOP = 0, // (動作していない)
154: IDLE, // ポーリング
1.1.1.8 root 155: COPY, // バッファ書き込み
156: NEXT, // RMD 更新
157: MISS, // 受信ミス
158: };
159: // 送信フェーズ
160: enum class TXPhase {
1.1.1.9 root 161: STOP = 0, // (動作していない)
162: IDLE, // ポーリング
1.1.1.8 root 163: COPY, // バッファ読み込み
164: SEND, // 送信
165: NEXT, // TMD 更新
166: };
1.1 root 167:
168: public:
169: LanceDevice();
1.1.1.12 root 170: ~LanceDevice() override;
1.1 root 171:
1.1.1.3 root 172: bool Init() override;
1.1.1.9 root 173: void ResetHard(bool poweron) override;
1.1 root 174:
1.1.1.2 root 175: // BusIO インタフェース
1.1.1.13 root 176: // (内蔵 ROM からのアクセス用に特別に public にしてある)
177: busdata WritePort(uint32 offset, uint32 data);
178: protected:
1.1.1.2 root 179: static const uint32 NPORT = 2;
1.1.1.13 root 180: busdata ReadPort(uint32 offset);
181: busdata PeekPort(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: // メモリアクセス
1.1.1.13 root 191: uint32 ReadMem(uint32 paddr);
192: void WriteMem(uint32 paddr, uint32 data);
193: uint32 PeekMem(uint32 paddr) const;
1.1 root 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.13 root 239: uint rmd_num {}; // 受信ディスクリプタ数
240: uint rmd_cur {}; // RMD 内の現在の注目インデックス
1.1.1.10 root 241: uint32 tmd_base {}; // 送信ディスクリプタの(SubRAMでの)開始アドレス
1.1.1.13 root 242: uint tmd_num {}; // 送信ディスクリプタ数
243: uint tmd_cur {}; // TMD 内の現在の注目インデックス
1.1.1.5 root 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:
1.1.1.14! root 267: Monitor *monitor {};
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: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.