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

Subversion Repositories ethmac

[/] [ethmac/] [tags/] [rel_1/] [rtl/] [verilog/] [eth_wishbone.v] - Blame information for rev 39

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

Line No. Rev Author Line
1 39 mohor
// Napravi, pause frame
2
 
3
// Poskusi spremeniti vse signale na wb strani da bodo imeli enake koncnice (npr _wb),
4
// vsi na MTxClk strani pa _txclk   
5
// Evaluiraj dato da pre start framom ni prisel abort ali kaj podobnega (kot je bilo v GotData, ki ga zbrisi)
6
 
7 38 mohor
//////////////////////////////////////////////////////////////////////
8
////                                                              ////
9
////  eth_wishbone.v                                              ////
10
////                                                              ////
11
////  This file is part of the Ethernet IP core project           ////
12
////  http://www.opencores.org/projects/ethmac/                   ////
13
////                                                              ////
14
////  Author(s):                                                  ////
15
////      - Igor Mohor (igorM@opencores.org)                      ////
16
////                                                              ////
17
////  All additional information is avaliable in the Readme.txt   ////
18
////  file.                                                       ////
19
////                                                              ////
20
//////////////////////////////////////////////////////////////////////
21
////                                                              ////
22
//// Copyright (C) 2001 Authors                                   ////
23
////                                                              ////
24
//// This source file may be used and distributed without         ////
25
//// restriction provided that this copyright statement is not    ////
26
//// removed from the file and that any derivative work contains  ////
27
//// the original copyright notice and the associated disclaimer. ////
28
////                                                              ////
29
//// This source file is free software; you can redistribute it   ////
30
//// and/or modify it under the terms of the GNU Lesser General   ////
31
//// Public License as published by the Free Software Foundation; ////
32
//// either version 2.1 of the License, or (at your option) any   ////
33
//// later version.                                               ////
34
////                                                              ////
35
//// This source is distributed in the hope that it will be       ////
36
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
37
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
38
//// PURPOSE.  See the GNU Lesser General Public License for more ////
39
//// details.                                                     ////
40
////                                                              ////
41
//// You should have received a copy of the GNU Lesser General    ////
42
//// Public License along with this source; if not, download it   ////
43
//// from http://www.opencores.org/lgpl.shtml                     ////
44
////                                                              ////
45
//////////////////////////////////////////////////////////////////////
46
//
47
// CVS Revision History
48
//
49
// $Log: not supported by cvs2svn $
50 39 mohor
// Revision 1.1  2002/01/23 10:47:59  mohor
51
// Initial version. Equals to eth_wishbonedma.v at this moment.
52 38 mohor
//
53
//
54
//
55 39 mohor
//
56 38 mohor
 
57
 
58
`include "eth_defines.v"
59
`include "timescale.v"
60
 
61
 
62
module eth_wishbone
63
   (
64
 
65
    // WISHBONE common
66
    WB_CLK_I, WB_RST_I, WB_DAT_I, WB_DAT_O,
67
 
68
    // WISHBONE slave
69
                WB_ADR_I, WB_SEL_I, WB_WE_I, WB_ACK_O,
70
                WB_REQ_O, WB_ACK_I, WB_ND_O, WB_RD_O, BDCs,
71
 
72 39 mohor
    // WISHBONE master
73
    m_wb_adr_o, m_wb_sel_o, m_wb_we_o,
74
    m_wb_dat_o, m_wb_dat_i, m_wb_cyc_o,
75
    m_wb_stb_o, m_wb_ack_i, m_wb_err_i,
76
 
77 38 mohor
    //TX
78
    MTxClk, TxStartFrm, TxEndFrm, TxUsedData, TxData, StatusIzTxEthMACModula,
79
    TxRetry, TxAbort, TxUnderRun, TxDone, TPauseRq, TxPauseTV, PerPacketCrcEn,
80
    PerPacketPad,
81
 
82
    //RX
83
    MRxClk, RxData, RxValid, RxStartFrm, RxEndFrm,
84
 
85
    // Register
86
    r_TxEn, r_RxEn, r_TxBDNum, r_DmaEn, TX_BD_NUM_Wr,
87
 
88 39 mohor
    WillSendControlFrame, TxCtrlEndFrm, // igor !!! WillSendControlFrame gre najbrz ven
89 38 mohor
 
90
    // Interrupts
91
    TxB_IRQ, TxE_IRQ, RxB_IRQ, RxF_IRQ, Busy_IRQ
92
 
93
                );
94
 
95
 
96
parameter Tp = 1;
97
 
98
// WISHBONE common
99
input           WB_CLK_I;       // WISHBONE clock
100
input           WB_RST_I;       // WISHBONE reset
101
input  [31:0]   WB_DAT_I;       // WISHBONE data input
102
output [31:0]   WB_DAT_O;       // WISHBONE data output
103
 
104
// WISHBONE slave
105
input   [9:2]   WB_ADR_I;       // WISHBONE address input
106
input   [3:0]   WB_SEL_I;       // WISHBONE byte select input
107
input           WB_WE_I;        // WISHBONE write enable input
108
input           BDCs;           // Buffer descriptors are selected
109
output          WB_ACK_O;       // WISHBONE acknowledge output
110
 
111 39 mohor
// WISHBONE master
112
output  [31:0]  m_wb_adr_o;     // 
113
output   [3:0]  m_wb_sel_o;     // 
114
output          m_wb_we_o;      // 
115
output  [31:0]  m_wb_dat_o;     // 
116
output          m_wb_cyc_o;     // 
117
output          m_wb_stb_o;     // 
118
input   [31:0]  m_wb_dat_i;     // 
119
input           m_wb_ack_i;     // 
120
input           m_wb_err_i;     // 
121
 
122
 
123
 
124
 
125 38 mohor
// DMA
126
input   [1:0]   WB_ACK_I;       // DMA acknowledge input
127
output  [1:0]   WB_REQ_O;       // DMA request output
128
output  [1:0]   WB_ND_O;        // DMA force new descriptor output
129
output          WB_RD_O;        // DMA restart descriptor output
130
 
131
// Tx
132
input           MTxClk;         // Transmit clock (from PHY)
133
input           TxUsedData;     // Transmit packet used data
134
input  [15:0]   StatusIzTxEthMACModula;
135
input           TxRetry;        // Transmit packet retry
136
input           TxAbort;        // Transmit packet abort
137
input           TxDone;         // Transmission ended
138
output          TxStartFrm;     // Transmit packet start frame
139
output          TxEndFrm;       // Transmit packet end frame
140
output  [7:0]   TxData;         // Transmit packet data byte
141
output          TxUnderRun;     // Transmit packet under-run
142
output          PerPacketCrcEn; // Per packet crc enable
143
output          PerPacketPad;   // Per packet pading
144
output          TPauseRq;       // Tx PAUSE control frame
145
output [15:0]   TxPauseTV;      // PAUSE timer value
146
input           WillSendControlFrame;
147
input           TxCtrlEndFrm;
148
 
149
// Rx
150
input           MRxClk;         // Receive clock (from PHY)
151
input   [7:0]   RxData;         // Received data byte (from PHY)
152
input           RxValid;        // 
153
input           RxStartFrm;     // 
154
input           RxEndFrm;       // 
155
 
156
//Register
157
input           r_TxEn;         // Transmit enable
158
input           r_RxEn;         // Receive enable
159
input   [7:0]   r_TxBDNum;      // Receive buffer descriptor number
160
input           r_DmaEn;        // DMA enable
161
input           TX_BD_NUM_Wr;   // RxBDNumber written
162
 
163
// Interrupts
164
output TxB_IRQ;
165
output TxE_IRQ;
166
output RxB_IRQ;
167
output RxF_IRQ;
168
output Busy_IRQ;
169
 
170
reg             WB_REQ_O_RX;
171
reg             WB_ND_O_TX;     // New descriptor
172
reg             WB_RD_O;        // Restart descriptor
173
 
174
reg             TxStartFrm;
175
reg             TxEndFrm;
176
reg     [7:0]   TxData;
177
 
178
reg             TxUnderRun;
179
 
180
reg             RxStartFrm_wb;
181
reg     [31:0]  RxData_wb;
182
reg             RxDataValid_wb;
183
reg             RxEndFrm_wb;
184
 
185
reg     [7:0]   BDAddress;    // BD address for access from MAC side
186
reg             BDRead_q;
187
 
188
reg             TxBDRead;
189 39 mohor
wire            TxStatusWrite;
190 38 mohor
 
191
reg     [1:0]   TxValidBytesLatched;
192
 
193
reg    [15:0]   TxLength;
194 39 mohor
reg    [15:0]   TxStatus;
195 38 mohor
 
196
reg    [15:0]   RxStatus;
197
 
198
reg             TxStartFrm_wb;
199
reg             TxRetry_wb;
200 39 mohor
reg             TxAbort_wb;
201 38 mohor
reg             TxDone_wb;
202
 
203
reg             RxStatusWriteOccured;
204
 
205
reg             TxDone_wb_q;
206
reg             TxAbort_wb_q;
207 39 mohor
reg             TxRetry_wb_q;
208 38 mohor
reg             RxBDReady;
209
reg             TxBDReady;
210
 
211
reg             RxBDRead;
212
reg             RxStatusWrite;
213
reg             WbWriteError;
214
 
215
reg    [31:0]   TxDataLatched;
216
reg     [1:0]   TxByteCnt;
217
reg             LastWord;
218 39 mohor
reg             ReadTxDataFromFifo_tck;
219 38 mohor
 
220
reg             Div2;
221
reg             Flop;
222
 
223
reg             BlockingTxStatusWrite;
224
reg             BlockingTxBDRead;
225
 
226
 
227
reg     [7:0]   TxBDAddress;
228
reg     [7:0]   RxBDAddress;
229
 
230
reg             GotDataSync1;
231
reg             GotDataSync2;
232
wire             GotDataSync3;
233 39 mohor
 
234 38 mohor
reg             GotData;
235
reg             TxRetrySync1;
236
reg             TxAbortSync1;
237 39 mohor
reg             TxDoneSync1;
238 38 mohor
 
239
reg             TxAbort_q;
240
reg             TxRetry_q;
241
reg             TxUsedData_q;
242 39 mohor
reg             TxCtrlEndFrm_q;
243 38 mohor
 
244
reg    [31:0]   RxDataLatched2;
245
reg    [15:0]   RxDataLatched1;
246
reg     [1:0]   RxValidBytes;
247
reg     [1:0]   RxByteCnt;
248
reg             LastByteIn;
249
reg             ShiftWillEnd;
250
 
251
reg             StartShifting;
252
reg             Shifting_wb_Sync1;
253
reg             Shifting_wb_Sync2;
254
reg             LatchNow_wb;
255
 
256
reg             ShiftEndedSync1;
257
reg             ShiftEndedSync2;
258
reg             ShiftEndedSync3;
259
wire            ShiftEnded;
260
 
261
reg             RxStartFrmSync1;
262
reg             RxStartFrmSync2;
263
wire            RxStartFrmSync3;
264
 
265
wire            DWord;                      // Only 32-bit accesses are valid
266 39 mohor
wire            BDWrite;                    // BD Write Enable for access from WISHBONE side
267 38 mohor
wire            BDRead;                     // BD Read access from WISHBONE side
268 39 mohor
wire   [31:0]   RxBDDataIn;                 // Rx BD data in
269
wire   [31:0]   TxBDDataIn;                 // Tx BD data in
270 38 mohor
wire   [31:0]   BDDataOut;                  // BD data out
271
 
272 39 mohor
reg             TxEndFrm_wb;
273 38 mohor
 
274 39 mohor
wire            TxRetryPulse;
275 38 mohor
wire            TxDonePulse;
276
wire            TxAbortPulse;
277
 
278
wire            StartRxBDRead;
279
wire            ResetRxBDRead;
280
wire            StartRxStatusWrite;
281
 
282
wire            ResetShifting_wb;
283
wire            StartShifting_wb;
284
wire            DMACycleFinishedRx;
285
 
286
wire   [31:0]   WB_BDDataOut;
287
 
288
wire            StartTxBDRead;
289
wire            StartTxStatusWrite;
290
wire            ResetTxStatusWrite;
291
 
292
wire            TxIRQEn;
293
wire            WrapTxStatusBit;
294
 
295
wire            WrapRxStatusBit;
296
 
297
wire    [1:0]   TxValidBytes;
298
 
299
wire    [7:0]   TempTxBDAddress;
300
wire    [7:0]   TempRxBDAddress;
301
 
302
wire   [15:0]   RxLength;
303
wire   [15:0]   NewRxStatus;
304
 
305
wire            SetGotData;
306
wire            ResetGotData;
307
wire            GotDataEvaluate;
308
wire            ResetTxDoneSync;
309
wire            ResetTxRetrySync;
310
wire            ResetTxAbortSync;
311
 
312
wire            SetTxAbortSync;
313
wire            ResetShiftEnded;
314
wire            ResetRxStartFrmSync1;
315
wire            StartShiftEnded;
316
wire            StartRxStartFrmSync1;
317
 
318 39 mohor
reg             temp_ack;
319 38 mohor
 
320 39 mohor
`ifdef ETH_REGISTERED_OUTPUTS
321
reg             temp_ack2;
322
reg [31:0]      registered_ram_do;
323
`endif
324 38 mohor
 
325 39 mohor
reg WbEn, WbEn_q;
326
reg RxEn, RxEn_q;
327
reg TxEn, TxEn_q;
328 38 mohor
 
329 39 mohor
wire ram_ce;
330
wire ram_we;
331
wire ram_oe;
332
reg [7:0]   ram_addr;
333
reg [31:0]  ram_di;
334
wire [31:0] ram_do;
335 38 mohor
 
336 39 mohor
wire StartTxPointerRead;
337
wire ResetTxPointerRead;
338
reg  TxPointerRead;
339
reg TxEn_needed;
340 38 mohor
 
341 39 mohor
//assign BDWrite = BDCs &  WB_WE_I & WbEn & ~WbEn_q;
342
assign BDWrite = BDCs &  WB_WE_I & WbEn & WbEn_q;
343
assign BDRead  = BDCs & ~WB_WE_I & WbEn_q;              // Read cycle is longer for one cycle
344 38 mohor
 
345 39 mohor
 
346
  always @ (posedge WB_CLK_I or posedge WB_RST_I)
347
  begin
348
    if(WB_RST_I)
349
      begin
350
        temp_ack <=#Tp 1'b0;
351
        `ifdef ETH_REGISTERED_OUTPUTS
352
        temp_ack2 <=#Tp 1'b0;
353
        registered_ram_do <=#Tp 32'h0;
354
        `endif
355
      end
356
    else
357
      begin
358
        temp_ack <=#Tp BDWrite | BDRead & ~WbEn;
359
        `ifdef ETH_REGISTERED_OUTPUTS
360
        temp_ack2 <=#Tp temp_ack;
361
        registered_ram_do <=#Tp ram_do;
362
        `endif
363
      end
364
  end
365
 
366
`ifdef ETH_REGISTERED_OUTPUTS
367
  assign WB_ACK_O = temp_ack2;
368
  assign WB_DAT_O = registered_ram_do;
369
`else
370
  assign WB_ACK_O = temp_ack;
371
  assign WB_DAT_O = ram_do;
372
`endif
373
 
374
 
375
 
376
 
377 38 mohor
// Generic synchronous two-port RAM interface
378 39 mohor
/*
379 38 mohor
generic_tpram     #(8, 32)  i_generic_tpram
380
(
381 39 mohor
  .clk_a(WB_CLK_I),   .rst_a(WB_RST_I),         .ce_a(1'b1),        .we_a(BDWrite),
382 38 mohor
  .oe_a(EnableRAM),   .addr_a(WB_ADR_I[9:2]),   .di_a(WB_DAT_I),    .do_a(WB_BDDataOut),
383
 
384
  .clk_b(WB_CLK_I),   .rst_b(WB_RST_I),         .ce_b(EnableRAM),   .we_b(BDStatusWrite),
385
  .oe_b(EnableRAM),   .addr_b(BDAddress[7:0]),  .di_b(BDDataIn),    .do_b(BDDataOut)
386
);
387 39 mohor
*/
388 38 mohor
 
389
 
390 39 mohor
 
391
RAMB4_S16 ram1 (.DO(ram_do[15:0]),  .ADDR(ram_addr), .DI(ram_di[15:0]),  .EN(ram_ce),
392
                .CLK(WB_CLK_I),     .WE(ram_we),     .RST(WB_RST_I));
393
RAMB4_S16 ram2 (.DO(ram_do[31:16]), .ADDR(ram_addr), .DI(ram_di[31:16]), .EN(ram_ce),
394
                .CLK(WB_CLK_I),     .WE(ram_we),     .RST(WB_RST_I));
395
 
396
 
397
 
398
/*
399
generic_spram #(8, 32) ram (
400
        // Generic synchronous single-port RAM interface
401
        .clk(WB_CLK_I), .rst(WB_RST_I), .ce(ram_ce), .we(ram_we), .oe(ram_oe), .addr(ram_addr), .di(ram_di), .do(ram_do)
402
);
403
*/
404
assign ram_ce = 1'b1;
405
assign ram_we = BDWrite | TxStatusWrite;    // tu manjka se write kad se vpisuje RxBD status
406
assign ram_oe = BDRead | TxEn & TxEn_q & TxBDRead;     // Tu manjka se read kadar se bere RxBD
407
 
408
reg [3:0] xxx_debug;
409
 
410
//assign TxEn_needed = ~TxBDReady | TxPointerRead;
411
 
412 38 mohor
always @ (posedge WB_CLK_I or posedge WB_RST_I)
413
begin
414
  if(WB_RST_I)
415 39 mohor
    TxEn_needed <=#Tp 1'b0;
416 38 mohor
  else
417 39 mohor
  if(~TxBDReady & WbEn)
418
    TxEn_needed <=#Tp 1'b1;
419
  else
420
  if(TxPointerRead & TxEn & TxEn_q)
421
    TxEn_needed <=#Tp 1'b0;
422 38 mohor
end
423
 
424
 
425
 
426
 
427 39 mohor
 
428
// Enabling access to the RAM for three devices.
429
always @ (posedge WB_CLK_I or posedge WB_RST_I)
430
begin
431
  if(WB_RST_I)
432
    begin
433
      WbEn <=#Tp 1'b1;
434
      RxEn <=#Tp 1'b0;
435
      TxEn <=#Tp 1'b0;
436
      ram_addr <=#Tp 8'h0;
437
      ram_di <=#Tp 32'h0;
438
      xxx_debug <=#Tp 0;      // igor !!! zbrisi xxx_debug, debug, ...
439
    end
440
  else
441
    begin
442
      // Switching between three stages depends on enable signals
443
//      casex ({WbEn_q, RxEn_q, TxEn_q, r_RxEn, r_TxEn, TxEn_needed})  // synopsys parallel_case
444
      casex ({WbEn_q, RxEn_q, TxEn_q, r_RxEn, r_TxEn & TxEn_needed})  // synopsys parallel_case
445
        5'b100_1x :
446
          begin
447
            WbEn <=#Tp 1'b0;
448
            RxEn <=#Tp 1'b1;  // wb access stage and r_RxEn is enabled
449
            TxEn <=#Tp 1'b0;
450
            ram_addr <=#Tp RxBDAddress;
451
            ram_di <=#Tp RxBDDataIn;
452
      xxx_debug <=#Tp 1;
453
          end
454
        5'b100_01 :
455
          begin
456
            WbEn <=#Tp 1'b0;
457
            RxEn <=#Tp 1'b0;
458
            TxEn <=#Tp 1'b1;  // wb access stage, r_RxEn is disabled but r_TxEn is enabled
459
            ram_addr <=#Tp TxBDAddress + TxPointerRead;
460
            ram_di <=#Tp TxBDDataIn;
461
      xxx_debug <=#Tp 2;
462
          end
463
        5'b010_x0 :
464
          begin
465
            WbEn <=#Tp 1'b1;  // RxEn access stage and r_TxEn is disabled
466
            RxEn <=#Tp 1'b0;
467
            TxEn <=#Tp 1'b0;
468
            ram_addr <=#Tp WB_ADR_I[9:2];
469
            ram_di <=#Tp WB_DAT_I;
470
      xxx_debug <=#Tp 3;
471
          end
472
        5'b010_x1 :
473
          begin
474
            WbEn <=#Tp 1'b0;
475
            RxEn <=#Tp 1'b0;
476
            TxEn <=#Tp 1'b1;  // RxEn access stage and r_TxEn is enabled
477
            ram_addr <=#Tp TxBDAddress + TxPointerRead;
478
            ram_di <=#Tp TxBDDataIn;
479
      xxx_debug <=#Tp 4;
480
          end
481
        5'b001_xx :
482
          begin
483
            WbEn <=#Tp 1'b1;  // TxEn access stage (we always go to wb access stage)
484
            RxEn <=#Tp 1'b0;
485
            TxEn <=#Tp 1'b0;
486
            ram_addr <=#Tp WB_ADR_I[9:2];
487
            ram_di <=#Tp WB_DAT_I;
488
      xxx_debug <=#Tp 5;
489
          end
490
        5'b100_00 :
491
          begin
492
            WbEn <=#Tp 1'b0;  // WbEn access stage and there is no need for other stages. WbEn needs to be switched off for a bit
493
      xxx_debug <=#Tp 6;
494
          end
495
        5'b000_00 :
496
          begin
497
            WbEn <=#Tp 1'b1;  // Idle state. We go to WbEn access stage.
498
            RxEn <=#Tp 1'b0;
499
            TxEn <=#Tp 1'b0;
500
            ram_addr <=#Tp WB_ADR_I[9:2];
501
            ram_di <=#Tp WB_DAT_I;
502
      xxx_debug <=#Tp 7;
503
          end
504
        default :
505
          begin
506
            WbEn <=#Tp 1'b1;  // We go to wb access stage
507
            RxEn <=#Tp 1'b0;
508
            TxEn <=#Tp 1'b0;
509
            ram_addr <=#Tp WB_ADR_I[9:2];
510
            ram_di <=#Tp WB_DAT_I;
511
      xxx_debug <=#Tp 8;
512
          end
513
      endcase
514
    end
515
end
516
 
517
 
518
// Delayed stage signals
519
always @ (posedge WB_CLK_I or posedge WB_RST_I)
520
begin
521
  if(WB_RST_I)
522
    begin
523
      WbEn_q <=#Tp 1'b0;
524
      RxEn_q <=#Tp 1'b0;
525
      TxEn_q <=#Tp 1'b0;
526
    end
527
  else
528
    begin
529
      WbEn_q <=#Tp WbEn;
530
      RxEn_q <=#Tp RxEn;
531
      TxEn_q <=#Tp TxEn;
532
    end
533
end
534
 
535 38 mohor
// Changes for tx occur every second clock. Flop is used for this manner.
536
always @ (posedge MTxClk or posedge WB_RST_I)
537
begin
538
  if(WB_RST_I)
539
    Flop <=#Tp 1'b0;
540
  else
541
  if(TxDone | TxAbort | TxRetry_q)
542
    Flop <=#Tp 1'b0;
543
  else
544
  if(TxUsedData)
545
    Flop <=#Tp ~Flop;
546
end
547
 
548 39 mohor
wire ResetTxBDReady;
549
assign ResetTxBDReady = TxDonePulse | TxAbortPulse | TxRetryPulse;
550 38 mohor
 
551
// Latching READY status of the Tx buffer descriptor
552
always @ (posedge WB_CLK_I or posedge WB_RST_I)
553
begin
554
  if(WB_RST_I)
555
    TxBDReady <=#Tp 1'b0;
556
  else
557 39 mohor
  if(TxEn & TxEn_q & TxBDRead & ~TxPointerRead)
558
    TxBDReady <=#Tp ram_do[15]; // TxBDReady is sampled only once at the beginning
559 38 mohor
  else
560 39 mohor
  if(ResetTxBDReady)
561 38 mohor
    TxBDReady <=#Tp 1'b0;
562
end
563
 
564
 
565 39 mohor
// Reading the Tx buffer descriptor
566
assign StartTxBDRead = (TxRetry_wb | TxStatusWrite) & ~BlockingTxBDRead;
567
 
568 38 mohor
always @ (posedge WB_CLK_I or posedge WB_RST_I)
569
begin
570
  if(WB_RST_I)
571 39 mohor
    TxBDRead <=#Tp 1'b1;
572 38 mohor
  else
573 39 mohor
  if(StartTxBDRead)
574
    TxBDRead <=#Tp 1'b1;
575 38 mohor
  else
576 39 mohor
  if(TxBDReady)
577
    TxBDRead <=#Tp 1'b0;
578 38 mohor
end
579
 
580
 
581 39 mohor
// Reading Tx BD pointer
582
assign StartTxPointerRead = TxBDRead & TxBDReady;
583 38 mohor
 
584 39 mohor
// Reading Tx BD Pointer
585 38 mohor
always @ (posedge WB_CLK_I or posedge WB_RST_I)
586
begin
587
  if(WB_RST_I)
588 39 mohor
    TxPointerRead <=#Tp 1'b0;
589 38 mohor
  else
590 39 mohor
  if(StartTxPointerRead)
591
    TxPointerRead <=#Tp 1'b1;
592 38 mohor
  else
593 39 mohor
  if(TxEn_q)
594
    TxPointerRead <=#Tp 1'b0;
595 38 mohor
end
596
 
597
 
598 39 mohor
// Writing status back to the Tx buffer descriptor
599
assign TxStatusWrite = (TxDone_wb | TxAbort_wb) & TxEn & TxEn_q & ~BlockingTxStatusWrite;
600 38 mohor
 
601
 
602
 
603 39 mohor
// Status writing must occur only once. Meanwhile it is blocked.
604 38 mohor
always @ (posedge WB_CLK_I or posedge WB_RST_I)
605
begin
606
  if(WB_RST_I)
607 39 mohor
    BlockingTxStatusWrite <=#Tp 1'b0;
608 38 mohor
  else
609 39 mohor
  if(TxStatusWrite)
610
    BlockingTxStatusWrite <=#Tp 1'b1;
611 38 mohor
  else
612 39 mohor
  if(~TxDone_wb & ~TxAbort_wb)
613
    BlockingTxStatusWrite <=#Tp 1'b0;
614 38 mohor
end
615
 
616
 
617 39 mohor
// TxBDRead state is activated only once. 
618
always @ (posedge WB_CLK_I or posedge WB_RST_I)
619
begin
620
  if(WB_RST_I)
621
    BlockingTxBDRead <=#Tp 1'b0;
622
  else
623
  if(StartTxBDRead)
624
    BlockingTxBDRead <=#Tp 1'b1;
625
  else
626
  if(TxStartFrm_wb)
627
    BlockingTxBDRead <=#Tp 1'b0;
628
end
629 38 mohor
 
630
 
631 39 mohor
// Latching status from the tx buffer descriptor
632
// Data is avaliable one cycle after the access is started (at that time signal TxEn is not active)
633 38 mohor
always @ (posedge WB_CLK_I or posedge WB_RST_I)
634
begin
635
  if(WB_RST_I)
636 39 mohor
    TxStatus <=#Tp 15'h0;
637 38 mohor
  else
638 39 mohor
  if(TxEn & TxEn_q & TxBDRead & ~TxPointerRead)
639
    TxStatus <=#Tp ram_do[15:0];
640 38 mohor
end
641
 
642 39 mohor
reg ReadDataFromTxBuffer;
643
wire WriteDataToRxBuffer = 0; // igor !!! Popravi to, da bo pravilno
644 38 mohor
 
645 39 mohor
reg MasterWbTX;
646
reg MasterWbRX;
647
 
648
reg [31:0] m_wb_dat_o;
649
reg [31:0] m_wb_adr_o;
650
reg        m_wb_cyc_o;
651
reg        m_wb_stb_o;
652
reg        m_wb_we_o;
653
wire [31:0] rx_fifo_data_out = 0; // Spremeni to, da bo pravilno
654
wire TxLengthEq0;
655
wire TxLengthLt4;
656
 
657
 
658
//Latching length from the buffer descriptor;
659 38 mohor
always @ (posedge WB_CLK_I or posedge WB_RST_I)
660
begin
661
  if(WB_RST_I)
662 39 mohor
    TxLength <=#Tp 16'h0;
663 38 mohor
  else
664 39 mohor
  if(TxEn & TxEn_q & TxBDRead)
665
    TxLength <=#Tp ram_do[31:16];
666 38 mohor
  else
667 39 mohor
  if(MasterWbTX & m_wb_ack_i)
668
    begin
669
      if(TxLengthLt4)
670
        TxLength <=#Tp 16'h0;
671
      else
672
        TxLength <=#Tp TxLength - 3'h4;    // Length is subtracted at the data request
673
    end
674 38 mohor
end
675
 
676 39 mohor
assign TxLengthEq0 = TxLength == 0;
677
assign TxLengthLt4 = TxLength < 4;
678 38 mohor
 
679 39 mohor
 
680
reg BlockingIncrementTxPointer;
681
 
682
reg [31:0] TxPointer;
683
reg [31:0] RxPointer;
684
 
685
//Latching Tx buffer pointer from buffer descriptor;
686 38 mohor
always @ (posedge WB_CLK_I or posedge WB_RST_I)
687
begin
688
  if(WB_RST_I)
689 39 mohor
    TxPointer <=#Tp 0;
690 38 mohor
  else
691 39 mohor
  if(TxEn & TxEn_q & TxPointerRead)
692
    TxPointer <=#Tp ram_do;
693 38 mohor
  else
694 39 mohor
  if(MasterWbTX & ~BlockingIncrementTxPointer)
695
    TxPointer <=#Tp TxPointer + 4;    // Pointer increment
696 38 mohor
end
697
 
698 39 mohor
wire MasterAccessFinished;
699 38 mohor
 
700 39 mohor
 
701
//Latching Tx buffer pointer from buffer descriptor;
702 38 mohor
always @ (posedge WB_CLK_I or posedge WB_RST_I)
703
begin
704
  if(WB_RST_I)
705 39 mohor
    BlockingIncrementTxPointer <=#Tp 0;
706 38 mohor
  else
707 39 mohor
  if(MasterAccessFinished)
708
    BlockingIncrementTxPointer <=#Tp 0;
709 38 mohor
  else
710 39 mohor
  if(MasterWbTX)
711
    BlockingIncrementTxPointer <=#Tp 1'b1;
712 38 mohor
end
713
 
714 39 mohor
wire RxPointerRead = 0; // igor !!! spremeni to da bo pravilno
715
//Latching Rx buffer pointer from buffer descriptor;
716
always @ (posedge WB_CLK_I or posedge WB_RST_I)
717
begin
718
  if(WB_RST_I)
719
    RxPointer <=#Tp 15'h0;
720
  else
721
  if(RxEn & RxEn_q & RxPointerRead)
722
    RxPointer <=#Tp ram_do;
723
end
724 38 mohor
 
725 39 mohor
wire TxBufferAlmostFull;
726
wire TxBufferFull;
727
wire TxBufferEmpty;
728
wire TxBufferAlmostEmpty;
729
wire ResetReadDataFromTxBuffer;
730
wire SetReadDataFromTxBuffer;
731
 
732
reg BlockReadDataFromTxBuffer;
733
 
734
//assign ResetReadDataFromTxBuffer = (TxLength < 4) | TxAbortPulse | TxRetryPulse;
735
assign ResetReadDataFromTxBuffer = (TxLengthEq0) | TxAbortPulse | TxRetryPulse;
736
assign SetReadDataFromTxBuffer = TxEn & TxEn_q & TxPointerRead;
737
 
738 38 mohor
always @ (posedge WB_CLK_I or posedge WB_RST_I)
739
begin
740
  if(WB_RST_I)
741 39 mohor
    ReadDataFromTxBuffer <=#Tp 1'b0;
742 38 mohor
  else
743 39 mohor
  if(ResetReadDataFromTxBuffer)
744
    ReadDataFromTxBuffer <=#Tp 1'b0;
745
  else
746
  if(SetReadDataFromTxBuffer)
747
    ReadDataFromTxBuffer <=#Tp 1'b1;
748 38 mohor
end
749
 
750 39 mohor
wire ReadDataFromTxBuffer_2 = ReadDataFromTxBuffer & ~BlockReadDataFromTxBuffer;
751
wire [31:0] TxData_wb;
752
wire ReadTxDataFromFifo_wb;
753 38 mohor
 
754
always @ (posedge WB_CLK_I or posedge WB_RST_I)
755
begin
756
  if(WB_RST_I)
757 39 mohor
    BlockReadDataFromTxBuffer <=#Tp 1'b0;
758 38 mohor
  else
759 39 mohor
  if(ReadTxDataFromFifo_wb)
760
    BlockReadDataFromTxBuffer <=#Tp 1'b0;
761 38 mohor
  else
762 39 mohor
//  if((TxBufferAlmostFull | TxLengthLt4)& MasterWbTX)
763
  if((TxBufferAlmostFull | TxLength <= 4)& MasterWbTX)
764
    BlockReadDataFromTxBuffer <=#Tp 1'b1;
765
end
766
 
767
 
768
 
769
assign MasterAccessFinished = m_wb_ack_i | m_wb_err_i;
770
 
771
assign m_wb_sel_o = 4'hf;
772
 
773
 
774
 reg [3:0]debug;
775
 
776
 
777
// Enabling master wishbone access to the memory for two devices TX and RX.
778
always @ (posedge WB_CLK_I or posedge WB_RST_I)
779
begin
780
  if(WB_RST_I)
781 38 mohor
    begin
782 39 mohor
      MasterWbTX <=#Tp 1'b0;
783
      MasterWbRX <=#Tp 1'b0;
784
      m_wb_dat_o <=#Tp 32'h0;
785
      m_wb_adr_o <=#Tp 32'h0;
786
      m_wb_cyc_o <=#Tp 1'b0;
787
      m_wb_stb_o <=#Tp 1'b0;
788
      m_wb_we_o  <=#Tp 1'b0;
789
      debug <=#Tp 0;
790 38 mohor
    end
791 39 mohor
  else
792
    begin
793
      // Switching between two stages depends on enable signals
794
      casex ({MasterWbTX, MasterWbRX, ReadDataFromTxBuffer_2, WriteDataToRxBuffer, MasterAccessFinished})  // synopsys parallel_case full_case
795
        5'b00_x1_x :
796
          begin
797
            MasterWbTX <=#Tp 1'b0;  // idle and master write is needed (data write to rx buffer)
798
            MasterWbRX <=#Tp 1'b1;
799
            m_wb_dat_o <=#Tp rx_fifo_data_out;
800
            m_wb_adr_o <=#Tp RxPointer;
801
            m_wb_cyc_o <=#Tp 1'b1;
802
            m_wb_stb_o <=#Tp 1'b1;
803
            m_wb_we_o  <=#Tp 1'b1;
804
            debug <=#Tp 1;
805
          end
806
        5'b00_10_x :
807
          begin
808
            $display("\n\tHere we go again");
809
            MasterWbTX <=#Tp 1'b1;  // idle and master read is needed (data read from tx buffer)
810
            MasterWbRX <=#Tp 1'b0;
811
            m_wb_adr_o <=#Tp TxPointer;
812
            m_wb_cyc_o <=#Tp 1'b1;
813
            m_wb_stb_o <=#Tp 1'b1;
814
            m_wb_we_o  <=#Tp 1'b0;
815
            debug <=#Tp 2;
816
          end
817
        5'b10_10_1 :
818
          begin
819
            $display("\n\tHere we go again");
820
            MasterWbTX <=#Tp 1'b1;  // master read and master read is needed (data read from tx buffer)
821
            MasterWbRX <=#Tp 1'b0;
822
            m_wb_adr_o <=#Tp TxPointer;
823
            m_wb_cyc_o <=#Tp 1'b1;
824
            m_wb_stb_o <=#Tp 1'b1;
825
            m_wb_we_o  <=#Tp 1'b0;
826
            debug <=#Tp 6;
827
          end
828
        5'b01_01_1 :
829
          begin
830
            MasterWbTX <=#Tp 1'b0;  // master write and master write is needed (data write to rx buffer)
831
            MasterWbRX <=#Tp 1'b1;
832
            m_wb_dat_o <=#Tp rx_fifo_data_out;
833
            m_wb_adr_o <=#Tp RxPointer;
834
            m_wb_we_o  <=#Tp 1'b1;
835
            debug <=#Tp 7;
836
          end
837
        5'b10_x1_1 :
838
          begin
839
            MasterWbTX <=#Tp 1'b0;  // master read and master write is needed (data write to rx buffer)
840
            MasterWbRX <=#Tp 1'b1;
841
            m_wb_dat_o <=#Tp rx_fifo_data_out;
842
            m_wb_adr_o <=#Tp RxPointer;
843
            m_wb_we_o  <=#Tp 1'b1;
844
            debug <=#Tp 3;
845
          end
846
        5'b01_1x_1 :
847
          begin
848
            MasterWbTX <=#Tp 1'b1;  // master write and master read is needed (data read from tx buffer)
849
            MasterWbRX <=#Tp 1'b0;
850
            m_wb_adr_o <=#Tp TxPointer;
851
            m_wb_we_o  <=#Tp 1'b0;
852
            debug <=#Tp 4;
853
          end
854
        5'bxx_00_1 :
855
          begin
856
            MasterWbTX <=#Tp 1'b0;  // whatever and no master read or write is needed (ack or err comes finishing previous access)
857
            MasterWbRX <=#Tp 1'b0;
858
            m_wb_cyc_o <=#Tp 1'b0;
859
            m_wb_stb_o <=#Tp 1'b0;
860
            debug <=#Tp 8;
861
          end
862
      endcase
863
    end
864 38 mohor
end
865
 
866 39 mohor
wire TxFifoClear;
867
assign TxFifoClear = (TxAbort_wb | TxRetry_wb) & ~TxBDReady;
868
eth_fifo tx_fifo (.data_in(m_wb_dat_i),   .data_out(TxData_wb),            .clk(WB_CLK_I),
869
                  .reset(WB_RST_I),       .write(MasterWbTX & m_wb_ack_i), .read(ReadTxDataFromFifo_wb),
870
                  .clear(TxFifoClear),    .full(TxBufferFull),             .almost_full(TxBufferAlmostFull),
871
                  .almost_empty(TxBufferAlmostEmpty),                      .empty(TxBufferEmpty));
872 38 mohor
 
873 39 mohor
 
874
 
875
 
876
 
877 38 mohor
// Latching Rx buffer descriptor status
878
// Data is avaliable one cycle after the access is started (at that time signal RxEn is not active)
879
always @ (posedge WB_CLK_I or posedge WB_RST_I)
880
begin
881
  if(WB_RST_I)
882
    RxStatus <=#Tp 16'h0;
883
  else
884
  if(RxBDRead & RxEn)
885
    RxStatus <=#Tp BDDataOut[15:0];
886
end
887
 
888
 
889 39 mohor
reg StartOccured;
890
reg TxStartFrm_sync1;
891
reg TxStartFrm_sync2;
892
reg TxStartFrm_syncb1;
893
reg TxStartFrm_syncb2;
894
 
895
 
896
 
897
// Start: Generation of the TxStartFrm_wb which is then synchronized to the MTxClk
898 38 mohor
always @ (posedge WB_CLK_I or posedge WB_RST_I)
899
begin
900
  if(WB_RST_I)
901 39 mohor
    TxStartFrm_wb <=#Tp 1'b0;
902 38 mohor
  else
903 39 mohor
  if(TxBDReady & ~StartOccured & (TxBufferFull | TxLengthEq0))
904
    TxStartFrm_wb <=#Tp 1'b1;
905 38 mohor
  else
906 39 mohor
  if(TxStartFrm_syncb2)
907
    TxStartFrm_wb <=#Tp 1'b0;
908 38 mohor
end
909
 
910 39 mohor
// StartOccured: TxStartFrm_wb occurs only ones at the beginning. Then it's blocked.
911 38 mohor
always @ (posedge WB_CLK_I or posedge WB_RST_I)
912
begin
913
  if(WB_RST_I)
914 39 mohor
    StartOccured <=#Tp 1'b0;
915 38 mohor
  else
916 39 mohor
  if(TxStartFrm_wb)
917
    StartOccured <=#Tp 1'b1;
918 38 mohor
  else
919 39 mohor
  if(ResetTxBDReady)
920
    StartOccured <=#Tp 1'b0;
921 38 mohor
end
922
 
923 39 mohor
// Synchronizing TxStartFrm_wb to MTxClk
924
always @ (posedge MTxClk or posedge WB_RST_I)
925
begin
926
  if(WB_RST_I)
927
    TxStartFrm_sync1 <=#Tp 1'b0;
928
  else
929
    TxStartFrm_sync1 <=#Tp TxStartFrm_wb;
930
end
931 38 mohor
 
932 39 mohor
always @ (posedge MTxClk or posedge WB_RST_I)
933
begin
934
  if(WB_RST_I)
935
    TxStartFrm_sync2 <=#Tp 1'b0;
936
  else
937
    TxStartFrm_sync2 <=#Tp TxStartFrm_sync1;
938
end
939
 
940 38 mohor
always @ (posedge WB_CLK_I or posedge WB_RST_I)
941
begin
942
  if(WB_RST_I)
943 39 mohor
    TxStartFrm_syncb1 <=#Tp 1'b0;
944 38 mohor
  else
945 39 mohor
    TxStartFrm_syncb1 <=#Tp TxStartFrm_sync2;
946 38 mohor
end
947
 
948
always @ (posedge WB_CLK_I or posedge WB_RST_I)
949
begin
950
  if(WB_RST_I)
951 39 mohor
    TxStartFrm_syncb2 <=#Tp 1'b0;
952 38 mohor
  else
953 39 mohor
    TxStartFrm_syncb2 <=#Tp TxStartFrm_syncb1;
954
end
955
 
956
always @ (posedge MTxClk or posedge WB_RST_I)
957
begin
958
  if(WB_RST_I)
959
    TxStartFrm <=#Tp 1'b0;
960 38 mohor
  else
961 39 mohor
  if(TxStartFrm_sync2)
962
    TxStartFrm <=#Tp 1'b1;      // igor !!! Dodaj se pogoj, da ni vmes prisel kaksen abort ali kaj podobnega
963
  else
964
  if(TxUsedData_q)
965
    TxStartFrm <=#Tp 1'b0;
966 38 mohor
end
967 39 mohor
// End: Generation of the TxStartFrm_wb which is then synchronized to the MTxClk
968 38 mohor
 
969
 
970
 
971
 
972 39 mohor
 
973
 
974
 
975
 
976
 
977
 
978
 
979
 
980
 
981
 
982
 
983
 
984
 
985
// TxEndFrm_wb: indicator of the end of frame
986 38 mohor
always @ (posedge WB_CLK_I or posedge WB_RST_I)
987
begin
988
  if(WB_RST_I)
989 39 mohor
    TxEndFrm_wb <=#Tp 1'b0;
990 38 mohor
  else
991 39 mohor
  if(TxLengthLt4 & TxBufferAlmostEmpty & TxUsedData)
992
    TxEndFrm_wb <=#Tp 1'b1;
993 38 mohor
  else
994 39 mohor
  if(TxRetryPulse | TxDonePulse | TxAbortPulse)
995
    TxEndFrm_wb <=#Tp 1'b0;
996 38 mohor
end
997
 
998
 
999
// Marks which bytes are valid within the word.
1000 39 mohor
assign TxValidBytes = TxLengthLt4 ? TxLength[1:0] : 2'b0;
1001 38 mohor
 
1002 39 mohor
reg LatchValidBytes;
1003
reg LatchValidBytes_q;
1004 38 mohor
 
1005
always @ (posedge WB_CLK_I or posedge WB_RST_I)
1006
begin
1007
  if(WB_RST_I)
1008 39 mohor
    LatchValidBytes <=#Tp 1'b0;
1009 38 mohor
  else
1010 39 mohor
  if(TxLengthLt4 & TxBDReady)
1011
    LatchValidBytes <=#Tp 1'b1;
1012 38 mohor
  else
1013 39 mohor
    LatchValidBytes <=#Tp 1'b0;
1014 38 mohor
end
1015
 
1016
always @ (posedge WB_CLK_I or posedge WB_RST_I)
1017
begin
1018
  if(WB_RST_I)
1019 39 mohor
    LatchValidBytes_q <=#Tp 1'b0;
1020 38 mohor
  else
1021 39 mohor
    LatchValidBytes_q <=#Tp LatchValidBytes;
1022 38 mohor
end
1023
 
1024
 
1025 39 mohor
// Latching valid bytes
1026 38 mohor
always @ (posedge WB_CLK_I or posedge WB_RST_I)
1027
begin
1028
  if(WB_RST_I)
1029 39 mohor
    TxValidBytesLatched <=#Tp 2'h0;
1030 38 mohor
  else
1031 39 mohor
  if(LatchValidBytes & ~LatchValidBytes_q)
1032
    TxValidBytesLatched <=#Tp TxValidBytes;
1033
  else
1034
  if(TxRetryPulse | TxDonePulse | TxAbortPulse)
1035
    TxValidBytesLatched <=#Tp 2'h0;
1036 38 mohor
end
1037
 
1038
 
1039
// Bit 14 is used as a wrap bit. When active it indicates the last buffer descriptor in a row. After
1040
// using this descriptor, first BD will be used again.
1041
 
1042
 
1043
 
1044
// TX
1045
// bit 15 od tx je ready
1046
// bit 14 od tx je interrupt (Tx buffer ali tx error bit se postavi v interrupt registru, ko se ta buffer odda)
1047
// bit 13 od tx je wrap
1048
// bit 12 od tx je pad
1049
// bit 11 od tx je crc
1050
// bit 10 od tx je last (crc se doda le ce je bit 11 in hkrati bit 10)
1051
// bit 9  od tx je pause request (control frame)
1052
    // Vsi zgornji biti gredo ven, spodnji biti (od 8 do 0) pa so statusni in se vpisejo po koncu oddajanja
1053
// bit 8  od tx je defer indication
1054
// bit 7  od tx je late collision
1055
// bit 6  od tx je retransmittion limit
1056
// bit 5  od tx je underrun
1057
// bit 4  od tx je carrier sense lost
1058
// bit [3:0] od tx je retry count
1059
 
1060
//assign TxBDReady      = TxStatus[15];     // already used
1061
assign TxIRQEn          = TxStatus[14];
1062
assign WrapTxStatusBit  = TxStatus[13];                                                   // ok povezan
1063
assign PerPacketPad     = TxStatus[12];                                                   // ok povezan
1064
assign PerPacketCrcEn   = TxStatus[11] & TxStatus[10];      // When last is also set      // ok povezan
1065
//assign TxPauseRq      = TxStatus[9];      // already used
1066
 
1067
 
1068
 
1069
// RX
1070
// bit 15 od rx je empty
1071
// bit 14 od rx je interrupt (Rx buffer ali rx frame received se postavi v interrupt registru, ko se ta buffer zapre)
1072
// bit 13 od rx je wrap
1073
// bit 12 od rx je reserved
1074
// bit 11 od rx je reserved
1075
// bit 10 od rx je last (crc se doda le ce je bit 11 in hkrati bit 10)
1076
// bit 9  od rx je pause request (control frame)
1077
    // Vsi zgornji biti gredo ven, spodnji biti (od 8 do 0) pa so statusni in se vpisejo po koncu oddajanja
1078
// bit 8  od rx je defer indication
1079
// bit 7  od rx je late collision
1080
// bit 6  od rx je retransmittion limit
1081
// bit 5  od rx je underrun
1082
// bit 4  od rx je carrier sense lost
1083
// bit [3:0] od rx je retry count
1084
 
1085
assign WrapRxStatusBit = RxStatus[13];
1086
 
1087
 
1088
// Temporary Tx and Rx buffer descriptor address 
1089 39 mohor
assign TempTxBDAddress[7:0] = {8{ TxStatusWrite     & ~WrapTxStatusBit}} & (TxBDAddress + 2'h2) ; // Tx BD increment or wrap (last BD)
1090 38 mohor
assign TempRxBDAddress[7:0] = {8{ WrapRxStatusBit}} & (r_TxBDNum)       | // Using first Rx BD
1091 39 mohor
                              {8{~WrapRxStatusBit}} & (RxBDAddress + 2'h2) ; // Using next Rx BD (incremenrement address)
1092 38 mohor
 
1093
 
1094
// Latching Tx buffer descriptor address
1095
always @ (posedge WB_CLK_I or posedge WB_RST_I)
1096
begin
1097
  if(WB_RST_I)
1098
    TxBDAddress <=#Tp 8'h0;
1099
  else
1100
  if(TxStatusWrite)
1101
    TxBDAddress <=#Tp TempTxBDAddress;
1102
end
1103
 
1104
 
1105
// Latching Rx buffer descriptor address
1106
always @ (posedge WB_CLK_I or posedge WB_RST_I)
1107
begin
1108
  if(WB_RST_I)
1109
    RxBDAddress <=#Tp 8'h0;
1110
  else
1111
  if(TX_BD_NUM_Wr)                        // When r_TxBDNum is updated, RxBDAddress is also
1112
    RxBDAddress <=#Tp WB_DAT_I[7:0];
1113
  else
1114
  if(RxStatusWrite)
1115
    RxBDAddress <=#Tp TempRxBDAddress;
1116
end
1117
 
1118
 
1119
// Selecting Tx or Rx buffer descriptor address
1120
always @ (posedge WB_CLK_I or posedge WB_RST_I)
1121
begin
1122
  if(WB_RST_I)
1123
    BDAddress <=#Tp 8'h0;
1124
  else
1125
  if(TxEn)
1126
    BDAddress <=#Tp TxBDAddress;
1127
  else
1128
    BDAddress <=#Tp RxBDAddress;
1129
end
1130
 
1131
 
1132
assign RxLength[15:0]  = 16'h1399;
1133
assign NewRxStatus[15:0] = {1'b0, WbWriteError, RxStatus[13:0]};
1134
 
1135
 
1136
//assign BDDataIn  = TxStatusWrite ? {TxLength[15:0], StatusIzTxEthMACModula} : {RxLength, NewRxStatus};
1137 39 mohor
//assign BDDataIn  = TxStatusWrite ? {TxStatus[31:9], 9'h0} 
1138
//                                 : {RxLength, NewRxStatus};
1139
assign RxBDDataIn = {RxLength, NewRxStatus};  // tu dopolni, da se bo vpisoval status
1140
//assign TxBDDataIn = {16'h0, TxStatus[15:9], 9'h0};   // tu dopolni, da se bo vpisoval status
1141
//assign TxBDDataIn = {32'hdead00ef};   // tu dopolni, da se bo vpisoval status
1142
assign TxBDDataIn = {32'h004380ef};   // tu dopolni, da se bo vpisoval status
1143 38 mohor
 
1144
 
1145
// Generating delayed signals
1146
always @ (posedge WB_CLK_I or posedge WB_RST_I)
1147
begin
1148
  if(WB_RST_I)
1149
    begin
1150 39 mohor
      TxRetry_wb_q          <=#Tp 1'b0;
1151 38 mohor
      BDRead_q              <=#Tp 1'b0;
1152
    end
1153
  else
1154
    begin
1155 39 mohor
      TxRetry_wb_q          <=#Tp TxRetry_wb;
1156 38 mohor
      BDRead_q              <=#Tp BDRead;
1157
    end
1158
end
1159
 
1160
 
1161
// Signals used for various purposes
1162 39 mohor
assign TxRetryPulse   = TxRetry_wb   & ~TxRetry_wb_q;
1163 38 mohor
assign TxDonePulse    = TxDone_wb    & ~TxDone_wb_q;
1164
assign TxAbortPulse   = TxAbort_wb   & ~TxAbort_wb_q;
1165
 
1166
 
1167
// Next descriptor for Tx DMA channel
1168
always @ (posedge WB_CLK_I or posedge WB_RST_I)
1169
begin
1170
  if(WB_RST_I)
1171
    WB_ND_O_TX <=#Tp 1'b0;
1172
  else
1173
  if(TxDonePulse | TxAbortPulse)
1174
    WB_ND_O_TX <=#Tp 1'b1;
1175
  else
1176
  if(WB_ND_O_TX)
1177
    WB_ND_O_TX <=#Tp 1'b0;
1178
end
1179
 
1180
 
1181
// Force next descriptor on DMA channel 0 (Tx)
1182
assign WB_ND_O[0] = WB_ND_O_TX;
1183
 
1184
 
1185
 
1186
// Restart descriptor for DMA channel 0 (Tx)
1187
always @ (posedge WB_CLK_I or posedge WB_RST_I)
1188
begin
1189
  if(WB_RST_I)
1190
    WB_RD_O <=#Tp 1'b0;
1191
  else
1192 39 mohor
  if(TxRetryPulse)
1193 38 mohor
    WB_RD_O <=#Tp 1'b1;
1194
  else
1195
  if(WB_RD_O)
1196
    WB_RD_O <=#Tp 1'b0;
1197
end
1198
 
1199
 
1200 39 mohor
// assign ClearTxBDReady = ~TxUsedData & TxUsedData_q;
1201 38 mohor
 
1202 39 mohor
assign TPauseRq = 0; // igor !!! v koncni fazi mora tu biti pause request
1203
assign TxPauseTV[15:0] = TxLength[15:0]; // igor !!! v koncni fazi mora tu biti pause request
1204 38 mohor
 
1205 39 mohor
// reg  WillSendControlFrameSync1;
1206
// reg  WillSendControlFrameSync2;
1207
// reg  WillSendControlFrameSync3;
1208
// wire WillSendControlFrame_wb;
1209 38 mohor
 
1210
 
1211 39 mohor
// always @ (posedge WB_CLK_I or posedge WB_RST_I)
1212
// begin
1213
//   if(WB_RST_I)
1214
//     WillSendControlFrameSync1 <=#Tp 1'b0;
1215
//   else
1216
//     WillSendControlFrameSync1 <=#Tp WillSendControlFrame;
1217
// end
1218
// 
1219
// always @ (posedge WB_CLK_I or posedge WB_RST_I)
1220
// begin
1221
//   if(WB_RST_I)
1222
//     WillSendControlFrameSync2 <=#Tp 1'b0;
1223
//   else
1224
//     WillSendControlFrameSync2 <=#Tp WillSendControlFrameSync1;
1225
// end
1226
// 
1227
// always @ (posedge WB_CLK_I or posedge WB_RST_I)
1228
// begin
1229
//   if(WB_RST_I)
1230
//     WillSendControlFrameSync3 <=#Tp 1'b0;
1231
//   else
1232
//     WillSendControlFrameSync3 <=#Tp WillSendControlFrameSync2;
1233
// end
1234
// 
1235
// assign WillSendControlFrame_wb = WillSendControlFrameSync2 & ~WillSendControlFrameSync3;
1236 38 mohor
 
1237
 
1238
 
1239
 
1240
 
1241
 
1242 39 mohor
 
1243
 
1244
// Generating delayed signals
1245 38 mohor
always @ (posedge MTxClk or posedge WB_RST_I)
1246
begin
1247
  if(WB_RST_I)
1248 39 mohor
    begin
1249
      TxAbort_q      <=#Tp 1'b0;
1250
      TxRetry_q      <=#Tp 1'b0;
1251
      TxUsedData_q   <=#Tp 1'b0;
1252
      TxCtrlEndFrm_q <=#Tp 1'b0;
1253
    end
1254 38 mohor
  else
1255 39 mohor
    begin
1256
      TxAbort_q      <=#Tp TxAbort;
1257
      TxRetry_q      <=#Tp TxRetry;
1258
      TxUsedData_q   <=#Tp TxUsedData;
1259
      TxCtrlEndFrm_q <=#Tp TxCtrlEndFrm;
1260
    end
1261 38 mohor
end
1262
 
1263
// Generating delayed signals
1264 39 mohor
always @ (posedge WB_CLK_I or posedge WB_RST_I)
1265 38 mohor
begin
1266
  if(WB_RST_I)
1267
    begin
1268 39 mohor
      TxDone_wb_q   <=#Tp 1'b0;
1269
      TxAbort_wb_q  <=#Tp 1'b0;
1270 38 mohor
    end
1271
  else
1272
    begin
1273 39 mohor
      TxDone_wb_q   <=#Tp TxDone_wb;
1274
      TxAbort_wb_q  <=#Tp TxAbort_wb;
1275 38 mohor
    end
1276
end
1277
 
1278
 
1279
// Sinchronizing and evaluating tx data
1280 39 mohor
//assign SetGotData = (TxStartFrm_wb | NewTxDataAvaliable_wb & ~TxAbort_wb & ~TxRetry_wb) & ~WB_CLK_I;
1281
assign SetGotData = (TxStartFrm_wb); // igor namesto zgornje
1282 38 mohor
 
1283
eth_sync_clk1_clk2 syn2 (.clk1(MTxClk),     .clk2(WB_CLK_I),            .reset1(WB_RST_I),    .reset2(WB_RST_I),
1284
                         .set2(SetGotData), .sync_out(GotDataSync3));
1285
 
1286
 
1287
// Evaluating data. If abort or retry occured meanwhile than data is ignored.
1288 39 mohor
assign GotDataEvaluate = GotDataSync3 & ~GotData & (~TxRetry & ~TxAbort | (TxRetry | TxAbort) & (TxStartFrm));
1289 38 mohor
 
1290
 
1291
// Indication of good data
1292
always @ (posedge MTxClk or posedge WB_RST_I)
1293
begin
1294
  if(WB_RST_I)
1295
    GotData <=#Tp 1'b0;
1296
  else
1297
  if(GotDataEvaluate)
1298
    GotData <=#Tp 1'b1;
1299
  else
1300
    GotData <=#Tp 1'b0;
1301
end
1302
 
1303
 
1304 39 mohor
// // Tx start frame generation
1305
// always @ (posedge MTxClk or posedge WB_RST_I)
1306
// begin
1307
//   if(WB_RST_I)
1308
//     TxStartFrm <=#Tp 1'b0;
1309
//   else
1310
//   if(TxUsedData_q | TxAbort & ~TxAbort_q | TxRetry & ~TxRetry_q)
1311
//     TxStartFrm <=#Tp 1'b0;
1312
//   else
1313
//   if(TxBDReady & GotData & TxStartFrmRequest)
1314
//     TxStartFrm <=#Tp 1'b1;
1315
// end
1316
// 
1317 38 mohor
 
1318
// Indication of the last word
1319
always @ (posedge MTxClk or posedge WB_RST_I)
1320
begin
1321
  if(WB_RST_I)
1322
    LastWord <=#Tp 1'b0;
1323
  else
1324
  if((TxEndFrm | TxAbort | TxRetry) & Flop)
1325
    LastWord <=#Tp 1'b0;
1326
  else
1327
  if(TxUsedData & Flop & TxByteCnt == 2'h3)
1328 39 mohor
//    LastWord <=#Tp TxEndFrm_wbLatched;
1329
    LastWord <=#Tp TxEndFrm_wb;
1330 38 mohor
end
1331
 
1332
 
1333
// Tx end frame generation
1334
always @ (posedge MTxClk or posedge WB_RST_I)
1335
begin
1336
  if(WB_RST_I)
1337
    TxEndFrm <=#Tp 1'b0;
1338
  else
1339 39 mohor
  if(Flop & TxEndFrm | TxAbort | TxRetry_q)     // igor !!! zakaj je tu TxRetry_q ?
1340 38 mohor
    TxEndFrm <=#Tp 1'b0;
1341
  else
1342
  if(Flop & LastWord)
1343
    begin
1344
      case (TxValidBytesLatched)
1345
        1 : TxEndFrm <=#Tp TxByteCnt == 2'h0;
1346
        2 : TxEndFrm <=#Tp TxByteCnt == 2'h1;
1347
        3 : TxEndFrm <=#Tp TxByteCnt == 2'h2;
1348
 
1349
        default : TxEndFrm <=#Tp 1'b0;
1350
      endcase
1351
    end
1352
end
1353
 
1354
 
1355
// Tx data selection (latching)
1356
always @ (posedge MTxClk or posedge WB_RST_I)
1357
begin
1358
  if(WB_RST_I)
1359
    TxData <=#Tp 8'h0;
1360
  else
1361 39 mohor
  if(TxStartFrm_sync2 & ~TxStartFrm)
1362
    TxData <=#Tp TxData_wb[7:0];
1363 38 mohor
  else
1364
  if(TxUsedData & Flop)
1365
    begin
1366
      case(TxByteCnt)
1367
 
1368
        1 : TxData <=#Tp TxDataLatched[15:8];
1369
        2 : TxData <=#Tp TxDataLatched[23:16];
1370
        3 : TxData <=#Tp TxDataLatched[31:24];
1371
      endcase
1372
    end
1373
end
1374
 
1375
 
1376
// Latching tx data
1377
always @ (posedge MTxClk or posedge WB_RST_I)
1378
begin
1379
  if(WB_RST_I)
1380
    TxDataLatched[31:0] <=#Tp 32'h0;
1381
  else
1382 39 mohor
  if(TxStartFrm_sync2 & ~TxStartFrm | TxUsedData & Flop & TxByteCnt == 2'h3)
1383
    TxDataLatched[31:0] <=#Tp TxData_wb[31:0];
1384 38 mohor
end
1385
 
1386
 
1387
// Tx under run
1388 39 mohor
always @ (posedge WB_CLK_I or posedge WB_RST_I)
1389 38 mohor
begin
1390
  if(WB_RST_I)
1391
    TxUnderRun <=#Tp 1'b0;
1392
  else
1393 39 mohor
  if(TxAbortPulse)
1394 38 mohor
    TxUnderRun <=#Tp 1'b0;
1395
  else
1396 39 mohor
  if(TxBufferEmpty & ReadTxDataFromFifo_wb)
1397 38 mohor
    TxUnderRun <=#Tp 1'b1;
1398
end
1399
 
1400
 
1401
 
1402
// Tx Byte counter
1403
always @ (posedge MTxClk or posedge WB_RST_I)
1404
begin
1405
  if(WB_RST_I)
1406
    TxByteCnt <=#Tp 2'h0;
1407
  else
1408
  if(TxAbort_q | TxRetry_q)
1409
    TxByteCnt <=#Tp 2'h0;
1410
  else
1411
  if(TxStartFrm & ~TxUsedData)
1412
    TxByteCnt <=#Tp 2'h1;
1413
  else
1414
  if(TxUsedData & Flop)
1415
    TxByteCnt <=#Tp TxByteCnt + 1;
1416
end
1417
 
1418
 
1419 39 mohor
// Start: Generation of the ReadTxDataFromFifo_tck signal and synchronization to the WB_CLK_I
1420
reg ReadTxDataFromFifo_sync1;
1421
reg ReadTxDataFromFifo_sync2;
1422
reg ReadTxDataFromFifo_sync3;
1423
reg ReadTxDataFromFifo_syncb1;
1424
reg ReadTxDataFromFifo_syncb2;
1425
 
1426
 
1427 38 mohor
always @ (posedge MTxClk or posedge WB_RST_I)
1428
begin
1429
  if(WB_RST_I)
1430 39 mohor
    ReadTxDataFromFifo_tck <=#Tp 1'b0;
1431 38 mohor
  else
1432 39 mohor
  if(ReadTxDataFromFifo_syncb2)
1433
    ReadTxDataFromFifo_tck <=#Tp 1'b0;
1434 38 mohor
  else
1435 39 mohor
//  if(TxUsedData & ~TxEndFrm_wbLatched & TxByteCnt == 2'h3)
1436
//    ReadTxDataFromFifo_tck <=#Tp ~LastWord;
1437
//  if(TxStartFrm_sync2 & ~TxStartFrm | TxUsedData & Flop & TxByteCnt == 2'h3)
1438
  if(TxStartFrm_sync2 & ~TxStartFrm | TxUsedData & Flop & TxByteCnt == 2'h3 & ~LastWord)
1439
     ReadTxDataFromFifo_tck <=#Tp 1'b1;
1440 38 mohor
end
1441
 
1442 39 mohor
// Synchronizing TxStartFrm_wb to MTxClk
1443
always @ (posedge WB_CLK_I or posedge WB_RST_I)
1444 38 mohor
begin
1445
  if(WB_RST_I)
1446 39 mohor
    ReadTxDataFromFifo_sync1 <=#Tp 1'b0;
1447 38 mohor
  else
1448 39 mohor
    ReadTxDataFromFifo_sync1 <=#Tp ReadTxDataFromFifo_tck;
1449
end
1450 38 mohor
 
1451 39 mohor
always @ (posedge WB_CLK_I or posedge WB_RST_I)
1452 38 mohor
begin
1453 39 mohor
  if(WB_RST_I)
1454
    ReadTxDataFromFifo_sync2 <=#Tp 1'b0;
1455 38 mohor
  else
1456 39 mohor
    ReadTxDataFromFifo_sync2 <=#Tp ReadTxDataFromFifo_sync1;
1457 38 mohor
end
1458
 
1459 39 mohor
always @ (posedge MTxClk or posedge WB_RST_I)
1460 38 mohor
begin
1461
  if(WB_RST_I)
1462 39 mohor
    ReadTxDataFromFifo_syncb1 <=#Tp 1'b0;
1463 38 mohor
  else
1464 39 mohor
    ReadTxDataFromFifo_syncb1 <=#Tp ReadTxDataFromFifo_sync2;
1465 38 mohor
end
1466
 
1467 39 mohor
always @ (posedge MTxClk or posedge WB_RST_I)
1468 38 mohor
begin
1469
  if(WB_RST_I)
1470 39 mohor
    ReadTxDataFromFifo_syncb2 <=#Tp 1'b0;
1471 38 mohor
  else
1472 39 mohor
    ReadTxDataFromFifo_syncb2 <=#Tp ReadTxDataFromFifo_syncb1;
1473 38 mohor
end
1474
 
1475
always @ (posedge WB_CLK_I or posedge WB_RST_I)
1476
begin
1477
  if(WB_RST_I)
1478 39 mohor
    ReadTxDataFromFifo_sync3 <=#Tp 1'b0;
1479 38 mohor
  else
1480 39 mohor
    ReadTxDataFromFifo_sync3 <=#Tp ReadTxDataFromFifo_sync2;
1481 38 mohor
end
1482
 
1483 39 mohor
assign ReadTxDataFromFifo_wb = ReadTxDataFromFifo_sync2 & ~ReadTxDataFromFifo_sync3;
1484
// End: Generation of the ReadTxDataFromFifo_tck signal and synchronization to the WB_CLK_I
1485 38 mohor
 
1486
 
1487 39 mohor
// Synchronizing TxRetry signal (synchronized to WISHBONE clock)
1488 38 mohor
always @ (posedge WB_CLK_I or posedge WB_RST_I)
1489
begin
1490
  if(WB_RST_I)
1491 39 mohor
    TxRetrySync1 <=#Tp 1'b0;
1492 38 mohor
  else
1493 39 mohor
    TxRetrySync1 <=#Tp TxRetry;
1494 38 mohor
end
1495
 
1496 39 mohor
always @ (posedge WB_CLK_I or posedge WB_RST_I)
1497 38 mohor
begin
1498 39 mohor
  if(WB_RST_I)
1499
    TxRetry_wb <=#Tp 1'b0;
1500 38 mohor
  else
1501 39 mohor
    TxRetry_wb <=#Tp TxRetrySync1;
1502 38 mohor
end
1503
 
1504
 
1505 39 mohor
// Synchronized TxDone_wb signal (synchronized to WISHBONE clock)
1506 38 mohor
always @ (posedge WB_CLK_I or posedge WB_RST_I)
1507
begin
1508
  if(WB_RST_I)
1509 39 mohor
    TxDoneSync1 <=#Tp 1'b0;
1510 38 mohor
  else
1511 39 mohor
    TxDoneSync1 <=#Tp TxDone;
1512 38 mohor
end
1513
 
1514
always @ (posedge WB_CLK_I or posedge WB_RST_I)
1515
begin
1516
  if(WB_RST_I)
1517 39 mohor
    TxDone_wb <=#Tp 1'b0;
1518 38 mohor
  else
1519 39 mohor
    TxDone_wb <=#Tp TxDoneSync1;
1520 38 mohor
end
1521
 
1522 39 mohor
// Synchronizing TxAbort signal (synchronized to WISHBONE clock)
1523 38 mohor
always @ (posedge WB_CLK_I or posedge WB_RST_I)
1524
begin
1525
  if(WB_RST_I)
1526 39 mohor
    TxAbortSync1 <=#Tp 1'b0;
1527 38 mohor
  else
1528 39 mohor
    TxAbortSync1 <=#Tp TxAbort;
1529 38 mohor
end
1530
 
1531
always @ (posedge WB_CLK_I or posedge WB_RST_I)
1532
begin
1533
  if(WB_RST_I)
1534
    TxAbort_wb <=#Tp 1'b0;
1535
  else
1536 39 mohor
    TxAbort_wb <=#Tp TxAbortSync1;
1537 38 mohor
end
1538
 
1539
 
1540 39 mohor
 
1541
 
1542
 
1543
 
1544
 
1545
 
1546
 
1547
 
1548
 
1549
 
1550
 
1551
 
1552 38 mohor
// Reading of the next receive buffer descriptor starts after reception status is
1553
// written to the previous one.
1554
assign StartRxBDRead = RxEn & RxStatusWriteOccured;
1555
assign ResetRxBDRead = RxBDRead & RxBDReady;          // Rx BD is read until READY bit is set.
1556
 
1557
 
1558
// Latching READY status of the Rx buffer descriptor
1559
always @ (negedge WB_CLK_I or posedge WB_RST_I)
1560
begin
1561
  if(WB_RST_I)
1562
    RxBDReady <=#Tp 1'b0;
1563
  else
1564
  if(RxEn & RxBDRead)
1565
    RxBDReady <=#Tp BDDataOut[15];
1566
  else
1567
  if(RxStatusWrite)
1568
    RxBDReady <=#Tp 1'b0;
1569
end
1570
 
1571
 
1572
// Reading the Rx buffer descriptor
1573
always @ (posedge WB_CLK_I or posedge WB_RST_I)
1574
begin
1575
  if(WB_RST_I)
1576
    RxBDRead <=#Tp 1'b1;
1577
  else
1578
  if(StartRxBDRead)
1579
    RxBDRead <=#Tp 1'b1;
1580
  else
1581
  if(ResetRxBDRead)
1582
    RxBDRead <=#Tp 1'b0;
1583
end
1584
 
1585
 
1586
// Reception status is written back to the buffer descriptor after the end of frame is detected.
1587
//assign StartRxStatusWrite = RxEn & RxEndFrm_wb;
1588
assign StartRxStatusWrite = RxEn & RxEndFrm_wb;
1589
 
1590
 
1591
// Writing status back to the Rx buffer descriptor
1592
always @ (posedge WB_CLK_I or posedge WB_RST_I)
1593
begin
1594
  if(WB_RST_I)
1595
    RxStatusWrite <=#Tp 1'b0;
1596
  else
1597
  if(StartRxStatusWrite)
1598
    RxStatusWrite <=#Tp 1'b1;
1599
  else
1600
    RxStatusWrite <=#Tp 1'b0;
1601
end
1602
 
1603
 
1604
// Forcing next descriptor on DMA channel 1 (Rx)
1605
assign WB_ND_O[1] = RxStatusWrite;
1606
 
1607
 
1608
// Latched status that a status write occured.
1609
always @ (posedge WB_CLK_I or posedge WB_RST_I)
1610
begin
1611
  if(WB_RST_I)
1612
    RxStatusWriteOccured <=#Tp 1'b0;
1613
  else
1614
  if(StartRxStatusWrite)
1615
    RxStatusWriteOccured <=#Tp 1'b1;
1616
  else
1617
  if(StartRxBDRead)
1618
    RxStatusWriteOccured <=#Tp 1'b0;
1619
end
1620
 
1621
 
1622
 
1623
// Generation of the synchronized signal ShiftEnded that indicates end of reception
1624
eth_sync_clk1_clk2 syn8 (.clk1(MRxClk),       .clk2(WB_CLK_I),            .reset1(WB_RST_I),    .reset2(WB_RST_I),
1625
                         .set2(RxEndFrm_wb),  .sync_out(ShiftEnded)
1626
                        );
1627
 
1628
 
1629
// Indicating that last byte is being reveived
1630
always @ (posedge MRxClk or posedge WB_RST_I)
1631
begin
1632
  if(WB_RST_I)
1633
    LastByteIn <=#Tp 1'b0;
1634
  else
1635
  if(ShiftWillEnd & (&RxByteCnt))
1636
    LastByteIn <=#Tp 1'b0;
1637
  else
1638
  if(RxValid & RxBDReady & RxEndFrm & ~(&RxByteCnt))
1639
    LastByteIn <=#Tp 1'b1;
1640
end
1641
 
1642
 
1643
// Indicating that data reception will end
1644
always @ (posedge MRxClk or posedge WB_RST_I)
1645
begin
1646
  if(WB_RST_I)
1647
    ShiftWillEnd <=#Tp 1'b0;
1648
  else
1649
  if(ShiftEnded)
1650
    ShiftWillEnd <=#Tp 1'b0;
1651
  else
1652
  if(LastByteIn & (&RxByteCnt) | RxValid & RxEndFrm & (&RxByteCnt))
1653
    ShiftWillEnd <=#Tp 1'b1;
1654
end
1655
 
1656
 
1657
// Receive byte counter
1658
always @ (posedge MRxClk or posedge WB_RST_I)
1659
begin
1660
  if(WB_RST_I)
1661
    RxByteCnt <=#Tp 2'h0;
1662
  else
1663
  if(ShiftEnded)
1664
    RxByteCnt <=#Tp 2'h0;
1665
  else
1666
  if(RxValid & RxBDReady | LastByteIn)
1667
    RxByteCnt <=#Tp RxByteCnt + 1;
1668
end
1669
 
1670
 
1671
// Indicates how many bytes are valid within the last word
1672
always @ (posedge MRxClk or posedge WB_RST_I)
1673
begin
1674
  if(WB_RST_I)
1675
    RxValidBytes <=#Tp 2'h1;
1676
  else
1677
  if(ShiftEnded)
1678
    RxValidBytes <=#Tp 2'h1;
1679
  else
1680
  if(RxValid & ~LastByteIn & ~RxStartFrm)
1681
    RxValidBytes <=#Tp RxValidBytes + 1;
1682
end
1683
 
1684
 
1685
// There is a maximum 3 MRxClk delay between RxDataLatched2 and RxData_wb. In the meantime data
1686
// is stored to the RxDataLatched1. 
1687
always @ (posedge MRxClk or posedge WB_RST_I)
1688
begin
1689
  if(WB_RST_I)
1690
    RxDataLatched1       <=#Tp 16'h0;
1691
  else
1692
  if(RxValid & RxBDReady & ~LastByteIn & RxByteCnt == 2'h0)
1693
    RxDataLatched1[7:0]  <=#Tp RxData;
1694
  else
1695
  if(RxValid & RxBDReady & ~LastByteIn & RxByteCnt == 2'h1)
1696
    RxDataLatched1[15:8] <=#Tp RxData;
1697
end
1698
 
1699
 
1700
// Latching incoming data to buffer
1701
always @ (posedge MRxClk or posedge WB_RST_I)
1702
begin
1703
  if(WB_RST_I)
1704
    RxDataLatched2        <=#Tp 32'h0;
1705
  else
1706
  if(RxValid & RxBDReady & ~LastByteIn & RxByteCnt == 2'h2)
1707
    RxDataLatched2[23:0]  <=#Tp {RxData,RxDataLatched1};
1708
  else
1709
  if(RxValid & RxBDReady & ~LastByteIn & RxByteCnt == 2'h3)
1710
    RxDataLatched2[31:24] <=#Tp RxData;
1711
end
1712
 
1713
 
1714
// Indicating start of the reception process
1715
always @ (posedge MRxClk or posedge WB_RST_I)
1716
begin
1717
  if(WB_RST_I)
1718
    StartShifting <=#Tp 1'b0;
1719
  else
1720
  if((RxValid & RxBDReady & ~RxStartFrm & (&RxByteCnt)) | (ShiftWillEnd &  LastByteIn & (&RxByteCnt)))
1721
    StartShifting <=#Tp 1'b1;
1722
  else
1723
    StartShifting <=#Tp 1'b0;
1724
end
1725
 
1726
 
1727
// Synchronizing Rx start frame to the WISHBONE clock
1728
assign StartRxStartFrmSync1 = RxStartFrm & RxBDReady;
1729
 
1730
eth_sync_clk1_clk2 syn9 (.clk1(WB_CLK_I),     .clk2(MRxClk),            .reset1(WB_RST_I),    .reset2(WB_RST_I),
1731
                         .set2(SetGotData), .sync_out(RxStartFrmSync3)
1732
                        );
1733
 
1734
 
1735
// Generating synchronized Rx start frame
1736
always @ ( posedge WB_CLK_I or posedge WB_RST_I)
1737
begin
1738
  if(WB_RST_I)
1739
    RxStartFrm_wb <=#Tp 1'b0;
1740
  else
1741
  if(RxStartFrmSync3 & ~RxStartFrm_wb)
1742
    RxStartFrm_wb <=#Tp 1'b1;
1743
  else
1744
    RxStartFrm_wb <=#Tp 1'b0;
1745
end
1746
 
1747
 
1748
//Synchronizing signal for latching data that will be written to the WISHBONE
1749
//eth_sync_clk1_clk2 syn10 (.clk1(WB_CLK_I),     .clk2(MRxClk),            .reset1(WB_RST_I),    .reset2(WB_RST_I), 
1750
//                         .set2(StartShifting), .sync_out(LatchNow_wb)
1751
//                        );
1752
 
1753
// This section still needs to be changed due to ASIC demands
1754
assign ResetShifting_wb = LatchNow_wb | WB_RST_I;
1755
assign StartShifting_wb = StartShifting;
1756
 
1757
 
1758
// Sync. stage 1
1759
always @ (posedge StartShifting_wb or posedge ResetShifting_wb)
1760
begin
1761
  if(ResetShifting_wb)
1762
    Shifting_wb_Sync1 <=#Tp 1'b0;
1763
  else
1764
    Shifting_wb_Sync1 <=#Tp 1'b1;
1765
end
1766
 
1767
 
1768
// Sync. stage 2
1769
always @ (posedge WB_CLK_I or posedge WB_RST_I)
1770
begin
1771
  if(WB_RST_I)
1772
    Shifting_wb_Sync2 <=#Tp 1'b0;
1773
  else
1774
  if(Shifting_wb_Sync1 & ~RxDataValid_wb)
1775
    Shifting_wb_Sync2 <=#Tp 1'b1;
1776
  else
1777
    Shifting_wb_Sync2 <=#Tp 1'b0;
1778
end
1779
 
1780
 
1781
// Generating synchronized signal that will latch data for writing to the WISHBONE
1782
always @ (posedge WB_CLK_I or posedge WB_RST_I)
1783
begin
1784
  if(WB_RST_I)
1785
    LatchNow_wb <=#Tp 1'b0;
1786
  else
1787
  if(Shifting_wb_Sync2 & ~RxDataValid_wb)
1788
    LatchNow_wb <=#Tp 1'b1;
1789
  else
1790
    LatchNow_wb <=#Tp 1'b0;
1791
end
1792
 
1793
 
1794
// Indicating that valid data is avaliable
1795
always @ (posedge WB_CLK_I or posedge WB_RST_I)
1796
begin
1797
  if(WB_RST_I)
1798
    RxDataValid_wb <=#Tp 1'b0;
1799
  else
1800
  if(LatchNow_wb & ~RxDataValid_wb)
1801
    RxDataValid_wb <=#Tp 1'b1;
1802
  else
1803
  if(RxDataValid_wb)
1804
    RxDataValid_wb <=#Tp 1'b0;
1805
end
1806
 
1807
 
1808
// Forcing next descriptor in the DMA (Channel 1 is used for rx)
1809
always @ (posedge WB_CLK_I or posedge WB_RST_I)
1810
begin
1811
  if(WB_RST_I)
1812
    WB_REQ_O_RX <=#Tp 1'b0;
1813
  else
1814
  if(LatchNow_wb & ~RxDataValid_wb & r_DmaEn)
1815
    WB_REQ_O_RX <=#Tp 1'b1;
1816
  else
1817
  if(DMACycleFinishedRx)
1818
    WB_REQ_O_RX <=#Tp 1'b0;
1819
end
1820
 
1821
 
1822
assign WB_REQ_O[1] = WB_REQ_O_RX;
1823
assign DMACycleFinishedRx = WB_REQ_O[1] & WB_ACK_I[1];
1824
 
1825
 
1826
// WbWriteError is generated when the previous word is not written to the wishbone on time
1827
always @ (posedge WB_CLK_I or posedge WB_RST_I)
1828
begin
1829
  if(WB_RST_I)
1830
    WbWriteError <=#Tp 1'b0;
1831
  else
1832
  if(LatchNow_wb & ~RxDataValid_wb)
1833
    begin
1834
      if(WB_REQ_O[1] & ~WB_ACK_I[1])
1835
        WbWriteError <=#Tp 1'b1;
1836
    end
1837
  else
1838
  if(RxStartFrm_wb)
1839
    WbWriteError <=#Tp 1'b0;
1840
end
1841
 
1842
 
1843
// Assembling data that will be written to the WISHBONE
1844
always @ (posedge WB_CLK_I or posedge WB_RST_I)
1845
begin
1846
  if(WB_RST_I)
1847
    RxData_wb <=#Tp 32'h0;
1848
  else
1849
  if(LatchNow_wb & ~RxDataValid_wb & ~ShiftWillEnd)
1850
    RxData_wb <=#Tp RxDataLatched2;
1851
  else
1852
  if(LatchNow_wb & ~RxDataValid_wb & ShiftWillEnd)
1853
    case(RxValidBytes)
1854
 
1855
      1 : RxData_wb <=#Tp {24'h0,                       RxDataLatched1[7:0]};
1856
      2 : RxData_wb <=#Tp {16'h0,                       RxDataLatched1[15:0]};
1857
      3 : RxData_wb <=#Tp {8'h0, RxDataLatched2[23:16], RxDataLatched1[15:0]};
1858
    endcase
1859
end
1860
 
1861
 
1862
// Selecting the data for the WISHBONE
1863 39 mohor
//assign WB_DAT_O[31:0] = BDRead? WB_BDDataOut : RxData_wb;
1864 38 mohor
 
1865
 
1866
// Generation of the end-of-frame signal
1867
always @ (posedge WB_CLK_I or posedge WB_RST_I)
1868
begin
1869
  if(WB_RST_I)
1870
    RxEndFrm_wb <=#Tp 1'b0;
1871
  else
1872
  if(LatchNow_wb & ~RxDataValid_wb & ShiftWillEnd)
1873
    RxEndFrm_wb <=#Tp 1'b1;
1874
  else
1875
  if(StartRxStatusWrite)
1876
    RxEndFrm_wb <=#Tp 1'b0;
1877
end
1878
 
1879
 
1880
// Interrupts
1881
assign TxB_IRQ = 1'b0;
1882
assign TxE_IRQ = 1'b0;
1883
assign RxB_IRQ = 1'b0;
1884
assign RxF_IRQ = 1'b0;
1885
assign Busy_IRQ = 1'b0;
1886
 
1887
 
1888
endmodule
1889
 

powered by: WebSVN 2.1.0

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