OpenCores
URL https://opencores.org/ocsvn/ethmac/ethmac/trunk

Subversion Repositories ethmac

[/] [ethmac/] [trunk/] [rtl/] [verilog/] [eth_wishbone.v] - Blame information for rev 38

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 38 mohor
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3
////  eth_wishbone.v                                              ////
4
////                                                              ////
5
////  This file is part of the Ethernet IP core project           ////
6
////  http://www.opencores.org/projects/ethmac/                   ////
7
////                                                              ////
8
////  Author(s):                                                  ////
9
////      - Igor Mohor (igorM@opencores.org)                      ////
10
////                                                              ////
11
////  All additional information is avaliable in the Readme.txt   ////
12
////  file.                                                       ////
13
////                                                              ////
14
//////////////////////////////////////////////////////////////////////
15
////                                                              ////
16
//// Copyright (C) 2001 Authors                                   ////
17
////                                                              ////
18
//// This source file may be used and distributed without         ////
19
//// restriction provided that this copyright statement is not    ////
20
//// removed from the file and that any derivative work contains  ////
21
//// the original copyright notice and the associated disclaimer. ////
22
////                                                              ////
23
//// This source file is free software; you can redistribute it   ////
24
//// and/or modify it under the terms of the GNU Lesser General   ////
25
//// Public License as published by the Free Software Foundation; ////
26
//// either version 2.1 of the License, or (at your option) any   ////
27
//// later version.                                               ////
28
////                                                              ////
29
//// This source is distributed in the hope that it will be       ////
30
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
31
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
32
//// PURPOSE.  See the GNU Lesser General Public License for more ////
33
//// details.                                                     ////
34
////                                                              ////
35
//// You should have received a copy of the GNU Lesser General    ////
36
//// Public License along with this source; if not, download it   ////
37
//// from http://www.opencores.org/lgpl.shtml                     ////
38
////                                                              ////
39
//////////////////////////////////////////////////////////////////////
40
//
41
// CVS Revision History
42
//
43
// $Log: not supported by cvs2svn $
44
//
45
//
46
//
47
 
48
 
49
`include "eth_defines.v"
50
`include "timescale.v"
51
 
52
 
53
module eth_wishbone
54
   (
55
 
56
    // WISHBONE common
57
    WB_CLK_I, WB_RST_I, WB_DAT_I, WB_DAT_O,
58
 
59
    // WISHBONE slave
60
                WB_ADR_I, WB_SEL_I, WB_WE_I, WB_ACK_O,
61
                WB_REQ_O, WB_ACK_I, WB_ND_O, WB_RD_O, BDCs,
62
 
63
    //TX
64
    MTxClk, TxStartFrm, TxEndFrm, TxUsedData, TxData, StatusIzTxEthMACModula,
65
    TxRetry, TxAbort, TxUnderRun, TxDone, TPauseRq, TxPauseTV, PerPacketCrcEn,
66
    PerPacketPad,
67
 
68
    //RX
69
    MRxClk, RxData, RxValid, RxStartFrm, RxEndFrm,
70
 
71
    // Register
72
    r_TxEn, r_RxEn, r_TxBDNum, r_DmaEn, TX_BD_NUM_Wr,
73
 
74
    WillSendControlFrame, TxCtrlEndFrm,
75
 
76
    // Interrupts
77
    TxB_IRQ, TxE_IRQ, RxB_IRQ, RxF_IRQ, Busy_IRQ
78
 
79
                );
80
 
81
 
82
parameter Tp = 1;
83
 
84
// WISHBONE common
85
input           WB_CLK_I;       // WISHBONE clock
86
input           WB_RST_I;       // WISHBONE reset
87
input  [31:0]   WB_DAT_I;       // WISHBONE data input
88
output [31:0]   WB_DAT_O;       // WISHBONE data output
89
 
90
// WISHBONE slave
91
input   [9:2]   WB_ADR_I;       // WISHBONE address input
92
input   [3:0]   WB_SEL_I;       // WISHBONE byte select input
93
input           WB_WE_I;        // WISHBONE write enable input
94
input           BDCs;           // Buffer descriptors are selected
95
output          WB_ACK_O;       // WISHBONE acknowledge output
96
 
97
// DMA
98
input   [1:0]   WB_ACK_I;       // DMA acknowledge input
99
output  [1:0]   WB_REQ_O;       // DMA request output
100
output  [1:0]   WB_ND_O;        // DMA force new descriptor output
101
output          WB_RD_O;        // DMA restart descriptor output
102
 
103
// Tx
104
input           MTxClk;         // Transmit clock (from PHY)
105
input           TxUsedData;     // Transmit packet used data
106
input  [15:0]   StatusIzTxEthMACModula;
107
input           TxRetry;        // Transmit packet retry
108
input           TxAbort;        // Transmit packet abort
109
input           TxDone;         // Transmission ended
110
output          TxStartFrm;     // Transmit packet start frame
111
output          TxEndFrm;       // Transmit packet end frame
112
output  [7:0]   TxData;         // Transmit packet data byte
113
output          TxUnderRun;     // Transmit packet under-run
114
output          PerPacketCrcEn; // Per packet crc enable
115
output          PerPacketPad;   // Per packet pading
116
output          TPauseRq;       // Tx PAUSE control frame
117
output [15:0]   TxPauseTV;      // PAUSE timer value
118
input           WillSendControlFrame;
119
input           TxCtrlEndFrm;
120
 
121
// Rx
122
input           MRxClk;         // Receive clock (from PHY)
123
input   [7:0]   RxData;         // Received data byte (from PHY)
124
input           RxValid;        // 
125
input           RxStartFrm;     // 
126
input           RxEndFrm;       // 
127
 
128
//Register
129
input           r_TxEn;         // Transmit enable
130
input           r_RxEn;         // Receive enable
131
input   [7:0]   r_TxBDNum;      // Receive buffer descriptor number
132
input           r_DmaEn;        // DMA enable
133
input           TX_BD_NUM_Wr;   // RxBDNumber written
134
 
135
// Interrupts
136
output TxB_IRQ;
137
output TxE_IRQ;
138
output RxB_IRQ;
139
output RxF_IRQ;
140
output Busy_IRQ;
141
 
142
reg             WB_REQ_O_RX;
143
reg             WB_ND_O_TX;     // New descriptor
144
reg             WB_RD_O;        // Restart descriptor
145
 
146
reg             TxStartFrm;
147
reg             TxEndFrm;
148
reg     [7:0]   TxData;
149
 
150
reg             TxUnderRun;
151
reg             TPauseRq;
152
reg             TxPauseRq;
153
 
154
reg             RxStartFrm_wb;
155
reg     [31:0]  RxData_wb;
156
reg             RxDataValid_wb;
157
reg             RxEndFrm_wb;
158
 
159
reg     [7:0]   BDAddress;    // BD address for access from MAC side
160
reg             BDRead_q;
161
 
162
reg             TxBDRead;
163
reg             TxDataRead;
164
reg             TxStatusWrite;
165
 
166
reg     [1:0]   TxValidBytesLatched;
167
reg             TxEndFrm_wbLatched;
168
 
169
reg    [15:0]   TxLength;
170
reg    [31:0]   TxStatus;
171
 
172
reg    [15:0]   RxStatus;
173
 
174
reg             TxStartFrm_wb;
175
reg             TxRetry_wb;
176
reg             GetNewTxData_wb;
177
reg             TxDone_wb;
178
reg             TxAbort_wb;
179
 
180
 
181
reg             TxStartFrmRequest;
182
reg    [31:0]   TxDataLatched_wb;
183
 
184
reg             RxStatusWriteOccured;
185
 
186
reg             TxRestart_wb_q;
187
reg             TxDone_wb_q;
188
reg             TxAbort_wb_q;
189
reg             RxBDReady;
190
reg             TxBDReady;
191
 
192
reg             RxBDRead;
193
reg             RxStatusWrite;
194
reg             WbWriteError;
195
 
196
reg    [31:0]   TxDataLatched;
197
reg     [1:0]   TxByteCnt;
198
reg             LastWord;
199
reg             GetNewTxData;
200
reg             TxRetryLatched;
201
 
202
reg             Div2;
203
reg             Flop;
204
 
205
reg             BlockingTxStatusWrite;
206
reg             TxStatusWriteOccured;
207
reg             BlockingTxBDRead;
208
 
209
reg             GetNewTxData_wb_latched;
210
 
211
reg             NewTxDataAvaliable_wb;
212
 
213
reg             TxBDAccessed;
214
 
215
reg     [7:0]   TxBDAddress;
216
reg     [7:0]   RxBDAddress;
217
 
218
reg             GotDataSync1;
219
reg             GotDataSync2;
220
wire            TPauseRqSync2;
221
wire             GotDataSync3;
222
reg             GotData;
223
reg             SyncGetNewTxData_wb1;
224
reg             SyncGetNewTxData_wb2;
225
reg             SyncGetNewTxData_wb3;
226
reg             TxDoneSync1;
227
reg             TxDoneSync2;
228
wire             TxDoneSync3;
229
reg             TxRetrySync1;
230
reg             TxRetrySync2;
231
wire            TxRetrySync3;
232
reg             TxAbortSync1;
233
reg             TxAbortSync2;
234
wire            TxAbortSync3;
235
 
236
reg             TxAbort_q;
237
reg             TxDone_q;
238
reg             TxRetry_q;
239
reg             TxUsedData_q;
240
 
241
reg    [31:0]   RxDataLatched2;
242
reg    [15:0]   RxDataLatched1;
243
reg     [1:0]   RxValidBytes;
244
reg     [1:0]   RxByteCnt;
245
reg             LastByteIn;
246
reg             ShiftWillEnd;
247
 
248
reg             StartShifting;
249
reg             Shifting_wb_Sync1;
250
reg             Shifting_wb_Sync2;
251
reg             LatchNow_wb;
252
 
253
reg             ShiftEndedSync1;
254
reg             ShiftEndedSync2;
255
reg             ShiftEndedSync3;
256
wire            ShiftEnded;
257
 
258
reg             RxStartFrmSync1;
259
reg             RxStartFrmSync2;
260
wire            RxStartFrmSync3;
261
 
262
reg             DMACycleFinishedTx_q;
263
reg             DataNotAvaliable;
264
 
265
reg             ClearTxBDReadySync1;
266
reg             ClearTxBDReadySync2;
267
reg             ClearTxBDReady;
268
 
269
reg             TxCtrlEndFrm_wbSync1;
270
reg             TxCtrlEndFrm_wbSync2;
271
wire            TxCtrlEndFrm_wbSync3;
272
reg             TxCtrlEndFrm_wb;
273
 
274
wire    [15:0]  TxPauseTV;
275
wire            ResetDataNotAvaliable;
276
wire            SetDataNotAvaliable;
277
wire            DWord;                      // Only 32-bit accesses are valid
278
wire            BDWe;                       // BD Write Enable for access from WISHBONE side
279
wire            BDRead;                     // BD Read access from WISHBONE side
280
wire   [31:0]   BDDataIn;                   // BD data in
281
wire   [31:0]   BDDataOut;                  // BD data out
282
 
283
wire            TxEndFrm_wb;
284
 
285
wire            DMACycleFinishedTx;
286
wire            BDStatusWrite;
287
 
288
wire            TxEn;
289
wire            RxEn;
290
wire            TxRestartPulse;
291
wire            TxDonePulse;
292
wire            TxAbortPulse;
293
 
294
wire            StartRxBDRead;
295
wire            ResetRxBDRead;
296
wire            StartRxStatusWrite;
297
 
298
wire            ResetShifting_wb;
299
wire            StartShifting_wb;
300
wire            DMACycleFinishedRx;
301
 
302
wire   [31:0]   WB_BDDataOut;
303
 
304
wire            StartTxBDRead;
305
wire            StartTxDataRead;
306
wire            ResetTxDataRead;
307
wire            StartTxStatusWrite;
308
wire            ResetTxStatusWrite;
309
 
310
wire            TxIRQEn;
311
wire            WrapTxStatusBit;
312
 
313
wire            WrapRxStatusBit;
314
 
315
wire    [1:0]   TxValidBytes;
316
 
317
wire    [7:0]   TempTxBDAddress;
318
wire    [7:0]   TempRxBDAddress;
319
 
320
wire   [15:0]   RxLength;
321
wire   [15:0]   NewRxStatus;
322
 
323
wire            SetGotData;
324
wire            ResetGotData;
325
wire            GotDataEvaluate;
326
wire            ResetSyncGetNewTxData_wb;
327
wire            ResetTxDoneSync;
328
wire            ResetTxRetrySync;
329
wire            ResetTxAbortSync;
330
wire            SetSyncGetNewTxData_wb;
331
 
332
wire            SetTxAbortSync;
333
wire            ResetShiftEnded;
334
wire            ResetRxStartFrmSync1;
335
wire            StartShiftEnded;
336
wire            StartRxStartFrmSync1;
337
 
338
wire            SetClearTxBDReady;
339
wire            ResetClearTxBDReady;
340
 
341
wire            ResetTxCtrlEndFrm_wb;
342
wire            SetTxCtrlEndFrm_wb;
343
 
344
 
345
 
346
 
347
assign BDWe   = BDCs &  WB_WE_I;
348
assign BDRead = BDCs & ~WB_WE_I;
349
assign WB_ACK_O = BDWe | BDRead & BDRead_q;  // ACK is delayed one clock because of BLOCKRAM properties when performing read
350
 
351
 
352
 
353
reg EnableRAM;
354
always @ (posedge WB_CLK_I or posedge WB_RST_I)
355
begin
356
  if(WB_RST_I)
357
    EnableRAM   <=#Tp 1'b0;
358
  else
359
  if(BDWe)
360
    EnableRAM   <=#Tp 1'b1;
361
  else
362
    EnableRAM   <=#Tp EnableRAM;
363
end
364
 
365
 
366
// Generic synchronous two-port RAM interface
367
generic_tpram     #(8, 32)  i_generic_tpram
368
(
369
  .clk_a(WB_CLK_I),   .rst_a(WB_RST_I),         .ce_a(1'b1),        .we_a(BDWe),
370
  .oe_a(EnableRAM),   .addr_a(WB_ADR_I[9:2]),   .di_a(WB_DAT_I),    .do_a(WB_BDDataOut),
371
 
372
  .clk_b(WB_CLK_I),   .rst_b(WB_RST_I),         .ce_b(EnableRAM),   .we_b(BDStatusWrite),
373
  .oe_b(EnableRAM),   .addr_b(BDAddress[7:0]),  .di_b(BDDataIn),    .do_b(BDDataOut)
374
);
375
 
376
 
377
// WB_CLK_I is divided by 2. This signal is used for enabling tx and rx operations sequentially
378
always @ (posedge WB_CLK_I or posedge WB_RST_I)
379
begin
380
  if(WB_RST_I)
381
    Div2 <=#Tp 1'h0;
382
  else
383
    Div2 <=#Tp ~Div2;
384
end
385
 
386
 
387
// Tx_En and Rx_En select who can access the BD memory (Tx or Rx)
388
assign TxEn =  Div2 & r_TxEn;
389
assign RxEn = ~Div2 & r_RxEn;
390
 
391
 
392
// Changes for tx occur every second clock. Flop is used for this manner.
393
always @ (posedge MTxClk or posedge WB_RST_I)
394
begin
395
  if(WB_RST_I)
396
    Flop <=#Tp 1'b0;
397
  else
398
  if(TxDone | TxAbort | TxRetry_q)
399
    Flop <=#Tp 1'b0;
400
  else
401
  if(TxUsedData)
402
    Flop <=#Tp ~Flop;
403
end
404
 
405
 
406
// Latching READY status of the Tx buffer descriptor
407
always @ (posedge WB_CLK_I or posedge WB_RST_I)
408
begin
409
  if(WB_RST_I)
410
    TxBDReady <=#Tp 1'b0;
411
  else
412
  if(TxEn & TxBDRead)
413
    TxBDReady <=#Tp BDDataOut[15]; // TxBDReady=BDDataOut[15]   // TxBDReady is sampled only once at the beginning
414
  else
415
  if(TxDone & ~TxDone_q | TxAbort & ~TxAbort_q | TxRetry & ~TxRetry_q | ClearTxBDReady | TxPauseRq)
416
    TxBDReady <=#Tp 1'b0;
417
end
418
 
419
 
420
// Latching READY status of the Tx buffer descriptor
421
always @ (posedge WB_CLK_I or posedge WB_RST_I)
422
begin
423
  if(WB_RST_I)
424
    begin
425
      TxPauseRq <=#Tp 1'b0;
426
    end
427
  else
428
  if(TxEn & TxBDRead)
429
    begin
430
      TxPauseRq <=#Tp BDDataOut[9];    // Tx PAUSE request
431
    end
432
  else
433
      TxPauseRq <=#Tp 1'b0;
434
end
435
 
436
 
437
assign TxPauseTV[15:0] = TxLength[15:0];
438
 
439
// Reading the Tx buffer descriptor
440
assign StartTxBDRead = TxEn & ~BlockingTxBDRead & (TxRetry_wb | TxStatusWriteOccured);
441
 
442
always @ (posedge WB_CLK_I or posedge WB_RST_I)
443
begin
444
  if(WB_RST_I)
445
    TxBDRead <=#Tp 1'b1;
446
  else
447
  if(StartTxBDRead)
448
    TxBDRead <=#Tp 1'b1;
449
  else
450
  if(StartTxDataRead | TxPauseRq)
451
    TxBDRead <=#Tp 1'b0;
452
end
453
 
454
 
455
 
456
// Requesting data (DMA)
457
assign StartTxDataRead = TxBDRead & TxBDReady & ~TxPauseRq | GetNewTxData_wb;
458
assign ResetTxDataRead = DMACycleFinishedTx | TxRestartPulse | TxAbortPulse | TxDonePulse;
459
 
460
 
461
// Reading data
462
always @ (posedge WB_CLK_I or posedge WB_RST_I)
463
begin
464
  if(WB_RST_I)
465
    TxDataRead <=#Tp 1'b0;
466
  else
467
  if(StartTxDataRead & r_DmaEn)
468
    TxDataRead <=#Tp 1'b1;
469
  else
470
  if(ResetTxDataRead)
471
    TxDataRead <=#Tp 1'b0;
472
end
473
 
474
// Requesting tx data from the DMA
475
assign WB_REQ_O[0] = TxDataRead;
476
assign DMACycleFinishedTx = WB_REQ_O[0] & WB_ACK_I[0] & TxBDReady;
477
 
478
 
479
// Writing status back to the Tx buffer descriptor
480
assign StartTxStatusWrite = TxEn & ~BlockingTxStatusWrite & (TxDone_wb | TxAbort_wb | TxCtrlEndFrm_wb);
481
assign ResetTxStatusWrite = TxStatusWrite;
482
 
483
always @ (posedge WB_CLK_I or posedge WB_RST_I)
484
begin
485
  if(WB_RST_I)
486
    TxStatusWrite <=#Tp 1'b0;
487
  else
488
  if(StartTxStatusWrite)
489
    TxStatusWrite <=#Tp 1'b1;
490
  else
491
  if(ResetTxStatusWrite)
492
    TxStatusWrite <=#Tp 1'b0;
493
end
494
 
495
 
496
// Status writing must occur only once. Meanwhile it is blocked.
497
always @ (posedge WB_CLK_I or posedge WB_RST_I)
498
begin
499
  if(WB_RST_I)
500
    BlockingTxStatusWrite <=#Tp 1'b0;
501
  else
502
  if(StartTxStatusWrite)
503
    BlockingTxStatusWrite <=#Tp 1'b1;
504
  else
505
  if(~TxDone_wb & ~TxAbort_wb)
506
    BlockingTxStatusWrite <=#Tp 1'b0;
507
end
508
 
509
 
510
// After a tx status write is finished, a new tx buffer descriptor is read. Signal must be
511
// latched because new BD read doesn't occur immediately.
512
always @ (posedge WB_CLK_I or posedge WB_RST_I)
513
begin
514
  if(WB_RST_I)
515
    TxStatusWriteOccured <=#Tp 1'b0;
516
  else
517
  if(StartTxStatusWrite)
518
    TxStatusWriteOccured <=#Tp 1'b1;
519
  else
520
  if(StartTxBDRead)
521
    TxStatusWriteOccured <=#Tp 1'b0;
522
end
523
 
524
 
525
// TxBDRead state is activated only once. 
526
always @ (posedge WB_CLK_I or posedge WB_RST_I)
527
begin
528
  if(WB_RST_I)
529
    BlockingTxBDRead <=#Tp 1'b0;
530
  else
531
  if(StartTxBDRead)
532
    BlockingTxBDRead <=#Tp 1'b1;
533
  else
534
  if(TxStartFrm_wb | TxCtrlEndFrm_wb)
535
    BlockingTxBDRead <=#Tp 1'b0;
536
end
537
 
538
 
539
// Latching status from the tx buffer descriptor
540
// Data is avaliable one cycle after the access is started (at that time signal TxEn is not active)
541
always @ (posedge WB_CLK_I or posedge WB_RST_I)
542
begin
543
  if(WB_RST_I)
544
    TxStatus <=#Tp 32'h0;
545
  else
546
  if(TxBDRead & TxEn)
547
    TxStatus <=#Tp BDDataOut;
548
end
549
 
550
 
551
//Latching length from the buffer descriptor;
552
always @ (posedge WB_CLK_I or posedge WB_RST_I)
553
begin
554
  if(WB_RST_I)
555
    TxLength <=#Tp 16'h0;
556
  else
557
  if(TxBDRead & TxEn)
558
    TxLength <=#Tp BDDataOut[31:16];
559
  else
560
  if(GetNewTxData_wb & ~WillSendControlFrame)
561
    begin
562
      if(TxLength > 4)
563
        TxLength <=#Tp TxLength - 4;    // Length is subtracted at the data request
564
      else
565
        TxLength <=#Tp 16'h0;
566
    end
567
end
568
 
569
 
570
// Latching Rx buffer descriptor status
571
// Data is avaliable one cycle after the access is started (at that time signal RxEn is not active)
572
always @ (posedge WB_CLK_I or posedge WB_RST_I)
573
begin
574
  if(WB_RST_I)
575
    RxStatus <=#Tp 16'h0;
576
  else
577
  if(RxBDRead & RxEn)
578
    RxStatus <=#Tp BDDataOut[15:0];
579
end
580
 
581
 
582
// Signal GetNewTxData_wb that requests new data from the DMA must be latched since the DMA response
583
// might be delayed.
584
always @ (posedge WB_CLK_I or posedge WB_RST_I)
585
begin
586
  if(WB_RST_I)
587
    GetNewTxData_wb_latched <=#Tp 1'b0;
588
  else
589
  if(GetNewTxData_wb)
590
    GetNewTxData_wb_latched <=#Tp 1'b1;
591
  else
592
  if(DMACycleFinishedTx)
593
    GetNewTxData_wb_latched <=#Tp 1'b0;
594
end
595
 
596
 
597
// New tx data is avaliable after the DMA access is finished
598
always @ (posedge WB_CLK_I or posedge WB_RST_I)
599
begin
600
  if(WB_RST_I)
601
    NewTxDataAvaliable_wb <=#Tp 1'b0;
602
  else
603
  if(DMACycleFinishedTx & GetNewTxData_wb_latched)
604
    NewTxDataAvaliable_wb <=#Tp 1'b1;
605
  else
606
  if(NewTxDataAvaliable_wb)
607
    NewTxDataAvaliable_wb <=#Tp 1'b0;
608
end
609
 
610
 
611
// Tx Buffer descriptor is only read at the beginning. This signal is used for generation of the
612
// TxStartFrm_wb signal.
613
always @ (posedge WB_CLK_I or posedge WB_RST_I)
614
begin
615
  if(WB_RST_I)
616
    TxBDAccessed <=#Tp 1'b0;
617
  else
618
  if(TxBDRead)
619
    TxBDAccessed <=#Tp 1'b1;
620
  else
621
  if(TxStartFrm_wb)
622
    TxBDAccessed <=#Tp 1'b0;
623
end
624
 
625
 
626
// TxStartFrm_wb: indicator of the start frame (synchronized to WB_CLK_I)
627
always @ (posedge WB_CLK_I or posedge WB_RST_I)
628
begin
629
  if(WB_RST_I)
630
    TxStartFrm_wb <=#Tp 1'b0;
631
  else
632
  if(DMACycleFinishedTx & TxBDAccessed & ~TxStartFrm_wb)
633
    TxStartFrm_wb <=#Tp 1'b1;
634
  else
635
  if(TxStartFrm_wb)
636
    TxStartFrm_wb <=#Tp 1'b0;
637
end
638
 
639
 
640
// TxEndFrm_wb: indicator of the end of frame
641
assign TxEndFrm_wb = (TxLength <= 4) & TxUsedData;
642
 
643
 
644
// Input latch of the end-of-frame indicator
645
always @ (posedge WB_CLK_I or posedge WB_RST_I)
646
begin
647
  if(WB_RST_I)
648
    TxEndFrm_wbLatched <=#Tp 1'b0;
649
  else
650
  if(TxEndFrm_wb)
651
    TxEndFrm_wbLatched <=#Tp 1'b1;
652
  else
653
  if(TxRestartPulse | TxDonePulse | TxAbortPulse)
654
    TxEndFrm_wbLatched <=#Tp 1'b0;
655
end
656
 
657
 
658
// Marks which bytes are valid within the word.
659
assign TxValidBytes = (TxLength >= 4)? 2'b0 : TxLength[1:0];
660
 
661
 
662
// Latching valid bytes
663
always @ (posedge WB_CLK_I or posedge WB_RST_I)
664
begin
665
  if(WB_RST_I)
666
    TxValidBytesLatched <=#Tp 2'h0;
667
  else
668
  if(TxEndFrm_wb & ~TxEndFrm_wbLatched)
669
    TxValidBytesLatched <=#Tp TxValidBytes;
670
  else
671
  if(TxRestartPulse | TxDonePulse | TxAbortPulse)
672
    TxValidBytesLatched <=#Tp 2'h0;
673
end
674
 
675
 
676
// Input Tx data latch 
677
always @ (posedge WB_CLK_I or posedge WB_RST_I)
678
begin
679
  if(WB_RST_I)
680
    TxDataLatched_wb <=#Tp 32'h0;
681
  else
682
  if(DMACycleFinishedTx)
683
    TxDataLatched_wb <=#Tp WB_DAT_I;
684
end
685
 
686
 
687
// TxStartFrmRequest is set when a new frame is avaliable or when new data of the same frame is avaliable)
688
always @ (posedge WB_CLK_I or posedge WB_RST_I)
689
begin
690
  if(WB_RST_I)
691
    TxStartFrmRequest <=#Tp 1'b0;
692
  else
693
  if(TxStartFrm_wb | NewTxDataAvaliable_wb)
694
    TxStartFrmRequest <=#Tp TxStartFrm_wb;
695
end
696
 
697
 
698
// Bit 14 is used as a wrap bit. When active it indicates the last buffer descriptor in a row. After
699
// using this descriptor, first BD will be used again.
700
 
701
 
702
 
703
// TX
704
// bit 15 od tx je ready
705
// bit 14 od tx je interrupt (Tx buffer ali tx error bit se postavi v interrupt registru, ko se ta buffer odda)
706
// bit 13 od tx je wrap
707
// bit 12 od tx je pad
708
// bit 11 od tx je crc
709
// bit 10 od tx je last (crc se doda le ce je bit 11 in hkrati bit 10)
710
// bit 9  od tx je pause request (control frame)
711
    // Vsi zgornji biti gredo ven, spodnji biti (od 8 do 0) pa so statusni in se vpisejo po koncu oddajanja
712
// bit 8  od tx je defer indication
713
// bit 7  od tx je late collision
714
// bit 6  od tx je retransmittion limit
715
// bit 5  od tx je underrun
716
// bit 4  od tx je carrier sense lost
717
// bit [3:0] od tx je retry count
718
 
719
//assign TxBDReady      = TxStatus[15];     // already used
720
assign TxIRQEn          = TxStatus[14];
721
assign WrapTxStatusBit  = TxStatus[13];                                                   // ok povezan
722
assign PerPacketPad     = TxStatus[12];                                                   // ok povezan
723
assign PerPacketCrcEn   = TxStatus[11] & TxStatus[10];      // When last is also set      // ok povezan
724
//assign TxPauseRq      = TxStatus[9];      // already used
725
 
726
 
727
 
728
// RX
729
// bit 15 od rx je empty
730
// bit 14 od rx je interrupt (Rx buffer ali rx frame received se postavi v interrupt registru, ko se ta buffer zapre)
731
// bit 13 od rx je wrap
732
// bit 12 od rx je reserved
733
// bit 11 od rx je reserved
734
// bit 10 od rx je last (crc se doda le ce je bit 11 in hkrati bit 10)
735
// bit 9  od rx je pause request (control frame)
736
    // Vsi zgornji biti gredo ven, spodnji biti (od 8 do 0) pa so statusni in se vpisejo po koncu oddajanja
737
// bit 8  od rx je defer indication
738
// bit 7  od rx je late collision
739
// bit 6  od rx je retransmittion limit
740
// bit 5  od rx je underrun
741
// bit 4  od rx je carrier sense lost
742
// bit [3:0] od rx je retry count
743
 
744
assign WrapRxStatusBit = RxStatus[13];
745
 
746
 
747
// Temporary Tx and Rx buffer descriptor address 
748
assign TempTxBDAddress[7:0] = {8{ TxStatusWrite    & ~WrapTxStatusBit}} & (TxBDAddress + 1) ; // Tx BD increment or wrap (last BD)
749
assign TempRxBDAddress[7:0] = {8{ WrapRxStatusBit}} & (r_TxBDNum)       | // Using first Rx BD
750
                              {8{~WrapRxStatusBit}} & (RxBDAddress + 1) ; // Using next Rx BD (incremenrement address)
751
 
752
 
753
// Latching Tx buffer descriptor address
754
always @ (posedge WB_CLK_I or posedge WB_RST_I)
755
begin
756
  if(WB_RST_I)
757
    TxBDAddress <=#Tp 8'h0;
758
  else
759
  if(TxStatusWrite)
760
    TxBDAddress <=#Tp TempTxBDAddress;
761
end
762
 
763
 
764
// Latching Rx buffer descriptor address
765
always @ (posedge WB_CLK_I or posedge WB_RST_I)
766
begin
767
  if(WB_RST_I)
768
    RxBDAddress <=#Tp 8'h0;
769
  else
770
  if(TX_BD_NUM_Wr)                        // When r_TxBDNum is updated, RxBDAddress is also
771
    RxBDAddress <=#Tp WB_DAT_I[7:0];
772
  else
773
  if(RxStatusWrite)
774
    RxBDAddress <=#Tp TempRxBDAddress;
775
end
776
 
777
 
778
// Selecting Tx or Rx buffer descriptor address
779
always @ (posedge WB_CLK_I or posedge WB_RST_I)
780
begin
781
  if(WB_RST_I)
782
    BDAddress <=#Tp 8'h0;
783
  else
784
  if(TxEn)
785
    BDAddress <=#Tp TxBDAddress;
786
  else
787
    BDAddress <=#Tp RxBDAddress;
788
end
789
 
790
 
791
assign RxLength[15:0]  = 16'h1399;
792
assign NewRxStatus[15:0] = {1'b0, WbWriteError, RxStatus[13:0]};
793
 
794
 
795
//assign BDDataIn  = TxStatusWrite ? {TxLength[15:0], StatusIzTxEthMACModula} : {RxLength, NewRxStatus};
796
assign BDDataIn  = TxStatusWrite ? {TxStatus[31:9], 9'h0}
797
                                 : {RxLength, NewRxStatus};
798
 
799
assign BDStatusWrite = TxStatusWrite | RxStatusWrite;
800
 
801
 
802
// Generating delayed signals
803
always @ (posedge WB_CLK_I or posedge WB_RST_I)
804
begin
805
  if(WB_RST_I)
806
    begin
807
      TxRestart_wb_q        <=#Tp 1'b0;
808
      TxDone_wb_q           <=#Tp 1'b0;
809
      TxAbort_wb_q          <=#Tp 1'b0;
810
      BDRead_q              <=#Tp 1'b0;
811
      DMACycleFinishedTx_q  <=#Tp 1'b0;
812
    end
813
  else
814
    begin
815
      TxRestart_wb_q        <=#Tp TxRetry_wb;
816
      TxDone_wb_q           <=#Tp TxDone_wb;
817
      TxAbort_wb_q          <=#Tp TxAbort_wb;
818
      BDRead_q              <=#Tp BDRead;
819
      DMACycleFinishedTx_q  <=#Tp DMACycleFinishedTx;
820
    end
821
end
822
 
823
 
824
// Signals used for various purposes
825
assign TxRestartPulse = TxRetry_wb   & ~TxRestart_wb_q;
826
assign TxDonePulse    = TxDone_wb    & ~TxDone_wb_q;
827
assign TxAbortPulse   = TxAbort_wb   & ~TxAbort_wb_q;
828
 
829
 
830
// Next descriptor for Tx DMA channel
831
always @ (posedge WB_CLK_I or posedge WB_RST_I)
832
begin
833
  if(WB_RST_I)
834
    WB_ND_O_TX <=#Tp 1'b0;
835
  else
836
  if(TxDonePulse | TxAbortPulse)
837
    WB_ND_O_TX <=#Tp 1'b1;
838
  else
839
  if(WB_ND_O_TX)
840
    WB_ND_O_TX <=#Tp 1'b0;
841
end
842
 
843
 
844
// Force next descriptor on DMA channel 0 (Tx)
845
assign WB_ND_O[0] = WB_ND_O_TX;
846
 
847
 
848
 
849
// Restart descriptor for DMA channel 0 (Tx)
850
always @ (posedge WB_CLK_I or posedge WB_RST_I)
851
begin
852
  if(WB_RST_I)
853
    WB_RD_O <=#Tp 1'b0;
854
  else
855
  if(TxRestartPulse)
856
    WB_RD_O <=#Tp 1'b1;
857
  else
858
  if(WB_RD_O)
859
    WB_RD_O <=#Tp 1'b0;
860
end
861
 
862
 
863
assign SetClearTxBDReady = ~TxUsedData & TxUsedData_q;
864
assign ResetClearTxBDReady = ClearTxBDReady | WB_RST_I;
865
 
866
 
867
always @ (posedge SetClearTxBDReady or posedge ResetClearTxBDReady)
868
begin
869
  if(ResetClearTxBDReady)
870
    ClearTxBDReadySync1 <=#Tp 1'b0;
871
  else
872
    ClearTxBDReadySync1 <=#Tp 1'b1;
873
end
874
 
875
always @ (posedge WB_CLK_I or posedge WB_RST_I)
876
begin
877
  if(WB_RST_I)
878
    ClearTxBDReadySync2 <=#Tp 1'b0;
879
  else
880
  if(ClearTxBDReadySync1 & ~ClearTxBDReady)
881
    ClearTxBDReadySync2 <=#Tp 1'b1;
882
  else
883
    ClearTxBDReadySync2 <=#Tp 1'b0;
884
end
885
 
886
 
887
always @ (posedge WB_CLK_I or posedge WB_RST_I)
888
begin
889
  if(WB_RST_I)
890
    ClearTxBDReady <=#Tp 1'b0;
891
  else
892
  if(ClearTxBDReadySync2 & ~ClearTxBDReady)
893
    ClearTxBDReady <=#Tp 1'b1;
894
  else
895
    ClearTxBDReady <=#Tp 1'b0;
896
end
897
 
898
 
899
 
900
// Latching and synchronizing the Tx pause request signal
901
eth_sync_clk1_clk2 syn1 (.clk1(MTxClk),     .clk2(WB_CLK_I),            .reset1(WB_RST_I),    .reset2(WB_RST_I),
902
                         .set2(TxPauseRq),  .sync_out(TPauseRqSync2)
903
                        );
904
 
905
 
906
always @ (posedge MTxClk or posedge WB_RST_I)
907
begin
908
  if(WB_RST_I)
909
    TPauseRq <=#Tp 1'b0;
910
  else
911
  if(TPauseRq )
912
    TPauseRq <=#Tp 1'b0;
913
  else
914
  if(TPauseRqSync2)
915
    TPauseRq <=#Tp 1'b1;
916
end
917
 
918
 
919
 
920
// Generating delayed signals
921
always @ (posedge MTxClk or posedge WB_RST_I)
922
begin
923
  if(WB_RST_I)
924
    begin
925
      TxAbort_q     <=#Tp 1'b0;
926
      TxDone_q      <=#Tp 1'b0;
927
      TxRetry_q     <=#Tp 1'b0;
928
      TxUsedData_q  <=#Tp 1'b0;
929
    end
930
  else
931
    begin
932
      TxAbort_q     <=#Tp TxAbort;
933
      TxDone_q      <=#Tp TxDone;
934
      TxRetry_q     <=#Tp TxRetry;
935
      TxUsedData_q  <=#Tp TxUsedData;
936
    end
937
end
938
 
939
 
940
 
941
// Sinchronizing and evaluating tx data
942
assign SetGotData = (TxStartFrm_wb | NewTxDataAvaliable_wb & ~TxAbort_wb & ~TxRetry_wb) & ~WB_CLK_I;
943
 
944
eth_sync_clk1_clk2 syn2 (.clk1(MTxClk),     .clk2(WB_CLK_I),            .reset1(WB_RST_I),    .reset2(WB_RST_I),
945
                         .set2(SetGotData), .sync_out(GotDataSync3));
946
 
947
 
948
// Evaluating data. If abort or retry occured meanwhile than data is ignored.
949
assign GotDataEvaluate = GotDataSync3 & ~GotData & (~TxRetry & ~TxAbort | (TxRetry | TxAbort) & (TxStartFrmRequest | TxStartFrm));
950
 
951
 
952
// Indication of good data
953
always @ (posedge MTxClk or posedge WB_RST_I)
954
begin
955
  if(WB_RST_I)
956
    GotData <=#Tp 1'b0;
957
  else
958
  if(GotDataEvaluate)
959
    GotData <=#Tp 1'b1;
960
  else
961
    GotData <=#Tp 1'b0;
962
end
963
 
964
 
965
// Tx start frame generation
966
always @ (posedge MTxClk or posedge WB_RST_I)
967
begin
968
  if(WB_RST_I)
969
    TxStartFrm <=#Tp 1'b0;
970
  else
971
  if(TxUsedData_q | TxAbort & ~TxAbort_q | TxRetry & ~TxRetry_q)
972
    TxStartFrm <=#Tp 1'b0;
973
  else
974
  if(TxBDReady & GotData & TxStartFrmRequest)
975
    TxStartFrm <=#Tp 1'b1;
976
end
977
 
978
 
979
// Indication of the last word
980
always @ (posedge MTxClk or posedge WB_RST_I)
981
begin
982
  if(WB_RST_I)
983
    LastWord <=#Tp 1'b0;
984
  else
985
  if((TxEndFrm | TxAbort | TxRetry) & Flop)
986
    LastWord <=#Tp 1'b0;
987
  else
988
  if(TxUsedData & Flop & TxByteCnt == 2'h3)
989
    LastWord <=#Tp TxEndFrm_wbLatched;
990
end
991
 
992
 
993
// Tx end frame generation
994
always @ (posedge MTxClk or posedge WB_RST_I)
995
begin
996
  if(WB_RST_I)
997
    TxEndFrm <=#Tp 1'b0;
998
  else
999
  if(Flop & TxEndFrm | TxAbort | TxRetry_q)
1000
    TxEndFrm <=#Tp 1'b0;
1001
  else
1002
  if(Flop & LastWord)
1003
    begin
1004
      case (TxValidBytesLatched)
1005
        1 : TxEndFrm <=#Tp TxByteCnt == 2'h0;
1006
        2 : TxEndFrm <=#Tp TxByteCnt == 2'h1;
1007
        3 : TxEndFrm <=#Tp TxByteCnt == 2'h2;
1008
 
1009
        default : TxEndFrm <=#Tp 1'b0;
1010
      endcase
1011
    end
1012
end
1013
 
1014
 
1015
// Tx data selection (latching)
1016
always @ (posedge MTxClk or posedge WB_RST_I)
1017
begin
1018
  if(WB_RST_I)
1019
    TxData <=#Tp 8'h0;
1020
  else
1021
  if(GotData & ~TxStartFrm & ~TxUsedData)
1022
    TxData <=#Tp TxDataLatched_wb[7:0];
1023
  else
1024
  if(TxUsedData & Flop)
1025
    begin
1026
      case(TxByteCnt)
1027
 
1028
        1 : TxData <=#Tp TxDataLatched[15:8];
1029
        2 : TxData <=#Tp TxDataLatched[23:16];
1030
        3 : TxData <=#Tp TxDataLatched[31:24];
1031
      endcase
1032
    end
1033
end
1034
 
1035
 
1036
// Latching tx data
1037
always @ (posedge MTxClk or posedge WB_RST_I)
1038
begin
1039
  if(WB_RST_I)
1040
    TxDataLatched[31:0] <=#Tp 32'h0;
1041
  else
1042
  if(GotData & ~TxUsedData & ~TxStartFrm)
1043
    TxDataLatched[31:0] <=#Tp TxDataLatched_wb[31:0];
1044
  else
1045
  if(TxUsedData & Flop & TxByteCnt == 2'h3)
1046
    TxDataLatched[31:0] <=#Tp TxDataLatched_wb[31:0];
1047
end
1048
 
1049
 
1050
// Generation of the DataNotAvaliable signal which is used for the generation of the TxUnderRun signal
1051
assign ResetDataNotAvaliable = DMACycleFinishedTx_q | WB_RST_I;
1052
assign SetDataNotAvaliable = GotData & ~TxUsedData & ~TxStartFrm | TxUsedData & Flop & TxByteCnt == 2'h3;
1053
 
1054
always @ (posedge MTxClk or posedge ResetDataNotAvaliable)
1055
begin
1056
  if(ResetDataNotAvaliable)
1057
    DataNotAvaliable <=#Tp 1'b0;
1058
  else
1059
  if(SetDataNotAvaliable) // data is latched here
1060
    DataNotAvaliable <=#Tp 1'b1;
1061
end
1062
 
1063
 
1064
// Tx under run
1065
always @ (posedge MTxClk or posedge WB_RST_I)
1066
begin
1067
  if(WB_RST_I)
1068
    TxUnderRun <=#Tp 1'b0;
1069
  else
1070
  if(TxAbort & ~TxAbort_q)
1071
    TxUnderRun <=#Tp 1'b0;
1072
  else
1073
  if(TxUsedData & Flop & TxByteCnt == 2'h3 & ~LastWord & DataNotAvaliable)
1074
    TxUnderRun <=#Tp 1'b1;
1075
end
1076
 
1077
 
1078
 
1079
// Tx Byte counter
1080
always @ (posedge MTxClk or posedge WB_RST_I)
1081
begin
1082
  if(WB_RST_I)
1083
    TxByteCnt <=#Tp 2'h0;
1084
  else
1085
  if(TxAbort_q | TxRetry_q)
1086
    TxByteCnt <=#Tp 2'h0;
1087
  else
1088
  if(TxStartFrm & ~TxUsedData)
1089
    TxByteCnt <=#Tp 2'h1;
1090
  else
1091
  if(TxUsedData & Flop)
1092
    TxByteCnt <=#Tp TxByteCnt + 1;
1093
end
1094
 
1095
 
1096
// Generation of the GetNewTxData signal
1097
always @ (posedge MTxClk or posedge WB_RST_I)
1098
begin
1099
  if(WB_RST_I)
1100
    GetNewTxData <=#Tp 1'b0;
1101
  else
1102
  if(GetNewTxData)
1103
    GetNewTxData <=#Tp 1'b0;
1104
  else
1105
  if(TxBDReady & GotData & ~(TxStartFrm | TxUsedData))
1106
     GetNewTxData <=#Tp 1'b1;
1107
  else
1108
  if(TxUsedData & ~TxEndFrm_wbLatched & TxByteCnt == 2'h3)
1109
    GetNewTxData <=#Tp ~LastWord;
1110
end
1111
 
1112
 
1113
// TxRetryLatched
1114
always @ (posedge MTxClk or posedge WB_RST_I)
1115
begin
1116
  if(WB_RST_I)
1117
    TxRetryLatched <=#Tp 1'b0;
1118
  else
1119
  if(TxStartFrm)
1120
    TxRetryLatched <=#Tp 1'b0;
1121
  else
1122
  if(TxRetry)
1123
    TxRetryLatched <=#Tp 1'b1;
1124
end
1125
 
1126
 
1127
 
1128
// Synchronizing request for a new tx data
1129
 
1130
//ne eth_sync_clk1_clk2 syn3 (.clk1(MTxClk),     .clk2(WB_CLK_I),            .reset1(WB_RST_I),    .reset2(WB_RST_I), 
1131
//                         .set2(SetGotData), .sync_out(GotDataSync3));
1132
 
1133
// This section still needs to be changed due to ASIC demands
1134
assign ResetSyncGetNewTxData_wb = SyncGetNewTxData_wb3 | TxAbort_wb | TxRetry_wb | WB_RST_I;
1135
assign SetSyncGetNewTxData_wb = GetNewTxData;
1136
 
1137
 
1138
// Sync. stage 1
1139
always @ (posedge SetSyncGetNewTxData_wb or posedge ResetSyncGetNewTxData_wb)
1140
begin
1141
  if(ResetSyncGetNewTxData_wb)
1142
    SyncGetNewTxData_wb1 <=#Tp 1'b0;
1143
  else
1144
    SyncGetNewTxData_wb1 <=#Tp 1'b1;
1145
end
1146
 
1147
 
1148
// Sync. stage 2
1149
always @ (posedge WB_CLK_I or posedge WB_RST_I)
1150
begin
1151
  if(WB_RST_I)
1152
    SyncGetNewTxData_wb2 <=#Tp 1'b0;
1153
  else
1154
  if(SyncGetNewTxData_wb1 & ~GetNewTxData_wb & ~TxAbort_wb & ~TxRetry_wb)
1155
    SyncGetNewTxData_wb2 <=#Tp 1'b1;
1156
  else
1157
    SyncGetNewTxData_wb2 <=#Tp 1'b0;
1158
end
1159
 
1160
 
1161
// Sync. stage 3
1162
always @ (posedge WB_CLK_I or posedge WB_RST_I)
1163
begin
1164
  if(WB_RST_I)
1165
    SyncGetNewTxData_wb3 <=#Tp 1'b0;
1166
  else
1167
  if(SyncGetNewTxData_wb2 & ~GetNewTxData_wb & ~TxAbort_wb & ~TxRetry_wb)
1168
    SyncGetNewTxData_wb3 <=#Tp 1'b1;
1169
  else
1170
    SyncGetNewTxData_wb3 <=#Tp 1'b0;
1171
end
1172
 
1173
 
1174
// Synchronized request for a new tx data
1175
always @ (posedge WB_CLK_I or posedge WB_RST_I)
1176
begin
1177
  if(WB_RST_I)
1178
    GetNewTxData_wb <=#Tp 1'b0;
1179
  else
1180
  if(GetNewTxData_wb)
1181
    GetNewTxData_wb <=#Tp 1'b0;
1182
  else
1183
  if(SyncGetNewTxData_wb3 & ~GetNewTxData_wb & ~TxAbort_wb & ~TxRetry_wb)
1184
    GetNewTxData_wb <=#Tp 1'b1;
1185
end
1186
 
1187
 
1188
// Synchronizine transmit done signal
1189
// Sinchronizing and evaluating tx data
1190
eth_sync_clk1_clk2 syn4 (.clk1(WB_CLK_I),     .clk2(MTxClk),            .reset1(WB_RST_I),    .reset2(WB_RST_I),
1191
                         .set2(TxDone),       .sync_out(TxDoneSync3)
1192
                        );
1193
 
1194
 
1195
// Syncronized signal TxDone_wb (sync. to WISHBONE clock)
1196
always @ (posedge WB_CLK_I or posedge WB_RST_I)
1197
begin
1198
  if(WB_RST_I)
1199
    TxDone_wb <=#Tp 1'b0;
1200
  else
1201
  if(TxStartFrm_wb | WillSendControlFrame)
1202
    TxDone_wb <=#Tp 1'b0;
1203
  else
1204
  if(TxDoneSync3 & ~TxStartFrmRequest)
1205
    TxDone_wb <=#Tp 1'b1;
1206
end
1207
 
1208
 
1209
assign ResetTxCtrlEndFrm_wb = TxCtrlEndFrm_wb | WB_RST_I;
1210
assign SetTxCtrlEndFrm_wb = TxCtrlEndFrm;
1211
 
1212
 
1213
// Sync stage 1
1214
always @ (posedge SetTxCtrlEndFrm_wb or posedge ResetTxCtrlEndFrm_wb)
1215
begin
1216
  if(ResetTxCtrlEndFrm_wb)
1217
    TxCtrlEndFrm_wbSync1 <=#Tp 1'b0;
1218
  else
1219
    TxCtrlEndFrm_wbSync1 <=#Tp 1'b1;
1220
end
1221
 
1222
 
1223
// Sync stage 2
1224
always @ (posedge WB_CLK_I or posedge WB_RST_I)
1225
begin
1226
  if(WB_RST_I)
1227
    TxCtrlEndFrm_wbSync2 <=#Tp 1'b0;
1228
  else
1229
  if(TxCtrlEndFrm_wbSync1 & ~TxCtrlEndFrm_wb)
1230
    TxCtrlEndFrm_wbSync2 <=#Tp 1'b1;
1231
  else
1232
    TxCtrlEndFrm_wbSync2 <=#Tp 1'b0;
1233
end
1234
 
1235
 
1236
// Synchronized Tx  control end frame
1237
always @ (posedge WB_CLK_I or posedge WB_RST_I)
1238
begin
1239
  if(WB_RST_I)
1240
    TxCtrlEndFrm_wb <=#Tp 1'b0;
1241
  else
1242
  if(TxCtrlEndFrm_wbSync2 & ~TxCtrlEndFrm_wb)
1243
    TxCtrlEndFrm_wb <=#Tp 1'b1;
1244
  else
1245
  if(StartTxStatusWrite)
1246
    TxCtrlEndFrm_wb <=#Tp 1'b0;
1247
end
1248
 
1249
 
1250
// Synchronizing TxRetry signal
1251
eth_sync_clk1_clk2 syn6 (.clk1(WB_CLK_I),       .clk2(MTxClk),            .reset1(WB_RST_I),    .reset2(WB_RST_I),
1252
                         .set2(TxRetryLatched), .sync_out(TxRetrySync3));
1253
 
1254
 
1255
// Synchronized signal TxRetry_wb (synchronized to WISHBONE clock)
1256
always @ (posedge WB_CLK_I or posedge WB_RST_I)
1257
begin
1258
  if(WB_RST_I)
1259
    TxRetry_wb <=#Tp 1'b0;
1260
  else
1261
  if(TxStartFrm_wb | WillSendControlFrame)
1262
    TxRetry_wb <=#Tp 1'b0;
1263
  else
1264
  if(TxRetrySync3)
1265
    TxRetry_wb <=#Tp 1'b1;
1266
end
1267
 
1268
 
1269
// Synchronizing TxAbort signal
1270
eth_sync_clk1_clk2 syn7 (.clk1(WB_CLK_I), .clk2(MTxClk),            .reset1(WB_RST_I),    .reset2(WB_RST_I),
1271
                         .set2(TxAbort),  .sync_out(TxAbortSync3));
1272
 
1273
 
1274
// Synchronized TxAbort_wb signal (synchronized to WISHBONE clock)
1275
always @ (posedge WB_CLK_I or posedge WB_RST_I)
1276
begin
1277
  if(WB_RST_I)
1278
    TxAbort_wb <=#Tp 1'b0;
1279
  else
1280
  if(TxStartFrm_wb)
1281
    TxAbort_wb <=#Tp 1'b0;
1282
  else
1283
  if(TxAbortSync3 & ~TxStartFrmRequest)
1284
    TxAbort_wb <=#Tp 1'b1;
1285
end
1286
 
1287
 
1288
// Reading of the next receive buffer descriptor starts after reception status is
1289
// written to the previous one.
1290
assign StartRxBDRead = RxEn & RxStatusWriteOccured;
1291
assign ResetRxBDRead = RxBDRead & RxBDReady;          // Rx BD is read until READY bit is set.
1292
 
1293
 
1294
// Latching READY status of the Rx buffer descriptor
1295
always @ (negedge WB_CLK_I or posedge WB_RST_I)
1296
begin
1297
  if(WB_RST_I)
1298
    RxBDReady <=#Tp 1'b0;
1299
  else
1300
  if(RxEn & RxBDRead)
1301
    RxBDReady <=#Tp BDDataOut[15];
1302
  else
1303
  if(RxStatusWrite)
1304
    RxBDReady <=#Tp 1'b0;
1305
end
1306
 
1307
 
1308
// Reading the Rx buffer descriptor
1309
always @ (posedge WB_CLK_I or posedge WB_RST_I)
1310
begin
1311
  if(WB_RST_I)
1312
    RxBDRead <=#Tp 1'b1;
1313
  else
1314
  if(StartRxBDRead)
1315
    RxBDRead <=#Tp 1'b1;
1316
  else
1317
  if(ResetRxBDRead)
1318
    RxBDRead <=#Tp 1'b0;
1319
end
1320
 
1321
 
1322
// Reception status is written back to the buffer descriptor after the end of frame is detected.
1323
//assign StartRxStatusWrite = RxEn & RxEndFrm_wb;
1324
assign StartRxStatusWrite = RxEn & RxEndFrm_wb;
1325
 
1326
 
1327
// Writing status back to the Rx buffer descriptor
1328
always @ (posedge WB_CLK_I or posedge WB_RST_I)
1329
begin
1330
  if(WB_RST_I)
1331
    RxStatusWrite <=#Tp 1'b0;
1332
  else
1333
  if(StartRxStatusWrite)
1334
    RxStatusWrite <=#Tp 1'b1;
1335
  else
1336
    RxStatusWrite <=#Tp 1'b0;
1337
end
1338
 
1339
 
1340
// Forcing next descriptor on DMA channel 1 (Rx)
1341
assign WB_ND_O[1] = RxStatusWrite;
1342
 
1343
 
1344
// Latched status that a status write occured.
1345
always @ (posedge WB_CLK_I or posedge WB_RST_I)
1346
begin
1347
  if(WB_RST_I)
1348
    RxStatusWriteOccured <=#Tp 1'b0;
1349
  else
1350
  if(StartRxStatusWrite)
1351
    RxStatusWriteOccured <=#Tp 1'b1;
1352
  else
1353
  if(StartRxBDRead)
1354
    RxStatusWriteOccured <=#Tp 1'b0;
1355
end
1356
 
1357
 
1358
 
1359
// Generation of the synchronized signal ShiftEnded that indicates end of reception
1360
eth_sync_clk1_clk2 syn8 (.clk1(MRxClk),       .clk2(WB_CLK_I),            .reset1(WB_RST_I),    .reset2(WB_RST_I),
1361
                         .set2(RxEndFrm_wb),  .sync_out(ShiftEnded)
1362
                        );
1363
 
1364
 
1365
// Indicating that last byte is being reveived
1366
always @ (posedge MRxClk or posedge WB_RST_I)
1367
begin
1368
  if(WB_RST_I)
1369
    LastByteIn <=#Tp 1'b0;
1370
  else
1371
  if(ShiftWillEnd & (&RxByteCnt))
1372
    LastByteIn <=#Tp 1'b0;
1373
  else
1374
  if(RxValid & RxBDReady & RxEndFrm & ~(&RxByteCnt))
1375
    LastByteIn <=#Tp 1'b1;
1376
end
1377
 
1378
 
1379
// Indicating that data reception will end
1380
always @ (posedge MRxClk or posedge WB_RST_I)
1381
begin
1382
  if(WB_RST_I)
1383
    ShiftWillEnd <=#Tp 1'b0;
1384
  else
1385
  if(ShiftEnded)
1386
    ShiftWillEnd <=#Tp 1'b0;
1387
  else
1388
  if(LastByteIn & (&RxByteCnt) | RxValid & RxEndFrm & (&RxByteCnt))
1389
    ShiftWillEnd <=#Tp 1'b1;
1390
end
1391
 
1392
 
1393
// Receive byte counter
1394
always @ (posedge MRxClk or posedge WB_RST_I)
1395
begin
1396
  if(WB_RST_I)
1397
    RxByteCnt <=#Tp 2'h0;
1398
  else
1399
  if(ShiftEnded)
1400
    RxByteCnt <=#Tp 2'h0;
1401
  else
1402
  if(RxValid & RxBDReady | LastByteIn)
1403
    RxByteCnt <=#Tp RxByteCnt + 1;
1404
end
1405
 
1406
 
1407
// Indicates how many bytes are valid within the last word
1408
always @ (posedge MRxClk or posedge WB_RST_I)
1409
begin
1410
  if(WB_RST_I)
1411
    RxValidBytes <=#Tp 2'h1;
1412
  else
1413
  if(ShiftEnded)
1414
    RxValidBytes <=#Tp 2'h1;
1415
  else
1416
  if(RxValid & ~LastByteIn & ~RxStartFrm)
1417
    RxValidBytes <=#Tp RxValidBytes + 1;
1418
end
1419
 
1420
 
1421
// There is a maximum 3 MRxClk delay between RxDataLatched2 and RxData_wb. In the meantime data
1422
// is stored to the RxDataLatched1. 
1423
always @ (posedge MRxClk or posedge WB_RST_I)
1424
begin
1425
  if(WB_RST_I)
1426
    RxDataLatched1       <=#Tp 16'h0;
1427
  else
1428
  if(RxValid & RxBDReady & ~LastByteIn & RxByteCnt == 2'h0)
1429
    RxDataLatched1[7:0]  <=#Tp RxData;
1430
  else
1431
  if(RxValid & RxBDReady & ~LastByteIn & RxByteCnt == 2'h1)
1432
    RxDataLatched1[15:8] <=#Tp RxData;
1433
end
1434
 
1435
 
1436
// Latching incoming data to buffer
1437
always @ (posedge MRxClk or posedge WB_RST_I)
1438
begin
1439
  if(WB_RST_I)
1440
    RxDataLatched2        <=#Tp 32'h0;
1441
  else
1442
  if(RxValid & RxBDReady & ~LastByteIn & RxByteCnt == 2'h2)
1443
    RxDataLatched2[23:0]  <=#Tp {RxData,RxDataLatched1};
1444
  else
1445
  if(RxValid & RxBDReady & ~LastByteIn & RxByteCnt == 2'h3)
1446
    RxDataLatched2[31:24] <=#Tp RxData;
1447
end
1448
 
1449
 
1450
// Indicating start of the reception process
1451
always @ (posedge MRxClk or posedge WB_RST_I)
1452
begin
1453
  if(WB_RST_I)
1454
    StartShifting <=#Tp 1'b0;
1455
  else
1456
  if((RxValid & RxBDReady & ~RxStartFrm & (&RxByteCnt)) | (ShiftWillEnd &  LastByteIn & (&RxByteCnt)))
1457
    StartShifting <=#Tp 1'b1;
1458
  else
1459
    StartShifting <=#Tp 1'b0;
1460
end
1461
 
1462
 
1463
// Synchronizing Rx start frame to the WISHBONE clock
1464
assign StartRxStartFrmSync1 = RxStartFrm & RxBDReady;
1465
 
1466
eth_sync_clk1_clk2 syn9 (.clk1(WB_CLK_I),     .clk2(MRxClk),            .reset1(WB_RST_I),    .reset2(WB_RST_I),
1467
                         .set2(SetGotData), .sync_out(RxStartFrmSync3)
1468
                        );
1469
 
1470
 
1471
// Generating synchronized Rx start frame
1472
always @ ( posedge WB_CLK_I or posedge WB_RST_I)
1473
begin
1474
  if(WB_RST_I)
1475
    RxStartFrm_wb <=#Tp 1'b0;
1476
  else
1477
  if(RxStartFrmSync3 & ~RxStartFrm_wb)
1478
    RxStartFrm_wb <=#Tp 1'b1;
1479
  else
1480
    RxStartFrm_wb <=#Tp 1'b0;
1481
end
1482
 
1483
 
1484
//Synchronizing signal for latching data that will be written to the WISHBONE
1485
//eth_sync_clk1_clk2 syn10 (.clk1(WB_CLK_I),     .clk2(MRxClk),            .reset1(WB_RST_I),    .reset2(WB_RST_I), 
1486
//                         .set2(StartShifting), .sync_out(LatchNow_wb)
1487
//                        );
1488
 
1489
// This section still needs to be changed due to ASIC demands
1490
assign ResetShifting_wb = LatchNow_wb | WB_RST_I;
1491
assign StartShifting_wb = StartShifting;
1492
 
1493
 
1494
// Sync. stage 1
1495
always @ (posedge StartShifting_wb or posedge ResetShifting_wb)
1496
begin
1497
  if(ResetShifting_wb)
1498
    Shifting_wb_Sync1 <=#Tp 1'b0;
1499
  else
1500
    Shifting_wb_Sync1 <=#Tp 1'b1;
1501
end
1502
 
1503
 
1504
// Sync. stage 2
1505
always @ (posedge WB_CLK_I or posedge WB_RST_I)
1506
begin
1507
  if(WB_RST_I)
1508
    Shifting_wb_Sync2 <=#Tp 1'b0;
1509
  else
1510
  if(Shifting_wb_Sync1 & ~RxDataValid_wb)
1511
    Shifting_wb_Sync2 <=#Tp 1'b1;
1512
  else
1513
    Shifting_wb_Sync2 <=#Tp 1'b0;
1514
end
1515
 
1516
 
1517
// Generating synchronized signal that will latch data for writing to the WISHBONE
1518
always @ (posedge WB_CLK_I or posedge WB_RST_I)
1519
begin
1520
  if(WB_RST_I)
1521
    LatchNow_wb <=#Tp 1'b0;
1522
  else
1523
  if(Shifting_wb_Sync2 & ~RxDataValid_wb)
1524
    LatchNow_wb <=#Tp 1'b1;
1525
  else
1526
    LatchNow_wb <=#Tp 1'b0;
1527
end
1528
 
1529
 
1530
// Indicating that valid data is avaliable
1531
always @ (posedge WB_CLK_I or posedge WB_RST_I)
1532
begin
1533
  if(WB_RST_I)
1534
    RxDataValid_wb <=#Tp 1'b0;
1535
  else
1536
  if(LatchNow_wb & ~RxDataValid_wb)
1537
    RxDataValid_wb <=#Tp 1'b1;
1538
  else
1539
  if(RxDataValid_wb)
1540
    RxDataValid_wb <=#Tp 1'b0;
1541
end
1542
 
1543
 
1544
// Forcing next descriptor in the DMA (Channel 1 is used for rx)
1545
always @ (posedge WB_CLK_I or posedge WB_RST_I)
1546
begin
1547
  if(WB_RST_I)
1548
    WB_REQ_O_RX <=#Tp 1'b0;
1549
  else
1550
  if(LatchNow_wb & ~RxDataValid_wb & r_DmaEn)
1551
    WB_REQ_O_RX <=#Tp 1'b1;
1552
  else
1553
  if(DMACycleFinishedRx)
1554
    WB_REQ_O_RX <=#Tp 1'b0;
1555
end
1556
 
1557
 
1558
assign WB_REQ_O[1] = WB_REQ_O_RX;
1559
assign DMACycleFinishedRx = WB_REQ_O[1] & WB_ACK_I[1];
1560
 
1561
 
1562
// WbWriteError is generated when the previous word is not written to the wishbone on time
1563
always @ (posedge WB_CLK_I or posedge WB_RST_I)
1564
begin
1565
  if(WB_RST_I)
1566
    WbWriteError <=#Tp 1'b0;
1567
  else
1568
  if(LatchNow_wb & ~RxDataValid_wb)
1569
    begin
1570
      if(WB_REQ_O[1] & ~WB_ACK_I[1])
1571
        WbWriteError <=#Tp 1'b1;
1572
    end
1573
  else
1574
  if(RxStartFrm_wb)
1575
    WbWriteError <=#Tp 1'b0;
1576
end
1577
 
1578
 
1579
// Assembling data that will be written to the WISHBONE
1580
always @ (posedge WB_CLK_I or posedge WB_RST_I)
1581
begin
1582
  if(WB_RST_I)
1583
    RxData_wb <=#Tp 32'h0;
1584
  else
1585
  if(LatchNow_wb & ~RxDataValid_wb & ~ShiftWillEnd)
1586
    RxData_wb <=#Tp RxDataLatched2;
1587
  else
1588
  if(LatchNow_wb & ~RxDataValid_wb & ShiftWillEnd)
1589
    case(RxValidBytes)
1590
 
1591
      1 : RxData_wb <=#Tp {24'h0,                       RxDataLatched1[7:0]};
1592
      2 : RxData_wb <=#Tp {16'h0,                       RxDataLatched1[15:0]};
1593
      3 : RxData_wb <=#Tp {8'h0, RxDataLatched2[23:16], RxDataLatched1[15:0]};
1594
    endcase
1595
end
1596
 
1597
 
1598
// Selecting the data for the WISHBONE
1599
assign WB_DAT_O[31:0] = BDRead? WB_BDDataOut : RxData_wb;
1600
 
1601
 
1602
// Generation of the end-of-frame signal
1603
always @ (posedge WB_CLK_I or posedge WB_RST_I)
1604
begin
1605
  if(WB_RST_I)
1606
    RxEndFrm_wb <=#Tp 1'b0;
1607
  else
1608
  if(LatchNow_wb & ~RxDataValid_wb & ShiftWillEnd)
1609
    RxEndFrm_wb <=#Tp 1'b1;
1610
  else
1611
  if(StartRxStatusWrite)
1612
    RxEndFrm_wb <=#Tp 1'b0;
1613
end
1614
 
1615
 
1616
// Interrupts
1617
assign TxB_IRQ = 1'b0;
1618
assign TxE_IRQ = 1'b0;
1619
assign RxB_IRQ = 1'b0;
1620
assign RxF_IRQ = 1'b0;
1621
assign Busy_IRQ = 1'b0;
1622
 
1623
 
1624
endmodule
1625
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.