File: dev_le.c

package info (click to toggle)
mips64emul 0.2.4-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,260 kB
  • ctags: 11,173
  • sloc: ansic: 43,056; sh: 535; asm: 326; makefile: 58
file content (807 lines) | stat: -rw-r--r-- 21,738 bytes parent folder | download
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
/*
 *  Copyright (C) 2003-2004  Anders Gavare.  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  1. Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *  2. 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.
 *  3. The name of the author may not be used to endorse or promote products
 *     derived from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 THE AUTHOR OR CONTRIBUTORS 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.
 *   
 *
 *  $Id: dev_le.c,v 1.29 2005/01/09 01:55:25 debug Exp $
 *  
 *  LANCE ethernet, as used in DECstations.
 *
 *  This is based on "PMAD-AA TURBOchannel Ethernet Module Functional
 *  Specification". I've tried to keep symbol names in this file to what
 *  the specs use.
 *
 *  This is what the memory layout looks like on a DECstation 5000/200:
 *
 *	0x000000 - 0x0fffff	Ethernet SRAM buffer  (should be 128KB)
 *	0x100000 - 0x17ffff	LANCE registers
 *	0x1c0000 - 0x1fffff	Ethernet Diagnostic ROM and Station
 *				Address ROM
 *
 *  The length of the device is set to 0x1c0200, however, because Sprite
 *  tries to read TURBOchannel rom data from 0x1c03f0, and that is provided
 *  by the turbochannel device, not this device.
 *
 *  TODO:  Make sure that this device works with both NetBSD and Ultrix
 *         and on as many DECstation models as possible.
 *
 *  TODO:  Error conditions (such as when there are not enough receive
 *	   buffers) are not emulated yet.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "devices.h"
#include "memory.h"
#include "misc.h"
#include "net.h"

#include "if_lereg.h"


#define	LE_TICK_SHIFT		14

/*  #define LE_DEBUG  */
/*  #define debug fatal  */

extern int quiet_mode;

#define	LE_MODE_LOOP		4
#define	LE_MODE_DTX		2
#define	LE_MODE_DRX		1


#define	N_REGISTERS		4
#define	SRAM_SIZE		(128*1024)
#define	ROM_SIZE		32


struct le_data {
	int		irq_nr;

	uint64_t	buf_start;
	uint64_t	buf_end;
	int		len;

	uint8_t		rom[ROM_SIZE];

	int		reg_select;
	uint16_t	reg[N_REGISTERS];

	unsigned char	sram[SRAM_SIZE];

	/*  Initialization block:  */
	uint32_t	init_block_addr;

	uint16_t	mode;
	uint64_t	padr;	/*  MAC address  */
	uint64_t	ladrf;
	uint32_t	rdra;	/*  receive descriptor ring address  */
	int		rlen;	/*  nr of rx descriptors  */
	uint32_t	tdra;	/*  transmit descriptor ring address  */
	int		tlen;	/*  nr ot tx descriptors  */

	/*  Current rx and tx descriptor indices:  */
	int		rxp;
	int		txp;

	unsigned char	*tx_packet;
	int		tx_packet_len;

	unsigned char	*rx_packet;
	int		rx_packet_len;
	int		rx_packet_offset;
	int		rx_middle_bit;
};


/*
 *  le_read_16bit():
 *
 *  Read a 16-bit word from the SRAM.
 */
uint64_t le_read_16bit(struct le_data *d, int addr)
{
	/*  TODO: This is for little endian only  */
	int x = d->sram[addr & (SRAM_SIZE-1)] +
	       (d->sram[(addr+1) & (SRAM_SIZE-1)] << 8);
	return x;
}


/*
 *  le_write_16bit():
 *
 *  Write a 16-bit word to the SRAM.
 */
void le_write_16bit(struct le_data *d, int addr, uint16_t x)
{
	/*  TODO: This is for little endian only  */
	d->sram[addr & (SRAM_SIZE-1)] = x & 0xff;
	d->sram[(addr+1) & (SRAM_SIZE-1)] = (x >> 8) & 0xff;
}


/*
 *  le_chip_init():
 *
 *  Initialize data structures by reading an 'initialization block' from the
 *  SRAM.
 */
void le_chip_init(struct le_data *d)
{
	d->init_block_addr = (d->reg[1] & 0xffff) + ((d->reg[2] & 0xff) << 16);
	if (d->init_block_addr & 1)
		fatal("[ le: WARNING! initialization block address not word aligned? ]\n");

	debug("[ le: d->init_block_addr = 0x%06x ]\n", d->init_block_addr);

	d->mode = le_read_16bit(d, d->init_block_addr + 0);
	d->padr = le_read_16bit(d, d->init_block_addr + 2);
	d->padr += (le_read_16bit(d, d->init_block_addr + 4) << 16);
	d->padr += (le_read_16bit(d, d->init_block_addr + 6) << 32);
	d->ladrf = le_read_16bit(d, d->init_block_addr + 8);
	d->ladrf += (le_read_16bit(d, d->init_block_addr + 10) << 16);
	d->ladrf += (le_read_16bit(d, d->init_block_addr + 12) << 32);
	d->ladrf += (le_read_16bit(d, d->init_block_addr + 14) << 48);
	d->rdra = le_read_16bit(d, d->init_block_addr + 16);
	d->rdra += ((le_read_16bit(d, d->init_block_addr + 18) & 0xff) << 16);
	d->rlen = 1 << ((le_read_16bit(d, d->init_block_addr + 18) >> 13) & 7);
	d->tdra = le_read_16bit(d, d->init_block_addr + 20);
	d->tdra += ((le_read_16bit(d, d->init_block_addr + 22) & 0xff) << 16);
	d->tlen = 1 << ((le_read_16bit(d, d->init_block_addr + 22) >> 13) & 7);

	debug("[ le: DEBUG: mode              %04x ]\n", d->mode);
	debug("[ le: DEBUG: padr  %016llx ]\n", (long long)d->padr);
	debug("[ le: DEBUG: ladrf %016llx ]\n", (long long)d->ladrf);
	debug("[ le: DEBUG: rdra            %06llx ]\n", d->rdra);
	debug("[ le: DEBUG: rlen               %3i ]\n", d->rlen);
	debug("[ le: DEBUG: tdra            %06llx ]\n", d->tdra);
	debug("[ le: DEBUG: tlen               %3i ]\n", d->tlen);

	/*  Set TXON and RXON, unless they are disabled by 'mode':  */
	if (d->mode & LE_MODE_DTX)
		d->reg[0] &= ~LE_TXON;
	else
		d->reg[0] |= LE_TXON;

	if (d->mode & LE_MODE_DRX)
		d->reg[0] &= ~LE_RXON;
	else
		d->reg[0] |= LE_RXON;

	/*  Go to the start of the descriptor rings:  */
	d->rxp = d->txp = 0;

	/*  Set IDON and reset the INIT bit when we are done.  */
	d->reg[0] |= LE_IDON;
	d->reg[0] &= ~LE_INIT;

	/*  Free any old packets:  */
	if (d->tx_packet != NULL)
		free(d->tx_packet);
	d->tx_packet = NULL;
	d->tx_packet_len = 0;

	if (d->rx_packet != NULL)
		free(d->rx_packet);
	d->rx_packet = NULL;
	d->rx_packet_len = 0;
	d->rx_packet_offset = 0;
	d->rx_middle_bit = 0;
}


/*
 *  le_tx():
 *
 *  Check the transmitter descriptor ring for buffers that are owned by the
 *  Lance chip (that is, buffers that are to be transmitted).
 *
 *  This routine should only be called if TXON is enabled.
 */
void le_tx(struct le_data *d)
{
	int start_txp = d->txp;
	uint16_t tx_descr[4];
	int stp, enp, i, cur_packet_offset;
	uint32_t bufaddr, buflen;

	do {
		/*  Load the 8 descriptor bytes:  */
		tx_descr[0] = le_read_16bit(d, d->tdra + d->txp*8 + 0);
		tx_descr[1] = le_read_16bit(d, d->tdra + d->txp*8 + 2);
		tx_descr[2] = le_read_16bit(d, d->tdra + d->txp*8 + 4);
		tx_descr[3] = le_read_16bit(d, d->tdra + d->txp*8 + 6);

		bufaddr = tx_descr[0] + ((tx_descr[1] & 0xff) << 16);
		stp = tx_descr[1] & LE_STP? 1 : 0;
		enp = tx_descr[1] & LE_ENP? 1 : 0;
		buflen = 4096 - (tx_descr[2] & 0xfff);

		/*
		 *  Check the OWN bit. If it is zero, then this buffer is
		 *  not ready to be transmitted yet.  Also check the '1111'
		 *  mark, and make sure that byte-count is reasonable.
		 */
		if (!(tx_descr[1] & LE_OWN))
			return;
		if ((tx_descr[2] & 0xf000) != 0xf000)
			return;
		if (buflen < 12 || buflen > 1900)
			return;

		debug("[ le: tx descr %3i DUMP: 0x%04x 0x%04x 0x%04x 0x%04x => addr=0x%06x, len=%i bytes, STP=%i ENP=%i ]\n",
		    d->txp, tx_descr[0], tx_descr[1], tx_descr[2], tx_descr[3],
		    bufaddr, buflen, stp, enp);

		if (d->tx_packet == NULL && !stp) {
			fatal("[ le: le_tx(): !stp but tx_packet == NULL ]\n");
			return;
		}

		if (d->tx_packet != NULL && stp) {
			fatal("[ le: le_tx(): stp but tx_packet != NULL ]\n");
			free(d->tx_packet);
			d->tx_packet = NULL;
			d->tx_packet_len = 0;
		}

		/*  Where to write to in the tx_packet:  */
		cur_packet_offset = d->tx_packet_len;

		/*  Start of a new packet:  */
		if (stp) {
			d->tx_packet_len = buflen;
			d->tx_packet = malloc(buflen);
			if (d->tx_packet == NULL) {
				fprintf(stderr, "out of memory (1) in le_tx()\n");
				exit(1);
			}
		} else {
			d->tx_packet_len += buflen;
			d->tx_packet = realloc(d->tx_packet, d->tx_packet_len);
			if (d->tx_packet == NULL) {
				fprintf(stderr, "out of memory (2) in le_tx()\n");
				exit(1);
			}
		}

		/*  Copy data from SRAM into the tx packet:  */
		for (i=0; i<buflen; i++) {
			unsigned char ch;
			ch = d->sram[(bufaddr + i) & (SRAM_SIZE-1)];
			d->tx_packet[cur_packet_offset + i] = ch;
		}

		/*
		 *  Is this the last buffer in a packet? Then transmit
		 *  it, cause an interrupt, and free the memory used by
		 *  the packet.
		 */
		if (enp) {
			net_ethernet_tx(d, d->tx_packet, d->tx_packet_len);

			free(d->tx_packet);
			d->tx_packet = NULL;
			d->tx_packet_len = 0;

			d->reg[0] |= LE_TINT;
		}

		/*  Clear the OWN bit:  */
		tx_descr[1] &= ~LE_OWN;

		/*  Write back the descriptor to SRAM:  */
		le_write_16bit(d, d->tdra + d->txp*8 + 2, tx_descr[1]);
		le_write_16bit(d, d->tdra + d->txp*8 + 4, tx_descr[2]);
		le_write_16bit(d, d->tdra + d->txp*8 + 6, tx_descr[3]);

		/*  Go to the next descriptor:  */
		d->txp ++;
		if (d->txp >= d->tlen)
			d->txp = 0;
	} while (d->txp != start_txp);

	/*  We are here if all descriptors were taken care of.  */
	fatal("[ le: le_tx(): all TX descriptors used up? ]\n");
}


/*
 *  le_rx():
 *
 *  This routine should only be called if RXON is enabled.
 */
void le_rx(struct le_data *d)
{
	int i, start_rxp = d->rxp;
	uint16_t rx_descr[4];
	uint32_t bufaddr, buflen;

	do {
		if (d->rx_packet == NULL)
			return;

		/*  Load the 8 descriptor bytes:  */
		rx_descr[0] = le_read_16bit(d, d->rdra + d->rxp*8 + 0);
		rx_descr[1] = le_read_16bit(d, d->rdra + d->rxp*8 + 2);
		rx_descr[2] = le_read_16bit(d, d->rdra + d->rxp*8 + 4);
		rx_descr[3] = le_read_16bit(d, d->rdra + d->rxp*8 + 6);

		bufaddr = rx_descr[0] + ((rx_descr[1] & 0xff) << 16);
		buflen = 4096 - (rx_descr[2] & 0xfff);

		/*
		 *  Check the OWN bit. If it is zero, then this buffer is
		 *  not ready to receive data yet.  Also check the '1111'
		 *  mark, and make sure that byte-count is reasonable.
		 */
		if (!(rx_descr[1] & LE_OWN))
			return;
		if ((rx_descr[2] & 0xf000) != 0xf000)
			return;
		if (buflen < 12 || buflen > 1900)
			return;

		debug("[ le: rx descr %3i DUMP: 0x%04x 0x%04x 0x%04x 0x%04x => addr=0x%06x, len=%i bytes ]\n",
		    d->rxp, rx_descr[0], rx_descr[1], rx_descr[2], rx_descr[3],
		    bufaddr, buflen);

		/*  Copy data from the packet into SRAM:  */
		for (i=0; i<buflen; i++) {
			if (d->rx_packet_offset + i >= d->rx_packet_len)
				break;
			d->sram[(bufaddr + i) & (SRAM_SIZE-1)] =
			    d->rx_packet[d->rx_packet_offset + i];
		}

		/*  Here, i is the number of bytes copied.  */
		d->rx_packet_offset += i;

		/*  Set the ENP bit if this was the end of a packet:  */
		if (d->rx_packet_offset >= d->rx_packet_len) {
			rx_descr[1] |= LE_ENP;

			/*
			 *  NOTE:  The Lance documentation that I have read
			 *  says _NOTHING_ about the length being 4 more than
			 *  the length of the data.  You can guess how
			 *  surprised I was when I saw the following in
			 *  NetBSD (dev/ic/am7990.c):
			 *
			 *	lance_read(sc, LE_RBUFADDR(sc, bix),
			 *		(int)rmd.rmd3 - 4);
			 */
			rx_descr[3] &= ~0xfff;
			rx_descr[3] |= d->rx_packet_len + 4;

			free(d->rx_packet);
			d->rx_packet = NULL;
			d->rx_packet_len = 0;
			d->rx_packet_offset = 0;
			d->rx_middle_bit = 0;

			d->reg[0] |= LE_RINT;
		}

		/*  Set the STP bit if this was the start of a packet:  */
		if (!d->rx_middle_bit) {
			rx_descr[1] |= LE_STP;

			/*  Are we continuing on this packet?  */
			if (d->rx_packet != NULL)
				d->rx_middle_bit = 1;
		}

		/*  Clear the OWN bit:  */
		rx_descr[1] &= ~LE_OWN;

		/*  Write back the descriptor to SRAM:  */
		le_write_16bit(d, d->rdra + d->rxp*8 + 2, rx_descr[1]);
		le_write_16bit(d, d->rdra + d->rxp*8 + 4, rx_descr[2]);
		le_write_16bit(d, d->rdra + d->rxp*8 + 6, rx_descr[3]);

		/*  Go to the next descriptor:  */
		d->rxp ++;
		if (d->rxp >= d->rlen)
			d->rxp = 0;
	} while (d->rxp != start_rxp);

	/*  We are here if all descriptors were taken care of.  */
	fatal("[ le: le_rx(): all RX descriptors used up? ]\n");
}


/*
 *  le_register_fix():
 */
void le_register_fix(struct le_data *d)
{
	/*  Should the chip be initialized with a new Initialization block?  */
	if (d->reg[0] & LE_INIT)
		le_chip_init(d);

	/*
	 *  If the receiver is on:
	 *  If there is a current rx_packet, try to receive it into the
	 *  Lance buffers.  Then try to receive any additional packets.
	 */
	if (d->reg[0] & LE_RXON) {
		do {
			if (d->rx_packet != NULL)
				/*  Try to receive the packet:  */
				le_rx(d);

			if (d->rx_packet != NULL)
				/*  If the packet wasn't fully received, 
				    then abort for now.  */
				break;

			if (d->rx_packet == NULL && net_ethernet_rx_avail(d))
				net_ethernet_rx(d, &d->rx_packet,
				    &d->rx_packet_len);
		} while (d->rx_packet != NULL);
	}

	/*  If the transmitter is on, check for outgoing buffers:  */
	if (d->reg[0] & LE_TXON)
		le_tx(d);

	/*  SERR should be the OR of BABL, CERR, MISS, and MERR:  */
	d->reg[0] &= ~LE_SERR;
	if (d->reg[0] & (LE_BABL | LE_CERR | LE_MISS | LE_MERR))
		d->reg[0] |= LE_SERR;

	/*  INTR should be the OR of BABL, MISS, MERR, RINT, TINT, IDON:  */
	d->reg[0] &= ~LE_INTR;
	if (d->reg[0] & (LE_BABL | LE_MISS | LE_MERR | LE_RINT |
	    LE_TINT | LE_IDON))
		d->reg[0] |= LE_INTR;

	/*  The MERR bit clears some bits:  */
	if (d->reg[0] & LE_MERR)
		d->reg[0] &= ~(LE_RXON | LE_TXON);

	/*  The STOP bit clears a lot of stuff:  */
#if 0
	/*  According to the LANCE manual: (doesn't work with Ultrix)  */
	if (d->reg[0] & LE_STOP)
		d->reg[0] &= ~(LE_SERR | LE_BABL | LE_CERR | LE_MISS | LE_MERR
		    | LE_RINT | LE_TINT | LE_IDON | LE_INTR | LE_INEA
		    | LE_RXON | LE_TXON | LE_TDMD);
#else
	/*  Works with Ultrix:  */
	if (d->reg[0] & LE_STOP)
		d->reg[0] &= ~(LE_IDON);
#endif
}


/*
 *  dev_le_tick():
 */
void dev_le_tick(struct cpu *cpu, void *extra)
{
	struct le_data *d = (struct le_data *) extra;

	le_register_fix(d);

	if (d->reg[0] & LE_INTR && d->reg[0] & LE_INEA)
		cpu_interrupt(cpu, d->irq_nr);
	else
		cpu_interrupt_ack(cpu, d->irq_nr);
}


/*
 *  le_register_write():
 *
 *  This function is called when the value 'x' is written to register 'r'.
 */
void le_register_write(struct le_data *d, int r, uint32_t x)
{
	switch (r) {
	case 0:	/*  CSR0:  */
		/*  Some bits are write-one-to-clear:  */
		if (x & LE_BABL)
			d->reg[r] &= ~LE_BABL;
		if (x & LE_CERR)
			d->reg[r] &= ~LE_CERR;
		if (x & LE_MISS)
			d->reg[r] &= ~LE_MISS;
		if (x & LE_MERR)
			d->reg[r] &= ~LE_MERR;
		if (x & LE_RINT)
			d->reg[r] &= ~LE_RINT;
		if (x & LE_TINT)
			d->reg[r] &= ~LE_TINT;
		if (x & LE_IDON)
			d->reg[r] &= ~LE_IDON;

		/*  Some bits are write-only settable, not clearable:  */
		if (x & LE_TDMD)
			d->reg[r] |= LE_TDMD;
		if (x & LE_STRT) {
			d->reg[r] |= LE_STRT;
			d->reg[r] &= ~LE_STOP;
		}
		if (x & LE_INIT) {
			if (!(d->reg[r] & LE_STOP))
				fatal("[ le: attempt to INIT before STOPped! ]\n");
			d->reg[r] |= LE_INIT;
			d->reg[r] &= ~LE_STOP;
		}
		if (x & LE_STOP) {
			d->reg[r] |= LE_STOP;
			/*  STOP takes precedence over STRT and INIT:  */
			d->reg[r] &= ~(LE_STRT | LE_INIT);
		}

		/*  Some bits get through, both settable and clearable:  */
		d->reg[r] &= ~LE_INEA;
		d->reg[r] |= (x & LE_INEA);
		break;

	default:
		/*  CSR1, CSR2, and CSR3:  */
		d->reg[r] = x;
	}
}


/*
 *  dev_le_sram_access():
 */
int dev_le_sram_access(struct cpu *cpu, struct memory *mem, uint64_t relative_addr,
	unsigned char *data, size_t len, int writeflag, void *extra)
{
	uint64_t idata = 0;
	int i, retval;
	struct le_data *d = extra;

	idata = memory_readmax64(cpu, data, len);

#ifdef LE_DEBUG
	if (writeflag == MEM_WRITE)
		fatal("[ le_sram: write to addr 0x%06x: 0x%08x ]\n",
		    relative_addr, idata);
#endif

	/*  Read/write of the SRAM:  */
	if (relative_addr < SRAM_SIZE && relative_addr + len <= SRAM_SIZE) {
		if (writeflag == MEM_READ) {
			memcpy(data, d->sram + relative_addr, len);
			if (!quiet_mode) {
				debug("[ le: read from SRAM offset 0x%05x:",
				    relative_addr);
				for (i=0; i<len; i++)
					debug(" %02x", data[i]);
				debug(" ]\n");
			}
			retval = 9;	/*  9 cycles  */
		} else {
			memcpy(d->sram + relative_addr, data, len);
			if (!quiet_mode) {
				debug("[ le: write to SRAM offset 0x%05x:",
				    relative_addr);
				for (i=0; i<len; i++)
					debug(" %02x", data[i]);
				debug(" ]\n");
			}
			retval = 6;	/*  6 cycles  */
		}
		return retval;
	}

	return 0;
}


/*
 *  dev_le_access():
 */
int dev_le_access(struct cpu *cpu, struct memory *mem, uint64_t relative_addr,
	unsigned char *data, size_t len, int writeflag, void *extra)
{
	uint64_t idata = 0, odata = 0;
	int i, retval = 1;
	struct le_data *d = extra;

	idata = memory_readmax64(cpu, data, len);

#ifdef LE_DEBUG
	if (writeflag == MEM_WRITE)
		fatal("[ le: write to addr 0x%06x: 0x%08x ]\n",
		    relative_addr, idata);
#endif

	/*  The rest of this code was written before the SRAM
	    was separated out for bintrans optimization.  */
	relative_addr += 0x100000;

	/*  Read from station's ROM (ethernet address):  */
	if (relative_addr >= 0x1c0000 && relative_addr <= 0x1fffff) {
		i = (relative_addr & 0xff) / 4;
		i = d->rom[i & (ROM_SIZE-1)];

		if (writeflag == MEM_READ) {
			odata = (i << 24) + (i << 16) + (i << 8) + i;
		} else {
			fatal("[ le: WRITE to ethernet addr (%08lx):",
			    (long)relative_addr);
			for (i=0; i<len; i++)
				fatal(" %02x", data[i]);
			fatal(" ]\n");
		}

		retval = 13;	/*  13 cycles  */
		goto do_return;
	}


	switch (relative_addr) {

	/*  Register read/write:  */
	case 0x100000:
		if (writeflag==MEM_READ) {
			odata = d->reg[d->reg_select];
			if (!quiet_mode)
				debug("[ le: read from register 0x%02x: 0x%02x ]\n",
				    d->reg_select, (int)odata);
			/*
			 *  A read from csr1..3 should return "undefined"
			 *  result if the stop bit is set.  However, Ultrix
			 *  seems to do just that, so let's _not_ print
			 *  a warning here.
			 */
		} else {
			if (!quiet_mode)
				debug("[ le: write to register 0x%02x: 0x%02x ]\n",
				    d->reg_select, (int)idata);
			/*
			 *  A write to from csr1..3 when the stop bit is
			 *  set should be ignored. However, Ultrix writes
			 *  even if the stop bit is set, so let's _not_
			 *  print a warning about it.
			 */
			le_register_write(d, d->reg_select, idata);
		}
		break;

	/*  Register select:  */
	case 0x100004:
		if (writeflag==MEM_READ) {
			odata = d->reg_select;
			if (!quiet_mode)
				debug("[ le: read from register select: 0x%02x ]\n",
				    (int)odata);
		} else {
			if (!quiet_mode)
				debug("[ le: write to register select: 0x%02x ]\n",
				    (int)idata);
			d->reg_select = idata & (N_REGISTERS - 1);
			if (idata >= N_REGISTERS)
				fatal("[ le: WARNING! register select %i (max is %i) ]\n",
				    idata, N_REGISTERS - 1);
		}
		break;

	default:
		if (writeflag==MEM_READ) {
			fatal("[ le: read from UNIMPLEMENTED addr 0x%06x ]\n",
			    relative_addr);
		} else {
			fatal("[ le: write to UNIMPLEMENTED addr 0x%06x: 0x%08x ]\n",
			    relative_addr, (int)idata);
		}
	}


do_return:

	if (writeflag == MEM_READ) {
		memory_writemax64(cpu, data, len, odata);
#ifdef LE_DEBUG
		fatal("[ le: read from addr 0x%06x: 0x%08x ]\n",
		    relative_addr, odata);
#endif
	}

	dev_le_tick(cpu, extra);

	return retval;
}


/*
 *  dev_le_init():
 */
void dev_le_init(struct cpu *cpu, struct memory *mem, uint64_t baseaddr,
	uint64_t buf_start, uint64_t buf_end, int irq_nr, int len)
{
	struct le_data *d = malloc(sizeof(struct le_data));
	if (d == NULL) {
		fprintf(stderr, "out of memory\n");
		exit(1);
	}

	memset(d, 0, sizeof(struct le_data));
	d->irq_nr    = irq_nr;

	/*  TODO:  Are these actually used yet?  */
	d->len       = len;
	d->buf_start = buf_start;
	d->buf_end   = buf_end;

	/*  Initial register contents:  */
	d->reg[0] = LE_STOP;

	d->tx_packet = NULL;
	d->rx_packet = NULL;

	/*  ROM (including the MAC address):  */
	d->rom[0] = 0x10;
	d->rom[1] = 0x20;
	d->rom[2] = 0x30;
	d->rom[3] = 0x40;
	d->rom[4] = 0x50;
	d->rom[5] = 0x60;

	/*
	 *  NOTE:  According to the Lance documentation, the low order bit of
	 *  a physical MAC address should be clear.  However, NetBSD and
	 *  Linux drop packets if the _first_ byte's lowest bit is not zero.
	 */
	d->rom[0] &= ~1;
	d->rom[5] &= ~1;

	/*  Copies of the MAC address and a test pattern:  */
	d->rom[10] = d->rom[21] = d->rom[5];
	d->rom[11] = d->rom[20] = d->rom[4];
	d->rom[12] = d->rom[19] = d->rom[3];
	d->rom[7] =  d->rom[8]  = d->rom[23] =
		     d->rom[13] = d->rom[18] = d->rom[2];
	d->rom[6] =  d->rom[9]  = d->rom[22] =
		     d->rom[14] = d->rom[17] = d->rom[1];
	d->rom[15] = d->rom[16] = d->rom[0];
	d->rom[24] = d->rom[28] = 0xff;
	d->rom[25] = d->rom[29] = 0x00;
	d->rom[26] = d->rom[30] = 0x55;
	d->rom[27] = d->rom[31] = 0xaa;

	memory_device_register(mem, "le_sram", baseaddr,
	    SRAM_SIZE, dev_le_sram_access, (void *)d,
	    MEM_BINTRANS_OK | MEM_BINTRANS_WRITE_OK, d->sram);

	memory_device_register(mem, "le", baseaddr + 0x100000,
	    len - 0x100000,
	    dev_le_access, (void *)d, MEM_DEFAULT, NULL);

	cpu_add_tickfunction(cpu, dev_le_tick, d, LE_TICK_SHIFT);
}