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