-
Notifications
You must be signed in to change notification settings - Fork 2
/
w5100.c
342 lines (271 loc) · 9.45 KB
/
w5100.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/******************************************************************************
Copyright (c) 2015, Oliver Schmidt
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the <organization> nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL OLIVER SCHMIDT BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************************************************************/
// The W5100 has the undocumented feature to wrap around the Address Register
// on an Auto-Increment at the end of physical address space to its beginning.
//
// However, the only way to make use of that feature is to have only a single
// socket that uses all of the W5100 physical address space. But having only
// a single socket by defining SINGLE_SOCKET comes with downsides too:
//
// One mustn't call into IP65 network functions anymore after w5100_config().
// Additionally the program doesn't support 'W5100 Shared Access' anymore
// (https://github.com/a2retrosystems/uthernet2/wiki/W5100-Shared-Access).
#ifdef SINGLE_SOCKET
#define SOCK_REG(offset) (0x0400 | (offset))
#else // SINGLE_SOCKET
#define SOCK_REG(offset) (0x0500 | (offset))
#endif // SINGLE_SOCKET
#include <ip65.h>
#include "w5100.h"
#define MIN(a,b) (((a)<(b))?(a):(b))
static volatile uint8_t* w5100_mode;
static volatile uint8_t* w5100_addr_hi;
static volatile uint8_t* w5100_addr_lo;
volatile uint8_t* w5100_data;
static uint16_t addr_basis[2];
static uint16_t addr_limit[2];
static uint16_t addr_mask [2];
static void set_addr(uint16_t addr)
{
*w5100_addr_hi = addr >> 8;
*w5100_addr_lo = addr;
}
static uint8_t get_byte(uint16_t addr)
{
set_addr(addr);
return *w5100_data;
}
static void set_byte(uint16_t addr, uint8_t data)
{
set_addr(addr);
*w5100_data = data;
}
static uint16_t get_word(uint16_t addr)
{
set_addr(addr);
return *w5100_data << 8 | *w5100_data;
}
static void set_word(uint16_t addr, uint16_t data)
{
set_addr(addr);
*w5100_data = data >> 8;
*w5100_data = data;
}
static void set_quad(uint16_t addr, uint32_t data)
{
set_addr(addr);
*w5100_data = data;
*w5100_data = data >> 8;
*w5100_data = data >> 16;
*w5100_data = data >> 24;
}
bool w5100_init(uint8_t eth_init)
{
w5100_mode = (uint8_t*)(eth_init << 4 | 0xC084);
w5100_addr_hi = w5100_mode + 1;
w5100_addr_lo = w5100_mode + 2;
w5100_data = w5100_mode + 3;
// PPP Link Control Protocol Request Timer Register defaults to 0x28
// on a real W5100. However, AppleWin features a virtual W5100 that
// supports DNS offloading. On that virtual W5100, the (otherwise
// anyhow unused) register defaults to 0x00 as detection mechanism.
// https://github.com/a2retrosystems/uthernet2/wiki/Virtual-W5100-with-DNS
return get_byte(0x0028) == 0x00;
}
void w5100_config(void)
{
#ifdef SINGLE_SOCKET
// IP65 is inhibited so disable the W5100 Ping Block Mode.
*w5100_mode &= ~0x10;
#endif // SINGLE_SOCKET
// Source IP Address Register
set_quad(0x000F, cfg_ip);
// Subnet Mask Register
set_quad(0x0005, cfg_netmask);
// Gateway IP Address Register
set_quad(0x0001, cfg_gateway);
{
bool do_send;
for (do_send = false; do_send <= true; ++do_send)
{
static uint16_t reg[2] = {0x001A, // RX Memory Size Register
0x001B}; // TX Memory Size Register
static uint16_t addr[2] = {0x6000, // RX Memory
0x4000}; // TX Memory
static uint16_t size[4] = {0x0400, // 1KB Memory
0x0800, // 2KB Memory
0x1000, // 4KB Memory
0x2000}; // 8KB Memory
#ifdef SINGLE_SOCKET
set_byte(reg[do_send], 0x03);
// Set Socket 0 Memory Size to 8KB
addr_basis[do_send] = addr [do_send];
addr_limit[do_send] = addr_basis[do_send] + size[0x03];
addr_mask [do_send] = size[0x03] - 1;
#else // SINGLE_SOCKET
uint8_t sizes = get_byte(reg[do_send]);
// Get Socket 1 Memory Size
addr_basis[do_send] = addr [do_send] + size[sizes & 0x03];
addr_limit[do_send] = addr_basis[do_send] + size[sizes >> 2 & 0x03];
addr_mask [do_send] = size[sizes >> 2 & 0x03] - 1;
#endif // SINGLE_SOCKET
}
}
}
static bool w5100_connect(uint16_t port)
{
// Socket x Source Port Register
set_word(SOCK_REG(0x04), ip65_random_word());
// Socket x Destination Port Register
set_word(SOCK_REG(0x10), port);
// Socket x Command Register: OPEN
set_byte(SOCK_REG(0x01), 0x01);
// Socket x Status Register: SOCK_INIT ?
while (get_byte(SOCK_REG(0x03)) != 0x13)
{
if (input_check_for_abort_key())
{
return false;
}
}
// Socket x Command Register: CONNECT
set_byte(SOCK_REG(0x01), 0x04);
while (true)
{
// Socket x Status Register
switch (get_byte(SOCK_REG(0x03)))
{
case 0x00: return false; // Socket Status: SOCK_CLOSED
case 0x17: return true; // Socket Status: SOCK_ESTABLISHED
}
if (input_check_for_abort_key())
{
return false;
}
}
}
bool w5100_connect_addr(uint32_t addr, uint16_t port)
{
// Socket x Mode Register: TCP, Use No Delayed ACK
set_byte(SOCK_REG(0x00), 0x21);
// Socket x Destination IP Address Register
set_quad(SOCK_REG(0x0C), addr);
return w5100_connect(port);
}
bool w5100_connect_name(const char* name, uint8_t length, uint16_t port)
{
// Socket x Mode Register: TCP, Use No Delayed ACK, Use DNS Offloading
set_byte(SOCK_REG(0x00), 0x29);
// Socket x DNS name length
set_byte(SOCK_REG(0x2A), length);
// Socket x DNS name chars
while (length--)
{
*w5100_data = *name++;
}
return w5100_connect(port);
}
bool w5100_connected(void)
{
// Socket x Status Register: SOCK_ESTABLISHED ?
return get_byte(SOCK_REG(0x03)) == 0x17;
}
void w5100_disconnect(void)
{
// Socket x Command Register: Command Pending ?
while (get_byte(SOCK_REG(0x01)))
{
if (input_check_for_abort_key())
{
return;
}
}
// Socket x Command Register: DISCON
set_byte(SOCK_REG(0x01), 0x08);
}
uint16_t w5100_data_request(bool do_send)
{
// Socket x Command Register: Command Pending ?
if (get_byte(SOCK_REG(0x01)))
{
return 0;
}
{
uint16_t size = 0;
uint16_t prev_size;
// Reread of nonzero RX Received Size Register / TX Free Size Register
// until its value settles ...
// - is present in the WIZnet driver - getSn_RX_RSR() / getSn_TX_FSR()
// - was additionally tested on 6502 machines to be actually necessary
do
{
prev_size = size;
{
static uint16_t reg[2] = {SOCK_REG(0x26), // Socket x RX Received Size Register
SOCK_REG(0x20)}; // Socket x TX Free Size Register
size = get_word(reg[do_send]);
}
}
while (size != prev_size);
if (!size)
{
return 0;
}
{
static uint16_t reg[2] = {SOCK_REG(0x28), // Socket x RX Read Pointer Register
SOCK_REG(0x24)}; // Socket x TX Write Pointer Register
// Calculate and set physical address
uint16_t addr = get_word(reg[do_send]) & addr_mask [do_send]
| addr_basis[do_send];
set_addr(addr);
#ifdef SINGLE_SOCKET
// The W5100 has the undocumented feature to wrap around the Address Register
// on an Auto-Increment at the end of physical address space to its beginning.
return size;
#else // SINGLE_SOCKET
// Access to *w5100_data is limited both by ...
// - size of received / free space
// - end of physical address space
return MIN(size, addr_limit[do_send] - addr);
#endif // SINGLE_SOCKET
}
}
}
void w5100_data_commit(bool do_send, uint16_t size)
{
{
static uint16_t reg[2] = {SOCK_REG(0x28), // Socket x RX Read Pointer Register
SOCK_REG(0x24)}; // Socket x TX Write Pointer Register
set_word(reg[do_send], get_word(reg[do_send]) + size);
}
{
static uint8_t cmd[2] = {0x40, // Socket Command: RECV
0x20}; // Socket Command: SEND
// Socket x Command Register
set_byte(SOCK_REG(0x01), cmd[do_send]);
}
// Do NOT wait for command completion here, rather
// let W5100 operation overlap with 6502 operation
}