summaryrefslogtreecommitdiffhomepage
path: root/source/switch.v
blob: 98aec4aed01c076ec7873eacf69d93fcd06ef19c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
/*
 *       switch.v
 *
 *   Copyright 2018, 2019, 2020, 2021 Mind Chasers Inc.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 *	function: Soft Ethernet Switch
 *
 */

`timescale 1ns /10ps

module switch(
	input rstn,
	input clk,
	
	// PHY status
	input [3:0] phy_up,
	input [3:0] mode_100Mbit,
	
	// FIFO input data from RX FIFOs
	input [8:0] rx_d_01, 
	input [8:0] rx_d_02,
	input [8:0] rx_d_03, 
	input [8:0] rx_d_10, 
	input [8:0] rx_d_12,
	input [8:0] rx_d_13, 
	input [8:0] rx_d_20, 
	input [8:0] rx_d_21,
	input [8:0] rx_d_23,
	input [8:0] rx_d_2u,
	input [8:0] rx_d_30,	
	input [8:0] rx_d_31,	
	input [8:0] rx_d_32,	
	input [8:0] rx_d_u2,
	
	// RX FIFO read enables
	output reg rx_fifo_re_01, rx_fifo_re_02, rx_fifo_re_03,
	output reg rx_fifo_re_10, rx_fifo_re_12, rx_fifo_re_13,
	output reg rx_fifo_re_20, rx_fifo_re_21, rx_fifo_re_23, rx_fifo_re_2u,
	output reg rx_fifo_re_30, rx_fifo_re_31, rx_fifo_re_32, 
	output reg rx_fifo_re_u2,
	
	// RX FIFO Empty flags
	input rx_fifo_empty_01, rx_fifo_empty_02, rx_fifo_empty_03,
	input rx_fifo_empty_10, rx_fifo_empty_12, rx_fifo_empty_13,
	input rx_fifo_empty_20, rx_fifo_empty_21, rx_fifo_empty_23, rx_fifo_empty_2u,
	input rx_fifo_empty_30, rx_fifo_empty_31, rx_fifo_empty_32, 
	input rx_fifo_empty_u2,
	
	// RX Byte Count
	input [3:0] rx_wr_done,
	input [10:0] rx0_byte_cnt,
	input [10:0] rx1_byte_cnt,
	input [10:0] rx2_byte_cnt,
	input [10:0] rx3_byte_cnt,
	
	// TX FIFO output from internal muxes
	output reg [8:0] tx_d0,
	output reg [8:0] tx_d1,
	output reg [8:0] tx_d2,
	output reg [8:0] tx_d3,
	output [8:0] tx_du,
	
	// TX FIFO read enable inputs (need to route to RX output FIFOs)
	input [3:0] tx_fifo_re,
	output reg tx_fifo_we_u,
	
	// TX FIFO Empty Flags (need to route to RX output FIFOs)
	output reg [3:0] tx_fifo_empty,
	
	// TX modes for the PHYs and uc
	output reg [2:0] tx_mode0,
	output reg [2:0] tx_mode1,	
	output reg [2:0] tx_mode2,
	output reg [2:0] tx_mode3,
	output reg tx_modeu,
	
	// TX byte cnt
	output reg [10:0] tx0_byte_cnt,
	output reg [10:0] tx1_byte_cnt,
	output reg [10:0] tx2_byte_cnt,
	output reg [10:0] tx3_byte_cnt,
	
	// TX Source Select
	output reg [2:0] tx_src_sel0,
	output reg [2:0] tx_src_sel1,
	output reg [2:0] tx_src_sel2,
	output reg [2:0] tx_src_sel3,
	
	// TX state machine done flag
	input [3:0] tx_f,
	
	// TX custom packet
	input tx_custom
);

// IPG for Port 0
wire ipg_met;
reg [6:0] ipg_cnt;
reg [3:0] fr_100mbit_cnt;

reg i_tx_fifo_we_u;

reg [10:0] i_rx0_byte_cnt, i_rx1_byte_cnt, i_rx2_byte_cnt, i_rx3_byte_cnt;

`include "ethernet_params.v"
`include "sgmii_params.v"
			
localparam  SEL_PHY0 = 3'b000,
			SEL_PHY1 = 3'b001,
			SEL_PHY2 = 3'b010,
			SEL_PHY3 = 3'b011,
			SEL_UC =   3'b111;


// capture the rx_byte_cnt values when write is done.  TODO: needs to be a shallow Q to match FIFO
always @(posedge clk, negedge rstn)
	if ( !rstn )
		i_rx0_byte_cnt <= 'h0;
	else if ( rx_wr_done[0])
		i_rx0_byte_cnt <= rx0_byte_cnt;
	
always @(posedge clk, negedge rstn)
	if ( !rstn )
		i_rx1_byte_cnt <= 'h0;
	else if ( rx_wr_done[1])
		i_rx1_byte_cnt <= rx1_byte_cnt;
		
always @(posedge clk, negedge rstn)
	if ( !rstn )
		i_rx2_byte_cnt <= 'h0;
	else if ( rx_wr_done[2])
		i_rx2_byte_cnt <= rx2_byte_cnt;
			
always @(posedge clk, negedge rstn)
	if ( !rstn )
		i_rx3_byte_cnt <= 'h0;
	else if ( rx_wr_done[3])
		i_rx3_byte_cnt <= rx3_byte_cnt;

assign ipg_met = ipg_cnt >= IPG ? 1'b1 : 1'b0;
	
/* free running 100Mbit counter */
always @(posedge clk, negedge rstn) 
	if ( !rstn )
		fr_100mbit_cnt <= 4'd0;
	else if ( fr_100mbit_cnt == 4'd9 )
		fr_100mbit_cnt <= 4'd0;
	else
		fr_100mbit_cnt <= fr_100mbit_cnt + 1;
	
/* IPG counter */
always @(posedge clk, negedge rstn) 
	if ( !rstn )
		ipg_cnt <= 7'd0;
	else if ( tx_f[0] && tx_mode0 >= TX_MODE_XMT_PKT )
		ipg_cnt <= 7'd0;
	else if ( mode_100Mbit[0] && fr_100mbit_cnt == 4'd9 && !ipg_met )
		ipg_cnt <= ipg_cnt + 1;
	else if ( !mode_100Mbit[0] && !ipg_met )
		ipg_cnt <= ipg_cnt + 1;


// TX0 Switch Logic
// Possible sources: 1, 2
always @(posedge clk, negedge rstn)
	if ( !rstn )
	begin
		tx_mode0 <= TX_MODE_AN;
		tx_src_sel0 <= SEL_PHY1;
		tx0_byte_cnt <= 'h0;
	end
	else if ( tx_f[0] )
		case( tx_mode0 )
			TX_MODE_AN: 
				if ( phy_up[0] )
					tx_mode0 <= TX_MODE_IDLE;
			TX_MODE_IDLE: 
				if ( !phy_up[0] ) 
					tx_mode0 <= TX_MODE_AN;
				else if ( !ipg_met )
					tx_mode0 <= TX_MODE_IDLE;
				else if (tx_src_sel0==SEL_PHY1  && !rx_fifo_empty_20 )
				begin
					tx_mode0 <= TX_MODE_XMT_CUSTOM;
					tx_src_sel0 <= SEL_PHY2;
					tx0_byte_cnt <= i_rx2_byte_cnt;
				end
				else if (!rx_fifo_empty_10 )
				begin
					tx_mode0 <= TX_MODE_XMT_PKT;
					tx_src_sel0 <= SEL_PHY1;
					tx0_byte_cnt <= i_rx1_byte_cnt;
				end	
				else if (!rx_fifo_empty_20 )
				begin
					tx_mode0 <= TX_MODE_XMT_CUSTOM;
					tx_src_sel0 <= SEL_PHY2;
					tx0_byte_cnt <= i_rx2_byte_cnt;
				end
			TX_MODE_XMT_PKT: 
				if ( !phy_up[0] )
					tx_mode0 <= TX_MODE_AN;
				else
					tx_mode0 <= TX_MODE_IDLE;
			default: tx_mode0 <= TX_MODE_IDLE;
		endcase
	
// TX0 data mux
always @(*) begin
	case(tx_src_sel0)
		SEL_PHY0: tx_d0 = 9'h000;
		SEL_PHY1: tx_d0 = rx_d_10;
		SEL_PHY2: tx_d0 = rx_d_20;		
		SEL_UC:   tx_d0 = 9'h000;
		default:  tx_d0 = 9'h000;
	endcase
end

// TX0 FIFO read enable
always @(*) begin
	rx_fifo_re_10 = 1'b0;
	rx_fifo_re_20 = 1'b0;
	rx_fifo_re_30 = 1'b0;
	case(tx_src_sel0)
		SEL_PHY1: rx_fifo_re_10 = tx_fifo_re[0];
		SEL_PHY2: rx_fifo_re_20 = tx_fifo_re[0];
		SEL_PHY3: rx_fifo_re_30 = tx_fifo_re[0];
	endcase
end

// TX0 FIFO Empty Routing
always @(*) begin
	case(tx_src_sel0)
		SEL_PHY1: tx_fifo_empty[0] = rx_fifo_empty_10;
		SEL_PHY2: tx_fifo_empty[0] = rx_fifo_empty_20;
		SEL_PHY3: tx_fifo_empty[0] = rx_fifo_empty_30;
		default: tx_fifo_empty[0]  = 1'b1;
	endcase
end

// TX1 Switch Logic
// Possible sources: 0, 2, 3 in priority
always @(posedge clk, negedge rstn)
	if ( !rstn )
	begin
		tx_mode1 <= TX_MODE_AN;
		tx_src_sel1 <= SEL_PHY0;
	end
	else if ( tx_f[1] )
		case( tx_mode1 )
			TX_MODE_AN: 
				if ( phy_up[1] ) 
					tx_mode1 <= TX_MODE_IDLE;
			TX_MODE_IDLE: 
				if ( !phy_up[1] ) 
					tx_mode1 <= TX_MODE_AN;
				else if (tx_src_sel1==SEL_PHY0  && !rx_fifo_empty_21 )
				begin
					tx_mode1 <= TX_MODE_XMT_PKT;
					tx_src_sel1 <= SEL_PHY2;
				end
				else if (tx_src_sel1==SEL_PHY0  && !rx_fifo_empty_31 )
				begin
					tx_mode1 <= TX_MODE_XMT_PKT;
					tx_src_sel1 <= SEL_PHY3;
				end
				else if (!rx_fifo_empty_01 )
				begin
					tx_mode1 <= TX_MODE_XMT_PKT;
					tx_src_sel1 <= SEL_PHY0;
				end
				else if (!rx_fifo_empty_21 )
				begin
					tx_mode1 <= TX_MODE_XMT_PKT;
					tx_src_sel1 <= SEL_PHY2;
				end	
				else if (!rx_fifo_empty_31 )
				begin
					tx_mode1 <= TX_MODE_XMT_PKT;
					tx_src_sel1 <= SEL_PHY3;
				end	
			TX_MODE_XMT_PKT: 
				if ( !phy_up[1] )
					tx_mode1 <= TX_MODE_AN;
				else
					tx_mode1 <= TX_MODE_IDLE;
			default: tx_mode1 <= TX_MODE_IDLE;
		endcase
		
// TX1 data mux
always @(*) begin
	case(tx_src_sel1)
		SEL_PHY0: tx_d1 = rx_d_01;
		SEL_PHY1: tx_d1 = 9'h000;
		SEL_PHY2: tx_d1 = rx_d_21;
		SEL_PHY3: tx_d1 = rx_d_31;
		SEL_UC:   tx_d1 = 9'h000;
		default:  tx_d1 = 9'h000;
	endcase
end

// TX1 FIFO read enable
always @(*) begin
	rx_fifo_re_01 = 1'b0;
	rx_fifo_re_21 = 1'b0;
	rx_fifo_re_31 = 1'b0;
	case(tx_src_sel1)
		SEL_PHY0: rx_fifo_re_01 = tx_fifo_re[1];
		SEL_PHY2: rx_fifo_re_21 = tx_fifo_re[1];
		SEL_PHY3: rx_fifo_re_31 = tx_fifo_re[1];
	endcase
end

// TX1 FIFO Empty Routing
always @(*) begin
	case(tx_src_sel1)
		SEL_PHY0: tx_fifo_empty[1] = rx_fifo_empty_01;
		SEL_PHY1: tx_fifo_empty[1] = 1'b1;
		SEL_PHY2: tx_fifo_empty[1] = rx_fifo_empty_21;
		SEL_PHY3: tx_fifo_empty[1] = rx_fifo_empty_31;
		SEL_UC:   tx_fifo_empty[1] = 1'b1;
		default:  tx_fifo_empty[1] = 1'b1;	
	endcase
end	

	
/* 	
 * 	TX2 Switch Logic
 * 	Possible Sources: 0, 1, UC
 * 	Note that for TX_MODE_XMT_METRICS, we set the source to be loopback, but it is expected that the 
 * 	phy controller will use a combination of local memory and the metrics block for its data.  These decisions 
 * 	should be revisited when implementing loopback within this switch module.
 */ 
always @(posedge clk, negedge rstn)
	if ( !rstn )
		begin
			tx_mode2 <= TX_MODE_AN;
			tx_src_sel2 <= SEL_PHY0;
		end
	else if ( tx_f[2] )
		case( tx_mode2 )
			TX_MODE_AN: 
				if ( phy_up[2] ) 
					tx_mode2 <= TX_MODE_IDLE;
			TX_MODE_IDLE: 
				if ( !phy_up[2] ) 
					tx_mode2 <= TX_MODE_AN;
				else if (tx_src_sel2==SEL_PHY0  && !rx_fifo_empty_12 )
					begin
						tx_mode2 <= TX_MODE_XMT_PKT;
						tx_src_sel2 <= SEL_PHY1;
					end
				else if (!rx_fifo_empty_02 )
					begin
						tx_mode2 <= TX_MODE_XMT_PKT;
						tx_src_sel2 <= SEL_PHY0;
					end	
				else if (!rx_fifo_empty_12 )
					begin
						tx_mode2 <= TX_MODE_XMT_PKT;
						tx_src_sel2 <= SEL_PHY1;
					end
				else if (!rx_fifo_empty_u2)
					begin
						tx_mode2 <= TX_MODE_XMT_PKT;
						tx_src_sel2 <= SEL_UC;
					end
				else if (tx_custom)
					begin
						tx_mode2 <= TX_MODE_XMT_CUSTOM;
						tx_src_sel2 <= SEL_PHY2;
					end
			TX_MODE_XMT_PKT: 
				if ( !phy_up[2] )
					tx_mode2 <= TX_MODE_AN;
				else
					tx_mode2 <= TX_MODE_IDLE;
			default: tx_mode2 <= TX_MODE_IDLE;
		endcase
	
// TX2 data mux
always @(*) begin
	case(tx_src_sel2)
		SEL_PHY0: tx_d2 = rx_d_02;
		SEL_PHY1: tx_d2 = rx_d_12;
		SEL_PHY2: tx_d2 = 9'h000;
		SEL_UC:   tx_d2 = rx_d_u2;
		default:  tx_d2 = 9'h000;
	endcase
end

// TX2 FIFO read enable
always @(*) begin
	rx_fifo_re_02 = 1'b0;
	rx_fifo_re_12 = 1'b0;
	rx_fifo_re_u2 = 1'b0;
	case(tx_src_sel2)
		SEL_PHY0: rx_fifo_re_02 = tx_fifo_re[2];
		SEL_PHY1: rx_fifo_re_12 = tx_fifo_re[2];
		SEL_UC:   rx_fifo_re_u2 = tx_fifo_re[2];
	endcase
end
		
// TX2 FIFO Empty Routing
always @(*) begin
	case(tx_src_sel2)
		SEL_PHY0: tx_fifo_empty[2] = rx_fifo_empty_02;
		SEL_PHY1: tx_fifo_empty[2] = rx_fifo_empty_12;
		SEL_PHY2: tx_fifo_empty[2] = 1'b0;					//   
		SEL_UC:   tx_fifo_empty[2] = rx_fifo_empty_u2;
		default:  tx_fifo_empty[2] = 1'b1;
	endcase
end
	
	
// TX3 Switch Logic
// Possible sources: 0, 1
always @(posedge clk, negedge rstn)
	if ( !rstn )
	begin
		tx_mode3 <= TX_MODE_AN;
		tx_src_sel3 <= SEL_PHY1;
	end
	else if ( tx_f[3] )
		case( tx_mode3 )
			TX_MODE_AN: 
				if ( phy_up[3] ) 
					tx_mode3 <= TX_MODE_IDLE;
			TX_MODE_IDLE: 
				if ( !phy_up[3] ) 
					tx_mode3 <= TX_MODE_AN;
				else if (tx_src_sel3==SEL_PHY1  && !rx_fifo_empty_03 )
				begin
					tx_mode3 <= TX_MODE_XMT_PKT;
					tx_src_sel3 <= SEL_PHY0;
				end
				else if (!rx_fifo_empty_13 )
				begin
					tx_mode3 <= TX_MODE_XMT_PKT;
					tx_src_sel3 <= SEL_PHY1;
				end	
				else if (!rx_fifo_empty_03 )
				begin
					tx_mode3 <= TX_MODE_XMT_PKT;
					tx_src_sel3 <= SEL_PHY0;
				end
			TX_MODE_XMT_PKT: 
				if ( !phy_up[3] )
					tx_mode3 <= TX_MODE_AN;
				else
					tx_mode3 <= TX_MODE_IDLE;
			default: tx_mode3 <= TX_MODE_IDLE;
		endcase

	// TX3 data mux
	always @(*) begin
		case(tx_src_sel3)
			SEL_PHY0: tx_d3 = rx_d_03;
			SEL_PHY1: tx_d3 = rx_d_13;
			SEL_PHY2: tx_d3 = rx_d_23;
			SEL_PHY3: tx_d3 = 9'h000;		
			SEL_UC:   tx_d3 = 9'h000;
			default:  tx_d3 = 9'h000;	
		endcase
	end

	// TX3 FIFO read enable
	always @(*) begin
		rx_fifo_re_03 = 1'b0;
		rx_fifo_re_13 = 1'b0;
		rx_fifo_re_23 = 1'b0;
		case(tx_src_sel3)
			SEL_PHY0: rx_fifo_re_03 = tx_fifo_re[3];
			SEL_PHY1: rx_fifo_re_13 = tx_fifo_re[3];
			SEL_PHY2: rx_fifo_re_23 = tx_fifo_re[3];
		endcase
	end

	// TX3 FIFO Empty Routing
	always @(*) begin
		case(tx_src_sel3)
			SEL_PHY0: tx_fifo_empty[3] = rx_fifo_empty_03;
			SEL_PHY1: tx_fifo_empty[3] = rx_fifo_empty_13;
			SEL_PHY2: tx_fifo_empty[3] = rx_fifo_empty_23;
			default: tx_fifo_empty[3]  = 1'b1;
		endcase
	end

/* 
 *	Transmit Logic for UC	
 *	
 *	The only possible driver is PHY2
 *
 *	We need to delay the fifo_we one clock since the DPRAM read data comes out one clock delayed
 */
	
assign tx_du = rx_d_2u;

always @(*)
	if ( !rx_fifo_empty_2u )
		begin
			i_tx_fifo_we_u = 1'b1;
			rx_fifo_re_2u = 1'b1;
		end
	else
		begin
			i_tx_fifo_we_u = 1'b0;
			rx_fifo_re_2u = 1'b0; 
		end
		
always @(posedge clk, negedge rstn)
	if ( !rstn )
		tx_fifo_we_u <= 1'b0;
	else
		tx_fifo_we_u <= i_tx_fifo_we_u;

	
endmodule